93151fb285c02ed25881de6994d4b98f93f7c73d
[linux-flexiantxendom0-3.2.10.git] / fs / reiserfs / namei.c
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  *
4  * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
5  *
6  * Trivial Changes:
7  * Rights granted to Hans Reiser to redistribute under other terms providing
8  * he accepts all liability including but not limited to patent, fitness
9  * for purpose, and direct or indirect claims arising from failure to perform.
10  *
11  * NO WARRANTY
12  */
13
14 #include <linux/config.h>
15 #include <linux/time.h>
16 #include <linux/bitops.h>
17 #include <linux/reiserfs_fs.h>
18 #include <linux/smp_lock.h>
19
20 #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; }
21 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) i->i_nlink--;
22
23 // directory item contains array of entry headers. This performs
24 // binary search through that array
25 static int bin_search_in_dir_item (struct reiserfs_dir_entry * de, loff_t off)
26 {
27     struct item_head * ih = de->de_ih;
28     struct reiserfs_de_head * deh = de->de_deh;
29     int rbound, lbound, j;
30
31     lbound = 0;
32     rbound = I_ENTRY_COUNT (ih) - 1;
33
34     for (j = (rbound + lbound) / 2; lbound <= rbound; j = (rbound + lbound) / 2) {
35         if (off < deh_offset (deh + j)) {
36             rbound = j - 1;
37             continue;
38         }
39         if (off > deh_offset (deh + j)) {
40             lbound = j + 1;
41             continue;
42         }
43         // this is not name found, but matched third key component
44         de->de_entry_num = j;
45         return NAME_FOUND;
46     }
47
48     de->de_entry_num = lbound;
49     return NAME_NOT_FOUND;
50 }
51
52
53 // comment?  maybe something like set de to point to what the path points to?
54 static inline void set_de_item_location (struct reiserfs_dir_entry * de, struct path * path)
55 {
56     de->de_bh = get_last_bh (path);
57     de->de_ih = get_ih (path);
58     de->de_deh = B_I_DEH (de->de_bh, de->de_ih);
59     de->de_item_num = PATH_LAST_POSITION (path);
60
61
62
63 // de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
64 inline void set_de_name_and_namelen (struct reiserfs_dir_entry * de)
65 {
66     struct reiserfs_de_head * deh = de->de_deh + de->de_entry_num;
67
68     if (de->de_entry_num >= ih_entry_count (de->de_ih))
69         BUG ();
70
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);
76 }
77
78
79 // what entry points to
80 static inline void set_de_object_key (struct reiserfs_dir_entry * de)
81 {
82     if (de->de_entry_num >= ih_entry_count (de->de_ih))
83         BUG ();
84     de->de_dir_id = deh_dir_id( &(de->de_deh[de->de_entry_num]));
85     de->de_objectid = deh_objectid( &(de->de_deh[de->de_entry_num]));
86 }
87
88
89 static inline void store_de_entry_key (struct reiserfs_dir_entry * de)
90 {
91     struct reiserfs_de_head * deh = de->de_deh + de->de_entry_num;
92
93     if (de->de_entry_num >= ih_entry_count (de->de_ih))
94         BUG ();
95
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 = le32_to_cpu (de->de_ih->ih_key.k_dir_id);
99     de->de_entry_key.on_disk_key.k_objectid = le32_to_cpu (de->de_ih->ih_key.k_objectid);
100     set_cpu_key_k_offset (&(de->de_entry_key), deh_offset (deh));
101     set_cpu_key_k_type (&(de->de_entry_key), TYPE_DIRENTRY);
102 }
103
104
105 /* We assign a key to each directory item, and place multiple entries
106 in a single directory item.  A directory item has a key equal to the
107 key of the first directory entry in it.
108
109 This function first calls search_by_key, then, if item whose first
110 entry matches is not found it looks for the entry inside directory
111 item found by search_by_key. Fills the path to the entry, and to the
112 entry position in the item 
113
114 */
115
116 /* The function is NOT SCHEDULE-SAFE! */
117 int search_by_entry_key (struct super_block * sb, const struct cpu_key * key,
118                          struct path * path, struct reiserfs_dir_entry * de)
119 {
120     int retval;
121
122     retval = search_item (sb, key, path);
123     switch (retval) {
124     case ITEM_NOT_FOUND:
125         if (!PATH_LAST_POSITION (path)) {
126             reiserfs_warning ("vs-7000: search_by_entry_key: search_by_key returned item position == 0");
127             pathrelse(path) ;
128             return IO_ERROR ;
129         }
130         PATH_LAST_POSITION (path) --;
131
132     case ITEM_FOUND:
133         break;
134
135     case IO_ERROR:
136         return retval;
137
138     default:
139         pathrelse (path);
140         reiserfs_warning ("vs-7002: search_by_entry_key: no path to here");
141         return IO_ERROR;
142     }
143
144     set_de_item_location (de, path);
145
146 #ifdef CONFIG_REISERFS_CHECK
147     if (!is_direntry_le_ih (de->de_ih) || 
148         COMP_SHORT_KEYS (&(de->de_ih->ih_key), key)) {
149         print_block (de->de_bh, 0, -1, -1);
150         reiserfs_panic (sb, "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", de->de_ih, key);
152     }
153 #endif /* CONFIG_REISERFS_CHECK */
154
155     /* binary search in directory item by third componen t of the
156        key. sets de->de_entry_num of de */
157     retval = bin_search_in_dir_item (de, cpu_key_k_offset (key));
158     path->pos_in_item = de->de_entry_num;
159     if (retval != NAME_NOT_FOUND) {
160         // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
161         set_de_name_and_namelen (de);
162         set_de_object_key (de);
163     }
164     return retval;
165 }
166
167
168
169 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
170
171 /* The third component is hashed, and you can choose from more than
172    one hash function.  Per directory hashes are not yet implemented
173    but are thought about. This function should be moved to hashes.c
174    Jedi, please do so.  -Hans */
175
176 static __u32 get_third_component (struct super_block * s, 
177                                   const char * name, int len)
178 {
179     __u32 res;
180
181     if (!len || (len == 1 && name[0] == '.'))
182         return DOT_OFFSET;
183     if (len == 2 && name[0] == '.' && name[1] == '.')
184         return DOT_DOT_OFFSET;
185
186     res = REISERFS_SB(s)->s_hash_function (name, len);
187
188     // take bits from 7-th to 30-th including both bounds
189     res = GET_HASH_VALUE(res);
190     if (res == 0)
191         // needed to have no names before "." and ".." those have hash
192         // value == 0 and generation conters 1 and 2 accordingly
193         res = 128;
194     return res + MAX_GENERATION_NUMBER;
195 }
196
197
198 static int reiserfs_match (struct reiserfs_dir_entry * de, 
199                            const char * name, int namelen)
200 {
201     int retval = NAME_NOT_FOUND;
202
203     if ((namelen == de->de_namelen) &&
204         !memcmp(de->de_name, name, de->de_namelen))
205         retval = (de_visible (de->de_deh + de->de_entry_num) ? NAME_FOUND : NAME_FOUND_INVISIBLE);
206
207     return retval;
208 }
209
210
211 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
212
213                                 /* used when hash collisions exist */
214
215
216 static int linear_search_in_dir_item (struct cpu_key * key, struct reiserfs_dir_entry * de,
217                                       const char * name, int namelen)
218 {
219     struct reiserfs_de_head * deh = de->de_deh;
220     int retval;
221     int i;
222
223     i = de->de_entry_num;
224
225     if (i == I_ENTRY_COUNT (de->de_ih) ||
226         GET_HASH_VALUE (deh_offset (deh + i)) != GET_HASH_VALUE (cpu_key_k_offset (key))) {
227         i --;
228     }
229
230     RFALSE( de->de_deh != B_I_DEH (de->de_bh, de->de_ih),
231             "vs-7010: array of entry headers not found");
232
233     deh += i;
234
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;
240         }
241    
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)), (unsigned long *)de->de_gen_number_bit_string);
245
246         // calculate pointer to name and namelen
247         de->de_entry_num = i;
248         set_de_name_and_namelen (de);
249
250         if ((retval = reiserfs_match (de, name, namelen)) != NAME_NOT_FOUND) {
251             // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
252
253             // key of pointed object
254             set_de_object_key (de);
255
256             store_de_entry_key (de);
257
258             // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
259             return retval;
260         }
261     }
262
263     if (GET_GENERATION_NUMBER (le_ih_k_offset (de->de_ih)) == 0)
264         /* we have reached left most entry in the node. In common we
265            have to go to the left neighbor, but if generation counter
266            is 0 already, we know for sure, that there is no name with
267            the same hash value */
268         // FIXME: this work correctly only because hash value can not
269         // be 0. Btw, in case of Yura's hash it is probably possible,
270         // so, this is a bug
271         return NAME_NOT_FOUND;
272
273     RFALSE( de->de_item_num,
274             "vs-7015: two diritems of the same directory in one node?");
275
276     return GOTO_PREVIOUS_ITEM;
277 }
278
279
280 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
281 // FIXME: should add something like IOERROR
282 static int reiserfs_find_entry (struct inode * dir, const char * name, int namelen, 
283                                 struct path * path_to_entry, struct reiserfs_dir_entry * de)
284 {
285     struct cpu_key key_to_search;
286     int retval;
287
288
289     if (namelen > REISERFS_MAX_NAME (dir->i_sb->s_blocksize))
290         return NAME_NOT_FOUND;
291
292     /* we will search for this key in the tree */
293     make_cpu_key (&key_to_search, dir, 
294                   get_third_component (dir->i_sb, name, namelen), TYPE_DIRENTRY, 3);
295
296     while (1) {
297         retval = search_by_entry_key (dir->i_sb, &key_to_search, path_to_entry, de);
298         if (retval == IO_ERROR) {
299             reiserfs_warning ("zam-7001: io error in %s\n", __FUNCTION__);
300             return IO_ERROR;
301         }
302
303         /* compare names for all entries having given hash value */
304         retval = linear_search_in_dir_item (&key_to_search, de, name, namelen);
305         if (retval != GOTO_PREVIOUS_ITEM) {
306             /* there is no need to scan directory anymore. Given entry found or does not exist */
307             path_to_entry->pos_in_item = de->de_entry_num;
308             return retval;
309         }
310
311         /* there is left neighboring item of this directory and given entry can be there */
312         set_cpu_key_k_offset (&key_to_search, le_ih_k_offset (de->de_ih) - 1);
313         pathrelse (path_to_entry);
314
315     } /* while (1) */
316 }
317
318
319 static struct dentry * reiserfs_lookup (struct inode * dir, struct dentry * dentry, struct nameidata *nd)
320 {
321     int retval;
322     struct inode * inode = NULL;
323     struct reiserfs_dir_entry de;
324     INITIALIZE_PATH (path_to_entry);
325
326     if (REISERFS_MAX_NAME (dir->i_sb->s_blocksize) < dentry->d_name.len)
327         return ERR_PTR(-ENAMETOOLONG);
328
329     reiserfs_write_lock(dir->i_sb);
330     de.de_gen_number_bit_string = 0;
331     retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &path_to_entry, &de);
332     pathrelse (&path_to_entry);
333     if (retval == NAME_FOUND) {
334         inode = reiserfs_iget (dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
335         if (!inode || IS_ERR(inode)) {
336             reiserfs_write_unlock(dir->i_sb);
337             return ERR_PTR(-EACCES);
338         }
339     }
340     reiserfs_write_unlock(dir->i_sb);
341     if ( retval == IO_ERROR ) {
342         return ERR_PTR(-EIO);
343     }
344
345     if (inode)
346             return d_splice_alias(inode, dentry);
347     
348     d_add(dentry, inode);
349     return NULL;
350 }
351
352
353 /* 
354 ** looks up the dentry of the parent directory for child.
355 ** taken from ext2_get_parent
356 */
357 struct dentry *reiserfs_get_parent(struct dentry *child)
358 {
359     int retval;
360     struct inode * inode = NULL;
361     struct reiserfs_dir_entry de;
362     INITIALIZE_PATH (path_to_entry);
363     struct dentry *parent;
364     struct inode *dir = child->d_inode ;
365
366
367     if (dir->i_nlink == 0) {
368         return ERR_PTR(-ENOENT);
369     }
370     de.de_gen_number_bit_string = 0;
371
372     reiserfs_write_lock(dir->i_sb);
373     retval = reiserfs_find_entry (dir, "..", 2, &path_to_entry, &de);
374     pathrelse (&path_to_entry);
375     if (retval != NAME_FOUND) {
376         reiserfs_write_unlock(dir->i_sb);
377         return ERR_PTR(-ENOENT);
378     }
379     inode = reiserfs_iget (dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
380     reiserfs_write_unlock(dir->i_sb);
381
382     if (!inode || IS_ERR(inode)) {
383         return ERR_PTR(-EACCES);
384     }
385     parent = d_alloc_anon(inode);
386     if (!parent) {
387         iput(inode);
388         parent = ERR_PTR(-ENOMEM);
389     }
390     return parent;
391 }
392
393
394 /* add entry to the directory (entry can be hidden). 
395
396 insert definition of when hidden directories are used here -Hans
397
398  Does not mark dir   inode dirty, do it after successesfull call to it */
399
400 static int reiserfs_add_entry (struct reiserfs_transaction_handle *th, struct inode * dir,
401                                const char * name, int namelen, struct inode * inode,
402                                int visible)
403 {
404     struct cpu_key entry_key;
405     struct reiserfs_de_head * deh;
406     INITIALIZE_PATH (path);
407     struct reiserfs_dir_entry de;
408     int bit_string [MAX_GENERATION_NUMBER / (sizeof(int) * 8) + 1];
409     int gen_number;
410     char small_buf[32+DEH_SIZE] ; /* 48 bytes now and we avoid kmalloc
411                                      if we create file with short name */
412     char * buffer;
413     int buflen, paste_size;
414     int retval;
415
416
417     /* cannot allow items to be added into a busy deleted directory */
418     if (!namelen)
419         return -EINVAL;
420
421     if (namelen > REISERFS_MAX_NAME (dir->i_sb->s_blocksize))
422         return -ENAMETOOLONG;
423
424     /* each entry has unique key. compose it */
425     make_cpu_key (&entry_key, dir, 
426                   get_third_component (dir->i_sb, name, namelen), TYPE_DIRENTRY, 3);
427
428     /* get memory for composing the entry */
429     buflen = DEH_SIZE + ROUND_UP (namelen);
430     if (buflen > sizeof (small_buf)) {
431         buffer = reiserfs_kmalloc (buflen, GFP_NOFS, dir->i_sb);
432         if (buffer == 0)
433             return -ENOMEM;
434     } else
435         buffer = small_buf;
436
437     paste_size = (get_inode_sd_version (dir) == STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
438
439     /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
440     deh = (struct reiserfs_de_head *)buffer;
441     deh->deh_location = 0; /* JDM Endian safe if 0 */
442     put_deh_offset( deh, cpu_key_k_offset( &entry_key ) );
443     deh->deh_state = 0; /* JDM Endian safe if 0 */
444     /* put key (ino analog) to de */
445     deh->deh_dir_id = INODE_PKEY (inode)->k_dir_id; /* safe: k_dir_id is le */
446     deh->deh_objectid = INODE_PKEY (inode)->k_objectid; /* safe: k_objectid is le */
447
448     /* copy name */
449     memcpy ((char *)(deh + 1), name, namelen);
450     /* padd by 0s to the 4 byte boundary */
451     padd_item ((char *)(deh + 1), ROUND_UP (namelen), namelen);
452
453     /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
454     mark_de_without_sd (deh);
455     visible ? mark_de_visible (deh) : mark_de_hidden (deh);
456
457     /* find the proper place for the new entry */
458     memset (bit_string, 0, sizeof (bit_string));
459     de.de_gen_number_bit_string = (char *)bit_string;
460     retval = reiserfs_find_entry (dir, name, namelen, &path, &de);
461     if( retval != NAME_NOT_FOUND ) {
462         if (buffer != small_buf)
463             reiserfs_kfree (buffer, buflen, dir->i_sb);
464         pathrelse (&path);
465
466         if ( retval == IO_ERROR ) {
467             return -EIO;
468         }
469
470         if (retval != NAME_FOUND) {
471             reiserfs_warning ("zam-7002:%s: \"reiserfs_find_entry\" has returned"
472                               " unexpected value (%d)\n", __FUNCTION__, retval);
473        }
474
475         return -EEXIST;
476     }
477
478     gen_number = find_first_zero_bit ((unsigned long *)bit_string, MAX_GENERATION_NUMBER + 1);
479     if (gen_number > MAX_GENERATION_NUMBER) {
480       /* there is no free generation number */
481       reiserfs_warning ("reiserfs_add_entry: Congratulations! we have got hash function screwed up\n");
482       if (buffer != small_buf)
483           reiserfs_kfree (buffer, buflen, dir->i_sb);
484       pathrelse (&path);
485       return -EBUSY;
486     }
487     /* adjust offset of directory enrty */
488     put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
489     set_cpu_key_k_offset (&entry_key, deh_offset(deh));
490  
491     /* update max-hash-collisions counter in reiserfs_sb_info */
492     PROC_INFO_MAX( th -> t_super, max_hash_collisions, gen_number );
493                   
494     if (gen_number != 0) {      /* we need to re-search for the insertion point */
495       if (search_by_entry_key (dir->i_sb, &entry_key, &path, &de) != NAME_NOT_FOUND) {
496             reiserfs_warning ("vs-7032: reiserfs_add_entry: "
497                             "entry with this key (%K) already exists\n", &entry_key);
498
499             if (buffer != small_buf)
500                 reiserfs_kfree (buffer, buflen, dir->i_sb);
501             pathrelse (&path);
502             return -EBUSY;
503         }
504     }
505   
506     /* perform the insertion of the entry that we have prepared */
507     retval = reiserfs_paste_into_item (th, &path, &entry_key, buffer, paste_size);
508     if (buffer != small_buf)
509         reiserfs_kfree (buffer, buflen, dir->i_sb);
510     if (retval) {
511         reiserfs_check_path(&path) ;
512         return retval;
513     }
514
515     dir->i_size += paste_size;
516     dir->i_blocks = ((dir->i_size + 511) >> 9);
517     dir->i_mtime = dir->i_ctime = CURRENT_TIME;
518     if (!S_ISDIR (inode->i_mode) && visible)
519         // reiserfs_mkdir or reiserfs_rename will do that by itself
520         reiserfs_update_sd (th, dir);
521
522     reiserfs_check_path(&path) ;
523     return 0;
524 }
525
526 /* quota utility function, call if you've had to abort after calling
527 ** new_inode_init, and have not called reiserfs_new_inode yet.
528 ** This should only be called on inodes that do not hav stat data
529 ** inserted into the tree yet.
530 */
531 static int drop_new_inode(struct inode *inode) {
532     make_bad_inode(inode) ;
533     iput(inode) ;
534     return 0 ;
535 }
536
537 /* utility function that does setup for reiserfs_new_inode.  
538 ** DQUOT_ALLOC_INODE cannot be called inside a transaction, so we had
539 ** to pull some bits of reiserfs_new_inode out into this func.
540 ** Yes, the actual quota calls are missing, they are part of the quota
541 ** patch.
542 */
543 static int new_inode_init(struct inode *inode, struct inode *dir, int mode) {
544
545     /* the quota init calls have to know who to charge the quota to, so
546     ** we have to set uid and gid here
547     */
548     inode->i_uid = current->fsuid;
549     inode->i_mode = mode;
550
551     if (dir->i_mode & S_ISGID) {
552         inode->i_gid = dir->i_gid;
553         if (S_ISDIR(mode))
554             inode->i_mode |= S_ISGID;
555     } else {
556         inode->i_gid = current->fsgid;
557     }
558     return 0 ;
559 }
560
561 static int reiserfs_create (struct inode * dir, struct dentry *dentry, int mode,
562                 struct nameidata *nd)
563 {
564     int retval;
565     struct inode * inode;
566     int jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 ;
567     struct reiserfs_transaction_handle th ;
568
569     if (!(inode = new_inode(dir->i_sb))) {
570         return -ENOMEM ;
571     }
572     retval = new_inode_init(inode, dir, mode);
573     if (retval)
574         return retval;
575
576     reiserfs_write_lock(dir->i_sb);
577     journal_begin(&th, dir->i_sb, jbegin_count) ;
578     th.t_caller = "create" ;
579     retval = reiserfs_new_inode (&th, dir, mode, 0, 0/*i_size*/, dentry, inode);
580     if (retval) {
581         goto out_failed;
582     }
583         
584     inode->i_op = &reiserfs_file_inode_operations;
585     inode->i_fop = &reiserfs_file_operations;
586     inode->i_mapping->a_ops = &reiserfs_address_space_operations ;
587
588     retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len, 
589                                 inode, 1/*visible*/);
590     if (retval) {
591         inode->i_nlink--;
592         reiserfs_update_sd (&th, inode);
593         journal_end(&th, dir->i_sb, jbegin_count) ;
594         iput (inode);
595         goto out_failed;
596     }
597     reiserfs_update_inode_transaction(inode) ;
598     reiserfs_update_inode_transaction(dir) ;
599
600     d_instantiate(dentry, inode);
601     journal_end(&th, dir->i_sb, jbegin_count) ;
602
603 out_failed:
604     reiserfs_write_unlock(dir->i_sb);
605     return retval;
606 }
607
608
609 static int reiserfs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
610 {
611     int retval;
612     struct inode * inode;
613     struct reiserfs_transaction_handle th ;
614     int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3; 
615
616     if (!(inode = new_inode(dir->i_sb))) {
617         return -ENOMEM ;
618     }
619     retval = new_inode_init(inode, dir, mode);
620     if (retval)
621         return retval;
622
623     reiserfs_write_lock(dir->i_sb);
624     journal_begin(&th, dir->i_sb, jbegin_count) ;
625
626     retval = reiserfs_new_inode (&th, dir, mode, 0, 0/*i_size*/, dentry, inode);
627     if (retval) {
628         goto out_failed;
629     }
630
631     init_special_inode(inode, mode, rdev) ;
632
633     //FIXME: needed for block and char devices only
634     reiserfs_update_sd (&th, inode);
635
636     reiserfs_update_inode_transaction(inode) ;
637     reiserfs_update_inode_transaction(dir) ;
638
639     retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len, 
640                                  inode, 1/*visible*/);
641     if (retval) {
642         inode->i_nlink--;
643         reiserfs_update_sd (&th, inode);
644         journal_end(&th, dir->i_sb, jbegin_count) ;
645         iput (inode);
646         goto out_failed;
647     }
648
649     d_instantiate(dentry, inode);
650     journal_end(&th, dir->i_sb, jbegin_count) ;
651
652 out_failed:
653     reiserfs_write_unlock(dir->i_sb);
654     return retval;
655 }
656
657
658 static int reiserfs_mkdir (struct inode * dir, struct dentry *dentry, int mode)
659 {
660     int retval;
661     struct inode * inode;
662     struct reiserfs_transaction_handle th ;
663     int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3; 
664
665 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
666     /* set flag that new packing locality created and new blocks for the content     * of that directory are not displaced yet */
667     REISERFS_I(dir)->new_packing_locality = 1;
668 #endif
669     mode = S_IFDIR | mode;
670     if (!(inode = new_inode(dir->i_sb))) {
671         return -ENOMEM ;
672     }
673     retval = new_inode_init(inode, dir, mode);
674     if (retval)
675         return retval;
676
677     reiserfs_write_lock(dir->i_sb);
678     journal_begin(&th, dir->i_sb, jbegin_count) ;
679
680     /* inc the link count now, so another writer doesn't overflow it while
681     ** we sleep later on.
682     */
683     INC_DIR_INODE_NLINK(dir)
684
685     retval = reiserfs_new_inode (&th, dir, mode, 0/*symlink*/,
686                                 old_format_only (dir->i_sb) ? 
687                                 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
688                                 dentry, inode);
689     if (retval) {
690         dir->i_nlink-- ;
691         goto out_failed;
692     }
693     reiserfs_update_inode_transaction(inode) ;
694     reiserfs_update_inode_transaction(dir) ;
695
696     inode->i_op = &reiserfs_dir_inode_operations;
697     inode->i_fop = &reiserfs_dir_operations;
698
699     // note, _this_ add_entry will not update dir's stat data
700     retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len, 
701                                 inode, 1/*visible*/);
702     if (retval) {
703         inode->i_nlink = 0;
704         DEC_DIR_INODE_NLINK(dir);
705         reiserfs_update_sd (&th, inode);
706         journal_end(&th, dir->i_sb, jbegin_count) ;
707         iput (inode);
708         goto out_failed;
709     }
710
711     // the above add_entry did not update dir's stat data
712     reiserfs_update_sd (&th, dir);
713
714     d_instantiate(dentry, inode);
715     journal_end(&th, dir->i_sb, jbegin_count) ;
716 out_failed:
717     reiserfs_write_unlock(dir->i_sb);
718     return retval;
719 }
720
721 static inline int reiserfs_empty_dir(struct inode *inode) {
722     /* we can cheat because an old format dir cannot have
723     ** EMPTY_DIR_SIZE, and a new format dir cannot have
724     ** EMPTY_DIR_SIZE_V1.  So, if the inode is either size, 
725     ** regardless of disk format version, the directory is empty.
726     */
727     if (inode->i_size != EMPTY_DIR_SIZE &&
728         inode->i_size != EMPTY_DIR_SIZE_V1) {
729         return 0 ;
730     }
731     return 1 ;
732 }
733
734 static int reiserfs_rmdir (struct inode * dir, struct dentry *dentry)
735 {
736     int retval;
737     struct inode * inode;
738     int windex ;
739     struct reiserfs_transaction_handle th ;
740     int jbegin_count; 
741     INITIALIZE_PATH (path);
742     struct reiserfs_dir_entry de;
743
744
745     /* we will be doing 2 balancings and update 2 stat data */
746     jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 + 2;
747
748     reiserfs_write_lock(dir->i_sb);
749     journal_begin(&th, dir->i_sb, jbegin_count) ;
750     windex = push_journal_writer("reiserfs_rmdir") ;
751
752     de.de_gen_number_bit_string = 0;
753     if ( (retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &path, &de)) == NAME_NOT_FOUND) {
754         retval = -ENOENT;
755         goto end_rmdir;
756     } else if ( retval == IO_ERROR) {
757         retval = -EIO;
758         goto end_rmdir;
759     }
760
761     inode = dentry->d_inode;
762
763     reiserfs_update_inode_transaction(inode) ;
764     reiserfs_update_inode_transaction(dir) ;
765
766     if (de.de_objectid != inode->i_ino) {
767         // FIXME: compare key of an object and a key found in the
768         // entry
769         retval = -EIO;
770         goto end_rmdir;
771     }
772     if (!reiserfs_empty_dir(inode)) {
773         retval = -ENOTEMPTY;
774         goto end_rmdir;
775     }
776
777     /* cut entry from dir directory */
778     retval = reiserfs_cut_from_item (&th, &path, &(de.de_entry_key), dir, 
779                                      NULL, /* page */ 
780                                      0/*new file size - not used here*/);
781     if (retval < 0)
782         goto end_rmdir;
783
784     if ( inode->i_nlink != 2 && inode->i_nlink != 1 )
785         printk ("reiserfs_rmdir: empty directory has nlink != 2 (%d)\n", inode->i_nlink);
786
787     inode->i_nlink = 0;
788     inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
789     reiserfs_update_sd (&th, inode);
790
791     DEC_DIR_INODE_NLINK(dir)
792     dir->i_size -= (DEH_SIZE + de.de_entrylen);
793     dir->i_blocks = ((dir->i_size + 511) >> 9);
794     reiserfs_update_sd (&th, dir);
795
796     /* prevent empty directory from getting lost */
797     add_save_link (&th, inode, 0/* not truncate */);
798
799     pop_journal_writer(windex) ;
800     journal_end(&th, dir->i_sb, jbegin_count) ;
801     reiserfs_check_path(&path) ;
802     reiserfs_write_unlock(dir->i_sb);
803     return 0;
804         
805  end_rmdir:
806     /* we must release path, because we did not call
807        reiserfs_cut_from_item, or reiserfs_cut_from_item does not
808        release path if operation was not complete */
809     pathrelse (&path);
810     pop_journal_writer(windex) ;
811     journal_end(&th, dir->i_sb, jbegin_count) ;
812     reiserfs_write_unlock(dir->i_sb);
813     return retval;      
814 }
815
816 static int reiserfs_unlink (struct inode * dir, struct dentry *dentry)
817 {
818     int retval;
819     struct inode * inode;
820     struct reiserfs_dir_entry de;
821     INITIALIZE_PATH (path);
822     int windex ;
823     struct reiserfs_transaction_handle th ;
824     int jbegin_count;
825
826     inode = dentry->d_inode;
827
828     /* in this transaction we can be doing at max two balancings and update
829        two stat datas */
830     jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 + 2;
831
832     reiserfs_write_lock(dir->i_sb);
833     journal_begin(&th, dir->i_sb, jbegin_count) ;
834     windex = push_journal_writer("reiserfs_unlink") ;
835         
836     de.de_gen_number_bit_string = 0;
837     if ( (retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &path, &de)) == NAME_NOT_FOUND) {
838         retval = -ENOENT;
839         goto end_unlink;
840     } else if (retval == IO_ERROR) {
841         retval = -EIO;
842         goto end_unlink;
843     }
844
845     reiserfs_update_inode_transaction(inode) ;
846     reiserfs_update_inode_transaction(dir) ;
847
848     if (de.de_objectid != inode->i_ino) {
849         // FIXME: compare key of an object and a key found in the
850         // entry
851         retval = -EIO;
852         goto end_unlink;
853     }
854   
855     if (!inode->i_nlink) {
856         printk("reiserfs_unlink: deleting nonexistent file (%s:%lu), %d\n",
857                reiserfs_bdevname (inode->i_sb), inode->i_ino, inode->i_nlink);
858         inode->i_nlink = 1;
859     }
860
861     retval = reiserfs_cut_from_item (&th, &path, &(de.de_entry_key), dir, NULL, 0);
862     if (retval < 0)
863         goto end_unlink;
864
865     inode->i_nlink--;
866     inode->i_ctime = CURRENT_TIME;
867     reiserfs_update_sd (&th, inode);
868
869     dir->i_size -= (de.de_entrylen + DEH_SIZE);
870     dir->i_blocks = ((dir->i_size + 511) >> 9);
871     dir->i_ctime = dir->i_mtime = CURRENT_TIME;
872     reiserfs_update_sd (&th, dir);
873
874     if (!inode->i_nlink)
875        /* prevent file from getting lost */
876        add_save_link (&th, inode, 0/* not truncate */);
877
878     pop_journal_writer(windex) ;
879     journal_end(&th, dir->i_sb, jbegin_count) ;
880     reiserfs_check_path(&path) ;
881     reiserfs_write_unlock(dir->i_sb);
882     return 0;
883
884  end_unlink:
885     pathrelse (&path);
886     pop_journal_writer(windex) ;
887     journal_end(&th, dir->i_sb, jbegin_count) ;
888     reiserfs_check_path(&path) ;
889     reiserfs_write_unlock(dir->i_sb);
890     return retval;
891 }
892
893 static int reiserfs_symlink (struct inode * parent_dir, 
894                             struct dentry * dentry, const char * symname)
895 {
896     int retval;
897     struct inode * inode;
898     char * name;
899     int item_len;
900     struct reiserfs_transaction_handle th ;
901     int mode = S_IFLNK | S_IRWXUGO;
902     int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3; 
903
904     if (!(inode = new_inode(parent_dir->i_sb))) {
905         return -ENOMEM ;
906     }
907     retval = new_inode_init(inode, parent_dir, mode);
908     if (retval) {
909         return retval;
910     }
911
912     reiserfs_write_lock(parent_dir->i_sb);
913     item_len = ROUND_UP (strlen (symname));
914     if (item_len > MAX_DIRECT_ITEM_LEN (parent_dir->i_sb->s_blocksize)) {
915         retval =  -ENAMETOOLONG;
916         drop_new_inode(inode);
917         goto out_failed;
918     }
919   
920     name = reiserfs_kmalloc (item_len, GFP_NOFS, parent_dir->i_sb);
921     if (!name) {
922         drop_new_inode(inode);
923         retval =  -ENOMEM;
924         goto out_failed;
925     }
926     memcpy (name, symname, strlen (symname));
927     padd_item (name, item_len, strlen (symname));
928
929     journal_begin(&th, parent_dir->i_sb, jbegin_count) ;
930
931     retval = reiserfs_new_inode (&th, parent_dir, mode, name, strlen (symname), 
932                                  dentry, inode);
933     reiserfs_kfree (name, item_len, parent_dir->i_sb);
934     if (retval) { /* reiserfs_new_inode iputs for us */
935         goto out_failed;
936     }
937
938     reiserfs_update_inode_transaction(inode) ;
939     reiserfs_update_inode_transaction(parent_dir) ;
940
941     inode->i_op = &page_symlink_inode_operations;
942     inode->i_mapping->a_ops = &reiserfs_address_space_operations;
943
944     // must be sure this inode is written with this transaction
945     //
946     //reiserfs_update_sd (&th, inode, READ_BLOCKS);
947
948     retval = reiserfs_add_entry (&th, parent_dir, dentry->d_name.name, 
949                                  dentry->d_name.len, inode, 1/*visible*/);
950     if (retval) {
951         inode->i_nlink--;
952         reiserfs_update_sd (&th, inode);
953         journal_end(&th, parent_dir->i_sb, jbegin_count) ;
954         iput (inode);
955         goto out_failed;
956     }
957
958     d_instantiate(dentry, inode);
959     journal_end(&th, parent_dir->i_sb, jbegin_count) ;
960 out_failed:
961     reiserfs_write_unlock(parent_dir->i_sb);
962     return retval;
963 }
964
965 static int reiserfs_link (struct dentry * old_dentry, struct inode * dir, struct dentry * dentry)
966 {
967     int retval;
968     struct inode *inode = old_dentry->d_inode;
969     int windex ;
970     struct reiserfs_transaction_handle th ;
971     int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3; 
972
973     reiserfs_write_lock(dir->i_sb);
974     if (inode->i_nlink >= REISERFS_LINK_MAX) {
975         //FIXME: sd_nlink is 32 bit for new files
976         reiserfs_write_unlock(dir->i_sb);
977         return -EMLINK;
978     }
979
980     journal_begin(&th, dir->i_sb, jbegin_count) ;
981     windex = push_journal_writer("reiserfs_link") ;
982
983     /* create new entry */
984     retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len,
985                                  inode, 1/*visible*/);
986
987     reiserfs_update_inode_transaction(inode) ;
988     reiserfs_update_inode_transaction(dir) ;
989
990     if (retval) {
991         pop_journal_writer(windex) ;
992         journal_end(&th, dir->i_sb, jbegin_count) ;
993         reiserfs_write_unlock(dir->i_sb);
994         return retval;
995     }
996
997     inode->i_nlink++;
998     inode->i_ctime = CURRENT_TIME;
999     reiserfs_update_sd (&th, inode);
1000
1001     atomic_inc(&inode->i_count) ;
1002     d_instantiate(dentry, inode);
1003     pop_journal_writer(windex) ;
1004     journal_end(&th, dir->i_sb, jbegin_count) ;
1005     reiserfs_write_unlock(dir->i_sb);
1006     return 0;
1007 }
1008
1009
1010 // de contains information pointing to an entry which 
1011 static int de_still_valid (const char * name, int len, struct reiserfs_dir_entry * de)
1012 {
1013     struct reiserfs_dir_entry tmp = *de;
1014     
1015     // recalculate pointer to name and name length
1016     set_de_name_and_namelen (&tmp);
1017     // FIXME: could check more
1018     if (tmp.de_namelen != len || memcmp (name, de->de_name, len))
1019         return 0;
1020     return 1;
1021 }
1022
1023
1024 static int entry_points_to_object (const char * name, int len, struct reiserfs_dir_entry * de, struct inode * inode)
1025 {
1026     if (!de_still_valid (name, len, de))
1027         return 0;
1028
1029     if (inode) {
1030         if (!de_visible (de->de_deh + de->de_entry_num))
1031             reiserfs_panic (0, "vs-7042: entry_points_to_object: entry must be visible");
1032         return (de->de_objectid == inode->i_ino) ? 1 : 0;
1033     }
1034
1035     /* this must be added hidden entry */
1036     if (de_visible (de->de_deh + de->de_entry_num))
1037         reiserfs_panic (0, "vs-7043: entry_points_to_object: entry must be visible");
1038
1039     return 1;
1040 }
1041
1042
1043 /* sets key of objectid the entry has to point to */
1044 static void set_ino_in_dir_entry (struct reiserfs_dir_entry * de, struct key * key)
1045 {
1046     /* JDM These operations are endian safe - both are le */
1047     de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1048     de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1049 }
1050
1051
1052 /* 
1053  * process, that is going to call fix_nodes/do_balance must hold only
1054  * one path. If it holds 2 or more, it can get into endless waiting in
1055  * get_empty_nodes or its clones 
1056  */
1057 static int reiserfs_rename (struct inode * old_dir, struct dentry *old_dentry,
1058                             struct inode * new_dir, struct dentry *new_dentry)
1059 {
1060     int retval;
1061     INITIALIZE_PATH (old_entry_path);
1062     INITIALIZE_PATH (new_entry_path);
1063     INITIALIZE_PATH (dot_dot_entry_path);
1064     struct item_head new_entry_ih, old_entry_ih, dot_dot_ih ;
1065     struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1066     struct inode * old_inode, * new_dentry_inode;
1067     int windex ;
1068     struct reiserfs_transaction_handle th ;
1069     int jbegin_count ; 
1070     umode_t old_inode_mode;
1071
1072     /* two balancings: old name removal, new name insertion or "save" link,
1073        stat data updates: old directory and new directory and maybe block
1074        containing ".." of renamed directory */
1075     jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 3;
1076
1077     old_inode = old_dentry->d_inode;
1078     new_dentry_inode = new_dentry->d_inode;
1079
1080     // make sure, that oldname still exists and points to an object we
1081     // are going to rename
1082     old_de.de_gen_number_bit_string = 0;
1083     reiserfs_write_lock(old_dir->i_sb);
1084     retval = reiserfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len,
1085                                   &old_entry_path, &old_de);
1086     pathrelse (&old_entry_path);
1087     if (retval == IO_ERROR) {
1088         reiserfs_write_unlock(old_dir->i_sb);
1089         return -EIO;
1090     }
1091
1092     if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1093         reiserfs_write_unlock(old_dir->i_sb);
1094         return -ENOENT;
1095     }
1096
1097     old_inode_mode = old_inode->i_mode;
1098     if (S_ISDIR(old_inode_mode)) {
1099         // make sure, that directory being renamed has correct ".." 
1100         // and that its new parent directory has not too many links
1101         // already
1102
1103         if (new_dentry_inode) {
1104             if (!reiserfs_empty_dir(new_dentry_inode)) {
1105                 reiserfs_write_unlock(old_dir->i_sb);
1106                 return -ENOTEMPTY;
1107             }
1108         }
1109         
1110         /* directory is renamed, its parent directory will be changed, 
1111         ** so find ".." entry 
1112         */
1113         dot_dot_de.de_gen_number_bit_string = 0;
1114         retval = reiserfs_find_entry (old_inode, "..", 2, &dot_dot_entry_path, &dot_dot_de);
1115         pathrelse (&dot_dot_entry_path);
1116         if (retval != NAME_FOUND) {
1117             reiserfs_write_unlock(old_dir->i_sb);
1118             return -EIO;
1119         }
1120
1121         /* inode number of .. must equal old_dir->i_ino */
1122         if (dot_dot_de.de_objectid != old_dir->i_ino) {
1123             reiserfs_write_unlock(old_dir->i_sb);
1124             return -EIO;
1125         }
1126     }
1127
1128     journal_begin(&th, old_dir->i_sb, jbegin_count) ;
1129     windex = push_journal_writer("reiserfs_rename") ;
1130
1131     /* add new entry (or find the existing one) */
1132     retval = reiserfs_add_entry (&th, new_dir, new_dentry->d_name.name, new_dentry->d_name.len, 
1133                                  old_inode, 0);
1134     if (retval == -EEXIST) {
1135         if (!new_dentry_inode) {
1136             reiserfs_panic (old_dir->i_sb,
1137                             "vs-7050: new entry is found, new inode == 0\n");
1138         }
1139     } else if (retval) {
1140         pop_journal_writer(windex) ;
1141         journal_end(&th, old_dir->i_sb, jbegin_count) ;
1142         reiserfs_write_unlock(old_dir->i_sb);
1143         return retval;
1144     }
1145
1146     reiserfs_update_inode_transaction(old_dir) ;
1147     reiserfs_update_inode_transaction(new_dir) ;
1148
1149     /* this makes it so an fsync on an open fd for the old name will
1150     ** commit the rename operation
1151     */
1152     reiserfs_update_inode_transaction(old_inode) ;
1153
1154     if (new_dentry_inode) 
1155         reiserfs_update_inode_transaction(new_dentry_inode) ;
1156
1157     while (1) {
1158         // look for old name using corresponding entry key (found by reiserfs_find_entry)
1159         if (search_by_entry_key (new_dir->i_sb, &old_de.de_entry_key, &old_entry_path, &old_de) != NAME_FOUND)
1160             BUG ();
1161
1162         copy_item_head(&old_entry_ih, get_ih(&old_entry_path)) ;
1163
1164         reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1) ;
1165
1166         // look for new name by reiserfs_find_entry
1167         new_de.de_gen_number_bit_string = 0;
1168         retval = reiserfs_find_entry (new_dir, new_dentry->d_name.name, new_dentry->d_name.len, 
1169                                       &new_entry_path, &new_de);
1170         // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1171         // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1172         if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND)
1173             BUG ();
1174
1175         copy_item_head(&new_entry_ih, get_ih(&new_entry_path)) ;
1176
1177         reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1) ;
1178
1179         if (S_ISDIR(old_inode->i_mode)) {
1180             if (search_by_entry_key (new_dir->i_sb, &dot_dot_de.de_entry_key, &dot_dot_entry_path, &dot_dot_de) != NAME_FOUND)
1181                 BUG ();
1182             copy_item_head(&dot_dot_ih, get_ih(&dot_dot_entry_path)) ;
1183             // node containing ".." gets into transaction
1184             reiserfs_prepare_for_journal(old_inode->i_sb, dot_dot_de.de_bh, 1) ;
1185         }
1186                                 /* we should check seals here, not do
1187                                    this stuff, yes? Then, having
1188                                    gathered everything into RAM we
1189                                    should lock the buffers, yes?  -Hans */
1190                                 /* probably.  our rename needs to hold more 
1191                                 ** than one path at once.  The seals would 
1192                                 ** have to be written to deal with multi-path 
1193                                 ** issues -chris
1194                                 */
1195         /* sanity checking before doing the rename - avoid races many
1196         ** of the above checks could have scheduled.  We have to be
1197         ** sure our items haven't been shifted by another process.
1198         */
1199         if (item_moved(&new_entry_ih, &new_entry_path) ||
1200             !entry_points_to_object(new_dentry->d_name.name, 
1201                                     new_dentry->d_name.len,
1202                                     &new_de, new_dentry_inode) ||
1203             item_moved(&old_entry_ih, &old_entry_path) || 
1204             !entry_points_to_object (old_dentry->d_name.name, 
1205                                      old_dentry->d_name.len,
1206                                      &old_de, old_inode)) {
1207             reiserfs_restore_prepared_buffer (old_inode->i_sb, new_de.de_bh);
1208             reiserfs_restore_prepared_buffer (old_inode->i_sb, old_de.de_bh);
1209             if (S_ISDIR(old_inode_mode))
1210                 reiserfs_restore_prepared_buffer (old_inode->i_sb, dot_dot_de.de_bh);
1211             continue;
1212         }
1213         if (S_ISDIR(old_inode_mode)) {
1214             if ( item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1215                 !entry_points_to_object ( "..", 2, &dot_dot_de, old_dir) ) {
1216                 reiserfs_restore_prepared_buffer (old_inode->i_sb, old_de.de_bh);
1217                 reiserfs_restore_prepared_buffer (old_inode->i_sb, new_de.de_bh);
1218                 reiserfs_restore_prepared_buffer (old_inode->i_sb, dot_dot_de.de_bh);
1219                 continue;
1220             }
1221         }
1222
1223         RFALSE( S_ISDIR(old_inode_mode) && 
1224                  !reiserfs_buffer_prepared(dot_dot_de.de_bh), "" );
1225
1226         break;
1227     }
1228
1229     /* ok, all the changes can be done in one fell swoop when we
1230        have claimed all the buffers needed.*/
1231     
1232     mark_de_visible (new_de.de_deh + new_de.de_entry_num);
1233     set_ino_in_dir_entry (&new_de, INODE_PKEY (old_inode));
1234     journal_mark_dirty (&th, old_dir->i_sb, new_de.de_bh);
1235
1236     mark_de_hidden (old_de.de_deh + old_de.de_entry_num);
1237     journal_mark_dirty (&th, old_dir->i_sb, old_de.de_bh);
1238     old_dir->i_ctime = old_dir->i_mtime = 
1239     new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
1240
1241     if (new_dentry_inode) {
1242         // adjust link number of the victim
1243         if (S_ISDIR(new_dentry_inode->i_mode)) {
1244             new_dentry_inode->i_nlink  = 0;
1245         } else {
1246             new_dentry_inode->i_nlink--;
1247         }
1248         new_dentry_inode->i_ctime = new_dir->i_ctime;
1249     }
1250
1251     if (S_ISDIR(old_inode_mode)) {
1252         // adjust ".." of renamed directory 
1253         set_ino_in_dir_entry (&dot_dot_de, INODE_PKEY (new_dir));
1254         journal_mark_dirty (&th, new_dir->i_sb, dot_dot_de.de_bh);
1255         
1256         if (!new_dentry_inode)
1257             /* there (in new_dir) was no directory, so it got new link
1258                (".."  of renamed directory) */
1259             INC_DIR_INODE_NLINK(new_dir);
1260                 
1261         /* old directory lost one link - ".. " of renamed directory */
1262         DEC_DIR_INODE_NLINK(old_dir);
1263     }
1264
1265     // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1266     pathrelse (&new_entry_path);
1267     pathrelse (&dot_dot_entry_path);
1268
1269     // FIXME: this reiserfs_cut_from_item's return value may screw up
1270     // anybody, but it will panic if will not be able to find the
1271     // entry. This needs one more clean up
1272     if (reiserfs_cut_from_item (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL, 0) < 0)
1273         reiserfs_warning ("vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?\n");
1274
1275     old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1276     old_dir->i_blocks = ((old_dir->i_size + 511) >> 9);
1277
1278     reiserfs_update_sd (&th, old_dir);
1279     reiserfs_update_sd (&th, new_dir);
1280
1281     if (new_dentry_inode) {
1282         if (new_dentry_inode->i_nlink == 0)
1283             add_save_link (&th, new_dentry_inode, 0/* not truncate */);
1284         reiserfs_update_sd (&th, new_dentry_inode);
1285     }
1286
1287     pop_journal_writer(windex) ;
1288     journal_end(&th, old_dir->i_sb, jbegin_count) ;
1289     reiserfs_write_unlock(old_dir->i_sb);
1290     return 0;
1291 }
1292
1293
1294
1295 /*
1296  * directories can handle most operations...
1297  */
1298 struct inode_operations reiserfs_dir_inode_operations = {
1299   //&reiserfs_dir_operations,   /* default_file_ops */
1300     .create     = reiserfs_create,
1301     .lookup     = reiserfs_lookup,
1302     .link       = reiserfs_link,
1303     .unlink     = reiserfs_unlink,
1304     .symlink    = reiserfs_symlink,
1305     .mkdir      = reiserfs_mkdir,
1306     .rmdir      = reiserfs_rmdir,
1307     .mknod      = reiserfs_mknod,
1308     .rename     = reiserfs_rename,
1309 };
1310