diff options
author | Dave Chinner <dchinner@redhat.com> | 2013-08-12 06:49:46 -0400 |
---|---|---|
committer | Ben Myers <bpm@sgi.com> | 2013-08-12 17:54:09 -0400 |
commit | f6bba2017afb3bda8f2a27d6ba9d7023e8ff7f37 (patch) | |
tree | 85ea4ba3ab95623496b0beddeb76cc46e3957252 /fs/xfs | |
parent | c24b5dfadc4a4f7a13af373067871479c74455e6 (diff) |
xfs: consolidate xfs_rename.c
Move the rename code to xfs_inode.c to continue consolidating
all the kernel xfs_inode operations in the one place.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Mark Tinguely <tinguely@sgi.com>
Signed-off-by: Ben Myers <bpm@sgi.com>
Diffstat (limited to 'fs/xfs')
-rw-r--r-- | fs/xfs/Makefile | 1 | ||||
-rw-r--r-- | fs/xfs/xfs_inode.c | 308 | ||||
-rw-r--r-- | fs/xfs/xfs_rename.c | 347 |
3 files changed, 307 insertions, 349 deletions
diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index 8691dba546cd..e5a0758fd20c 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile | |||
@@ -50,7 +50,6 @@ xfs-y += xfs_aops.o \ | |||
50 | xfs_message.o \ | 50 | xfs_message.o \ |
51 | xfs_mount.o \ | 51 | xfs_mount.o \ |
52 | xfs_mru_cache.o \ | 52 | xfs_mru_cache.o \ |
53 | xfs_rename.o \ | ||
54 | xfs_super.o \ | 53 | xfs_super.o \ |
55 | xfs_symlink.o \ | 54 | xfs_symlink.o \ |
56 | xfs_trans.o \ | 55 | xfs_trans.o \ |
diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c index 73a7a1d84243..279ac3fdfde5 100644 --- a/fs/xfs/xfs_inode.c +++ b/fs/xfs/xfs_inode.c | |||
@@ -2268,6 +2268,313 @@ xfs_remove( | |||
2268 | return error; | 2268 | return error; |
2269 | } | 2269 | } |
2270 | 2270 | ||
2271 | /* | ||
2272 | * Enter all inodes for a rename transaction into a sorted array. | ||
2273 | */ | ||
2274 | STATIC void | ||
2275 | xfs_sort_for_rename( | ||
2276 | xfs_inode_t *dp1, /* in: old (source) directory inode */ | ||
2277 | xfs_inode_t *dp2, /* in: new (target) directory inode */ | ||
2278 | xfs_inode_t *ip1, /* in: inode of old entry */ | ||
2279 | xfs_inode_t *ip2, /* in: inode of new entry, if it | ||
2280 | already exists, NULL otherwise. */ | ||
2281 | xfs_inode_t **i_tab,/* out: array of inode returned, sorted */ | ||
2282 | int *num_inodes) /* out: number of inodes in array */ | ||
2283 | { | ||
2284 | xfs_inode_t *temp; | ||
2285 | int i, j; | ||
2286 | |||
2287 | /* | ||
2288 | * i_tab contains a list of pointers to inodes. We initialize | ||
2289 | * the table here & we'll sort it. We will then use it to | ||
2290 | * order the acquisition of the inode locks. | ||
2291 | * | ||
2292 | * Note that the table may contain duplicates. e.g., dp1 == dp2. | ||
2293 | */ | ||
2294 | i_tab[0] = dp1; | ||
2295 | i_tab[1] = dp2; | ||
2296 | i_tab[2] = ip1; | ||
2297 | if (ip2) { | ||
2298 | *num_inodes = 4; | ||
2299 | i_tab[3] = ip2; | ||
2300 | } else { | ||
2301 | *num_inodes = 3; | ||
2302 | i_tab[3] = NULL; | ||
2303 | } | ||
2304 | |||
2305 | /* | ||
2306 | * Sort the elements via bubble sort. (Remember, there are at | ||
2307 | * most 4 elements to sort, so this is adequate.) | ||
2308 | */ | ||
2309 | for (i = 0; i < *num_inodes; i++) { | ||
2310 | for (j = 1; j < *num_inodes; j++) { | ||
2311 | if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { | ||
2312 | temp = i_tab[j]; | ||
2313 | i_tab[j] = i_tab[j-1]; | ||
2314 | i_tab[j-1] = temp; | ||
2315 | } | ||
2316 | } | ||
2317 | } | ||
2318 | } | ||
2319 | |||
2320 | /* | ||
2321 | * xfs_rename | ||
2322 | */ | ||
2323 | int | ||
2324 | xfs_rename( | ||
2325 | xfs_inode_t *src_dp, | ||
2326 | struct xfs_name *src_name, | ||
2327 | xfs_inode_t *src_ip, | ||
2328 | xfs_inode_t *target_dp, | ||
2329 | struct xfs_name *target_name, | ||
2330 | xfs_inode_t *target_ip) | ||
2331 | { | ||
2332 | xfs_trans_t *tp = NULL; | ||
2333 | xfs_mount_t *mp = src_dp->i_mount; | ||
2334 | int new_parent; /* moving to a new dir */ | ||
2335 | int src_is_directory; /* src_name is a directory */ | ||
2336 | int error; | ||
2337 | xfs_bmap_free_t free_list; | ||
2338 | xfs_fsblock_t first_block; | ||
2339 | int cancel_flags; | ||
2340 | int committed; | ||
2341 | xfs_inode_t *inodes[4]; | ||
2342 | int spaceres; | ||
2343 | int num_inodes; | ||
2344 | |||
2345 | trace_xfs_rename(src_dp, target_dp, src_name, target_name); | ||
2346 | |||
2347 | new_parent = (src_dp != target_dp); | ||
2348 | src_is_directory = S_ISDIR(src_ip->i_d.di_mode); | ||
2349 | |||
2350 | xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, | ||
2351 | inodes, &num_inodes); | ||
2352 | |||
2353 | xfs_bmap_init(&free_list, &first_block); | ||
2354 | tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME); | ||
2355 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | ||
2356 | spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); | ||
2357 | error = xfs_trans_reserve(tp, spaceres, XFS_RENAME_LOG_RES(mp), 0, | ||
2358 | XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT); | ||
2359 | if (error == ENOSPC) { | ||
2360 | spaceres = 0; | ||
2361 | error = xfs_trans_reserve(tp, 0, XFS_RENAME_LOG_RES(mp), 0, | ||
2362 | XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT); | ||
2363 | } | ||
2364 | if (error) { | ||
2365 | xfs_trans_cancel(tp, 0); | ||
2366 | goto std_return; | ||
2367 | } | ||
2368 | |||
2369 | /* | ||
2370 | * Attach the dquots to the inodes | ||
2371 | */ | ||
2372 | error = xfs_qm_vop_rename_dqattach(inodes); | ||
2373 | if (error) { | ||
2374 | xfs_trans_cancel(tp, cancel_flags); | ||
2375 | goto std_return; | ||
2376 | } | ||
2377 | |||
2378 | /* | ||
2379 | * Lock all the participating inodes. Depending upon whether | ||
2380 | * the target_name exists in the target directory, and | ||
2381 | * whether the target directory is the same as the source | ||
2382 | * directory, we can lock from 2 to 4 inodes. | ||
2383 | */ | ||
2384 | xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); | ||
2385 | |||
2386 | /* | ||
2387 | * Join all the inodes to the transaction. From this point on, | ||
2388 | * we can rely on either trans_commit or trans_cancel to unlock | ||
2389 | * them. | ||
2390 | */ | ||
2391 | xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); | ||
2392 | if (new_parent) | ||
2393 | xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); | ||
2394 | xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); | ||
2395 | if (target_ip) | ||
2396 | xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); | ||
2397 | |||
2398 | /* | ||
2399 | * If we are using project inheritance, we only allow renames | ||
2400 | * into our tree when the project IDs are the same; else the | ||
2401 | * tree quota mechanism would be circumvented. | ||
2402 | */ | ||
2403 | if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | ||
2404 | (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) { | ||
2405 | error = XFS_ERROR(EXDEV); | ||
2406 | goto error_return; | ||
2407 | } | ||
2408 | |||
2409 | /* | ||
2410 | * Set up the target. | ||
2411 | */ | ||
2412 | if (target_ip == NULL) { | ||
2413 | /* | ||
2414 | * If there's no space reservation, check the entry will | ||
2415 | * fit before actually inserting it. | ||
2416 | */ | ||
2417 | error = xfs_dir_canenter(tp, target_dp, target_name, spaceres); | ||
2418 | if (error) | ||
2419 | goto error_return; | ||
2420 | /* | ||
2421 | * If target does not exist and the rename crosses | ||
2422 | * directories, adjust the target directory link count | ||
2423 | * to account for the ".." reference from the new entry. | ||
2424 | */ | ||
2425 | error = xfs_dir_createname(tp, target_dp, target_name, | ||
2426 | src_ip->i_ino, &first_block, | ||
2427 | &free_list, spaceres); | ||
2428 | if (error == ENOSPC) | ||
2429 | goto error_return; | ||
2430 | if (error) | ||
2431 | goto abort_return; | ||
2432 | |||
2433 | xfs_trans_ichgtime(tp, target_dp, | ||
2434 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
2435 | |||
2436 | if (new_parent && src_is_directory) { | ||
2437 | error = xfs_bumplink(tp, target_dp); | ||
2438 | if (error) | ||
2439 | goto abort_return; | ||
2440 | } | ||
2441 | } else { /* target_ip != NULL */ | ||
2442 | /* | ||
2443 | * If target exists and it's a directory, check that both | ||
2444 | * target and source are directories and that target can be | ||
2445 | * destroyed, or that neither is a directory. | ||
2446 | */ | ||
2447 | if (S_ISDIR(target_ip->i_d.di_mode)) { | ||
2448 | /* | ||
2449 | * Make sure target dir is empty. | ||
2450 | */ | ||
2451 | if (!(xfs_dir_isempty(target_ip)) || | ||
2452 | (target_ip->i_d.di_nlink > 2)) { | ||
2453 | error = XFS_ERROR(EEXIST); | ||
2454 | goto error_return; | ||
2455 | } | ||
2456 | } | ||
2457 | |||
2458 | /* | ||
2459 | * Link the source inode under the target name. | ||
2460 | * If the source inode is a directory and we are moving | ||
2461 | * it across directories, its ".." entry will be | ||
2462 | * inconsistent until we replace that down below. | ||
2463 | * | ||
2464 | * In case there is already an entry with the same | ||
2465 | * name at the destination directory, remove it first. | ||
2466 | */ | ||
2467 | error = xfs_dir_replace(tp, target_dp, target_name, | ||
2468 | src_ip->i_ino, | ||
2469 | &first_block, &free_list, spaceres); | ||
2470 | if (error) | ||
2471 | goto abort_return; | ||
2472 | |||
2473 | xfs_trans_ichgtime(tp, target_dp, | ||
2474 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
2475 | |||
2476 | /* | ||
2477 | * Decrement the link count on the target since the target | ||
2478 | * dir no longer points to it. | ||
2479 | */ | ||
2480 | error = xfs_droplink(tp, target_ip); | ||
2481 | if (error) | ||
2482 | goto abort_return; | ||
2483 | |||
2484 | if (src_is_directory) { | ||
2485 | /* | ||
2486 | * Drop the link from the old "." entry. | ||
2487 | */ | ||
2488 | error = xfs_droplink(tp, target_ip); | ||
2489 | if (error) | ||
2490 | goto abort_return; | ||
2491 | } | ||
2492 | } /* target_ip != NULL */ | ||
2493 | |||
2494 | /* | ||
2495 | * Remove the source. | ||
2496 | */ | ||
2497 | if (new_parent && src_is_directory) { | ||
2498 | /* | ||
2499 | * Rewrite the ".." entry to point to the new | ||
2500 | * directory. | ||
2501 | */ | ||
2502 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, | ||
2503 | target_dp->i_ino, | ||
2504 | &first_block, &free_list, spaceres); | ||
2505 | ASSERT(error != EEXIST); | ||
2506 | if (error) | ||
2507 | goto abort_return; | ||
2508 | } | ||
2509 | |||
2510 | /* | ||
2511 | * We always want to hit the ctime on the source inode. | ||
2512 | * | ||
2513 | * This isn't strictly required by the standards since the source | ||
2514 | * inode isn't really being changed, but old unix file systems did | ||
2515 | * it and some incremental backup programs won't work without it. | ||
2516 | */ | ||
2517 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); | ||
2518 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); | ||
2519 | |||
2520 | /* | ||
2521 | * Adjust the link count on src_dp. This is necessary when | ||
2522 | * renaming a directory, either within one parent when | ||
2523 | * the target existed, or across two parent directories. | ||
2524 | */ | ||
2525 | if (src_is_directory && (new_parent || target_ip != NULL)) { | ||
2526 | |||
2527 | /* | ||
2528 | * Decrement link count on src_directory since the | ||
2529 | * entry that's moved no longer points to it. | ||
2530 | */ | ||
2531 | error = xfs_droplink(tp, src_dp); | ||
2532 | if (error) | ||
2533 | goto abort_return; | ||
2534 | } | ||
2535 | |||
2536 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, | ||
2537 | &first_block, &free_list, spaceres); | ||
2538 | if (error) | ||
2539 | goto abort_return; | ||
2540 | |||
2541 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
2542 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); | ||
2543 | if (new_parent) | ||
2544 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); | ||
2545 | |||
2546 | /* | ||
2547 | * If this is a synchronous mount, make sure that the | ||
2548 | * rename transaction goes to disk before returning to | ||
2549 | * the user. | ||
2550 | */ | ||
2551 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | ||
2552 | xfs_trans_set_sync(tp); | ||
2553 | } | ||
2554 | |||
2555 | error = xfs_bmap_finish(&tp, &free_list, &committed); | ||
2556 | if (error) { | ||
2557 | xfs_bmap_cancel(&free_list); | ||
2558 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | | ||
2559 | XFS_TRANS_ABORT)); | ||
2560 | goto std_return; | ||
2561 | } | ||
2562 | |||
2563 | /* | ||
2564 | * trans_commit will unlock src_ip, target_ip & decrement | ||
2565 | * the vnode references. | ||
2566 | */ | ||
2567 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | ||
2568 | |||
2569 | abort_return: | ||
2570 | cancel_flags |= XFS_TRANS_ABORT; | ||
2571 | error_return: | ||
2572 | xfs_bmap_cancel(&free_list); | ||
2573 | xfs_trans_cancel(tp, cancel_flags); | ||
2574 | std_return: | ||
2575 | return error; | ||
2576 | } | ||
2577 | |||
2271 | STATIC int | 2578 | STATIC int |
2272 | xfs_iflush_cluster( | 2579 | xfs_iflush_cluster( |
2273 | xfs_inode_t *ip, | 2580 | xfs_inode_t *ip, |
@@ -2524,7 +2831,6 @@ abort_out: | |||
2524 | return error; | 2831 | return error; |
2525 | } | 2832 | } |
2526 | 2833 | ||
2527 | |||
2528 | STATIC int | 2834 | STATIC int |
2529 | xfs_iflush_int( | 2835 | xfs_iflush_int( |
2530 | struct xfs_inode *ip, | 2836 | struct xfs_inode *ip, |
diff --git a/fs/xfs/xfs_rename.c b/fs/xfs/xfs_rename.c deleted file mode 100644 index c15c6c581d56..000000000000 --- a/fs/xfs/xfs_rename.c +++ /dev/null | |||
@@ -1,347 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. | ||
3 | * All Rights Reserved. | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU General Public License as | ||
7 | * published by the Free Software Foundation. | ||
8 | * | ||
9 | * This program is distributed in the hope that it would be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write the Free Software Foundation, | ||
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | #include "xfs.h" | ||
19 | #include "xfs_fs.h" | ||
20 | #include "xfs_format.h" | ||
21 | #include "xfs_log.h" | ||
22 | #include "xfs_trans.h" | ||
23 | #include "xfs_sb.h" | ||
24 | #include "xfs_ag.h" | ||
25 | #include "xfs_mount.h" | ||
26 | #include "xfs_da_btree.h" | ||
27 | #include "xfs_dir2_format.h" | ||
28 | #include "xfs_dir2.h" | ||
29 | #include "xfs_bmap_btree.h" | ||
30 | #include "xfs_dinode.h" | ||
31 | #include "xfs_inode.h" | ||
32 | #include "xfs_inode_item.h" | ||
33 | #include "xfs_bmap.h" | ||
34 | #include "xfs_bmap_util.h" | ||
35 | #include "xfs_error.h" | ||
36 | #include "xfs_quota.h" | ||
37 | #include "xfs_utils.h" | ||
38 | #include "xfs_trans_space.h" | ||
39 | #include "xfs_trace.h" | ||
40 | |||
41 | |||
42 | /* | ||
43 | * Enter all inodes for a rename transaction into a sorted array. | ||
44 | */ | ||
45 | STATIC void | ||
46 | xfs_sort_for_rename( | ||
47 | xfs_inode_t *dp1, /* in: old (source) directory inode */ | ||
48 | xfs_inode_t *dp2, /* in: new (target) directory inode */ | ||
49 | xfs_inode_t *ip1, /* in: inode of old entry */ | ||
50 | xfs_inode_t *ip2, /* in: inode of new entry, if it | ||
51 | already exists, NULL otherwise. */ | ||
52 | xfs_inode_t **i_tab,/* out: array of inode returned, sorted */ | ||
53 | int *num_inodes) /* out: number of inodes in array */ | ||
54 | { | ||
55 | xfs_inode_t *temp; | ||
56 | int i, j; | ||
57 | |||
58 | /* | ||
59 | * i_tab contains a list of pointers to inodes. We initialize | ||
60 | * the table here & we'll sort it. We will then use it to | ||
61 | * order the acquisition of the inode locks. | ||
62 | * | ||
63 | * Note that the table may contain duplicates. e.g., dp1 == dp2. | ||
64 | */ | ||
65 | i_tab[0] = dp1; | ||
66 | i_tab[1] = dp2; | ||
67 | i_tab[2] = ip1; | ||
68 | if (ip2) { | ||
69 | *num_inodes = 4; | ||
70 | i_tab[3] = ip2; | ||
71 | } else { | ||
72 | *num_inodes = 3; | ||
73 | i_tab[3] = NULL; | ||
74 | } | ||
75 | |||
76 | /* | ||
77 | * Sort the elements via bubble sort. (Remember, there are at | ||
78 | * most 4 elements to sort, so this is adequate.) | ||
79 | */ | ||
80 | for (i = 0; i < *num_inodes; i++) { | ||
81 | for (j = 1; j < *num_inodes; j++) { | ||
82 | if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { | ||
83 | temp = i_tab[j]; | ||
84 | i_tab[j] = i_tab[j-1]; | ||
85 | i_tab[j-1] = temp; | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * xfs_rename | ||
93 | */ | ||
94 | int | ||
95 | xfs_rename( | ||
96 | xfs_inode_t *src_dp, | ||
97 | struct xfs_name *src_name, | ||
98 | xfs_inode_t *src_ip, | ||
99 | xfs_inode_t *target_dp, | ||
100 | struct xfs_name *target_name, | ||
101 | xfs_inode_t *target_ip) | ||
102 | { | ||
103 | xfs_trans_t *tp = NULL; | ||
104 | xfs_mount_t *mp = src_dp->i_mount; | ||
105 | int new_parent; /* moving to a new dir */ | ||
106 | int src_is_directory; /* src_name is a directory */ | ||
107 | int error; | ||
108 | xfs_bmap_free_t free_list; | ||
109 | xfs_fsblock_t first_block; | ||
110 | int cancel_flags; | ||
111 | int committed; | ||
112 | xfs_inode_t *inodes[4]; | ||
113 | int spaceres; | ||
114 | int num_inodes; | ||
115 | |||
116 | trace_xfs_rename(src_dp, target_dp, src_name, target_name); | ||
117 | |||
118 | new_parent = (src_dp != target_dp); | ||
119 | src_is_directory = S_ISDIR(src_ip->i_d.di_mode); | ||
120 | |||
121 | xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, | ||
122 | inodes, &num_inodes); | ||
123 | |||
124 | xfs_bmap_init(&free_list, &first_block); | ||
125 | tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME); | ||
126 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; | ||
127 | spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); | ||
128 | error = xfs_trans_reserve(tp, spaceres, XFS_RENAME_LOG_RES(mp), 0, | ||
129 | XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT); | ||
130 | if (error == ENOSPC) { | ||
131 | spaceres = 0; | ||
132 | error = xfs_trans_reserve(tp, 0, XFS_RENAME_LOG_RES(mp), 0, | ||
133 | XFS_TRANS_PERM_LOG_RES, XFS_RENAME_LOG_COUNT); | ||
134 | } | ||
135 | if (error) { | ||
136 | xfs_trans_cancel(tp, 0); | ||
137 | goto std_return; | ||
138 | } | ||
139 | |||
140 | /* | ||
141 | * Attach the dquots to the inodes | ||
142 | */ | ||
143 | error = xfs_qm_vop_rename_dqattach(inodes); | ||
144 | if (error) { | ||
145 | xfs_trans_cancel(tp, cancel_flags); | ||
146 | goto std_return; | ||
147 | } | ||
148 | |||
149 | /* | ||
150 | * Lock all the participating inodes. Depending upon whether | ||
151 | * the target_name exists in the target directory, and | ||
152 | * whether the target directory is the same as the source | ||
153 | * directory, we can lock from 2 to 4 inodes. | ||
154 | */ | ||
155 | xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); | ||
156 | |||
157 | /* | ||
158 | * Join all the inodes to the transaction. From this point on, | ||
159 | * we can rely on either trans_commit or trans_cancel to unlock | ||
160 | * them. | ||
161 | */ | ||
162 | xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); | ||
163 | if (new_parent) | ||
164 | xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); | ||
165 | xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); | ||
166 | if (target_ip) | ||
167 | xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); | ||
168 | |||
169 | /* | ||
170 | * If we are using project inheritance, we only allow renames | ||
171 | * into our tree when the project IDs are the same; else the | ||
172 | * tree quota mechanism would be circumvented. | ||
173 | */ | ||
174 | if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) && | ||
175 | (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) { | ||
176 | error = XFS_ERROR(EXDEV); | ||
177 | goto error_return; | ||
178 | } | ||
179 | |||
180 | /* | ||
181 | * Set up the target. | ||
182 | */ | ||
183 | if (target_ip == NULL) { | ||
184 | /* | ||
185 | * If there's no space reservation, check the entry will | ||
186 | * fit before actually inserting it. | ||
187 | */ | ||
188 | error = xfs_dir_canenter(tp, target_dp, target_name, spaceres); | ||
189 | if (error) | ||
190 | goto error_return; | ||
191 | /* | ||
192 | * If target does not exist and the rename crosses | ||
193 | * directories, adjust the target directory link count | ||
194 | * to account for the ".." reference from the new entry. | ||
195 | */ | ||
196 | error = xfs_dir_createname(tp, target_dp, target_name, | ||
197 | src_ip->i_ino, &first_block, | ||
198 | &free_list, spaceres); | ||
199 | if (error == ENOSPC) | ||
200 | goto error_return; | ||
201 | if (error) | ||
202 | goto abort_return; | ||
203 | |||
204 | xfs_trans_ichgtime(tp, target_dp, | ||
205 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
206 | |||
207 | if (new_parent && src_is_directory) { | ||
208 | error = xfs_bumplink(tp, target_dp); | ||
209 | if (error) | ||
210 | goto abort_return; | ||
211 | } | ||
212 | } else { /* target_ip != NULL */ | ||
213 | /* | ||
214 | * If target exists and it's a directory, check that both | ||
215 | * target and source are directories and that target can be | ||
216 | * destroyed, or that neither is a directory. | ||
217 | */ | ||
218 | if (S_ISDIR(target_ip->i_d.di_mode)) { | ||
219 | /* | ||
220 | * Make sure target dir is empty. | ||
221 | */ | ||
222 | if (!(xfs_dir_isempty(target_ip)) || | ||
223 | (target_ip->i_d.di_nlink > 2)) { | ||
224 | error = XFS_ERROR(EEXIST); | ||
225 | goto error_return; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * Link the source inode under the target name. | ||
231 | * If the source inode is a directory and we are moving | ||
232 | * it across directories, its ".." entry will be | ||
233 | * inconsistent until we replace that down below. | ||
234 | * | ||
235 | * In case there is already an entry with the same | ||
236 | * name at the destination directory, remove it first. | ||
237 | */ | ||
238 | error = xfs_dir_replace(tp, target_dp, target_name, | ||
239 | src_ip->i_ino, | ||
240 | &first_block, &free_list, spaceres); | ||
241 | if (error) | ||
242 | goto abort_return; | ||
243 | |||
244 | xfs_trans_ichgtime(tp, target_dp, | ||
245 | XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
246 | |||
247 | /* | ||
248 | * Decrement the link count on the target since the target | ||
249 | * dir no longer points to it. | ||
250 | */ | ||
251 | error = xfs_droplink(tp, target_ip); | ||
252 | if (error) | ||
253 | goto abort_return; | ||
254 | |||
255 | if (src_is_directory) { | ||
256 | /* | ||
257 | * Drop the link from the old "." entry. | ||
258 | */ | ||
259 | error = xfs_droplink(tp, target_ip); | ||
260 | if (error) | ||
261 | goto abort_return; | ||
262 | } | ||
263 | } /* target_ip != NULL */ | ||
264 | |||
265 | /* | ||
266 | * Remove the source. | ||
267 | */ | ||
268 | if (new_parent && src_is_directory) { | ||
269 | /* | ||
270 | * Rewrite the ".." entry to point to the new | ||
271 | * directory. | ||
272 | */ | ||
273 | error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, | ||
274 | target_dp->i_ino, | ||
275 | &first_block, &free_list, spaceres); | ||
276 | ASSERT(error != EEXIST); | ||
277 | if (error) | ||
278 | goto abort_return; | ||
279 | } | ||
280 | |||
281 | /* | ||
282 | * We always want to hit the ctime on the source inode. | ||
283 | * | ||
284 | * This isn't strictly required by the standards since the source | ||
285 | * inode isn't really being changed, but old unix file systems did | ||
286 | * it and some incremental backup programs won't work without it. | ||
287 | */ | ||
288 | xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); | ||
289 | xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); | ||
290 | |||
291 | /* | ||
292 | * Adjust the link count on src_dp. This is necessary when | ||
293 | * renaming a directory, either within one parent when | ||
294 | * the target existed, or across two parent directories. | ||
295 | */ | ||
296 | if (src_is_directory && (new_parent || target_ip != NULL)) { | ||
297 | |||
298 | /* | ||
299 | * Decrement link count on src_directory since the | ||
300 | * entry that's moved no longer points to it. | ||
301 | */ | ||
302 | error = xfs_droplink(tp, src_dp); | ||
303 | if (error) | ||
304 | goto abort_return; | ||
305 | } | ||
306 | |||
307 | error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, | ||
308 | &first_block, &free_list, spaceres); | ||
309 | if (error) | ||
310 | goto abort_return; | ||
311 | |||
312 | xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); | ||
313 | xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); | ||
314 | if (new_parent) | ||
315 | xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); | ||
316 | |||
317 | /* | ||
318 | * If this is a synchronous mount, make sure that the | ||
319 | * rename transaction goes to disk before returning to | ||
320 | * the user. | ||
321 | */ | ||
322 | if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) { | ||
323 | xfs_trans_set_sync(tp); | ||
324 | } | ||
325 | |||
326 | error = xfs_bmap_finish(&tp, &free_list, &committed); | ||
327 | if (error) { | ||
328 | xfs_bmap_cancel(&free_list); | ||
329 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | | ||
330 | XFS_TRANS_ABORT)); | ||
331 | goto std_return; | ||
332 | } | ||
333 | |||
334 | /* | ||
335 | * trans_commit will unlock src_ip, target_ip & decrement | ||
336 | * the vnode references. | ||
337 | */ | ||
338 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | ||
339 | |||
340 | abort_return: | ||
341 | cancel_flags |= XFS_TRANS_ABORT; | ||
342 | error_return: | ||
343 | xfs_bmap_cancel(&free_list); | ||
344 | xfs_trans_cancel(tp, cancel_flags); | ||
345 | std_return: | ||
346 | return error; | ||
347 | } | ||