compat_ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. #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/slab.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/types.h>
  12. #include <linux/uaccess.h>
  13. static int compat_put_ushort(unsigned long arg, unsigned short val)
  14. {
  15. return put_user(val, (unsigned short __user *)compat_ptr(arg));
  16. }
  17. static int compat_put_int(unsigned long arg, int val)
  18. {
  19. return put_user(val, (compat_int_t __user *)compat_ptr(arg));
  20. }
  21. static int compat_put_uint(unsigned long arg, unsigned int val)
  22. {
  23. return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
  24. }
  25. static int compat_put_long(unsigned long arg, long val)
  26. {
  27. return put_user(val, (compat_long_t __user *)compat_ptr(arg));
  28. }
  29. static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
  30. {
  31. return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
  32. }
  33. static int compat_put_u64(unsigned long arg, u64 val)
  34. {
  35. return put_user(val, (compat_u64 __user *)compat_ptr(arg));
  36. }
  37. struct compat_hd_geometry {
  38. unsigned char heads;
  39. unsigned char sectors;
  40. unsigned short cylinders;
  41. u32 start;
  42. };
  43. static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
  44. struct compat_hd_geometry __user *ugeo)
  45. {
  46. struct hd_geometry geo;
  47. int ret;
  48. if (!ugeo)
  49. return -EINVAL;
  50. if (!disk->fops->getgeo)
  51. return -ENOTTY;
  52. /*
  53. * We need to set the startsect first, the driver may
  54. * want to override it.
  55. */
  56. geo.start = get_start_sect(bdev);
  57. ret = disk->fops->getgeo(bdev, &geo);
  58. if (ret)
  59. return ret;
  60. ret = copy_to_user(ugeo, &geo, 4);
  61. ret |= __put_user(geo.start, &ugeo->start);
  62. if (ret)
  63. ret = -EFAULT;
  64. return ret;
  65. }
  66. static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
  67. unsigned int cmd, unsigned long arg)
  68. {
  69. mm_segment_t old_fs = get_fs();
  70. unsigned long kval;
  71. unsigned int __user *uvp;
  72. int error;
  73. set_fs(KERNEL_DS);
  74. error = __blkdev_driver_ioctl(bdev, mode,
  75. cmd, (unsigned long)(&kval));
  76. set_fs(old_fs);
  77. if (error == 0) {
  78. uvp = compat_ptr(arg);
  79. if (put_user(kval, uvp))
  80. error = -EFAULT;
  81. }
  82. return error;
  83. }
  84. struct compat_cdrom_read_audio {
  85. union cdrom_addr addr;
  86. u8 addr_format;
  87. compat_int_t nframes;
  88. compat_caddr_t buf;
  89. };
  90. struct compat_cdrom_generic_command {
  91. unsigned char cmd[CDROM_PACKET_SIZE];
  92. compat_caddr_t buffer;
  93. compat_uint_t buflen;
  94. compat_int_t stat;
  95. compat_caddr_t sense;
  96. unsigned char data_direction;
  97. compat_int_t quiet;
  98. compat_int_t timeout;
  99. compat_caddr_t reserved[1];
  100. };
  101. static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
  102. unsigned int cmd, unsigned long arg)
  103. {
  104. struct cdrom_read_audio __user *cdread_audio;
  105. struct compat_cdrom_read_audio __user *cdread_audio32;
  106. __u32 data;
  107. void __user *datap;
  108. cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
  109. cdread_audio32 = compat_ptr(arg);
  110. if (copy_in_user(&cdread_audio->addr,
  111. &cdread_audio32->addr,
  112. (sizeof(*cdread_audio32) -
  113. sizeof(compat_caddr_t))))
  114. return -EFAULT;
  115. if (get_user(data, &cdread_audio32->buf))
  116. return -EFAULT;
  117. datap = compat_ptr(data);
  118. if (put_user(datap, &cdread_audio->buf))
  119. return -EFAULT;
  120. return __blkdev_driver_ioctl(bdev, mode, cmd,
  121. (unsigned long)cdread_audio);
  122. }
  123. static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
  124. unsigned int cmd, unsigned long arg)
  125. {
  126. struct cdrom_generic_command __user *cgc;
  127. struct compat_cdrom_generic_command __user *cgc32;
  128. u32 data;
  129. unsigned char dir;
  130. int itmp;
  131. cgc = compat_alloc_user_space(sizeof(*cgc));
  132. cgc32 = compat_ptr(arg);
  133. if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
  134. get_user(data, &cgc32->buffer) ||
  135. put_user(compat_ptr(data), &cgc->buffer) ||
  136. copy_in_user(&cgc->buflen, &cgc32->buflen,
  137. (sizeof(unsigned int) + sizeof(int))) ||
  138. get_user(data, &cgc32->sense) ||
  139. put_user(compat_ptr(data), &cgc->sense) ||
  140. get_user(dir, &cgc32->data_direction) ||
  141. put_user(dir, &cgc->data_direction) ||
  142. get_user(itmp, &cgc32->quiet) ||
  143. put_user(itmp, &cgc->quiet) ||
  144. get_user(itmp, &cgc32->timeout) ||
  145. put_user(itmp, &cgc->timeout) ||
  146. get_user(data, &cgc32->reserved[0]) ||
  147. put_user(compat_ptr(data), &cgc->reserved[0]))
  148. return -EFAULT;
  149. return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
  150. }
  151. struct compat_blkpg_ioctl_arg {
  152. compat_int_t op;
  153. compat_int_t flags;
  154. compat_int_t datalen;
  155. compat_caddr_t data;
  156. };
  157. static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
  158. unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
  159. {
  160. struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
  161. compat_caddr_t udata;
  162. compat_int_t n;
  163. int err;
  164. err = get_user(n, &ua32->op);
  165. err |= put_user(n, &a->op);
  166. err |= get_user(n, &ua32->flags);
  167. err |= put_user(n, &a->flags);
  168. err |= get_user(n, &ua32->datalen);
  169. err |= put_user(n, &a->datalen);
  170. err |= get_user(udata, &ua32->data);
  171. err |= put_user(compat_ptr(udata), &a->data);
  172. if (err)
  173. return err;
  174. return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
  175. }
  176. #define BLKBSZGET_32 _IOR(0x12, 112, int)
  177. #define BLKBSZSET_32 _IOW(0x12, 113, int)
  178. #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
  179. struct compat_floppy_struct {
  180. compat_uint_t size;
  181. compat_uint_t sect;
  182. compat_uint_t head;
  183. compat_uint_t track;
  184. compat_uint_t stretch;
  185. unsigned char gap;
  186. unsigned char rate;
  187. unsigned char spec1;
  188. unsigned char fmt_gap;
  189. const compat_caddr_t name;
  190. };
  191. struct compat_floppy_drive_params {
  192. char cmos;
  193. compat_ulong_t max_dtr;
  194. compat_ulong_t hlt;
  195. compat_ulong_t hut;
  196. compat_ulong_t srt;
  197. compat_ulong_t spinup;
  198. compat_ulong_t spindown;
  199. unsigned char spindown_offset;
  200. unsigned char select_delay;
  201. unsigned char rps;
  202. unsigned char tracks;
  203. compat_ulong_t timeout;
  204. unsigned char interleave_sect;
  205. struct floppy_max_errors max_errors;
  206. char flags;
  207. char read_track;
  208. short autodetect[8];
  209. compat_int_t checkfreq;
  210. compat_int_t native_format;
  211. };
  212. struct compat_floppy_drive_struct {
  213. signed char flags;
  214. compat_ulong_t spinup_date;
  215. compat_ulong_t select_date;
  216. compat_ulong_t first_read_date;
  217. short probed_format;
  218. short track;
  219. short maxblock;
  220. short maxtrack;
  221. compat_int_t generation;
  222. compat_int_t keep_data;
  223. compat_int_t fd_ref;
  224. compat_int_t fd_device;
  225. compat_int_t last_checked;
  226. compat_caddr_t dmabuf;
  227. compat_int_t bufblocks;
  228. };
  229. struct compat_floppy_fdc_state {
  230. compat_int_t spec1;
  231. compat_int_t spec2;
  232. compat_int_t dtr;
  233. unsigned char version;
  234. unsigned char dor;
  235. compat_ulong_t address;
  236. unsigned int rawcmd:2;
  237. unsigned int reset:1;
  238. unsigned int need_configure:1;
  239. unsigned int perp_mode:2;
  240. unsigned int has_fifo:1;
  241. unsigned int driver_version;
  242. unsigned char track[4];
  243. };
  244. struct compat_floppy_write_errors {
  245. unsigned int write_errors;
  246. compat_ulong_t first_error_sector;
  247. compat_int_t first_error_generation;
  248. compat_ulong_t last_error_sector;
  249. compat_int_t last_error_generation;
  250. compat_uint_t badness;
  251. };
  252. #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
  253. #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
  254. #define FDGETPRM32 _IOR(2, 0x04, struct compat_floppy_struct)
  255. #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
  256. #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
  257. #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
  258. #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
  259. #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
  260. #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
  261. static struct {
  262. unsigned int cmd32;
  263. unsigned int cmd;
  264. } fd_ioctl_trans_table[] = {
  265. { FDSETPRM32, FDSETPRM },
  266. { FDDEFPRM32, FDDEFPRM },
  267. { FDGETPRM32, FDGETPRM },
  268. { FDSETDRVPRM32, FDSETDRVPRM },
  269. { FDGETDRVPRM32, FDGETDRVPRM },
  270. { FDGETDRVSTAT32, FDGETDRVSTAT },
  271. { FDPOLLDRVSTAT32, FDPOLLDRVSTAT },
  272. { FDGETFDCSTAT32, FDGETFDCSTAT },
  273. { FDWERRORGET32, FDWERRORGET }
  274. };
  275. #define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table)
  276. static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode,
  277. unsigned int cmd, unsigned long arg)
  278. {
  279. mm_segment_t old_fs = get_fs();
  280. void *karg = NULL;
  281. unsigned int kcmd = 0;
  282. int i, err;
  283. for (i = 0; i < NR_FD_IOCTL_TRANS; i++)
  284. if (cmd == fd_ioctl_trans_table[i].cmd32) {
  285. kcmd = fd_ioctl_trans_table[i].cmd;
  286. break;
  287. }
  288. if (!kcmd)
  289. return -EINVAL;
  290. switch (cmd) {
  291. case FDSETPRM32:
  292. case FDDEFPRM32:
  293. case FDGETPRM32:
  294. {
  295. compat_uptr_t name;
  296. struct compat_floppy_struct __user *uf;
  297. struct floppy_struct *f;
  298. uf = compat_ptr(arg);
  299. f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL);
  300. if (!karg)
  301. return -ENOMEM;
  302. if (cmd == FDGETPRM32)
  303. break;
  304. err = __get_user(f->size, &uf->size);
  305. err |= __get_user(f->sect, &uf->sect);
  306. err |= __get_user(f->head, &uf->head);
  307. err |= __get_user(f->track, &uf->track);
  308. err |= __get_user(f->stretch, &uf->stretch);
  309. err |= __get_user(f->gap, &uf->gap);
  310. err |= __get_user(f->rate, &uf->rate);
  311. err |= __get_user(f->spec1, &uf->spec1);
  312. err |= __get_user(f->fmt_gap, &uf->fmt_gap);
  313. err |= __get_user(name, &uf->name);
  314. f->name = compat_ptr(name);
  315. if (err) {
  316. err = -EFAULT;
  317. goto out;
  318. }
  319. break;
  320. }
  321. case FDSETDRVPRM32:
  322. case FDGETDRVPRM32:
  323. {
  324. struct compat_floppy_drive_params __user *uf;
  325. struct floppy_drive_params *f;
  326. uf = compat_ptr(arg);
  327. f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL);
  328. if (!karg)
  329. return -ENOMEM;
  330. if (cmd == FDGETDRVPRM32)
  331. break;
  332. err = __get_user(f->cmos, &uf->cmos);
  333. err |= __get_user(f->max_dtr, &uf->max_dtr);
  334. err |= __get_user(f->hlt, &uf->hlt);
  335. err |= __get_user(f->hut, &uf->hut);
  336. err |= __get_user(f->srt, &uf->srt);
  337. err |= __get_user(f->spinup, &uf->spinup);
  338. err |= __get_user(f->spindown, &uf->spindown);
  339. err |= __get_user(f->spindown_offset, &uf->spindown_offset);
  340. err |= __get_user(f->select_delay, &uf->select_delay);
  341. err |= __get_user(f->rps, &uf->rps);
  342. err |= __get_user(f->tracks, &uf->tracks);
  343. err |= __get_user(f->timeout, &uf->timeout);
  344. err |= __get_user(f->interleave_sect, &uf->interleave_sect);
  345. err |= __copy_from_user(&f->max_errors, &uf->max_errors, sizeof(f->max_errors));
  346. err |= __get_user(f->flags, &uf->flags);
  347. err |= __get_user(f->read_track, &uf->read_track);
  348. err |= __copy_from_user(f->autodetect, uf->autodetect, sizeof(f->autodetect));
  349. err |= __get_user(f->checkfreq, &uf->checkfreq);
  350. err |= __get_user(f->native_format, &uf->native_format);
  351. if (err) {
  352. err = -EFAULT;
  353. goto out;
  354. }
  355. break;
  356. }
  357. case FDGETDRVSTAT32:
  358. case FDPOLLDRVSTAT32:
  359. karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL);
  360. if (!karg)
  361. return -ENOMEM;
  362. break;
  363. case FDGETFDCSTAT32:
  364. karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL);
  365. if (!karg)
  366. return -ENOMEM;
  367. break;
  368. case FDWERRORGET32:
  369. karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL);
  370. if (!karg)
  371. return -ENOMEM;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. set_fs(KERNEL_DS);
  377. err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg);
  378. set_fs(old_fs);
  379. if (err)
  380. goto out;
  381. switch (cmd) {
  382. case FDGETPRM32:
  383. {
  384. struct floppy_struct *f = karg;
  385. struct compat_floppy_struct __user *uf = compat_ptr(arg);
  386. err = __put_user(f->size, &uf->size);
  387. err |= __put_user(f->sect, &uf->sect);
  388. err |= __put_user(f->head, &uf->head);
  389. err |= __put_user(f->track, &uf->track);
  390. err |= __put_user(f->stretch, &uf->stretch);
  391. err |= __put_user(f->gap, &uf->gap);
  392. err |= __put_user(f->rate, &uf->rate);
  393. err |= __put_user(f->spec1, &uf->spec1);
  394. err |= __put_user(f->fmt_gap, &uf->fmt_gap);
  395. err |= __put_user((u64)f->name, (compat_caddr_t __user *)&uf->name);
  396. break;
  397. }
  398. case FDGETDRVPRM32:
  399. {
  400. struct compat_floppy_drive_params __user *uf;
  401. struct floppy_drive_params *f = karg;
  402. uf = compat_ptr(arg);
  403. err = __put_user(f->cmos, &uf->cmos);
  404. err |= __put_user(f->max_dtr, &uf->max_dtr);
  405. err |= __put_user(f->hlt, &uf->hlt);
  406. err |= __put_user(f->hut, &uf->hut);
  407. err |= __put_user(f->srt, &uf->srt);
  408. err |= __put_user(f->spinup, &uf->spinup);
  409. err |= __put_user(f->spindown, &uf->spindown);
  410. err |= __put_user(f->spindown_offset, &uf->spindown_offset);
  411. err |= __put_user(f->select_delay, &uf->select_delay);
  412. err |= __put_user(f->rps, &uf->rps);
  413. err |= __put_user(f->tracks, &uf->tracks);
  414. err |= __put_user(f->timeout, &uf->timeout);
  415. err |= __put_user(f->interleave_sect, &uf->interleave_sect);
  416. err |= __copy_to_user(&uf->max_errors, &f->max_errors, sizeof(f->max_errors));
  417. err |= __put_user(f->flags, &uf->flags);
  418. err |= __put_user(f->read_track, &uf->read_track);
  419. err |= __copy_to_user(uf->autodetect, f->autodetect, sizeof(f->autodetect));
  420. err |= __put_user(f->checkfreq, &uf->checkfreq);
  421. err |= __put_user(f->native_format, &uf->native_format);
  422. break;
  423. }
  424. case FDGETDRVSTAT32:
  425. case FDPOLLDRVSTAT32:
  426. {
  427. struct compat_floppy_drive_struct __user *uf;
  428. struct floppy_drive_struct *f = karg;
  429. uf = compat_ptr(arg);
  430. err = __put_user(f->flags, &uf->flags);
  431. err |= __put_user(f->spinup_date, &uf->spinup_date);
  432. err |= __put_user(f->select_date, &uf->select_date);
  433. err |= __put_user(f->first_read_date, &uf->first_read_date);
  434. err |= __put_user(f->probed_format, &uf->probed_format);
  435. err |= __put_user(f->track, &uf->track);
  436. err |= __put_user(f->maxblock, &uf->maxblock);
  437. err |= __put_user(f->maxtrack, &uf->maxtrack);
  438. err |= __put_user(f->generation, &uf->generation);
  439. err |= __put_user(f->keep_data, &uf->keep_data);
  440. err |= __put_user(f->fd_ref, &uf->fd_ref);
  441. err |= __put_user(f->fd_device, &uf->fd_device);
  442. err |= __put_user(f->last_checked, &uf->last_checked);
  443. err |= __put_user((u64)f->dmabuf, &uf->dmabuf);
  444. err |= __put_user((u64)f->bufblocks, &uf->bufblocks);
  445. break;
  446. }
  447. case FDGETFDCSTAT32:
  448. {
  449. struct compat_floppy_fdc_state __user *uf;
  450. struct floppy_fdc_state *f = karg;
  451. uf = compat_ptr(arg);
  452. err = __put_user(f->spec1, &uf->spec1);
  453. err |= __put_user(f->spec2, &uf->spec2);
  454. err |= __put_user(f->dtr, &uf->dtr);
  455. err |= __put_user(f->version, &uf->version);
  456. err |= __put_user(f->dor, &uf->dor);
  457. err |= __put_user(f->address, &uf->address);
  458. err |= __copy_to_user((char __user *)&uf->address + sizeof(uf->address),
  459. (char *)&f->address + sizeof(f->address), sizeof(int));
  460. err |= __put_user(f->driver_version, &uf->driver_version);
  461. err |= __copy_to_user(uf->track, f->track, sizeof(f->track));
  462. break;
  463. }
  464. case FDWERRORGET32:
  465. {
  466. struct compat_floppy_write_errors __user *uf;
  467. struct floppy_write_errors *f = karg;
  468. uf = compat_ptr(arg);
  469. err = __put_user(f->write_errors, &uf->write_errors);
  470. err |= __put_user(f->first_error_sector, &uf->first_error_sector);
  471. err |= __put_user(f->first_error_generation, &uf->first_error_generation);
  472. err |= __put_user(f->last_error_sector, &uf->last_error_sector);
  473. err |= __put_user(f->last_error_generation, &uf->last_error_generation);
  474. err |= __put_user(f->badness, &uf->badness);
  475. break;
  476. }
  477. default:
  478. break;
  479. }
  480. if (err)
  481. err = -EFAULT;
  482. out:
  483. kfree(karg);
  484. return err;
  485. }
  486. static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
  487. unsigned cmd, unsigned long arg)
  488. {
  489. switch (cmd) {
  490. case HDIO_GET_UNMASKINTR:
  491. case HDIO_GET_MULTCOUNT:
  492. case HDIO_GET_KEEPSETTINGS:
  493. case HDIO_GET_32BIT:
  494. case HDIO_GET_NOWERR:
  495. case HDIO_GET_DMA:
  496. case HDIO_GET_NICE:
  497. case HDIO_GET_WCACHE:
  498. case HDIO_GET_ACOUSTIC:
  499. case HDIO_GET_ADDRESS:
  500. case HDIO_GET_BUSSTATE:
  501. return compat_hdio_ioctl(bdev, mode, cmd, arg);
  502. case FDSETPRM32:
  503. case FDDEFPRM32:
  504. case FDGETPRM32:
  505. case FDSETDRVPRM32:
  506. case FDGETDRVPRM32:
  507. case FDGETDRVSTAT32:
  508. case FDPOLLDRVSTAT32:
  509. case FDGETFDCSTAT32:
  510. case FDWERRORGET32:
  511. return compat_fd_ioctl(bdev, mode, cmd, arg);
  512. case CDROMREADAUDIO:
  513. return compat_cdrom_read_audio(bdev, mode, cmd, arg);
  514. case CDROM_SEND_PACKET:
  515. return compat_cdrom_generic_command(bdev, mode, cmd, arg);
  516. /*
  517. * No handler required for the ones below, we just need to
  518. * convert arg to a 64 bit pointer.
  519. */
  520. case BLKSECTSET:
  521. /*
  522. * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
  523. * Some need translations, these do not.
  524. */
  525. case HDIO_GET_IDENTITY:
  526. case HDIO_DRIVE_TASK:
  527. case HDIO_DRIVE_CMD:
  528. /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
  529. case 0x330:
  530. /* 0x02 -- Floppy ioctls */
  531. case FDMSGON:
  532. case FDMSGOFF:
  533. case FDSETEMSGTRESH:
  534. case FDFLUSH:
  535. case FDWERRORCLR:
  536. case FDSETMAXERRS:
  537. case FDGETMAXERRS:
  538. case FDGETDRVTYP:
  539. case FDEJECT:
  540. case FDCLRPRM:
  541. case FDFMTBEG:
  542. case FDFMTEND:
  543. case FDRESET:
  544. case FDTWADDLE:
  545. case FDFMTTRK:
  546. case FDRAWCMD:
  547. /* CDROM stuff */
  548. case CDROMPAUSE:
  549. case CDROMRESUME:
  550. case CDROMPLAYMSF:
  551. case CDROMPLAYTRKIND:
  552. case CDROMREADTOCHDR:
  553. case CDROMREADTOCENTRY:
  554. case CDROMSTOP:
  555. case CDROMSTART:
  556. case CDROMEJECT:
  557. case CDROMVOLCTRL:
  558. case CDROMSUBCHNL:
  559. case CDROMMULTISESSION:
  560. case CDROM_GET_MCN:
  561. case CDROMRESET:
  562. case CDROMVOLREAD:
  563. case CDROMSEEK:
  564. case CDROMPLAYBLK:
  565. case CDROMCLOSETRAY:
  566. case CDROM_DISC_STATUS:
  567. case CDROM_CHANGER_NSLOTS:
  568. case CDROM_GET_CAPABILITY:
  569. /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
  570. * not take a struct cdrom_read, instead they take a struct cdrom_msf
  571. * which is compatible.
  572. */
  573. case CDROMREADMODE2:
  574. case CDROMREADMODE1:
  575. case CDROMREADRAW:
  576. case CDROMREADCOOKED:
  577. case CDROMREADALL:
  578. /* DVD ioctls */
  579. case DVD_READ_STRUCT:
  580. case DVD_WRITE_STRUCT:
  581. case DVD_AUTH:
  582. arg = (unsigned long)compat_ptr(arg);
  583. /* These intepret arg as an unsigned long, not as a pointer,
  584. * so we must not do compat_ptr() conversion. */
  585. case HDIO_SET_MULTCOUNT:
  586. case HDIO_SET_UNMASKINTR:
  587. case HDIO_SET_KEEPSETTINGS:
  588. case HDIO_SET_32BIT:
  589. case HDIO_SET_NOWERR:
  590. case HDIO_SET_DMA:
  591. case HDIO_SET_PIO_MODE:
  592. case HDIO_SET_NICE:
  593. case HDIO_SET_WCACHE:
  594. case HDIO_SET_ACOUSTIC:
  595. case HDIO_SET_BUSSTATE:
  596. case HDIO_SET_ADDRESS:
  597. case CDROMEJECT_SW:
  598. case CDROM_SET_OPTIONS:
  599. case CDROM_CLEAR_OPTIONS:
  600. case CDROM_SELECT_SPEED:
  601. case CDROM_SELECT_DISC:
  602. case CDROM_MEDIA_CHANGED:
  603. case CDROM_DRIVE_STATUS:
  604. case CDROM_LOCKDOOR:
  605. case CDROM_DEBUG:
  606. break;
  607. default:
  608. /* unknown ioctl number */
  609. return -ENOIOCTLCMD;
  610. }
  611. return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  612. }
  613. /* Most of the generic ioctls are handled in the normal fallback path.
  614. This assumes the blkdev's low level compat_ioctl always returns
  615. ENOIOCTLCMD for unknown ioctls. */
  616. long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  617. {
  618. int ret = -ENOIOCTLCMD;
  619. struct inode *inode = file->f_mapping->host;
  620. struct block_device *bdev = inode->i_bdev;
  621. struct gendisk *disk = bdev->bd_disk;
  622. fmode_t mode = file->f_mode;
  623. struct backing_dev_info *bdi;
  624. loff_t size;
  625. /*
  626. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  627. * to updated it before every ioctl.
  628. */
  629. if (file->f_flags & O_NDELAY)
  630. mode |= FMODE_NDELAY;
  631. else
  632. mode &= ~FMODE_NDELAY;
  633. switch (cmd) {
  634. case HDIO_GETGEO:
  635. return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
  636. case BLKPBSZGET:
  637. return compat_put_uint(arg, bdev_physical_block_size(bdev));
  638. case BLKIOMIN:
  639. return compat_put_uint(arg, bdev_io_min(bdev));
  640. case BLKIOOPT:
  641. return compat_put_uint(arg, bdev_io_opt(bdev));
  642. case BLKALIGNOFF:
  643. return compat_put_int(arg, bdev_alignment_offset(bdev));
  644. case BLKDISCARDZEROES:
  645. return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
  646. case BLKFLSBUF:
  647. case BLKROSET:
  648. case BLKDISCARD:
  649. case BLKSECDISCARD:
  650. /*
  651. * the ones below are implemented in blkdev_locked_ioctl,
  652. * but we call blkdev_ioctl, which gets the lock for us
  653. */
  654. case BLKRRPART:
  655. return blkdev_ioctl(bdev, mode, cmd,
  656. (unsigned long)compat_ptr(arg));
  657. case BLKBSZSET_32:
  658. return blkdev_ioctl(bdev, mode, BLKBSZSET,
  659. (unsigned long)compat_ptr(arg));
  660. case BLKPG:
  661. return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
  662. case BLKRAGET:
  663. case BLKFRAGET:
  664. if (!arg)
  665. return -EINVAL;
  666. bdi = blk_get_backing_dev_info(bdev);
  667. if (bdi == NULL)
  668. return -ENOTTY;
  669. return compat_put_long(arg,
  670. (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  671. case BLKROGET: /* compatible */
  672. return compat_put_int(arg, bdev_read_only(bdev) != 0);
  673. case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
  674. return compat_put_int(arg, block_size(bdev));
  675. case BLKSSZGET: /* get block device hardware sector size */
  676. return compat_put_int(arg, bdev_logical_block_size(bdev));
  677. case BLKSECTGET:
  678. return compat_put_ushort(arg,
  679. queue_max_sectors(bdev_get_queue(bdev)));
  680. case BLKRASET: /* compatible, but no compat_ptr (!) */
  681. case BLKFRASET:
  682. if (!capable(CAP_SYS_ADMIN))
  683. return -EACCES;
  684. bdi = blk_get_backing_dev_info(bdev);
  685. if (bdi == NULL)
  686. return -ENOTTY;
  687. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  688. return 0;
  689. case BLKGETSIZE:
  690. size = i_size_read(bdev->bd_inode);
  691. if ((size >> 9) > ~0UL)
  692. return -EFBIG;
  693. return compat_put_ulong(arg, size >> 9);
  694. case BLKGETSIZE64_32:
  695. return compat_put_u64(arg, i_size_read(bdev->bd_inode));
  696. case BLKTRACESETUP32:
  697. case BLKTRACESTART: /* compatible */
  698. case BLKTRACESTOP: /* compatible */
  699. case BLKTRACETEARDOWN: /* compatible */
  700. ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
  701. return ret;
  702. default:
  703. if (disk->fops->compat_ioctl)
  704. ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
  705. if (ret == -ENOIOCTLCMD)
  706. ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
  707. return ret;
  708. }
  709. }