compat_ioctl.c 21 KB

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