aboutsummaryrefslogtreecommitdiffstats
path: root/fs/xfs/xfs_inode.c
diff options
context:
space:
mode:
authorDave Chinner <dchinner@redhat.com>2013-08-12 06:49:46 -0400
committerBen Myers <bpm@sgi.com>2013-08-12 17:54:09 -0400
commitf6bba2017afb3bda8f2a27d6ba9d7023e8ff7f37 (patch)
tree85ea4ba3ab95623496b0beddeb76cc46e3957252 /fs/xfs/xfs_inode.c
parentc24b5dfadc4a4f7a13af373067871479c74455e6 (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/xfs_inode.c')
-rw-r--r--fs/xfs/xfs_inode.c308
1 files changed, 307 insertions, 1 deletions
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 */
2274STATIC void
2275xfs_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 */
2323int
2324xfs_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
2271STATIC int 2578STATIC int
2272xfs_iflush_cluster( 2579xfs_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
2528STATIC int 2834STATIC int
2529xfs_iflush_int( 2835xfs_iflush_int(
2530 struct xfs_inode *ip, 2836 struct xfs_inode *ip,