ioctl.c 23 KB

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