compat_ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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_drive_params {
  180. char cmos;
  181. compat_ulong_t max_dtr;
  182. compat_ulong_t hlt;
  183. compat_ulong_t hut;
  184. compat_ulong_t srt;
  185. compat_ulong_t spinup;
  186. compat_ulong_t spindown;
  187. unsigned char spindown_offset;
  188. unsigned char select_delay;
  189. unsigned char rps;
  190. unsigned char tracks;
  191. compat_ulong_t timeout;
  192. unsigned char interleave_sect;
  193. struct floppy_max_errors max_errors;
  194. char flags;
  195. char read_track;
  196. short autodetect[8];
  197. compat_int_t checkfreq;
  198. compat_int_t native_format;
  199. };
  200. struct compat_floppy_drive_struct {
  201. signed char flags;
  202. compat_ulong_t spinup_date;
  203. compat_ulong_t select_date;
  204. compat_ulong_t first_read_date;
  205. short probed_format;
  206. short track;
  207. short maxblock;
  208. short maxtrack;
  209. compat_int_t generation;
  210. compat_int_t keep_data;
  211. compat_int_t fd_ref;
  212. compat_int_t fd_device;
  213. compat_int_t last_checked;
  214. compat_caddr_t dmabuf;
  215. compat_int_t bufblocks;
  216. };
  217. struct compat_floppy_fdc_state {
  218. compat_int_t spec1;
  219. compat_int_t spec2;
  220. compat_int_t dtr;
  221. unsigned char version;
  222. unsigned char dor;
  223. compat_ulong_t address;
  224. unsigned int rawcmd:2;
  225. unsigned int reset:1;
  226. unsigned int need_configure:1;
  227. unsigned int perp_mode:2;
  228. unsigned int has_fifo:1;
  229. unsigned int driver_version;
  230. unsigned char track[4];
  231. };
  232. struct compat_floppy_write_errors {
  233. unsigned int write_errors;
  234. compat_ulong_t first_error_sector;
  235. compat_int_t first_error_generation;
  236. compat_ulong_t last_error_sector;
  237. compat_int_t last_error_generation;
  238. compat_uint_t badness;
  239. };
  240. #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
  241. #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
  242. #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
  243. #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
  244. #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
  245. #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
  246. #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
  247. #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
  248. static struct {
  249. unsigned int cmd32;
  250. unsigned int cmd;
  251. } fd_ioctl_trans_table[] = {
  252. { FDSETPRM32, FDSETPRM },
  253. { FDDEFPRM32, FDDEFPRM },
  254. { FDGETPRM32, FDGETPRM },
  255. { FDSETDRVPRM32, FDSETDRVPRM },
  256. { FDGETDRVPRM32, FDGETDRVPRM },
  257. { FDGETDRVSTAT32, FDGETDRVSTAT },
  258. { FDPOLLDRVSTAT32, FDPOLLDRVSTAT },
  259. { FDGETFDCSTAT32, FDGETFDCSTAT },
  260. { FDWERRORGET32, FDWERRORGET }
  261. };
  262. #define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table)
  263. static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode,
  264. unsigned int cmd, unsigned long arg)
  265. {
  266. mm_segment_t old_fs = get_fs();
  267. void *karg = NULL;
  268. unsigned int kcmd = 0;
  269. int i, err;
  270. for (i = 0; i < NR_FD_IOCTL_TRANS; i++)
  271. if (cmd == fd_ioctl_trans_table[i].cmd32) {
  272. kcmd = fd_ioctl_trans_table[i].cmd;
  273. break;
  274. }
  275. if (!kcmd)
  276. return -EINVAL;
  277. switch (cmd) {
  278. case FDSETPRM32:
  279. case FDDEFPRM32:
  280. case FDGETPRM32:
  281. {
  282. compat_uptr_t name;
  283. struct compat_floppy_struct __user *uf;
  284. struct floppy_struct *f;
  285. uf = compat_ptr(arg);
  286. f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL);
  287. if (!karg)
  288. return -ENOMEM;
  289. if (cmd == FDGETPRM32)
  290. break;
  291. err = __get_user(f->size, &uf->size);
  292. err |= __get_user(f->sect, &uf->sect);
  293. err |= __get_user(f->head, &uf->head);
  294. err |= __get_user(f->track, &uf->track);
  295. err |= __get_user(f->stretch, &uf->stretch);
  296. err |= __get_user(f->gap, &uf->gap);
  297. err |= __get_user(f->rate, &uf->rate);
  298. err |= __get_user(f->spec1, &uf->spec1);
  299. err |= __get_user(f->fmt_gap, &uf->fmt_gap);
  300. err |= __get_user(name, &uf->name);
  301. f->name = compat_ptr(name);
  302. if (err) {
  303. err = -EFAULT;
  304. goto out;
  305. }
  306. break;
  307. }
  308. case FDSETDRVPRM32:
  309. case FDGETDRVPRM32:
  310. {
  311. struct compat_floppy_drive_params __user *uf;
  312. struct floppy_drive_params *f;
  313. uf = compat_ptr(arg);
  314. f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL);
  315. if (!karg)
  316. return -ENOMEM;
  317. if (cmd == FDGETDRVPRM32)
  318. break;
  319. err = __get_user(f->cmos, &uf->cmos);
  320. err |= __get_user(f->max_dtr, &uf->max_dtr);
  321. err |= __get_user(f->hlt, &uf->hlt);
  322. err |= __get_user(f->hut, &uf->hut);
  323. err |= __get_user(f->srt, &uf->srt);
  324. err |= __get_user(f->spinup, &uf->spinup);
  325. err |= __get_user(f->spindown, &uf->spindown);
  326. err |= __get_user(f->spindown_offset, &uf->spindown_offset);
  327. err |= __get_user(f->select_delay, &uf->select_delay);
  328. err |= __get_user(f->rps, &uf->rps);
  329. err |= __get_user(f->tracks, &uf->tracks);
  330. err |= __get_user(f->timeout, &uf->timeout);
  331. err |= __get_user(f->interleave_sect, &uf->interleave_sect);
  332. err |= __copy_from_user(&f->max_errors, &uf->max_errors, sizeof(f->max_errors));
  333. err |= __get_user(f->flags, &uf->flags);
  334. err |= __get_user(f->read_track, &uf->read_track);
  335. err |= __copy_from_user(f->autodetect, uf->autodetect, sizeof(f->autodetect));
  336. err |= __get_user(f->checkfreq, &uf->checkfreq);
  337. err |= __get_user(f->native_format, &uf->native_format);
  338. if (err) {
  339. err = -EFAULT;
  340. goto out;
  341. }
  342. break;
  343. }
  344. case FDGETDRVSTAT32:
  345. case FDPOLLDRVSTAT32:
  346. karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL);
  347. if (!karg)
  348. return -ENOMEM;
  349. break;
  350. case FDGETFDCSTAT32:
  351. karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL);
  352. if (!karg)
  353. return -ENOMEM;
  354. break;
  355. case FDWERRORGET32:
  356. karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL);
  357. if (!karg)
  358. return -ENOMEM;
  359. break;
  360. default:
  361. return -EINVAL;
  362. }
  363. set_fs(KERNEL_DS);
  364. err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg);
  365. set_fs(old_fs);
  366. if (err)
  367. goto out;
  368. switch (cmd) {
  369. case FDGETPRM32:
  370. {
  371. struct floppy_struct *f = karg;
  372. struct compat_floppy_struct __user *uf = compat_ptr(arg);
  373. err = __put_user(f->size, &uf->size);
  374. err |= __put_user(f->sect, &uf->sect);
  375. err |= __put_user(f->head, &uf->head);
  376. err |= __put_user(f->track, &uf->track);
  377. err |= __put_user(f->stretch, &uf->stretch);
  378. err |= __put_user(f->gap, &uf->gap);
  379. err |= __put_user(f->rate, &uf->rate);
  380. err |= __put_user(f->spec1, &uf->spec1);
  381. err |= __put_user(f->fmt_gap, &uf->fmt_gap);
  382. err |= __put_user((u64)f->name, (compat_caddr_t __user *)&uf->name);
  383. break;
  384. }
  385. case FDGETDRVPRM32:
  386. {
  387. struct compat_floppy_drive_params __user *uf;
  388. struct floppy_drive_params *f = karg;
  389. uf = compat_ptr(arg);
  390. err = __put_user(f->cmos, &uf->cmos);
  391. err |= __put_user(f->max_dtr, &uf->max_dtr);
  392. err |= __put_user(f->hlt, &uf->hlt);
  393. err |= __put_user(f->hut, &uf->hut);
  394. err |= __put_user(f->srt, &uf->srt);
  395. err |= __put_user(f->spinup, &uf->spinup);
  396. err |= __put_user(f->spindown, &uf->spindown);
  397. err |= __put_user(f->spindown_offset, &uf->spindown_offset);
  398. err |= __put_user(f->select_delay, &uf->select_delay);
  399. err |= __put_user(f->rps, &uf->rps);
  400. err |= __put_user(f->tracks, &uf->tracks);
  401. err |= __put_user(f->timeout, &uf->timeout);
  402. err |= __put_user(f->interleave_sect, &uf->interleave_sect);
  403. err |= __copy_to_user(&uf->max_errors, &f->max_errors, sizeof(f->max_errors));
  404. err |= __put_user(f->flags, &uf->flags);
  405. err |= __put_user(f->read_track, &uf->read_track);
  406. err |= __copy_to_user(uf->autodetect, f->autodetect, sizeof(f->autodetect));
  407. err |= __put_user(f->checkfreq, &uf->checkfreq);
  408. err |= __put_user(f->native_format, &uf->native_format);
  409. break;
  410. }
  411. case FDGETDRVSTAT32:
  412. case FDPOLLDRVSTAT32:
  413. {
  414. struct compat_floppy_drive_struct __user *uf;
  415. struct floppy_drive_struct *f = karg;
  416. uf = compat_ptr(arg);
  417. err = __put_user(f->flags, &uf->flags);
  418. err |= __put_user(f->spinup_date, &uf->spinup_date);
  419. err |= __put_user(f->select_date, &uf->select_date);
  420. err |= __put_user(f->first_read_date, &uf->first_read_date);
  421. err |= __put_user(f->probed_format, &uf->probed_format);
  422. err |= __put_user(f->track, &uf->track);
  423. err |= __put_user(f->maxblock, &uf->maxblock);
  424. err |= __put_user(f->maxtrack, &uf->maxtrack);
  425. err |= __put_user(f->generation, &uf->generation);
  426. err |= __put_user(f->keep_data, &uf->keep_data);
  427. err |= __put_user(f->fd_ref, &uf->fd_ref);
  428. err |= __put_user(f->fd_device, &uf->fd_device);
  429. err |= __put_user(f->last_checked, &uf->last_checked);
  430. err |= __put_user((u64)f->dmabuf, &uf->dmabuf);
  431. err |= __put_user((u64)f->bufblocks, &uf->bufblocks);
  432. break;
  433. }
  434. case FDGETFDCSTAT32:
  435. {
  436. struct compat_floppy_fdc_state __user *uf;
  437. struct floppy_fdc_state *f = karg;
  438. uf = compat_ptr(arg);
  439. err = __put_user(f->spec1, &uf->spec1);
  440. err |= __put_user(f->spec2, &uf->spec2);
  441. err |= __put_user(f->dtr, &uf->dtr);
  442. err |= __put_user(f->version, &uf->version);
  443. err |= __put_user(f->dor, &uf->dor);
  444. err |= __put_user(f->address, &uf->address);
  445. err |= __copy_to_user((char __user *)&uf->address + sizeof(uf->address),
  446. (char *)&f->address + sizeof(f->address), sizeof(int));
  447. err |= __put_user(f->driver_version, &uf->driver_version);
  448. err |= __copy_to_user(uf->track, f->track, sizeof(f->track));
  449. break;
  450. }
  451. case FDWERRORGET32:
  452. {
  453. struct compat_floppy_write_errors __user *uf;
  454. struct floppy_write_errors *f = karg;
  455. uf = compat_ptr(arg);
  456. err = __put_user(f->write_errors, &uf->write_errors);
  457. err |= __put_user(f->first_error_sector, &uf->first_error_sector);
  458. err |= __put_user(f->first_error_generation, &uf->first_error_generation);
  459. err |= __put_user(f->last_error_sector, &uf->last_error_sector);
  460. err |= __put_user(f->last_error_generation, &uf->last_error_generation);
  461. err |= __put_user(f->badness, &uf->badness);
  462. break;
  463. }
  464. default:
  465. break;
  466. }
  467. if (err)
  468. err = -EFAULT;
  469. out:
  470. kfree(karg);
  471. return err;
  472. }
  473. static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
  474. unsigned cmd, unsigned long arg)
  475. {
  476. switch (cmd) {
  477. case HDIO_GET_UNMASKINTR:
  478. case HDIO_GET_MULTCOUNT:
  479. case HDIO_GET_KEEPSETTINGS:
  480. case HDIO_GET_32BIT:
  481. case HDIO_GET_NOWERR:
  482. case HDIO_GET_DMA:
  483. case HDIO_GET_NICE:
  484. case HDIO_GET_WCACHE:
  485. case HDIO_GET_ACOUSTIC:
  486. case HDIO_GET_ADDRESS:
  487. case HDIO_GET_BUSSTATE:
  488. return compat_hdio_ioctl(bdev, mode, cmd, arg);
  489. case FDSETPRM32:
  490. case FDDEFPRM32:
  491. case FDGETPRM32:
  492. case FDSETDRVPRM32:
  493. case FDGETDRVPRM32:
  494. case FDGETDRVSTAT32:
  495. case FDPOLLDRVSTAT32:
  496. case FDGETFDCSTAT32:
  497. case FDWERRORGET32:
  498. return compat_fd_ioctl(bdev, mode, cmd, arg);
  499. case CDROMREADAUDIO:
  500. return compat_cdrom_read_audio(bdev, mode, cmd, arg);
  501. case CDROM_SEND_PACKET:
  502. return compat_cdrom_generic_command(bdev, mode, cmd, arg);
  503. /*
  504. * No handler required for the ones below, we just need to
  505. * convert arg to a 64 bit pointer.
  506. */
  507. case BLKSECTSET:
  508. /*
  509. * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
  510. * Some need translations, these do not.
  511. */
  512. case HDIO_GET_IDENTITY:
  513. case HDIO_DRIVE_TASK:
  514. case HDIO_DRIVE_CMD:
  515. /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
  516. case 0x330:
  517. /* 0x02 -- Floppy ioctls */
  518. case FDMSGON:
  519. case FDMSGOFF:
  520. case FDSETEMSGTRESH:
  521. case FDFLUSH:
  522. case FDWERRORCLR:
  523. case FDSETMAXERRS:
  524. case FDGETMAXERRS:
  525. case FDGETDRVTYP:
  526. case FDEJECT:
  527. case FDCLRPRM:
  528. case FDFMTBEG:
  529. case FDFMTEND:
  530. case FDRESET:
  531. case FDTWADDLE:
  532. case FDFMTTRK:
  533. case FDRAWCMD:
  534. /* CDROM stuff */
  535. case CDROMPAUSE:
  536. case CDROMRESUME:
  537. case CDROMPLAYMSF:
  538. case CDROMPLAYTRKIND:
  539. case CDROMREADTOCHDR:
  540. case CDROMREADTOCENTRY:
  541. case CDROMSTOP:
  542. case CDROMSTART:
  543. case CDROMEJECT:
  544. case CDROMVOLCTRL:
  545. case CDROMSUBCHNL:
  546. case CDROMMULTISESSION:
  547. case CDROM_GET_MCN:
  548. case CDROMRESET:
  549. case CDROMVOLREAD:
  550. case CDROMSEEK:
  551. case CDROMPLAYBLK:
  552. case CDROMCLOSETRAY:
  553. case CDROM_DISC_STATUS:
  554. case CDROM_CHANGER_NSLOTS:
  555. case CDROM_GET_CAPABILITY:
  556. /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
  557. * not take a struct cdrom_read, instead they take a struct cdrom_msf
  558. * which is compatible.
  559. */
  560. case CDROMREADMODE2:
  561. case CDROMREADMODE1:
  562. case CDROMREADRAW:
  563. case CDROMREADCOOKED:
  564. case CDROMREADALL:
  565. /* DVD ioctls */
  566. case DVD_READ_STRUCT:
  567. case DVD_WRITE_STRUCT:
  568. case DVD_AUTH:
  569. arg = (unsigned long)compat_ptr(arg);
  570. /* These intepret arg as an unsigned long, not as a pointer,
  571. * so we must not do compat_ptr() conversion. */
  572. case HDIO_SET_MULTCOUNT:
  573. case HDIO_SET_UNMASKINTR:
  574. case HDIO_SET_KEEPSETTINGS:
  575. case HDIO_SET_32BIT:
  576. case HDIO_SET_NOWERR:
  577. case HDIO_SET_DMA:
  578. case HDIO_SET_PIO_MODE:
  579. case HDIO_SET_NICE:
  580. case HDIO_SET_WCACHE:
  581. case HDIO_SET_ACOUSTIC:
  582. case HDIO_SET_BUSSTATE:
  583. case HDIO_SET_ADDRESS:
  584. case CDROMEJECT_SW:
  585. case CDROM_SET_OPTIONS:
  586. case CDROM_CLEAR_OPTIONS:
  587. case CDROM_SELECT_SPEED:
  588. case CDROM_SELECT_DISC:
  589. case CDROM_MEDIA_CHANGED:
  590. case CDROM_DRIVE_STATUS:
  591. case CDROM_LOCKDOOR:
  592. case CDROM_DEBUG:
  593. break;
  594. default:
  595. /* unknown ioctl number */
  596. return -ENOIOCTLCMD;
  597. }
  598. return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  599. }
  600. /* Most of the generic ioctls are handled in the normal fallback path.
  601. This assumes the blkdev's low level compat_ioctl always returns
  602. ENOIOCTLCMD for unknown ioctls. */
  603. long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  604. {
  605. int ret = -ENOIOCTLCMD;
  606. struct inode *inode = file->f_mapping->host;
  607. struct block_device *bdev = inode->i_bdev;
  608. struct gendisk *disk = bdev->bd_disk;
  609. fmode_t mode = file->f_mode;
  610. struct backing_dev_info *bdi;
  611. loff_t size;
  612. /*
  613. * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
  614. * to updated it before every ioctl.
  615. */
  616. if (file->f_flags & O_NDELAY)
  617. mode |= FMODE_NDELAY;
  618. else
  619. mode &= ~FMODE_NDELAY;
  620. switch (cmd) {
  621. case HDIO_GETGEO:
  622. return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
  623. case BLKPBSZGET:
  624. return compat_put_uint(arg, bdev_physical_block_size(bdev));
  625. case BLKIOMIN:
  626. return compat_put_uint(arg, bdev_io_min(bdev));
  627. case BLKIOOPT:
  628. return compat_put_uint(arg, bdev_io_opt(bdev));
  629. case BLKALIGNOFF:
  630. return compat_put_int(arg, bdev_alignment_offset(bdev));
  631. case BLKDISCARDZEROES:
  632. return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
  633. case BLKFLSBUF:
  634. case BLKROSET:
  635. case BLKDISCARD:
  636. case BLKSECDISCARD:
  637. /*
  638. * the ones below are implemented in blkdev_locked_ioctl,
  639. * but we call blkdev_ioctl, which gets the lock for us
  640. */
  641. case BLKRRPART:
  642. return blkdev_ioctl(bdev, mode, cmd,
  643. (unsigned long)compat_ptr(arg));
  644. case BLKBSZSET_32:
  645. return blkdev_ioctl(bdev, mode, BLKBSZSET,
  646. (unsigned long)compat_ptr(arg));
  647. case BLKPG:
  648. return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
  649. case BLKRAGET:
  650. case BLKFRAGET:
  651. if (!arg)
  652. return -EINVAL;
  653. bdi = blk_get_backing_dev_info(bdev);
  654. if (bdi == NULL)
  655. return -ENOTTY;
  656. return compat_put_long(arg,
  657. (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  658. case BLKROGET: /* compatible */
  659. return compat_put_int(arg, bdev_read_only(bdev) != 0);
  660. case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
  661. return compat_put_int(arg, block_size(bdev));
  662. case BLKSSZGET: /* get block device hardware sector size */
  663. return compat_put_int(arg, bdev_logical_block_size(bdev));
  664. case BLKSECTGET:
  665. return compat_put_ushort(arg,
  666. queue_max_sectors(bdev_get_queue(bdev)));
  667. case BLKROTATIONAL:
  668. return compat_put_ushort(arg,
  669. !blk_queue_nonrot(bdev_get_queue(bdev)));
  670. case BLKRASET: /* compatible, but no compat_ptr (!) */
  671. case BLKFRASET:
  672. if (!capable(CAP_SYS_ADMIN))
  673. return -EACCES;
  674. bdi = blk_get_backing_dev_info(bdev);
  675. if (bdi == NULL)
  676. return -ENOTTY;
  677. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  678. return 0;
  679. case BLKGETSIZE:
  680. size = i_size_read(bdev->bd_inode);
  681. if ((size >> 9) > ~0UL)
  682. return -EFBIG;
  683. return compat_put_ulong(arg, size >> 9);
  684. case BLKGETSIZE64_32:
  685. return compat_put_u64(arg, i_size_read(bdev->bd_inode));
  686. case BLKTRACESETUP32:
  687. case BLKTRACESTART: /* compatible */
  688. case BLKTRACESTOP: /* compatible */
  689. case BLKTRACETEARDOWN: /* compatible */
  690. ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
  691. return ret;
  692. default:
  693. if (disk->fops->compat_ioctl)
  694. ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
  695. if (ret == -ENOIOCTLCMD)
  696. ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
  697. return ret;
  698. }
  699. }