[PATCH] x86-64: Simplify the vector allocator.
[linux-2.6/kmemtrace.git] / fs / reiserfs / namei.c
blobabde1edc2235605c9a4a9680cfdcafbf830f6a12
1 /*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
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.
11 * NO WARRANTY
14 #include <linux/time.h>
15 #include <linux/bitops.h>
16 #include <linux/reiserfs_fs.h>
17 #include <linux/reiserfs_acl.h>
18 #include <linux/reiserfs_xattr.h>
19 #include <linux/smp_lock.h>
20 #include <linux/quotaops.h>
22 #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) i->i_nlink=1; }
23 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
25 // directory item contains array of entry headers. This performs
26 // binary search through that array
27 static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
29 struct item_head *ih = de->de_ih;
30 struct reiserfs_de_head *deh = de->de_deh;
31 int rbound, lbound, j;
33 lbound = 0;
34 rbound = I_ENTRY_COUNT(ih) - 1;
36 for (j = (rbound + lbound) / 2; lbound <= rbound;
37 j = (rbound + lbound) / 2) {
38 if (off < deh_offset(deh + j)) {
39 rbound = j - 1;
40 continue;
42 if (off > deh_offset(deh + j)) {
43 lbound = j + 1;
44 continue;
46 // this is not name found, but matched third key component
47 de->de_entry_num = j;
48 return NAME_FOUND;
51 de->de_entry_num = lbound;
52 return NAME_NOT_FOUND;
55 // comment? maybe something like set de to point to what the path points to?
56 static inline void set_de_item_location(struct reiserfs_dir_entry *de,
57 struct path *path)
59 de->de_bh = get_last_bh(path);
60 de->de_ih = get_ih(path);
61 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
62 de->de_item_num = PATH_LAST_POSITION(path);
65 // de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
66 inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
68 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
70 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
72 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
73 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
74 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
75 if (de->de_name[de->de_namelen - 1] == 0)
76 de->de_namelen = strlen(de->de_name);
79 // what entry points to
80 static inline void set_de_object_key(struct reiserfs_dir_entry *de)
82 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
83 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
84 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
87 static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
89 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
91 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
93 /* store key of the found entry */
94 de->de_entry_key.version = KEY_FORMAT_3_5;
95 de->de_entry_key.on_disk_key.k_dir_id =
96 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
97 de->de_entry_key.on_disk_key.k_objectid =
98 le32_to_cpu(de->de_ih->ih_key.k_objectid);
99 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
100 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
103 /* We assign a key to each directory item, and place multiple entries
104 in a single directory item. A directory item has a key equal to the
105 key of the first directory entry in it.
107 This function first calls search_by_key, then, if item whose first
108 entry matches is not found it looks for the entry inside directory
109 item found by search_by_key. Fills the path to the entry, and to the
110 entry position in the item
114 /* The function is NOT SCHEDULE-SAFE! */
115 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
116 struct path *path, struct reiserfs_dir_entry *de)
118 int retval;
120 retval = search_item(sb, key, path);
121 switch (retval) {
122 case ITEM_NOT_FOUND:
123 if (!PATH_LAST_POSITION(path)) {
124 reiserfs_warning(sb,
125 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
126 pathrelse(path);
127 return IO_ERROR;
129 PATH_LAST_POSITION(path)--;
131 case ITEM_FOUND:
132 break;
134 case IO_ERROR:
135 return retval;
137 default:
138 pathrelse(path);
139 reiserfs_warning(sb,
140 "vs-7002: search_by_entry_key: no path to here");
141 return IO_ERROR;
144 set_de_item_location(de, path);
146 #ifdef CONFIG_REISERFS_CHECK
147 if (!is_direntry_le_ih(de->de_ih) ||
148 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
149 print_block(de->de_bh, 0, -1, -1);
150 reiserfs_panic(sb,
151 "vs-7005: search_by_entry_key: found item %h is not directory item or "
152 "does not belong to the same directory as key %K",
153 de->de_ih, key);
155 #endif /* CONFIG_REISERFS_CHECK */
157 /* binary search in directory item by third componen t of the
158 key. sets de->de_entry_num of de */
159 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
160 path->pos_in_item = de->de_entry_num;
161 if (retval != NAME_NOT_FOUND) {
162 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
163 set_de_name_and_namelen(de);
164 set_de_object_key(de);
166 return retval;
169 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
171 /* The third component is hashed, and you can choose from more than
172 one hash function. Per directory hashes are not yet implemented
173 but are thought about. This function should be moved to hashes.c
174 Jedi, please do so. -Hans */
176 static __u32 get_third_component(struct super_block *s,
177 const char *name, int len)
179 __u32 res;
181 if (!len || (len == 1 && name[0] == '.'))
182 return DOT_OFFSET;
183 if (len == 2 && name[0] == '.' && name[1] == '.')
184 return DOT_DOT_OFFSET;
186 res = REISERFS_SB(s)->s_hash_function(name, len);
188 // take bits from 7-th to 30-th including both bounds
189 res = GET_HASH_VALUE(res);
190 if (res == 0)
191 // needed to have no names before "." and ".." those have hash
192 // value == 0 and generation conters 1 and 2 accordingly
193 res = 128;
194 return res + MAX_GENERATION_NUMBER;
197 static int reiserfs_match(struct reiserfs_dir_entry *de,
198 const char *name, int namelen)
200 int retval = NAME_NOT_FOUND;
202 if ((namelen == de->de_namelen) &&
203 !memcmp(de->de_name, name, de->de_namelen))
204 retval =
205 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
206 NAME_FOUND_INVISIBLE);
208 return retval;
211 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
213 /* used when hash collisions exist */
215 static int linear_search_in_dir_item(struct cpu_key *key,
216 struct reiserfs_dir_entry *de,
217 const char *name, int namelen)
219 struct reiserfs_de_head *deh = de->de_deh;
220 int retval;
221 int i;
223 i = de->de_entry_num;
225 if (i == I_ENTRY_COUNT(de->de_ih) ||
226 GET_HASH_VALUE(deh_offset(deh + i)) !=
227 GET_HASH_VALUE(cpu_key_k_offset(key))) {
228 i--;
231 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
232 "vs-7010: array of entry headers not found");
234 deh += i;
236 for (; i >= 0; i--, deh--) {
237 if (GET_HASH_VALUE(deh_offset(deh)) !=
238 GET_HASH_VALUE(cpu_key_k_offset(key))) {
239 // hash value does not match, no need to check whole name
240 return NAME_NOT_FOUND;
243 /* mark, that this generation number is used */
244 if (de->de_gen_number_bit_string)
245 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
246 de->de_gen_number_bit_string);
248 // calculate pointer to name and namelen
249 de->de_entry_num = i;
250 set_de_name_and_namelen(de);
252 if ((retval =
253 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
254 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
256 // key of pointed object
257 set_de_object_key(de);
259 store_de_entry_key(de);
261 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
262 return retval;
266 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
267 /* we have reached left most entry in the node. In common we
268 have to go to the left neighbor, but if generation counter
269 is 0 already, we know for sure, that there is no name with
270 the same hash value */
271 // FIXME: this work correctly only because hash value can not
272 // be 0. Btw, in case of Yura's hash it is probably possible,
273 // so, this is a bug
274 return NAME_NOT_FOUND;
276 RFALSE(de->de_item_num,
277 "vs-7015: two diritems of the same directory in one node?");
279 return GOTO_PREVIOUS_ITEM;
282 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
283 // FIXME: should add something like IOERROR
284 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
285 struct path *path_to_entry,
286 struct reiserfs_dir_entry *de)
288 struct cpu_key key_to_search;
289 int retval;
291 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
292 return NAME_NOT_FOUND;
294 /* we will search for this key in the tree */
295 make_cpu_key(&key_to_search, dir,
296 get_third_component(dir->i_sb, name, namelen),
297 TYPE_DIRENTRY, 3);
299 while (1) {
300 retval =
301 search_by_entry_key(dir->i_sb, &key_to_search,
302 path_to_entry, de);
303 if (retval == IO_ERROR) {
304 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
305 __FUNCTION__);
306 return IO_ERROR;
309 /* compare names for all entries having given hash value */
310 retval =
311 linear_search_in_dir_item(&key_to_search, de, name,
312 namelen);
313 if (retval != GOTO_PREVIOUS_ITEM) {
314 /* there is no need to scan directory anymore. Given entry found or does not exist */
315 path_to_entry->pos_in_item = de->de_entry_num;
316 return retval;
319 /* there is left neighboring item of this directory and given entry can be there */
320 set_cpu_key_k_offset(&key_to_search,
321 le_ih_k_offset(de->de_ih) - 1);
322 pathrelse(path_to_entry);
324 } /* while (1) */
327 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
328 struct nameidata *nd)
330 int retval;
331 struct inode *inode = NULL;
332 struct reiserfs_dir_entry de;
333 INITIALIZE_PATH(path_to_entry);
335 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
336 return ERR_PTR(-ENAMETOOLONG);
338 reiserfs_write_lock(dir->i_sb);
339 de.de_gen_number_bit_string = NULL;
340 retval =
341 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
342 &path_to_entry, &de);
343 pathrelse(&path_to_entry);
344 if (retval == NAME_FOUND) {
345 /* Hide the .reiserfs_priv directory */
346 if (reiserfs_xattrs(dir->i_sb) &&
347 !old_format_only(dir->i_sb) &&
348 REISERFS_SB(dir->i_sb)->priv_root &&
349 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
350 de.de_objectid ==
351 le32_to_cpu(INODE_PKEY
352 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
353 k_objectid)) {
354 reiserfs_write_unlock(dir->i_sb);
355 return ERR_PTR(-EACCES);
358 inode =
359 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
360 if (!inode || IS_ERR(inode)) {
361 reiserfs_write_unlock(dir->i_sb);
362 return ERR_PTR(-EACCES);
365 /* Propogate the priv_object flag so we know we're in the priv tree */
366 if (is_reiserfs_priv_object(dir))
367 reiserfs_mark_inode_private(inode);
369 reiserfs_write_unlock(dir->i_sb);
370 if (retval == IO_ERROR) {
371 return ERR_PTR(-EIO);
374 return d_splice_alias(inode, dentry);
378 ** looks up the dentry of the parent directory for child.
379 ** taken from ext2_get_parent
381 struct dentry *reiserfs_get_parent(struct dentry *child)
383 int retval;
384 struct inode *inode = NULL;
385 struct reiserfs_dir_entry de;
386 INITIALIZE_PATH(path_to_entry);
387 struct dentry *parent;
388 struct inode *dir = child->d_inode;
390 if (dir->i_nlink == 0) {
391 return ERR_PTR(-ENOENT);
393 de.de_gen_number_bit_string = NULL;
395 reiserfs_write_lock(dir->i_sb);
396 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
397 pathrelse(&path_to_entry);
398 if (retval != NAME_FOUND) {
399 reiserfs_write_unlock(dir->i_sb);
400 return ERR_PTR(-ENOENT);
402 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
403 reiserfs_write_unlock(dir->i_sb);
405 if (!inode || IS_ERR(inode)) {
406 return ERR_PTR(-EACCES);
408 parent = d_alloc_anon(inode);
409 if (!parent) {
410 iput(inode);
411 parent = ERR_PTR(-ENOMEM);
413 return parent;
416 /* add entry to the directory (entry can be hidden).
418 insert definition of when hidden directories are used here -Hans
420 Does not mark dir inode dirty, do it after successesfull call to it */
422 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
423 struct inode *dir, const char *name, int namelen,
424 struct inode *inode, int visible)
426 struct cpu_key entry_key;
427 struct reiserfs_de_head *deh;
428 INITIALIZE_PATH(path);
429 struct reiserfs_dir_entry de;
430 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
431 int gen_number;
432 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
433 if we create file with short name */
434 char *buffer;
435 int buflen, paste_size;
436 int retval;
438 BUG_ON(!th->t_trans_id);
440 /* cannot allow items to be added into a busy deleted directory */
441 if (!namelen)
442 return -EINVAL;
444 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
445 return -ENAMETOOLONG;
447 /* each entry has unique key. compose it */
448 make_cpu_key(&entry_key, dir,
449 get_third_component(dir->i_sb, name, namelen),
450 TYPE_DIRENTRY, 3);
452 /* get memory for composing the entry */
453 buflen = DEH_SIZE + ROUND_UP(namelen);
454 if (buflen > sizeof(small_buf)) {
455 buffer = kmalloc(buflen, GFP_NOFS);
456 if (buffer == 0)
457 return -ENOMEM;
458 } else
459 buffer = small_buf;
461 paste_size =
462 (get_inode_sd_version(dir) ==
463 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
465 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
466 deh = (struct reiserfs_de_head *)buffer;
467 deh->deh_location = 0; /* JDM Endian safe if 0 */
468 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
469 deh->deh_state = 0; /* JDM Endian safe if 0 */
470 /* put key (ino analog) to de */
471 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
472 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
474 /* copy name */
475 memcpy((char *)(deh + 1), name, namelen);
476 /* padd by 0s to the 4 byte boundary */
477 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
479 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
480 mark_de_without_sd(deh);
481 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
483 /* find the proper place for the new entry */
484 memset(bit_string, 0, sizeof(bit_string));
485 de.de_gen_number_bit_string = bit_string;
486 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
487 if (retval != NAME_NOT_FOUND) {
488 if (buffer != small_buf)
489 kfree(buffer);
490 pathrelse(&path);
492 if (retval == IO_ERROR) {
493 return -EIO;
496 if (retval != NAME_FOUND) {
497 reiserfs_warning(dir->i_sb,
498 "zam-7002:%s: \"reiserfs_find_entry\" "
499 "has returned unexpected value (%d)",
500 __FUNCTION__, retval);
503 return -EEXIST;
506 gen_number =
507 find_first_zero_bit(bit_string,
508 MAX_GENERATION_NUMBER + 1);
509 if (gen_number > MAX_GENERATION_NUMBER) {
510 /* there is no free generation number */
511 reiserfs_warning(dir->i_sb,
512 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
513 if (buffer != small_buf)
514 kfree(buffer);
515 pathrelse(&path);
516 return -EBUSY;
518 /* adjust offset of directory enrty */
519 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
520 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
522 /* update max-hash-collisions counter in reiserfs_sb_info */
523 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
525 if (gen_number != 0) { /* we need to re-search for the insertion point */
526 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
527 NAME_NOT_FOUND) {
528 reiserfs_warning(dir->i_sb,
529 "vs-7032: reiserfs_add_entry: "
530 "entry with this key (%K) already exists",
531 &entry_key);
533 if (buffer != small_buf)
534 kfree(buffer);
535 pathrelse(&path);
536 return -EBUSY;
540 /* perform the insertion of the entry that we have prepared */
541 retval =
542 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
543 paste_size);
544 if (buffer != small_buf)
545 kfree(buffer);
546 if (retval) {
547 reiserfs_check_path(&path);
548 return retval;
551 dir->i_size += paste_size;
552 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
553 if (!S_ISDIR(inode->i_mode) && visible)
554 // reiserfs_mkdir or reiserfs_rename will do that by itself
555 reiserfs_update_sd(th, dir);
557 reiserfs_check_path(&path);
558 return 0;
561 /* quota utility function, call if you've had to abort after calling
562 ** new_inode_init, and have not called reiserfs_new_inode yet.
563 ** This should only be called on inodes that do not have stat data
564 ** inserted into the tree yet.
566 static int drop_new_inode(struct inode *inode)
568 DQUOT_DROP(inode);
569 make_bad_inode(inode);
570 inode->i_flags |= S_NOQUOTA;
571 iput(inode);
572 return 0;
575 /* utility function that does setup for reiserfs_new_inode.
576 ** DQUOT_INIT needs lots of credits so it's better to have it
577 ** outside of a transaction, so we had to pull some bits of
578 ** reiserfs_new_inode out into this func.
580 static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
583 /* the quota init calls have to know who to charge the quota to, so
584 ** we have to set uid and gid here
586 inode->i_uid = current->fsuid;
587 inode->i_mode = mode;
588 /* Make inode invalid - just in case we are going to drop it before
589 * the initialization happens */
590 INODE_PKEY(inode)->k_objectid = 0;
592 if (dir->i_mode & S_ISGID) {
593 inode->i_gid = dir->i_gid;
594 if (S_ISDIR(mode))
595 inode->i_mode |= S_ISGID;
596 } else {
597 inode->i_gid = current->fsgid;
599 DQUOT_INIT(inode);
600 return 0;
603 static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
604 struct nameidata *nd)
606 int retval;
607 struct inode *inode;
608 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
609 int jbegin_count =
610 JOURNAL_PER_BALANCE_CNT * 2 +
611 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
612 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
613 struct reiserfs_transaction_handle th;
614 int locked;
616 if (!(inode = new_inode(dir->i_sb))) {
617 return -ENOMEM;
619 new_inode_init(inode, dir, mode);
621 locked = reiserfs_cache_default_acl(dir);
623 reiserfs_write_lock(dir->i_sb);
625 if (locked)
626 reiserfs_write_lock_xattrs(dir->i_sb);
628 retval = journal_begin(&th, dir->i_sb, jbegin_count);
629 if (retval) {
630 drop_new_inode(inode);
631 goto out_failed;
634 retval =
635 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
636 inode);
637 if (retval)
638 goto out_failed;
640 if (locked) {
641 reiserfs_write_unlock_xattrs(dir->i_sb);
642 locked = 0;
645 inode->i_op = &reiserfs_file_inode_operations;
646 inode->i_fop = &reiserfs_file_operations;
647 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
649 retval =
650 reiserfs_add_entry(&th, dir, dentry->d_name.name,
651 dentry->d_name.len, inode, 1 /*visible */ );
652 if (retval) {
653 int err;
654 inode->i_nlink--;
655 reiserfs_update_sd(&th, inode);
656 err = journal_end(&th, dir->i_sb, jbegin_count);
657 if (err)
658 retval = err;
659 iput(inode);
660 goto out_failed;
662 reiserfs_update_inode_transaction(inode);
663 reiserfs_update_inode_transaction(dir);
665 d_instantiate(dentry, inode);
666 retval = journal_end(&th, dir->i_sb, jbegin_count);
668 out_failed:
669 if (locked)
670 reiserfs_write_unlock_xattrs(dir->i_sb);
671 reiserfs_write_unlock(dir->i_sb);
672 return retval;
675 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
676 dev_t rdev)
678 int retval;
679 struct inode *inode;
680 struct reiserfs_transaction_handle th;
681 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
682 int jbegin_count =
683 JOURNAL_PER_BALANCE_CNT * 3 +
684 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
685 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
686 int locked;
688 if (!new_valid_dev(rdev))
689 return -EINVAL;
691 if (!(inode = new_inode(dir->i_sb))) {
692 return -ENOMEM;
694 new_inode_init(inode, dir, mode);
696 locked = reiserfs_cache_default_acl(dir);
698 reiserfs_write_lock(dir->i_sb);
700 if (locked)
701 reiserfs_write_lock_xattrs(dir->i_sb);
703 retval = journal_begin(&th, dir->i_sb, jbegin_count);
704 if (retval) {
705 drop_new_inode(inode);
706 goto out_failed;
709 retval =
710 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
711 inode);
712 if (retval) {
713 goto out_failed;
716 if (locked) {
717 reiserfs_write_unlock_xattrs(dir->i_sb);
718 locked = 0;
721 inode->i_op = &reiserfs_special_inode_operations;
722 init_special_inode(inode, inode->i_mode, rdev);
724 //FIXME: needed for block and char devices only
725 reiserfs_update_sd(&th, inode);
727 reiserfs_update_inode_transaction(inode);
728 reiserfs_update_inode_transaction(dir);
730 retval =
731 reiserfs_add_entry(&th, dir, dentry->d_name.name,
732 dentry->d_name.len, inode, 1 /*visible */ );
733 if (retval) {
734 int err;
735 inode->i_nlink--;
736 reiserfs_update_sd(&th, inode);
737 err = journal_end(&th, dir->i_sb, jbegin_count);
738 if (err)
739 retval = err;
740 iput(inode);
741 goto out_failed;
744 d_instantiate(dentry, inode);
745 retval = journal_end(&th, dir->i_sb, jbegin_count);
747 out_failed:
748 if (locked)
749 reiserfs_write_unlock_xattrs(dir->i_sb);
750 reiserfs_write_unlock(dir->i_sb);
751 return retval;
754 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
756 int retval;
757 struct inode *inode;
758 struct reiserfs_transaction_handle th;
759 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
760 int jbegin_count =
761 JOURNAL_PER_BALANCE_CNT * 3 +
762 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
763 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
764 int locked;
766 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
767 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
768 REISERFS_I(dir)->new_packing_locality = 1;
769 #endif
770 mode = S_IFDIR | mode;
771 if (!(inode = new_inode(dir->i_sb))) {
772 return -ENOMEM;
774 new_inode_init(inode, dir, mode);
776 locked = reiserfs_cache_default_acl(dir);
778 reiserfs_write_lock(dir->i_sb);
779 if (locked)
780 reiserfs_write_lock_xattrs(dir->i_sb);
782 retval = journal_begin(&th, dir->i_sb, jbegin_count);
783 if (retval) {
784 drop_new_inode(inode);
785 goto out_failed;
788 /* inc the link count now, so another writer doesn't overflow it while
789 ** we sleep later on.
791 INC_DIR_INODE_NLINK(dir)
793 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
794 old_format_only(dir->i_sb) ?
795 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
796 dentry, inode);
797 if (retval) {
798 dir->i_nlink--;
799 goto out_failed;
802 if (locked) {
803 reiserfs_write_unlock_xattrs(dir->i_sb);
804 locked = 0;
807 reiserfs_update_inode_transaction(inode);
808 reiserfs_update_inode_transaction(dir);
810 inode->i_op = &reiserfs_dir_inode_operations;
811 inode->i_fop = &reiserfs_dir_operations;
813 // note, _this_ add_entry will not update dir's stat data
814 retval =
815 reiserfs_add_entry(&th, dir, dentry->d_name.name,
816 dentry->d_name.len, inode, 1 /*visible */ );
817 if (retval) {
818 int err;
819 inode->i_nlink = 0;
820 DEC_DIR_INODE_NLINK(dir);
821 reiserfs_update_sd(&th, inode);
822 err = journal_end(&th, dir->i_sb, jbegin_count);
823 if (err)
824 retval = err;
825 iput(inode);
826 goto out_failed;
828 // the above add_entry did not update dir's stat data
829 reiserfs_update_sd(&th, dir);
831 d_instantiate(dentry, inode);
832 retval = journal_end(&th, dir->i_sb, jbegin_count);
833 out_failed:
834 if (locked)
835 reiserfs_write_unlock_xattrs(dir->i_sb);
836 reiserfs_write_unlock(dir->i_sb);
837 return retval;
840 static inline int reiserfs_empty_dir(struct inode *inode)
842 /* we can cheat because an old format dir cannot have
843 ** EMPTY_DIR_SIZE, and a new format dir cannot have
844 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
845 ** regardless of disk format version, the directory is empty.
847 if (inode->i_size != EMPTY_DIR_SIZE &&
848 inode->i_size != EMPTY_DIR_SIZE_V1) {
849 return 0;
851 return 1;
854 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
856 int retval, err;
857 struct inode *inode;
858 struct reiserfs_transaction_handle th;
859 int jbegin_count;
860 INITIALIZE_PATH(path);
861 struct reiserfs_dir_entry de;
863 /* we will be doing 2 balancings and update 2 stat data, we change quotas
864 * of the owner of the directory and of the owner of the parent directory.
865 * The quota structure is possibly deleted only on last iput => outside
866 * of this transaction */
867 jbegin_count =
868 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
869 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
871 reiserfs_write_lock(dir->i_sb);
872 retval = journal_begin(&th, dir->i_sb, jbegin_count);
873 if (retval)
874 goto out_rmdir;
876 de.de_gen_number_bit_string = NULL;
877 if ((retval =
878 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
879 &path, &de)) == NAME_NOT_FOUND) {
880 retval = -ENOENT;
881 goto end_rmdir;
882 } else if (retval == IO_ERROR) {
883 retval = -EIO;
884 goto end_rmdir;
887 inode = dentry->d_inode;
889 reiserfs_update_inode_transaction(inode);
890 reiserfs_update_inode_transaction(dir);
892 if (de.de_objectid != inode->i_ino) {
893 // FIXME: compare key of an object and a key found in the
894 // entry
895 retval = -EIO;
896 goto end_rmdir;
898 if (!reiserfs_empty_dir(inode)) {
899 retval = -ENOTEMPTY;
900 goto end_rmdir;
903 /* cut entry from dir directory */
904 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
905 0 /*new file size - not used here */ );
906 if (retval < 0)
907 goto end_rmdir;
909 if (inode->i_nlink != 2 && inode->i_nlink != 1)
910 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
911 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
913 clear_nlink(inode);
914 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
915 reiserfs_update_sd(&th, inode);
917 DEC_DIR_INODE_NLINK(dir)
918 dir->i_size -= (DEH_SIZE + de.de_entrylen);
919 reiserfs_update_sd(&th, dir);
921 /* prevent empty directory from getting lost */
922 add_save_link(&th, inode, 0 /* not truncate */ );
924 retval = journal_end(&th, dir->i_sb, jbegin_count);
925 reiserfs_check_path(&path);
926 out_rmdir:
927 reiserfs_write_unlock(dir->i_sb);
928 return retval;
930 end_rmdir:
931 /* we must release path, because we did not call
932 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
933 release path if operation was not complete */
934 pathrelse(&path);
935 err = journal_end(&th, dir->i_sb, jbegin_count);
936 reiserfs_write_unlock(dir->i_sb);
937 return err ? err : retval;
940 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
942 int retval, err;
943 struct inode *inode;
944 struct reiserfs_dir_entry de;
945 INITIALIZE_PATH(path);
946 struct reiserfs_transaction_handle th;
947 int jbegin_count;
948 unsigned long savelink;
950 inode = dentry->d_inode;
952 /* in this transaction we can be doing at max two balancings and update
953 * two stat datas, we change quotas of the owner of the directory and of
954 * the owner of the parent directory. The quota structure is possibly
955 * deleted only on iput => outside of this transaction */
956 jbegin_count =
957 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
958 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
960 reiserfs_write_lock(dir->i_sb);
961 retval = journal_begin(&th, dir->i_sb, jbegin_count);
962 if (retval)
963 goto out_unlink;
965 de.de_gen_number_bit_string = NULL;
966 if ((retval =
967 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
968 &path, &de)) == NAME_NOT_FOUND) {
969 retval = -ENOENT;
970 goto end_unlink;
971 } else if (retval == IO_ERROR) {
972 retval = -EIO;
973 goto end_unlink;
976 reiserfs_update_inode_transaction(inode);
977 reiserfs_update_inode_transaction(dir);
979 if (de.de_objectid != inode->i_ino) {
980 // FIXME: compare key of an object and a key found in the
981 // entry
982 retval = -EIO;
983 goto end_unlink;
986 if (!inode->i_nlink) {
987 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
988 "(%s:%lu), %d", __FUNCTION__,
989 reiserfs_bdevname(inode->i_sb), inode->i_ino,
990 inode->i_nlink);
991 inode->i_nlink = 1;
994 drop_nlink(inode);
997 * we schedule before doing the add_save_link call, save the link
998 * count so we don't race
1000 savelink = inode->i_nlink;
1002 retval =
1003 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
1005 if (retval < 0) {
1006 inc_nlink(inode);
1007 goto end_unlink;
1009 inode->i_ctime = CURRENT_TIME_SEC;
1010 reiserfs_update_sd(&th, inode);
1012 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1013 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1014 reiserfs_update_sd(&th, dir);
1016 if (!savelink)
1017 /* prevent file from getting lost */
1018 add_save_link(&th, inode, 0 /* not truncate */ );
1020 retval = journal_end(&th, dir->i_sb, jbegin_count);
1021 reiserfs_check_path(&path);
1022 reiserfs_write_unlock(dir->i_sb);
1023 return retval;
1025 end_unlink:
1026 pathrelse(&path);
1027 err = journal_end(&th, dir->i_sb, jbegin_count);
1028 reiserfs_check_path(&path);
1029 if (err)
1030 retval = err;
1031 out_unlink:
1032 reiserfs_write_unlock(dir->i_sb);
1033 return retval;
1036 static int reiserfs_symlink(struct inode *parent_dir,
1037 struct dentry *dentry, const char *symname)
1039 int retval;
1040 struct inode *inode;
1041 char *name;
1042 int item_len;
1043 struct reiserfs_transaction_handle th;
1044 int mode = S_IFLNK | S_IRWXUGO;
1045 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1046 int jbegin_count =
1047 JOURNAL_PER_BALANCE_CNT * 3 +
1048 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1049 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1051 if (!(inode = new_inode(parent_dir->i_sb))) {
1052 return -ENOMEM;
1054 new_inode_init(inode, parent_dir, mode);
1056 reiserfs_write_lock(parent_dir->i_sb);
1057 item_len = ROUND_UP(strlen(symname));
1058 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1059 retval = -ENAMETOOLONG;
1060 drop_new_inode(inode);
1061 goto out_failed;
1064 name = kmalloc(item_len, GFP_NOFS);
1065 if (!name) {
1066 drop_new_inode(inode);
1067 retval = -ENOMEM;
1068 goto out_failed;
1070 memcpy(name, symname, strlen(symname));
1071 padd_item(name, item_len, strlen(symname));
1073 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1075 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1076 if (retval) {
1077 drop_new_inode(inode);
1078 kfree(name);
1079 goto out_failed;
1082 retval =
1083 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1084 dentry, inode);
1085 kfree(name);
1086 if (retval) { /* reiserfs_new_inode iputs for us */
1087 goto out_failed;
1090 reiserfs_update_inode_transaction(inode);
1091 reiserfs_update_inode_transaction(parent_dir);
1093 inode->i_op = &reiserfs_symlink_inode_operations;
1094 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1096 // must be sure this inode is written with this transaction
1098 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1100 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1101 dentry->d_name.len, inode, 1 /*visible */ );
1102 if (retval) {
1103 int err;
1104 inode->i_nlink--;
1105 reiserfs_update_sd(&th, inode);
1106 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1107 if (err)
1108 retval = err;
1109 iput(inode);
1110 goto out_failed;
1113 d_instantiate(dentry, inode);
1114 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1115 out_failed:
1116 reiserfs_write_unlock(parent_dir->i_sb);
1117 return retval;
1120 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1121 struct dentry *dentry)
1123 int retval;
1124 struct inode *inode = old_dentry->d_inode;
1125 struct reiserfs_transaction_handle th;
1126 /* We need blocks for transaction + update of quotas for the owners of the directory */
1127 int jbegin_count =
1128 JOURNAL_PER_BALANCE_CNT * 3 +
1129 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1131 reiserfs_write_lock(dir->i_sb);
1132 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1133 //FIXME: sd_nlink is 32 bit for new files
1134 reiserfs_write_unlock(dir->i_sb);
1135 return -EMLINK;
1137 if (inode->i_nlink == 0) {
1138 reiserfs_write_unlock(dir->i_sb);
1139 return -ENOENT;
1142 /* inc before scheduling so reiserfs_unlink knows we are here */
1143 inc_nlink(inode);
1145 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1146 if (retval) {
1147 inode->i_nlink--;
1148 reiserfs_write_unlock(dir->i_sb);
1149 return retval;
1152 /* create new entry */
1153 retval =
1154 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1155 dentry->d_name.len, inode, 1 /*visible */ );
1157 reiserfs_update_inode_transaction(inode);
1158 reiserfs_update_inode_transaction(dir);
1160 if (retval) {
1161 int err;
1162 inode->i_nlink--;
1163 err = journal_end(&th, dir->i_sb, jbegin_count);
1164 reiserfs_write_unlock(dir->i_sb);
1165 return err ? err : retval;
1168 inode->i_ctime = CURRENT_TIME_SEC;
1169 reiserfs_update_sd(&th, inode);
1171 atomic_inc(&inode->i_count);
1172 d_instantiate(dentry, inode);
1173 retval = journal_end(&th, dir->i_sb, jbegin_count);
1174 reiserfs_write_unlock(dir->i_sb);
1175 return retval;
1178 // de contains information pointing to an entry which
1179 static int de_still_valid(const char *name, int len,
1180 struct reiserfs_dir_entry *de)
1182 struct reiserfs_dir_entry tmp = *de;
1184 // recalculate pointer to name and name length
1185 set_de_name_and_namelen(&tmp);
1186 // FIXME: could check more
1187 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1188 return 0;
1189 return 1;
1192 static int entry_points_to_object(const char *name, int len,
1193 struct reiserfs_dir_entry *de,
1194 struct inode *inode)
1196 if (!de_still_valid(name, len, de))
1197 return 0;
1199 if (inode) {
1200 if (!de_visible(de->de_deh + de->de_entry_num))
1201 reiserfs_panic(NULL,
1202 "vs-7042: entry_points_to_object: entry must be visible");
1203 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1206 /* this must be added hidden entry */
1207 if (de_visible(de->de_deh + de->de_entry_num))
1208 reiserfs_panic(NULL,
1209 "vs-7043: entry_points_to_object: entry must be visible");
1211 return 1;
1214 /* sets key of objectid the entry has to point to */
1215 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1216 struct reiserfs_key *key)
1218 /* JDM These operations are endian safe - both are le */
1219 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1220 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1224 * process, that is going to call fix_nodes/do_balance must hold only
1225 * one path. If it holds 2 or more, it can get into endless waiting in
1226 * get_empty_nodes or its clones
1228 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1229 struct inode *new_dir, struct dentry *new_dentry)
1231 int retval;
1232 INITIALIZE_PATH(old_entry_path);
1233 INITIALIZE_PATH(new_entry_path);
1234 INITIALIZE_PATH(dot_dot_entry_path);
1235 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1236 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1237 struct inode *old_inode, *new_dentry_inode;
1238 struct reiserfs_transaction_handle th;
1239 int jbegin_count;
1240 umode_t old_inode_mode;
1241 unsigned long savelink = 1;
1242 struct timespec ctime;
1244 /* three balancings: (1) old name removal, (2) new name insertion
1245 and (3) maybe "save" link insertion
1246 stat data updates: (1) old directory,
1247 (2) new directory and (3) maybe old object stat data (when it is
1248 directory) and (4) maybe stat data of object to which new entry
1249 pointed initially and (5) maybe block containing ".." of
1250 renamed directory
1251 quota updates: two parent directories */
1252 jbegin_count =
1253 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1254 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1256 old_inode = old_dentry->d_inode;
1257 new_dentry_inode = new_dentry->d_inode;
1259 // make sure, that oldname still exists and points to an object we
1260 // are going to rename
1261 old_de.de_gen_number_bit_string = NULL;
1262 reiserfs_write_lock(old_dir->i_sb);
1263 retval =
1264 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1265 old_dentry->d_name.len, &old_entry_path,
1266 &old_de);
1267 pathrelse(&old_entry_path);
1268 if (retval == IO_ERROR) {
1269 reiserfs_write_unlock(old_dir->i_sb);
1270 return -EIO;
1273 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1274 reiserfs_write_unlock(old_dir->i_sb);
1275 return -ENOENT;
1278 old_inode_mode = old_inode->i_mode;
1279 if (S_ISDIR(old_inode_mode)) {
1280 // make sure, that directory being renamed has correct ".."
1281 // and that its new parent directory has not too many links
1282 // already
1284 if (new_dentry_inode) {
1285 if (!reiserfs_empty_dir(new_dentry_inode)) {
1286 reiserfs_write_unlock(old_dir->i_sb);
1287 return -ENOTEMPTY;
1291 /* directory is renamed, its parent directory will be changed,
1292 ** so find ".." entry
1294 dot_dot_de.de_gen_number_bit_string = NULL;
1295 retval =
1296 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1297 &dot_dot_de);
1298 pathrelse(&dot_dot_entry_path);
1299 if (retval != NAME_FOUND) {
1300 reiserfs_write_unlock(old_dir->i_sb);
1301 return -EIO;
1304 /* inode number of .. must equal old_dir->i_ino */
1305 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1306 reiserfs_write_unlock(old_dir->i_sb);
1307 return -EIO;
1311 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1312 if (retval) {
1313 reiserfs_write_unlock(old_dir->i_sb);
1314 return retval;
1317 /* add new entry (or find the existing one) */
1318 retval =
1319 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1320 new_dentry->d_name.len, old_inode, 0);
1321 if (retval == -EEXIST) {
1322 if (!new_dentry_inode) {
1323 reiserfs_panic(old_dir->i_sb,
1324 "vs-7050: new entry is found, new inode == 0\n");
1326 } else if (retval) {
1327 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1328 reiserfs_write_unlock(old_dir->i_sb);
1329 return err ? err : retval;
1332 reiserfs_update_inode_transaction(old_dir);
1333 reiserfs_update_inode_transaction(new_dir);
1335 /* this makes it so an fsync on an open fd for the old name will
1336 ** commit the rename operation
1338 reiserfs_update_inode_transaction(old_inode);
1340 if (new_dentry_inode)
1341 reiserfs_update_inode_transaction(new_dentry_inode);
1343 while (1) {
1344 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1345 if ((retval =
1346 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1347 &old_entry_path,
1348 &old_de)) != NAME_FOUND) {
1349 pathrelse(&old_entry_path);
1350 journal_end(&th, old_dir->i_sb, jbegin_count);
1351 reiserfs_write_unlock(old_dir->i_sb);
1352 return -EIO;
1355 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
1357 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1359 // look for new name by reiserfs_find_entry
1360 new_de.de_gen_number_bit_string = NULL;
1361 retval =
1362 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1363 new_dentry->d_name.len, &new_entry_path,
1364 &new_de);
1365 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1366 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1367 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1368 pathrelse(&new_entry_path);
1369 pathrelse(&old_entry_path);
1370 journal_end(&th, old_dir->i_sb, jbegin_count);
1371 reiserfs_write_unlock(old_dir->i_sb);
1372 return -EIO;
1375 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1377 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1379 if (S_ISDIR(old_inode->i_mode)) {
1380 if ((retval =
1381 search_by_entry_key(new_dir->i_sb,
1382 &dot_dot_de.de_entry_key,
1383 &dot_dot_entry_path,
1384 &dot_dot_de)) != NAME_FOUND) {
1385 pathrelse(&dot_dot_entry_path);
1386 pathrelse(&new_entry_path);
1387 pathrelse(&old_entry_path);
1388 journal_end(&th, old_dir->i_sb, jbegin_count);
1389 reiserfs_write_unlock(old_dir->i_sb);
1390 return -EIO;
1392 copy_item_head(&dot_dot_ih,
1393 get_ih(&dot_dot_entry_path));
1394 // node containing ".." gets into transaction
1395 reiserfs_prepare_for_journal(old_inode->i_sb,
1396 dot_dot_de.de_bh, 1);
1398 /* we should check seals here, not do
1399 this stuff, yes? Then, having
1400 gathered everything into RAM we
1401 should lock the buffers, yes? -Hans */
1402 /* probably. our rename needs to hold more
1403 ** than one path at once. The seals would
1404 ** have to be written to deal with multi-path
1405 ** issues -chris
1407 /* sanity checking before doing the rename - avoid races many
1408 ** of the above checks could have scheduled. We have to be
1409 ** sure our items haven't been shifted by another process.
1411 if (item_moved(&new_entry_ih, &new_entry_path) ||
1412 !entry_points_to_object(new_dentry->d_name.name,
1413 new_dentry->d_name.len,
1414 &new_de, new_dentry_inode) ||
1415 item_moved(&old_entry_ih, &old_entry_path) ||
1416 !entry_points_to_object(old_dentry->d_name.name,
1417 old_dentry->d_name.len,
1418 &old_de, old_inode)) {
1419 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1420 new_de.de_bh);
1421 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1422 old_de.de_bh);
1423 if (S_ISDIR(old_inode_mode))
1424 reiserfs_restore_prepared_buffer(old_inode->
1425 i_sb,
1426 dot_dot_de.
1427 de_bh);
1428 continue;
1430 if (S_ISDIR(old_inode_mode)) {
1431 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1432 !entry_points_to_object("..", 2, &dot_dot_de,
1433 old_dir)) {
1434 reiserfs_restore_prepared_buffer(old_inode->
1435 i_sb,
1436 old_de.de_bh);
1437 reiserfs_restore_prepared_buffer(old_inode->
1438 i_sb,
1439 new_de.de_bh);
1440 reiserfs_restore_prepared_buffer(old_inode->
1441 i_sb,
1442 dot_dot_de.
1443 de_bh);
1444 continue;
1448 RFALSE(S_ISDIR(old_inode_mode) &&
1449 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1451 break;
1454 /* ok, all the changes can be done in one fell swoop when we
1455 have claimed all the buffers needed. */
1457 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1458 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1459 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1461 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1462 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1463 ctime = CURRENT_TIME_SEC;
1464 old_dir->i_ctime = old_dir->i_mtime = ctime;
1465 new_dir->i_ctime = new_dir->i_mtime = ctime;
1466 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1467 renamed object */
1468 old_inode->i_ctime = ctime;
1470 if (new_dentry_inode) {
1471 // adjust link number of the victim
1472 if (S_ISDIR(new_dentry_inode->i_mode)) {
1473 clear_nlink(new_dentry_inode);
1474 } else {
1475 drop_nlink(new_dentry_inode);
1477 new_dentry_inode->i_ctime = ctime;
1478 savelink = new_dentry_inode->i_nlink;
1481 if (S_ISDIR(old_inode_mode)) {
1482 // adjust ".." of renamed directory
1483 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1484 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1486 if (!new_dentry_inode)
1487 /* there (in new_dir) was no directory, so it got new link
1488 (".." of renamed directory) */
1489 INC_DIR_INODE_NLINK(new_dir);
1491 /* old directory lost one link - ".. " of renamed directory */
1492 DEC_DIR_INODE_NLINK(old_dir);
1494 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1495 pathrelse(&new_entry_path);
1496 pathrelse(&dot_dot_entry_path);
1498 // FIXME: this reiserfs_cut_from_item's return value may screw up
1499 // anybody, but it will panic if will not be able to find the
1500 // entry. This needs one more clean up
1501 if (reiserfs_cut_from_item
1502 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1503 0) < 0)
1504 reiserfs_warning(old_dir->i_sb,
1505 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1507 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1509 reiserfs_update_sd(&th, old_dir);
1510 reiserfs_update_sd(&th, new_dir);
1511 reiserfs_update_sd(&th, old_inode);
1513 if (new_dentry_inode) {
1514 if (savelink == 0)
1515 add_save_link(&th, new_dentry_inode,
1516 0 /* not truncate */ );
1517 reiserfs_update_sd(&th, new_dentry_inode);
1520 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1521 reiserfs_write_unlock(old_dir->i_sb);
1522 return retval;
1526 * directories can handle most operations...
1528 struct inode_operations reiserfs_dir_inode_operations = {
1529 //&reiserfs_dir_operations, /* default_file_ops */
1530 .create = reiserfs_create,
1531 .lookup = reiserfs_lookup,
1532 .link = reiserfs_link,
1533 .unlink = reiserfs_unlink,
1534 .symlink = reiserfs_symlink,
1535 .mkdir = reiserfs_mkdir,
1536 .rmdir = reiserfs_rmdir,
1537 .mknod = reiserfs_mknod,
1538 .rename = reiserfs_rename,
1539 .setattr = reiserfs_setattr,
1540 .setxattr = reiserfs_setxattr,
1541 .getxattr = reiserfs_getxattr,
1542 .listxattr = reiserfs_listxattr,
1543 .removexattr = reiserfs_removexattr,
1544 .permission = reiserfs_permission,
1548 * symlink operations.. same as page_symlink_inode_operations, with xattr
1549 * stuff added
1551 struct inode_operations reiserfs_symlink_inode_operations = {
1552 .readlink = generic_readlink,
1553 .follow_link = page_follow_link_light,
1554 .put_link = page_put_link,
1555 .setattr = reiserfs_setattr,
1556 .setxattr = reiserfs_setxattr,
1557 .getxattr = reiserfs_getxattr,
1558 .listxattr = reiserfs_listxattr,
1559 .removexattr = reiserfs_removexattr,
1560 .permission = reiserfs_permission,
1565 * special file operations.. just xattr/acl stuff
1567 struct inode_operations reiserfs_special_inode_operations = {
1568 .setattr = reiserfs_setattr,
1569 .setxattr = reiserfs_setxattr,
1570 .getxattr = reiserfs_getxattr,
1571 .listxattr = reiserfs_listxattr,
1572 .removexattr = reiserfs_removexattr,
1573 .permission = reiserfs_permission,