]>
Commit | Line | Data |
---|---|---|
57e7138a BF |
1 | /* |
2 | * Copyright (c) 2018 Red Hat, 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 | ||
19 | #include "command.h" | |
20 | #include "input.h" | |
21 | #include "init.h" | |
22 | #include "io.h" | |
23 | ||
24 | static cmdinfo_t swapext_cmd; | |
25 | ||
26 | static void | |
27 | swapext_help(void) | |
28 | { | |
29 | printf(_( | |
30 | "\n" | |
31 | " Swaps extents between the open file descriptor and the supplied filename.\n" | |
32 | "\n")); | |
33 | } | |
34 | ||
35 | static int | |
36 | xfs_bulkstat_single( | |
37 | int fd, | |
38 | xfs_ino_t *lastip, | |
39 | struct xfs_bstat *ubuffer) | |
40 | { | |
41 | struct xfs_fsop_bulkreq bulkreq; | |
42 | ||
43 | bulkreq.lastip = (__u64 *)lastip; | |
44 | bulkreq.icount = 1; | |
45 | bulkreq.ubuffer = ubuffer; | |
46 | bulkreq.ocount = NULL; | |
47 | return ioctl(fd, XFS_IOC_FSBULKSTAT_SINGLE, &bulkreq); | |
48 | } | |
49 | ||
50 | static int | |
51 | swapext_f( | |
52 | int argc, | |
53 | char **argv) | |
54 | { | |
55 | int fd; | |
56 | int error; | |
57 | struct xfs_swapext sx; | |
58 | struct stat stat; | |
59 | ||
60 | /* open the donor file */ | |
61 | fd = openfile(argv[1], NULL, 0, 0, NULL); | |
62 | if (fd < 0) | |
63 | return 0; | |
64 | ||
65 | /* | |
66 | * stat the target file to get the inode number and use the latter to | |
67 | * get the bulkstat info for the swapext cmd. | |
68 | */ | |
69 | error = fstat(file->fd, &stat); | |
70 | if (error) { | |
71 | perror("fstat"); | |
72 | goto out; | |
73 | } | |
74 | ||
75 | error = xfs_bulkstat_single(file->fd, &stat.st_ino, &sx.sx_stat); | |
76 | if (error) { | |
77 | perror("bulkstat"); | |
78 | goto out; | |
79 | } | |
80 | sx.sx_version = XFS_SX_VERSION; | |
81 | sx.sx_fdtarget = file->fd; | |
82 | sx.sx_fdtmp = fd; | |
83 | sx.sx_offset = 0; | |
84 | sx.sx_length = stat.st_size; | |
85 | error = ioctl(file->fd, XFS_IOC_SWAPEXT, &sx); | |
86 | if (error) | |
87 | perror("swapext"); | |
88 | ||
89 | out: | |
90 | close(fd); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | void | |
95 | swapext_init(void) | |
96 | { | |
97 | swapext_cmd.name = "swapext"; | |
98 | swapext_cmd.cfunc = swapext_f; | |
99 | swapext_cmd.argmin = 1; | |
100 | swapext_cmd.argmax = 1; | |
101 | swapext_cmd.flags = CMD_NOMAP_OK; | |
102 | swapext_cmd.args = _("<donorfile>"); | |
103 | swapext_cmd.oneline = _("Swap extents between files."); | |
104 | swapext_cmd.help = swapext_help; | |
105 | ||
106 | add_command(&swapext_cmd); | |
107 | } |