2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
38 #include "xfs_trans.h"
42 #include "xfs_dmapi.h"
43 #include "xfs_mount.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_attr_sf.h"
46 #include "xfs_dir_sf.h"
47 #include "xfs_dir2_sf.h"
48 #include "xfs_dinode.h"
49 #include "xfs_inode_item.h"
50 #include "xfs_inode.h"
52 #include "xfs_error.h"
53 #include "xfs_quota.h"
54 #include "xfs_refcache.h"
55 #include "xfs_utils.h"
56 #include "xfs_trans_space.h"
57 #include "xfs_da_btree.h"
58 #include "xfs_dir_leaf.h"
62 * Given an array of up to 4 inode pointers, unlock the pointed to inodes.
63 * If there are fewer than 4 entries in the array, the empty entries will
64 * be at the end and will have NULL pointers in them.
73 xfs_iunlock(i_tab[0], lock_mode);
74 for (i = 1; i < 4; i++) {
75 if (i_tab[i] == NULL) {
79 * Watch out for duplicate entries in the table.
81 if (i_tab[i] != i_tab[i-1]) {
82 xfs_iunlock(i_tab[i], lock_mode);
88 int xfs_rename_skip, xfs_rename_nskip;
92 * The following routine will acquire the locks required for a rename
93 * operation. The code understands the semantics of renames and will
94 * validate that name1 exists under dp1 & that name2 may or may not
97 * We are renaming dp1/name1 to dp2/name2.
99 * Return ENOENT if dp1 does not exist, other lookup errors, or 0 for success.
103 xfs_inode_t *dp1, /* old (source) directory inode */
104 xfs_inode_t *dp2, /* new (target) directory inode */
105 vname_t *vname1,/* old entry name */
106 vname_t *vname2,/* new entry name */
107 xfs_inode_t **ipp1, /* inode of old entry */
108 xfs_inode_t **ipp2, /* inode of new entry, if it
109 already exists, NULL otherwise. */
110 xfs_inode_t **i_tab,/* array of inode returned, sorted */
111 int *num_inodes) /* number of inodes in array */
113 xfs_inode_t *ip1, *ip2, *temp;
114 xfs_ino_t inum1, inum2;
118 int diff_dirs = (dp1 != dp2);
123 * First, find out the current inums of the entries so that we
124 * can determine the initial locking order. We'll have to
125 * sanity check stuff after all the locks have been acquired
126 * to see if we still have the right inodes, directories, etc.
128 lock_mode = xfs_ilock_map_shared(dp1);
129 error = xfs_get_dir_entry(vname1, &ip1);
131 xfs_iunlock_map_shared(dp1, lock_mode);
141 * Unlock dp1 and lock dp2 if they are different.
145 xfs_iunlock_map_shared(dp1, lock_mode);
146 lock_mode = xfs_ilock_map_shared(dp2);
149 error = xfs_dir_lookup_int(XFS_ITOBHV(dp2), lock_mode,
150 vname2, &inum2, &ip2);
151 if (error == ENOENT) { /* target does not need to exist. */
155 * If dp2 and dp1 are the same, the next line unlocks dp1.
158 xfs_iunlock_map_shared(dp2, lock_mode);
166 * i_tab contains a list of pointers to inodes. We initialize
167 * the table here & we'll sort it. We will then use it to
168 * order the acquisition of the inode locks.
170 * Note that the table may contain duplicates. e.g., dp1 == dp2.
184 * Sort the elements via bubble sort. (Remember, there are at
185 * most 4 elements to sort, so this is adequate.)
187 for (i=0; i < *num_inodes; i++) {
188 for (j=1; j < *num_inodes; j++) {
189 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
191 i_tab[j] = i_tab[j-1];
198 * We have dp2 locked. If it isn't first, unlock it.
199 * If it is first, tell xfs_lock_inodes so it can skip it
200 * when locking. if dp1 == dp2, xfs_lock_inodes will skip both
201 * since they are equal. xfs_lock_inodes needs all these inodes
202 * so that it can unlock and retry if there might be a dead-lock
203 * potential with the log.
206 if (i_tab[0] == dp2 && lock_mode == XFS_ILOCK_SHARED) {
210 xfs_lock_inodes(i_tab, *num_inodes, 1, XFS_ILOCK_SHARED);
215 xfs_iunlock_map_shared(dp2, lock_mode);
216 xfs_lock_inodes(i_tab, *num_inodes, 0, XFS_ILOCK_SHARED);
220 * Set the return value. Null out any unused entries in i_tab.
222 *ipp1 = *ipp2 = NULL;
223 for (i=0; i < *num_inodes; i++) {
224 if (i_tab[i]->i_ino == inum1) {
227 if (i_tab[i]->i_ino == inum2) {
242 bhv_desc_t *src_dir_bdp,
244 vnode_t *target_dir_vp,
245 vname_t *target_vname,
249 xfs_inode_t *src_dp, *target_dp, *src_ip, *target_ip;
251 int new_parent; /* moving to a new dir */
252 int src_is_directory; /* src_name is a directory */
254 xfs_bmap_free_t free_list;
255 xfs_fsblock_t first_block;
258 xfs_inode_t *inodes[4];
259 int target_ip_dropped = 0; /* dropped target_ip link? */
261 bhv_desc_t *target_dir_bdp;
263 int target_link_zero = 0;
265 char *src_name = VNAME(src_vname);
266 char *target_name = VNAME(target_vname);
267 int src_namelen = VNAMELEN(src_vname);
268 int target_namelen = VNAMELEN(target_vname);
270 src_dir_vp = BHV_TO_VNODE(src_dir_bdp);
271 vn_trace_entry(src_dir_vp, "xfs_rename", (inst_t *)__return_address);
272 vn_trace_entry(target_dir_vp, "xfs_rename", (inst_t *)__return_address);
275 * Find the XFS behavior descriptor for the target directory
276 * vnode since it was not handed to us.
278 target_dir_bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(target_dir_vp),
280 if (target_dir_bdp == NULL) {
281 return XFS_ERROR(EXDEV);
284 src_dp = XFS_BHVTOI(src_dir_bdp);
285 target_dp = XFS_BHVTOI(target_dir_bdp);
286 mp = src_dp->i_mount;
288 if (DM_EVENT_ENABLED(src_dir_vp->v_vfsp, src_dp, DM_EVENT_RENAME) ||
289 DM_EVENT_ENABLED(target_dir_vp->v_vfsp,
290 target_dp, DM_EVENT_RENAME)) {
291 error = XFS_SEND_NAMESP(mp, DM_EVENT_RENAME,
292 src_dir_vp, DM_RIGHT_NULL,
293 target_dir_vp, DM_RIGHT_NULL,
294 src_name, target_name,
300 /* Return through std_return after this point. */
303 * Lock all the participating inodes. Depending upon whether
304 * the target_name exists in the target directory, and
305 * whether the target directory is the same as the source
306 * directory, we can lock from 2 to 4 inodes.
307 * xfs_lock_for_rename() will return ENOENT if src_name
308 * does not exist in the source directory.
311 error = xfs_lock_for_rename(src_dp, target_dp, src_vname,
312 target_vname, &src_ip, &target_ip, inodes,
317 * We have nothing locked, no inode references, and
318 * no transaction, so just get out.
323 ASSERT(src_ip != NULL);
325 if ((src_ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
327 * Check for link count overflow on target_dp
329 if (target_ip == NULL && (src_dp != target_dp) &&
330 target_dp->i_d.di_nlink >= XFS_MAXLINK) {
331 error = XFS_ERROR(EMLINK);
332 xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
337 new_parent = (src_dp != target_dp);
338 src_is_directory = ((src_ip->i_d.di_mode & S_IFMT) == S_IFDIR);
341 * Drop the locks on our inodes so that we can start the transaction.
343 xfs_rename_unlock4(inodes, XFS_ILOCK_SHARED);
345 XFS_BMAP_INIT(&free_list, &first_block);
346 tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
347 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
348 spaceres = XFS_RENAME_SPACE_RES(mp, target_namelen);
349 error = xfs_trans_reserve(tp, spaceres, XFS_RENAME_LOG_RES(mp), 0,
350 XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
351 if (error == ENOSPC) {
353 error = xfs_trans_reserve(tp, 0, XFS_RENAME_LOG_RES(mp), 0,
354 XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
357 xfs_trans_cancel(tp, 0);
362 * Attach the dquots to the inodes
364 if ((error = XFS_QM_DQVOPRENAME(mp, inodes))) {
365 xfs_trans_cancel(tp, cancel_flags);
370 * Reacquire the inode locks we dropped above.
372 xfs_lock_inodes(inodes, num_inodes, 0, XFS_ILOCK_EXCL);
375 * Join all the inodes to the transaction. From this point on,
376 * we can rely on either trans_commit or trans_cancel to unlock
377 * them. Note that we need to add a vnode reference to the
378 * directories since trans_commit & trans_cancel will decrement
379 * them when they unlock the inodes. Also, we need to be careful
380 * not to add an inode to the transaction more than once.
383 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
385 VN_HOLD(target_dir_vp);
386 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
388 if ((src_ip != src_dp) && (src_ip != target_dp)) {
389 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
391 if ((target_ip != NULL) &&
392 (target_ip != src_ip) &&
393 (target_ip != src_dp) &&
394 (target_ip != target_dp)) {
395 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
401 if (target_ip == NULL) {
403 * If there's no space reservation, check the entry will
404 * fit before actually inserting it.
407 (error = XFS_DIR_CANENTER(mp, tp, target_dp, target_name,
412 * If target does not exist and the rename crosses
413 * directories, adjust the target directory link count
414 * to account for the ".." reference from the new entry.
416 error = XFS_DIR_CREATENAME(mp, tp, target_dp, target_name,
417 target_namelen, src_ip->i_ino,
418 &first_block, &free_list, spaceres);
419 if (error == ENOSPC) {
425 xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
427 if (new_parent && src_is_directory) {
428 error = xfs_bumplink(tp, target_dp);
433 } else { /* target_ip != NULL */
436 * If target exists and it's a directory, check that both
437 * target and source are directories and that target can be
438 * destroyed, or that neither is a directory.
440 if ((target_ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
442 * Make sure target dir is empty.
444 if (!(XFS_DIR_ISEMPTY(target_ip->i_mount, target_ip)) ||
445 (target_ip->i_d.di_nlink > 2)) {
446 error = XFS_ERROR(EEXIST);
452 * Link the source inode under the target name.
453 * If the source inode is a directory and we are moving
454 * it across directories, its ".." entry will be
455 * inconsistent until we replace that down below.
457 * In case there is already an entry with the same
458 * name at the destination directory, remove it first.
460 error = XFS_DIR_REPLACE(mp, tp, target_dp, target_name,
461 target_namelen, src_ip->i_ino, &first_block,
462 &free_list, spaceres);
466 xfs_ichgtime(target_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
469 * Decrement the link count on the target since the target
470 * dir no longer points to it.
472 error = xfs_droplink(tp, target_ip);
476 target_ip_dropped = 1;
478 if (src_is_directory) {
480 * Drop the link from the old "." entry.
482 error = xfs_droplink(tp, target_ip);
488 /* Do this test while we still hold the locks */
489 target_link_zero = (target_ip)->i_d.di_nlink==0;
491 } /* target_ip != NULL */
496 if (new_parent && src_is_directory) {
499 * Rewrite the ".." entry to point to the new
502 error = XFS_DIR_REPLACE(mp, tp, src_ip, "..", 2,
503 target_dp->i_ino, &first_block,
504 &free_list, spaceres);
505 ASSERT(error != EEXIST);
509 xfs_ichgtime(src_ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
513 * We always want to hit the ctime on the source inode.
514 * We do it in the if clause above for the 'new_parent &&
515 * src_is_directory' case, and here we get all the other
516 * cases. This isn't strictly required by the standards
517 * since the source inode isn't really being changed,
518 * but old unix file systems did it and some incremental
519 * backup programs won't work without it.
521 xfs_ichgtime(src_ip, XFS_ICHGTIME_CHG);
525 * Adjust the link count on src_dp. This is necessary when
526 * renaming a directory, either within one parent when
527 * the target existed, or across two parent directories.
529 if (src_is_directory && (new_parent || target_ip != NULL)) {
532 * Decrement link count on src_directory since the
533 * entry that's moved no longer points to it.
535 error = xfs_droplink(tp, src_dp);
541 error = XFS_DIR_REMOVENAME(mp, tp, src_dp, src_name, src_namelen,
542 src_ip->i_ino, &first_block, &free_list, spaceres);
546 xfs_ichgtime(src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
549 * Update the generation counts on all the directory inodes
550 * that we're modifying.
553 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
557 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
561 * If there was a target inode, take an extra reference on
562 * it here so that it doesn't go to xfs_inactive() from
565 if (target_ip != NULL) {
570 * If this is a synchronous mount, make sure that the
571 * rename transaction goes to disk before returning to
574 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
575 xfs_trans_set_sync(tp);
579 * Take refs. for vop_link_removed calls below. No need to worry
580 * about directory refs. because the caller holds them.
582 * Do holds before the xfs_bmap_finish since it might rele them down
586 if (target_ip_dropped)
590 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
592 xfs_bmap_cancel(&free_list);
593 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
595 if (target_ip != NULL) {
598 if (target_ip_dropped) {
606 * trans_commit will unlock src_ip, target_ip & decrement
607 * the vnode references.
609 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
610 if (target_ip != NULL) {
611 xfs_refcache_purge_ip(target_ip);
615 * Let interposed file systems know about removed links.
617 if (target_ip_dropped) {
618 VOP_LINK_REMOVED(XFS_ITOV(target_ip), target_dir_vp,
623 FSC_NOTIFY_NAME_CHANGED(XFS_ITOV(src_ip));
627 /* Fall through to std_return with error = 0 or errno from
628 * xfs_trans_commit */
630 if (DM_EVENT_ENABLED(src_dir_vp->v_vfsp, src_dp, DM_EVENT_POSTRENAME) ||
631 DM_EVENT_ENABLED(target_dir_vp->v_vfsp,
632 target_dp, DM_EVENT_POSTRENAME)) {
633 (void) XFS_SEND_NAMESP (mp, DM_EVENT_POSTRENAME,
634 src_dir_vp, DM_RIGHT_NULL,
635 target_dir_vp, DM_RIGHT_NULL,
636 src_name, target_name,
642 cancel_flags |= XFS_TRANS_ABORT;
645 xfs_bmap_cancel(&free_list);
646 xfs_trans_cancel(tp, cancel_flags);
651 if (target_ip != NULL) {