ioctl.c 23 KB

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