ioctl.c 22 KB

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