revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / plugin / inode_ops_rename.c
blobdf40d0b19ee4b5527a0bbd08cbf0c22092d1fcd4
1 /* Copyright 2001, 2002, 2003, 2004 by Hans Reiser, licensing governed by
2 * reiser4/README */
4 #include "../inode.h"
5 #include "../safe_link.h"
7 static const char *possible_leak = "Possible disk space leak.";
9 /* re-bind existing name at @from_coord in @from_dir to point to @to_inode.
11 Helper function called from hashed_rename() */
12 static int replace_name(struct inode *to_inode, /* inode where @from_coord is
13 * to be re-targeted at */
14 struct inode *from_dir, /* directory where @from_coord
15 * lives */
16 struct inode *from_inode, /* inode @from_coord
17 * originally point to */
18 coord_t * from_coord, /* where directory entry is in
19 * the tree */
20 lock_handle * from_lh /* lock handle on @from_coord */ )
22 item_plugin *from_item;
23 int result;
24 znode *node;
26 coord_clear_iplug(from_coord);
27 node = from_coord->node;
28 result = zload(node);
29 if (result != 0)
30 return result;
31 from_item = item_plugin_by_coord(from_coord);
32 if (plugin_of_group(item_plugin_by_coord(from_coord),
33 DIR_ENTRY_ITEM_TYPE))
35 reiser4_key to_key;
37 build_sd_key(to_inode, &to_key);
39 /* everything is found and prepared to change directory entry
40 at @from_coord to point to @to_inode.
42 @to_inode is just about to get new name, so bump its link
43 counter.
46 result = reiser4_add_nlink(to_inode, from_dir, 0);
47 if (result != 0) {
48 /* Don't issue warning: this may be plain -EMLINK */
49 zrelse(node);
50 return result;
53 result =
54 from_item->s.dir.update_key(from_coord, &to_key, from_lh);
55 if (result != 0) {
56 reiser4_del_nlink(to_inode, from_dir, 0);
57 zrelse(node);
58 return result;
61 /* @from_inode just lost its name, he-he.
63 If @from_inode was directory, it contained dotdot pointing
64 to @from_dir. @from_dir i_nlink will be decreased when
65 iput() will be called on @from_inode.
67 If file-system is not ADG (hard-links are
68 supported on directories), iput(from_inode) will not remove
69 @from_inode, and thus above is incorrect, but hard-links on
70 directories are problematic in many other respects.
72 result = reiser4_del_nlink(from_inode, from_dir, 0);
73 if (result != 0) {
74 warning("nikita-2330",
75 "Cannot remove link from source: %i. %s",
76 result, possible_leak);
78 /* Has to return success, because entry is already
79 * modified. */
80 result = 0;
82 /* NOTE-NIKITA consider calling plugin method in stead of
83 accessing inode fields directly. */
84 from_dir->i_mtime = CURRENT_TIME;
85 } else {
86 warning("nikita-2326", "Unexpected item type");
87 result = RETERR(-EIO);
89 zrelse(node);
90 return result;
93 /* add new entry pointing to @inode into @dir at @coord, locked by @lh
95 Helper function used by hashed_rename(). */
96 static int add_name(struct inode *inode, /* inode where @coord is to be
97 * re-targeted at */
98 struct inode *dir, /* directory where @coord lives */
99 struct dentry *name, /* new name */
100 coord_t * coord, /* where directory entry is in the tree */
101 lock_handle * lh, /* lock handle on @coord */
102 int is_dir /* true, if @inode is directory */ )
104 int result;
105 reiser4_dir_entry_desc entry;
107 assert("nikita-2333", lh->node == coord->node);
108 assert("nikita-2334", is_dir == S_ISDIR(inode->i_mode));
110 memset(&entry, 0, sizeof entry);
111 entry.obj = inode;
112 /* build key of directory entry description */
113 inode_dir_plugin(dir)->build_entry_key(dir, &name->d_name, &entry.key);
115 /* ext2 does this in different order: first inserts new entry,
116 then increases directory nlink. We don't want do this,
117 because reiser4_add_nlink() calls ->add_link() plugin
118 method that can fail for whatever reason, leaving as with
119 cleanup problems.
121 /* @inode is getting new name */
122 reiser4_add_nlink(inode, dir, 0);
123 /* create @new_name in @new_dir pointing to
124 @old_inode */
125 result = WITH_COORD(coord,
126 inode_dir_item_plugin(dir)->s.dir.add_entry(dir,
127 coord,
129 name,
130 &entry));
131 if (result != 0) {
132 int result2;
133 result2 = reiser4_del_nlink(inode, dir, 0);
134 if (result2 != 0) {
135 warning("nikita-2327",
136 "Cannot drop link on %lli %i. %s",
137 (unsigned long long)get_inode_oid(inode),
138 result2, possible_leak);
140 } else
141 INODE_INC_FIELD(dir, i_size);
142 return result;
145 static reiser4_block_nr estimate_rename(struct inode *old_dir, /* directory where @old is located */
146 struct dentry *old_name, /* old name */
147 struct inode *new_dir, /* directory where @new is located */
148 struct dentry *new_name /* new name */ )
150 reiser4_block_nr res1, res2;
151 dir_plugin *p_parent_old, *p_parent_new;
152 file_plugin *p_child_old, *p_child_new;
154 assert("vpf-311", old_dir != NULL);
155 assert("vpf-312", new_dir != NULL);
156 assert("vpf-313", old_name != NULL);
157 assert("vpf-314", new_name != NULL);
159 p_parent_old = inode_dir_plugin(old_dir);
160 p_parent_new = inode_dir_plugin(new_dir);
161 p_child_old = inode_file_plugin(old_name->d_inode);
162 if (new_name->d_inode)
163 p_child_new = inode_file_plugin(new_name->d_inode);
164 else
165 p_child_new = NULL;
167 /* find_entry - can insert one leaf. */
168 res1 = res2 = 1;
170 /* replace_name */
172 /* reiser4_add_nlink(p_child_old) and reiser4_del_nlink(p_child_old) */
173 res1 += 2 * p_child_old->estimate.update(old_name->d_inode);
174 /* update key */
175 res1 += 1;
176 /* reiser4_del_nlink(p_child_new) */
177 if (p_child_new)
178 res1 += p_child_new->estimate.update(new_name->d_inode);
181 /* else add_name */
183 /* reiser4_add_nlink(p_parent_new) and reiser4_del_nlink(p_parent_new) */
184 res2 +=
185 2 * inode_file_plugin(new_dir)->estimate.update(new_dir);
186 /* reiser4_add_nlink(p_parent_old) */
187 res2 += p_child_old->estimate.update(old_name->d_inode);
188 /* add_entry(p_parent_new) */
189 res2 += p_parent_new->estimate.add_entry(new_dir);
190 /* reiser4_del_nlink(p_parent_old) */
191 res2 += p_child_old->estimate.update(old_name->d_inode);
194 res1 = res1 < res2 ? res2 : res1;
196 /* reiser4_write_sd(p_parent_new) */
197 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
199 /* reiser4_write_sd(p_child_new) */
200 if (p_child_new)
201 res1 += p_child_new->estimate.update(new_name->d_inode);
203 /* hashed_rem_entry(p_parent_old) */
204 res1 += p_parent_old->estimate.rem_entry(old_dir);
206 /* reiser4_del_nlink(p_child_old) */
207 res1 += p_child_old->estimate.update(old_name->d_inode);
209 /* replace_name */
211 /* reiser4_add_nlink(p_parent_dir_new) */
212 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
213 /* update_key */
214 res1 += 1;
215 /* reiser4_del_nlink(p_parent_new) */
216 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
217 /* reiser4_del_nlink(p_parent_old) */
218 res1 += inode_file_plugin(old_dir)->estimate.update(old_dir);
221 /* reiser4_write_sd(p_parent_old) */
222 res1 += inode_file_plugin(old_dir)->estimate.update(old_dir);
224 /* reiser4_write_sd(p_child_old) */
225 res1 += p_child_old->estimate.update(old_name->d_inode);
227 return res1;
230 static int hashed_rename_estimate_and_grab(struct inode *old_dir, /* directory where @old is located */
231 struct dentry *old_name, /* old name */
232 struct inode *new_dir, /* directory where @new is located */
233 struct dentry *new_name
234 /* new name */ )
236 reiser4_block_nr reserve;
238 reserve = estimate_rename(old_dir, old_name, new_dir, new_name);
240 if (reiser4_grab_space(reserve, BA_CAN_COMMIT))
241 return RETERR(-ENOSPC);
243 return 0;
246 /* check whether @old_inode and @new_inode can be moved within file system
247 * tree. This singles out attempts to rename pseudo-files, for example. */
248 static int can_rename(struct inode *old_dir, struct inode *old_inode,
249 struct inode *new_dir, struct inode *new_inode)
251 file_plugin *fplug;
252 dir_plugin *dplug;
254 assert("nikita-3370", old_inode != NULL);
256 dplug = inode_dir_plugin(new_dir);
257 fplug = inode_file_plugin(old_inode);
259 if (dplug == NULL)
260 return RETERR(-ENOTDIR);
261 else if (new_dir->i_op->create == NULL)
262 return RETERR(-EPERM);
263 else if (!fplug->can_add_link(old_inode))
264 return RETERR(-EMLINK);
265 else if (new_inode != NULL) {
266 fplug = inode_file_plugin(new_inode);
267 if (fplug->can_rem_link != NULL &&
268 !fplug->can_rem_link(new_inode))
269 return RETERR(-EBUSY);
271 return 0;
274 int reiser4_find_entry(struct inode *, struct dentry *, lock_handle *,
275 znode_lock_mode, reiser4_dir_entry_desc *);
276 int reiser4_update_dir(struct inode *);
278 /* this is common implementation of vfs's rename method of struct
279 inode_operations
280 See comments in the body.
282 It is arguable that this function can be made generic so, that it
283 will be applicable to any kind of directory plugin that deals with
284 directories composed out of directory entries. The only obstacle
285 here is that we don't have any data-type to represent directory
286 entry. This should be re-considered when more than one different
287 directory plugin will be implemented.
289 int reiser4_rename_common(struct inode *old_dir /* directory where @old
290 * is located */ ,
291 struct dentry *old_name /* old name */ ,
292 struct inode *new_dir /* directory where @new
293 * is located */ ,
294 struct dentry *new_name /* new name */ )
296 /* From `The Open Group Base Specifications Issue 6'
298 If either the old or new argument names a symbolic link, rename()
299 shall operate on the symbolic link itself, and shall not resolve
300 the last component of the argument. If the old argument and the new
301 argument resolve to the same existing file, rename() shall return
302 successfully and perform no other action.
304 [this is done by VFS: vfs_rename()]
306 If the old argument points to the pathname of a file that is not a
307 directory, the new argument shall not point to the pathname of a
308 directory.
310 [checked by VFS: vfs_rename->may_delete()]
312 If the link named by the new argument exists, it shall
313 be removed and old renamed to new. In this case, a link named new
314 shall remain visible to other processes throughout the renaming
315 operation and refer either to the file referred to by new or old
316 before the operation began.
318 [we should assure this]
320 Write access permission is required for
321 both the directory containing old and the directory containing new.
323 [checked by VFS: vfs_rename->may_delete(), may_create()]
325 If the old argument points to the pathname of a directory, the new
326 argument shall not point to the pathname of a file that is not a
327 directory.
329 [checked by VFS: vfs_rename->may_delete()]
331 If the directory named by the new argument exists, it
332 shall be removed and old renamed to new. In this case, a link named
333 new shall exist throughout the renaming operation and shall refer
334 either to the directory referred to by new or old before the
335 operation began.
337 [we should assure this]
339 If new names an existing directory, it shall be
340 required to be an empty directory.
342 [we should check this]
344 If the old argument points to a pathname of a symbolic link, the
345 symbolic link shall be renamed. If the new argument points to a
346 pathname of a symbolic link, the symbolic link shall be removed.
348 The new pathname shall not contain a path prefix that names
349 old. Write access permission is required for the directory
350 containing old and the directory containing new. If the old
351 argument points to the pathname of a directory, write access
352 permission may be required for the directory named by old, and, if
353 it exists, the directory named by new.
355 [checked by VFS: vfs_rename(), vfs_rename_dir()]
357 If the link named by the new argument exists and the file's link
358 count becomes 0 when it is removed and no process has the file
359 open, the space occupied by the file shall be freed and the file
360 shall no longer be accessible. If one or more processes have the
361 file open when the last link is removed, the link shall be removed
362 before rename() returns, but the removal of the file contents shall
363 be postponed until all references to the file are closed.
365 [iput() handles this, but we can do this manually, a la
366 reiser4_unlink()]
368 Upon successful completion, rename() shall mark for update the
369 st_ctime and st_mtime fields of the parent directory of each file.
371 [N/A]
374 reiser4_context *ctx;
375 int result;
376 int is_dir; /* is @old_name directory */
378 struct inode *old_inode;
379 struct inode *new_inode;
380 coord_t *new_coord;
382 struct reiser4_dentry_fsdata *new_fsdata;
383 dir_plugin *dplug;
384 file_plugin *fplug;
386 reiser4_dir_entry_desc *old_entry, *new_entry, *dotdot_entry;
387 lock_handle *new_lh, *dotdot_lh;
388 struct dentry *dotdot_name;
389 struct reiser4_dentry_fsdata *dataonstack;
391 ctx = reiser4_init_context(old_dir->i_sb);
392 if (IS_ERR(ctx))
393 return PTR_ERR(ctx);
395 old_entry = kzalloc(3 * sizeof(*old_entry) + 2 * sizeof(*new_lh) +
396 sizeof(*dotdot_name) + sizeof(*dataonstack),
397 reiser4_ctx_gfp_mask_get());
398 if (!old_entry) {
399 context_set_commit_async(ctx);
400 reiser4_exit_context(ctx);
401 return RETERR(-ENOMEM);
404 new_entry = old_entry + 1;
405 dotdot_entry = old_entry + 2;
406 new_lh = (lock_handle *)(old_entry + 3);
407 dotdot_lh = new_lh + 1;
408 dotdot_name = (struct dentry *)(new_lh + 2);
409 dataonstack = (struct reiser4_dentry_fsdata *)(dotdot_name + 1);
411 assert("nikita-2318", old_dir != NULL);
412 assert("nikita-2319", new_dir != NULL);
413 assert("nikita-2320", old_name != NULL);
414 assert("nikita-2321", new_name != NULL);
416 old_inode = old_name->d_inode;
417 new_inode = new_name->d_inode;
419 dplug = inode_dir_plugin(old_dir);
420 fplug = NULL;
422 new_fsdata = reiser4_get_dentry_fsdata(new_name);
423 if (IS_ERR(new_fsdata)) {
424 kfree(old_entry);
425 context_set_commit_async(ctx);
426 reiser4_exit_context(ctx);
427 return PTR_ERR(new_fsdata);
430 new_coord = &new_fsdata->dec.entry_coord;
431 coord_clear_iplug(new_coord);
433 is_dir = S_ISDIR(old_inode->i_mode);
435 assert("nikita-3461", old_inode->i_nlink >= 1 + !!is_dir);
437 /* if target is existing directory and it's not empty---return error.
439 This check is done specifically, because is_dir_empty() requires
440 tree traversal and have to be done before locks are taken.
442 if (is_dir && new_inode != NULL && is_dir_empty(new_inode) != 0) {
443 kfree(old_entry);
444 context_set_commit_async(ctx);
445 reiser4_exit_context(ctx);
446 return RETERR(-ENOTEMPTY);
449 result = can_rename(old_dir, old_inode, new_dir, new_inode);
450 if (result != 0) {
451 kfree(old_entry);
452 context_set_commit_async(ctx);
453 reiser4_exit_context(ctx);
454 return result;
457 result = hashed_rename_estimate_and_grab(old_dir, old_name,
458 new_dir, new_name);
459 if (result != 0) {
460 kfree(old_entry);
461 context_set_commit_async(ctx);
462 reiser4_exit_context(ctx);
463 return result;
466 init_lh(new_lh);
468 /* find entry for @new_name */
469 result = reiser4_find_entry(new_dir, new_name, new_lh, ZNODE_WRITE_LOCK,
470 new_entry);
472 if (IS_CBKERR(result)) {
473 done_lh(new_lh);
474 kfree(old_entry);
475 context_set_commit_async(ctx);
476 reiser4_exit_context(ctx);
477 return result;
480 reiser4_seal_done(&new_fsdata->dec.entry_seal);
482 /* add or replace name for @old_inode as @new_name */
483 if (new_inode != NULL) {
484 /* target (@new_name) exists. */
485 /* Not clear what to do with objects that are
486 both directories and files at the same time. */
487 if (result == CBK_COORD_FOUND) {
488 result = replace_name(old_inode,
489 new_dir,
490 new_inode, new_coord, new_lh);
491 if (result == 0)
492 fplug = inode_file_plugin(new_inode);
493 } else if (result == CBK_COORD_NOTFOUND) {
494 /* VFS told us that @new_name is bound to existing
495 inode, but we failed to find directory entry. */
496 warning("nikita-2324", "Target not found");
497 result = RETERR(-ENOENT);
499 } else {
500 /* target (@new_name) doesn't exists. */
501 if (result == CBK_COORD_NOTFOUND)
502 result = add_name(old_inode,
503 new_dir,
504 new_name, new_coord, new_lh, is_dir);
505 else if (result == CBK_COORD_FOUND) {
506 /* VFS told us that @new_name is "negative" dentry,
507 but we found directory entry. */
508 warning("nikita-2331", "Target found unexpectedly");
509 result = RETERR(-EIO);
513 assert("nikita-3462", ergo(result == 0,
514 old_inode->i_nlink >= 2 + !!is_dir));
516 /* We are done with all modifications to the @new_dir, release lock on
517 node. */
518 done_lh(new_lh);
520 if (fplug != NULL) {
521 /* detach @new_inode from name-space */
522 result = fplug->detach(new_inode, new_dir);
523 if (result != 0)
524 warning("nikita-2330", "Cannot detach %lli: %i. %s",
525 (unsigned long long)get_inode_oid(new_inode),
526 result, possible_leak);
529 if (new_inode != NULL)
530 reiser4_update_sd(new_inode);
532 if (result == 0) {
533 old_entry->obj = old_inode;
535 dplug->build_entry_key(old_dir,
536 &old_name->d_name, &old_entry->key);
538 /* At this stage new name was introduced for
539 @old_inode. @old_inode, @new_dir, and @new_inode i_nlink
540 counters were updated.
542 We want to remove @old_name now. If @old_inode wasn't
543 directory this is simple.
545 result = dplug->rem_entry(old_dir, old_name, old_entry);
546 if (result != 0 && result != -ENOMEM) {
547 warning("nikita-2335",
548 "Cannot remove old name: %i", result);
549 } else {
550 result = reiser4_del_nlink(old_inode, old_dir, 0);
551 if (result != 0 && result != -ENOMEM) {
552 warning("nikita-2337",
553 "Cannot drop link on old: %i", result);
557 if (result == 0 && is_dir) {
558 /* @old_inode is directory. We also have to update
559 dotdot entry. */
560 coord_t *dotdot_coord;
562 memset(dataonstack, 0, sizeof dataonstack);
563 memset(dotdot_entry, 0, sizeof dotdot_entry);
564 dotdot_entry->obj = old_dir;
565 memset(dotdot_name, 0, sizeof dotdot_name);
566 dotdot_name->d_name.name = "..";
567 dotdot_name->d_name.len = 2;
569 * allocate ->d_fsdata on the stack to avoid using
570 * reiser4_get_dentry_fsdata(). Locking is not needed,
571 * because dentry is private to the current thread.
573 dotdot_name->d_fsdata = dataonstack;
574 init_lh(dotdot_lh);
576 dotdot_coord = &dataonstack->dec.entry_coord;
577 coord_clear_iplug(dotdot_coord);
579 result = reiser4_find_entry(old_inode, dotdot_name,
580 dotdot_lh, ZNODE_WRITE_LOCK,
581 dotdot_entry);
582 if (result == 0) {
583 /* replace_name() decreases i_nlink on
584 * @old_dir */
585 result = replace_name(new_dir,
586 old_inode,
587 old_dir,
588 dotdot_coord, dotdot_lh);
589 } else
590 result = RETERR(-EIO);
591 done_lh(dotdot_lh);
594 reiser4_update_dir(new_dir);
595 reiser4_update_dir(old_dir);
596 reiser4_update_sd(old_inode);
597 if (result == 0) {
598 file_plugin *fplug;
600 if (new_inode != NULL) {
601 /* add safe-link for target file (in case we removed
602 * last reference to the poor fellow */
603 fplug = inode_file_plugin(new_inode);
604 if (new_inode->i_nlink == 0)
605 result = safe_link_add(new_inode, SAFE_UNLINK);
608 kfree(old_entry);
609 context_set_commit_async(ctx);
610 reiser4_exit_context(ctx);
611 return result;
614 #if 0
615 int reiser4_rename_common(struct inode *old_dir /* directory where @old
616 * is located */ ,
617 struct dentry *old_name /* old name */ ,
618 struct inode *new_dir /* directory where @new
619 * is located */ ,
620 struct dentry *new_name /* new name */ )
622 /* From `The Open Group Base Specifications Issue 6'
624 If either the old or new argument names a symbolic link, rename()
625 shall operate on the symbolic link itself, and shall not resolve
626 the last component of the argument. If the old argument and the new
627 argument resolve to the same existing file, rename() shall return
628 successfully and perform no other action.
630 [this is done by VFS: vfs_rename()]
632 If the old argument points to the pathname of a file that is not a
633 directory, the new argument shall not point to the pathname of a
634 directory.
636 [checked by VFS: vfs_rename->may_delete()]
638 If the link named by the new argument exists, it shall
639 be removed and old renamed to new. In this case, a link named new
640 shall remain visible to other processes throughout the renaming
641 operation and refer either to the file referred to by new or old
642 before the operation began.
644 [we should assure this]
646 Write access permission is required for
647 both the directory containing old and the directory containing new.
649 [checked by VFS: vfs_rename->may_delete(), may_create()]
651 If the old argument points to the pathname of a directory, the new
652 argument shall not point to the pathname of a file that is not a
653 directory.
655 [checked by VFS: vfs_rename->may_delete()]
657 If the directory named by the new argument exists, it
658 shall be removed and old renamed to new. In this case, a link named
659 new shall exist throughout the renaming operation and shall refer
660 either to the directory referred to by new or old before the
661 operation began.
663 [we should assure this]
665 If new names an existing directory, it shall be
666 required to be an empty directory.
668 [we should check this]
670 If the old argument points to a pathname of a symbolic link, the
671 symbolic link shall be renamed. If the new argument points to a
672 pathname of a symbolic link, the symbolic link shall be removed.
674 The new pathname shall not contain a path prefix that names
675 old. Write access permission is required for the directory
676 containing old and the directory containing new. If the old
677 argument points to the pathname of a directory, write access
678 permission may be required for the directory named by old, and, if
679 it exists, the directory named by new.
681 [checked by VFS: vfs_rename(), vfs_rename_dir()]
683 If the link named by the new argument exists and the file's link
684 count becomes 0 when it is removed and no process has the file
685 open, the space occupied by the file shall be freed and the file
686 shall no longer be accessible. If one or more processes have the
687 file open when the last link is removed, the link shall be removed
688 before rename() returns, but the removal of the file contents shall
689 be postponed until all references to the file are closed.
691 [iput() handles this, but we can do this manually, a la
692 reiser4_unlink()]
694 Upon successful completion, rename() shall mark for update the
695 st_ctime and st_mtime fields of the parent directory of each file.
697 [N/A]
700 reiser4_context *ctx;
701 int result;
702 int is_dir; /* is @old_name directory */
703 struct inode *old_inode;
704 struct inode *new_inode;
705 reiser4_dir_entry_desc old_entry;
706 reiser4_dir_entry_desc new_entry;
707 coord_t *new_coord;
708 struct reiser4_dentry_fsdata *new_fsdata;
709 lock_handle new_lh;
710 dir_plugin *dplug;
711 file_plugin *fplug;
713 ctx = reiser4_init_context(old_dir->i_sb);
714 if (IS_ERR(ctx))
715 return PTR_ERR(ctx);
717 assert("nikita-2318", old_dir != NULL);
718 assert("nikita-2319", new_dir != NULL);
719 assert("nikita-2320", old_name != NULL);
720 assert("nikita-2321", new_name != NULL);
722 old_inode = old_name->d_inode;
723 new_inode = new_name->d_inode;
725 dplug = inode_dir_plugin(old_dir);
726 fplug = NULL;
728 new_fsdata = reiser4_get_dentry_fsdata(new_name);
729 if (IS_ERR(new_fsdata)) {
730 result = PTR_ERR(new_fsdata);
731 goto exit;
734 new_coord = &new_fsdata->dec.entry_coord;
735 coord_clear_iplug(new_coord);
737 is_dir = S_ISDIR(old_inode->i_mode);
739 assert("nikita-3461", old_inode->i_nlink >= 1 + !!is_dir);
741 /* if target is existing directory and it's not empty---return error.
743 This check is done specifically, because is_dir_empty() requires
744 tree traversal and have to be done before locks are taken.
746 if (is_dir && new_inode != NULL && is_dir_empty(new_inode) != 0)
747 return RETERR(-ENOTEMPTY);
749 result = can_rename(old_dir, old_inode, new_dir, new_inode);
750 if (result != 0)
751 goto exit;
753 result = hashed_rename_estimate_and_grab(old_dir, old_name,
754 new_dir, new_name);
755 if (result != 0)
756 goto exit;
758 init_lh(&new_lh);
760 /* find entry for @new_name */
761 result = reiser4_find_entry(new_dir, new_name, &new_lh,
762 ZNODE_WRITE_LOCK, &new_entry);
764 if (IS_CBKERR(result)) {
765 done_lh(&new_lh);
766 goto exit;
769 reiser4_seal_done(&new_fsdata->dec.entry_seal);
771 /* add or replace name for @old_inode as @new_name */
772 if (new_inode != NULL) {
773 /* target (@new_name) exists. */
774 /* Not clear what to do with objects that are
775 both directories and files at the same time. */
776 if (result == CBK_COORD_FOUND) {
777 result = replace_name(old_inode,
778 new_dir,
779 new_inode, new_coord, &new_lh);
780 if (result == 0)
781 fplug = inode_file_plugin(new_inode);
782 } else if (result == CBK_COORD_NOTFOUND) {
783 /* VFS told us that @new_name is bound to existing
784 inode, but we failed to find directory entry. */
785 warning("nikita-2324", "Target not found");
786 result = RETERR(-ENOENT);
788 } else {
789 /* target (@new_name) doesn't exists. */
790 if (result == CBK_COORD_NOTFOUND)
791 result = add_name(old_inode,
792 new_dir,
793 new_name, new_coord, &new_lh, is_dir);
794 else if (result == CBK_COORD_FOUND) {
795 /* VFS told us that @new_name is "negative" dentry,
796 but we found directory entry. */
797 warning("nikita-2331", "Target found unexpectedly");
798 result = RETERR(-EIO);
802 assert("nikita-3462", ergo(result == 0,
803 old_inode->i_nlink >= 2 + !!is_dir));
805 /* We are done with all modifications to the @new_dir, release lock on
806 node. */
807 done_lh(&new_lh);
809 if (fplug != NULL) {
810 /* detach @new_inode from name-space */
811 result = fplug->detach(new_inode, new_dir);
812 if (result != 0)
813 warning("nikita-2330", "Cannot detach %lli: %i. %s",
814 (unsigned long long)get_inode_oid(new_inode),
815 result, possible_leak);
818 if (new_inode != NULL)
819 reiser4_update_sd(new_inode);
821 if (result == 0) {
822 memset(&old_entry, 0, sizeof old_entry);
823 old_entry.obj = old_inode;
825 dplug->build_entry_key(old_dir,
826 &old_name->d_name, &old_entry.key);
828 /* At this stage new name was introduced for
829 @old_inode. @old_inode, @new_dir, and @new_inode i_nlink
830 counters were updated.
832 We want to remove @old_name now. If @old_inode wasn't
833 directory this is simple.
835 result = dplug->rem_entry(old_dir, old_name, &old_entry);
836 /*result = rem_entry_hashed(old_dir, old_name, &old_entry); */
837 if (result != 0 && result != -ENOMEM) {
838 warning("nikita-2335",
839 "Cannot remove old name: %i", result);
840 } else {
841 result = reiser4_del_nlink(old_inode, old_dir, 0);
842 if (result != 0 && result != -ENOMEM) {
843 warning("nikita-2337",
844 "Cannot drop link on old: %i", result);
848 if (result == 0 && is_dir) {
849 /* @old_inode is directory. We also have to update
850 dotdot entry. */
851 coord_t *dotdot_coord;
852 lock_handle dotdot_lh;
853 struct dentry dotdot_name;
854 reiser4_dir_entry_desc dotdot_entry;
855 struct reiser4_dentry_fsdata dataonstack;
856 struct reiser4_dentry_fsdata *fsdata;
858 memset(&dataonstack, 0, sizeof dataonstack);
859 memset(&dotdot_entry, 0, sizeof dotdot_entry);
860 dotdot_entry.obj = old_dir;
861 memset(&dotdot_name, 0, sizeof dotdot_name);
862 dotdot_name.d_name.name = "..";
863 dotdot_name.d_name.len = 2;
865 * allocate ->d_fsdata on the stack to avoid using
866 * reiser4_get_dentry_fsdata(). Locking is not needed,
867 * because dentry is private to the current thread.
869 dotdot_name.d_fsdata = &dataonstack;
870 init_lh(&dotdot_lh);
872 fsdata = &dataonstack;
873 dotdot_coord = &fsdata->dec.entry_coord;
874 coord_clear_iplug(dotdot_coord);
876 result = reiser4_find_entry(old_inode,
877 &dotdot_name,
878 &dotdot_lh,
879 ZNODE_WRITE_LOCK,
880 &dotdot_entry);
881 if (result == 0) {
882 /* replace_name() decreases i_nlink on
883 * @old_dir */
884 result = replace_name(new_dir,
885 old_inode,
886 old_dir,
887 dotdot_coord, &dotdot_lh);
888 } else
889 result = RETERR(-EIO);
890 done_lh(&dotdot_lh);
893 reiser4_update_dir(new_dir);
894 reiser4_update_dir(old_dir);
895 reiser4_update_sd(old_inode);
896 if (result == 0) {
897 file_plugin *fplug;
899 if (new_inode != NULL) {
900 /* add safe-link for target file (in case we removed
901 * last reference to the poor fellow */
902 fplug = inode_file_plugin(new_inode);
903 if (new_inode->i_nlink == 0)
904 result = safe_link_add(new_inode, SAFE_UNLINK);
907 exit:
908 context_set_commit_async(ctx);
909 reiser4_exit_context(ctx);
910 return result;
912 #endif