ioctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * linux/fs/ext4/ioctl.c
  3. *
  4. * Copyright (C) 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/capability.h>
  11. #include <linux/time.h>
  12. #include <linux/compat.h>
  13. #include <linux/mount.h>
  14. #include <linux/file.h>
  15. #include <linux/quotaops.h>
  16. #include <linux/uuid.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/delay.h>
  19. #include "ext4_jbd2.h"
  20. #include "ext4.h"
  21. #include <linux/fsmap.h>
  22. #include "fsmap.h"
  23. #include <trace/events/ext4.h>
  24. /**
  25. * Swap memory between @a and @b for @len bytes.
  26. *
  27. * @a: pointer to first memory area
  28. * @b: pointer to second memory area
  29. * @len: number of bytes to swap
  30. *
  31. */
  32. static void memswap(void *a, void *b, size_t len)
  33. {
  34. unsigned char *ap, *bp;
  35. ap = (unsigned char *)a;
  36. bp = (unsigned char *)b;
  37. while (len-- > 0) {
  38. swap(*ap, *bp);
  39. ap++;
  40. bp++;
  41. }
  42. }
  43. /**
  44. * Swap i_data and associated attributes between @inode1 and @inode2.
  45. * This function is used for the primary swap between inode1 and inode2
  46. * and also to revert this primary swap in case of errors.
  47. *
  48. * Therefore you have to make sure, that calling this method twice
  49. * will revert all changes.
  50. *
  51. * @inode1: pointer to first inode
  52. * @inode2: pointer to second inode
  53. */
  54. static void swap_inode_data(struct inode *inode1, struct inode *inode2)
  55. {
  56. loff_t isize;
  57. struct ext4_inode_info *ei1;
  58. struct ext4_inode_info *ei2;
  59. ei1 = EXT4_I(inode1);
  60. ei2 = EXT4_I(inode2);
  61. swap(inode1->i_flags, inode2->i_flags);
  62. swap(inode1->i_version, inode2->i_version);
  63. swap(inode1->i_blocks, inode2->i_blocks);
  64. swap(inode1->i_bytes, inode2->i_bytes);
  65. swap(inode1->i_atime, inode2->i_atime);
  66. swap(inode1->i_mtime, inode2->i_mtime);
  67. memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
  68. swap(ei1->i_flags, ei2->i_flags);
  69. swap(ei1->i_disksize, ei2->i_disksize);
  70. ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
  71. ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
  72. isize = i_size_read(inode1);
  73. i_size_write(inode1, i_size_read(inode2));
  74. i_size_write(inode2, isize);
  75. }
  76. /**
  77. * Swap the information from the given @inode and the inode
  78. * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
  79. * important fields of the inodes.
  80. *
  81. * @sb: the super block of the filesystem
  82. * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
  83. *
  84. */
  85. static long swap_inode_boot_loader(struct super_block *sb,
  86. struct inode *inode)
  87. {
  88. handle_t *handle;
  89. int err;
  90. struct inode *inode_bl;
  91. struct ext4_inode_info *ei_bl;
  92. struct ext4_sb_info *sbi = EXT4_SB(sb);
  93. if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
  94. return -EINVAL;
  95. if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
  96. return -EPERM;
  97. inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
  98. if (IS_ERR(inode_bl))
  99. return PTR_ERR(inode_bl);
  100. ei_bl = EXT4_I(inode_bl);
  101. filemap_flush(inode->i_mapping);
  102. filemap_flush(inode_bl->i_mapping);
  103. /* Protect orig inodes against a truncate and make sure,
  104. * that only 1 swap_inode_boot_loader is running. */
  105. lock_two_nondirectories(inode, inode_bl);
  106. truncate_inode_pages(&inode->i_data, 0);
  107. truncate_inode_pages(&inode_bl->i_data, 0);
  108. /* Wait for all existing dio workers */
  109. ext4_inode_block_unlocked_dio(inode);
  110. ext4_inode_block_unlocked_dio(inode_bl);
  111. inode_dio_wait(inode);
  112. inode_dio_wait(inode_bl);
  113. handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
  114. if (IS_ERR(handle)) {
  115. err = -EINVAL;
  116. goto journal_err_out;
  117. }
  118. /* Protect extent tree against block allocations via delalloc */
  119. ext4_double_down_write_data_sem(inode, inode_bl);
  120. if (inode_bl->i_nlink == 0) {
  121. /* this inode has never been used as a BOOT_LOADER */
  122. set_nlink(inode_bl, 1);
  123. i_uid_write(inode_bl, 0);
  124. i_gid_write(inode_bl, 0);
  125. inode_bl->i_flags = 0;
  126. ei_bl->i_flags = 0;
  127. inode_bl->i_version = 1;
  128. i_size_write(inode_bl, 0);
  129. inode_bl->i_mode = S_IFREG;
  130. if (ext4_has_feature_extents(sb)) {
  131. ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
  132. ext4_ext_tree_init(handle, inode_bl);
  133. } else
  134. memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
  135. }
  136. swap_inode_data(inode, inode_bl);
  137. inode->i_ctime = inode_bl->i_ctime = current_time(inode);
  138. spin_lock(&sbi->s_next_gen_lock);
  139. inode->i_generation = sbi->s_next_generation++;
  140. inode_bl->i_generation = sbi->s_next_generation++;
  141. spin_unlock(&sbi->s_next_gen_lock);
  142. ext4_discard_preallocations(inode);
  143. err = ext4_mark_inode_dirty(handle, inode);
  144. if (err < 0) {
  145. ext4_warning(inode->i_sb,
  146. "couldn't mark inode #%lu dirty (err %d)",
  147. inode->i_ino, err);
  148. /* Revert all changes: */
  149. swap_inode_data(inode, inode_bl);
  150. } else {
  151. err = ext4_mark_inode_dirty(handle, inode_bl);
  152. if (err < 0) {
  153. ext4_warning(inode_bl->i_sb,
  154. "couldn't mark inode #%lu dirty (err %d)",
  155. inode_bl->i_ino, err);
  156. /* Revert all changes: */
  157. swap_inode_data(inode, inode_bl);
  158. ext4_mark_inode_dirty(handle, inode);
  159. }
  160. }
  161. ext4_journal_stop(handle);
  162. ext4_double_up_write_data_sem(inode, inode_bl);
  163. journal_err_out:
  164. ext4_inode_resume_unlocked_dio(inode);
  165. ext4_inode_resume_unlocked_dio(inode_bl);
  166. unlock_two_nondirectories(inode, inode_bl);
  167. iput(inode_bl);
  168. return err;
  169. }
  170. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  171. static int uuid_is_zero(__u8 u[16])
  172. {
  173. int i;
  174. for (i = 0; i < 16; i++)
  175. if (u[i])
  176. return 0;
  177. return 1;
  178. }
  179. #endif
  180. static int ext4_ioctl_setflags(struct inode *inode,
  181. unsigned int flags)
  182. {
  183. struct ext4_inode_info *ei = EXT4_I(inode);
  184. handle_t *handle = NULL;
  185. int err = -EPERM, migrate = 0;
  186. struct ext4_iloc iloc;
  187. unsigned int oldflags, mask, i;
  188. unsigned int jflag;
  189. /* Is it quota file? Do not allow user to mess with it */
  190. if (ext4_is_quota_file(inode))
  191. goto flags_out;
  192. oldflags = ei->i_flags;
  193. /* The JOURNAL_DATA flag is modifiable only by root */
  194. jflag = flags & EXT4_JOURNAL_DATA_FL;
  195. /*
  196. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  197. * the relevant capability.
  198. *
  199. * This test looks nicer. Thanks to Pauline Middelink
  200. */
  201. if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
  202. if (!capable(CAP_LINUX_IMMUTABLE))
  203. goto flags_out;
  204. }
  205. /*
  206. * The JOURNAL_DATA flag can only be changed by
  207. * the relevant capability.
  208. */
  209. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
  210. if (!capable(CAP_SYS_RESOURCE))
  211. goto flags_out;
  212. }
  213. if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
  214. migrate = 1;
  215. if (flags & EXT4_EOFBLOCKS_FL) {
  216. /* we don't support adding EOFBLOCKS flag */
  217. if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
  218. err = -EOPNOTSUPP;
  219. goto flags_out;
  220. }
  221. } else if (oldflags & EXT4_EOFBLOCKS_FL) {
  222. err = ext4_truncate(inode);
  223. if (err)
  224. goto flags_out;
  225. }
  226. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  227. if (IS_ERR(handle)) {
  228. err = PTR_ERR(handle);
  229. goto flags_out;
  230. }
  231. if (IS_SYNC(inode))
  232. ext4_handle_sync(handle);
  233. err = ext4_reserve_inode_write(handle, inode, &iloc);
  234. if (err)
  235. goto flags_err;
  236. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  237. if (!(mask & EXT4_FL_USER_MODIFIABLE))
  238. continue;
  239. /* These flags get special treatment later */
  240. if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
  241. continue;
  242. if (mask & flags)
  243. ext4_set_inode_flag(inode, i);
  244. else
  245. ext4_clear_inode_flag(inode, i);
  246. }
  247. ext4_set_inode_flags(inode);
  248. inode->i_ctime = current_time(inode);
  249. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  250. flags_err:
  251. ext4_journal_stop(handle);
  252. if (err)
  253. goto flags_out;
  254. if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
  255. err = ext4_change_inode_journal_flag(inode, jflag);
  256. if (err)
  257. goto flags_out;
  258. if (migrate) {
  259. if (flags & EXT4_EXTENTS_FL)
  260. err = ext4_ext_migrate(inode);
  261. else
  262. err = ext4_ind_migrate(inode);
  263. }
  264. flags_out:
  265. return err;
  266. }
  267. #ifdef CONFIG_QUOTA
  268. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  269. {
  270. struct inode *inode = file_inode(filp);
  271. struct super_block *sb = inode->i_sb;
  272. struct ext4_inode_info *ei = EXT4_I(inode);
  273. int err, rc;
  274. handle_t *handle;
  275. kprojid_t kprojid;
  276. struct ext4_iloc iloc;
  277. struct ext4_inode *raw_inode;
  278. struct dquot *transfer_to[MAXQUOTAS] = { };
  279. if (!ext4_has_feature_project(sb)) {
  280. if (projid != EXT4_DEF_PROJID)
  281. return -EOPNOTSUPP;
  282. else
  283. return 0;
  284. }
  285. if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
  286. return -EOPNOTSUPP;
  287. kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
  288. if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
  289. return 0;
  290. err = mnt_want_write_file(filp);
  291. if (err)
  292. return err;
  293. err = -EPERM;
  294. inode_lock(inode);
  295. /* Is it quota file? Do not allow user to mess with it */
  296. if (ext4_is_quota_file(inode))
  297. goto out_unlock;
  298. err = ext4_get_inode_loc(inode, &iloc);
  299. if (err)
  300. goto out_unlock;
  301. raw_inode = ext4_raw_inode(&iloc);
  302. if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
  303. err = ext4_expand_extra_isize(inode,
  304. EXT4_SB(sb)->s_want_extra_isize,
  305. &iloc);
  306. if (err)
  307. goto out_unlock;
  308. } else {
  309. brelse(iloc.bh);
  310. }
  311. dquot_initialize(inode);
  312. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  313. EXT4_QUOTA_INIT_BLOCKS(sb) +
  314. EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
  315. if (IS_ERR(handle)) {
  316. err = PTR_ERR(handle);
  317. goto out_unlock;
  318. }
  319. err = ext4_reserve_inode_write(handle, inode, &iloc);
  320. if (err)
  321. goto out_stop;
  322. transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
  323. if (!IS_ERR(transfer_to[PRJQUOTA])) {
  324. /* __dquot_transfer() calls back ext4_get_inode_usage() which
  325. * counts xattr inode references.
  326. */
  327. down_read(&EXT4_I(inode)->xattr_sem);
  328. err = __dquot_transfer(inode, transfer_to);
  329. up_read(&EXT4_I(inode)->xattr_sem);
  330. dqput(transfer_to[PRJQUOTA]);
  331. if (err)
  332. goto out_dirty;
  333. }
  334. EXT4_I(inode)->i_projid = kprojid;
  335. inode->i_ctime = current_time(inode);
  336. out_dirty:
  337. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  338. if (!err)
  339. err = rc;
  340. out_stop:
  341. ext4_journal_stop(handle);
  342. out_unlock:
  343. inode_unlock(inode);
  344. mnt_drop_write_file(filp);
  345. return err;
  346. }
  347. #else
  348. static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
  349. {
  350. if (projid != EXT4_DEF_PROJID)
  351. return -EOPNOTSUPP;
  352. return 0;
  353. }
  354. #endif
  355. /* Transfer internal flags to xflags */
  356. static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
  357. {
  358. __u32 xflags = 0;
  359. if (iflags & EXT4_SYNC_FL)
  360. xflags |= FS_XFLAG_SYNC;
  361. if (iflags & EXT4_IMMUTABLE_FL)
  362. xflags |= FS_XFLAG_IMMUTABLE;
  363. if (iflags & EXT4_APPEND_FL)
  364. xflags |= FS_XFLAG_APPEND;
  365. if (iflags & EXT4_NODUMP_FL)
  366. xflags |= FS_XFLAG_NODUMP;
  367. if (iflags & EXT4_NOATIME_FL)
  368. xflags |= FS_XFLAG_NOATIME;
  369. if (iflags & EXT4_PROJINHERIT_FL)
  370. xflags |= FS_XFLAG_PROJINHERIT;
  371. return xflags;
  372. }
  373. #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
  374. FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
  375. FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
  376. /* Transfer xflags flags to internal */
  377. static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
  378. {
  379. unsigned long iflags = 0;
  380. if (xflags & FS_XFLAG_SYNC)
  381. iflags |= EXT4_SYNC_FL;
  382. if (xflags & FS_XFLAG_IMMUTABLE)
  383. iflags |= EXT4_IMMUTABLE_FL;
  384. if (xflags & FS_XFLAG_APPEND)
  385. iflags |= EXT4_APPEND_FL;
  386. if (xflags & FS_XFLAG_NODUMP)
  387. iflags |= EXT4_NODUMP_FL;
  388. if (xflags & FS_XFLAG_NOATIME)
  389. iflags |= EXT4_NOATIME_FL;
  390. if (xflags & FS_XFLAG_PROJINHERIT)
  391. iflags |= EXT4_PROJINHERIT_FL;
  392. return iflags;
  393. }
  394. static int ext4_shutdown(struct super_block *sb, unsigned long arg)
  395. {
  396. struct ext4_sb_info *sbi = EXT4_SB(sb);
  397. __u32 flags;
  398. if (!capable(CAP_SYS_ADMIN))
  399. return -EPERM;
  400. if (get_user(flags, (__u32 __user *)arg))
  401. return -EFAULT;
  402. if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
  403. return -EINVAL;
  404. if (ext4_forced_shutdown(sbi))
  405. return 0;
  406. ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
  407. switch (flags) {
  408. case EXT4_GOING_FLAGS_DEFAULT:
  409. freeze_bdev(sb->s_bdev);
  410. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  411. thaw_bdev(sb->s_bdev, sb);
  412. break;
  413. case EXT4_GOING_FLAGS_LOGFLUSH:
  414. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  415. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
  416. (void) ext4_force_commit(sb);
  417. jbd2_journal_abort(sbi->s_journal, 0);
  418. }
  419. break;
  420. case EXT4_GOING_FLAGS_NOLOGFLUSH:
  421. set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
  422. if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
  423. msleep(100);
  424. jbd2_journal_abort(sbi->s_journal, 0);
  425. }
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. clear_opt(sb, DISCARD);
  431. return 0;
  432. }
  433. struct getfsmap_info {
  434. struct super_block *gi_sb;
  435. struct fsmap_head __user *gi_data;
  436. unsigned int gi_idx;
  437. __u32 gi_last_flags;
  438. };
  439. static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
  440. {
  441. struct getfsmap_info *info = priv;
  442. struct fsmap fm;
  443. trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
  444. info->gi_last_flags = xfm->fmr_flags;
  445. ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
  446. if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
  447. sizeof(struct fsmap)))
  448. return -EFAULT;
  449. return 0;
  450. }
  451. static int ext4_ioc_getfsmap(struct super_block *sb,
  452. struct fsmap_head __user *arg)
  453. {
  454. struct getfsmap_info info = {0};
  455. struct ext4_fsmap_head xhead = {0};
  456. struct fsmap_head head;
  457. bool aborted = false;
  458. int error;
  459. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  460. return -EFAULT;
  461. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  462. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  463. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  464. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  465. sizeof(head.fmh_keys[1].fmr_reserved)))
  466. return -EINVAL;
  467. /*
  468. * ext4 doesn't report file extents at all, so the only valid
  469. * file offsets are the magic ones (all zeroes or all ones).
  470. */
  471. if (head.fmh_keys[0].fmr_offset ||
  472. (head.fmh_keys[1].fmr_offset != 0 &&
  473. head.fmh_keys[1].fmr_offset != -1ULL))
  474. return -EINVAL;
  475. xhead.fmh_iflags = head.fmh_iflags;
  476. xhead.fmh_count = head.fmh_count;
  477. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
  478. ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
  479. trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
  480. trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
  481. info.gi_sb = sb;
  482. info.gi_data = arg;
  483. error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
  484. if (error == EXT4_QUERY_RANGE_ABORT) {
  485. error = 0;
  486. aborted = true;
  487. } else if (error)
  488. return error;
  489. /* If we didn't abort, set the "last" flag in the last fmx */
  490. if (!aborted && info.gi_idx) {
  491. info.gi_last_flags |= FMR_OF_LAST;
  492. if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
  493. &info.gi_last_flags,
  494. sizeof(info.gi_last_flags)))
  495. return -EFAULT;
  496. }
  497. /* copy back header */
  498. head.fmh_entries = xhead.fmh_entries;
  499. head.fmh_oflags = xhead.fmh_oflags;
  500. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  501. return -EFAULT;
  502. return 0;
  503. }
  504. long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  505. {
  506. struct inode *inode = file_inode(filp);
  507. struct super_block *sb = inode->i_sb;
  508. struct ext4_inode_info *ei = EXT4_I(inode);
  509. unsigned int flags;
  510. ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
  511. switch (cmd) {
  512. case FS_IOC_GETFSMAP:
  513. return ext4_ioc_getfsmap(sb, (void __user *)arg);
  514. case EXT4_IOC_GETFLAGS:
  515. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  516. return put_user(flags, (int __user *) arg);
  517. case EXT4_IOC_SETFLAGS: {
  518. int err;
  519. if (!inode_owner_or_capable(inode))
  520. return -EACCES;
  521. if (get_user(flags, (int __user *) arg))
  522. return -EFAULT;
  523. if (flags & ~EXT4_FL_USER_VISIBLE)
  524. return -EOPNOTSUPP;
  525. /*
  526. * chattr(1) grabs flags via GETFLAGS, modifies the result and
  527. * passes that to SETFLAGS. So we cannot easily make SETFLAGS
  528. * more restrictive than just silently masking off visible but
  529. * not settable flags as we always did.
  530. */
  531. flags &= EXT4_FL_USER_MODIFIABLE;
  532. if (ext4_mask_flags(inode->i_mode, flags) != flags)
  533. return -EOPNOTSUPP;
  534. err = mnt_want_write_file(filp);
  535. if (err)
  536. return err;
  537. inode_lock(inode);
  538. err = ext4_ioctl_setflags(inode, flags);
  539. inode_unlock(inode);
  540. mnt_drop_write_file(filp);
  541. return err;
  542. }
  543. case EXT4_IOC_GETVERSION:
  544. case EXT4_IOC_GETVERSION_OLD:
  545. return put_user(inode->i_generation, (int __user *) arg);
  546. case EXT4_IOC_SETVERSION:
  547. case EXT4_IOC_SETVERSION_OLD: {
  548. handle_t *handle;
  549. struct ext4_iloc iloc;
  550. __u32 generation;
  551. int err;
  552. if (!inode_owner_or_capable(inode))
  553. return -EPERM;
  554. if (ext4_has_metadata_csum(inode->i_sb)) {
  555. ext4_warning(sb, "Setting inode version is not "
  556. "supported with metadata_csum enabled.");
  557. return -ENOTTY;
  558. }
  559. err = mnt_want_write_file(filp);
  560. if (err)
  561. return err;
  562. if (get_user(generation, (int __user *) arg)) {
  563. err = -EFAULT;
  564. goto setversion_out;
  565. }
  566. inode_lock(inode);
  567. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  568. if (IS_ERR(handle)) {
  569. err = PTR_ERR(handle);
  570. goto unlock_out;
  571. }
  572. err = ext4_reserve_inode_write(handle, inode, &iloc);
  573. if (err == 0) {
  574. inode->i_ctime = current_time(inode);
  575. inode->i_generation = generation;
  576. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  577. }
  578. ext4_journal_stop(handle);
  579. unlock_out:
  580. inode_unlock(inode);
  581. setversion_out:
  582. mnt_drop_write_file(filp);
  583. return err;
  584. }
  585. case EXT4_IOC_GROUP_EXTEND: {
  586. ext4_fsblk_t n_blocks_count;
  587. int err, err2=0;
  588. err = ext4_resize_begin(sb);
  589. if (err)
  590. return err;
  591. if (get_user(n_blocks_count, (__u32 __user *)arg)) {
  592. err = -EFAULT;
  593. goto group_extend_out;
  594. }
  595. if (ext4_has_feature_bigalloc(sb)) {
  596. ext4_msg(sb, KERN_ERR,
  597. "Online resizing not supported with bigalloc");
  598. err = -EOPNOTSUPP;
  599. goto group_extend_out;
  600. }
  601. err = mnt_want_write_file(filp);
  602. if (err)
  603. goto group_extend_out;
  604. err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
  605. if (EXT4_SB(sb)->s_journal) {
  606. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  607. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  608. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  609. }
  610. if (err == 0)
  611. err = err2;
  612. mnt_drop_write_file(filp);
  613. group_extend_out:
  614. ext4_resize_end(sb);
  615. return err;
  616. }
  617. case EXT4_IOC_MOVE_EXT: {
  618. struct move_extent me;
  619. struct fd donor;
  620. int err;
  621. if (!(filp->f_mode & FMODE_READ) ||
  622. !(filp->f_mode & FMODE_WRITE))
  623. return -EBADF;
  624. if (copy_from_user(&me,
  625. (struct move_extent __user *)arg, sizeof(me)))
  626. return -EFAULT;
  627. me.moved_len = 0;
  628. donor = fdget(me.donor_fd);
  629. if (!donor.file)
  630. return -EBADF;
  631. if (!(donor.file->f_mode & FMODE_WRITE)) {
  632. err = -EBADF;
  633. goto mext_out;
  634. }
  635. if (ext4_has_feature_bigalloc(sb)) {
  636. ext4_msg(sb, KERN_ERR,
  637. "Online defrag not supported with bigalloc");
  638. err = -EOPNOTSUPP;
  639. goto mext_out;
  640. } else if (IS_DAX(inode)) {
  641. ext4_msg(sb, KERN_ERR,
  642. "Online defrag not supported with DAX");
  643. err = -EOPNOTSUPP;
  644. goto mext_out;
  645. }
  646. err = mnt_want_write_file(filp);
  647. if (err)
  648. goto mext_out;
  649. err = ext4_move_extents(filp, donor.file, me.orig_start,
  650. me.donor_start, me.len, &me.moved_len);
  651. mnt_drop_write_file(filp);
  652. if (copy_to_user((struct move_extent __user *)arg,
  653. &me, sizeof(me)))
  654. err = -EFAULT;
  655. mext_out:
  656. fdput(donor);
  657. return err;
  658. }
  659. case EXT4_IOC_GROUP_ADD: {
  660. struct ext4_new_group_data input;
  661. int err, err2=0;
  662. err = ext4_resize_begin(sb);
  663. if (err)
  664. return err;
  665. if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
  666. sizeof(input))) {
  667. err = -EFAULT;
  668. goto group_add_out;
  669. }
  670. if (ext4_has_feature_bigalloc(sb)) {
  671. ext4_msg(sb, KERN_ERR,
  672. "Online resizing not supported with bigalloc");
  673. err = -EOPNOTSUPP;
  674. goto group_add_out;
  675. }
  676. err = mnt_want_write_file(filp);
  677. if (err)
  678. goto group_add_out;
  679. err = ext4_group_add(sb, &input);
  680. if (EXT4_SB(sb)->s_journal) {
  681. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  682. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  683. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  684. }
  685. if (err == 0)
  686. err = err2;
  687. mnt_drop_write_file(filp);
  688. if (!err && ext4_has_group_desc_csum(sb) &&
  689. test_opt(sb, INIT_INODE_TABLE))
  690. err = ext4_register_li_request(sb, input.group);
  691. group_add_out:
  692. ext4_resize_end(sb);
  693. return err;
  694. }
  695. case EXT4_IOC_MIGRATE:
  696. {
  697. int err;
  698. if (!inode_owner_or_capable(inode))
  699. return -EACCES;
  700. err = mnt_want_write_file(filp);
  701. if (err)
  702. return err;
  703. /*
  704. * inode_mutex prevent write and truncate on the file.
  705. * Read still goes through. We take i_data_sem in
  706. * ext4_ext_swap_inode_data before we switch the
  707. * inode format to prevent read.
  708. */
  709. inode_lock((inode));
  710. err = ext4_ext_migrate(inode);
  711. inode_unlock((inode));
  712. mnt_drop_write_file(filp);
  713. return err;
  714. }
  715. case EXT4_IOC_ALLOC_DA_BLKS:
  716. {
  717. int err;
  718. if (!inode_owner_or_capable(inode))
  719. return -EACCES;
  720. err = mnt_want_write_file(filp);
  721. if (err)
  722. return err;
  723. err = ext4_alloc_da_blocks(inode);
  724. mnt_drop_write_file(filp);
  725. return err;
  726. }
  727. case EXT4_IOC_SWAP_BOOT:
  728. {
  729. int err;
  730. if (!(filp->f_mode & FMODE_WRITE))
  731. return -EBADF;
  732. err = mnt_want_write_file(filp);
  733. if (err)
  734. return err;
  735. err = swap_inode_boot_loader(sb, inode);
  736. mnt_drop_write_file(filp);
  737. return err;
  738. }
  739. case EXT4_IOC_RESIZE_FS: {
  740. ext4_fsblk_t n_blocks_count;
  741. int err = 0, err2 = 0;
  742. ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
  743. if (ext4_has_feature_bigalloc(sb)) {
  744. ext4_msg(sb, KERN_ERR,
  745. "Online resizing not (yet) supported with bigalloc");
  746. return -EOPNOTSUPP;
  747. }
  748. if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
  749. sizeof(__u64))) {
  750. return -EFAULT;
  751. }
  752. err = ext4_resize_begin(sb);
  753. if (err)
  754. return err;
  755. err = mnt_want_write_file(filp);
  756. if (err)
  757. goto resizefs_out;
  758. err = ext4_resize_fs(sb, n_blocks_count);
  759. if (EXT4_SB(sb)->s_journal) {
  760. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  761. err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  762. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  763. }
  764. if (err == 0)
  765. err = err2;
  766. mnt_drop_write_file(filp);
  767. if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
  768. ext4_has_group_desc_csum(sb) &&
  769. test_opt(sb, INIT_INODE_TABLE))
  770. err = ext4_register_li_request(sb, o_group);
  771. resizefs_out:
  772. ext4_resize_end(sb);
  773. return err;
  774. }
  775. case FITRIM:
  776. {
  777. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  778. struct fstrim_range range;
  779. int ret = 0;
  780. if (!capable(CAP_SYS_ADMIN))
  781. return -EPERM;
  782. if (!blk_queue_discard(q))
  783. return -EOPNOTSUPP;
  784. if (copy_from_user(&range, (struct fstrim_range __user *)arg,
  785. sizeof(range)))
  786. return -EFAULT;
  787. range.minlen = max((unsigned int)range.minlen,
  788. q->limits.discard_granularity);
  789. ret = ext4_trim_fs(sb, &range);
  790. if (ret < 0)
  791. return ret;
  792. if (copy_to_user((struct fstrim_range __user *)arg, &range,
  793. sizeof(range)))
  794. return -EFAULT;
  795. return 0;
  796. }
  797. case EXT4_IOC_PRECACHE_EXTENTS:
  798. return ext4_ext_precache(inode);
  799. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  800. if (!ext4_has_feature_encrypt(sb))
  801. return -EOPNOTSUPP;
  802. return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
  803. case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
  804. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  805. int err, err2;
  806. struct ext4_sb_info *sbi = EXT4_SB(sb);
  807. handle_t *handle;
  808. if (!ext4_has_feature_encrypt(sb))
  809. return -EOPNOTSUPP;
  810. if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
  811. err = mnt_want_write_file(filp);
  812. if (err)
  813. return err;
  814. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  815. if (IS_ERR(handle)) {
  816. err = PTR_ERR(handle);
  817. goto pwsalt_err_exit;
  818. }
  819. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  820. if (err)
  821. goto pwsalt_err_journal;
  822. generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
  823. err = ext4_handle_dirty_metadata(handle, NULL,
  824. sbi->s_sbh);
  825. pwsalt_err_journal:
  826. err2 = ext4_journal_stop(handle);
  827. if (err2 && !err)
  828. err = err2;
  829. pwsalt_err_exit:
  830. mnt_drop_write_file(filp);
  831. if (err)
  832. return err;
  833. }
  834. if (copy_to_user((void __user *) arg,
  835. sbi->s_es->s_encrypt_pw_salt, 16))
  836. return -EFAULT;
  837. return 0;
  838. #else
  839. return -EOPNOTSUPP;
  840. #endif
  841. }
  842. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  843. return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
  844. case EXT4_IOC_FSGETXATTR:
  845. {
  846. struct fsxattr fa;
  847. memset(&fa, 0, sizeof(struct fsxattr));
  848. fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
  849. if (ext4_has_feature_project(inode->i_sb)) {
  850. fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
  851. EXT4_I(inode)->i_projid);
  852. }
  853. if (copy_to_user((struct fsxattr __user *)arg,
  854. &fa, sizeof(fa)))
  855. return -EFAULT;
  856. return 0;
  857. }
  858. case EXT4_IOC_FSSETXATTR:
  859. {
  860. struct fsxattr fa;
  861. int err;
  862. if (copy_from_user(&fa, (struct fsxattr __user *)arg,
  863. sizeof(fa)))
  864. return -EFAULT;
  865. /* Make sure caller has proper permission */
  866. if (!inode_owner_or_capable(inode))
  867. return -EACCES;
  868. if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
  869. return -EOPNOTSUPP;
  870. flags = ext4_xflags_to_iflags(fa.fsx_xflags);
  871. if (ext4_mask_flags(inode->i_mode, flags) != flags)
  872. return -EOPNOTSUPP;
  873. err = mnt_want_write_file(filp);
  874. if (err)
  875. return err;
  876. inode_lock(inode);
  877. flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
  878. (flags & EXT4_FL_XFLAG_VISIBLE);
  879. err = ext4_ioctl_setflags(inode, flags);
  880. inode_unlock(inode);
  881. mnt_drop_write_file(filp);
  882. if (err)
  883. return err;
  884. err = ext4_ioctl_setproject(filp, fa.fsx_projid);
  885. if (err)
  886. return err;
  887. return 0;
  888. }
  889. case EXT4_IOC_SHUTDOWN:
  890. return ext4_shutdown(sb, arg);
  891. default:
  892. return -ENOTTY;
  893. }
  894. }
  895. #ifdef CONFIG_COMPAT
  896. long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  897. {
  898. /* These are just misnamed, they actually get/put from/to user an int */
  899. switch (cmd) {
  900. case EXT4_IOC32_GETFLAGS:
  901. cmd = EXT4_IOC_GETFLAGS;
  902. break;
  903. case EXT4_IOC32_SETFLAGS:
  904. cmd = EXT4_IOC_SETFLAGS;
  905. break;
  906. case EXT4_IOC32_GETVERSION:
  907. cmd = EXT4_IOC_GETVERSION;
  908. break;
  909. case EXT4_IOC32_SETVERSION:
  910. cmd = EXT4_IOC_SETVERSION;
  911. break;
  912. case EXT4_IOC32_GROUP_EXTEND:
  913. cmd = EXT4_IOC_GROUP_EXTEND;
  914. break;
  915. case EXT4_IOC32_GETVERSION_OLD:
  916. cmd = EXT4_IOC_GETVERSION_OLD;
  917. break;
  918. case EXT4_IOC32_SETVERSION_OLD:
  919. cmd = EXT4_IOC_SETVERSION_OLD;
  920. break;
  921. case EXT4_IOC32_GETRSVSZ:
  922. cmd = EXT4_IOC_GETRSVSZ;
  923. break;
  924. case EXT4_IOC32_SETRSVSZ:
  925. cmd = EXT4_IOC_SETRSVSZ;
  926. break;
  927. case EXT4_IOC32_GROUP_ADD: {
  928. struct compat_ext4_new_group_input __user *uinput;
  929. struct ext4_new_group_input input;
  930. mm_segment_t old_fs;
  931. int err;
  932. uinput = compat_ptr(arg);
  933. err = get_user(input.group, &uinput->group);
  934. err |= get_user(input.block_bitmap, &uinput->block_bitmap);
  935. err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
  936. err |= get_user(input.inode_table, &uinput->inode_table);
  937. err |= get_user(input.blocks_count, &uinput->blocks_count);
  938. err |= get_user(input.reserved_blocks,
  939. &uinput->reserved_blocks);
  940. if (err)
  941. return -EFAULT;
  942. old_fs = get_fs();
  943. set_fs(KERNEL_DS);
  944. err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
  945. (unsigned long) &input);
  946. set_fs(old_fs);
  947. return err;
  948. }
  949. case EXT4_IOC_MOVE_EXT:
  950. case EXT4_IOC_RESIZE_FS:
  951. case EXT4_IOC_PRECACHE_EXTENTS:
  952. case EXT4_IOC_SET_ENCRYPTION_POLICY:
  953. case EXT4_IOC_GET_ENCRYPTION_PWSALT:
  954. case EXT4_IOC_GET_ENCRYPTION_POLICY:
  955. case EXT4_IOC_SHUTDOWN:
  956. case FS_IOC_GETFSMAP:
  957. break;
  958. default:
  959. return -ENOIOCTLCMD;
  960. }
  961. return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  962. }
  963. #endif