ioctl.c 23 KB

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