]> granicus.if.org Git - strace/blob - file_ioctl.c
Mpersify RTC_* ioctl parser
[strace] / file_ioctl.c
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"
29 #include <linux/ioctl.h>
30 #include <linux/fs.h>
31
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
37
38 #ifndef FICLONE
39 # define FICLONE         _IOW(0x94, 9, int)
40 #endif
41
42 #ifndef FICLONERANGE
43 # define FICLONERANGE    _IOW(0x94, 13, struct file_clone_range)
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
53 # define FIDEDUPERANGE   _IOWR(0x94, 54, struct file_dedupe_range)
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
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;
83
84         if (entering(tcp)) {
85                 tprints("{dest_fd=");
86                 printfd(tcp, info->dest_fd);
87                 tprintf(", dest_offset=%" PRIu64 "}",
88                         (uint64_t) info->dest_offset);
89         } else {
90                 tprintf("{bytes_deduped=%" PRIu64 ", status=%d}",
91                         (uint64_t) info->bytes_deduped, info->status);
92         }
93
94         return true;
95 }
96
97 #ifdef HAVE_LINUX_FIEMAP_H
98 static bool
99 print_fiemap_extent(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
100 {
101         const struct fiemap_extent *fe = elem_buf;
102
103         tprintf("{fe_logical=%" PRI__u64
104                 ", fe_physical=%" PRI__u64
105                 ", fe_length=%" PRI__u64 ", ",
106                 fe->fe_logical, fe->fe_physical, fe->fe_length);
107
108         printflags64(fiemap_extent_flags, fe->fe_flags,
109                      "FIEMAP_EXTENT_???");
110         tprints("}");
111
112         return true;
113 }
114 #endif /* HAVE_LINUX_FIEMAP_H */
115
116 int
117 file_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
118 {
119         switch (code) {
120         case FICLONE:   /* W */
121                 tprintf(", %d", (int) arg);
122                 break;
123
124         case FICLONERANGE: { /* W */
125                 struct file_clone_range args;
126
127                 tprints(", ");
128                 if (umove_or_printaddr(tcp, arg, &args))
129                         break;
130
131                 tprints("{src_fd=");
132                 printfd(tcp, args.src_fd);
133                 tprintf(", src_offset=%" PRIu64
134                         ", src_length=%" PRIu64
135                         ", dest_offset=%" PRIu64 "}",
136                         (uint64_t) args.src_offset,
137                         (uint64_t) args.src_length,
138                         (uint64_t) args.dest_offset);
139                 break;
140         }
141
142         case FIDEDUPERANGE: { /* RW */
143                 struct file_dedupe_range args;
144
145                 if (entering(tcp))
146                         tprints(", ");
147                 else if (syserror(tcp))
148                         break;
149                 else
150                         tprints(" => ");
151
152                 if (umove_or_printaddr(tcp, arg, &args))
153                         break;
154
155                 tprints("{");
156                 if (entering(tcp)) {
157                         tprintf("src_offset=%" PRIu64
158                                 ", src_length=%" PRIu64
159                                 ", dest_count=%hu, ",
160                                 (uint64_t) args.src_offset,
161                                 (uint64_t) args.src_length,
162                                 (uint16_t) args.dest_count);
163                 }
164
165                 bool rc = false;
166                 tprints("info=");
167                 if (abbrev(tcp)) {
168                         tprints("...");
169                 } else {
170                         struct file_dedupe_range_info info;
171                         rc = print_array(tcp,
172                                          arg + offsetof(typeof(args), info),
173                                          args.dest_count,
174                                          &info, sizeof(info),
175                                          umoven_or_printaddr,
176                                          print_file_dedupe_range_info, 0);
177                 }
178
179                 tprints("}");
180                 if (!rc || exiting(tcp))
181                         break;
182
183                 return 0;
184         }
185
186 #ifdef HAVE_LINUX_FIEMAP_H
187         case FS_IOC_FIEMAP: {
188                 struct fiemap args;
189
190                 if (entering(tcp))
191                         tprints(", ");
192                 else if (syserror(tcp))
193                         break;
194                 else
195                         tprints(" => ");
196
197                 if (umove_or_printaddr(tcp, arg, &args))
198                         break;
199
200                 if (entering(tcp)) {
201                         tprintf("{fm_start=%" PRI__u64 ", "
202                                 "fm_length=%" PRI__u64 ", "
203                                 "fm_flags=",
204                                 args.fm_start, args.fm_length);
205                         printflags64(fiemap_flags, args.fm_flags,
206                                      "FIEMAP_FLAG_???");
207                         tprintf(", fm_extent_count=%u}", args.fm_extent_count);
208                         return 0;
209                 }
210
211                 tprints("{fm_flags=");
212                 printflags64(fiemap_flags, args.fm_flags,
213                              "FIEMAP_FLAG_???");
214                 tprintf(", fm_mapped_extents=%u",
215                         args.fm_mapped_extents);
216                 tprints(", fm_extents=");
217                 if (abbrev(tcp)) {
218                         tprints("...");
219                 } else {
220                         struct fiemap_extent fe;
221                         print_array(tcp,
222                                     arg + offsetof(typeof(args), fm_extents),
223                                     args.fm_mapped_extents, &fe, sizeof(fe),
224                                     umoven_or_printaddr,
225                                     print_fiemap_extent, 0);
226                 }
227                 tprints("}");
228
229                 break;
230         }
231 #endif /* HAVE_LINUX_FIEMAP_H */
232
233         default:
234                 return RVAL_DECODED;
235         };
236
237         return RVAL_DECODED | 1;
238 }