]> granicus.if.org Git - strace/blob - file_ioctl.c
rtnl_link: print pad field in the struct ifla_port_vsi decoder
[strace] / file_ioctl.c
1 /*
2  * Copyright (c) 2016 Jeff Mahoney <jeffm@suse.com>
3  * Copyright (c) 2016-2018 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: LGPL-2.1-or-later
7  */
8
9 #include "defs.h"
10 #include <linux/ioctl.h>
11 #include <linux/fs.h>
12
13 #ifdef HAVE_LINUX_FIEMAP_H
14 # include <linux/fiemap.h>
15 # include "xlat/fiemap_flags.h"
16 # include "xlat/fiemap_extent_flags.h"
17 #endif
18
19 #ifndef FICLONE
20 # define FICLONE        _IOW(0x94, 9, int)
21 #endif
22
23 #ifndef FICLONERANGE
24 # define FICLONERANGE   _IOW(0x94, 13, struct file_clone_range)
25 struct file_clone_range {
26         int64_t src_fd;
27         uint64_t src_offset;
28         uint64_t src_length;
29         uint64_t dest_offset;
30 };
31 #endif
32
33 #ifndef FIDEDUPERANGE
34 # define FIDEDUPERANGE  _IOWR(0x94, 54, struct file_dedupe_range)
35 struct file_dedupe_range_info {
36         int64_t dest_fd;        /* in - destination file */
37         uint64_t dest_offset;   /* in - start of extent in destination */
38         uint64_t bytes_deduped; /* out - total # of bytes we were able
39                                  * to dedupe from this file. */
40         /* status of this dedupe operation:
41          * < 0 for error
42          * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds
43          * == FILE_DEDUPE_RANGE_DIFFERS if data differs
44          */
45         int32_t status;         /* out - see above description */
46         uint32_t reserved;      /* must be zero */
47 };
48
49 struct file_dedupe_range {
50         uint64_t src_offset;    /* in - start of extent in source */
51         uint64_t src_length;    /* in - length of extent */
52         uint16_t dest_count;    /* in - total elements in info array */
53         uint16_t reserved1;     /* must be zero */
54         uint32_t reserved2;     /* must be zero */
55         struct file_dedupe_range_info info[0];
56 };
57 #endif
58
59 static bool
60 print_file_dedupe_range_info(struct tcb *tcp, void *elem_buf,
61                              size_t elem_size, void *data)
62 {
63         const struct file_dedupe_range_info *info = elem_buf;
64         unsigned int *count = data;
65
66         if (count) {
67                 if (*count == 0) {
68                         tprints("...");
69                         return false;
70                 }
71                 --*count;
72         }
73
74         if (entering(tcp)) {
75                 tprints("{dest_fd=");
76                 printfd(tcp, info->dest_fd);
77                 tprintf(", dest_offset=%" PRIu64 "}",
78                         (uint64_t) info->dest_offset);
79         } else {
80                 tprintf("{bytes_deduped=%" PRIu64 ", status=%d}",
81                         (uint64_t) info->bytes_deduped, info->status);
82         }
83
84         return true;
85 }
86
87 #ifdef HAVE_LINUX_FIEMAP_H
88 static bool
89 print_fiemap_extent(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
90 {
91         const struct fiemap_extent *fe = elem_buf;
92
93         tprintf("{fe_logical=%" PRI__u64
94                 ", fe_physical=%" PRI__u64
95                 ", fe_length=%" PRI__u64 ", ",
96                 fe->fe_logical, fe->fe_physical, fe->fe_length);
97
98         printflags64(fiemap_extent_flags, fe->fe_flags,
99                      "FIEMAP_EXTENT_???");
100         tprints("}");
101
102         return true;
103 }
104 #endif /* HAVE_LINUX_FIEMAP_H */
105
106 int
107 file_ioctl(struct tcb *const tcp, const unsigned int code,
108            const kernel_ulong_t arg)
109 {
110         switch (code) {
111         case FICLONE:   /* W */
112                 tprintf(", %d", (int) arg);
113                 break;
114
115         case FICLONERANGE: { /* W */
116                 struct file_clone_range args;
117
118                 tprints(", ");
119                 if (umove_or_printaddr(tcp, arg, &args))
120                         break;
121
122                 tprints("{src_fd=");
123                 printfd(tcp, args.src_fd);
124                 tprintf(", src_offset=%" PRIu64
125                         ", src_length=%" PRIu64
126                         ", dest_offset=%" PRIu64 "}",
127                         (uint64_t) args.src_offset,
128                         (uint64_t) args.src_length,
129                         (uint64_t) args.dest_offset);
130                 break;
131         }
132
133         case FIDEDUPERANGE: { /* RW */
134                 struct file_dedupe_range args;
135                 struct file_dedupe_range_info info;
136                 unsigned int *limit = NULL;
137                 unsigned int count = 2;
138                 bool rc;
139
140                 if (entering(tcp))
141                         tprints(", ");
142                 else if (syserror(tcp))
143                         break;
144                 else
145                         tprints(" => ");
146
147                 if (umove_or_printaddr(tcp, arg, &args))
148                         break;
149
150                 tprints("{");
151                 if (entering(tcp)) {
152                         tprintf("src_offset=%" PRIu64
153                                 ", src_length=%" PRIu64
154                                 ", dest_count=%hu, ",
155                                 (uint64_t) args.src_offset,
156                                 (uint64_t) args.src_length,
157                                 (uint16_t) args.dest_count);
158                 }
159
160                 tprints("info=");
161
162                 /* Limit how many elements we print in abbrev mode. */
163                 if (abbrev(tcp) && args.dest_count > count)
164                         limit = &count;
165
166                 rc = print_array(tcp, arg + offsetof(typeof(args), info),
167                                  args.dest_count, &info, sizeof(info),
168                                  tfetch_mem,
169                                  print_file_dedupe_range_info, limit);
170
171                 tprints("}");
172                 if (!rc || exiting(tcp))
173                         break;
174
175                 return 0;
176         }
177
178 #ifdef HAVE_LINUX_FIEMAP_H
179         case FS_IOC_FIEMAP: {
180                 struct fiemap args;
181
182                 if (entering(tcp))
183                         tprints(", ");
184                 else if (syserror(tcp))
185                         break;
186                 else
187                         tprints(" => ");
188
189                 if (umove_or_printaddr(tcp, arg, &args))
190                         break;
191
192                 if (entering(tcp)) {
193                         tprintf("{fm_start=%" PRI__u64 ", "
194                                 "fm_length=%" PRI__u64 ", "
195                                 "fm_flags=",
196                                 args.fm_start, args.fm_length);
197                         printflags64(fiemap_flags, args.fm_flags,
198                                      "FIEMAP_FLAG_???");
199                         tprintf(", fm_extent_count=%u}", args.fm_extent_count);
200                         return 0;
201                 }
202
203                 tprints("{fm_flags=");
204                 printflags64(fiemap_flags, args.fm_flags,
205                              "FIEMAP_FLAG_???");
206                 tprintf(", fm_mapped_extents=%u",
207                         args.fm_mapped_extents);
208                 if (abbrev(tcp)) {
209                         tprints(", ...");
210                 } else {
211                         struct fiemap_extent fe;
212                         tprints(", fm_extents=");
213                         print_array(tcp,
214                                     arg + offsetof(typeof(args), fm_extents),
215                                     args.fm_mapped_extents, &fe, sizeof(fe),
216                                     tfetch_mem,
217                                     print_fiemap_extent, 0);
218                 }
219                 tprints("}");
220
221                 break;
222         }
223 #endif /* HAVE_LINUX_FIEMAP_H */
224
225         default:
226                 return RVAL_DECODED;
227         };
228
229         return RVAL_IOCTL_DECODED;
230 }