ioctl.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. 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. 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. mutex_lock(&inode_alloc->i_mutex);
  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. mutex_unlock(&inode_alloc->i_mutex);
  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. mutex_lock(&gb_inode->i_mutex);
  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. mutex_unlock(&gb_inode->i_mutex);
  480. if (gb_inode)
  481. iput(gb_inode);
  482. brelse(bh);
  483. return status;
  484. }
  485. static int ocfs2_info_handle_freefrag(struct inode *inode,
  486. struct ocfs2_info_request __user *req)
  487. {
  488. u64 blkno = -1;
  489. char namebuf[40];
  490. int status, type = GLOBAL_BITMAP_SYSTEM_INODE;
  491. struct ocfs2_info_freefrag *oiff;
  492. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  493. struct inode *gb_inode = NULL;
  494. oiff = kzalloc(sizeof(struct ocfs2_info_freefrag), GFP_KERNEL);
  495. if (!oiff) {
  496. status = -ENOMEM;
  497. mlog_errno(status);
  498. goto out_err;
  499. }
  500. if (o2info_from_user(*oiff, req)) {
  501. status = -EFAULT;
  502. goto out_free;
  503. }
  504. /*
  505. * chunksize from userspace should be power of 2.
  506. */
  507. if ((oiff->iff_chunksize & (oiff->iff_chunksize - 1)) ||
  508. (!oiff->iff_chunksize)) {
  509. status = -EINVAL;
  510. goto bail;
  511. }
  512. if (o2info_coherent(&oiff->iff_req)) {
  513. gb_inode = ocfs2_get_system_file_inode(osb, type,
  514. OCFS2_INVALID_SLOT);
  515. if (!gb_inode) {
  516. mlog(ML_ERROR, "unable to get global_bitmap inode\n");
  517. status = -EIO;
  518. goto bail;
  519. }
  520. } else {
  521. ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type,
  522. OCFS2_INVALID_SLOT);
  523. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  524. namebuf,
  525. strlen(namebuf),
  526. &blkno);
  527. if (status < 0) {
  528. status = -ENOENT;
  529. goto bail;
  530. }
  531. }
  532. status = ocfs2_info_freefrag_scan_bitmap(osb, gb_inode, blkno, oiff);
  533. if (status < 0)
  534. goto bail;
  535. o2info_set_request_filled(&oiff->iff_req);
  536. if (o2info_to_user(*oiff, req)) {
  537. status = -EFAULT;
  538. goto out_free;
  539. }
  540. status = 0;
  541. bail:
  542. if (status)
  543. o2info_set_request_error(&oiff->iff_req, req);
  544. out_free:
  545. kfree(oiff);
  546. out_err:
  547. return status;
  548. }
  549. static int ocfs2_info_handle_unknown(struct inode *inode,
  550. struct ocfs2_info_request __user *req)
  551. {
  552. struct ocfs2_info_request oir;
  553. if (o2info_from_user(oir, req))
  554. return -EFAULT;
  555. o2info_clear_request_filled(&oir);
  556. if (o2info_to_user(oir, req))
  557. return -EFAULT;
  558. return 0;
  559. }
  560. /*
  561. * Validate and distinguish OCFS2_IOC_INFO requests.
  562. *
  563. * - validate the magic number.
  564. * - distinguish different requests.
  565. * - validate size of different requests.
  566. */
  567. static int ocfs2_info_handle_request(struct inode *inode,
  568. struct ocfs2_info_request __user *req)
  569. {
  570. int status = -EFAULT;
  571. struct ocfs2_info_request oir;
  572. if (o2info_from_user(oir, req))
  573. goto bail;
  574. status = -EINVAL;
  575. if (oir.ir_magic != OCFS2_INFO_MAGIC)
  576. goto bail;
  577. switch (oir.ir_code) {
  578. case OCFS2_INFO_BLOCKSIZE:
  579. if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
  580. status = ocfs2_info_handle_blocksize(inode, req);
  581. break;
  582. case OCFS2_INFO_CLUSTERSIZE:
  583. if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
  584. status = ocfs2_info_handle_clustersize(inode, req);
  585. break;
  586. case OCFS2_INFO_MAXSLOTS:
  587. if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
  588. status = ocfs2_info_handle_maxslots(inode, req);
  589. break;
  590. case OCFS2_INFO_LABEL:
  591. if (oir.ir_size == sizeof(struct ocfs2_info_label))
  592. status = ocfs2_info_handle_label(inode, req);
  593. break;
  594. case OCFS2_INFO_UUID:
  595. if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
  596. status = ocfs2_info_handle_uuid(inode, req);
  597. break;
  598. case OCFS2_INFO_FS_FEATURES:
  599. if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
  600. status = ocfs2_info_handle_fs_features(inode, req);
  601. break;
  602. case OCFS2_INFO_JOURNAL_SIZE:
  603. if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
  604. status = ocfs2_info_handle_journal_size(inode, req);
  605. break;
  606. case OCFS2_INFO_FREEINODE:
  607. if (oir.ir_size == sizeof(struct ocfs2_info_freeinode))
  608. status = ocfs2_info_handle_freeinode(inode, req);
  609. break;
  610. case OCFS2_INFO_FREEFRAG:
  611. if (oir.ir_size == sizeof(struct ocfs2_info_freefrag))
  612. status = ocfs2_info_handle_freefrag(inode, req);
  613. break;
  614. default:
  615. status = ocfs2_info_handle_unknown(inode, req);
  616. break;
  617. }
  618. bail:
  619. return status;
  620. }
  621. static int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
  622. u64 *req_addr, int compat_flag)
  623. {
  624. int status = -EFAULT;
  625. u64 __user *bp = NULL;
  626. if (compat_flag) {
  627. #ifdef CONFIG_COMPAT
  628. /*
  629. * pointer bp stores the base address of a pointers array,
  630. * which collects all addresses of separate request.
  631. */
  632. bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
  633. #else
  634. BUG();
  635. #endif
  636. } else
  637. bp = (u64 __user *)(unsigned long)(info->oi_requests);
  638. if (o2info_from_user(*req_addr, bp + idx))
  639. goto bail;
  640. status = 0;
  641. bail:
  642. return status;
  643. }
  644. /*
  645. * OCFS2_IOC_INFO handles an array of requests passed from userspace.
  646. *
  647. * ocfs2_info_handle() recevies a large info aggregation, grab and
  648. * validate the request count from header, then break it into small
  649. * pieces, later specific handlers can handle them one by one.
  650. *
  651. * Idea here is to make each separate request small enough to ensure
  652. * a better backward&forward compatibility, since a small piece of
  653. * request will be less likely to be broken if disk layout get changed.
  654. */
  655. static int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
  656. int compat_flag)
  657. {
  658. int i, status = 0;
  659. u64 req_addr;
  660. struct ocfs2_info_request __user *reqp;
  661. if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
  662. (!info->oi_requests)) {
  663. status = -EINVAL;
  664. goto bail;
  665. }
  666. for (i = 0; i < info->oi_count; i++) {
  667. status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
  668. if (status)
  669. break;
  670. reqp = (struct ocfs2_info_request __user *)(unsigned long)req_addr;
  671. if (!reqp) {
  672. status = -EINVAL;
  673. goto bail;
  674. }
  675. status = ocfs2_info_handle_request(inode, reqp);
  676. if (status)
  677. break;
  678. }
  679. bail:
  680. return status;
  681. }
  682. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  683. {
  684. struct inode *inode = file_inode(filp);
  685. unsigned int flags;
  686. int new_clusters;
  687. int status;
  688. struct ocfs2_space_resv sr;
  689. struct ocfs2_new_group_input input;
  690. struct reflink_arguments args;
  691. const char __user *old_path;
  692. const char __user *new_path;
  693. bool preserve;
  694. struct ocfs2_info info;
  695. void __user *argp = (void __user *)arg;
  696. switch (cmd) {
  697. case OCFS2_IOC_GETFLAGS:
  698. status = ocfs2_get_inode_attr(inode, &flags);
  699. if (status < 0)
  700. return status;
  701. flags &= OCFS2_FL_VISIBLE;
  702. return put_user(flags, (int __user *) arg);
  703. case OCFS2_IOC_SETFLAGS:
  704. if (get_user(flags, (int __user *) arg))
  705. return -EFAULT;
  706. status = mnt_want_write_file(filp);
  707. if (status)
  708. return status;
  709. status = ocfs2_set_inode_attr(inode, flags,
  710. OCFS2_FL_MODIFIABLE);
  711. mnt_drop_write_file(filp);
  712. return status;
  713. case OCFS2_IOC_RESVSP:
  714. case OCFS2_IOC_RESVSP64:
  715. case OCFS2_IOC_UNRESVSP:
  716. case OCFS2_IOC_UNRESVSP64:
  717. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  718. return -EFAULT;
  719. return ocfs2_change_file_space(filp, cmd, &sr);
  720. case OCFS2_IOC_GROUP_EXTEND:
  721. if (!capable(CAP_SYS_RESOURCE))
  722. return -EPERM;
  723. if (get_user(new_clusters, (int __user *)arg))
  724. return -EFAULT;
  725. status = mnt_want_write_file(filp);
  726. if (status)
  727. return status;
  728. status = ocfs2_group_extend(inode, new_clusters);
  729. mnt_drop_write_file(filp);
  730. return status;
  731. case OCFS2_IOC_GROUP_ADD:
  732. case OCFS2_IOC_GROUP_ADD64:
  733. if (!capable(CAP_SYS_RESOURCE))
  734. return -EPERM;
  735. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  736. return -EFAULT;
  737. status = mnt_want_write_file(filp);
  738. if (status)
  739. return status;
  740. status = ocfs2_group_add(inode, &input);
  741. mnt_drop_write_file(filp);
  742. return status;
  743. case OCFS2_IOC_REFLINK:
  744. if (copy_from_user(&args, argp, sizeof(args)))
  745. return -EFAULT;
  746. old_path = (const char __user *)(unsigned long)args.old_path;
  747. new_path = (const char __user *)(unsigned long)args.new_path;
  748. preserve = (args.preserve != 0);
  749. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  750. case OCFS2_IOC_INFO:
  751. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  752. return -EFAULT;
  753. return ocfs2_info_handle(inode, &info, 0);
  754. case FITRIM:
  755. {
  756. struct super_block *sb = inode->i_sb;
  757. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  758. struct fstrim_range range;
  759. int ret = 0;
  760. if (!capable(CAP_SYS_ADMIN))
  761. return -EPERM;
  762. if (!blk_queue_discard(q))
  763. return -EOPNOTSUPP;
  764. if (copy_from_user(&range, argp, sizeof(range)))
  765. return -EFAULT;
  766. range.minlen = max_t(u64, q->limits.discard_granularity,
  767. range.minlen);
  768. ret = ocfs2_trim_fs(sb, &range);
  769. if (ret < 0)
  770. return ret;
  771. if (copy_to_user(argp, &range, sizeof(range)))
  772. return -EFAULT;
  773. return 0;
  774. }
  775. case OCFS2_IOC_MOVE_EXT:
  776. return ocfs2_ioctl_move_extents(filp, argp);
  777. default:
  778. return -ENOTTY;
  779. }
  780. }
  781. #ifdef CONFIG_COMPAT
  782. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  783. {
  784. bool preserve;
  785. struct reflink_arguments args;
  786. struct inode *inode = file_inode(file);
  787. struct ocfs2_info info;
  788. void __user *argp = (void __user *)arg;
  789. switch (cmd) {
  790. case OCFS2_IOC32_GETFLAGS:
  791. cmd = OCFS2_IOC_GETFLAGS;
  792. break;
  793. case OCFS2_IOC32_SETFLAGS:
  794. cmd = OCFS2_IOC_SETFLAGS;
  795. break;
  796. case OCFS2_IOC_RESVSP:
  797. case OCFS2_IOC_RESVSP64:
  798. case OCFS2_IOC_UNRESVSP:
  799. case OCFS2_IOC_UNRESVSP64:
  800. case OCFS2_IOC_GROUP_EXTEND:
  801. case OCFS2_IOC_GROUP_ADD:
  802. case OCFS2_IOC_GROUP_ADD64:
  803. case FITRIM:
  804. break;
  805. case OCFS2_IOC_REFLINK:
  806. if (copy_from_user(&args, argp, sizeof(args)))
  807. return -EFAULT;
  808. preserve = (args.preserve != 0);
  809. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  810. compat_ptr(args.new_path), preserve);
  811. case OCFS2_IOC_INFO:
  812. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  813. return -EFAULT;
  814. return ocfs2_info_handle(inode, &info, 1);
  815. case OCFS2_IOC_MOVE_EXT:
  816. break;
  817. default:
  818. return -ENOIOCTLCMD;
  819. }
  820. return ocfs2_ioctl(file, cmd, arg);
  821. }
  822. #endif