ioctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ocfs2/ioctl.c
  4. *
  5. * Copyright (C) 2006 Herbert Poetzl
  6. * adapted from Remy Card's ext2/ioctl.c
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/mount.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/compat.h>
  12. #include <cluster/masklog.h>
  13. #include "ocfs2.h"
  14. #include "alloc.h"
  15. #include "dlmglue.h"
  16. #include "file.h"
  17. #include "inode.h"
  18. #include "journal.h"
  19. #include "ocfs2_fs.h"
  20. #include "ioctl.h"
  21. #include "resize.h"
  22. #include "refcounttree.h"
  23. #include "sysfile.h"
  24. #include "dir.h"
  25. #include "buffer_head_io.h"
  26. #include "suballoc.h"
  27. #include "move_extents.h"
  28. #define o2info_from_user(a, b) \
  29. copy_from_user(&(a), (b), sizeof(a))
  30. #define o2info_to_user(a, b) \
  31. copy_to_user((typeof(a) __user *)b, &(a), sizeof(a))
  32. /*
  33. * This is just a best-effort to tell userspace that this request
  34. * caused the error.
  35. */
  36. static inline void o2info_set_request_error(struct ocfs2_info_request *kreq,
  37. struct ocfs2_info_request __user *req)
  38. {
  39. kreq->ir_flags |= OCFS2_INFO_FL_ERROR;
  40. (void)put_user(kreq->ir_flags, (__u32 __user *)&(req->ir_flags));
  41. }
  42. static inline void o2info_set_request_filled(struct ocfs2_info_request *req)
  43. {
  44. req->ir_flags |= OCFS2_INFO_FL_FILLED;
  45. }
  46. static inline void o2info_clear_request_filled(struct ocfs2_info_request *req)
  47. {
  48. req->ir_flags &= ~OCFS2_INFO_FL_FILLED;
  49. }
  50. static inline int o2info_coherent(struct ocfs2_info_request *req)
  51. {
  52. return (!(req->ir_flags & OCFS2_INFO_FL_NON_COHERENT));
  53. }
  54. static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
  55. {
  56. int status;
  57. status = ocfs2_inode_lock(inode, NULL, 0);
  58. if (status < 0) {
  59. mlog_errno(status);
  60. return status;
  61. }
  62. ocfs2_get_inode_flags(OCFS2_I(inode));
  63. *flags = OCFS2_I(inode)->ip_attr;
  64. ocfs2_inode_unlock(inode, 0);
  65. return status;
  66. }
  67. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  68. unsigned mask)
  69. {
  70. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  71. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  72. handle_t *handle = NULL;
  73. struct buffer_head *bh = NULL;
  74. unsigned oldflags;
  75. int status;
  76. inode_lock(inode);
  77. status = ocfs2_inode_lock(inode, &bh, 1);
  78. if (status < 0) {
  79. mlog_errno(status);
  80. goto bail;
  81. }
  82. status = -EACCES;
  83. if (!inode_owner_or_capable(inode))
  84. goto bail_unlock;
  85. if (!S_ISDIR(inode->i_mode))
  86. flags &= ~OCFS2_DIRSYNC_FL;
  87. oldflags = ocfs2_inode->ip_attr;
  88. flags = flags & mask;
  89. flags |= oldflags & ~mask;
  90. status = vfs_ioc_setflags_prepare(inode, oldflags, flags);
  91. if (status)
  92. goto bail_unlock;
  93. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  94. if (IS_ERR(handle)) {
  95. status = PTR_ERR(handle);
  96. mlog_errno(status);
  97. goto bail_unlock;
  98. }
  99. ocfs2_inode->ip_attr = flags;
  100. ocfs2_set_inode_flags(inode);
  101. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  102. if (status < 0)
  103. mlog_errno(status);
  104. ocfs2_commit_trans(osb, handle);
  105. bail_unlock:
  106. ocfs2_inode_unlock(inode, 1);
  107. bail:
  108. inode_unlock(inode);
  109. brelse(bh);
  110. return status;
  111. }
  112. static int ocfs2_info_handle_blocksize(struct inode *inode,
  113. struct ocfs2_info_request __user *req)
  114. {
  115. struct ocfs2_info_blocksize oib;
  116. if (o2info_from_user(oib, req))
  117. return -EFAULT;
  118. oib.ib_blocksize = inode->i_sb->s_blocksize;
  119. o2info_set_request_filled(&oib.ib_req);
  120. if (o2info_to_user(oib, req))
  121. return -EFAULT;
  122. return 0;
  123. }
  124. static int ocfs2_info_handle_clustersize(struct inode *inode,
  125. struct ocfs2_info_request __user *req)
  126. {
  127. struct ocfs2_info_clustersize oic;
  128. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  129. if (o2info_from_user(oic, req))
  130. return -EFAULT;
  131. oic.ic_clustersize = osb->s_clustersize;
  132. o2info_set_request_filled(&oic.ic_req);
  133. if (o2info_to_user(oic, req))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. static int ocfs2_info_handle_maxslots(struct inode *inode,
  138. struct ocfs2_info_request __user *req)
  139. {
  140. struct ocfs2_info_maxslots oim;
  141. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  142. if (o2info_from_user(oim, req))
  143. return -EFAULT;
  144. oim.im_max_slots = osb->max_slots;
  145. o2info_set_request_filled(&oim.im_req);
  146. if (o2info_to_user(oim, req))
  147. return -EFAULT;
  148. return 0;
  149. }
  150. static int ocfs2_info_handle_label(struct inode *inode,
  151. struct ocfs2_info_request __user *req)
  152. {
  153. struct ocfs2_info_label oil;
  154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  155. if (o2info_from_user(oil, req))
  156. return -EFAULT;
  157. memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
  158. o2info_set_request_filled(&oil.il_req);
  159. if (o2info_to_user(oil, req))
  160. return -EFAULT;
  161. return 0;
  162. }
  163. static int ocfs2_info_handle_uuid(struct inode *inode,
  164. struct ocfs2_info_request __user *req)
  165. {
  166. struct ocfs2_info_uuid oiu;
  167. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  168. if (o2info_from_user(oiu, req))
  169. return -EFAULT;
  170. memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
  171. o2info_set_request_filled(&oiu.iu_req);
  172. if (o2info_to_user(oiu, req))
  173. return -EFAULT;
  174. return 0;
  175. }
  176. static int ocfs2_info_handle_fs_features(struct inode *inode,
  177. struct ocfs2_info_request __user *req)
  178. {
  179. struct ocfs2_info_fs_features oif;
  180. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  181. if (o2info_from_user(oif, req))
  182. return -EFAULT;
  183. oif.if_compat_features = osb->s_feature_compat;
  184. oif.if_incompat_features = osb->s_feature_incompat;
  185. oif.if_ro_compat_features = osb->s_feature_ro_compat;
  186. o2info_set_request_filled(&oif.if_req);
  187. if (o2info_to_user(oif, req))
  188. return -EFAULT;
  189. return 0;
  190. }
  191. static int ocfs2_info_handle_journal_size(struct inode *inode,
  192. struct ocfs2_info_request __user *req)
  193. {
  194. struct ocfs2_info_journal_size oij;
  195. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  196. if (o2info_from_user(oij, req))
  197. return -EFAULT;
  198. oij.ij_journal_size = i_size_read(osb->journal->j_inode);
  199. o2info_set_request_filled(&oij.ij_req);
  200. if (o2info_to_user(oij, req))
  201. return -EFAULT;
  202. return 0;
  203. }
  204. static int ocfs2_info_scan_inode_alloc(struct ocfs2_super *osb,
  205. struct inode *inode_alloc, u64 blkno,
  206. struct ocfs2_info_freeinode *fi,
  207. u32 slot)
  208. {
  209. int status = 0, unlock = 0;
  210. struct buffer_head *bh = NULL;
  211. struct ocfs2_dinode *dinode_alloc = NULL;
  212. if (inode_alloc)
  213. inode_lock(inode_alloc);
  214. if (inode_alloc && o2info_coherent(&fi->ifi_req)) {
  215. status = ocfs2_inode_lock(inode_alloc, &bh, 0);
  216. if (status < 0) {
  217. mlog_errno(status);
  218. goto bail;
  219. }
  220. unlock = 1;
  221. } else {
  222. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  223. if (status < 0) {
  224. mlog_errno(status);
  225. goto bail;
  226. }
  227. }
  228. dinode_alloc = (struct ocfs2_dinode *)bh->b_data;
  229. fi->ifi_stat[slot].lfi_total =
  230. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total);
  231. fi->ifi_stat[slot].lfi_free =
  232. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total) -
  233. le32_to_cpu(dinode_alloc->id1.bitmap1.i_used);
  234. bail:
  235. if (unlock)
  236. ocfs2_inode_unlock(inode_alloc, 0);
  237. if (inode_alloc)
  238. inode_unlock(inode_alloc);
  239. brelse(bh);
  240. return status;
  241. }
  242. static int ocfs2_info_handle_freeinode(struct inode *inode,
  243. struct ocfs2_info_request __user *req)
  244. {
  245. u32 i;
  246. u64 blkno = -1;
  247. char namebuf[40];
  248. int status, type = INODE_ALLOC_SYSTEM_INODE;
  249. struct ocfs2_info_freeinode *oifi = NULL;
  250. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  251. struct inode *inode_alloc = NULL;
  252. oifi = kzalloc(sizeof(struct ocfs2_info_freeinode), GFP_KERNEL);
  253. if (!oifi) {
  254. status = -ENOMEM;
  255. mlog_errno(status);
  256. goto out_err;
  257. }
  258. if (o2info_from_user(*oifi, req)) {
  259. status = -EFAULT;
  260. goto out_free;
  261. }
  262. oifi->ifi_slotnum = osb->max_slots;
  263. for (i = 0; i < oifi->ifi_slotnum; i++) {
  264. if (o2info_coherent(&oifi->ifi_req)) {
  265. inode_alloc = ocfs2_get_system_file_inode(osb, type, i);
  266. if (!inode_alloc) {
  267. mlog(ML_ERROR, "unable to get alloc inode in "
  268. "slot %u\n", i);
  269. status = -EIO;
  270. goto bail;
  271. }
  272. } else {
  273. ocfs2_sprintf_system_inode_name(namebuf,
  274. sizeof(namebuf),
  275. type, i);
  276. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  277. namebuf,
  278. strlen(namebuf),
  279. &blkno);
  280. if (status < 0) {
  281. status = -ENOENT;
  282. goto bail;
  283. }
  284. }
  285. status = ocfs2_info_scan_inode_alloc(osb, inode_alloc, blkno, oifi, i);
  286. iput(inode_alloc);
  287. inode_alloc = NULL;
  288. if (status < 0)
  289. goto bail;
  290. }
  291. o2info_set_request_filled(&oifi->ifi_req);
  292. if (o2info_to_user(*oifi, req)) {
  293. status = -EFAULT;
  294. goto out_free;
  295. }
  296. status = 0;
  297. bail:
  298. if (status)
  299. o2info_set_request_error(&oifi->ifi_req, req);
  300. out_free:
  301. kfree(oifi);
  302. out_err:
  303. return status;
  304. }
  305. static void o2ffg_update_histogram(struct ocfs2_info_free_chunk_list *hist,
  306. unsigned int chunksize)
  307. {
  308. u32 index;
  309. index = __ilog2_u32(chunksize);
  310. if (index >= OCFS2_INFO_MAX_HIST)
  311. index = OCFS2_INFO_MAX_HIST - 1;
  312. hist->fc_chunks[index]++;
  313. hist->fc_clusters[index] += chunksize;
  314. }
  315. static void o2ffg_update_stats(struct ocfs2_info_freefrag_stats *stats,
  316. unsigned int chunksize)
  317. {
  318. if (chunksize > stats->ffs_max)
  319. stats->ffs_max = chunksize;
  320. if (chunksize < stats->ffs_min)
  321. stats->ffs_min = chunksize;
  322. stats->ffs_avg += chunksize;
  323. stats->ffs_free_chunks_real++;
  324. }
  325. static void ocfs2_info_update_ffg(struct ocfs2_info_freefrag *ffg,
  326. unsigned int chunksize)
  327. {
  328. o2ffg_update_histogram(&(ffg->iff_ffs.ffs_fc_hist), chunksize);
  329. o2ffg_update_stats(&(ffg->iff_ffs), chunksize);
  330. }
  331. static int ocfs2_info_freefrag_scan_chain(struct ocfs2_super *osb,
  332. struct inode *gb_inode,
  333. struct ocfs2_dinode *gb_dinode,
  334. struct ocfs2_chain_rec *rec,
  335. struct ocfs2_info_freefrag *ffg,
  336. u32 chunks_in_group)
  337. {
  338. int status = 0, used;
  339. u64 blkno;
  340. struct buffer_head *bh = NULL;
  341. struct ocfs2_group_desc *bg = NULL;
  342. unsigned int max_bits, num_clusters;
  343. unsigned int offset = 0, cluster, chunk;
  344. unsigned int chunk_free, last_chunksize = 0;
  345. if (!le32_to_cpu(rec->c_free))
  346. goto bail;
  347. do {
  348. if (!bg)
  349. blkno = le64_to_cpu(rec->c_blkno);
  350. else
  351. blkno = le64_to_cpu(bg->bg_next_group);
  352. if (bh) {
  353. brelse(bh);
  354. bh = NULL;
  355. }
  356. if (o2info_coherent(&ffg->iff_req))
  357. status = ocfs2_read_group_descriptor(gb_inode,
  358. gb_dinode,
  359. blkno, &bh);
  360. else
  361. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  362. if (status < 0) {
  363. mlog(ML_ERROR, "Can't read the group descriptor # "
  364. "%llu from device.", (unsigned long long)blkno);
  365. status = -EIO;
  366. goto bail;
  367. }
  368. bg = (struct ocfs2_group_desc *)bh->b_data;
  369. if (!le16_to_cpu(bg->bg_free_bits_count))
  370. continue;
  371. max_bits = le16_to_cpu(bg->bg_bits);
  372. offset = 0;
  373. for (chunk = 0; chunk < chunks_in_group; chunk++) {
  374. /*
  375. * last chunk may be not an entire one.
  376. */
  377. if ((offset + ffg->iff_chunksize) > max_bits)
  378. num_clusters = max_bits - offset;
  379. else
  380. num_clusters = ffg->iff_chunksize;
  381. chunk_free = 0;
  382. for (cluster = 0; cluster < num_clusters; cluster++) {
  383. used = ocfs2_test_bit(offset,
  384. (unsigned long *)bg->bg_bitmap);
  385. /*
  386. * - chunk_free counts free clusters in #N chunk.
  387. * - last_chunksize records the size(in) clusters
  388. * for the last real free chunk being counted.
  389. */
  390. if (!used) {
  391. last_chunksize++;
  392. chunk_free++;
  393. }
  394. if (used && last_chunksize) {
  395. ocfs2_info_update_ffg(ffg,
  396. last_chunksize);
  397. last_chunksize = 0;
  398. }
  399. offset++;
  400. }
  401. if (chunk_free == ffg->iff_chunksize)
  402. ffg->iff_ffs.ffs_free_chunks++;
  403. }
  404. /*
  405. * need to update the info for last free chunk.
  406. */
  407. if (last_chunksize)
  408. ocfs2_info_update_ffg(ffg, last_chunksize);
  409. } while (le64_to_cpu(bg->bg_next_group));
  410. bail:
  411. brelse(bh);
  412. return status;
  413. }
  414. static int ocfs2_info_freefrag_scan_bitmap(struct ocfs2_super *osb,
  415. struct inode *gb_inode, u64 blkno,
  416. struct ocfs2_info_freefrag *ffg)
  417. {
  418. u32 chunks_in_group;
  419. int status = 0, unlock = 0, i;
  420. struct buffer_head *bh = NULL;
  421. struct ocfs2_chain_list *cl = NULL;
  422. struct ocfs2_chain_rec *rec = NULL;
  423. struct ocfs2_dinode *gb_dinode = NULL;
  424. if (gb_inode)
  425. inode_lock(gb_inode);
  426. if (o2info_coherent(&ffg->iff_req)) {
  427. status = ocfs2_inode_lock(gb_inode, &bh, 0);
  428. if (status < 0) {
  429. mlog_errno(status);
  430. goto bail;
  431. }
  432. unlock = 1;
  433. } else {
  434. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  435. if (status < 0) {
  436. mlog_errno(status);
  437. goto bail;
  438. }
  439. }
  440. gb_dinode = (struct ocfs2_dinode *)bh->b_data;
  441. cl = &(gb_dinode->id2.i_chain);
  442. /*
  443. * Chunksize(in) clusters from userspace should be
  444. * less than clusters in a group.
  445. */
  446. if (ffg->iff_chunksize > le16_to_cpu(cl->cl_cpg)) {
  447. status = -EINVAL;
  448. goto bail;
  449. }
  450. memset(&ffg->iff_ffs, 0, sizeof(struct ocfs2_info_freefrag_stats));
  451. ffg->iff_ffs.ffs_min = ~0U;
  452. ffg->iff_ffs.ffs_clusters =
  453. le32_to_cpu(gb_dinode->id1.bitmap1.i_total);
  454. ffg->iff_ffs.ffs_free_clusters = ffg->iff_ffs.ffs_clusters -
  455. le32_to_cpu(gb_dinode->id1.bitmap1.i_used);
  456. chunks_in_group = le16_to_cpu(cl->cl_cpg) / ffg->iff_chunksize + 1;
  457. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  458. rec = &(cl->cl_recs[i]);
  459. status = ocfs2_info_freefrag_scan_chain(osb, gb_inode,
  460. gb_dinode,
  461. rec, ffg,
  462. chunks_in_group);
  463. if (status)
  464. goto bail;
  465. }
  466. if (ffg->iff_ffs.ffs_free_chunks_real)
  467. ffg->iff_ffs.ffs_avg = (ffg->iff_ffs.ffs_avg /
  468. ffg->iff_ffs.ffs_free_chunks_real);
  469. bail:
  470. if (unlock)
  471. ocfs2_inode_unlock(gb_inode, 0);
  472. if (gb_inode)
  473. inode_unlock(gb_inode);
  474. iput(gb_inode);
  475. brelse(bh);
  476. return status;
  477. }
  478. static int ocfs2_info_handle_freefrag(struct inode *inode,
  479. struct ocfs2_info_request __user *req)
  480. {
  481. u64 blkno = -1;
  482. char namebuf[40];
  483. int status, type = GLOBAL_BITMAP_SYSTEM_INODE;
  484. struct ocfs2_info_freefrag *oiff;
  485. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  486. struct inode *gb_inode = NULL;
  487. oiff = kzalloc(sizeof(struct ocfs2_info_freefrag), GFP_KERNEL);
  488. if (!oiff) {
  489. status = -ENOMEM;
  490. mlog_errno(status);
  491. goto out_err;
  492. }
  493. if (o2info_from_user(*oiff, req)) {
  494. status = -EFAULT;
  495. goto out_free;
  496. }
  497. /*
  498. * chunksize from userspace should be power of 2.
  499. */
  500. if ((oiff->iff_chunksize & (oiff->iff_chunksize - 1)) ||
  501. (!oiff->iff_chunksize)) {
  502. status = -EINVAL;
  503. goto bail;
  504. }
  505. if (o2info_coherent(&oiff->iff_req)) {
  506. gb_inode = ocfs2_get_system_file_inode(osb, type,
  507. OCFS2_INVALID_SLOT);
  508. if (!gb_inode) {
  509. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  510. status = -EIO;
  511. goto bail;
  512. }
  513. } else {
  514. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type,
  515. OCFS2_INVALID_SLOT);
  516. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  517. namebuf,
  518. strlen(namebuf),
  519. &blkno);
  520. if (status < 0) {
  521. status = -ENOENT;
  522. goto bail;
  523. }
  524. }
  525. status = ocfs2_info_freefrag_scan_bitmap(osb, gb_inode, blkno, oiff);
  526. if (status < 0)
  527. goto bail;
  528. o2info_set_request_filled(&oiff->iff_req);
  529. if (o2info_to_user(*oiff, req)) {
  530. status = -EFAULT;
  531. goto out_free;
  532. }
  533. status = 0;
  534. bail:
  535. if (status)
  536. o2info_set_request_error(&oiff->iff_req, req);
  537. out_free:
  538. kfree(oiff);
  539. out_err:
  540. return status;
  541. }
  542. static int ocfs2_info_handle_unknown(struct inode *inode,
  543. struct ocfs2_info_request __user *req)
  544. {
  545. struct ocfs2_info_request oir;
  546. if (o2info_from_user(oir, req))
  547. return -EFAULT;
  548. o2info_clear_request_filled(&oir);
  549. if (o2info_to_user(oir, req))
  550. return -EFAULT;
  551. return 0;
  552. }
  553. /*
  554. * Validate and distinguish OCFS2_IOC_INFO requests.
  555. *
  556. * - validate the magic number.
  557. * - distinguish different requests.
  558. * - validate size of different requests.
  559. */
  560. static int ocfs2_info_handle_request(struct inode *inode,
  561. struct ocfs2_info_request __user *req)
  562. {
  563. int status = -EFAULT;
  564. struct ocfs2_info_request oir;
  565. if (o2info_from_user(oir, req))
  566. goto bail;
  567. status = -EINVAL;
  568. if (oir.ir_magic != OCFS2_INFO_MAGIC)
  569. goto bail;
  570. switch (oir.ir_code) {
  571. case OCFS2_INFO_BLOCKSIZE:
  572. if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
  573. status = ocfs2_info_handle_blocksize(inode, req);
  574. break;
  575. case OCFS2_INFO_CLUSTERSIZE:
  576. if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
  577. status = ocfs2_info_handle_clustersize(inode, req);
  578. break;
  579. case OCFS2_INFO_MAXSLOTS:
  580. if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
  581. status = ocfs2_info_handle_maxslots(inode, req);
  582. break;
  583. case OCFS2_INFO_LABEL:
  584. if (oir.ir_size == sizeof(struct ocfs2_info_label))
  585. status = ocfs2_info_handle_label(inode, req);
  586. break;
  587. case OCFS2_INFO_UUID:
  588. if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
  589. status = ocfs2_info_handle_uuid(inode, req);
  590. break;
  591. case OCFS2_INFO_FS_FEATURES:
  592. if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
  593. status = ocfs2_info_handle_fs_features(inode, req);
  594. break;
  595. case OCFS2_INFO_JOURNAL_SIZE:
  596. if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
  597. status = ocfs2_info_handle_journal_size(inode, req);
  598. break;
  599. case OCFS2_INFO_FREEINODE:
  600. if (oir.ir_size == sizeof(struct ocfs2_info_freeinode))
  601. status = ocfs2_info_handle_freeinode(inode, req);
  602. break;
  603. case OCFS2_INFO_FREEFRAG:
  604. if (oir.ir_size == sizeof(struct ocfs2_info_freefrag))
  605. status = ocfs2_info_handle_freefrag(inode, req);
  606. break;
  607. default:
  608. status = ocfs2_info_handle_unknown(inode, req);
  609. break;
  610. }
  611. bail:
  612. return status;
  613. }
  614. static int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
  615. u64 *req_addr, int compat_flag)
  616. {
  617. int status = -EFAULT;
  618. u64 __user *bp = NULL;
  619. if (compat_flag) {
  620. #ifdef CONFIG_COMPAT
  621. /*
  622. * pointer bp stores the base address of a pointers array,
  623. * which collects all addresses of separate request.
  624. */
  625. bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
  626. #else
  627. BUG();
  628. #endif
  629. } else
  630. bp = (u64 __user *)(unsigned long)(info->oi_requests);
  631. if (o2info_from_user(*req_addr, bp + idx))
  632. goto bail;
  633. status = 0;
  634. bail:
  635. return status;
  636. }
  637. /*
  638. * OCFS2_IOC_INFO handles an array of requests passed from userspace.
  639. *
  640. * ocfs2_info_handle() recevies a large info aggregation, grab and
  641. * validate the request count from header, then break it into small
  642. * pieces, later specific handlers can handle them one by one.
  643. *
  644. * Idea here is to make each separate request small enough to ensure
  645. * a better backward&forward compatibility, since a small piece of
  646. * request will be less likely to be broken if disk layout get changed.
  647. */
  648. static int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
  649. int compat_flag)
  650. {
  651. int i, status = 0;
  652. u64 req_addr;
  653. struct ocfs2_info_request __user *reqp;
  654. if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
  655. (!info->oi_requests)) {
  656. status = -EINVAL;
  657. goto bail;
  658. }
  659. for (i = 0; i < info->oi_count; i++) {
  660. status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
  661. if (status)
  662. break;
  663. reqp = (struct ocfs2_info_request __user *)(unsigned long)req_addr;
  664. if (!reqp) {
  665. status = -EINVAL;
  666. goto bail;
  667. }
  668. status = ocfs2_info_handle_request(inode, reqp);
  669. if (status)
  670. break;
  671. }
  672. bail:
  673. return status;
  674. }
  675. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  676. {
  677. struct inode *inode = file_inode(filp);
  678. unsigned int flags;
  679. int new_clusters;
  680. int status;
  681. struct ocfs2_space_resv sr;
  682. struct ocfs2_new_group_input input;
  683. struct reflink_arguments args;
  684. const char __user *old_path;
  685. const char __user *new_path;
  686. bool preserve;
  687. struct ocfs2_info info;
  688. void __user *argp = (void __user *)arg;
  689. switch (cmd) {
  690. case OCFS2_IOC_GETFLAGS:
  691. status = ocfs2_get_inode_attr(inode, &flags);
  692. if (status < 0)
  693. return status;
  694. flags &= OCFS2_FL_VISIBLE;
  695. return put_user(flags, (int __user *) arg);
  696. case OCFS2_IOC_SETFLAGS:
  697. if (get_user(flags, (int __user *) arg))
  698. return -EFAULT;
  699. status = mnt_want_write_file(filp);
  700. if (status)
  701. return status;
  702. status = ocfs2_set_inode_attr(inode, flags,
  703. OCFS2_FL_MODIFIABLE);
  704. mnt_drop_write_file(filp);
  705. return status;
  706. case OCFS2_IOC_RESVSP:
  707. case OCFS2_IOC_RESVSP64:
  708. case OCFS2_IOC_UNRESVSP:
  709. case OCFS2_IOC_UNRESVSP64:
  710. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  711. return -EFAULT;
  712. return ocfs2_change_file_space(filp, cmd, &sr);
  713. case OCFS2_IOC_GROUP_EXTEND:
  714. if (!capable(CAP_SYS_RESOURCE))
  715. return -EPERM;
  716. if (get_user(new_clusters, (int __user *)arg))
  717. return -EFAULT;
  718. status = mnt_want_write_file(filp);
  719. if (status)
  720. return status;
  721. status = ocfs2_group_extend(inode, new_clusters);
  722. mnt_drop_write_file(filp);
  723. return status;
  724. case OCFS2_IOC_GROUP_ADD:
  725. case OCFS2_IOC_GROUP_ADD64:
  726. if (!capable(CAP_SYS_RESOURCE))
  727. return -EPERM;
  728. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  729. return -EFAULT;
  730. status = mnt_want_write_file(filp);
  731. if (status)
  732. return status;
  733. status = ocfs2_group_add(inode, &input);
  734. mnt_drop_write_file(filp);
  735. return status;
  736. case OCFS2_IOC_REFLINK:
  737. if (copy_from_user(&args, argp, sizeof(args)))
  738. return -EFAULT;
  739. old_path = (const char __user *)(unsigned long)args.old_path;
  740. new_path = (const char __user *)(unsigned long)args.new_path;
  741. preserve = (args.preserve != 0);
  742. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  743. case OCFS2_IOC_INFO:
  744. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  745. return -EFAULT;
  746. return ocfs2_info_handle(inode, &info, 0);
  747. case FITRIM:
  748. {
  749. struct super_block *sb = inode->i_sb;
  750. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  751. struct fstrim_range range;
  752. int ret = 0;
  753. if (!capable(CAP_SYS_ADMIN))
  754. return -EPERM;
  755. if (!blk_queue_discard(q))
  756. return -EOPNOTSUPP;
  757. if (copy_from_user(&range, argp, sizeof(range)))
  758. return -EFAULT;
  759. range.minlen = max_t(u64, q->limits.discard_granularity,
  760. range.minlen);
  761. ret = ocfs2_trim_fs(sb, &range);
  762. if (ret < 0)
  763. return ret;
  764. if (copy_to_user(argp, &range, sizeof(range)))
  765. return -EFAULT;
  766. return 0;
  767. }
  768. case OCFS2_IOC_MOVE_EXT:
  769. return ocfs2_ioctl_move_extents(filp, argp);
  770. default:
  771. return -ENOTTY;
  772. }
  773. }
  774. #ifdef CONFIG_COMPAT
  775. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  776. {
  777. bool preserve;
  778. struct reflink_arguments args;
  779. struct inode *inode = file_inode(file);
  780. struct ocfs2_info info;
  781. void __user *argp = (void __user *)arg;
  782. switch (cmd) {
  783. case OCFS2_IOC32_GETFLAGS:
  784. cmd = OCFS2_IOC_GETFLAGS;
  785. break;
  786. case OCFS2_IOC32_SETFLAGS:
  787. cmd = OCFS2_IOC_SETFLAGS;
  788. break;
  789. case OCFS2_IOC_RESVSP:
  790. case OCFS2_IOC_RESVSP64:
  791. case OCFS2_IOC_UNRESVSP:
  792. case OCFS2_IOC_UNRESVSP64:
  793. case OCFS2_IOC_GROUP_EXTEND:
  794. case OCFS2_IOC_GROUP_ADD:
  795. case OCFS2_IOC_GROUP_ADD64:
  796. break;
  797. case OCFS2_IOC_REFLINK:
  798. if (copy_from_user(&args, argp, sizeof(args)))
  799. return -EFAULT;
  800. preserve = (args.preserve != 0);
  801. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  802. compat_ptr(args.new_path), preserve);
  803. case OCFS2_IOC_INFO:
  804. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  805. return -EFAULT;
  806. return ocfs2_info_handle(inode, &info, 1);
  807. case FITRIM:
  808. case OCFS2_IOC_MOVE_EXT:
  809. break;
  810. default:
  811. return -ENOIOCTLCMD;
  812. }
  813. return ocfs2_ioctl(file, cmd, arg);
  814. }
  815. #endif