[PATCH] orinoco: Remove unneeded forward declarations.
[linux-2.6/sactl.git] / fs / xfs / xfs_rename.c
blob23b48ac1cb7e5fb9ff73e1f041f8cbf37d9ef38b
1 /*
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:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
34 #include "xfs_macros.h"
35 #include "xfs_types.h"
36 #include "xfs_inum.h"
37 #include "xfs_log.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.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"
51 #include "xfs_bmap.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.
66 STATIC void
67 xfs_rename_unlock4(
68 xfs_inode_t **i_tab,
69 uint lock_mode)
71 int i;
73 xfs_iunlock(i_tab[0], lock_mode);
74 for (i = 1; i < 4; i++) {
75 if (i_tab[i] == NULL) {
76 break;
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);
87 #ifdef DEBUG
88 int xfs_rename_skip, xfs_rename_nskip;
89 #endif
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
95 * exist under dp2.
97 * We are renaming dp1/name1 to dp2/name2.
99 * Return ENOENT if dp1 does not exist, other lookup errors, or 0 for success.
101 STATIC int
102 xfs_lock_for_rename(
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;
115 int error;
116 int i, j;
117 uint lock_mode;
118 int diff_dirs = (dp1 != dp2);
120 ip2 = NULL;
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);
130 if (error) {
131 xfs_iunlock_map_shared(dp1, lock_mode);
132 return error;
135 inum1 = ip1->i_ino;
137 ASSERT(ip1);
138 ITRACE(ip1);
141 * Unlock dp1 and lock dp2 if they are different.
144 if (diff_dirs) {
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. */
152 inum2 = 0;
153 } else if (error) {
155 * If dp2 and dp1 are the same, the next line unlocks dp1.
156 * Got it?
158 xfs_iunlock_map_shared(dp2, lock_mode);
159 IRELE (ip1);
160 return error;
161 } else {
162 ITRACE(ip2);
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.
172 i_tab[0] = dp1;
173 i_tab[1] = dp2;
174 i_tab[2] = ip1;
175 if (inum2 == 0) {
176 *num_inodes = 3;
177 i_tab[3] = NULL;
178 } else {
179 *num_inodes = 4;
180 i_tab[3] = ip2;
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) {
190 temp = i_tab[j];
191 i_tab[j] = i_tab[j-1];
192 i_tab[j-1] = temp;
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) {
207 #ifdef DEBUG
208 xfs_rename_skip++;
209 #endif
210 xfs_lock_inodes(i_tab, *num_inodes, 1, XFS_ILOCK_SHARED);
211 } else {
212 #ifdef DEBUG
213 xfs_rename_nskip++;
214 #endif
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) {
225 *ipp1 = i_tab[i];
227 if (i_tab[i]->i_ino == inum2) {
228 *ipp2 = i_tab[i];
231 for (;i < 4; i++) {
232 i_tab[i] = NULL;
234 return 0;
238 * xfs_rename
241 xfs_rename(
242 bhv_desc_t *src_dir_bdp,
243 vname_t *src_vname,
244 vnode_t *target_dir_vp,
245 vname_t *target_vname,
246 cred_t *credp)
248 xfs_trans_t *tp;
249 xfs_inode_t *src_dp, *target_dp, *src_ip, *target_ip;
250 xfs_mount_t *mp;
251 int new_parent; /* moving to a new dir */
252 int src_is_directory; /* src_name is a directory */
253 int error;
254 xfs_bmap_free_t free_list;
255 xfs_fsblock_t first_block;
256 int cancel_flags;
257 int committed;
258 xfs_inode_t *inodes[4];
259 int target_ip_dropped = 0; /* dropped target_ip link? */
260 vnode_t *src_dir_vp;
261 bhv_desc_t *target_dir_bdp;
262 int spaceres;
263 int target_link_zero = 0;
264 int num_inodes;
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),
279 &xfs_vnodeops);
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,
295 0, 0, 0);
296 if (error) {
297 return error;
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.
310 tp = NULL;
311 error = xfs_lock_for_rename(src_dp, target_dp, src_vname,
312 target_vname, &src_ip, &target_ip, inodes,
313 &num_inodes);
315 if (error) {
317 * We have nothing locked, no inode references, and
318 * no transaction, so just get out.
320 goto std_return;
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);
333 goto rele_return;
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) {
352 spaceres = 0;
353 error = xfs_trans_reserve(tp, 0, XFS_RENAME_LOG_RES(mp), 0,
354 XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT);
356 if (error) {
357 xfs_trans_cancel(tp, 0);
358 goto rele_return;
362 * Attach the dquots to the inodes
364 if ((error = XFS_QM_DQVOPRENAME(mp, inodes))) {
365 xfs_trans_cancel(tp, cancel_flags);
366 goto rele_return;
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.
382 VN_HOLD(src_dir_vp);
383 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
384 if (new_parent) {
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);
399 * Set up the target.
401 if (target_ip == NULL) {
403 * If there's no space reservation, check the entry will
404 * fit before actually inserting it.
406 if (spaceres == 0 &&
407 (error = XFS_DIR_CANENTER(mp, tp, target_dp, target_name,
408 target_namelen))) {
409 goto error_return;
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) {
420 goto error_return;
422 if (error) {
423 goto abort_return;
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);
429 if (error) {
430 goto abort_return;
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);
447 goto error_return;
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);
463 if (error) {
464 goto abort_return;
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);
473 if (error) {
474 goto abort_return;
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);
483 if (error) {
484 goto abort_return;
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 */
494 * Remove the source.
496 if (new_parent && src_is_directory) {
499 * Rewrite the ".." entry to point to the new
500 * directory.
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);
506 if (error) {
507 goto abort_return;
509 xfs_ichgtime(src_ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
511 } else {
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);
536 if (error) {
537 goto abort_return;
541 error = XFS_DIR_REMOVENAME(mp, tp, src_dp, src_name, src_namelen,
542 src_ip->i_ino, &first_block, &free_list, spaceres);
543 if (error) {
544 goto abort_return;
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.
552 src_dp->i_gen++;
553 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
555 if (new_parent) {
556 target_dp->i_gen++;
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
563 * within the commit.
565 if (target_ip != NULL) {
566 IHOLD(target_ip);
570 * If this is a synchronous mount, make sure that the
571 * rename transaction goes to disk before returning to
572 * the user.
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
583 * to zero.
586 if (target_ip_dropped)
587 IHOLD(target_ip);
588 IHOLD(src_ip);
590 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
591 if (error) {
592 xfs_bmap_cancel(&free_list);
593 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
594 XFS_TRANS_ABORT));
595 if (target_ip != NULL) {
596 IRELE(target_ip);
598 if (target_ip_dropped) {
599 IRELE(target_ip);
601 IRELE(src_ip);
602 goto std_return;
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);
612 IRELE(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,
619 target_link_zero);
620 IRELE(target_ip);
623 FSC_NOTIFY_NAME_CHANGED(XFS_ITOV(src_ip));
625 IRELE(src_ip);
627 /* Fall through to std_return with error = 0 or errno from
628 * xfs_trans_commit */
629 std_return:
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,
637 0, error, 0);
639 return error;
641 abort_return:
642 cancel_flags |= XFS_TRANS_ABORT;
643 /* FALLTHROUGH */
644 error_return:
645 xfs_bmap_cancel(&free_list);
646 xfs_trans_cancel(tp, cancel_flags);
647 goto std_return;
649 rele_return:
650 IRELE(src_ip);
651 if (target_ip != NULL) {
652 IRELE(target_ip);
654 goto std_return;