ioctl.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. oldflags = ocfs2_inode->ip_attr;
  87. flags = flags & mask;
  88. flags |= oldflags & ~mask;
  89. /*
  90. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  91. * the relevant capability.
  92. */
  93. status = -EPERM;
  94. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  95. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  96. if (!capable(CAP_LINUX_IMMUTABLE))
  97. goto bail_unlock;
  98. }
  99. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  100. if (IS_ERR(handle)) {
  101. status = PTR_ERR(handle);
  102. mlog_errno(status);
  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 out_err;
  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. out_err:
  338. return status;
  339. }
  340. static void o2ffg_update_histogram(struct ocfs2_info_free_chunk_list *hist,
  341. unsigned int chunksize)
  342. {
  343. int index;
  344. index = __ilog2_u32(chunksize);
  345. if (index >= OCFS2_INFO_MAX_HIST)
  346. index = OCFS2_INFO_MAX_HIST - 1;
  347. hist->fc_chunks[index]++;
  348. hist->fc_clusters[index] += chunksize;
  349. }
  350. static void o2ffg_update_stats(struct ocfs2_info_freefrag_stats *stats,
  351. unsigned int chunksize)
  352. {
  353. if (chunksize > stats->ffs_max)
  354. stats->ffs_max = chunksize;
  355. if (chunksize < stats->ffs_min)
  356. stats->ffs_min = chunksize;
  357. stats->ffs_avg += chunksize;
  358. stats->ffs_free_chunks_real++;
  359. }
  360. void ocfs2_info_update_ffg(struct ocfs2_info_freefrag *ffg,
  361. unsigned int chunksize)
  362. {
  363. o2ffg_update_histogram(&(ffg->iff_ffs.ffs_fc_hist), chunksize);
  364. o2ffg_update_stats(&(ffg->iff_ffs), chunksize);
  365. }
  366. int ocfs2_info_freefrag_scan_chain(struct ocfs2_super *osb,
  367. struct inode *gb_inode,
  368. struct ocfs2_dinode *gb_dinode,
  369. struct ocfs2_chain_rec *rec,
  370. struct ocfs2_info_freefrag *ffg,
  371. u32 chunks_in_group)
  372. {
  373. int status = 0, used;
  374. u64 blkno;
  375. struct buffer_head *bh = NULL;
  376. struct ocfs2_group_desc *bg = NULL;
  377. unsigned int max_bits, num_clusters;
  378. unsigned int offset = 0, cluster, chunk;
  379. unsigned int chunk_free, last_chunksize = 0;
  380. if (!le32_to_cpu(rec->c_free))
  381. goto bail;
  382. do {
  383. if (!bg)
  384. blkno = le64_to_cpu(rec->c_blkno);
  385. else
  386. blkno = le64_to_cpu(bg->bg_next_group);
  387. if (bh) {
  388. brelse(bh);
  389. bh = NULL;
  390. }
  391. if (o2info_coherent(&ffg->iff_req))
  392. status = ocfs2_read_group_descriptor(gb_inode,
  393. gb_dinode,
  394. blkno, &bh);
  395. else
  396. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  397. if (status < 0) {
  398. mlog(ML_ERROR, "Can't read the group descriptor # "
  399. "%llu from device.", (unsigned long long)blkno);
  400. status = -EIO;
  401. goto bail;
  402. }
  403. bg = (struct ocfs2_group_desc *)bh->b_data;
  404. if (!le16_to_cpu(bg->bg_free_bits_count))
  405. continue;
  406. max_bits = le16_to_cpu(bg->bg_bits);
  407. offset = 0;
  408. for (chunk = 0; chunk < chunks_in_group; chunk++) {
  409. /*
  410. * last chunk may be not an entire one.
  411. */
  412. if ((offset + ffg->iff_chunksize) > max_bits)
  413. num_clusters = max_bits - offset;
  414. else
  415. num_clusters = ffg->iff_chunksize;
  416. chunk_free = 0;
  417. for (cluster = 0; cluster < num_clusters; cluster++) {
  418. used = ocfs2_test_bit(offset,
  419. (unsigned long *)bg->bg_bitmap);
  420. /*
  421. * - chunk_free counts free clusters in #N chunk.
  422. * - last_chunksize records the size(in) clusters
  423. * for the last real free chunk being counted.
  424. */
  425. if (!used) {
  426. last_chunksize++;
  427. chunk_free++;
  428. }
  429. if (used && last_chunksize) {
  430. ocfs2_info_update_ffg(ffg,
  431. last_chunksize);
  432. last_chunksize = 0;
  433. }
  434. offset++;
  435. }
  436. if (chunk_free == ffg->iff_chunksize)
  437. ffg->iff_ffs.ffs_free_chunks++;
  438. }
  439. /*
  440. * need to update the info for last free chunk.
  441. */
  442. if (last_chunksize)
  443. ocfs2_info_update_ffg(ffg, last_chunksize);
  444. } while (le64_to_cpu(bg->bg_next_group));
  445. bail:
  446. brelse(bh);
  447. return status;
  448. }
  449. int ocfs2_info_freefrag_scan_bitmap(struct ocfs2_super *osb,
  450. struct inode *gb_inode, u64 blkno,
  451. struct ocfs2_info_freefrag *ffg)
  452. {
  453. u32 chunks_in_group;
  454. int status = 0, unlock = 0, i;
  455. struct buffer_head *bh = NULL;
  456. struct ocfs2_chain_list *cl = NULL;
  457. struct ocfs2_chain_rec *rec = NULL;
  458. struct ocfs2_dinode *gb_dinode = NULL;
  459. if (gb_inode)
  460. mutex_lock(&gb_inode->i_mutex);
  461. if (o2info_coherent(&ffg->iff_req)) {
  462. status = ocfs2_inode_lock(gb_inode, &bh, 0);
  463. if (status < 0) {
  464. mlog_errno(status);
  465. goto bail;
  466. }
  467. unlock = 1;
  468. } else {
  469. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  470. if (status < 0) {
  471. mlog_errno(status);
  472. goto bail;
  473. }
  474. }
  475. gb_dinode = (struct ocfs2_dinode *)bh->b_data;
  476. cl = &(gb_dinode->id2.i_chain);
  477. /*
  478. * Chunksize(in) clusters from userspace should be
  479. * less than clusters in a group.
  480. */
  481. if (ffg->iff_chunksize > le16_to_cpu(cl->cl_cpg)) {
  482. status = -EINVAL;
  483. goto bail;
  484. }
  485. memset(&ffg->iff_ffs, 0, sizeof(struct ocfs2_info_freefrag_stats));
  486. ffg->iff_ffs.ffs_min = ~0U;
  487. ffg->iff_ffs.ffs_clusters =
  488. le32_to_cpu(gb_dinode->id1.bitmap1.i_total);
  489. ffg->iff_ffs.ffs_free_clusters = ffg->iff_ffs.ffs_clusters -
  490. le32_to_cpu(gb_dinode->id1.bitmap1.i_used);
  491. chunks_in_group = le16_to_cpu(cl->cl_cpg) / ffg->iff_chunksize + 1;
  492. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  493. rec = &(cl->cl_recs[i]);
  494. status = ocfs2_info_freefrag_scan_chain(osb, gb_inode,
  495. gb_dinode,
  496. rec, ffg,
  497. chunks_in_group);
  498. if (status)
  499. goto bail;
  500. }
  501. if (ffg->iff_ffs.ffs_free_chunks_real)
  502. ffg->iff_ffs.ffs_avg = (ffg->iff_ffs.ffs_avg /
  503. ffg->iff_ffs.ffs_free_chunks_real);
  504. bail:
  505. if (unlock)
  506. ocfs2_inode_unlock(gb_inode, 0);
  507. if (gb_inode)
  508. mutex_unlock(&gb_inode->i_mutex);
  509. if (gb_inode)
  510. iput(gb_inode);
  511. brelse(bh);
  512. return status;
  513. }
  514. int ocfs2_info_handle_freefrag(struct inode *inode,
  515. struct ocfs2_info_request __user *req)
  516. {
  517. u64 blkno = -1;
  518. char namebuf[40];
  519. int status = -EFAULT, type = GLOBAL_BITMAP_SYSTEM_INODE;
  520. struct ocfs2_info_freefrag *oiff;
  521. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  522. struct inode *gb_inode = NULL;
  523. oiff = kzalloc(sizeof(struct ocfs2_info_freefrag), GFP_KERNEL);
  524. if (!oiff) {
  525. status = -ENOMEM;
  526. mlog_errno(status);
  527. goto out_err;
  528. }
  529. if (o2info_from_user(*oiff, req))
  530. goto bail;
  531. /*
  532. * chunksize from userspace should be power of 2.
  533. */
  534. if ((oiff->iff_chunksize & (oiff->iff_chunksize - 1)) ||
  535. (!oiff->iff_chunksize)) {
  536. status = -EINVAL;
  537. goto bail;
  538. }
  539. if (o2info_coherent(&oiff->iff_req)) {
  540. gb_inode = ocfs2_get_system_file_inode(osb, type,
  541. OCFS2_INVALID_SLOT);
  542. if (!gb_inode) {
  543. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  544. status = -EIO;
  545. goto bail;
  546. }
  547. } else {
  548. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type,
  549. OCFS2_INVALID_SLOT);
  550. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  551. namebuf,
  552. strlen(namebuf),
  553. &blkno);
  554. if (status < 0) {
  555. status = -ENOENT;
  556. goto bail;
  557. }
  558. }
  559. status = ocfs2_info_freefrag_scan_bitmap(osb, gb_inode, blkno, oiff);
  560. if (status < 0)
  561. goto bail;
  562. o2info_set_request_filled(&oiff->iff_req);
  563. if (o2info_to_user(*oiff, req)) {
  564. status = -EFAULT;
  565. goto bail;
  566. }
  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 __user *)(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 = file_inode(filp);
  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 __user *old_path;
  723. const char __user *new_path;
  724. bool preserve;
  725. struct ocfs2_info info;
  726. void __user *argp = (void __user *)arg;
  727. switch (cmd) {
  728. case OCFS2_IOC_GETFLAGS:
  729. status = ocfs2_get_inode_attr(inode, &flags);
  730. if (status < 0)
  731. return status;
  732. flags &= OCFS2_FL_VISIBLE;
  733. return put_user(flags, (int __user *) arg);
  734. case OCFS2_IOC_SETFLAGS:
  735. if (get_user(flags, (int __user *) arg))
  736. return -EFAULT;
  737. status = mnt_want_write_file(filp);
  738. if (status)
  739. return status;
  740. status = ocfs2_set_inode_attr(inode, flags,
  741. OCFS2_FL_MODIFIABLE);
  742. mnt_drop_write_file(filp);
  743. return status;
  744. case OCFS2_IOC_RESVSP:
  745. case OCFS2_IOC_RESVSP64:
  746. case OCFS2_IOC_UNRESVSP:
  747. case OCFS2_IOC_UNRESVSP64:
  748. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  749. return -EFAULT;
  750. return ocfs2_change_file_space(filp, cmd, &sr);
  751. case OCFS2_IOC_GROUP_EXTEND:
  752. if (!capable(CAP_SYS_RESOURCE))
  753. return -EPERM;
  754. if (get_user(new_clusters, (int __user *)arg))
  755. return -EFAULT;
  756. status = mnt_want_write_file(filp);
  757. if (status)
  758. return status;
  759. status = ocfs2_group_extend(inode, new_clusters);
  760. mnt_drop_write_file(filp);
  761. return status;
  762. case OCFS2_IOC_GROUP_ADD:
  763. case OCFS2_IOC_GROUP_ADD64:
  764. if (!capable(CAP_SYS_RESOURCE))
  765. return -EPERM;
  766. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  767. return -EFAULT;
  768. status = mnt_want_write_file(filp);
  769. if (status)
  770. return status;
  771. status = ocfs2_group_add(inode, &input);
  772. mnt_drop_write_file(filp);
  773. return status;
  774. case OCFS2_IOC_REFLINK:
  775. if (copy_from_user(&args, argp, sizeof(args)))
  776. return -EFAULT;
  777. old_path = (const char __user *)(unsigned long)args.old_path;
  778. new_path = (const char __user *)(unsigned long)args.new_path;
  779. preserve = (args.preserve != 0);
  780. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  781. case OCFS2_IOC_INFO:
  782. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  783. return -EFAULT;
  784. return ocfs2_info_handle(inode, &info, 0);
  785. case FITRIM:
  786. {
  787. struct super_block *sb = inode->i_sb;
  788. struct fstrim_range range;
  789. int ret = 0;
  790. if (!capable(CAP_SYS_ADMIN))
  791. return -EPERM;
  792. if (copy_from_user(&range, argp, sizeof(range)))
  793. return -EFAULT;
  794. ret = ocfs2_trim_fs(sb, &range);
  795. if (ret < 0)
  796. return ret;
  797. if (copy_to_user(argp, &range, sizeof(range)))
  798. return -EFAULT;
  799. return 0;
  800. }
  801. case OCFS2_IOC_MOVE_EXT:
  802. return ocfs2_ioctl_move_extents(filp, argp);
  803. default:
  804. return -ENOTTY;
  805. }
  806. }
  807. #ifdef CONFIG_COMPAT
  808. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  809. {
  810. bool preserve;
  811. struct reflink_arguments args;
  812. struct inode *inode = file_inode(file);
  813. struct ocfs2_info info;
  814. void __user *argp = (void __user *)arg;
  815. switch (cmd) {
  816. case OCFS2_IOC32_GETFLAGS:
  817. cmd = OCFS2_IOC_GETFLAGS;
  818. break;
  819. case OCFS2_IOC32_SETFLAGS:
  820. cmd = OCFS2_IOC_SETFLAGS;
  821. break;
  822. case OCFS2_IOC_RESVSP:
  823. case OCFS2_IOC_RESVSP64:
  824. case OCFS2_IOC_UNRESVSP:
  825. case OCFS2_IOC_UNRESVSP64:
  826. case OCFS2_IOC_GROUP_EXTEND:
  827. case OCFS2_IOC_GROUP_ADD:
  828. case OCFS2_IOC_GROUP_ADD64:
  829. case FITRIM:
  830. break;
  831. case OCFS2_IOC_REFLINK:
  832. if (copy_from_user(&args, argp, sizeof(args)))
  833. return -EFAULT;
  834. preserve = (args.preserve != 0);
  835. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  836. compat_ptr(args.new_path), preserve);
  837. case OCFS2_IOC_INFO:
  838. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  839. return -EFAULT;
  840. return ocfs2_info_handle(inode, &info, 1);
  841. case OCFS2_IOC_MOVE_EXT:
  842. break;
  843. default:
  844. return -ENOIOCTLCMD;
  845. }
  846. return ocfs2_ioctl(file, cmd, arg);
  847. }
  848. #endif