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