On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / fs / reiser4 / plugin / inode_ops_rename.c
blobc3517405246d8d49c7dd1ec6057e419183427c53
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)) {
34 reiser4_key to_key;
36 build_sd_key(to_inode, &to_key);
38 /* everything is found and prepared to change directory entry
39 at @from_coord to point to @to_inode.
41 @to_inode is just about to get new name, so bump its link
42 counter.
45 result = reiser4_add_nlink(to_inode, from_dir, 0);
46 if (result != 0) {
47 /* Don't issue warning: this may be plain -EMLINK */
48 zrelse(node);
49 return result;
52 result =
53 from_item->s.dir.update_key(from_coord, &to_key, from_lh);
54 if (result != 0) {
55 reiser4_del_nlink(to_inode, from_dir, 0);
56 zrelse(node);
57 return result;
60 /* @from_inode just lost its name, he-he.
62 If @from_inode was directory, it contained dotdot pointing
63 to @from_dir. @from_dir i_nlink will be decreased when
64 iput() will be called on @from_inode.
66 If file-system is not ADG (hard-links are
67 supported on directories), iput(from_inode) will not remove
68 @from_inode, and thus above is incorrect, but hard-links on
69 directories are problematic in many other respects.
71 result = reiser4_del_nlink(from_inode, from_dir, 0);
72 if (result != 0) {
73 warning("nikita-2330",
74 "Cannot remove link from source: %i. %s",
75 result, possible_leak);
77 /* Has to return success, because entry is already
78 * modified. */
79 result = 0;
81 /* NOTE-NIKITA consider calling plugin method in stead of
82 accessing inode fields directly. */
83 from_dir->i_mtime = CURRENT_TIME;
84 } else {
85 warning("nikita-2326", "Unexpected item type");
86 result = RETERR(-EIO);
88 zrelse(node);
89 return result;
92 /* add new entry pointing to @inode into @dir at @coord, locked by @lh
94 Helper function used by hashed_rename(). */
95 static int add_name(struct inode *inode, /* inode where @coord is to be
96 * re-targeted at */
97 struct inode *dir, /* directory where @coord lives */
98 struct dentry *name, /* new name */
99 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
146 * where @old is
147 * located */
148 struct dentry *old_name,/* old name */
149 struct inode *new_dir, /* directory
150 * where @new is
151 * located */
152 struct dentry *new_name /* new name */)
154 reiser4_block_nr res1, res2;
155 dir_plugin * p_parent_old, *p_parent_new;
156 file_plugin * p_child_old, *p_child_new;
158 assert("vpf-311", old_dir != NULL);
159 assert("vpf-312", new_dir != NULL);
160 assert("vpf-313", old_name != NULL);
161 assert("vpf-314", new_name != NULL);
163 p_parent_old = inode_dir_plugin(old_dir);
164 p_parent_new = inode_dir_plugin(new_dir);
165 p_child_old = inode_file_plugin(old_name->d_inode);
166 if (new_name->d_inode)
167 p_child_new = inode_file_plugin(new_name->d_inode);
168 else
169 p_child_new = NULL;
171 /* find_entry - can insert one leaf. */
172 res1 = res2 = 1;
174 /* replace_name */
176 /* reiser4_add_nlink(p_child_old) and
177 * reiser4_del_nlink(p_child_old) */
178 res1 += 2 * p_child_old->estimate.update(old_name->d_inode);
179 /* update key */
180 res1 += 1;
181 /* reiser4_del_nlink(p_child_new) */
182 if (p_child_new)
183 res1 += p_child_new->estimate.update(new_name->d_inode);
186 /* else add_name */
188 /* reiser4_add_nlink(p_parent_new) and
189 * reiser4_del_nlink(p_parent_new) */
190 res2 +=
191 2 * inode_file_plugin(new_dir)->estimate.update(new_dir);
192 /* reiser4_add_nlink(p_parent_old) */
193 res2 += p_child_old->estimate.update(old_name->d_inode);
194 /* add_entry(p_parent_new) */
195 res2 += p_parent_new->estimate.add_entry(new_dir);
196 /* reiser4_del_nlink(p_parent_old) */
197 res2 += p_child_old->estimate.update(old_name->d_inode);
200 res1 = res1 < res2 ? res2 : res1;
202 /* reiser4_write_sd(p_parent_new) */
203 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
205 /* reiser4_write_sd(p_child_new) */
206 if (p_child_new)
207 res1 += p_child_new->estimate.update(new_name->d_inode);
209 /* hashed_rem_entry(p_parent_old) */
210 res1 += p_parent_old->estimate.rem_entry(old_dir);
212 /* reiser4_del_nlink(p_child_old) */
213 res1 += p_child_old->estimate.update(old_name->d_inode);
215 /* replace_name */
217 /* reiser4_add_nlink(p_parent_dir_new) */
218 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
219 /* update_key */
220 res1 += 1;
221 /* reiser4_del_nlink(p_parent_new) */
222 res1 += inode_file_plugin(new_dir)->estimate.update(new_dir);
223 /* reiser4_del_nlink(p_parent_old) */
224 res1 += inode_file_plugin(old_dir)->estimate.update(old_dir);
227 /* reiser4_write_sd(p_parent_old) */
228 res1 += inode_file_plugin(old_dir)->estimate.update(old_dir);
230 /* reiser4_write_sd(p_child_old) */
231 res1 += p_child_old->estimate.update(old_name->d_inode);
233 return res1;
236 static int hashed_rename_estimate_and_grab(struct inode *old_dir, /* directory
237 * where @old
238 * is located
240 struct dentry *old_name,/* old name
242 struct inode *new_dir, /* directory
243 * where @new
244 * is located
246 struct dentry *new_name /* new name
249 reiser4_block_nr reserve;
251 reserve = estimate_rename(old_dir, old_name, new_dir, new_name);
253 if (reiser4_grab_space(reserve, BA_CAN_COMMIT))
254 return RETERR(-ENOSPC);
256 return 0;
259 /* check whether @old_inode and @new_inode can be moved within file system
260 * tree. This singles out attempts to rename pseudo-files, for example. */
261 static int can_rename(struct inode *old_dir, struct inode *old_inode,
262 struct inode *new_dir, struct inode *new_inode)
264 file_plugin *fplug;
265 dir_plugin *dplug;
267 assert("nikita-3370", old_inode != NULL);
269 dplug = inode_dir_plugin(new_dir);
270 fplug = inode_file_plugin(old_inode);
272 if (dplug == NULL)
273 return RETERR(-ENOTDIR);
274 else if (new_dir->i_op->create == NULL)
275 return RETERR(-EPERM);
276 else if (!fplug->can_add_link(old_inode))
277 return RETERR(-EMLINK);
278 else if (new_inode != NULL) {
279 fplug = inode_file_plugin(new_inode);
280 if (fplug->can_rem_link != NULL &&
281 !fplug->can_rem_link(new_inode))
282 return RETERR(-EBUSY);
284 return 0;
287 int reiser4_find_entry(struct inode *, struct dentry *, lock_handle * ,
288 znode_lock_mode, reiser4_dir_entry_desc *);
289 int reiser4_update_dir(struct inode *);
291 /* this is common implementation of vfs's rename method of struct
292 inode_operations
293 See comments in the body.
295 It is arguable that this function can be made generic so, that it
296 will be applicable to any kind of directory plugin that deals with
297 directories composed out of directory entries. The only obstacle
298 here is that we don't have any data-type to represent directory
299 entry. This should be re-considered when more than one different
300 directory plugin will be implemented.
302 int reiser4_rename_common(struct inode *old_dir /* directory where @old
303 * is located */ ,
304 struct dentry *old_name /* old name */ ,
305 struct inode *new_dir /* directory where @new
306 * is located */ ,
307 struct dentry *new_name/* new name */)
309 /* From `The Open Group Base Specifications Issue 6'
311 If either the old or new argument names a symbolic link, rename()
312 shall operate on the symbolic link itself, and shall not resolve
313 the last component of the argument. If the old argument and the new
314 argument resolve to the same existing file, rename() shall return
315 successfully and perform no other action.
317 [this is done by VFS: vfs_rename()]
319 If the old argument points to the pathname of a file that is not a
320 directory, the new argument shall not point to the pathname of a
321 directory.
323 [checked by VFS: vfs_rename->may_delete()]
325 If the link named by the new argument exists, it shall
326 be removed and old renamed to new. In this case, a link named new
327 shall remain visible to other processes throughout the renaming
328 operation and refer either to the file referred to by new or old
329 before the operation began.
331 [we should assure this]
333 Write access permission is required for
334 both the directory containing old and the directory containing new.
336 [checked by VFS: vfs_rename->may_delete(), may_create()]
338 If the old argument points to the pathname of a directory, the new
339 argument shall not point to the pathname of a file that is not a
340 directory.
342 [checked by VFS: vfs_rename->may_delete()]
344 If the directory named by the new argument exists, it
345 shall be removed and old renamed to new. In this case, a link named
346 new shall exist throughout the renaming operation and shall refer
347 either to the directory referred to by new or old before the
348 operation began.
350 [we should assure this]
352 If new names an existing directory, it shall be
353 required to be an empty directory.
355 [we should check this]
357 If the old argument points to a pathname of a symbolic link, the
358 symbolic link shall be renamed. If the new argument points to a
359 pathname of a symbolic link, the symbolic link shall be removed.
361 The new pathname shall not contain a path prefix that names
362 old. Write access permission is required for the directory
363 containing old and the directory containing new. If the old
364 argument points to the pathname of a directory, write access
365 permission may be required for the directory named by old, and, if
366 it exists, the directory named by new.
368 [checked by VFS: vfs_rename(), vfs_rename_dir()]
370 If the link named by the new argument exists and the file's link
371 count becomes 0 when it is removed and no process has the file
372 open, the space occupied by the file shall be freed and the file
373 shall no longer be accessible. If one or more processes have the
374 file open when the last link is removed, the link shall be removed
375 before rename() returns, but the removal of the file contents shall
376 be postponed until all references to the file are closed.
378 [iput() handles this, but we can do this manually, a la
379 reiser4_unlink()]
381 Upon successful completion, rename() shall mark for update the
382 st_ctime and st_mtime fields of the parent directory of each file.
384 [N/A]
387 reiser4_context *ctx;
388 int result;
389 int is_dir; /* is @old_name directory */
391 struct inode *old_inode;
392 struct inode *new_inode;
393 coord_t *new_coord;
395 struct reiser4_dentry_fsdata *new_fsdata;
396 dir_plugin *dplug;
397 file_plugin *fplug;
399 reiser4_dir_entry_desc *old_entry, *new_entry, *dotdot_entry;
400 lock_handle * new_lh, *dotdot_lh;
401 struct dentry *dotdot_name;
402 struct reiser4_dentry_fsdata *dataonstack;
404 ctx = reiser4_init_context(old_dir->i_sb);
405 if (IS_ERR(ctx))
406 return PTR_ERR(ctx);
408 old_entry = kzalloc(3 * sizeof(*old_entry) + 2 * sizeof(*new_lh) +
409 sizeof(*dotdot_name) + sizeof(*dataonstack),
410 reiser4_ctx_gfp_mask_get());
411 if (!old_entry) {
412 context_set_commit_async(ctx);
413 reiser4_exit_context(ctx);
414 return RETERR(-ENOMEM);
417 new_entry = old_entry + 1;
418 dotdot_entry = old_entry + 2;
419 new_lh = (lock_handle *)(old_entry + 3);
420 dotdot_lh = new_lh + 1;
421 dotdot_name = (struct dentry *)(new_lh + 2);
422 dataonstack = (struct reiser4_dentry_fsdata *)(dotdot_name + 1);
424 assert("nikita-2318", old_dir != NULL);
425 assert("nikita-2319", new_dir != NULL);
426 assert("nikita-2320", old_name != NULL);
427 assert("nikita-2321", new_name != NULL);
429 old_inode = old_name->d_inode;
430 new_inode = new_name->d_inode;
432 dplug = inode_dir_plugin(old_dir);
433 fplug = NULL;
435 new_fsdata = reiser4_get_dentry_fsdata(new_name);
436 if (IS_ERR(new_fsdata)) {
437 kfree(old_entry);
438 context_set_commit_async(ctx);
439 reiser4_exit_context(ctx);
440 return PTR_ERR(new_fsdata);
443 new_coord = &new_fsdata->dec.entry_coord;
444 coord_clear_iplug(new_coord);
446 is_dir = S_ISDIR(old_inode->i_mode);
448 assert("nikita-3461", old_inode->i_nlink >= 1 + !!is_dir);
450 /* if target is existing directory and it's not empty---return error.
452 This check is done specifically, because is_dir_empty() requires
453 tree traversal and have to be done before locks are taken.
455 if (is_dir && new_inode != NULL && is_dir_empty(new_inode) != 0) {
456 kfree(old_entry);
457 context_set_commit_async(ctx);
458 reiser4_exit_context(ctx);
459 return RETERR(-ENOTEMPTY);
462 result = can_rename(old_dir, old_inode, new_dir, new_inode);
463 if (result != 0) {
464 kfree(old_entry);
465 context_set_commit_async(ctx);
466 reiser4_exit_context(ctx);
467 return result;
470 result = hashed_rename_estimate_and_grab(old_dir, old_name,
471 new_dir, new_name);
472 if (result != 0) {
473 kfree(old_entry);
474 context_set_commit_async(ctx);
475 reiser4_exit_context(ctx);
476 return result;
479 init_lh(new_lh);
481 /* find entry for @new_name */
482 result = reiser4_find_entry(new_dir, new_name, new_lh, ZNODE_WRITE_LOCK,
483 new_entry);
485 if (IS_CBKERR(result)) {
486 done_lh(new_lh);
487 kfree(old_entry);
488 context_set_commit_async(ctx);
489 reiser4_exit_context(ctx);
490 return result;
493 reiser4_seal_done(&new_fsdata->dec.entry_seal);
495 /* add or replace name for @old_inode as @new_name */
496 if (new_inode != NULL) {
497 /* target (@new_name) exists. */
498 /* Not clear what to do with objects that are
499 both directories and files at the same time. */
500 if (result == CBK_COORD_FOUND) {
501 result = replace_name(old_inode,
502 new_dir,
503 new_inode, new_coord, new_lh);
504 if (result == 0)
505 fplug = inode_file_plugin(new_inode);
506 } else if (result == CBK_COORD_NOTFOUND) {
507 /* VFS told us that @new_name is bound to existing
508 inode, but we failed to find directory entry. */
509 warning("nikita-2324", "Target not found");
510 result = RETERR(-ENOENT);
512 } else {
513 /* target (@new_name) doesn't exists. */
514 if (result == CBK_COORD_NOTFOUND)
515 result = add_name(old_inode,
516 new_dir,
517 new_name, new_coord, new_lh, is_dir);
518 else if (result == CBK_COORD_FOUND) {
519 /* VFS told us that @new_name is "negative" dentry,
520 but we found directory entry. */
521 warning("nikita-2331", "Target found unexpectedly");
522 result = RETERR(-EIO);
526 assert("nikita-3462", ergo(result == 0,
527 old_inode->i_nlink >= 2 + !!is_dir));
529 /* We are done with all modifications to the @new_dir, release lock on
530 node. */
531 done_lh(new_lh);
533 if (fplug != NULL) {
534 /* detach @new_inode from name-space */
535 result = fplug->detach(new_inode, new_dir);
536 if (result != 0)
537 warning("nikita-2330", "Cannot detach %lli: %i. %s",
538 (unsigned long long)get_inode_oid(new_inode),
539 result, possible_leak);
542 if (new_inode != NULL)
543 reiser4_update_sd(new_inode);
545 if (result == 0) {
546 old_entry->obj = old_inode;
548 dplug->build_entry_key(old_dir,
549 &old_name->d_name, &old_entry->key);
551 /* At this stage new name was introduced for
552 @old_inode. @old_inode, @new_dir, and @new_inode i_nlink
553 counters were updated.
555 We want to remove @old_name now. If @old_inode wasn't
556 directory this is simple.
558 result = dplug->rem_entry(old_dir, old_name, old_entry);
559 if (result != 0 && result != -ENOMEM) {
560 warning("nikita-2335",
561 "Cannot remove old name: %i", result);
562 } else {
563 result = reiser4_del_nlink(old_inode, old_dir, 0);
564 if (result != 0 && result != -ENOMEM) {
565 warning("nikita-2337",
566 "Cannot drop link on old: %i", result);
570 if (result == 0 && is_dir) {
571 /* @old_inode is directory. We also have to update
572 dotdot entry. */
573 coord_t *dotdot_coord;
575 memset(dataonstack, 0, sizeof dataonstack);
576 memset(dotdot_entry, 0, sizeof dotdot_entry);
577 dotdot_entry->obj = old_dir;
578 memset(dotdot_name, 0, sizeof dotdot_name);
579 dotdot_name->d_name.name = "..";
580 dotdot_name->d_name.len = 2;
582 * allocate ->d_fsdata on the stack to avoid using
583 * reiser4_get_dentry_fsdata(). Locking is not needed,
584 * because dentry is private to the current thread.
586 dotdot_name->d_fsdata = dataonstack;
587 init_lh(dotdot_lh);
589 dotdot_coord = &dataonstack->dec.entry_coord;
590 coord_clear_iplug(dotdot_coord);
592 result = reiser4_find_entry(old_inode, dotdot_name,
593 dotdot_lh, ZNODE_WRITE_LOCK,
594 dotdot_entry);
595 if (result == 0) {
596 /* replace_name() decreases i_nlink on
597 * @old_dir */
598 result = replace_name(new_dir,
599 old_inode,
600 old_dir,
601 dotdot_coord, dotdot_lh);
602 } else
603 result = RETERR(-EIO);
604 done_lh(dotdot_lh);
607 reiser4_update_dir(new_dir);
608 reiser4_update_dir(old_dir);
609 reiser4_update_sd(old_inode);
610 if (result == 0) {
611 file_plugin *fplug;
613 if (new_inode != NULL) {
614 /* add safe-link for target file (in case we removed
615 * last reference to the poor fellow */
616 fplug = inode_file_plugin(new_inode);
617 if (new_inode->i_nlink == 0)
618 result = safe_link_add(new_inode, SAFE_UNLINK);
621 kfree(old_entry);
622 context_set_commit_async(ctx);
623 reiser4_exit_context(ctx);
624 return result;
627 #if 0
628 int reiser4_rename_common(struct inode *old_dir /* directory where @old
629 * is located */ ,
630 struct dentry *old_name /* old name */ ,
631 struct inode *new_dir /* directory where @new
632 * is located */ ,
633 struct dentry *new_name/* new name */)
635 /* From `The Open Group Base Specifications Issue 6'
637 If either the old or new argument names a symbolic link, rename()
638 shall operate on the symbolic link itself, and shall not resolve
639 the last component of the argument. If the old argument and the new
640 argument resolve to the same existing file, rename() shall return
641 successfully and perform no other action.
643 [this is done by VFS: vfs_rename()]
645 If the old argument points to the pathname of a file that is not a
646 directory, the new argument shall not point to the pathname of a
647 directory.
649 [checked by VFS: vfs_rename->may_delete()]
651 If the link named by the new argument exists, it shall
652 be removed and old renamed to new. In this case, a link named new
653 shall remain visible to other processes throughout the renaming
654 operation and refer either to the file referred to by new or old
655 before the operation began.
657 [we should assure this]
659 Write access permission is required for
660 both the directory containing old and the directory containing new.
662 [checked by VFS: vfs_rename->may_delete(), may_create()]
664 If the old argument points to the pathname of a directory, the new
665 argument shall not point to the pathname of a file that is not a
666 directory.
668 [checked by VFS: vfs_rename->may_delete()]
670 If the directory named by the new argument exists, it
671 shall be removed and old renamed to new. In this case, a link named
672 new shall exist throughout the renaming operation and shall refer
673 either to the directory referred to by new or old before the
674 operation began.
676 [we should assure this]
678 If new names an existing directory, it shall be
679 required to be an empty directory.
681 [we should check this]
683 If the old argument points to a pathname of a symbolic link, the
684 symbolic link shall be renamed. If the new argument points to a
685 pathname of a symbolic link, the symbolic link shall be removed.
687 The new pathname shall not contain a path prefix that names
688 old. Write access permission is required for the directory
689 containing old and the directory containing new. If the old
690 argument points to the pathname of a directory, write access
691 permission may be required for the directory named by old, and, if
692 it exists, the directory named by new.
694 [checked by VFS: vfs_rename(), vfs_rename_dir()]
696 If the link named by the new argument exists and the file's link
697 count becomes 0 when it is removed and no process has the file
698 open, the space occupied by the file shall be freed and the file
699 shall no longer be accessible. If one or more processes have the
700 file open when the last link is removed, the link shall be removed
701 before rename() returns, but the removal of the file contents shall
702 be postponed until all references to the file are closed.
704 [iput() handles this, but we can do this manually, a la
705 reiser4_unlink()]
707 Upon successful completion, rename() shall mark for update the
708 st_ctime and st_mtime fields of the parent directory of each file.
710 [N/A]
713 reiser4_context *ctx;
714 int result;
715 int is_dir; /* is @old_name directory */
716 struct inode *old_inode;
717 struct inode *new_inode;
718 reiser4_dir_entry_desc old_entry;
719 reiser4_dir_entry_desc new_entry;
720 coord_t *new_coord;
721 struct reiser4_dentry_fsdata *new_fsdata;
722 lock_handle new_lh;
723 dir_plugin *dplug;
724 file_plugin *fplug;
726 ctx = reiser4_init_context(old_dir->i_sb);
727 if (IS_ERR(ctx))
728 return PTR_ERR(ctx);
730 assert("nikita-2318", old_dir != NULL);
731 assert("nikita-2319", new_dir != NULL);
732 assert("nikita-2320", old_name != NULL);
733 assert("nikita-2321", new_name != NULL);
735 old_inode = old_name->d_inode;
736 new_inode = new_name->d_inode;
738 dplug = inode_dir_plugin(old_dir);
739 fplug = NULL;
741 new_fsdata = reiser4_get_dentry_fsdata(new_name);
742 if (IS_ERR(new_fsdata)) {
743 result = PTR_ERR(new_fsdata);
744 goto exit;
747 new_coord = &new_fsdata->dec.entry_coord;
748 coord_clear_iplug(new_coord);
750 is_dir = S_ISDIR(old_inode->i_mode);
752 assert("nikita-3461", old_inode->i_nlink >= 1 + !!is_dir);
754 /* if target is existing directory and it's not empty---return error.
756 This check is done specifically, because is_dir_empty() requires
757 tree traversal and have to be done before locks are taken.
759 if (is_dir && new_inode != NULL && is_dir_empty(new_inode) != 0)
760 return RETERR(-ENOTEMPTY);
762 result = can_rename(old_dir, old_inode, new_dir, new_inode);
763 if (result != 0)
764 goto exit;
766 result = hashed_rename_estimate_and_grab(old_dir, old_name,
767 new_dir, new_name);
768 if (result != 0)
769 goto exit;
771 init_lh(&new_lh);
773 /* find entry for @new_name */
774 result = reiser4_find_entry(new_dir, new_name, &new_lh,
775 ZNODE_WRITE_LOCK, &new_entry);
777 if (IS_CBKERR(result)) {
778 done_lh(&new_lh);
779 goto exit;
782 reiser4_seal_done(&new_fsdata->dec.entry_seal);
784 /* add or replace name for @old_inode as @new_name */
785 if (new_inode != NULL) {
786 /* target (@new_name) exists. */
787 /* Not clear what to do with objects that are
788 both directories and files at the same time. */
789 if (result == CBK_COORD_FOUND) {
790 result = replace_name(old_inode,
791 new_dir,
792 new_inode, new_coord, &new_lh);
793 if (result == 0)
794 fplug = inode_file_plugin(new_inode);
795 } else if (result == CBK_COORD_NOTFOUND) {
796 /* VFS told us that @new_name is bound to existing
797 inode, but we failed to find directory entry. */
798 warning("nikita-2324", "Target not found");
799 result = RETERR(-ENOENT);
801 } else {
802 /* target (@new_name) doesn't exists. */
803 if (result == CBK_COORD_NOTFOUND)
804 result = add_name(old_inode,
805 new_dir,
806 new_name, new_coord, &new_lh, is_dir);
807 else if (result == CBK_COORD_FOUND) {
808 /* VFS told us that @new_name is "negative" dentry,
809 but we found directory entry. */
810 warning("nikita-2331", "Target found unexpectedly");
811 result = RETERR(-EIO);
815 assert("nikita-3462", ergo(result == 0,
816 old_inode->i_nlink >= 2 + !!is_dir));
818 /* We are done with all modifications to the @new_dir, release lock on
819 node. */
820 done_lh(&new_lh);
822 if (fplug != NULL) {
823 /* detach @new_inode from name-space */
824 result = fplug->detach(new_inode, new_dir);
825 if (result != 0)
826 warning("nikita-2330", "Cannot detach %lli: %i. %s",
827 (unsigned long long)get_inode_oid(new_inode),
828 result, possible_leak);
831 if (new_inode != NULL)
832 reiser4_update_sd(new_inode);
834 if (result == 0) {
835 memset(&old_entry, 0, sizeof old_entry);
836 old_entry.obj = old_inode;
838 dplug->build_entry_key(old_dir,
839 &old_name->d_name, &old_entry.key);
841 /* At this stage new name was introduced for
842 @old_inode. @old_inode, @new_dir, and @new_inode i_nlink
843 counters were updated.
845 We want to remove @old_name now. If @old_inode wasn't
846 directory this is simple.
848 result = dplug->rem_entry(old_dir, old_name, &old_entry);
849 /*result = rem_entry_hashed(old_dir, old_name, &old_entry); */
850 if (result != 0 && result != -ENOMEM) {
851 warning("nikita-2335",
852 "Cannot remove old name: %i", result);
853 } else {
854 result = reiser4_del_nlink(old_inode, old_dir, 0);
855 if (result != 0 && result != -ENOMEM) {
856 warning("nikita-2337",
857 "Cannot drop link on old: %i", result);
861 if (result == 0 && is_dir) {
862 /* @old_inode is directory. We also have to update
863 dotdot entry. */
864 coord_t *dotdot_coord;
865 lock_handle dotdot_lh;
866 struct dentry dotdot_name;
867 reiser4_dir_entry_desc dotdot_entry;
868 struct reiser4_dentry_fsdata dataonstack;
869 struct reiser4_dentry_fsdata *fsdata;
871 memset(&dataonstack, 0, sizeof dataonstack);
872 memset(&dotdot_entry, 0, sizeof dotdot_entry);
873 dotdot_entry.obj = old_dir;
874 memset(&dotdot_name, 0, sizeof dotdot_name);
875 dotdot_name.d_name.name = "..";
876 dotdot_name.d_name.len = 2;
878 * allocate ->d_fsdata on the stack to avoid using
879 * reiser4_get_dentry_fsdata(). Locking is not needed,
880 * because dentry is private to the current thread.
882 dotdot_name.d_fsdata = &dataonstack;
883 init_lh(&dotdot_lh);
885 fsdata = &dataonstack;
886 dotdot_coord = &fsdata->dec.entry_coord;
887 coord_clear_iplug(dotdot_coord);
889 result = reiser4_find_entry(old_inode,
890 &dotdot_name,
891 &dotdot_lh,
892 ZNODE_WRITE_LOCK,
893 &dotdot_entry);
894 if (result == 0) {
895 /* replace_name() decreases i_nlink on
896 * @old_dir */
897 result = replace_name(new_dir,
898 old_inode,
899 old_dir,
900 dotdot_coord, &dotdot_lh);
901 } else
902 result = RETERR(-EIO);
903 done_lh(&dotdot_lh);
906 reiser4_update_dir(new_dir);
907 reiser4_update_dir(old_dir);
908 reiser4_update_sd(old_inode);
909 if (result == 0) {
910 file_plugin *fplug;
912 if (new_inode != NULL) {
913 /* add safe-link for target file (in case we removed
914 * last reference to the poor fellow */
915 fplug = inode_file_plugin(new_inode);
916 if (new_inode->i_nlink == 0)
917 result = safe_link_add(new_inode, SAFE_UNLINK);
920 exit:
921 context_set_commit_async(ctx);
922 reiser4_exit_context(ctx);
923 return result;
925 #endif