ioctl.c 23 KB

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