drivers/md/raid5.c: fix printk warnings
[wandboard.git] / fs / reiserfs / namei.c
blobb378eea332caefaaf069e91f75db339cc6c9b3b2
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/quotaops.h>
21 #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; }
22 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
24 // directory item contains array of entry headers. This performs
25 // binary search through that array
26 static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
28 struct item_head *ih = de->de_ih;
29 struct reiserfs_de_head *deh = de->de_deh;
30 int rbound, lbound, j;
32 lbound = 0;
33 rbound = I_ENTRY_COUNT(ih) - 1;
35 for (j = (rbound + lbound) / 2; lbound <= rbound;
36 j = (rbound + lbound) / 2) {
37 if (off < deh_offset(deh + j)) {
38 rbound = j - 1;
39 continue;
41 if (off > deh_offset(deh + j)) {
42 lbound = j + 1;
43 continue;
45 // this is not name found, but matched third key component
46 de->de_entry_num = j;
47 return NAME_FOUND;
50 de->de_entry_num = lbound;
51 return NAME_NOT_FOUND;
54 // comment? maybe something like set de to point to what the path points to?
55 static inline void set_de_item_location(struct reiserfs_dir_entry *de,
56 struct treepath *path)
58 de->de_bh = get_last_bh(path);
59 de->de_ih = get_ih(path);
60 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
61 de->de_item_num = PATH_LAST_POSITION(path);
64 // de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
65 inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
67 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
69 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
71 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
72 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
73 de->de_name = B_I_PITEM(de->de_bh, de->de_ih) + deh_location(deh);
74 if (de->de_name[de->de_namelen - 1] == 0)
75 de->de_namelen = strlen(de->de_name);
78 // what entry points to
79 static inline void set_de_object_key(struct reiserfs_dir_entry *de)
81 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
82 de->de_dir_id = deh_dir_id(&(de->de_deh[de->de_entry_num]));
83 de->de_objectid = deh_objectid(&(de->de_deh[de->de_entry_num]));
86 static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
88 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
90 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
92 /* store key of the found entry */
93 de->de_entry_key.version = KEY_FORMAT_3_5;
94 de->de_entry_key.on_disk_key.k_dir_id =
95 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
96 de->de_entry_key.on_disk_key.k_objectid =
97 le32_to_cpu(de->de_ih->ih_key.k_objectid);
98 set_cpu_key_k_offset(&(de->de_entry_key), deh_offset(deh));
99 set_cpu_key_k_type(&(de->de_entry_key), TYPE_DIRENTRY);
102 /* We assign a key to each directory item, and place multiple entries
103 in a single directory item. A directory item has a key equal to the
104 key of the first directory entry in it.
106 This function first calls search_by_key, then, if item whose first
107 entry matches is not found it looks for the entry inside directory
108 item found by search_by_key. Fills the path to the entry, and to the
109 entry position in the item
113 /* The function is NOT SCHEDULE-SAFE! */
114 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
115 struct treepath *path, struct reiserfs_dir_entry *de)
117 int retval;
119 retval = search_item(sb, key, path);
120 switch (retval) {
121 case ITEM_NOT_FOUND:
122 if (!PATH_LAST_POSITION(path)) {
123 reiserfs_warning(sb,
124 "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
125 pathrelse(path);
126 return IO_ERROR;
128 PATH_LAST_POSITION(path)--;
130 case ITEM_FOUND:
131 break;
133 case IO_ERROR:
134 return retval;
136 default:
137 pathrelse(path);
138 reiserfs_warning(sb,
139 "vs-7002: search_by_entry_key: no path to here");
140 return IO_ERROR;
143 set_de_item_location(de, path);
145 #ifdef CONFIG_REISERFS_CHECK
146 if (!is_direntry_le_ih(de->de_ih) ||
147 COMP_SHORT_KEYS(&(de->de_ih->ih_key), key)) {
148 print_block(de->de_bh, 0, -1, -1);
149 reiserfs_panic(sb,
150 "vs-7005: search_by_entry_key: found item %h is not directory item or "
151 "does not belong to the same directory as key %K",
152 de->de_ih, key);
154 #endif /* CONFIG_REISERFS_CHECK */
156 /* binary search in directory item by third componen t of the
157 key. sets de->de_entry_num of de */
158 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
159 path->pos_in_item = de->de_entry_num;
160 if (retval != NAME_NOT_FOUND) {
161 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
162 set_de_name_and_namelen(de);
163 set_de_object_key(de);
165 return retval;
168 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
170 /* The third component is hashed, and you can choose from more than
171 one hash function. Per directory hashes are not yet implemented
172 but are thought about. This function should be moved to hashes.c
173 Jedi, please do so. -Hans */
175 static __u32 get_third_component(struct super_block *s,
176 const char *name, int len)
178 __u32 res;
180 if (!len || (len == 1 && name[0] == '.'))
181 return DOT_OFFSET;
182 if (len == 2 && name[0] == '.' && name[1] == '.')
183 return DOT_DOT_OFFSET;
185 res = REISERFS_SB(s)->s_hash_function(name, len);
187 // take bits from 7-th to 30-th including both bounds
188 res = GET_HASH_VALUE(res);
189 if (res == 0)
190 // needed to have no names before "." and ".." those have hash
191 // value == 0 and generation conters 1 and 2 accordingly
192 res = 128;
193 return res + MAX_GENERATION_NUMBER;
196 static int reiserfs_match(struct reiserfs_dir_entry *de,
197 const char *name, int namelen)
199 int retval = NAME_NOT_FOUND;
201 if ((namelen == de->de_namelen) &&
202 !memcmp(de->de_name, name, de->de_namelen))
203 retval =
204 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
205 NAME_FOUND_INVISIBLE);
207 return retval;
210 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
212 /* used when hash collisions exist */
214 static int linear_search_in_dir_item(struct cpu_key *key,
215 struct reiserfs_dir_entry *de,
216 const char *name, int namelen)
218 struct reiserfs_de_head *deh = de->de_deh;
219 int retval;
220 int i;
222 i = de->de_entry_num;
224 if (i == I_ENTRY_COUNT(de->de_ih) ||
225 GET_HASH_VALUE(deh_offset(deh + i)) !=
226 GET_HASH_VALUE(cpu_key_k_offset(key))) {
227 i--;
230 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
231 "vs-7010: array of entry headers not found");
233 deh += i;
235 for (; i >= 0; i--, deh--) {
236 if (GET_HASH_VALUE(deh_offset(deh)) !=
237 GET_HASH_VALUE(cpu_key_k_offset(key))) {
238 // hash value does not match, no need to check whole name
239 return NAME_NOT_FOUND;
242 /* mark, that this generation number is used */
243 if (de->de_gen_number_bit_string)
244 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
245 de->de_gen_number_bit_string);
247 // calculate pointer to name and namelen
248 de->de_entry_num = i;
249 set_de_name_and_namelen(de);
251 if ((retval =
252 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
253 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
255 // key of pointed object
256 set_de_object_key(de);
258 store_de_entry_key(de);
260 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
261 return retval;
265 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
266 /* we have reached left most entry in the node. In common we
267 have to go to the left neighbor, but if generation counter
268 is 0 already, we know for sure, that there is no name with
269 the same hash value */
270 // FIXME: this work correctly only because hash value can not
271 // be 0. Btw, in case of Yura's hash it is probably possible,
272 // so, this is a bug
273 return NAME_NOT_FOUND;
275 RFALSE(de->de_item_num,
276 "vs-7015: two diritems of the same directory in one node?");
278 return GOTO_PREVIOUS_ITEM;
281 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
282 // FIXME: should add something like IOERROR
283 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
284 struct treepath *path_to_entry,
285 struct reiserfs_dir_entry *de)
287 struct cpu_key key_to_search;
288 int retval;
290 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
291 return NAME_NOT_FOUND;
293 /* we will search for this key in the tree */
294 make_cpu_key(&key_to_search, dir,
295 get_third_component(dir->i_sb, name, namelen),
296 TYPE_DIRENTRY, 3);
298 while (1) {
299 retval =
300 search_by_entry_key(dir->i_sb, &key_to_search,
301 path_to_entry, de);
302 if (retval == IO_ERROR) {
303 reiserfs_warning(dir->i_sb, "zam-7001: io error in %s",
304 __FUNCTION__);
305 return IO_ERROR;
308 /* compare names for all entries having given hash value */
309 retval =
310 linear_search_in_dir_item(&key_to_search, de, name,
311 namelen);
312 if (retval != GOTO_PREVIOUS_ITEM) {
313 /* there is no need to scan directory anymore. Given entry found or does not exist */
314 path_to_entry->pos_in_item = de->de_entry_num;
315 return retval;
318 /* there is left neighboring item of this directory and given entry can be there */
319 set_cpu_key_k_offset(&key_to_search,
320 le_ih_k_offset(de->de_ih) - 1);
321 pathrelse(path_to_entry);
323 } /* while (1) */
326 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
327 struct nameidata *nd)
329 int retval;
330 struct inode *inode = NULL;
331 struct reiserfs_dir_entry de;
332 INITIALIZE_PATH(path_to_entry);
334 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
335 return ERR_PTR(-ENAMETOOLONG);
337 reiserfs_write_lock(dir->i_sb);
338 de.de_gen_number_bit_string = NULL;
339 retval =
340 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
341 &path_to_entry, &de);
342 pathrelse(&path_to_entry);
343 if (retval == NAME_FOUND) {
344 /* Hide the .reiserfs_priv directory */
345 if (reiserfs_xattrs(dir->i_sb) &&
346 !old_format_only(dir->i_sb) &&
347 REISERFS_SB(dir->i_sb)->priv_root &&
348 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
349 de.de_objectid ==
350 le32_to_cpu(INODE_PKEY
351 (REISERFS_SB(dir->i_sb)->priv_root->d_inode)->
352 k_objectid)) {
353 reiserfs_write_unlock(dir->i_sb);
354 return ERR_PTR(-EACCES);
357 inode =
358 reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
359 if (!inode || IS_ERR(inode)) {
360 reiserfs_write_unlock(dir->i_sb);
361 return ERR_PTR(-EACCES);
364 /* Propogate the priv_object flag so we know we're in the priv tree */
365 if (is_reiserfs_priv_object(dir))
366 reiserfs_mark_inode_private(inode);
368 reiserfs_write_unlock(dir->i_sb);
369 if (retval == IO_ERROR) {
370 return ERR_PTR(-EIO);
373 return d_splice_alias(inode, dentry);
377 ** looks up the dentry of the parent directory for child.
378 ** taken from ext2_get_parent
380 struct dentry *reiserfs_get_parent(struct dentry *child)
382 int retval;
383 struct inode *inode = NULL;
384 struct reiserfs_dir_entry de;
385 INITIALIZE_PATH(path_to_entry);
386 struct dentry *parent;
387 struct inode *dir = child->d_inode;
389 if (dir->i_nlink == 0) {
390 return ERR_PTR(-ENOENT);
392 de.de_gen_number_bit_string = NULL;
394 reiserfs_write_lock(dir->i_sb);
395 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
396 pathrelse(&path_to_entry);
397 if (retval != NAME_FOUND) {
398 reiserfs_write_unlock(dir->i_sb);
399 return ERR_PTR(-ENOENT);
401 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
402 reiserfs_write_unlock(dir->i_sb);
404 if (!inode || IS_ERR(inode)) {
405 return ERR_PTR(-EACCES);
407 parent = d_alloc_anon(inode);
408 if (!parent) {
409 iput(inode);
410 parent = ERR_PTR(-ENOMEM);
412 return parent;
415 /* add entry to the directory (entry can be hidden).
417 insert definition of when hidden directories are used here -Hans
419 Does not mark dir inode dirty, do it after successesfull call to it */
421 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
422 struct inode *dir, const char *name, int namelen,
423 struct inode *inode, int visible)
425 struct cpu_key entry_key;
426 struct reiserfs_de_head *deh;
427 INITIALIZE_PATH(path);
428 struct reiserfs_dir_entry de;
429 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
430 int gen_number;
431 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
432 if we create file with short name */
433 char *buffer;
434 int buflen, paste_size;
435 int retval;
437 BUG_ON(!th->t_trans_id);
439 /* cannot allow items to be added into a busy deleted directory */
440 if (!namelen)
441 return -EINVAL;
443 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
444 return -ENAMETOOLONG;
446 /* each entry has unique key. compose it */
447 make_cpu_key(&entry_key, dir,
448 get_third_component(dir->i_sb, name, namelen),
449 TYPE_DIRENTRY, 3);
451 /* get memory for composing the entry */
452 buflen = DEH_SIZE + ROUND_UP(namelen);
453 if (buflen > sizeof(small_buf)) {
454 buffer = kmalloc(buflen, GFP_NOFS);
455 if (buffer == 0)
456 return -ENOMEM;
457 } else
458 buffer = small_buf;
460 paste_size =
461 (get_inode_sd_version(dir) ==
462 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
464 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
465 deh = (struct reiserfs_de_head *)buffer;
466 deh->deh_location = 0; /* JDM Endian safe if 0 */
467 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
468 deh->deh_state = 0; /* JDM Endian safe if 0 */
469 /* put key (ino analog) to de */
470 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
471 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
473 /* copy name */
474 memcpy((char *)(deh + 1), name, namelen);
475 /* padd by 0s to the 4 byte boundary */
476 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
478 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
479 mark_de_without_sd(deh);
480 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
482 /* find the proper place for the new entry */
483 memset(bit_string, 0, sizeof(bit_string));
484 de.de_gen_number_bit_string = bit_string;
485 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
486 if (retval != NAME_NOT_FOUND) {
487 if (buffer != small_buf)
488 kfree(buffer);
489 pathrelse(&path);
491 if (retval == IO_ERROR) {
492 return -EIO;
495 if (retval != NAME_FOUND) {
496 reiserfs_warning(dir->i_sb,
497 "zam-7002:%s: \"reiserfs_find_entry\" "
498 "has returned unexpected value (%d)",
499 __FUNCTION__, retval);
502 return -EEXIST;
505 gen_number =
506 find_first_zero_bit(bit_string,
507 MAX_GENERATION_NUMBER + 1);
508 if (gen_number > MAX_GENERATION_NUMBER) {
509 /* there is no free generation number */
510 reiserfs_warning(dir->i_sb,
511 "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
512 if (buffer != small_buf)
513 kfree(buffer);
514 pathrelse(&path);
515 return -EBUSY;
517 /* adjust offset of directory enrty */
518 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
519 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
521 /* update max-hash-collisions counter in reiserfs_sb_info */
522 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
524 if (gen_number != 0) { /* we need to re-search for the insertion point */
525 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
526 NAME_NOT_FOUND) {
527 reiserfs_warning(dir->i_sb,
528 "vs-7032: reiserfs_add_entry: "
529 "entry with this key (%K) already exists",
530 &entry_key);
532 if (buffer != small_buf)
533 kfree(buffer);
534 pathrelse(&path);
535 return -EBUSY;
539 /* perform the insertion of the entry that we have prepared */
540 retval =
541 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
542 paste_size);
543 if (buffer != small_buf)
544 kfree(buffer);
545 if (retval) {
546 reiserfs_check_path(&path);
547 return retval;
550 dir->i_size += paste_size;
551 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
552 if (!S_ISDIR(inode->i_mode) && visible)
553 // reiserfs_mkdir or reiserfs_rename will do that by itself
554 reiserfs_update_sd(th, dir);
556 reiserfs_check_path(&path);
557 return 0;
560 /* quota utility function, call if you've had to abort after calling
561 ** new_inode_init, and have not called reiserfs_new_inode yet.
562 ** This should only be called on inodes that do not have stat data
563 ** inserted into the tree yet.
565 static int drop_new_inode(struct inode *inode)
567 DQUOT_DROP(inode);
568 make_bad_inode(inode);
569 inode->i_flags |= S_NOQUOTA;
570 iput(inode);
571 return 0;
574 /* utility function that does setup for reiserfs_new_inode.
575 ** DQUOT_INIT needs lots of credits so it's better to have it
576 ** outside of a transaction, so we had to pull some bits of
577 ** reiserfs_new_inode out into this func.
579 static int new_inode_init(struct inode *inode, struct inode *dir, int mode)
582 /* the quota init calls have to know who to charge the quota to, so
583 ** we have to set uid and gid here
585 inode->i_uid = current->fsuid;
586 inode->i_mode = mode;
587 /* Make inode invalid - just in case we are going to drop it before
588 * the initialization happens */
589 INODE_PKEY(inode)->k_objectid = 0;
591 if (dir->i_mode & S_ISGID) {
592 inode->i_gid = dir->i_gid;
593 if (S_ISDIR(mode))
594 inode->i_mode |= S_ISGID;
595 } else {
596 inode->i_gid = current->fsgid;
598 DQUOT_INIT(inode);
599 return 0;
602 static int reiserfs_create(struct inode *dir, struct dentry *dentry, int mode,
603 struct nameidata *nd)
605 int retval;
606 struct inode *inode;
607 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
608 int jbegin_count =
609 JOURNAL_PER_BALANCE_CNT * 2 +
610 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
611 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
612 struct reiserfs_transaction_handle th;
613 int locked;
615 if (!(inode = new_inode(dir->i_sb))) {
616 return -ENOMEM;
618 new_inode_init(inode, dir, mode);
620 locked = reiserfs_cache_default_acl(dir);
622 reiserfs_write_lock(dir->i_sb);
624 if (locked)
625 reiserfs_write_lock_xattrs(dir->i_sb);
627 retval = journal_begin(&th, dir->i_sb, jbegin_count);
628 if (retval) {
629 drop_new_inode(inode);
630 goto out_failed;
633 retval =
634 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
635 inode);
636 if (retval)
637 goto out_failed;
639 if (locked) {
640 reiserfs_write_unlock_xattrs(dir->i_sb);
641 locked = 0;
644 inode->i_op = &reiserfs_file_inode_operations;
645 inode->i_fop = &reiserfs_file_operations;
646 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
648 retval =
649 reiserfs_add_entry(&th, dir, dentry->d_name.name,
650 dentry->d_name.len, inode, 1 /*visible */ );
651 if (retval) {
652 int err;
653 inode->i_nlink--;
654 reiserfs_update_sd(&th, inode);
655 err = journal_end(&th, dir->i_sb, jbegin_count);
656 if (err)
657 retval = err;
658 iput(inode);
659 goto out_failed;
661 reiserfs_update_inode_transaction(inode);
662 reiserfs_update_inode_transaction(dir);
664 d_instantiate(dentry, inode);
665 retval = journal_end(&th, dir->i_sb, jbegin_count);
667 out_failed:
668 if (locked)
669 reiserfs_write_unlock_xattrs(dir->i_sb);
670 reiserfs_write_unlock(dir->i_sb);
671 return retval;
674 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
675 dev_t rdev)
677 int retval;
678 struct inode *inode;
679 struct reiserfs_transaction_handle th;
680 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
681 int jbegin_count =
682 JOURNAL_PER_BALANCE_CNT * 3 +
683 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
684 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
685 int locked;
687 if (!new_valid_dev(rdev))
688 return -EINVAL;
690 if (!(inode = new_inode(dir->i_sb))) {
691 return -ENOMEM;
693 new_inode_init(inode, dir, mode);
695 locked = reiserfs_cache_default_acl(dir);
697 reiserfs_write_lock(dir->i_sb);
699 if (locked)
700 reiserfs_write_lock_xattrs(dir->i_sb);
702 retval = journal_begin(&th, dir->i_sb, jbegin_count);
703 if (retval) {
704 drop_new_inode(inode);
705 goto out_failed;
708 retval =
709 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
710 inode);
711 if (retval) {
712 goto out_failed;
715 if (locked) {
716 reiserfs_write_unlock_xattrs(dir->i_sb);
717 locked = 0;
720 inode->i_op = &reiserfs_special_inode_operations;
721 init_special_inode(inode, inode->i_mode, rdev);
723 //FIXME: needed for block and char devices only
724 reiserfs_update_sd(&th, inode);
726 reiserfs_update_inode_transaction(inode);
727 reiserfs_update_inode_transaction(dir);
729 retval =
730 reiserfs_add_entry(&th, dir, dentry->d_name.name,
731 dentry->d_name.len, inode, 1 /*visible */ );
732 if (retval) {
733 int err;
734 inode->i_nlink--;
735 reiserfs_update_sd(&th, inode);
736 err = journal_end(&th, dir->i_sb, jbegin_count);
737 if (err)
738 retval = err;
739 iput(inode);
740 goto out_failed;
743 d_instantiate(dentry, inode);
744 retval = journal_end(&th, dir->i_sb, jbegin_count);
746 out_failed:
747 if (locked)
748 reiserfs_write_unlock_xattrs(dir->i_sb);
749 reiserfs_write_unlock(dir->i_sb);
750 return retval;
753 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
755 int retval;
756 struct inode *inode;
757 struct reiserfs_transaction_handle th;
758 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
759 int jbegin_count =
760 JOURNAL_PER_BALANCE_CNT * 3 +
761 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
762 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
763 int locked;
765 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
766 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
767 REISERFS_I(dir)->new_packing_locality = 1;
768 #endif
769 mode = S_IFDIR | mode;
770 if (!(inode = new_inode(dir->i_sb))) {
771 return -ENOMEM;
773 new_inode_init(inode, dir, mode);
775 locked = reiserfs_cache_default_acl(dir);
777 reiserfs_write_lock(dir->i_sb);
778 if (locked)
779 reiserfs_write_lock_xattrs(dir->i_sb);
781 retval = journal_begin(&th, dir->i_sb, jbegin_count);
782 if (retval) {
783 drop_new_inode(inode);
784 goto out_failed;
787 /* inc the link count now, so another writer doesn't overflow it while
788 ** we sleep later on.
790 INC_DIR_INODE_NLINK(dir)
792 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
793 old_format_only(dir->i_sb) ?
794 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
795 dentry, inode);
796 if (retval) {
797 dir->i_nlink--;
798 goto out_failed;
801 if (locked) {
802 reiserfs_write_unlock_xattrs(dir->i_sb);
803 locked = 0;
806 reiserfs_update_inode_transaction(inode);
807 reiserfs_update_inode_transaction(dir);
809 inode->i_op = &reiserfs_dir_inode_operations;
810 inode->i_fop = &reiserfs_dir_operations;
812 // note, _this_ add_entry will not update dir's stat data
813 retval =
814 reiserfs_add_entry(&th, dir, dentry->d_name.name,
815 dentry->d_name.len, inode, 1 /*visible */ );
816 if (retval) {
817 int err;
818 inode->i_nlink = 0;
819 DEC_DIR_INODE_NLINK(dir);
820 reiserfs_update_sd(&th, inode);
821 err = journal_end(&th, dir->i_sb, jbegin_count);
822 if (err)
823 retval = err;
824 iput(inode);
825 goto out_failed;
827 // the above add_entry did not update dir's stat data
828 reiserfs_update_sd(&th, dir);
830 d_instantiate(dentry, inode);
831 retval = journal_end(&th, dir->i_sb, jbegin_count);
832 out_failed:
833 if (locked)
834 reiserfs_write_unlock_xattrs(dir->i_sb);
835 reiserfs_write_unlock(dir->i_sb);
836 return retval;
839 static inline int reiserfs_empty_dir(struct inode *inode)
841 /* we can cheat because an old format dir cannot have
842 ** EMPTY_DIR_SIZE, and a new format dir cannot have
843 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
844 ** regardless of disk format version, the directory is empty.
846 if (inode->i_size != EMPTY_DIR_SIZE &&
847 inode->i_size != EMPTY_DIR_SIZE_V1) {
848 return 0;
850 return 1;
853 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
855 int retval, err;
856 struct inode *inode;
857 struct reiserfs_transaction_handle th;
858 int jbegin_count;
859 INITIALIZE_PATH(path);
860 struct reiserfs_dir_entry de;
862 /* we will be doing 2 balancings and update 2 stat data, we change quotas
863 * of the owner of the directory and of the owner of the parent directory.
864 * The quota structure is possibly deleted only on last iput => outside
865 * of this transaction */
866 jbegin_count =
867 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
868 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
870 reiserfs_write_lock(dir->i_sb);
871 retval = journal_begin(&th, dir->i_sb, jbegin_count);
872 if (retval)
873 goto out_rmdir;
875 de.de_gen_number_bit_string = NULL;
876 if ((retval =
877 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
878 &path, &de)) == NAME_NOT_FOUND) {
879 retval = -ENOENT;
880 goto end_rmdir;
881 } else if (retval == IO_ERROR) {
882 retval = -EIO;
883 goto end_rmdir;
886 inode = dentry->d_inode;
888 reiserfs_update_inode_transaction(inode);
889 reiserfs_update_inode_transaction(dir);
891 if (de.de_objectid != inode->i_ino) {
892 // FIXME: compare key of an object and a key found in the
893 // entry
894 retval = -EIO;
895 goto end_rmdir;
897 if (!reiserfs_empty_dir(inode)) {
898 retval = -ENOTEMPTY;
899 goto end_rmdir;
902 /* cut entry from dir directory */
903 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
904 0 /*new file size - not used here */ );
905 if (retval < 0)
906 goto end_rmdir;
908 if (inode->i_nlink != 2 && inode->i_nlink != 1)
909 reiserfs_warning(inode->i_sb, "%s: empty directory has nlink "
910 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
912 clear_nlink(inode);
913 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
914 reiserfs_update_sd(&th, inode);
916 DEC_DIR_INODE_NLINK(dir)
917 dir->i_size -= (DEH_SIZE + de.de_entrylen);
918 reiserfs_update_sd(&th, dir);
920 /* prevent empty directory from getting lost */
921 add_save_link(&th, inode, 0 /* not truncate */ );
923 retval = journal_end(&th, dir->i_sb, jbegin_count);
924 reiserfs_check_path(&path);
925 out_rmdir:
926 reiserfs_write_unlock(dir->i_sb);
927 return retval;
929 end_rmdir:
930 /* we must release path, because we did not call
931 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
932 release path if operation was not complete */
933 pathrelse(&path);
934 err = journal_end(&th, dir->i_sb, jbegin_count);
935 reiserfs_write_unlock(dir->i_sb);
936 return err ? err : retval;
939 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
941 int retval, err;
942 struct inode *inode;
943 struct reiserfs_dir_entry de;
944 INITIALIZE_PATH(path);
945 struct reiserfs_transaction_handle th;
946 int jbegin_count;
947 unsigned long savelink;
949 inode = dentry->d_inode;
951 /* in this transaction we can be doing at max two balancings and update
952 * two stat datas, we change quotas of the owner of the directory and of
953 * the owner of the parent directory. The quota structure is possibly
954 * deleted only on iput => outside of this transaction */
955 jbegin_count =
956 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
957 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
959 reiserfs_write_lock(dir->i_sb);
960 retval = journal_begin(&th, dir->i_sb, jbegin_count);
961 if (retval)
962 goto out_unlink;
964 de.de_gen_number_bit_string = NULL;
965 if ((retval =
966 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
967 &path, &de)) == NAME_NOT_FOUND) {
968 retval = -ENOENT;
969 goto end_unlink;
970 } else if (retval == IO_ERROR) {
971 retval = -EIO;
972 goto end_unlink;
975 reiserfs_update_inode_transaction(inode);
976 reiserfs_update_inode_transaction(dir);
978 if (de.de_objectid != inode->i_ino) {
979 // FIXME: compare key of an object and a key found in the
980 // entry
981 retval = -EIO;
982 goto end_unlink;
985 if (!inode->i_nlink) {
986 reiserfs_warning(inode->i_sb, "%s: deleting nonexistent file "
987 "(%s:%lu), %d", __FUNCTION__,
988 reiserfs_bdevname(inode->i_sb), inode->i_ino,
989 inode->i_nlink);
990 inode->i_nlink = 1;
993 drop_nlink(inode);
996 * we schedule before doing the add_save_link call, save the link
997 * count so we don't race
999 savelink = inode->i_nlink;
1001 retval =
1002 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
1004 if (retval < 0) {
1005 inc_nlink(inode);
1006 goto end_unlink;
1008 inode->i_ctime = CURRENT_TIME_SEC;
1009 reiserfs_update_sd(&th, inode);
1011 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1012 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
1013 reiserfs_update_sd(&th, dir);
1015 if (!savelink)
1016 /* prevent file from getting lost */
1017 add_save_link(&th, inode, 0 /* not truncate */ );
1019 retval = journal_end(&th, dir->i_sb, jbegin_count);
1020 reiserfs_check_path(&path);
1021 reiserfs_write_unlock(dir->i_sb);
1022 return retval;
1024 end_unlink:
1025 pathrelse(&path);
1026 err = journal_end(&th, dir->i_sb, jbegin_count);
1027 reiserfs_check_path(&path);
1028 if (err)
1029 retval = err;
1030 out_unlink:
1031 reiserfs_write_unlock(dir->i_sb);
1032 return retval;
1035 static int reiserfs_symlink(struct inode *parent_dir,
1036 struct dentry *dentry, const char *symname)
1038 int retval;
1039 struct inode *inode;
1040 char *name;
1041 int item_len;
1042 struct reiserfs_transaction_handle th;
1043 int mode = S_IFLNK | S_IRWXUGO;
1044 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1045 int jbegin_count =
1046 JOURNAL_PER_BALANCE_CNT * 3 +
1047 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1048 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1050 if (!(inode = new_inode(parent_dir->i_sb))) {
1051 return -ENOMEM;
1053 new_inode_init(inode, parent_dir, mode);
1055 reiserfs_write_lock(parent_dir->i_sb);
1056 item_len = ROUND_UP(strlen(symname));
1057 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1058 retval = -ENAMETOOLONG;
1059 drop_new_inode(inode);
1060 goto out_failed;
1063 name = kmalloc(item_len, GFP_NOFS);
1064 if (!name) {
1065 drop_new_inode(inode);
1066 retval = -ENOMEM;
1067 goto out_failed;
1069 memcpy(name, symname, strlen(symname));
1070 padd_item(name, item_len, strlen(symname));
1072 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1074 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1075 if (retval) {
1076 drop_new_inode(inode);
1077 kfree(name);
1078 goto out_failed;
1081 retval =
1082 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1083 dentry, inode);
1084 kfree(name);
1085 if (retval) { /* reiserfs_new_inode iputs for us */
1086 goto out_failed;
1089 reiserfs_update_inode_transaction(inode);
1090 reiserfs_update_inode_transaction(parent_dir);
1092 inode->i_op = &reiserfs_symlink_inode_operations;
1093 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1095 // must be sure this inode is written with this transaction
1097 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1099 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1100 dentry->d_name.len, inode, 1 /*visible */ );
1101 if (retval) {
1102 int err;
1103 inode->i_nlink--;
1104 reiserfs_update_sd(&th, inode);
1105 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1106 if (err)
1107 retval = err;
1108 iput(inode);
1109 goto out_failed;
1112 d_instantiate(dentry, inode);
1113 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1114 out_failed:
1115 reiserfs_write_unlock(parent_dir->i_sb);
1116 return retval;
1119 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1120 struct dentry *dentry)
1122 int retval;
1123 struct inode *inode = old_dentry->d_inode;
1124 struct reiserfs_transaction_handle th;
1125 /* We need blocks for transaction + update of quotas for the owners of the directory */
1126 int jbegin_count =
1127 JOURNAL_PER_BALANCE_CNT * 3 +
1128 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1130 reiserfs_write_lock(dir->i_sb);
1131 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1132 //FIXME: sd_nlink is 32 bit for new files
1133 reiserfs_write_unlock(dir->i_sb);
1134 return -EMLINK;
1136 if (inode->i_nlink == 0) {
1137 reiserfs_write_unlock(dir->i_sb);
1138 return -ENOENT;
1141 /* inc before scheduling so reiserfs_unlink knows we are here */
1142 inc_nlink(inode);
1144 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1145 if (retval) {
1146 inode->i_nlink--;
1147 reiserfs_write_unlock(dir->i_sb);
1148 return retval;
1151 /* create new entry */
1152 retval =
1153 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1154 dentry->d_name.len, inode, 1 /*visible */ );
1156 reiserfs_update_inode_transaction(inode);
1157 reiserfs_update_inode_transaction(dir);
1159 if (retval) {
1160 int err;
1161 inode->i_nlink--;
1162 err = journal_end(&th, dir->i_sb, jbegin_count);
1163 reiserfs_write_unlock(dir->i_sb);
1164 return err ? err : retval;
1167 inode->i_ctime = CURRENT_TIME_SEC;
1168 reiserfs_update_sd(&th, inode);
1170 atomic_inc(&inode->i_count);
1171 d_instantiate(dentry, inode);
1172 retval = journal_end(&th, dir->i_sb, jbegin_count);
1173 reiserfs_write_unlock(dir->i_sb);
1174 return retval;
1177 // de contains information pointing to an entry which
1178 static int de_still_valid(const char *name, int len,
1179 struct reiserfs_dir_entry *de)
1181 struct reiserfs_dir_entry tmp = *de;
1183 // recalculate pointer to name and name length
1184 set_de_name_and_namelen(&tmp);
1185 // FIXME: could check more
1186 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1187 return 0;
1188 return 1;
1191 static int entry_points_to_object(const char *name, int len,
1192 struct reiserfs_dir_entry *de,
1193 struct inode *inode)
1195 if (!de_still_valid(name, len, de))
1196 return 0;
1198 if (inode) {
1199 if (!de_visible(de->de_deh + de->de_entry_num))
1200 reiserfs_panic(NULL,
1201 "vs-7042: entry_points_to_object: entry must be visible");
1202 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1205 /* this must be added hidden entry */
1206 if (de_visible(de->de_deh + de->de_entry_num))
1207 reiserfs_panic(NULL,
1208 "vs-7043: entry_points_to_object: entry must be visible");
1210 return 1;
1213 /* sets key of objectid the entry has to point to */
1214 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1215 struct reiserfs_key *key)
1217 /* JDM These operations are endian safe - both are le */
1218 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1219 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1223 * process, that is going to call fix_nodes/do_balance must hold only
1224 * one path. If it holds 2 or more, it can get into endless waiting in
1225 * get_empty_nodes or its clones
1227 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1228 struct inode *new_dir, struct dentry *new_dentry)
1230 int retval;
1231 INITIALIZE_PATH(old_entry_path);
1232 INITIALIZE_PATH(new_entry_path);
1233 INITIALIZE_PATH(dot_dot_entry_path);
1234 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1235 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1236 struct inode *old_inode, *new_dentry_inode;
1237 struct reiserfs_transaction_handle th;
1238 int jbegin_count;
1239 umode_t old_inode_mode;
1240 unsigned long savelink = 1;
1241 struct timespec ctime;
1243 /* three balancings: (1) old name removal, (2) new name insertion
1244 and (3) maybe "save" link insertion
1245 stat data updates: (1) old directory,
1246 (2) new directory and (3) maybe old object stat data (when it is
1247 directory) and (4) maybe stat data of object to which new entry
1248 pointed initially and (5) maybe block containing ".." of
1249 renamed directory
1250 quota updates: two parent directories */
1251 jbegin_count =
1252 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1253 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1255 old_inode = old_dentry->d_inode;
1256 new_dentry_inode = new_dentry->d_inode;
1258 // make sure, that oldname still exists and points to an object we
1259 // are going to rename
1260 old_de.de_gen_number_bit_string = NULL;
1261 reiserfs_write_lock(old_dir->i_sb);
1262 retval =
1263 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1264 old_dentry->d_name.len, &old_entry_path,
1265 &old_de);
1266 pathrelse(&old_entry_path);
1267 if (retval == IO_ERROR) {
1268 reiserfs_write_unlock(old_dir->i_sb);
1269 return -EIO;
1272 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1273 reiserfs_write_unlock(old_dir->i_sb);
1274 return -ENOENT;
1277 old_inode_mode = old_inode->i_mode;
1278 if (S_ISDIR(old_inode_mode)) {
1279 // make sure, that directory being renamed has correct ".."
1280 // and that its new parent directory has not too many links
1281 // already
1283 if (new_dentry_inode) {
1284 if (!reiserfs_empty_dir(new_dentry_inode)) {
1285 reiserfs_write_unlock(old_dir->i_sb);
1286 return -ENOTEMPTY;
1290 /* directory is renamed, its parent directory will be changed,
1291 ** so find ".." entry
1293 dot_dot_de.de_gen_number_bit_string = NULL;
1294 retval =
1295 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1296 &dot_dot_de);
1297 pathrelse(&dot_dot_entry_path);
1298 if (retval != NAME_FOUND) {
1299 reiserfs_write_unlock(old_dir->i_sb);
1300 return -EIO;
1303 /* inode number of .. must equal old_dir->i_ino */
1304 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1305 reiserfs_write_unlock(old_dir->i_sb);
1306 return -EIO;
1310 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1311 if (retval) {
1312 reiserfs_write_unlock(old_dir->i_sb);
1313 return retval;
1316 /* add new entry (or find the existing one) */
1317 retval =
1318 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1319 new_dentry->d_name.len, old_inode, 0);
1320 if (retval == -EEXIST) {
1321 if (!new_dentry_inode) {
1322 reiserfs_panic(old_dir->i_sb,
1323 "vs-7050: new entry is found, new inode == 0\n");
1325 } else if (retval) {
1326 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1327 reiserfs_write_unlock(old_dir->i_sb);
1328 return err ? err : retval;
1331 reiserfs_update_inode_transaction(old_dir);
1332 reiserfs_update_inode_transaction(new_dir);
1334 /* this makes it so an fsync on an open fd for the old name will
1335 ** commit the rename operation
1337 reiserfs_update_inode_transaction(old_inode);
1339 if (new_dentry_inode)
1340 reiserfs_update_inode_transaction(new_dentry_inode);
1342 while (1) {
1343 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1344 if ((retval =
1345 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1346 &old_entry_path,
1347 &old_de)) != NAME_FOUND) {
1348 pathrelse(&old_entry_path);
1349 journal_end(&th, old_dir->i_sb, jbegin_count);
1350 reiserfs_write_unlock(old_dir->i_sb);
1351 return -EIO;
1354 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
1356 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1358 // look for new name by reiserfs_find_entry
1359 new_de.de_gen_number_bit_string = NULL;
1360 retval =
1361 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1362 new_dentry->d_name.len, &new_entry_path,
1363 &new_de);
1364 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1365 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1366 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1367 pathrelse(&new_entry_path);
1368 pathrelse(&old_entry_path);
1369 journal_end(&th, old_dir->i_sb, jbegin_count);
1370 reiserfs_write_unlock(old_dir->i_sb);
1371 return -EIO;
1374 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1376 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1378 if (S_ISDIR(old_inode->i_mode)) {
1379 if ((retval =
1380 search_by_entry_key(new_dir->i_sb,
1381 &dot_dot_de.de_entry_key,
1382 &dot_dot_entry_path,
1383 &dot_dot_de)) != NAME_FOUND) {
1384 pathrelse(&dot_dot_entry_path);
1385 pathrelse(&new_entry_path);
1386 pathrelse(&old_entry_path);
1387 journal_end(&th, old_dir->i_sb, jbegin_count);
1388 reiserfs_write_unlock(old_dir->i_sb);
1389 return -EIO;
1391 copy_item_head(&dot_dot_ih,
1392 get_ih(&dot_dot_entry_path));
1393 // node containing ".." gets into transaction
1394 reiserfs_prepare_for_journal(old_inode->i_sb,
1395 dot_dot_de.de_bh, 1);
1397 /* we should check seals here, not do
1398 this stuff, yes? Then, having
1399 gathered everything into RAM we
1400 should lock the buffers, yes? -Hans */
1401 /* probably. our rename needs to hold more
1402 ** than one path at once. The seals would
1403 ** have to be written to deal with multi-path
1404 ** issues -chris
1406 /* sanity checking before doing the rename - avoid races many
1407 ** of the above checks could have scheduled. We have to be
1408 ** sure our items haven't been shifted by another process.
1410 if (item_moved(&new_entry_ih, &new_entry_path) ||
1411 !entry_points_to_object(new_dentry->d_name.name,
1412 new_dentry->d_name.len,
1413 &new_de, new_dentry_inode) ||
1414 item_moved(&old_entry_ih, &old_entry_path) ||
1415 !entry_points_to_object(old_dentry->d_name.name,
1416 old_dentry->d_name.len,
1417 &old_de, old_inode)) {
1418 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1419 new_de.de_bh);
1420 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1421 old_de.de_bh);
1422 if (S_ISDIR(old_inode_mode))
1423 reiserfs_restore_prepared_buffer(old_inode->
1424 i_sb,
1425 dot_dot_de.
1426 de_bh);
1427 continue;
1429 if (S_ISDIR(old_inode_mode)) {
1430 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1431 !entry_points_to_object("..", 2, &dot_dot_de,
1432 old_dir)) {
1433 reiserfs_restore_prepared_buffer(old_inode->
1434 i_sb,
1435 old_de.de_bh);
1436 reiserfs_restore_prepared_buffer(old_inode->
1437 i_sb,
1438 new_de.de_bh);
1439 reiserfs_restore_prepared_buffer(old_inode->
1440 i_sb,
1441 dot_dot_de.
1442 de_bh);
1443 continue;
1447 RFALSE(S_ISDIR(old_inode_mode) &&
1448 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1450 break;
1453 /* ok, all the changes can be done in one fell swoop when we
1454 have claimed all the buffers needed. */
1456 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1457 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1458 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1460 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1461 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1462 ctime = CURRENT_TIME_SEC;
1463 old_dir->i_ctime = old_dir->i_mtime = ctime;
1464 new_dir->i_ctime = new_dir->i_mtime = ctime;
1465 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1466 renamed object */
1467 old_inode->i_ctime = ctime;
1469 if (new_dentry_inode) {
1470 // adjust link number of the victim
1471 if (S_ISDIR(new_dentry_inode->i_mode)) {
1472 clear_nlink(new_dentry_inode);
1473 } else {
1474 drop_nlink(new_dentry_inode);
1476 new_dentry_inode->i_ctime = ctime;
1477 savelink = new_dentry_inode->i_nlink;
1480 if (S_ISDIR(old_inode_mode)) {
1481 // adjust ".." of renamed directory
1482 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1483 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1485 if (!new_dentry_inode)
1486 /* there (in new_dir) was no directory, so it got new link
1487 (".." of renamed directory) */
1488 INC_DIR_INODE_NLINK(new_dir);
1490 /* old directory lost one link - ".. " of renamed directory */
1491 DEC_DIR_INODE_NLINK(old_dir);
1493 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1494 pathrelse(&new_entry_path);
1495 pathrelse(&dot_dot_entry_path);
1497 // FIXME: this reiserfs_cut_from_item's return value may screw up
1498 // anybody, but it will panic if will not be able to find the
1499 // entry. This needs one more clean up
1500 if (reiserfs_cut_from_item
1501 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1502 0) < 0)
1503 reiserfs_warning(old_dir->i_sb,
1504 "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1506 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1508 reiserfs_update_sd(&th, old_dir);
1509 reiserfs_update_sd(&th, new_dir);
1510 reiserfs_update_sd(&th, old_inode);
1512 if (new_dentry_inode) {
1513 if (savelink == 0)
1514 add_save_link(&th, new_dentry_inode,
1515 0 /* not truncate */ );
1516 reiserfs_update_sd(&th, new_dentry_inode);
1519 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1520 reiserfs_write_unlock(old_dir->i_sb);
1521 return retval;
1525 * directories can handle most operations...
1527 const struct inode_operations reiserfs_dir_inode_operations = {
1528 //&reiserfs_dir_operations, /* default_file_ops */
1529 .create = reiserfs_create,
1530 .lookup = reiserfs_lookup,
1531 .link = reiserfs_link,
1532 .unlink = reiserfs_unlink,
1533 .symlink = reiserfs_symlink,
1534 .mkdir = reiserfs_mkdir,
1535 .rmdir = reiserfs_rmdir,
1536 .mknod = reiserfs_mknod,
1537 .rename = reiserfs_rename,
1538 .setattr = reiserfs_setattr,
1539 .setxattr = reiserfs_setxattr,
1540 .getxattr = reiserfs_getxattr,
1541 .listxattr = reiserfs_listxattr,
1542 .removexattr = reiserfs_removexattr,
1543 .permission = reiserfs_permission,
1547 * symlink operations.. same as page_symlink_inode_operations, with xattr
1548 * stuff added
1550 const struct inode_operations reiserfs_symlink_inode_operations = {
1551 .readlink = generic_readlink,
1552 .follow_link = page_follow_link_light,
1553 .put_link = page_put_link,
1554 .setattr = reiserfs_setattr,
1555 .setxattr = reiserfs_setxattr,
1556 .getxattr = reiserfs_getxattr,
1557 .listxattr = reiserfs_listxattr,
1558 .removexattr = reiserfs_removexattr,
1559 .permission = reiserfs_permission,
1564 * special file operations.. just xattr/acl stuff
1566 const struct inode_operations reiserfs_special_inode_operations = {
1567 .setattr = reiserfs_setattr,
1568 .setxattr = reiserfs_setxattr,
1569 .getxattr = reiserfs_getxattr,
1570 .listxattr = reiserfs_listxattr,
1571 .removexattr = reiserfs_removexattr,
1572 .permission = reiserfs_permission,