Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[sfrench/cifs-2.6.git] / 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_vfs.h"
40 #include "xfs_vnode.h"
41 #include "xfs_dinode.h"
42 #include "xfs_inode.h"
43 #include "xfs_itable.h"
44 #include "xfs_error.h"
45 #include "xfs_dfrag.h"
46 #include "xfs_vnodeops.h"
47 #include "xfs_ioctl32.h"
48
49 #define  _NATIVE_IOC(cmd, type) \
50           _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
51
52 #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
53 #define BROKEN_X86_ALIGNMENT
54 #define _PACKED __attribute__((packed))
55 /* on ia32 l_start is on a 32-bit boundary */
56 typedef struct xfs_flock64_32 {
57         __s16           l_type;
58         __s16           l_whence;
59         __s64           l_start __attribute__((packed));
60                         /* len == 0 means until end of file */
61         __s64           l_len __attribute__((packed));
62         __s32           l_sysid;
63         __u32           l_pid;
64         __s32           l_pad[4];       /* reserve area */
65 } xfs_flock64_32_t;
66
67 #define XFS_IOC_ALLOCSP_32      _IOW ('X', 10, struct xfs_flock64_32)
68 #define XFS_IOC_FREESP_32       _IOW ('X', 11, struct xfs_flock64_32)
69 #define XFS_IOC_ALLOCSP64_32    _IOW ('X', 36, struct xfs_flock64_32)
70 #define XFS_IOC_FREESP64_32     _IOW ('X', 37, struct xfs_flock64_32)
71 #define XFS_IOC_RESVSP_32       _IOW ('X', 40, struct xfs_flock64_32)
72 #define XFS_IOC_UNRESVSP_32     _IOW ('X', 41, struct xfs_flock64_32)
73 #define XFS_IOC_RESVSP64_32     _IOW ('X', 42, struct xfs_flock64_32)
74 #define XFS_IOC_UNRESVSP64_32   _IOW ('X', 43, struct xfs_flock64_32)
75
76 /* just account for different alignment */
77 STATIC unsigned long
78 xfs_ioctl32_flock(
79         unsigned long           arg)
80 {
81         xfs_flock64_32_t        __user *p32 = (void __user *)arg;
82         xfs_flock64_t           __user *p = compat_alloc_user_space(sizeof(*p));
83
84         if (copy_in_user(&p->l_type,    &p32->l_type,   sizeof(s16)) ||
85             copy_in_user(&p->l_whence,  &p32->l_whence, sizeof(s16)) ||
86             copy_in_user(&p->l_start,   &p32->l_start,  sizeof(s64)) ||
87             copy_in_user(&p->l_len,     &p32->l_len,    sizeof(s64)) ||
88             copy_in_user(&p->l_sysid,   &p32->l_sysid,  sizeof(s32)) ||
89             copy_in_user(&p->l_pid,     &p32->l_pid,    sizeof(u32)) ||
90             copy_in_user(&p->l_pad,     &p32->l_pad,    4*sizeof(u32)))
91                 return -EFAULT;
92
93         return (unsigned long)p;
94 }
95
96 typedef struct compat_xfs_fsop_geom_v1 {
97         __u32           blocksize;      /* filesystem (data) block size */
98         __u32           rtextsize;      /* realtime extent size         */
99         __u32           agblocks;       /* fsblocks in an AG            */
100         __u32           agcount;        /* number of allocation groups  */
101         __u32           logblocks;      /* fsblocks in the log          */
102         __u32           sectsize;       /* (data) sector size, bytes    */
103         __u32           inodesize;      /* inode size in bytes          */
104         __u32           imaxpct;        /* max allowed inode space(%)   */
105         __u64           datablocks;     /* fsblocks in data subvolume   */
106         __u64           rtblocks;       /* fsblocks in realtime subvol  */
107         __u64           rtextents;      /* rt extents in realtime subvol*/
108         __u64           logstart;       /* starting fsblock of the log  */
109         unsigned char   uuid[16];       /* unique id of the filesystem  */
110         __u32           sunit;          /* stripe unit, fsblocks        */
111         __u32           swidth;         /* stripe width, fsblocks       */
112         __s32           version;        /* structure version            */
113         __u32           flags;          /* superblock version flags     */
114         __u32           logsectsize;    /* log sector size, bytes       */
115         __u32           rtsectsize;     /* realtime sector size, bytes  */
116         __u32           dirblocksize;   /* directory block size, bytes  */
117 } __attribute__((packed)) compat_xfs_fsop_geom_v1_t;
118
119 #define XFS_IOC_FSGEOMETRY_V1_32  \
120         _IOR ('X', 100, struct compat_xfs_fsop_geom_v1)
121
122 STATIC unsigned long xfs_ioctl32_geom_v1(unsigned long arg)
123 {
124         compat_xfs_fsop_geom_v1_t __user *p32 = (void __user *)arg;
125         xfs_fsop_geom_v1_t __user *p = compat_alloc_user_space(sizeof(*p));
126
127         if (copy_in_user(p, p32, sizeof(*p32)))
128                 return -EFAULT;
129         return (unsigned long)p;
130 }
131
132 typedef struct compat_xfs_inogrp {
133         __u64           xi_startino;    /* starting inode number        */
134         __s32           xi_alloccount;  /* # bits set in allocmask      */
135         __u64           xi_allocmask;   /* mask of allocated inodes     */
136 } __attribute__((packed)) compat_xfs_inogrp_t;
137
138 STATIC int xfs_inumbers_fmt_compat(
139         void __user *ubuffer,
140         const xfs_inogrp_t *buffer,
141         long count,
142         long *written)
143 {
144         compat_xfs_inogrp_t __user *p32 = ubuffer;
145         long i;
146
147         for (i = 0; i < count; i++) {
148                 if (put_user(buffer[i].xi_startino,   &p32[i].xi_startino) ||
149                     put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
150                     put_user(buffer[i].xi_allocmask,  &p32[i].xi_allocmask))
151                         return -EFAULT;
152         }
153         *written = count * sizeof(*p32);
154         return 0;
155 }
156
157 #else
158
159 #define xfs_inumbers_fmt_compat xfs_inumbers_fmt
160 #define _PACKED
161
162 #endif
163
164 /* XFS_IOC_FSBULKSTAT and friends */
165
166 typedef struct compat_xfs_bstime {
167         __s32           tv_sec;         /* seconds              */
168         __s32           tv_nsec;        /* and nanoseconds      */
169 } compat_xfs_bstime_t;
170
171 STATIC int xfs_bstime_store_compat(
172         compat_xfs_bstime_t __user *p32,
173         const xfs_bstime_t *p)
174 {
175         __s32 sec32;
176
177         sec32 = p->tv_sec;
178         if (put_user(sec32, &p32->tv_sec) ||
179             put_user(p->tv_nsec, &p32->tv_nsec))
180                 return -EFAULT;
181         return 0;
182 }
183
184 typedef struct compat_xfs_bstat {
185         __u64           bs_ino;         /* inode number                 */
186         __u16           bs_mode;        /* type and mode                */
187         __u16           bs_nlink;       /* number of links              */
188         __u32           bs_uid;         /* user id                      */
189         __u32           bs_gid;         /* group id                     */
190         __u32           bs_rdev;        /* device value                 */
191         __s32           bs_blksize;     /* block size                   */
192         __s64           bs_size;        /* file size                    */
193         compat_xfs_bstime_t bs_atime;   /* access time                  */
194         compat_xfs_bstime_t bs_mtime;   /* modify time                  */
195         compat_xfs_bstime_t bs_ctime;   /* inode change time            */
196         int64_t         bs_blocks;      /* number of blocks             */
197         __u32           bs_xflags;      /* extended flags               */
198         __s32           bs_extsize;     /* extent size                  */
199         __s32           bs_extents;     /* number of extents            */
200         __u32           bs_gen;         /* generation count             */
201         __u16           bs_projid;      /* project id                   */
202         unsigned char   bs_pad[14];     /* pad space, unused            */
203         __u32           bs_dmevmask;    /* DMIG event mask              */
204         __u16           bs_dmstate;     /* DMIG state info              */
205         __u16           bs_aextents;    /* attribute number of extents  */
206 } _PACKED compat_xfs_bstat_t;
207
208 STATIC int xfs_bulkstat_one_fmt_compat(
209         void                    __user *ubuffer,
210         const xfs_bstat_t       *buffer)
211 {
212         compat_xfs_bstat_t __user *p32 = ubuffer;
213
214         if (put_user(buffer->bs_ino, &p32->bs_ino) ||
215             put_user(buffer->bs_mode, &p32->bs_mode) ||
216             put_user(buffer->bs_nlink, &p32->bs_nlink) ||
217             put_user(buffer->bs_uid, &p32->bs_uid) ||
218             put_user(buffer->bs_gid, &p32->bs_gid) ||
219             put_user(buffer->bs_rdev, &p32->bs_rdev) ||
220             put_user(buffer->bs_blksize, &p32->bs_blksize) ||
221             put_user(buffer->bs_size, &p32->bs_size) ||
222             xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
223             xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
224             xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
225             put_user(buffer->bs_blocks, &p32->bs_blocks) ||
226             put_user(buffer->bs_xflags, &p32->bs_xflags) ||
227             put_user(buffer->bs_extsize, &p32->bs_extsize) ||
228             put_user(buffer->bs_extents, &p32->bs_extents) ||
229             put_user(buffer->bs_gen, &p32->bs_gen) ||
230             put_user(buffer->bs_projid, &p32->bs_projid) ||
231             put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
232             put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
233             put_user(buffer->bs_aextents, &p32->bs_aextents))
234                 return -EFAULT;
235         return sizeof(*p32);
236 }
237
238
239
240 typedef struct compat_xfs_fsop_bulkreq {
241         compat_uptr_t   lastip;         /* last inode # pointer         */
242         __s32           icount;         /* count of entries in buffer   */
243         compat_uptr_t   ubuffer;        /* user buffer for inode desc.  */
244         compat_uptr_t   ocount;         /* output count pointer         */
245 } compat_xfs_fsop_bulkreq_t;
246
247 #define XFS_IOC_FSBULKSTAT_32 \
248         _IOWR('X', 101, struct compat_xfs_fsop_bulkreq)
249 #define XFS_IOC_FSBULKSTAT_SINGLE_32 \
250         _IOWR('X', 102, struct compat_xfs_fsop_bulkreq)
251 #define XFS_IOC_FSINUMBERS_32 \
252         _IOWR('X', 103, struct compat_xfs_fsop_bulkreq)
253
254 /* copied from xfs_ioctl.c */
255 STATIC int
256 xfs_ioc_bulkstat_compat(
257         xfs_mount_t             *mp,
258         unsigned int            cmd,
259         void                    __user *arg)
260 {
261         compat_xfs_fsop_bulkreq_t __user *p32 = (void __user *)arg;
262         u32                     addr;
263         xfs_fsop_bulkreq_t      bulkreq;
264         int                     count;  /* # of records returned */
265         xfs_ino_t               inlast; /* last inode number */
266         int                     done;
267         int                     error;
268
269         /* done = 1 if there are more stats to get and if bulkstat */
270         /* should be called again (unused here, but used in dmapi) */
271
272         if (!capable(CAP_SYS_ADMIN))
273                 return -EPERM;
274
275         if (XFS_FORCED_SHUTDOWN(mp))
276                 return -XFS_ERROR(EIO);
277
278         if (get_user(addr, &p32->lastip))
279                 return -EFAULT;
280         bulkreq.lastip = compat_ptr(addr);
281         if (get_user(bulkreq.icount, &p32->icount) ||
282             get_user(addr, &p32->ubuffer))
283                 return -EFAULT;
284         bulkreq.ubuffer = compat_ptr(addr);
285         if (get_user(addr, &p32->ocount))
286                 return -EFAULT;
287         bulkreq.ocount = compat_ptr(addr);
288
289         if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
290                 return -XFS_ERROR(EFAULT);
291
292         if ((count = bulkreq.icount) <= 0)
293                 return -XFS_ERROR(EINVAL);
294
295         if (bulkreq.ubuffer == NULL)
296                 return -XFS_ERROR(EINVAL);
297
298         if (cmd == XFS_IOC_FSINUMBERS)
299                 error = xfs_inumbers(mp, &inlast, &count,
300                                 bulkreq.ubuffer, xfs_inumbers_fmt_compat);
301         else {
302                 /* declare a var to get a warning in case the type changes */
303                 bulkstat_one_fmt_pf formatter = xfs_bulkstat_one_fmt_compat;
304                 error = xfs_bulkstat(mp, &inlast, &count,
305                         xfs_bulkstat_one, formatter,
306                         sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
307                         BULKSTAT_FG_QUICK, &done);
308         }
309         if (error)
310                 return -error;
311
312         if (bulkreq.ocount != NULL) {
313                 if (copy_to_user(bulkreq.lastip, &inlast,
314                                                 sizeof(xfs_ino_t)))
315                         return -XFS_ERROR(EFAULT);
316
317                 if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
318                         return -XFS_ERROR(EFAULT);
319         }
320
321         return 0;
322 }
323
324
325
326 typedef struct compat_xfs_fsop_handlereq {
327         __u32           fd;             /* fd for FD_TO_HANDLE          */
328         compat_uptr_t   path;           /* user pathname                */
329         __u32           oflags;         /* open flags                   */
330         compat_uptr_t   ihandle;        /* user supplied handle         */
331         __u32           ihandlen;       /* user supplied length         */
332         compat_uptr_t   ohandle;        /* user buffer for handle       */
333         compat_uptr_t   ohandlen;       /* user buffer length           */
334 } compat_xfs_fsop_handlereq_t;
335
336 #define XFS_IOC_PATH_TO_FSHANDLE_32 \
337         _IOWR('X', 104, struct compat_xfs_fsop_handlereq)
338 #define XFS_IOC_PATH_TO_HANDLE_32 \
339         _IOWR('X', 105, struct compat_xfs_fsop_handlereq)
340 #define XFS_IOC_FD_TO_HANDLE_32 \
341         _IOWR('X', 106, struct compat_xfs_fsop_handlereq)
342 #define XFS_IOC_OPEN_BY_HANDLE_32 \
343         _IOWR('X', 107, struct compat_xfs_fsop_handlereq)
344 #define XFS_IOC_READLINK_BY_HANDLE_32 \
345         _IOWR('X', 108, struct compat_xfs_fsop_handlereq)
346
347 STATIC unsigned long xfs_ioctl32_fshandle(unsigned long arg)
348 {
349         compat_xfs_fsop_handlereq_t __user *p32 = (void __user *)arg;
350         xfs_fsop_handlereq_t __user *p = compat_alloc_user_space(sizeof(*p));
351         u32 addr;
352
353         if (copy_in_user(&p->fd, &p32->fd, sizeof(__u32)) ||
354             get_user(addr, &p32->path) ||
355             put_user(compat_ptr(addr), &p->path) ||
356             copy_in_user(&p->oflags, &p32->oflags, sizeof(__u32)) ||
357             get_user(addr, &p32->ihandle) ||
358             put_user(compat_ptr(addr), &p->ihandle) ||
359             copy_in_user(&p->ihandlen, &p32->ihandlen, sizeof(__u32)) ||
360             get_user(addr, &p32->ohandle) ||
361             put_user(compat_ptr(addr), &p->ohandle) ||
362             get_user(addr, &p32->ohandlen) ||
363             put_user(compat_ptr(addr), &p->ohandlen))
364                 return -EFAULT;
365
366         return (unsigned long)p;
367 }
368
369
370 STATIC long
371 xfs_compat_ioctl(
372         int             mode,
373         struct file     *file,
374         unsigned        cmd,
375         unsigned long   arg)
376 {
377         struct inode    *inode = file->f_path.dentry->d_inode;
378         int             error;
379
380         switch (cmd) {
381         case XFS_IOC_DIOINFO:
382         case XFS_IOC_FSGEOMETRY:
383         case XFS_IOC_FSGETXATTR:
384         case XFS_IOC_FSSETXATTR:
385         case XFS_IOC_FSGETXATTRA:
386         case XFS_IOC_FSSETDM:
387         case XFS_IOC_GETBMAP:
388         case XFS_IOC_GETBMAPA:
389         case XFS_IOC_GETBMAPX:
390 /* not handled
391         case XFS_IOC_FSSETDM_BY_HANDLE:
392         case XFS_IOC_ATTRLIST_BY_HANDLE:
393         case XFS_IOC_ATTRMULTI_BY_HANDLE:
394 */
395         case XFS_IOC_FSCOUNTS:
396         case XFS_IOC_SET_RESBLKS:
397         case XFS_IOC_GET_RESBLKS:
398         case XFS_IOC_FSGROWFSDATA:
399         case XFS_IOC_FSGROWFSLOG:
400         case XFS_IOC_FSGROWFSRT:
401         case XFS_IOC_FREEZE:
402         case XFS_IOC_THAW:
403         case XFS_IOC_GOINGDOWN:
404         case XFS_IOC_ERROR_INJECTION:
405         case XFS_IOC_ERROR_CLEARALL:
406                 break;
407
408         case XFS_IOC32_GETXFLAGS:
409         case XFS_IOC32_SETXFLAGS:
410         case XFS_IOC32_GETVERSION:
411                 cmd = _NATIVE_IOC(cmd, long);
412                 break;
413 #ifdef BROKEN_X86_ALIGNMENT
414         /* xfs_flock_t has wrong u32 vs u64 alignment */
415         case XFS_IOC_ALLOCSP_32:
416         case XFS_IOC_FREESP_32:
417         case XFS_IOC_ALLOCSP64_32:
418         case XFS_IOC_FREESP64_32:
419         case XFS_IOC_RESVSP_32:
420         case XFS_IOC_UNRESVSP_32:
421         case XFS_IOC_RESVSP64_32:
422         case XFS_IOC_UNRESVSP64_32:
423                 arg = xfs_ioctl32_flock(arg);
424                 cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
425                 break;
426         case XFS_IOC_FSGEOMETRY_V1_32:
427                 arg = xfs_ioctl32_geom_v1(arg);
428                 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_geom_v1);
429                 break;
430
431 #else /* These are handled fine if no alignment issues */
432         case XFS_IOC_ALLOCSP:
433         case XFS_IOC_FREESP:
434         case XFS_IOC_RESVSP:
435         case XFS_IOC_UNRESVSP:
436         case XFS_IOC_ALLOCSP64:
437         case XFS_IOC_FREESP64:
438         case XFS_IOC_RESVSP64:
439         case XFS_IOC_UNRESVSP64:
440         case XFS_IOC_FSGEOMETRY_V1:
441                 break;
442
443         /* xfs_bstat_t still has wrong u32 vs u64 alignment */
444         case XFS_IOC_SWAPEXT:
445                 break;
446
447 #endif
448         case XFS_IOC_FSBULKSTAT_32:
449         case XFS_IOC_FSBULKSTAT_SINGLE_32:
450         case XFS_IOC_FSINUMBERS_32:
451                 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_bulkreq);
452                 return xfs_ioc_bulkstat_compat(XFS_I(inode)->i_mount,
453                                 cmd, (void __user*)arg);
454         case XFS_IOC_FD_TO_HANDLE_32:
455         case XFS_IOC_PATH_TO_HANDLE_32:
456         case XFS_IOC_PATH_TO_FSHANDLE_32:
457         case XFS_IOC_OPEN_BY_HANDLE_32:
458         case XFS_IOC_READLINK_BY_HANDLE_32:
459                 arg = xfs_ioctl32_fshandle(arg);
460                 cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
461                 break;
462         default:
463                 return -ENOIOCTLCMD;
464         }
465
466         error = xfs_ioctl(XFS_I(inode), file, mode, cmd, (void __user *)arg);
467         xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
468
469         return error;
470 }
471
472 long
473 xfs_file_compat_ioctl(
474         struct file             *file,
475         unsigned                cmd,
476         unsigned long           arg)
477 {
478         return xfs_compat_ioctl(0, file, cmd, arg);
479 }
480
481 long
482 xfs_file_compat_invis_ioctl(
483         struct file             *file,
484         unsigned                cmd,
485         unsigned long           arg)
486 {
487         return xfs_compat_ioctl(IO_INVIS, file, cmd, arg);
488 }