Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Jeff Mahoney <jeffm@suse.com> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "defs.h" |
Dmitry V. Levin | 1d2201c | 2016-05-24 00:19:40 +0000 | [diff] [blame] | 29 | #include <linux/ioctl.h> |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 30 | #include <linux/fs.h> |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 31 | |
Dmitry V. Levin | c525307 | 2016-05-24 00:19:17 +0000 | [diff] [blame] | 32 | #ifdef HAVE_LINUX_FIEMAP_H |
| 33 | # include <linux/fiemap.h> |
| 34 | # include "xlat/fiemap_flags.h" |
| 35 | # include "xlat/fiemap_extent_flags.h" |
| 36 | #endif |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 37 | |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 38 | #ifndef FICLONE |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 39 | # define FICLONE _IOW(0x94, 9, int) |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #ifndef FICLONERANGE |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 43 | # define FICLONERANGE _IOW(0x94, 13, struct file_clone_range) |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 44 | struct file_clone_range { |
| 45 | int64_t src_fd; |
| 46 | uint64_t src_offset; |
| 47 | uint64_t src_length; |
| 48 | uint64_t dest_offset; |
| 49 | }; |
| 50 | #endif |
| 51 | |
| 52 | #ifndef FIDEDUPERANGE |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 53 | # define FIDEDUPERANGE _IOWR(0x94, 54, struct file_dedupe_range) |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 54 | struct file_dedupe_range_info { |
| 55 | int64_t dest_fd; /* in - destination file */ |
| 56 | uint64_t dest_offset; /* in - start of extent in destination */ |
| 57 | uint64_t bytes_deduped; /* out - total # of bytes we were able |
| 58 | * to dedupe from this file. */ |
| 59 | /* status of this dedupe operation: |
| 60 | * < 0 for error |
| 61 | * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds |
| 62 | * == FILE_DEDUPE_RANGE_DIFFERS if data differs |
| 63 | */ |
| 64 | int32_t status; /* out - see above description */ |
| 65 | uint32_t reserved; /* must be zero */ |
| 66 | }; |
| 67 | |
| 68 | struct file_dedupe_range { |
| 69 | uint64_t src_offset; /* in - start of extent in source */ |
| 70 | uint64_t src_length; /* in - length of extent */ |
| 71 | uint16_t dest_count; /* in - total elements in info array */ |
| 72 | uint16_t reserved1; /* must be zero */ |
| 73 | uint32_t reserved2; /* must be zero */ |
| 74 | struct file_dedupe_range_info info[0]; |
| 75 | }; |
| 76 | #endif |
| 77 | |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 78 | static bool |
| 79 | print_file_dedupe_range_info(struct tcb *tcp, void *elem_buf, |
| 80 | size_t elem_size, void *data) |
| 81 | { |
| 82 | const struct file_dedupe_range_info *info = elem_buf; |
Jeff Mahoney | eb36e38 | 2016-05-27 16:07:22 -0400 | [diff] [blame] | 83 | unsigned int *count = data; |
| 84 | |
| 85 | if (count) { |
| 86 | if (*count == 0) { |
| 87 | tprints("..."); |
| 88 | return false; |
| 89 | } |
| 90 | --*count; |
| 91 | } |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 92 | |
| 93 | if (entering(tcp)) { |
Dmitry V. Levin | eea8601 | 2016-05-25 07:59:13 +0000 | [diff] [blame] | 94 | tprints("{dest_fd="); |
| 95 | printfd(tcp, info->dest_fd); |
| 96 | tprintf(", dest_offset=%" PRIu64 "}", |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 97 | (uint64_t) info->dest_offset); |
| 98 | } else { |
| 99 | tprintf("{bytes_deduped=%" PRIu64 ", status=%d}", |
| 100 | (uint64_t) info->bytes_deduped, info->status); |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | #ifdef HAVE_LINUX_FIEMAP_H |
| 107 | static bool |
| 108 | print_fiemap_extent(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data) |
| 109 | { |
| 110 | const struct fiemap_extent *fe = elem_buf; |
| 111 | |
| 112 | tprintf("{fe_logical=%" PRI__u64 |
| 113 | ", fe_physical=%" PRI__u64 |
| 114 | ", fe_length=%" PRI__u64 ", ", |
| 115 | fe->fe_logical, fe->fe_physical, fe->fe_length); |
| 116 | |
| 117 | printflags64(fiemap_extent_flags, fe->fe_flags, |
| 118 | "FIEMAP_EXTENT_???"); |
| 119 | tprints("}"); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | #endif /* HAVE_LINUX_FIEMAP_H */ |
| 124 | |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 125 | int |
| 126 | file_ioctl(struct tcb *tcp, const unsigned int code, const long arg) |
| 127 | { |
| 128 | switch (code) { |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 129 | case FICLONE: /* W */ |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 130 | tprintf(", %d", (int) arg); |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case FICLONERANGE: { /* W */ |
| 134 | struct file_clone_range args; |
| 135 | |
| 136 | tprints(", "); |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 137 | if (umove_or_printaddr(tcp, arg, &args)) |
| 138 | break; |
| 139 | |
Dmitry V. Levin | eea8601 | 2016-05-25 07:59:13 +0000 | [diff] [blame] | 140 | tprints("{src_fd="); |
| 141 | printfd(tcp, args.src_fd); |
| 142 | tprintf(", src_offset=%" PRIu64 |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 143 | ", src_length=%" PRIu64 |
| 144 | ", dest_offset=%" PRIu64 "}", |
Dmitry V. Levin | 0b13d9c | 2016-05-24 00:19:53 +0000 | [diff] [blame] | 145 | (uint64_t) args.src_offset, |
| 146 | (uint64_t) args.src_length, |
| 147 | (uint64_t) args.dest_offset); |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | |
| 151 | case FIDEDUPERANGE: { /* RW */ |
| 152 | struct file_dedupe_range args; |
Jeff Mahoney | eb36e38 | 2016-05-27 16:07:22 -0400 | [diff] [blame] | 153 | struct file_dedupe_range_info info; |
| 154 | unsigned int *limit = NULL; |
| 155 | unsigned int count = 2; |
| 156 | bool rc; |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 157 | |
| 158 | if (entering(tcp)) |
| 159 | tprints(", "); |
| 160 | else if (syserror(tcp)) |
| 161 | break; |
| 162 | else |
| 163 | tprints(" => "); |
| 164 | |
| 165 | if (umove_or_printaddr(tcp, arg, &args)) |
| 166 | break; |
| 167 | |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 168 | tprints("{"); |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 169 | if (entering(tcp)) { |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 170 | tprintf("src_offset=%" PRIu64 |
| 171 | ", src_length=%" PRIu64 |
| 172 | ", dest_count=%hu, ", |
| 173 | (uint64_t) args.src_offset, |
| 174 | (uint64_t) args.src_length, |
| 175 | (uint16_t) args.dest_count); |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 176 | } |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 177 | |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 178 | tprints("info="); |
Jeff Mahoney | eb36e38 | 2016-05-27 16:07:22 -0400 | [diff] [blame] | 179 | |
| 180 | /* Limit how many elements we print in abbrev mode. */ |
| 181 | if (abbrev(tcp) && args.dest_count > count) |
| 182 | limit = &count; |
| 183 | |
| 184 | rc = print_array(tcp, arg + offsetof(typeof(args), info), |
| 185 | args.dest_count, &info, sizeof(info), |
| 186 | umoven_or_printaddr, |
| 187 | print_file_dedupe_range_info, limit); |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 188 | |
| 189 | tprints("}"); |
| 190 | if (!rc || exiting(tcp)) |
| 191 | break; |
| 192 | |
| 193 | return 0; |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Dmitry V. Levin | c525307 | 2016-05-24 00:19:17 +0000 | [diff] [blame] | 196 | #ifdef HAVE_LINUX_FIEMAP_H |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 197 | case FS_IOC_FIEMAP: { |
| 198 | struct fiemap args; |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 199 | |
| 200 | if (entering(tcp)) |
| 201 | tprints(", "); |
| 202 | else if (syserror(tcp)) |
| 203 | break; |
| 204 | else |
| 205 | tprints(" => "); |
| 206 | |
| 207 | if (umove_or_printaddr(tcp, arg, &args)) |
| 208 | break; |
| 209 | |
| 210 | if (entering(tcp)) { |
| 211 | tprintf("{fm_start=%" PRI__u64 ", " |
| 212 | "fm_length=%" PRI__u64 ", " |
| 213 | "fm_flags=", |
| 214 | args.fm_start, args.fm_length); |
| 215 | printflags64(fiemap_flags, args.fm_flags, |
| 216 | "FIEMAP_FLAG_???"); |
| 217 | tprintf(", fm_extent_count=%u}", args.fm_extent_count); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | tprints("{fm_flags="); |
| 222 | printflags64(fiemap_flags, args.fm_flags, |
| 223 | "FIEMAP_FLAG_???"); |
| 224 | tprintf(", fm_mapped_extents=%u", |
| 225 | args.fm_mapped_extents); |
| 226 | tprints(", fm_extents="); |
| 227 | if (abbrev(tcp)) { |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 228 | tprints("..."); |
| 229 | } else { |
| 230 | struct fiemap_extent fe; |
| 231 | print_array(tcp, |
| 232 | arg + offsetof(typeof(args), fm_extents), |
| 233 | args.fm_mapped_extents, &fe, sizeof(fe), |
| 234 | umoven_or_printaddr, |
| 235 | print_fiemap_extent, 0); |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 236 | } |
Dmitry V. Levin | d2bd683 | 2016-05-24 00:20:07 +0000 | [diff] [blame] | 237 | tprints("}"); |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 238 | |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 239 | break; |
| 240 | } |
Dmitry V. Levin | c525307 | 2016-05-24 00:19:17 +0000 | [diff] [blame] | 241 | #endif /* HAVE_LINUX_FIEMAP_H */ |
Jeff Mahoney | 398fe04 | 2016-05-18 18:09:40 -0400 | [diff] [blame] | 242 | |
Jeff Mahoney | 8cc6962 | 2016-05-18 18:09:39 -0400 | [diff] [blame] | 243 | default: |
| 244 | return RVAL_DECODED; |
| 245 | }; |
| 246 | |
| 247 | return RVAL_DECODED | 1; |
| 248 | } |