[XFS] Fix xfs_bulkstat_one size checks & error handling
[safe/jmp/linux-2.6] / fs / xfs / linux-2.6 / xfs_ioctl32.c
1 /*
2  * Copyright (c) 2004-2005 Silicon Graphics, 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 #include <linux/compat.h>
19 #include <linux/init.h>
20 #include <linux/ioctl.h>
21 #include <linux/syscalls.h>
22 #include <linux/types.h>
23 #include <linux/fs.h>
24 #include <asm/uaccess.h>
25 #include "xfs.h"
26 #include "xfs_fs.h"
27 #include "xfs_bit.h"
28 #include "xfs_log.h"
29 #include "xfs_inum.h"
30 #include "xfs_trans.h"
31 #include "xfs_sb.h"
32 #include "xfs_ag.h"
33 #include "xfs_dir2.h"
34 #include "xfs_dmapi.h"
35 #include "xfs_mount.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_vnode.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
42 #include "xfs_itable.h"
43 #include "xfs_error.h"
44 #include "xfs_dfrag.h"
45 #include "xfs_vnodeops.h"
46 #include "xfs_fsops.h"
47 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_ioctl.h"
50 #include "xfs_ioctl32.h"
51
52 #define  _NATIVE_IOC(cmd, type) \
53           _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
54
55 #ifdef BROKEN_X86_ALIGNMENT
56 STATIC int
57 xfs_compat_flock64_copyin(
58         xfs_flock64_t           *bf,
59         compat_xfs_flock64_t    __user *arg32)
60 {
61         if (get_user(bf->l_type,        &arg32->l_type) ||
62             get_user(bf->l_whence,      &arg32->l_whence) ||
63             get_user(bf->l_start,       &arg32->l_start) ||
64             get_user(bf->l_len,         &arg32->l_len) ||
65             get_user(bf->l_sysid,       &arg32->l_sysid) ||
66             get_user(bf->l_pid,         &arg32->l_pid) ||
67             copy_from_user(bf->l_pad,   &arg32->l_pad,  4*sizeof(u32)))
68                 return -XFS_ERROR(EFAULT);
69         return 0;
70 }
71
72 STATIC int
73 xfs_compat_ioc_fsgeometry_v1(
74         struct xfs_mount          *mp,
75         compat_xfs_fsop_geom_v1_t __user *arg32)
76 {
77         xfs_fsop_geom_t           fsgeo;
78         int                       error;
79
80         error = xfs_fs_geometry(mp, &fsgeo, 3);
81         if (error)
82                 return -error;
83         /* The 32-bit variant simply has some padding at the end */
84         if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
85                 return -XFS_ERROR(EFAULT);
86         return 0;
87 }
88
89 STATIC int
90 xfs_compat_growfs_data_copyin(
91         struct xfs_growfs_data   *in,
92         compat_xfs_growfs_data_t __user *arg32)
93 {
94         if (get_user(in->newblocks, &arg32->newblocks) ||
95             get_user(in->imaxpct,   &arg32->imaxpct))
96                 return -XFS_ERROR(EFAULT);
97         return 0;
98 }
99
100 STATIC int
101 xfs_compat_growfs_rt_copyin(
102         struct xfs_growfs_rt     *in,
103         compat_xfs_growfs_rt_t  __user *arg32)
104 {
105         if (get_user(in->newblocks, &arg32->newblocks) ||
106             get_user(in->extsize,   &arg32->extsize))
107                 return -XFS_ERROR(EFAULT);
108         return 0;
109 }
110
111 STATIC int
112 xfs_inumbers_fmt_compat(
113         void                    __user *ubuffer,
114         const xfs_inogrp_t      *buffer,
115         long                    count,
116         long                    *written)
117 {
118         compat_xfs_inogrp_t     __user *p32 = ubuffer;
119         long                    i;
120
121         for (i = 0; i < count; i++) {
122                 if (put_user(buffer[i].xi_startino,   &p32[i].xi_startino) ||
123                     put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
124                     put_user(buffer[i].xi_allocmask,  &p32[i].xi_allocmask))
125                         return -XFS_ERROR(EFAULT);
126         }
127         *written = count * sizeof(*p32);
128         return 0;
129 }
130
131 #else
132 #define xfs_inumbers_fmt_compat xfs_inumbers_fmt
133 #endif
134
135 STATIC int
136 xfs_ioctl32_bstime_copyin(
137         xfs_bstime_t            *bstime,
138         compat_xfs_bstime_t     __user *bstime32)
139 {
140         compat_time_t           sec32;  /* tv_sec differs on 64 vs. 32 */
141
142         if (get_user(sec32,             &bstime32->tv_sec)      ||
143             get_user(bstime->tv_nsec,   &bstime32->tv_nsec))
144                 return -XFS_ERROR(EFAULT);
145         bstime->tv_sec = sec32;
146         return 0;
147 }
148
149 /* xfs_bstat_t has differing alignment on intel, & bstime_t sizes everywhere */
150 STATIC int
151 xfs_ioctl32_bstat_copyin(
152         xfs_bstat_t             *bstat,
153         compat_xfs_bstat_t      __user *bstat32)
154 {
155         if (get_user(bstat->bs_ino,     &bstat32->bs_ino)       ||
156             get_user(bstat->bs_mode,    &bstat32->bs_mode)      ||
157             get_user(bstat->bs_nlink,   &bstat32->bs_nlink)     ||
158             get_user(bstat->bs_uid,     &bstat32->bs_uid)       ||
159             get_user(bstat->bs_gid,     &bstat32->bs_gid)       ||
160             get_user(bstat->bs_rdev,    &bstat32->bs_rdev)      ||
161             get_user(bstat->bs_blksize, &bstat32->bs_blksize)   ||
162             get_user(bstat->bs_size,    &bstat32->bs_size)      ||
163             xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
164             xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
165             xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
166             get_user(bstat->bs_blocks,  &bstat32->bs_size)      ||
167             get_user(bstat->bs_xflags,  &bstat32->bs_size)      ||
168             get_user(bstat->bs_extsize, &bstat32->bs_extsize)   ||
169             get_user(bstat->bs_extents, &bstat32->bs_extents)   ||
170             get_user(bstat->bs_gen,     &bstat32->bs_gen)       ||
171             get_user(bstat->bs_projid,  &bstat32->bs_projid)    ||
172             get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) ||
173             get_user(bstat->bs_dmstate, &bstat32->bs_dmstate)   ||
174             get_user(bstat->bs_aextents, &bstat32->bs_aextents))
175                 return -XFS_ERROR(EFAULT);
176         return 0;
177 }
178
179 /* XFS_IOC_FSBULKSTAT and friends */
180
181 STATIC int
182 xfs_bstime_store_compat(
183         compat_xfs_bstime_t     __user *p32,
184         const xfs_bstime_t      *p)
185 {
186         __s32                   sec32;
187
188         sec32 = p->tv_sec;
189         if (put_user(sec32, &p32->tv_sec) ||
190             put_user(p->tv_nsec, &p32->tv_nsec))
191                 return -XFS_ERROR(EFAULT);
192         return 0;
193 }
194
195 /* Return 0 on success or positive error (to xfs_bulkstat()) */
196 STATIC int
197 xfs_bulkstat_one_fmt_compat(
198         void                    __user *ubuffer,
199         int                     ubsize,
200         int                     *ubused,
201         const xfs_bstat_t       *buffer)
202 {
203         compat_xfs_bstat_t      __user *p32 = ubuffer;
204
205         if (ubsize < sizeof(*p32))
206                 return XFS_ERROR(ENOMEM);
207
208         if (put_user(buffer->bs_ino,      &p32->bs_ino)         ||
209             put_user(buffer->bs_mode,     &p32->bs_mode)        ||
210             put_user(buffer->bs_nlink,    &p32->bs_nlink)       ||
211             put_user(buffer->bs_uid,      &p32->bs_uid)         ||
212             put_user(buffer->bs_gid,      &p32->bs_gid)         ||
213             put_user(buffer->bs_rdev,     &p32->bs_rdev)        ||
214             put_user(buffer->bs_blksize,  &p32->bs_blksize)     ||
215             put_user(buffer->bs_size,     &p32->bs_size)        ||
216             xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
217             xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
218             xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
219             put_user(buffer->bs_blocks,   &p32->bs_blocks)      ||
220             put_user(buffer->bs_xflags,   &p32->bs_xflags)      ||
221             put_user(buffer->bs_extsize,  &p32->bs_extsize)     ||
222             put_user(buffer->bs_extents,  &p32->bs_extents)     ||
223             put_user(buffer->bs_gen,      &p32->bs_gen)         ||
224             put_user(buffer->bs_projid,   &p32->bs_projid)      ||
225             put_user(buffer->bs_dmevmask, &p32->bs_dmevmask)    ||
226             put_user(buffer->bs_dmstate,  &p32->bs_dmstate)     ||
227             put_user(buffer->bs_aextents, &p32->bs_aextents))
228                 return XFS_ERROR(EFAULT);
229         if (ubused)
230                 *ubused = sizeof(*p32);
231         return 0;
232 }
233
234 STATIC int
235 xfs_bulkstat_one_compat(
236         xfs_mount_t     *mp,            /* mount point for filesystem */
237         xfs_ino_t       ino,            /* inode number to get data for */
238         void            __user *buffer, /* buffer to place output in */
239         int             ubsize,         /* size of buffer */
240         void            *private_data,  /* my private data */
241         xfs_daddr_t     bno,            /* starting bno of inode cluster */
242         int             *ubused,        /* bytes used by me */
243         void            *dibuff,        /* on-disk inode buffer */
244         int             *stat)          /* BULKSTAT_RV_... */
245 {
246         return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
247                                     xfs_bulkstat_one_fmt_compat, bno,
248                                     ubused, dibuff, stat);
249 }
250
251 /* copied from xfs_ioctl.c */
252 STATIC int
253 xfs_compat_ioc_bulkstat(
254         xfs_mount_t               *mp,
255         unsigned int              cmd,
256         compat_xfs_fsop_bulkreq_t __user *p32)
257 {
258         u32                     addr;
259         xfs_fsop_bulkreq_t      bulkreq;
260         int                     count;  /* # of records returned */
261         xfs_ino_t               inlast; /* last inode number */
262         int                     done;
263         int                     error;
264
265         /* done = 1 if there are more stats to get and if bulkstat */
266         /* should be called again (unused here, but used in dmapi) */
267
268         if (!capable(CAP_SYS_ADMIN))
269                 return -XFS_ERROR(EPERM);
270
271         if (XFS_FORCED_SHUTDOWN(mp))
272                 return -XFS_ERROR(EIO);
273
274         if (get_user(addr, &p32->lastip))
275                 return -XFS_ERROR(EFAULT);
276         bulkreq.lastip = compat_ptr(addr);
277         if (get_user(bulkreq.icount, &p32->icount) ||
278             get_user(addr, &p32->ubuffer))
279                 return -XFS_ERROR(EFAULT);
280         bulkreq.ubuffer = compat_ptr(addr);
281         if (get_user(addr, &p32->ocount))
282                 return -XFS_ERROR(EFAULT);
283         bulkreq.ocount = compat_ptr(addr);
284
285         if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
286                 return -XFS_ERROR(EFAULT);
287
288         if ((count = bulkreq.icount) <= 0)
289                 return -XFS_ERROR(EINVAL);
290
291         if (bulkreq.ubuffer == NULL)
292                 return -XFS_ERROR(EINVAL);
293
294         if (cmd == XFS_IOC_FSINUMBERS_32)
295                 error = xfs_inumbers(mp, &inlast, &count,
296                                 bulkreq.ubuffer, xfs_inumbers_fmt_compat);
297         else {
298                 error = xfs_bulkstat(mp, &inlast, &count,
299                         xfs_bulkstat_one_compat, NULL,
300                         sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
301                         BULKSTAT_FG_QUICK, &done);
302         }
303         if (error)
304                 return -error;
305
306         if (bulkreq.ocount != NULL) {
307                 if (copy_to_user(bulkreq.lastip, &inlast,
308                                                 sizeof(xfs_ino_t)))
309                         return -XFS_ERROR(EFAULT);
310
311                 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
312                         return -XFS_ERROR(EFAULT);
313         }
314
315         return 0;
316 }
317
318 STATIC int
319 xfs_compat_handlereq_copyin(
320         xfs_fsop_handlereq_t            *hreq,
321         compat_xfs_fsop_handlereq_t     __user *arg32)
322 {
323         compat_xfs_fsop_handlereq_t     hreq32;
324
325         if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
326                 return -XFS_ERROR(EFAULT);
327
328         hreq->fd = hreq32.fd;
329         hreq->path = compat_ptr(hreq32.path);
330         hreq->oflags = hreq32.oflags;
331         hreq->ihandle = compat_ptr(hreq32.ihandle);
332         hreq->ihandlen = hreq32.ihandlen;
333         hreq->ohandle = compat_ptr(hreq32.ohandle);
334         hreq->ohandlen = compat_ptr(hreq32.ohandlen);
335
336         return 0;
337 }
338
339 STATIC long
340 xfs_compat_ioctl(
341         xfs_inode_t     *ip,
342         struct file     *filp,
343         int             ioflags,
344         unsigned        cmd,
345         void            __user *arg)
346 {
347         struct inode    *inode = filp->f_path.dentry->d_inode;
348         xfs_mount_t     *mp = ip->i_mount;
349         int             error;
350
351         xfs_itrace_entry(XFS_I(inode));
352         switch (cmd) {
353         case XFS_IOC_DIOINFO:
354         case XFS_IOC_FSGEOMETRY:
355         case XFS_IOC_FSGETXATTR:
356         case XFS_IOC_FSSETXATTR:
357         case XFS_IOC_FSGETXATTRA:
358         case XFS_IOC_FSSETDM:
359         case XFS_IOC_GETBMAP:
360         case XFS_IOC_GETBMAPA:
361         case XFS_IOC_GETBMAPX:
362 /* not handled
363         case XFS_IOC_FSSETDM_BY_HANDLE:
364         case XFS_IOC_ATTRLIST_BY_HANDLE:
365         case XFS_IOC_ATTRMULTI_BY_HANDLE:
366 */
367         case XFS_IOC_FSCOUNTS:
368         case XFS_IOC_SET_RESBLKS:
369         case XFS_IOC_GET_RESBLKS:
370         case XFS_IOC_FSGROWFSDATA:
371         case XFS_IOC_FSGROWFSLOG:
372         case XFS_IOC_FSGROWFSRT:
373         case XFS_IOC_FREEZE:
374         case XFS_IOC_THAW:
375         case XFS_IOC_GOINGDOWN:
376         case XFS_IOC_ERROR_INJECTION:
377         case XFS_IOC_ERROR_CLEARALL:
378                 break;
379
380         case XFS_IOC_GETXFLAGS_32:
381         case XFS_IOC_SETXFLAGS_32:
382         case XFS_IOC_GETVERSION_32:
383                 cmd = _NATIVE_IOC(cmd, long);
384                 break;
385         case XFS_IOC_SWAPEXT: {
386                 struct xfs_swapext        sxp;
387                 struct compat_xfs_swapext __user *sxu = arg;
388
389                 /* Bulk copy in up to the sx_stat field, then grab bstat */
390                 if (copy_from_user(&sxp, sxu,
391                                    offsetof(xfs_swapext_t, sx_stat)) ||
392                     xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
393                         return -XFS_ERROR(EFAULT);
394                 error = xfs_swapext(&sxp);
395                 return -error;
396         }
397 #ifdef BROKEN_X86_ALIGNMENT
398         /* xfs_flock_t has wrong u32 vs u64 alignment */
399         case XFS_IOC_ALLOCSP_32:
400         case XFS_IOC_FREESP_32:
401         case XFS_IOC_ALLOCSP64_32:
402         case XFS_IOC_FREESP64_32:
403         case XFS_IOC_RESVSP_32:
404         case XFS_IOC_UNRESVSP_32:
405         case XFS_IOC_RESVSP64_32:
406         case XFS_IOC_UNRESVSP64_32: {
407                 struct xfs_flock64      bf;
408
409                 if (xfs_compat_flock64_copyin(&bf, arg))
410                         return -XFS_ERROR(EFAULT);
411                 cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
412                 return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
413         }
414         case XFS_IOC_FSGEOMETRY_V1_32:
415                 return xfs_compat_ioc_fsgeometry_v1(mp, arg);
416         case XFS_IOC_FSGROWFSDATA_32: {
417                 struct xfs_growfs_data  in;
418
419                 if (xfs_compat_growfs_data_copyin(&in, arg))
420                         return -XFS_ERROR(EFAULT);
421                 error = xfs_growfs_data(mp, &in);
422                 return -error;
423         }
424         case XFS_IOC_FSGROWFSRT_32: {
425                 struct xfs_growfs_rt    in;
426
427                 if (xfs_compat_growfs_rt_copyin(&in, arg))
428                         return -XFS_ERROR(EFAULT);
429                 error = xfs_growfs_rt(mp, &in);
430                 return -error;
431         }
432 #else /* These are handled fine if no alignment issues */
433         case XFS_IOC_ALLOCSP:
434         case XFS_IOC_FREESP:
435         case XFS_IOC_RESVSP:
436         case XFS_IOC_UNRESVSP:
437         case XFS_IOC_ALLOCSP64:
438         case XFS_IOC_FREESP64:
439         case XFS_IOC_RESVSP64:
440         case XFS_IOC_UNRESVSP64:
441         case XFS_IOC_FSGEOMETRY_V1:
442                 break;
443 #endif
444         case XFS_IOC_FSBULKSTAT_32:
445         case XFS_IOC_FSBULKSTAT_SINGLE_32:
446         case XFS_IOC_FSINUMBERS_32:
447                 return xfs_compat_ioc_bulkstat(mp, cmd, arg);
448         case XFS_IOC_FD_TO_HANDLE_32:
449         case XFS_IOC_PATH_TO_HANDLE_32:
450         case XFS_IOC_PATH_TO_FSHANDLE_32: {
451                 struct xfs_fsop_handlereq       hreq;
452
453                 if (xfs_compat_handlereq_copyin(&hreq, arg))
454                         return -XFS_ERROR(EFAULT);
455                 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
456                 return xfs_find_handle(cmd, &hreq);
457         }
458         case XFS_IOC_OPEN_BY_HANDLE_32: {
459                 struct xfs_fsop_handlereq       hreq;
460
461                 if (xfs_compat_handlereq_copyin(&hreq, arg))
462                         return -XFS_ERROR(EFAULT);
463                 return xfs_open_by_handle(mp, &hreq, filp, inode);
464         }
465         case XFS_IOC_READLINK_BY_HANDLE_32: {
466                 struct xfs_fsop_handlereq       hreq;
467
468                 if (xfs_compat_handlereq_copyin(&hreq, arg))
469                         return -XFS_ERROR(EFAULT);
470                 return xfs_readlink_by_handle(mp, &hreq, inode);
471         }
472         default:
473                 return -XFS_ERROR(ENOIOCTLCMD);
474         }
475
476         error = xfs_ioctl(ip, filp, ioflags, cmd, arg);
477         return error;
478 }
479
480 long
481 xfs_file_compat_ioctl(
482         struct file             *filp,
483         unsigned int            cmd,
484         unsigned long           p)
485 {
486         struct inode    *inode = filp->f_path.dentry->d_inode;
487
488         return xfs_compat_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
489 }
490
491 long
492 xfs_file_compat_invis_ioctl(
493         struct file             *filp,
494         unsigned int            cmd,
495         unsigned long           p)
496 {
497         struct inode    *inode = filp->f_path.dentry->d_inode;
498
499         return xfs_compat_ioctl(XFS_I(inode), filp, IO_INVIS, cmd,
500                                 (void __user *)p);
501 }