[PATCH] r/o bind mounts: unlink: monitor i_nlink
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / reiserfs / namei.c
blobc76d427e027b3d8246b0a6ebd8d4bce74fd958bf
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) { i->i_nlink++; 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 if (de->de_entry_num >= ih_entry_count(de->de_ih))
71 BUG();
73 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
74 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
75 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
76 if (de->de_name[de->de_namelen - 1] == 0)
77 de->de_namelen = strlen(de->de_name);
80 // what entry points to
81 static inline void set_de_object_key(struct reiserfs_dir_entry *de)
83 if (de->de_entry_num >= ih_entry_count(de->de_ih))
84 BUG();
85 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
86 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
89 static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
91 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
93 if (de->de_entry_num >= ih_entry_count(de->de_ih))
94 BUG();
96 /* store key of the found entry */
97 de->de_entry_key.version = KEY_FORMAT_3_5;
98 de->de_entry_key.on_disk_key.k_dir_id =
99 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
100 de->de_entry_key.on_disk_key.k_objectid =
101 le32_to_cpu(de->de_ih->ih_key.k_objectid);
102 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
103 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
106 /* We assign a key to each directory item, and place multiple entries
107 in a single directory item. A directory item has a key equal to the
108 key of the first directory entry in it.
110 This function first calls search_by_key, then, if item whose first
111 entry matches is not found it looks for the entry inside directory
112 item found by search_by_key. Fills the path to the entry, and to the
113 entry position in the item
117 /* The function is NOT SCHEDULE-SAFE! */
118 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
119 struct path *path, struct reiserfs_dir_entry *de)
121 int retval;
123 retval = search_item(sb, key, path);
124 switch (retval) {
125 case ITEM_NOT_FOUND:
126 if (!PATH_LAST_POSITION(path)) {
127 reiserfs_warning(sb,
128 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
129 pathrelse(path);
130 return IO_ERROR;
132 PATH_LAST_POSITION(path)--;
134 case ITEM_FOUND:
135 break;
137 case IO_ERROR:
138 return retval;
140 default:
141 pathrelse(path);
142 reiserfs_warning(sb,
143 "vs-7002: search_by_entry_key: no path to here");
144 return IO_ERROR;
147 set_de_item_location(de, path);
149 #ifdef CONFIG_REISERFS_CHECK
150 if (!is_direntry_le_ih(de->de_ih) ||
151 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
152 print_block(de->de_bh, 0, -1, -1);
153 reiserfs_panic(sb,
154 "vs-7005: search_by_entry_key: found item %h is not directory item or "
155 "does not belong to the same directory as key %K",
156 de->de_ih, key);
158 #endif /* CONFIG_REISERFS_CHECK */
160 /* binary search in directory item by third componen t of the
161 key. sets de->de_entry_num of de */
162 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
163 path->pos_in_item = de->de_entry_num;
164 if (retval != NAME_NOT_FOUND) {
165 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
166 set_de_name_and_namelen(de);
167 set_de_object_key(de);
169 return retval;
172 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
174 /* The third component is hashed, and you can choose from more than
175 one hash function. Per directory hashes are not yet implemented
176 but are thought about. This function should be moved to hashes.c
177 Jedi, please do so. -Hans */
179 static __u32 get_third_component(struct super_block *s,
180 const char *name, int len)
182 __u32 res;
184 if (!len || (len == 1 && name[0] == '.'))
185 return DOT_OFFSET;
186 if (len == 2 && name[0] == '.' && name[1] == '.')
187 return DOT_DOT_OFFSET;
189 res = REISERFS_SB(s)->s_hash_function(name, len);
191 // take bits from 7-th to 30-th including both bounds
192 res = GET_HASH_VALUE(res);
193 if (res == 0)
194 // needed to have no names before "." and ".." those have hash
195 // value == 0 and generation conters 1 and 2 accordingly
196 res = 128;
197 return res + MAX_GENERATION_NUMBER;
200 static int reiserfs_match(struct reiserfs_dir_entry *de,
201 const char *name, int namelen)
203 int retval = NAME_NOT_FOUND;
205 if ((namelen == de->de_namelen) &&
206 !memcmp(de->de_name, name, de->de_namelen))
207 retval =
208 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
209 NAME_FOUND_INVISIBLE);
211 return retval;
214 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
216 /* used when hash collisions exist */
218 static int linear_search_in_dir_item(struct cpu_key *key,
219 struct reiserfs_dir_entry *de,
220 const char *name, int namelen)
222 struct reiserfs_de_head *deh = de->de_deh;
223 int retval;
224 int i;
226 i = de->de_entry_num;
228 if (i == I_ENTRY_COUNT(de->de_ih) ||
229 GET_HASH_VALUE(deh_offset(deh + i)) !=
230 GET_HASH_VALUE(cpu_key_k_offset(key))) {
231 i--;
234 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
235 "vs-7010: array of entry headers not found");
237 deh += i;
239 for (; i >= 0; i--, deh--) {
240 if (GET_HASH_VALUE(deh_offset(deh)) !=
241 GET_HASH_VALUE(cpu_key_k_offset(key))) {
242 // hash value does not match, no need to check whole name
243 return NAME_NOT_FOUND;
246 /* mark, that this generation number is used */
247 if (de->de_gen_number_bit_string)
248 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
249 de->de_gen_number_bit_string);
251 // calculate pointer to name and namelen
252 de->de_entry_num = i;
253 set_de_name_and_namelen(de);
255 if ((retval =
256 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
257 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
259 // key of pointed object
260 set_de_object_key(de);
262 store_de_entry_key(de);
264 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
265 return retval;
269 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
270 /* we have reached left most entry in the node. In common we
271 have to go to the left neighbor, but if generation counter
272 is 0 already, we know for sure, that there is no name with
273 the same hash value */
274 // FIXME: this work correctly only because hash value can not
275 // be 0. Btw, in case of Yura's hash it is probably possible,
276 // so, this is a bug
277 return NAME_NOT_FOUND;
279 RFALSE(de->de_item_num,
280 "vs-7015: two diritems of the same directory in one node?");
282 return GOTO_PREVIOUS_ITEM;
285 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
286 // FIXME: should add something like IOERROR
287 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
288 struct path *path_to_entry,
289 struct reiserfs_dir_entry *de)
291 struct cpu_key key_to_search;
292 int retval;
294 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
295 return NAME_NOT_FOUND;
297 /* we will search for this key in the tree */
298 make_cpu_key(&key_to_search, dir,
299 get_third_component(dir->i_sb, name, namelen),
300 TYPE_DIRENTRY, 3);
302 while (1) {
303 retval =
304 search_by_entry_key(dir->i_sb, &key_to_search,
305 path_to_entry, de);
306 if (retval == IO_ERROR) {
307 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
308 __FUNCTION__);
309 return IO_ERROR;
312 /* compare names for all entries having given hash value */
313 retval =
314 linear_search_in_dir_item(&key_to_search, de, name,
315 namelen);
316 if (retval != GOTO_PREVIOUS_ITEM) {
317 /* there is no need to scan directory anymore. Given entry found or does not exist */
318 path_to_entry->pos_in_item = de->de_entry_num;
319 return retval;
322 /* there is left neighboring item of this directory and given entry can be there */
323 set_cpu_key_k_offset(&key_to_search,
324 le_ih_k_offset(de->de_ih) - 1);
325 pathrelse(path_to_entry);
327 } /* while (1) */
330 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
331 struct nameidata *nd)
333 int retval;
334 struct inode *inode = NULL;
335 struct reiserfs_dir_entry de;
336 INITIALIZE_PATH(path_to_entry);
338 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
339 return ERR_PTR(-ENAMETOOLONG);
341 reiserfs_write_lock(dir->i_sb);
342 de.de_gen_number_bit_string = NULL;
343 retval =
344 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
345 &path_to_entry, &de);
346 pathrelse(&path_to_entry);
347 if (retval == NAME_FOUND) {
348 /* Hide the .reiserfs_priv directory */
349 if (reiserfs_xattrs(dir->i_sb) &&
350 !old_format_only(dir->i_sb) &&
351 REISERFS_SB(dir->i_sb)->priv_root &&
352 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
353 de.de_objectid ==
354 le32_to_cpu(INODE_PKEY
355 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
356 k_objectid)) {
357 reiserfs_write_unlock(dir->i_sb);
358 return ERR_PTR(-EACCES);
361 inode =
362 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
363 if (!inode || IS_ERR(inode)) {
364 reiserfs_write_unlock(dir->i_sb);
365 return ERR_PTR(-EACCES);
368 /* Propogate the priv_object flag so we know we're in the priv tree */
369 if (is_reiserfs_priv_object(dir))
370 reiserfs_mark_inode_private(inode);
372 reiserfs_write_unlock(dir->i_sb);
373 if (retval == IO_ERROR) {
374 return ERR_PTR(-EIO);
377 return d_splice_alias(inode, dentry);
381 ** looks up the dentry of the parent directory for child.
382 ** taken from ext2_get_parent
384 struct dentry *reiserfs_get_parent(struct dentry *child)
386 int retval;
387 struct inode *inode = NULL;
388 struct reiserfs_dir_entry de;
389 INITIALIZE_PATH(path_to_entry);
390 struct dentry *parent;
391 struct inode *dir = child->d_inode;
393 if (dir->i_nlink == 0) {
394 return ERR_PTR(-ENOENT);
396 de.de_gen_number_bit_string = NULL;
398 reiserfs_write_lock(dir->i_sb);
399 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
400 pathrelse(&path_to_entry);
401 if (retval != NAME_FOUND) {
402 reiserfs_write_unlock(dir->i_sb);
403 return ERR_PTR(-ENOENT);
405 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
406 reiserfs_write_unlock(dir->i_sb);
408 if (!inode || IS_ERR(inode)) {
409 return ERR_PTR(-EACCES);
411 parent = d_alloc_anon(inode);
412 if (!parent) {
413 iput(inode);
414 parent = ERR_PTR(-ENOMEM);
416 return parent;
419 /* add entry to the directory (entry can be hidden).
421 insert definition of when hidden directories are used here -Hans
423 Does not mark dir inode dirty, do it after successesfull call to it */
425 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
426 struct inode *dir, const char *name, int namelen,
427 struct inode *inode, int visible)
429 struct cpu_key entry_key;
430 struct reiserfs_de_head *deh;
431 INITIALIZE_PATH(path);
432 struct reiserfs_dir_entry de;
433 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
434 int gen_number;
435 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
436 if we create file with short name */
437 char *buffer;
438 int buflen, paste_size;
439 int retval;
441 BUG_ON(!th->t_trans_id);
443 /* cannot allow items to be added into a busy deleted directory */
444 if (!namelen)
445 return -EINVAL;
447 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
448 return -ENAMETOOLONG;
450 /* each entry has unique key. compose it */
451 make_cpu_key(&entry_key, dir,
452 get_third_component(dir->i_sb, name, namelen),
453 TYPE_DIRENTRY, 3);
455 /* get memory for composing the entry */
456 buflen = DEH_SIZE + ROUND_UP(namelen);
457 if (buflen > sizeof(small_buf)) {
458 buffer = kmalloc(buflen, GFP_NOFS);
459 if (buffer == 0)
460 return -ENOMEM;
461 } else
462 buffer = small_buf;
464 paste_size =
465 (get_inode_sd_version(dir) ==
466 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
468 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
469 deh = (struct reiserfs_de_head *)buffer;
470 deh->deh_location = 0; /* JDM Endian safe if 0 */
471 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
472 deh->deh_state = 0; /* JDM Endian safe if 0 */
473 /* put key (ino analog) to de */
474 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
475 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
477 /* copy name */
478 memcpy((char *)(deh + 1), name, namelen);
479 /* padd by 0s to the 4 byte boundary */
480 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
482 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
483 mark_de_without_sd(deh);
484 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
486 /* find the proper place for the new entry */
487 memset(bit_string, 0, sizeof(bit_string));
488 de.de_gen_number_bit_string = bit_string;
489 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
490 if (retval != NAME_NOT_FOUND) {
491 if (buffer != small_buf)
492 kfree(buffer);
493 pathrelse(&path);
495 if (retval == IO_ERROR) {
496 return -EIO;
499 if (retval != NAME_FOUND) {
500 reiserfs_warning(dir->i_sb,
501 "zam-7002:%s: \"reiserfs_find_entry\" "
502 "has returned unexpected value (%d)",
503 __FUNCTION__, retval);
506 return -EEXIST;
509 gen_number =
510 find_first_zero_bit(bit_string,
511 MAX_GENERATION_NUMBER + 1);
512 if (gen_number > MAX_GENERATION_NUMBER) {
513 /* there is no free generation number */
514 reiserfs_warning(dir->i_sb,
515 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
516 if (buffer != small_buf)
517 kfree(buffer);
518 pathrelse(&path);
519 return -EBUSY;
521 /* adjust offset of directory enrty */
522 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
523 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
525 /* update max-hash-collisions counter in reiserfs_sb_info */
526 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
528 if (gen_number != 0) { /* we need to re-search for the insertion point */
529 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
530 NAME_NOT_FOUND) {
531 reiserfs_warning(dir->i_sb,
532 "vs-7032: reiserfs_add_entry: "
533 "entry with this key (%K) already exists",
534 &entry_key);
536 if (buffer != small_buf)
537 kfree(buffer);
538 pathrelse(&path);
539 return -EBUSY;
543 /* perform the insertion of the entry that we have prepared */
544 retval =
545 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
546 paste_size);
547 if (buffer != small_buf)
548 kfree(buffer);
549 if (retval) {
550 reiserfs_check_path(&path);
551 return retval;
554 dir->i_size += paste_size;
555 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
556 if (!S_ISDIR(inode->i_mode) && visible)
557 // reiserfs_mkdir or reiserfs_rename will do that by itself
558 reiserfs_update_sd(th, dir);
560 reiserfs_check_path(&path);
561 return 0;
564 /* quota utility function, call if you've had to abort after calling
565 ** new_inode_init, and have not called reiserfs_new_inode yet.
566 ** This should only be called on inodes that do not have stat data
567 ** inserted into the tree yet.
569 static int drop_new_inode(struct inode *inode)
571 DQUOT_DROP(inode);
572 make_bad_inode(inode);
573 inode->i_flags |= S_NOQUOTA;
574 iput(inode);
575 return 0;
578 /* utility function that does setup for reiserfs_new_inode.
579 ** DQUOT_INIT needs lots of credits so it's better to have it
580 ** outside of a transaction, so we had to pull some bits of
581 ** reiserfs_new_inode out into this func.
583 static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
586 /* the quota init calls have to know who to charge the quota to, so
587 ** we have to set uid and gid here
589 inode->i_uid = current->fsuid;
590 inode->i_mode = mode;
591 /* Make inode invalid - just in case we are going to drop it before
592 * the initialization happens */
593 INODE_PKEY(inode)->k_objectid = 0;
595 if (dir->i_mode & S_ISGID) {
596 inode->i_gid = dir->i_gid;
597 if (S_ISDIR(mode))
598 inode->i_mode |= S_ISGID;
599 } else {
600 inode->i_gid = current->fsgid;
602 DQUOT_INIT(inode);
603 return 0;
606 static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
607 struct nameidata *nd)
609 int retval;
610 struct inode *inode;
611 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
612 int jbegin_count =
613 JOURNAL_PER_BALANCE_CNT * 2 +
614 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
615 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
616 struct reiserfs_transaction_handle th;
617 int locked;
619 if (!(inode = new_inode(dir->i_sb))) {
620 return -ENOMEM;
622 new_inode_init(inode, dir, mode);
624 locked = reiserfs_cache_default_acl(dir);
626 reiserfs_write_lock(dir->i_sb);
628 if (locked)
629 reiserfs_write_lock_xattrs(dir->i_sb);
631 retval = journal_begin(&th, dir->i_sb, jbegin_count);
632 if (retval) {
633 drop_new_inode(inode);
634 goto out_failed;
637 retval =
638 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
639 inode);
640 if (retval)
641 goto out_failed;
643 if (locked) {
644 reiserfs_write_unlock_xattrs(dir->i_sb);
645 locked = 0;
648 inode->i_op = &reiserfs_file_inode_operations;
649 inode->i_fop = &reiserfs_file_operations;
650 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
652 retval =
653 reiserfs_add_entry(&th, dir, dentry->d_name.name,
654 dentry->d_name.len, inode, 1 /*visible */ );
655 if (retval) {
656 int err;
657 inode->i_nlink--;
658 reiserfs_update_sd(&th, inode);
659 err = journal_end(&th, dir->i_sb, jbegin_count);
660 if (err)
661 retval = err;
662 iput(inode);
663 goto out_failed;
665 reiserfs_update_inode_transaction(inode);
666 reiserfs_update_inode_transaction(dir);
668 d_instantiate(dentry, inode);
669 retval = journal_end(&th, dir->i_sb, jbegin_count);
671 out_failed:
672 if (locked)
673 reiserfs_write_unlock_xattrs(dir->i_sb);
674 reiserfs_write_unlock(dir->i_sb);
675 return retval;
678 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
679 dev_t rdev)
681 int retval;
682 struct inode *inode;
683 struct reiserfs_transaction_handle th;
684 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
685 int jbegin_count =
686 JOURNAL_PER_BALANCE_CNT * 3 +
687 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
688 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
689 int locked;
691 if (!new_valid_dev(rdev))
692 return -EINVAL;
694 if (!(inode = new_inode(dir->i_sb))) {
695 return -ENOMEM;
697 new_inode_init(inode, dir, mode);
699 locked = reiserfs_cache_default_acl(dir);
701 reiserfs_write_lock(dir->i_sb);
703 if (locked)
704 reiserfs_write_lock_xattrs(dir->i_sb);
706 retval = journal_begin(&th, dir->i_sb, jbegin_count);
707 if (retval) {
708 drop_new_inode(inode);
709 goto out_failed;
712 retval =
713 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
714 inode);
715 if (retval) {
716 goto out_failed;
719 if (locked) {
720 reiserfs_write_unlock_xattrs(dir->i_sb);
721 locked = 0;
724 inode->i_op = &reiserfs_special_inode_operations;
725 init_special_inode(inode, inode->i_mode, rdev);
727 //FIXME: needed for block and char devices only
728 reiserfs_update_sd(&th, inode);
730 reiserfs_update_inode_transaction(inode);
731 reiserfs_update_inode_transaction(dir);
733 retval =
734 reiserfs_add_entry(&th, dir, dentry->d_name.name,
735 dentry->d_name.len, inode, 1 /*visible */ );
736 if (retval) {
737 int err;
738 inode->i_nlink--;
739 reiserfs_update_sd(&th, inode);
740 err = journal_end(&th, dir->i_sb, jbegin_count);
741 if (err)
742 retval = err;
743 iput(inode);
744 goto out_failed;
747 d_instantiate(dentry, inode);
748 retval = journal_end(&th, dir->i_sb, jbegin_count);
750 out_failed:
751 if (locked)
752 reiserfs_write_unlock_xattrs(dir->i_sb);
753 reiserfs_write_unlock(dir->i_sb);
754 return retval;
757 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
759 int retval;
760 struct inode *inode;
761 struct reiserfs_transaction_handle th;
762 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
763 int jbegin_count =
764 JOURNAL_PER_BALANCE_CNT * 3 +
765 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
766 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
767 int locked;
769 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
770 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
771 REISERFS_I(dir)->new_packing_locality = 1;
772 #endif
773 mode = S_IFDIR | mode;
774 if (!(inode = new_inode(dir->i_sb))) {
775 return -ENOMEM;
777 new_inode_init(inode, dir, mode);
779 locked = reiserfs_cache_default_acl(dir);
781 reiserfs_write_lock(dir->i_sb);
782 if (locked)
783 reiserfs_write_lock_xattrs(dir->i_sb);
785 retval = journal_begin(&th, dir->i_sb, jbegin_count);
786 if (retval) {
787 drop_new_inode(inode);
788 goto out_failed;
791 /* inc the link count now, so another writer doesn't overflow it while
792 ** we sleep later on.
794 INC_DIR_INODE_NLINK(dir)
796 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
797 old_format_only(dir->i_sb) ?
798 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
799 dentry, inode);
800 if (retval) {
801 dir->i_nlink--;
802 goto out_failed;
805 if (locked) {
806 reiserfs_write_unlock_xattrs(dir->i_sb);
807 locked = 0;
810 reiserfs_update_inode_transaction(inode);
811 reiserfs_update_inode_transaction(dir);
813 inode->i_op = &reiserfs_dir_inode_operations;
814 inode->i_fop = &reiserfs_dir_operations;
816 // note, _this_ add_entry will not update dir's stat data
817 retval =
818 reiserfs_add_entry(&th, dir, dentry->d_name.name,
819 dentry->d_name.len, inode, 1 /*visible */ );
820 if (retval) {
821 int err;
822 inode->i_nlink = 0;
823 DEC_DIR_INODE_NLINK(dir);
824 reiserfs_update_sd(&th, inode);
825 err = journal_end(&th, dir->i_sb, jbegin_count);
826 if (err)
827 retval = err;
828 iput(inode);
829 goto out_failed;
831 // the above add_entry did not update dir's stat data
832 reiserfs_update_sd(&th, dir);
834 d_instantiate(dentry, inode);
835 retval = journal_end(&th, dir->i_sb, jbegin_count);
836 out_failed:
837 if (locked)
838 reiserfs_write_unlock_xattrs(dir->i_sb);
839 reiserfs_write_unlock(dir->i_sb);
840 return retval;
843 static inline int reiserfs_empty_dir(struct inode *inode)
845 /* we can cheat because an old format dir cannot have
846 ** EMPTY_DIR_SIZE, and a new format dir cannot have
847 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
848 ** regardless of disk format version, the directory is empty.
850 if (inode->i_size != EMPTY_DIR_SIZE &&
851 inode->i_size != EMPTY_DIR_SIZE_V1) {
852 return 0;
854 return 1;
857 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
859 int retval, err;
860 struct inode *inode;
861 struct reiserfs_transaction_handle th;
862 int jbegin_count;
863 INITIALIZE_PATH(path);
864 struct reiserfs_dir_entry de;
866 /* we will be doing 2 balancings and update 2 stat data, we change quotas
867 * of the owner of the directory and of the owner of the parent directory.
868 * The quota structure is possibly deleted only on last iput => outside
869 * of this transaction */
870 jbegin_count =
871 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
872 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
874 reiserfs_write_lock(dir->i_sb);
875 retval = journal_begin(&th, dir->i_sb, jbegin_count);
876 if (retval)
877 goto out_rmdir;
879 de.de_gen_number_bit_string = NULL;
880 if ((retval =
881 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
882 &path, &de)) == NAME_NOT_FOUND) {
883 retval = -ENOENT;
884 goto end_rmdir;
885 } else if (retval == IO_ERROR) {
886 retval = -EIO;
887 goto end_rmdir;
890 inode = dentry->d_inode;
892 reiserfs_update_inode_transaction(inode);
893 reiserfs_update_inode_transaction(dir);
895 if (de.de_objectid != inode->i_ino) {
896 // FIXME: compare key of an object and a key found in the
897 // entry
898 retval = -EIO;
899 goto end_rmdir;
901 if (!reiserfs_empty_dir(inode)) {
902 retval = -ENOTEMPTY;
903 goto end_rmdir;
906 /* cut entry from dir directory */
907 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
908 0 /*new file size - not used here */ );
909 if (retval < 0)
910 goto end_rmdir;
912 if (inode->i_nlink != 2 && inode->i_nlink != 1)
913 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
914 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
916 inode->i_nlink = 0;
917 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
918 reiserfs_update_sd(&th, inode);
920 DEC_DIR_INODE_NLINK(dir)
921 dir->i_size -= (DEH_SIZE + de.de_entrylen);
922 reiserfs_update_sd(&th, dir);
924 /* prevent empty directory from getting lost */
925 add_save_link(&th, inode, 0 /* not truncate */ );
927 retval = journal_end(&th, dir->i_sb, jbegin_count);
928 reiserfs_check_path(&path);
929 out_rmdir:
930 reiserfs_write_unlock(dir->i_sb);
931 return retval;
933 end_rmdir:
934 /* we must release path, because we did not call
935 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
936 release path if operation was not complete */
937 pathrelse(&path);
938 err = journal_end(&th, dir->i_sb, jbegin_count);
939 reiserfs_write_unlock(dir->i_sb);
940 return err ? err : retval;
943 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
945 int retval, err;
946 struct inode *inode;
947 struct reiserfs_dir_entry de;
948 INITIALIZE_PATH(path);
949 struct reiserfs_transaction_handle th;
950 int jbegin_count;
951 unsigned long savelink;
953 inode = dentry->d_inode;
955 /* in this transaction we can be doing at max two balancings and update
956 * two stat datas, we change quotas of the owner of the directory and of
957 * the owner of the parent directory. The quota structure is possibly
958 * deleted only on iput => outside of this transaction */
959 jbegin_count =
960 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
961 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
963 reiserfs_write_lock(dir->i_sb);
964 retval = journal_begin(&th, dir->i_sb, jbegin_count);
965 if (retval)
966 goto out_unlink;
968 de.de_gen_number_bit_string = NULL;
969 if ((retval =
970 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
971 &path, &de)) == NAME_NOT_FOUND) {
972 retval = -ENOENT;
973 goto end_unlink;
974 } else if (retval == IO_ERROR) {
975 retval = -EIO;
976 goto end_unlink;
979 reiserfs_update_inode_transaction(inode);
980 reiserfs_update_inode_transaction(dir);
982 if (de.de_objectid != inode->i_ino) {
983 // FIXME: compare key of an object and a key found in the
984 // entry
985 retval = -EIO;
986 goto end_unlink;
989 if (!inode->i_nlink) {
990 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
991 "(%s:%lu), %d", __FUNCTION__,
992 reiserfs_bdevname(inode->i_sb), inode->i_ino,
993 inode->i_nlink);
994 inode->i_nlink = 1;
997 drop_nlink(inode);
1000 * we schedule before doing the add_save_link call, save the link
1001 * count so we don't race
1003 savelink = inode->i_nlink;
1005 retval =
1006 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
1008 if (retval < 0) {
1009 inode->i_nlink++;
1010 goto end_unlink;
1012 inode->i_ctime = CURRENT_TIME_SEC;
1013 reiserfs_update_sd(&th, inode);
1015 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1016 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1017 reiserfs_update_sd(&th, dir);
1019 if (!savelink)
1020 /* prevent file from getting lost */
1021 add_save_link(&th, inode, 0 /* not truncate */ );
1023 retval = journal_end(&th, dir->i_sb, jbegin_count);
1024 reiserfs_check_path(&path);
1025 reiserfs_write_unlock(dir->i_sb);
1026 return retval;
1028 end_unlink:
1029 pathrelse(&path);
1030 err = journal_end(&th, dir->i_sb, jbegin_count);
1031 reiserfs_check_path(&path);
1032 if (err)
1033 retval = err;
1034 out_unlink:
1035 reiserfs_write_unlock(dir->i_sb);
1036 return retval;
1039 static int reiserfs_symlink(struct inode *parent_dir,
1040 struct dentry *dentry, const char *symname)
1042 int retval;
1043 struct inode *inode;
1044 char *name;
1045 int item_len;
1046 struct reiserfs_transaction_handle th;
1047 int mode = S_IFLNK | S_IRWXUGO;
1048 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1049 int jbegin_count =
1050 JOURNAL_PER_BALANCE_CNT * 3 +
1051 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1052 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1054 if (!(inode = new_inode(parent_dir->i_sb))) {
1055 return -ENOMEM;
1057 new_inode_init(inode, parent_dir, mode);
1059 reiserfs_write_lock(parent_dir->i_sb);
1060 item_len = ROUND_UP(strlen(symname));
1061 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1062 retval = -ENAMETOOLONG;
1063 drop_new_inode(inode);
1064 goto out_failed;
1067 name = kmalloc(item_len, GFP_NOFS);
1068 if (!name) {
1069 drop_new_inode(inode);
1070 retval = -ENOMEM;
1071 goto out_failed;
1073 memcpy(name, symname, strlen(symname));
1074 padd_item(name, item_len, strlen(symname));
1076 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1078 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1079 if (retval) {
1080 drop_new_inode(inode);
1081 kfree(name);
1082 goto out_failed;
1085 retval =
1086 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1087 dentry, inode);
1088 kfree(name);
1089 if (retval) { /* reiserfs_new_inode iputs for us */
1090 goto out_failed;
1093 reiserfs_update_inode_transaction(inode);
1094 reiserfs_update_inode_transaction(parent_dir);
1096 inode->i_op = &reiserfs_symlink_inode_operations;
1097 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1099 // must be sure this inode is written with this transaction
1101 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1103 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1104 dentry->d_name.len, inode, 1 /*visible */ );
1105 if (retval) {
1106 int err;
1107 inode->i_nlink--;
1108 reiserfs_update_sd(&th, inode);
1109 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1110 if (err)
1111 retval = err;
1112 iput(inode);
1113 goto out_failed;
1116 d_instantiate(dentry, inode);
1117 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1118 out_failed:
1119 reiserfs_write_unlock(parent_dir->i_sb);
1120 return retval;
1123 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1124 struct dentry *dentry)
1126 int retval;
1127 struct inode *inode = old_dentry->d_inode;
1128 struct reiserfs_transaction_handle th;
1129 /* We need blocks for transaction + update of quotas for the owners of the directory */
1130 int jbegin_count =
1131 JOURNAL_PER_BALANCE_CNT * 3 +
1132 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1134 reiserfs_write_lock(dir->i_sb);
1135 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1136 //FIXME: sd_nlink is 32 bit for new files
1137 reiserfs_write_unlock(dir->i_sb);
1138 return -EMLINK;
1140 if (inode->i_nlink == 0) {
1141 reiserfs_write_unlock(dir->i_sb);
1142 return -ENOENT;
1145 /* inc before scheduling so reiserfs_unlink knows we are here */
1146 inode->i_nlink++;
1148 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1149 if (retval) {
1150 inode->i_nlink--;
1151 reiserfs_write_unlock(dir->i_sb);
1152 return retval;
1155 /* create new entry */
1156 retval =
1157 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1158 dentry->d_name.len, inode, 1 /*visible */ );
1160 reiserfs_update_inode_transaction(inode);
1161 reiserfs_update_inode_transaction(dir);
1163 if (retval) {
1164 int err;
1165 inode->i_nlink--;
1166 err = journal_end(&th, dir->i_sb, jbegin_count);
1167 reiserfs_write_unlock(dir->i_sb);
1168 return err ? err : retval;
1171 inode->i_ctime = CURRENT_TIME_SEC;
1172 reiserfs_update_sd(&th, inode);
1174 atomic_inc(&inode->i_count);
1175 d_instantiate(dentry, inode);
1176 retval = journal_end(&th, dir->i_sb, jbegin_count);
1177 reiserfs_write_unlock(dir->i_sb);
1178 return retval;
1181 // de contains information pointing to an entry which
1182 static int de_still_valid(const char *name, int len,
1183 struct reiserfs_dir_entry *de)
1185 struct reiserfs_dir_entry tmp = *de;
1187 // recalculate pointer to name and name length
1188 set_de_name_and_namelen(&tmp);
1189 // FIXME: could check more
1190 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1191 return 0;
1192 return 1;
1195 static int entry_points_to_object(const char *name, int len,
1196 struct reiserfs_dir_entry *de,
1197 struct inode *inode)
1199 if (!de_still_valid(name, len, de))
1200 return 0;
1202 if (inode) {
1203 if (!de_visible(de->de_deh + de->de_entry_num))
1204 reiserfs_panic(NULL,
1205 "vs-7042: entry_points_to_object: entry must be visible");
1206 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1209 /* this must be added hidden entry */
1210 if (de_visible(de->de_deh + de->de_entry_num))
1211 reiserfs_panic(NULL,
1212 "vs-7043: entry_points_to_object: entry must be visible");
1214 return 1;
1217 /* sets key of objectid the entry has to point to */
1218 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1219 struct reiserfs_key *key)
1221 /* JDM These operations are endian safe - both are le */
1222 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1223 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1227 * process, that is going to call fix_nodes/do_balance must hold only
1228 * one path. If it holds 2 or more, it can get into endless waiting in
1229 * get_empty_nodes or its clones
1231 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1232 struct inode *new_dir, struct dentry *new_dentry)
1234 int retval;
1235 INITIALIZE_PATH(old_entry_path);
1236 INITIALIZE_PATH(new_entry_path);
1237 INITIALIZE_PATH(dot_dot_entry_path);
1238 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1239 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1240 struct inode *old_inode, *new_dentry_inode;
1241 struct reiserfs_transaction_handle th;
1242 int jbegin_count;
1243 umode_t old_inode_mode;
1244 unsigned long savelink = 1;
1245 struct timespec ctime;
1247 /* three balancings: (1) old name removal, (2) new name insertion
1248 and (3) maybe "save" link insertion
1249 stat data updates: (1) old directory,
1250 (2) new directory and (3) maybe old object stat data (when it is
1251 directory) and (4) maybe stat data of object to which new entry
1252 pointed initially and (5) maybe block containing ".." of
1253 renamed directory
1254 quota updates: two parent directories */
1255 jbegin_count =
1256 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1257 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1259 old_inode = old_dentry->d_inode;
1260 new_dentry_inode = new_dentry->d_inode;
1262 // make sure, that oldname still exists and points to an object we
1263 // are going to rename
1264 old_de.de_gen_number_bit_string = NULL;
1265 reiserfs_write_lock(old_dir->i_sb);
1266 retval =
1267 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1268 old_dentry->d_name.len, &old_entry_path,
1269 &old_de);
1270 pathrelse(&old_entry_path);
1271 if (retval == IO_ERROR) {
1272 reiserfs_write_unlock(old_dir->i_sb);
1273 return -EIO;
1276 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1277 reiserfs_write_unlock(old_dir->i_sb);
1278 return -ENOENT;
1281 old_inode_mode = old_inode->i_mode;
1282 if (S_ISDIR(old_inode_mode)) {
1283 // make sure, that directory being renamed has correct ".."
1284 // and that its new parent directory has not too many links
1285 // already
1287 if (new_dentry_inode) {
1288 if (!reiserfs_empty_dir(new_dentry_inode)) {
1289 reiserfs_write_unlock(old_dir->i_sb);
1290 return -ENOTEMPTY;
1294 /* directory is renamed, its parent directory will be changed,
1295 ** so find ".." entry
1297 dot_dot_de.de_gen_number_bit_string = NULL;
1298 retval =
1299 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1300 &dot_dot_de);
1301 pathrelse(&dot_dot_entry_path);
1302 if (retval != NAME_FOUND) {
1303 reiserfs_write_unlock(old_dir->i_sb);
1304 return -EIO;
1307 /* inode number of .. must equal old_dir->i_ino */
1308 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1309 reiserfs_write_unlock(old_dir->i_sb);
1310 return -EIO;
1314 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1315 if (retval) {
1316 reiserfs_write_unlock(old_dir->i_sb);
1317 return retval;
1320 /* add new entry (or find the existing one) */
1321 retval =
1322 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1323 new_dentry->d_name.len, old_inode, 0);
1324 if (retval == -EEXIST) {
1325 if (!new_dentry_inode) {
1326 reiserfs_panic(old_dir->i_sb,
1327 "vs-7050: new entry is found, new inode == 0\n");
1329 } else if (retval) {
1330 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1331 reiserfs_write_unlock(old_dir->i_sb);
1332 return err ? err : retval;
1335 reiserfs_update_inode_transaction(old_dir);
1336 reiserfs_update_inode_transaction(new_dir);
1338 /* this makes it so an fsync on an open fd for the old name will
1339 ** commit the rename operation
1341 reiserfs_update_inode_transaction(old_inode);
1343 if (new_dentry_inode)
1344 reiserfs_update_inode_transaction(new_dentry_inode);
1346 while (1) {
1347 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1348 if ((retval =
1349 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1350 &old_entry_path,
1351 &old_de)) != NAME_FOUND) {
1352 pathrelse(&old_entry_path);
1353 journal_end(&th, old_dir->i_sb, jbegin_count);
1354 reiserfs_write_unlock(old_dir->i_sb);
1355 return -EIO;
1358 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
1360 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1362 // look for new name by reiserfs_find_entry
1363 new_de.de_gen_number_bit_string = NULL;
1364 retval =
1365 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1366 new_dentry->d_name.len, &new_entry_path,
1367 &new_de);
1368 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1369 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1370 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1371 pathrelse(&new_entry_path);
1372 pathrelse(&old_entry_path);
1373 journal_end(&th, old_dir->i_sb, jbegin_count);
1374 reiserfs_write_unlock(old_dir->i_sb);
1375 return -EIO;
1378 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1380 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1382 if (S_ISDIR(old_inode->i_mode)) {
1383 if ((retval =
1384 search_by_entry_key(new_dir->i_sb,
1385 &dot_dot_de.de_entry_key,
1386 &dot_dot_entry_path,
1387 &dot_dot_de)) != NAME_FOUND) {
1388 pathrelse(&dot_dot_entry_path);
1389 pathrelse(&new_entry_path);
1390 pathrelse(&old_entry_path);
1391 journal_end(&th, old_dir->i_sb, jbegin_count);
1392 reiserfs_write_unlock(old_dir->i_sb);
1393 return -EIO;
1395 copy_item_head(&dot_dot_ih,
1396 get_ih(&dot_dot_entry_path));
1397 // node containing ".." gets into transaction
1398 reiserfs_prepare_for_journal(old_inode->i_sb,
1399 dot_dot_de.de_bh, 1);
1401 /* we should check seals here, not do
1402 this stuff, yes? Then, having
1403 gathered everything into RAM we
1404 should lock the buffers, yes? -Hans */
1405 /* probably. our rename needs to hold more
1406 ** than one path at once. The seals would
1407 ** have to be written to deal with multi-path
1408 ** issues -chris
1410 /* sanity checking before doing the rename - avoid races many
1411 ** of the above checks could have scheduled. We have to be
1412 ** sure our items haven't been shifted by another process.
1414 if (item_moved(&new_entry_ih, &new_entry_path) ||
1415 !entry_points_to_object(new_dentry->d_name.name,
1416 new_dentry->d_name.len,
1417 &new_de, new_dentry_inode) ||
1418 item_moved(&old_entry_ih, &old_entry_path) ||
1419 !entry_points_to_object(old_dentry->d_name.name,
1420 old_dentry->d_name.len,
1421 &old_de, old_inode)) {
1422 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1423 new_de.de_bh);
1424 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1425 old_de.de_bh);
1426 if (S_ISDIR(old_inode_mode))
1427 reiserfs_restore_prepared_buffer(old_inode->
1428 i_sb,
1429 dot_dot_de.
1430 de_bh);
1431 continue;
1433 if (S_ISDIR(old_inode_mode)) {
1434 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1435 !entry_points_to_object("..", 2, &dot_dot_de,
1436 old_dir)) {
1437 reiserfs_restore_prepared_buffer(old_inode->
1438 i_sb,
1439 old_de.de_bh);
1440 reiserfs_restore_prepared_buffer(old_inode->
1441 i_sb,
1442 new_de.de_bh);
1443 reiserfs_restore_prepared_buffer(old_inode->
1444 i_sb,
1445 dot_dot_de.
1446 de_bh);
1447 continue;
1451 RFALSE(S_ISDIR(old_inode_mode) &&
1452 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1454 break;
1457 /* ok, all the changes can be done in one fell swoop when we
1458 have claimed all the buffers needed. */
1460 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1461 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1462 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1464 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1465 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1466 ctime = CURRENT_TIME_SEC;
1467 old_dir->i_ctime = old_dir->i_mtime = ctime;
1468 new_dir->i_ctime = new_dir->i_mtime = ctime;
1469 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1470 renamed object */
1471 old_inode->i_ctime = ctime;
1473 if (new_dentry_inode) {
1474 // adjust link number of the victim
1475 if (S_ISDIR(new_dentry_inode->i_mode)) {
1476 new_dentry_inode->i_nlink = 0;
1477 } else {
1478 drop_nlink(new_dentry_inode);
1480 new_dentry_inode->i_ctime = ctime;
1481 savelink = new_dentry_inode->i_nlink;
1484 if (S_ISDIR(old_inode_mode)) {
1485 // adjust ".." of renamed directory
1486 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1487 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1489 if (!new_dentry_inode)
1490 /* there (in new_dir) was no directory, so it got new link
1491 (".." of renamed directory) */
1492 INC_DIR_INODE_NLINK(new_dir);
1494 /* old directory lost one link - ".. " of renamed directory */
1495 DEC_DIR_INODE_NLINK(old_dir);
1497 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1498 pathrelse(&new_entry_path);
1499 pathrelse(&dot_dot_entry_path);
1501 // FIXME: this reiserfs_cut_from_item's return value may screw up
1502 // anybody, but it will panic if will not be able to find the
1503 // entry. This needs one more clean up
1504 if (reiserfs_cut_from_item
1505 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1506 0) < 0)
1507 reiserfs_warning(old_dir->i_sb,
1508 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1510 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1512 reiserfs_update_sd(&th, old_dir);
1513 reiserfs_update_sd(&th, new_dir);
1514 reiserfs_update_sd(&th, old_inode);
1516 if (new_dentry_inode) {
1517 if (savelink == 0)
1518 add_save_link(&th, new_dentry_inode,
1519 0 /* not truncate */ );
1520 reiserfs_update_sd(&th, new_dentry_inode);
1523 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1524 reiserfs_write_unlock(old_dir->i_sb);
1525 return retval;
1529 * directories can handle most operations...
1531 struct inode_operations reiserfs_dir_inode_operations = {
1532 //&reiserfs_dir_operations, /* default_file_ops */
1533 .create = reiserfs_create,
1534 .lookup = reiserfs_lookup,
1535 .link = reiserfs_link,
1536 .unlink = reiserfs_unlink,
1537 .symlink = reiserfs_symlink,
1538 .mkdir = reiserfs_mkdir,
1539 .rmdir = reiserfs_rmdir,
1540 .mknod = reiserfs_mknod,
1541 .rename = reiserfs_rename,
1542 .setattr = reiserfs_setattr,
1543 .setxattr = reiserfs_setxattr,
1544 .getxattr = reiserfs_getxattr,
1545 .listxattr = reiserfs_listxattr,
1546 .removexattr = reiserfs_removexattr,
1547 .permission = reiserfs_permission,
1551 * symlink operations.. same as page_symlink_inode_operations, with xattr
1552 * stuff added
1554 struct inode_operations reiserfs_symlink_inode_operations = {
1555 .readlink = generic_readlink,
1556 .follow_link = page_follow_link_light,
1557 .put_link = page_put_link,
1558 .setattr = reiserfs_setattr,
1559 .setxattr = reiserfs_setxattr,
1560 .getxattr = reiserfs_getxattr,
1561 .listxattr = reiserfs_listxattr,
1562 .removexattr = reiserfs_removexattr,
1563 .permission = reiserfs_permission,
1568 * special file operations.. just xattr/acl stuff
1570 struct inode_operations reiserfs_special_inode_operations = {
1571 .setattr = reiserfs_setattr,
1572 .setxattr = reiserfs_setxattr,
1573 .getxattr = reiserfs_getxattr,
1574 .listxattr = reiserfs_listxattr,
1575 .removexattr = reiserfs_removexattr,
1576 .permission = reiserfs_permission,