ioctl.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. #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_commit;
  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. bail_commit:
  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 = osb->journal->j_inode->i_size;
  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. if (status < 0)
  326. goto bail;
  327. iput(inode_alloc);
  328. inode_alloc = NULL;
  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. goto bail;
  566. status = 0;
  567. bail:
  568. if (status)
  569. o2info_set_request_error(&oiff->iff_req, req);
  570. kfree(oiff);
  571. out_err:
  572. return status;
  573. }
  574. int ocfs2_info_handle_unknown(struct inode *inode,
  575. struct ocfs2_info_request __user *req)
  576. {
  577. int status = -EFAULT;
  578. struct ocfs2_info_request oir;
  579. if (o2info_from_user(oir, req))
  580. goto bail;
  581. o2info_clear_request_filled(&oir);
  582. if (o2info_to_user(oir, req))
  583. goto bail;
  584. status = 0;
  585. bail:
  586. if (status)
  587. o2info_set_request_error(&oir, req);
  588. return status;
  589. }
  590. /*
  591. * Validate and distinguish OCFS2_IOC_INFO requests.
  592. *
  593. * - validate the magic number.
  594. * - distinguish different requests.
  595. * - validate size of different requests.
  596. */
  597. int ocfs2_info_handle_request(struct inode *inode,
  598. struct ocfs2_info_request __user *req)
  599. {
  600. int status = -EFAULT;
  601. struct ocfs2_info_request oir;
  602. if (o2info_from_user(oir, req))
  603. goto bail;
  604. status = -EINVAL;
  605. if (oir.ir_magic != OCFS2_INFO_MAGIC)
  606. goto bail;
  607. switch (oir.ir_code) {
  608. case OCFS2_INFO_BLOCKSIZE:
  609. if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
  610. status = ocfs2_info_handle_blocksize(inode, req);
  611. break;
  612. case OCFS2_INFO_CLUSTERSIZE:
  613. if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
  614. status = ocfs2_info_handle_clustersize(inode, req);
  615. break;
  616. case OCFS2_INFO_MAXSLOTS:
  617. if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
  618. status = ocfs2_info_handle_maxslots(inode, req);
  619. break;
  620. case OCFS2_INFO_LABEL:
  621. if (oir.ir_size == sizeof(struct ocfs2_info_label))
  622. status = ocfs2_info_handle_label(inode, req);
  623. break;
  624. case OCFS2_INFO_UUID:
  625. if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
  626. status = ocfs2_info_handle_uuid(inode, req);
  627. break;
  628. case OCFS2_INFO_FS_FEATURES:
  629. if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
  630. status = ocfs2_info_handle_fs_features(inode, req);
  631. break;
  632. case OCFS2_INFO_JOURNAL_SIZE:
  633. if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
  634. status = ocfs2_info_handle_journal_size(inode, req);
  635. break;
  636. case OCFS2_INFO_FREEINODE:
  637. if (oir.ir_size == sizeof(struct ocfs2_info_freeinode))
  638. status = ocfs2_info_handle_freeinode(inode, req);
  639. break;
  640. case OCFS2_INFO_FREEFRAG:
  641. if (oir.ir_size == sizeof(struct ocfs2_info_freefrag))
  642. status = ocfs2_info_handle_freefrag(inode, req);
  643. break;
  644. default:
  645. status = ocfs2_info_handle_unknown(inode, req);
  646. break;
  647. }
  648. bail:
  649. return status;
  650. }
  651. int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
  652. u64 *req_addr, int compat_flag)
  653. {
  654. int status = -EFAULT;
  655. u64 __user *bp = NULL;
  656. if (compat_flag) {
  657. #ifdef CONFIG_COMPAT
  658. /*
  659. * pointer bp stores the base address of a pointers array,
  660. * which collects all addresses of separate request.
  661. */
  662. bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
  663. #else
  664. BUG();
  665. #endif
  666. } else
  667. bp = (u64 __user *)(unsigned long)(info->oi_requests);
  668. if (o2info_from_user(*req_addr, bp + idx))
  669. goto bail;
  670. status = 0;
  671. bail:
  672. return status;
  673. }
  674. /*
  675. * OCFS2_IOC_INFO handles an array of requests passed from userspace.
  676. *
  677. * ocfs2_info_handle() recevies a large info aggregation, grab and
  678. * validate the request count from header, then break it into small
  679. * pieces, later specific handlers can handle them one by one.
  680. *
  681. * Idea here is to make each separate request small enough to ensure
  682. * a better backward&forward compatibility, since a small piece of
  683. * request will be less likely to be broken if disk layout get changed.
  684. */
  685. int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
  686. int compat_flag)
  687. {
  688. int i, status = 0;
  689. u64 req_addr;
  690. struct ocfs2_info_request __user *reqp;
  691. if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
  692. (!info->oi_requests)) {
  693. status = -EINVAL;
  694. goto bail;
  695. }
  696. for (i = 0; i < info->oi_count; i++) {
  697. status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
  698. if (status)
  699. break;
  700. reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
  701. if (!reqp) {
  702. status = -EINVAL;
  703. goto bail;
  704. }
  705. status = ocfs2_info_handle_request(inode, reqp);
  706. if (status)
  707. break;
  708. }
  709. bail:
  710. return status;
  711. }
  712. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  713. {
  714. struct inode *inode = filp->f_path.dentry->d_inode;
  715. unsigned int flags;
  716. int new_clusters;
  717. int status;
  718. struct ocfs2_space_resv sr;
  719. struct ocfs2_new_group_input input;
  720. struct reflink_arguments args;
  721. const char *old_path, *new_path;
  722. bool preserve;
  723. struct ocfs2_info info;
  724. switch (cmd) {
  725. case OCFS2_IOC_GETFLAGS:
  726. status = ocfs2_get_inode_attr(inode, &flags);
  727. if (status < 0)
  728. return status;
  729. flags &= OCFS2_FL_VISIBLE;
  730. return put_user(flags, (int __user *) arg);
  731. case OCFS2_IOC_SETFLAGS:
  732. if (get_user(flags, (int __user *) arg))
  733. return -EFAULT;
  734. status = mnt_want_write_file(filp);
  735. if (status)
  736. return status;
  737. status = ocfs2_set_inode_attr(inode, flags,
  738. OCFS2_FL_MODIFIABLE);
  739. mnt_drop_write_file(filp);
  740. return status;
  741. case OCFS2_IOC_RESVSP:
  742. case OCFS2_IOC_RESVSP64:
  743. case OCFS2_IOC_UNRESVSP:
  744. case OCFS2_IOC_UNRESVSP64:
  745. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  746. return -EFAULT;
  747. return ocfs2_change_file_space(filp, cmd, &sr);
  748. case OCFS2_IOC_GROUP_EXTEND:
  749. if (!capable(CAP_SYS_RESOURCE))
  750. return -EPERM;
  751. if (get_user(new_clusters, (int __user *)arg))
  752. return -EFAULT;
  753. return ocfs2_group_extend(inode, new_clusters);
  754. case OCFS2_IOC_GROUP_ADD:
  755. case OCFS2_IOC_GROUP_ADD64:
  756. if (!capable(CAP_SYS_RESOURCE))
  757. return -EPERM;
  758. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  759. return -EFAULT;
  760. return ocfs2_group_add(inode, &input);
  761. case OCFS2_IOC_REFLINK:
  762. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  763. sizeof(args)))
  764. return -EFAULT;
  765. old_path = (const char *)(unsigned long)args.old_path;
  766. new_path = (const char *)(unsigned long)args.new_path;
  767. preserve = (args.preserve != 0);
  768. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  769. case OCFS2_IOC_INFO:
  770. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  771. sizeof(struct ocfs2_info)))
  772. return -EFAULT;
  773. return ocfs2_info_handle(inode, &info, 0);
  774. case FITRIM:
  775. {
  776. struct super_block *sb = inode->i_sb;
  777. struct fstrim_range range;
  778. int ret = 0;
  779. if (!capable(CAP_SYS_ADMIN))
  780. return -EPERM;
  781. if (copy_from_user(&range, (struct fstrim_range *)arg,
  782. sizeof(range)))
  783. return -EFAULT;
  784. ret = ocfs2_trim_fs(sb, &range);
  785. if (ret < 0)
  786. return ret;
  787. if (copy_to_user((struct fstrim_range *)arg, &range,
  788. sizeof(range)))
  789. return -EFAULT;
  790. return 0;
  791. }
  792. case OCFS2_IOC_MOVE_EXT:
  793. return ocfs2_ioctl_move_extents(filp, (void __user *)arg);
  794. default:
  795. return -ENOTTY;
  796. }
  797. }
  798. #ifdef CONFIG_COMPAT
  799. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  800. {
  801. bool preserve;
  802. struct reflink_arguments args;
  803. struct inode *inode = file->f_path.dentry->d_inode;
  804. struct ocfs2_info info;
  805. switch (cmd) {
  806. case OCFS2_IOC32_GETFLAGS:
  807. cmd = OCFS2_IOC_GETFLAGS;
  808. break;
  809. case OCFS2_IOC32_SETFLAGS:
  810. cmd = OCFS2_IOC_SETFLAGS;
  811. break;
  812. case OCFS2_IOC_RESVSP:
  813. case OCFS2_IOC_RESVSP64:
  814. case OCFS2_IOC_UNRESVSP:
  815. case OCFS2_IOC_UNRESVSP64:
  816. case OCFS2_IOC_GROUP_EXTEND:
  817. case OCFS2_IOC_GROUP_ADD:
  818. case OCFS2_IOC_GROUP_ADD64:
  819. case FITRIM:
  820. break;
  821. case OCFS2_IOC_REFLINK:
  822. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  823. sizeof(args)))
  824. return -EFAULT;
  825. preserve = (args.preserve != 0);
  826. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  827. compat_ptr(args.new_path), preserve);
  828. case OCFS2_IOC_INFO:
  829. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  830. sizeof(struct ocfs2_info)))
  831. return -EFAULT;
  832. return ocfs2_info_handle(inode, &info, 1);
  833. case OCFS2_IOC_MOVE_EXT:
  834. break;
  835. default:
  836. return -ENOIOCTLCMD;
  837. }
  838. return ocfs2_ioctl(file, cmd, arg);
  839. }
  840. #endif