]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/swapext.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / io / swapext.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018 Red Hat, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include "init.h"
10 #include "io.h"
11
12 static cmdinfo_t swapext_cmd;
13
14 static void
15 swapext_help(void)
16 {
17 printf(_(
18 "\n"
19 " Swaps extents between the open file descriptor and the supplied filename.\n"
20 "\n"));
21 }
22
23 static int
24 xfs_bulkstat_single(
25 int fd,
26 xfs_ino_t *lastip,
27 struct xfs_bstat *ubuffer)
28 {
29 struct xfs_fsop_bulkreq bulkreq;
30
31 bulkreq.lastip = (__u64 *)lastip;
32 bulkreq.icount = 1;
33 bulkreq.ubuffer = ubuffer;
34 bulkreq.ocount = NULL;
35 return ioctl(fd, XFS_IOC_FSBULKSTAT_SINGLE, &bulkreq);
36 }
37
38 static int
39 swapext_f(
40 int argc,
41 char **argv)
42 {
43 int fd;
44 int error;
45 struct xfs_swapext sx;
46 struct stat stat;
47
48 /* open the donor file */
49 fd = openfile(argv[1], NULL, 0, 0, NULL);
50 if (fd < 0)
51 return 0;
52
53 /*
54 * stat the target file to get the inode number and use the latter to
55 * get the bulkstat info for the swapext cmd.
56 */
57 error = fstat(file->fd, &stat);
58 if (error) {
59 perror("fstat");
60 goto out;
61 }
62
63 error = xfs_bulkstat_single(file->fd, &stat.st_ino, &sx.sx_stat);
64 if (error) {
65 perror("bulkstat");
66 goto out;
67 }
68 sx.sx_version = XFS_SX_VERSION;
69 sx.sx_fdtarget = file->fd;
70 sx.sx_fdtmp = fd;
71 sx.sx_offset = 0;
72 sx.sx_length = stat.st_size;
73 error = ioctl(file->fd, XFS_IOC_SWAPEXT, &sx);
74 if (error)
75 perror("swapext");
76
77 out:
78 close(fd);
79 return 0;
80 }
81
82 void
83 swapext_init(void)
84 {
85 swapext_cmd.name = "swapext";
86 swapext_cmd.cfunc = swapext_f;
87 swapext_cmd.argmin = 1;
88 swapext_cmd.argmax = 1;
89 swapext_cmd.flags = CMD_NOMAP_OK;
90 swapext_cmd.args = _("<donorfile>");
91 swapext_cmd.oneline = _("Swap extents between files.");
92 swapext_cmd.help = swapext_help;
93
94 add_command(&swapext_cmd);
95 }