blob: 4eb8e9ea4af561cd9d141a44709cb94138a25594 [file] [log] [blame]
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02001#include <linux/blkdev.h>
2#include <linux/blkpg.h>
3#include <linux/blktrace_api.h>
4#include <linux/cdrom.h>
5#include <linux/compat.h>
6#include <linux/elevator.h>
7#include <linux/fd.h>
8#include <linux/hdreg.h>
9#include <linux/syscalls.h>
10#include <linux/smp_lock.h>
11#include <linux/types.h>
12#include <linux/uaccess.h>
13
14static int compat_put_ushort(unsigned long arg, unsigned short val)
15{
16 return put_user(val, (unsigned short __user *)compat_ptr(arg));
17}
18
19static int compat_put_int(unsigned long arg, int val)
20{
21 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
22}
23
Martin K. Petersenac481c22009-10-03 20:52:01 +020024static int compat_put_uint(unsigned long arg, unsigned int val)
25{
26 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
27}
28
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020029static int compat_put_long(unsigned long arg, long val)
30{
31 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
32}
33
34static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
35{
36 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
37}
38
39static int compat_put_u64(unsigned long arg, u64 val)
40{
41 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
42}
43
Arnd Bergmann9617db02007-10-09 13:23:55 +020044struct compat_hd_geometry {
45 unsigned char heads;
46 unsigned char sectors;
47 unsigned short cylinders;
48 u32 start;
49};
50
51static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
52 struct compat_hd_geometry __user *ugeo)
53{
54 struct hd_geometry geo;
55 int ret;
56
57 if (!ugeo)
58 return -EINVAL;
59 if (!disk->fops->getgeo)
60 return -ENOTTY;
61
62 /*
63 * We need to set the startsect first, the driver may
64 * want to override it.
65 */
66 geo.start = get_start_sect(bdev);
67 ret = disk->fops->getgeo(bdev, &geo);
68 if (ret)
69 return ret;
70
71 ret = copy_to_user(ugeo, &geo, 4);
72 ret |= __put_user(geo.start, &ugeo->start);
73 if (ret)
74 ret = -EFAULT;
75
76 return ret;
77}
78
Al Viro33c2dca2008-02-22 22:02:50 -050079static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
80 unsigned int cmd, unsigned long arg)
Arnd Bergmann9617db02007-10-09 13:23:55 +020081{
82 mm_segment_t old_fs = get_fs();
83 unsigned long kval;
84 unsigned int __user *uvp;
85 int error;
86
87 set_fs(KERNEL_DS);
Al Viro33c2dca2008-02-22 22:02:50 -050088 error = __blkdev_driver_ioctl(bdev, mode,
Arnd Bergmann9617db02007-10-09 13:23:55 +020089 cmd, (unsigned long)(&kval));
90 set_fs(old_fs);
91
92 if (error == 0) {
93 uvp = compat_ptr(arg);
94 if (put_user(kval, uvp))
95 error = -EFAULT;
96 }
97 return error;
98}
99
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200100struct compat_cdrom_read_audio {
101 union cdrom_addr addr;
102 u8 addr_format;
103 compat_int_t nframes;
104 compat_caddr_t buf;
105};
106
107struct compat_cdrom_generic_command {
108 unsigned char cmd[CDROM_PACKET_SIZE];
109 compat_caddr_t buffer;
110 compat_uint_t buflen;
111 compat_int_t stat;
112 compat_caddr_t sense;
113 unsigned char data_direction;
114 compat_int_t quiet;
115 compat_int_t timeout;
116 compat_caddr_t reserved[1];
117};
118
Al Viro33c2dca2008-02-22 22:02:50 -0500119static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
120 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200121{
122 struct cdrom_read_audio __user *cdread_audio;
123 struct compat_cdrom_read_audio __user *cdread_audio32;
124 __u32 data;
125 void __user *datap;
126
127 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
128 cdread_audio32 = compat_ptr(arg);
129
130 if (copy_in_user(&cdread_audio->addr,
131 &cdread_audio32->addr,
132 (sizeof(*cdread_audio32) -
133 sizeof(compat_caddr_t))))
134 return -EFAULT;
135
136 if (get_user(data, &cdread_audio32->buf))
137 return -EFAULT;
138 datap = compat_ptr(data);
139 if (put_user(datap, &cdread_audio->buf))
140 return -EFAULT;
141
Al Viro33c2dca2008-02-22 22:02:50 -0500142 return __blkdev_driver_ioctl(bdev, mode, cmd,
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200143 (unsigned long)cdread_audio);
144}
145
Al Viro33c2dca2008-02-22 22:02:50 -0500146static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
147 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200148{
149 struct cdrom_generic_command __user *cgc;
150 struct compat_cdrom_generic_command __user *cgc32;
151 u32 data;
152 unsigned char dir;
153 int itmp;
154
155 cgc = compat_alloc_user_space(sizeof(*cgc));
156 cgc32 = compat_ptr(arg);
157
158 if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
159 get_user(data, &cgc32->buffer) ||
160 put_user(compat_ptr(data), &cgc->buffer) ||
161 copy_in_user(&cgc->buflen, &cgc32->buflen,
162 (sizeof(unsigned int) + sizeof(int))) ||
163 get_user(data, &cgc32->sense) ||
164 put_user(compat_ptr(data), &cgc->sense) ||
165 get_user(dir, &cgc32->data_direction) ||
166 put_user(dir, &cgc->data_direction) ||
167 get_user(itmp, &cgc32->quiet) ||
168 put_user(itmp, &cgc->quiet) ||
169 get_user(itmp, &cgc32->timeout) ||
170 put_user(itmp, &cgc->timeout) ||
171 get_user(data, &cgc32->reserved[0]) ||
172 put_user(compat_ptr(data), &cgc->reserved[0]))
173 return -EFAULT;
174
Al Viro33c2dca2008-02-22 22:02:50 -0500175 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200176}
177
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200178struct compat_blkpg_ioctl_arg {
179 compat_int_t op;
180 compat_int_t flags;
181 compat_int_t datalen;
182 compat_caddr_t data;
183};
184
Al Viro56b26ad2008-09-19 03:17:36 -0400185static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200186 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
187{
188 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
189 compat_caddr_t udata;
190 compat_int_t n;
191 int err;
192
193 err = get_user(n, &ua32->op);
194 err |= put_user(n, &a->op);
195 err |= get_user(n, &ua32->flags);
196 err |= put_user(n, &a->flags);
197 err |= get_user(n, &ua32->datalen);
198 err |= put_user(n, &a->datalen);
199 err |= get_user(udata, &ua32->data);
200 err |= put_user(compat_ptr(udata), &a->data);
201 if (err)
202 return err;
203
Al Viro56b26ad2008-09-19 03:17:36 -0400204 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200205}
206
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200207#define BLKBSZGET_32 _IOR(0x12, 112, int)
208#define BLKBSZSET_32 _IOW(0x12, 113, int)
209#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
210
Arnd Bergmann1ca91cd02007-10-09 13:23:57 +0200211struct compat_floppy_struct {
212 compat_uint_t size;
213 compat_uint_t sect;
214 compat_uint_t head;
215 compat_uint_t track;
216 compat_uint_t stretch;
217 unsigned char gap;
218 unsigned char rate;
219 unsigned char spec1;
220 unsigned char fmt_gap;
221 const compat_caddr_t name;
222};
223
224struct compat_floppy_drive_params {
225 char cmos;
226 compat_ulong_t max_dtr;
227 compat_ulong_t hlt;
228 compat_ulong_t hut;
229 compat_ulong_t srt;
230 compat_ulong_t spinup;
231 compat_ulong_t spindown;
232 unsigned char spindown_offset;
233 unsigned char select_delay;
234 unsigned char rps;
235 unsigned char tracks;
236 compat_ulong_t timeout;
237 unsigned char interleave_sect;
238 struct floppy_max_errors max_errors;
239 char flags;
240 char read_track;
241 short autodetect[8];
242 compat_int_t checkfreq;
243 compat_int_t native_format;
244};
245
246struct compat_floppy_drive_struct {
247 signed char flags;
248 compat_ulong_t spinup_date;
249 compat_ulong_t select_date;
250 compat_ulong_t first_read_date;
251 short probed_format;
252 short track;
253 short maxblock;
254 short maxtrack;
255 compat_int_t generation;
256 compat_int_t keep_data;
257 compat_int_t fd_ref;
258 compat_int_t fd_device;
259 compat_int_t last_checked;
260 compat_caddr_t dmabuf;
261 compat_int_t bufblocks;
262};
263
264struct compat_floppy_fdc_state {
265 compat_int_t spec1;
266 compat_int_t spec2;
267 compat_int_t dtr;
268 unsigned char version;
269 unsigned char dor;
270 compat_ulong_t address;
271 unsigned int rawcmd:2;
272 unsigned int reset:1;
273 unsigned int need_configure:1;
274 unsigned int perp_mode:2;
275 unsigned int has_fifo:1;
276 unsigned int driver_version;
277 unsigned char track[4];
278};
279
280struct compat_floppy_write_errors {
281 unsigned int write_errors;
282 compat_ulong_t first_error_sector;
283 compat_int_t first_error_generation;
284 compat_ulong_t last_error_sector;
285 compat_int_t last_error_generation;
286 compat_uint_t badness;
287};
288
289#define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
290#define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
291#define FDGETPRM32 _IOR(2, 0x04, struct compat_floppy_struct)
292#define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
293#define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
294#define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
295#define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
296#define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
297#define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
298
299static struct {
300 unsigned int cmd32;
301 unsigned int cmd;
302} fd_ioctl_trans_table[] = {
303 { FDSETPRM32, FDSETPRM },
304 { FDDEFPRM32, FDDEFPRM },
305 { FDGETPRM32, FDGETPRM },
306 { FDSETDRVPRM32, FDSETDRVPRM },
307 { FDGETDRVPRM32, FDGETDRVPRM },
308 { FDGETDRVSTAT32, FDGETDRVSTAT },
309 { FDPOLLDRVSTAT32, FDPOLLDRVSTAT },
310 { FDGETFDCSTAT32, FDGETFDCSTAT },
311 { FDWERRORGET32, FDWERRORGET }
312};
313
314#define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table)
315
Al Viro33c2dca2008-02-22 22:02:50 -0500316static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode,
317 unsigned int cmd, unsigned long arg)
Arnd Bergmann1ca91cd02007-10-09 13:23:57 +0200318{
319 mm_segment_t old_fs = get_fs();
320 void *karg = NULL;
321 unsigned int kcmd = 0;
322 int i, err;
323
324 for (i = 0; i < NR_FD_IOCTL_TRANS; i++)
325 if (cmd == fd_ioctl_trans_table[i].cmd32) {
326 kcmd = fd_ioctl_trans_table[i].cmd;
327 break;
328 }
329 if (!kcmd)
330 return -EINVAL;
331
332 switch (cmd) {
333 case FDSETPRM32:
334 case FDDEFPRM32:
335 case FDGETPRM32:
336 {
337 compat_uptr_t name;
338 struct compat_floppy_struct __user *uf;
339 struct floppy_struct *f;
340
341 uf = compat_ptr(arg);
342 f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL);
343 if (!karg)
344 return -ENOMEM;
345 if (cmd == FDGETPRM32)
346 break;
347 err = __get_user(f->size, &uf->size);
348 err |= __get_user(f->sect, &uf->sect);
349 err |= __get_user(f->head, &uf->head);
350 err |= __get_user(f->track, &uf->track);
351 err |= __get_user(f->stretch, &uf->stretch);
352 err |= __get_user(f->gap, &uf->gap);
353 err |= __get_user(f->rate, &uf->rate);
354 err |= __get_user(f->spec1, &uf->spec1);
355 err |= __get_user(f->fmt_gap, &uf->fmt_gap);
356 err |= __get_user(name, &uf->name);
357 f->name = compat_ptr(name);
358 if (err) {
359 err = -EFAULT;
360 goto out;
361 }
362 break;
363 }
364 case FDSETDRVPRM32:
365 case FDGETDRVPRM32:
366 {
367 struct compat_floppy_drive_params __user *uf;
368 struct floppy_drive_params *f;
369
370 uf = compat_ptr(arg);
371 f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL);
372 if (!karg)
373 return -ENOMEM;
374 if (cmd == FDGETDRVPRM32)
375 break;
376 err = __get_user(f->cmos, &uf->cmos);
377 err |= __get_user(f->max_dtr, &uf->max_dtr);
378 err |= __get_user(f->hlt, &uf->hlt);
379 err |= __get_user(f->hut, &uf->hut);
380 err |= __get_user(f->srt, &uf->srt);
381 err |= __get_user(f->spinup, &uf->spinup);
382 err |= __get_user(f->spindown, &uf->spindown);
383 err |= __get_user(f->spindown_offset, &uf->spindown_offset);
384 err |= __get_user(f->select_delay, &uf->select_delay);
385 err |= __get_user(f->rps, &uf->rps);
386 err |= __get_user(f->tracks, &uf->tracks);
387 err |= __get_user(f->timeout, &uf->timeout);
388 err |= __get_user(f->interleave_sect, &uf->interleave_sect);
389 err |= __copy_from_user(&f->max_errors, &uf->max_errors, sizeof(f->max_errors));
390 err |= __get_user(f->flags, &uf->flags);
391 err |= __get_user(f->read_track, &uf->read_track);
392 err |= __copy_from_user(f->autodetect, uf->autodetect, sizeof(f->autodetect));
393 err |= __get_user(f->checkfreq, &uf->checkfreq);
394 err |= __get_user(f->native_format, &uf->native_format);
395 if (err) {
396 err = -EFAULT;
397 goto out;
398 }
399 break;
400 }
401 case FDGETDRVSTAT32:
402 case FDPOLLDRVSTAT32:
403 karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL);
404 if (!karg)
405 return -ENOMEM;
406 break;
407 case FDGETFDCSTAT32:
408 karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL);
409 if (!karg)
410 return -ENOMEM;
411 break;
412 case FDWERRORGET32:
413 karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL);
414 if (!karg)
415 return -ENOMEM;
416 break;
417 default:
418 return -EINVAL;
419 }
420 set_fs(KERNEL_DS);
Al Viro33c2dca2008-02-22 22:02:50 -0500421 err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg);
Arnd Bergmann1ca91cd02007-10-09 13:23:57 +0200422 set_fs(old_fs);
423 if (err)
424 goto out;
425 switch (cmd) {
426 case FDGETPRM32:
427 {
428 struct floppy_struct *f = karg;
429 struct compat_floppy_struct __user *uf = compat_ptr(arg);
430
431 err = __put_user(f->size, &uf->size);
432 err |= __put_user(f->sect, &uf->sect);
433 err |= __put_user(f->head, &uf->head);
434 err |= __put_user(f->track, &uf->track);
435 err |= __put_user(f->stretch, &uf->stretch);
436 err |= __put_user(f->gap, &uf->gap);
437 err |= __put_user(f->rate, &uf->rate);
438 err |= __put_user(f->spec1, &uf->spec1);
439 err |= __put_user(f->fmt_gap, &uf->fmt_gap);
440 err |= __put_user((u64)f->name, (compat_caddr_t __user *)&uf->name);
441 break;
442 }
443 case FDGETDRVPRM32:
444 {
445 struct compat_floppy_drive_params __user *uf;
446 struct floppy_drive_params *f = karg;
447
448 uf = compat_ptr(arg);
449 err = __put_user(f->cmos, &uf->cmos);
450 err |= __put_user(f->max_dtr, &uf->max_dtr);
451 err |= __put_user(f->hlt, &uf->hlt);
452 err |= __put_user(f->hut, &uf->hut);
453 err |= __put_user(f->srt, &uf->srt);
454 err |= __put_user(f->spinup, &uf->spinup);
455 err |= __put_user(f->spindown, &uf->spindown);
456 err |= __put_user(f->spindown_offset, &uf->spindown_offset);
457 err |= __put_user(f->select_delay, &uf->select_delay);
458 err |= __put_user(f->rps, &uf->rps);
459 err |= __put_user(f->tracks, &uf->tracks);
460 err |= __put_user(f->timeout, &uf->timeout);
461 err |= __put_user(f->interleave_sect, &uf->interleave_sect);
462 err |= __copy_to_user(&uf->max_errors, &f->max_errors, sizeof(f->max_errors));
463 err |= __put_user(f->flags, &uf->flags);
464 err |= __put_user(f->read_track, &uf->read_track);
465 err |= __copy_to_user(uf->autodetect, f->autodetect, sizeof(f->autodetect));
466 err |= __put_user(f->checkfreq, &uf->checkfreq);
467 err |= __put_user(f->native_format, &uf->native_format);
468 break;
469 }
470 case FDGETDRVSTAT32:
471 case FDPOLLDRVSTAT32:
472 {
473 struct compat_floppy_drive_struct __user *uf;
474 struct floppy_drive_struct *f = karg;
475
476 uf = compat_ptr(arg);
477 err = __put_user(f->flags, &uf->flags);
478 err |= __put_user(f->spinup_date, &uf->spinup_date);
479 err |= __put_user(f->select_date, &uf->select_date);
480 err |= __put_user(f->first_read_date, &uf->first_read_date);
481 err |= __put_user(f->probed_format, &uf->probed_format);
482 err |= __put_user(f->track, &uf->track);
483 err |= __put_user(f->maxblock, &uf->maxblock);
484 err |= __put_user(f->maxtrack, &uf->maxtrack);
485 err |= __put_user(f->generation, &uf->generation);
486 err |= __put_user(f->keep_data, &uf->keep_data);
487 err |= __put_user(f->fd_ref, &uf->fd_ref);
488 err |= __put_user(f->fd_device, &uf->fd_device);
489 err |= __put_user(f->last_checked, &uf->last_checked);
490 err |= __put_user((u64)f->dmabuf, &uf->dmabuf);
491 err |= __put_user((u64)f->bufblocks, &uf->bufblocks);
492 break;
493 }
494 case FDGETFDCSTAT32:
495 {
496 struct compat_floppy_fdc_state __user *uf;
497 struct floppy_fdc_state *f = karg;
498
499 uf = compat_ptr(arg);
500 err = __put_user(f->spec1, &uf->spec1);
501 err |= __put_user(f->spec2, &uf->spec2);
502 err |= __put_user(f->dtr, &uf->dtr);
503 err |= __put_user(f->version, &uf->version);
504 err |= __put_user(f->dor, &uf->dor);
505 err |= __put_user(f->address, &uf->address);
506 err |= __copy_to_user((char __user *)&uf->address + sizeof(uf->address),
507 (char *)&f->address + sizeof(f->address), sizeof(int));
508 err |= __put_user(f->driver_version, &uf->driver_version);
509 err |= __copy_to_user(uf->track, f->track, sizeof(f->track));
510 break;
511 }
512 case FDWERRORGET32:
513 {
514 struct compat_floppy_write_errors __user *uf;
515 struct floppy_write_errors *f = karg;
516
517 uf = compat_ptr(arg);
518 err = __put_user(f->write_errors, &uf->write_errors);
519 err |= __put_user(f->first_error_sector, &uf->first_error_sector);
520 err |= __put_user(f->first_error_generation, &uf->first_error_generation);
521 err |= __put_user(f->last_error_sector, &uf->last_error_sector);
522 err |= __put_user(f->last_error_generation, &uf->last_error_generation);
523 err |= __put_user(f->badness, &uf->badness);
524 break;
525 }
526 default:
527 break;
528 }
529 if (err)
530 err = -EFAULT;
531
532out:
533 kfree(karg);
534 return err;
535}
536
Arnd Bergmann171044d42007-10-09 13:23:53 +0200537struct compat_blk_user_trace_setup {
538 char name[32];
539 u16 act_mask;
540 u32 buf_size;
541 u32 buf_nr;
542 compat_u64 start_lba;
543 compat_u64 end_lba;
544 u32 pid;
545};
546#define BLKTRACESETUP32 _IOWR(0x12, 115, struct compat_blk_user_trace_setup)
547
548static int compat_blk_trace_setup(struct block_device *bdev, char __user *arg)
549{
550 struct blk_user_trace_setup buts;
551 struct compat_blk_user_trace_setup cbuts;
552 struct request_queue *q;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100553 char b[BDEVNAME_SIZE];
Arnd Bergmann171044d42007-10-09 13:23:53 +0200554 int ret;
555
556 q = bdev_get_queue(bdev);
557 if (!q)
558 return -ENXIO;
559
560 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
561 return -EFAULT;
562
Jean Delvaref36f21e2008-05-12 14:02:33 -0700563 bdevname(bdev, b);
Christof Schmitt6da127a2008-01-11 10:09:43 +0100564
Arnd Bergmann171044d42007-10-09 13:23:53 +0200565 buts = (struct blk_user_trace_setup) {
566 .act_mask = cbuts.act_mask,
567 .buf_size = cbuts.buf_size,
568 .buf_nr = cbuts.buf_nr,
569 .start_lba = cbuts.start_lba,
570 .end_lba = cbuts.end_lba,
571 .pid = cbuts.pid,
572 };
573 memcpy(&buts.name, &cbuts.name, 32);
574
575 mutex_lock(&bdev->bd_mutex);
Shawn Dud0deef52009-04-14 13:58:56 +0800576 ret = do_blk_trace_setup(q, b, bdev->bd_dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200577 mutex_unlock(&bdev->bd_mutex);
578 if (ret)
579 return ret;
580
581 if (copy_to_user(arg, &buts.name, 32))
582 return -EFAULT;
583
584 return 0;
585}
586
Al Viro33c2dca2008-02-22 22:02:50 -0500587static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
588 unsigned cmd, unsigned long arg)
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200589{
Philip Langdale33013a82007-10-27 13:34:10 +0200590 switch (cmd) {
Arnd Bergmann9617db02007-10-09 13:23:55 +0200591 case HDIO_GET_UNMASKINTR:
592 case HDIO_GET_MULTCOUNT:
593 case HDIO_GET_KEEPSETTINGS:
594 case HDIO_GET_32BIT:
595 case HDIO_GET_NOWERR:
596 case HDIO_GET_DMA:
597 case HDIO_GET_NICE:
598 case HDIO_GET_WCACHE:
599 case HDIO_GET_ACOUSTIC:
600 case HDIO_GET_ADDRESS:
601 case HDIO_GET_BUSSTATE:
Al Viro33c2dca2008-02-22 22:02:50 -0500602 return compat_hdio_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann1ca91cd02007-10-09 13:23:57 +0200603 case FDSETPRM32:
604 case FDDEFPRM32:
605 case FDGETPRM32:
606 case FDSETDRVPRM32:
607 case FDGETDRVPRM32:
608 case FDGETDRVSTAT32:
609 case FDPOLLDRVSTAT32:
610 case FDGETFDCSTAT32:
611 case FDWERRORGET32:
Al Viro33c2dca2008-02-22 22:02:50 -0500612 return compat_fd_ioctl(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200613 case CDROMREADAUDIO:
Al Viro33c2dca2008-02-22 22:02:50 -0500614 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200615 case CDROM_SEND_PACKET:
Al Viro33c2dca2008-02-22 22:02:50 -0500616 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200617
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200618 /*
619 * No handler required for the ones below, we just need to
620 * convert arg to a 64 bit pointer.
621 */
622 case BLKSECTSET:
623 /*
624 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
625 * Some need translations, these do not.
626 */
627 case HDIO_GET_IDENTITY:
628 case HDIO_DRIVE_TASK:
629 case HDIO_DRIVE_CMD:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200630 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
631 case 0x330:
632 /* 0x02 -- Floppy ioctls */
633 case FDMSGON:
634 case FDMSGOFF:
635 case FDSETEMSGTRESH:
636 case FDFLUSH:
637 case FDWERRORCLR:
638 case FDSETMAXERRS:
639 case FDGETMAXERRS:
640 case FDGETDRVTYP:
641 case FDEJECT:
642 case FDCLRPRM:
643 case FDFMTBEG:
644 case FDFMTEND:
645 case FDRESET:
646 case FDTWADDLE:
647 case FDFMTTRK:
648 case FDRAWCMD:
649 /* CDROM stuff */
650 case CDROMPAUSE:
651 case CDROMRESUME:
652 case CDROMPLAYMSF:
653 case CDROMPLAYTRKIND:
654 case CDROMREADTOCHDR:
655 case CDROMREADTOCENTRY:
656 case CDROMSTOP:
657 case CDROMSTART:
658 case CDROMEJECT:
659 case CDROMVOLCTRL:
660 case CDROMSUBCHNL:
661 case CDROMMULTISESSION:
662 case CDROM_GET_MCN:
663 case CDROMRESET:
664 case CDROMVOLREAD:
665 case CDROMSEEK:
666 case CDROMPLAYBLK:
667 case CDROMCLOSETRAY:
668 case CDROM_DISC_STATUS:
669 case CDROM_CHANGER_NSLOTS:
670 case CDROM_GET_CAPABILITY:
671 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
672 * not take a struct cdrom_read, instead they take a struct cdrom_msf
673 * which is compatible.
674 */
675 case CDROMREADMODE2:
676 case CDROMREADMODE1:
677 case CDROMREADRAW:
678 case CDROMREADCOOKED:
679 case CDROMREADALL:
680 /* DVD ioctls */
681 case DVD_READ_STRUCT:
682 case DVD_WRITE_STRUCT:
683 case DVD_AUTH:
684 arg = (unsigned long)compat_ptr(arg);
Andreas Schwab1c925602008-10-31 22:39:46 +0100685 /* These intepret arg as an unsigned long, not as a pointer,
686 * so we must not do compat_ptr() conversion. */
687 case HDIO_SET_MULTCOUNT:
688 case HDIO_SET_UNMASKINTR:
689 case HDIO_SET_KEEPSETTINGS:
690 case HDIO_SET_32BIT:
691 case HDIO_SET_NOWERR:
692 case HDIO_SET_DMA:
693 case HDIO_SET_PIO_MODE:
694 case HDIO_SET_NICE:
695 case HDIO_SET_WCACHE:
696 case HDIO_SET_ACOUSTIC:
697 case HDIO_SET_BUSSTATE:
698 case HDIO_SET_ADDRESS:
699 case CDROMEJECT_SW:
700 case CDROM_SET_OPTIONS:
701 case CDROM_CLEAR_OPTIONS:
702 case CDROM_SELECT_SPEED:
703 case CDROM_SELECT_DISC:
704 case CDROM_MEDIA_CHANGED:
705 case CDROM_DRIVE_STATUS:
706 case CDROM_LOCKDOOR:
707 case CDROM_DEBUG:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200708 break;
709 default:
710 /* unknown ioctl number */
711 return -ENOIOCTLCMD;
712 }
713
Al Viro33c2dca2008-02-22 22:02:50 -0500714 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200715}
716
Al Viro45048d02008-09-18 15:53:24 -0400717/* Most of the generic ioctls are handled in the normal fallback path.
718 This assumes the blkdev's low level compat_ioctl always returns
719 ENOIOCTLCMD for unknown ioctls. */
720long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200721{
Al Viro45048d02008-09-18 15:53:24 -0400722 int ret = -ENOIOCTLCMD;
723 struct inode *inode = file->f_mapping->host;
724 struct block_device *bdev = inode->i_bdev;
725 struct gendisk *disk = bdev->bd_disk;
726 fmode_t mode = file->f_mode;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200727 struct backing_dev_info *bdi;
Al Viro45048d02008-09-18 15:53:24 -0400728 loff_t size;
729
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100730 /*
731 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
732 * to updated it before every ioctl.
733 */
Al Viro45048d02008-09-18 15:53:24 -0400734 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100735 mode |= FMODE_NDELAY;
736 else
737 mode &= ~FMODE_NDELAY;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200738
739 switch (cmd) {
Al Viro45048d02008-09-18 15:53:24 -0400740 case HDIO_GETGEO:
741 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200742 case BLKPBSZGET:
743 return compat_put_uint(arg, bdev_physical_block_size(bdev));
744 case BLKIOMIN:
745 return compat_put_uint(arg, bdev_io_min(bdev));
746 case BLKIOOPT:
747 return compat_put_uint(arg, bdev_io_opt(bdev));
748 case BLKALIGNOFF:
749 return compat_put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100750 case BLKDISCARDZEROES:
751 return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400752 case BLKFLSBUF:
753 case BLKROSET:
754 case BLKDISCARD:
755 /*
756 * the ones below are implemented in blkdev_locked_ioctl,
757 * but we call blkdev_ioctl, which gets the lock for us
758 */
759 case BLKRRPART:
Al Viro56b26ad2008-09-19 03:17:36 -0400760 return blkdev_ioctl(bdev, mode, cmd,
Al Viro45048d02008-09-18 15:53:24 -0400761 (unsigned long)compat_ptr(arg));
762 case BLKBSZSET_32:
Al Viro56b26ad2008-09-19 03:17:36 -0400763 return blkdev_ioctl(bdev, mode, BLKBSZSET,
Al Viro45048d02008-09-18 15:53:24 -0400764 (unsigned long)compat_ptr(arg));
765 case BLKPG:
Al Viro56b26ad2008-09-19 03:17:36 -0400766 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200767 case BLKRAGET:
768 case BLKFRAGET:
769 if (!arg)
770 return -EINVAL;
771 bdi = blk_get_backing_dev_info(bdev);
772 if (bdi == NULL)
773 return -ENOTTY;
774 return compat_put_long(arg,
775 (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
776 case BLKROGET: /* compatible */
777 return compat_put_int(arg, bdev_read_only(bdev) != 0);
778 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
779 return compat_put_int(arg, block_size(bdev));
780 case BLKSSZGET: /* get block device hardware sector size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400781 return compat_put_int(arg, bdev_logical_block_size(bdev));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200782 case BLKSECTGET:
783 return compat_put_ushort(arg,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400784 queue_max_sectors(bdev_get_queue(bdev)));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200785 case BLKRASET: /* compatible, but no compat_ptr (!) */
786 case BLKFRASET:
787 if (!capable(CAP_SYS_ADMIN))
788 return -EACCES;
789 bdi = blk_get_backing_dev_info(bdev);
790 if (bdi == NULL)
791 return -ENOTTY;
792 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
793 return 0;
794 case BLKGETSIZE:
Al Viro45048d02008-09-18 15:53:24 -0400795 size = bdev->bd_inode->i_size;
796 if ((size >> 9) > ~0UL)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200797 return -EFBIG;
Al Viro45048d02008-09-18 15:53:24 -0400798 return compat_put_ulong(arg, size >> 9);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200799
800 case BLKGETSIZE64_32:
801 return compat_put_u64(arg, bdev->bd_inode->i_size);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200802
803 case BLKTRACESETUP32:
Al Viro45048d02008-09-18 15:53:24 -0400804 lock_kernel();
805 ret = compat_blk_trace_setup(bdev, compat_ptr(arg));
806 unlock_kernel();
807 return ret;
Arnd Bergmann171044d42007-10-09 13:23:53 +0200808 case BLKTRACESTART: /* compatible */
809 case BLKTRACESTOP: /* compatible */
810 case BLKTRACETEARDOWN: /* compatible */
Al Viro45048d02008-09-18 15:53:24 -0400811 lock_kernel();
812 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
813 unlock_kernel();
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200814 return ret;
Al Viro45048d02008-09-18 15:53:24 -0400815 default:
816 if (disk->fops->compat_ioctl)
817 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
818 if (ret == -ENOIOCTLCMD)
819 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
820 return ret;
821 }
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200822}