ioctl.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. #include <linux/ext2_fs.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. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  88. if (IS_ERR(handle)) {
  89. status = PTR_ERR(handle);
  90. mlog_errno(status);
  91. goto bail_unlock;
  92. }
  93. oldflags = ocfs2_inode->ip_attr;
  94. flags = flags & mask;
  95. flags |= oldflags & ~mask;
  96. /*
  97. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  98. * the relevant capability.
  99. */
  100. status = -EPERM;
  101. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  102. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  103. if (!capable(CAP_LINUX_IMMUTABLE))
  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. int ocfs2_info_handle_blocksize(struct inode *inode,
  120. struct ocfs2_info_request __user *req)
  121. {
  122. int status = -EFAULT;
  123. struct ocfs2_info_blocksize oib;
  124. if (o2info_from_user(oib, req))
  125. goto bail;
  126. oib.ib_blocksize = inode->i_sb->s_blocksize;
  127. o2info_set_request_filled(&oib.ib_req);
  128. if (o2info_to_user(oib, req))
  129. goto bail;
  130. status = 0;
  131. bail:
  132. if (status)
  133. o2info_set_request_error(&oib.ib_req, req);
  134. return status;
  135. }
  136. int ocfs2_info_handle_clustersize(struct inode *inode,
  137. struct ocfs2_info_request __user *req)
  138. {
  139. int status = -EFAULT;
  140. struct ocfs2_info_clustersize oic;
  141. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  142. if (o2info_from_user(oic, req))
  143. goto bail;
  144. oic.ic_clustersize = osb->s_clustersize;
  145. o2info_set_request_filled(&oic.ic_req);
  146. if (o2info_to_user(oic, req))
  147. goto bail;
  148. status = 0;
  149. bail:
  150. if (status)
  151. o2info_set_request_error(&oic.ic_req, req);
  152. return status;
  153. }
  154. int ocfs2_info_handle_maxslots(struct inode *inode,
  155. struct ocfs2_info_request __user *req)
  156. {
  157. int status = -EFAULT;
  158. struct ocfs2_info_maxslots oim;
  159. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  160. if (o2info_from_user(oim, req))
  161. goto bail;
  162. oim.im_max_slots = osb->max_slots;
  163. o2info_set_request_filled(&oim.im_req);
  164. if (o2info_to_user(oim, req))
  165. goto bail;
  166. status = 0;
  167. bail:
  168. if (status)
  169. o2info_set_request_error(&oim.im_req, req);
  170. return status;
  171. }
  172. int ocfs2_info_handle_label(struct inode *inode,
  173. struct ocfs2_info_request __user *req)
  174. {
  175. int status = -EFAULT;
  176. struct ocfs2_info_label oil;
  177. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  178. if (o2info_from_user(oil, req))
  179. goto bail;
  180. memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
  181. o2info_set_request_filled(&oil.il_req);
  182. if (o2info_to_user(oil, req))
  183. goto bail;
  184. status = 0;
  185. bail:
  186. if (status)
  187. o2info_set_request_error(&oil.il_req, req);
  188. return status;
  189. }
  190. int ocfs2_info_handle_uuid(struct inode *inode,
  191. struct ocfs2_info_request __user *req)
  192. {
  193. int status = -EFAULT;
  194. struct ocfs2_info_uuid oiu;
  195. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  196. if (o2info_from_user(oiu, req))
  197. goto bail;
  198. memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
  199. o2info_set_request_filled(&oiu.iu_req);
  200. if (o2info_to_user(oiu, req))
  201. goto bail;
  202. status = 0;
  203. bail:
  204. if (status)
  205. o2info_set_request_error(&oiu.iu_req, req);
  206. return status;
  207. }
  208. int ocfs2_info_handle_fs_features(struct inode *inode,
  209. struct ocfs2_info_request __user *req)
  210. {
  211. int status = -EFAULT;
  212. struct ocfs2_info_fs_features oif;
  213. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  214. if (o2info_from_user(oif, req))
  215. goto bail;
  216. oif.if_compat_features = osb->s_feature_compat;
  217. oif.if_incompat_features = osb->s_feature_incompat;
  218. oif.if_ro_compat_features = osb->s_feature_ro_compat;
  219. o2info_set_request_filled(&oif.if_req);
  220. if (o2info_to_user(oif, req))
  221. goto bail;
  222. status = 0;
  223. bail:
  224. if (status)
  225. o2info_set_request_error(&oif.if_req, req);
  226. return status;
  227. }
  228. int ocfs2_info_handle_journal_size(struct inode *inode,
  229. struct ocfs2_info_request __user *req)
  230. {
  231. int status = -EFAULT;
  232. struct ocfs2_info_journal_size oij;
  233. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  234. if (o2info_from_user(oij, req))
  235. goto bail;
  236. oij.ij_journal_size = osb->journal->j_inode->i_size;
  237. o2info_set_request_filled(&oij.ij_req);
  238. if (o2info_to_user(oij, req))
  239. goto bail;
  240. status = 0;
  241. bail:
  242. if (status)
  243. o2info_set_request_error(&oij.ij_req, req);
  244. return status;
  245. }
  246. int ocfs2_info_scan_inode_alloc(struct ocfs2_super *osb,
  247. struct inode *inode_alloc, u64 blkno,
  248. struct ocfs2_info_freeinode *fi, u32 slot)
  249. {
  250. int status = 0, unlock = 0;
  251. struct buffer_head *bh = NULL;
  252. struct ocfs2_dinode *dinode_alloc = NULL;
  253. if (inode_alloc)
  254. mutex_lock(&inode_alloc->i_mutex);
  255. if (o2info_coherent(&fi->ifi_req)) {
  256. status = ocfs2_inode_lock(inode_alloc, &bh, 0);
  257. if (status < 0) {
  258. mlog_errno(status);
  259. goto bail;
  260. }
  261. unlock = 1;
  262. } else {
  263. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  264. if (status < 0) {
  265. mlog_errno(status);
  266. goto bail;
  267. }
  268. }
  269. dinode_alloc = (struct ocfs2_dinode *)bh->b_data;
  270. fi->ifi_stat[slot].lfi_total =
  271. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total);
  272. fi->ifi_stat[slot].lfi_free =
  273. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total) -
  274. le32_to_cpu(dinode_alloc->id1.bitmap1.i_used);
  275. bail:
  276. if (unlock)
  277. ocfs2_inode_unlock(inode_alloc, 0);
  278. if (inode_alloc)
  279. mutex_unlock(&inode_alloc->i_mutex);
  280. brelse(bh);
  281. return status;
  282. }
  283. int ocfs2_info_handle_freeinode(struct inode *inode,
  284. struct ocfs2_info_request __user *req)
  285. {
  286. u32 i;
  287. u64 blkno = -1;
  288. char namebuf[40];
  289. int status = -EFAULT, type = INODE_ALLOC_SYSTEM_INODE;
  290. struct ocfs2_info_freeinode *oifi = NULL;
  291. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  292. struct inode *inode_alloc = NULL;
  293. oifi = kzalloc(sizeof(struct ocfs2_info_freeinode), GFP_KERNEL);
  294. if (!oifi) {
  295. status = -ENOMEM;
  296. mlog_errno(status);
  297. goto bail;
  298. }
  299. if (o2info_from_user(*oifi, req))
  300. goto bail;
  301. oifi->ifi_slotnum = osb->max_slots;
  302. for (i = 0; i < oifi->ifi_slotnum; i++) {
  303. if (o2info_coherent(&oifi->ifi_req)) {
  304. inode_alloc = ocfs2_get_system_file_inode(osb, type, i);
  305. if (!inode_alloc) {
  306. mlog(ML_ERROR, "unable to get alloc inode in "
  307. "slot %u\n", i);
  308. status = -EIO;
  309. goto bail;
  310. }
  311. } else {
  312. ocfs2_sprintf_system_inode_name(namebuf,
  313. sizeof(namebuf),
  314. type, i);
  315. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  316. namebuf,
  317. strlen(namebuf),
  318. &blkno);
  319. if (status < 0) {
  320. status = -ENOENT;
  321. goto bail;
  322. }
  323. }
  324. status = ocfs2_info_scan_inode_alloc(osb, inode_alloc, blkno, oifi, i);
  325. if (status < 0)
  326. goto bail;
  327. iput(inode_alloc);
  328. inode_alloc = NULL;
  329. }
  330. o2info_set_request_filled(&oifi->ifi_req);
  331. if (o2info_to_user(*oifi, req))
  332. goto bail;
  333. status = 0;
  334. bail:
  335. if (status)
  336. o2info_set_request_error(&oifi->ifi_req, req);
  337. kfree(oifi);
  338. 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 bail;
  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. goto bail;
  565. status = 0;
  566. bail:
  567. if (status)
  568. o2info_set_request_error(&oiff->iff_req, req);
  569. kfree(oiff);
  570. return status;
  571. }
  572. int ocfs2_info_handle_unknown(struct inode *inode,
  573. struct ocfs2_info_request __user *req)
  574. {
  575. int status = -EFAULT;
  576. struct ocfs2_info_request oir;
  577. if (o2info_from_user(oir, req))
  578. goto bail;
  579. o2info_clear_request_filled(&oir);
  580. if (o2info_to_user(oir, req))
  581. goto bail;
  582. status = 0;
  583. bail:
  584. if (status)
  585. o2info_set_request_error(&oir, req);
  586. return status;
  587. }
  588. /*
  589. * Validate and distinguish OCFS2_IOC_INFO requests.
  590. *
  591. * - validate the magic number.
  592. * - distinguish different requests.
  593. * - validate size of different requests.
  594. */
  595. int ocfs2_info_handle_request(struct inode *inode,
  596. struct ocfs2_info_request __user *req)
  597. {
  598. int status = -EFAULT;
  599. struct ocfs2_info_request oir;
  600. if (o2info_from_user(oir, req))
  601. goto bail;
  602. status = -EINVAL;
  603. if (oir.ir_magic != OCFS2_INFO_MAGIC)
  604. goto bail;
  605. switch (oir.ir_code) {
  606. case OCFS2_INFO_BLOCKSIZE:
  607. if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
  608. status = ocfs2_info_handle_blocksize(inode, req);
  609. break;
  610. case OCFS2_INFO_CLUSTERSIZE:
  611. if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
  612. status = ocfs2_info_handle_clustersize(inode, req);
  613. break;
  614. case OCFS2_INFO_MAXSLOTS:
  615. if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
  616. status = ocfs2_info_handle_maxslots(inode, req);
  617. break;
  618. case OCFS2_INFO_LABEL:
  619. if (oir.ir_size == sizeof(struct ocfs2_info_label))
  620. status = ocfs2_info_handle_label(inode, req);
  621. break;
  622. case OCFS2_INFO_UUID:
  623. if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
  624. status = ocfs2_info_handle_uuid(inode, req);
  625. break;
  626. case OCFS2_INFO_FS_FEATURES:
  627. if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
  628. status = ocfs2_info_handle_fs_features(inode, req);
  629. break;
  630. case OCFS2_INFO_JOURNAL_SIZE:
  631. if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
  632. status = ocfs2_info_handle_journal_size(inode, req);
  633. break;
  634. case OCFS2_INFO_FREEINODE:
  635. if (oir.ir_size == sizeof(struct ocfs2_info_freeinode))
  636. status = ocfs2_info_handle_freeinode(inode, req);
  637. break;
  638. case OCFS2_INFO_FREEFRAG:
  639. if (oir.ir_size == sizeof(struct ocfs2_info_freefrag))
  640. status = ocfs2_info_handle_freefrag(inode, req);
  641. break;
  642. default:
  643. status = ocfs2_info_handle_unknown(inode, req);
  644. break;
  645. }
  646. bail:
  647. return status;
  648. }
  649. int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
  650. u64 *req_addr, int compat_flag)
  651. {
  652. int status = -EFAULT;
  653. u64 __user *bp = NULL;
  654. if (compat_flag) {
  655. #ifdef CONFIG_COMPAT
  656. /*
  657. * pointer bp stores the base address of a pointers array,
  658. * which collects all addresses of separate request.
  659. */
  660. bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
  661. #else
  662. BUG();
  663. #endif
  664. } else
  665. bp = (u64 __user *)(unsigned long)(info->oi_requests);
  666. if (o2info_from_user(*req_addr, bp + idx))
  667. goto bail;
  668. status = 0;
  669. bail:
  670. return status;
  671. }
  672. /*
  673. * OCFS2_IOC_INFO handles an array of requests passed from userspace.
  674. *
  675. * ocfs2_info_handle() recevies a large info aggregation, grab and
  676. * validate the request count from header, then break it into small
  677. * pieces, later specific handlers can handle them one by one.
  678. *
  679. * Idea here is to make each separate request small enough to ensure
  680. * a better backward&forward compatibility, since a small piece of
  681. * request will be less likely to be broken if disk layout get changed.
  682. */
  683. int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
  684. int compat_flag)
  685. {
  686. int i, status = 0;
  687. u64 req_addr;
  688. struct ocfs2_info_request __user *reqp;
  689. if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
  690. (!info->oi_requests)) {
  691. status = -EINVAL;
  692. goto bail;
  693. }
  694. for (i = 0; i < info->oi_count; i++) {
  695. status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
  696. if (status)
  697. break;
  698. reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
  699. if (!reqp) {
  700. status = -EINVAL;
  701. goto bail;
  702. }
  703. status = ocfs2_info_handle_request(inode, reqp);
  704. if (status)
  705. break;
  706. }
  707. bail:
  708. return status;
  709. }
  710. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  711. {
  712. struct inode *inode = filp->f_path.dentry->d_inode;
  713. unsigned int flags;
  714. int new_clusters;
  715. int status;
  716. struct ocfs2_space_resv sr;
  717. struct ocfs2_new_group_input input;
  718. struct reflink_arguments args;
  719. const char *old_path, *new_path;
  720. bool preserve;
  721. struct ocfs2_info info;
  722. switch (cmd) {
  723. case OCFS2_IOC_GETFLAGS:
  724. status = ocfs2_get_inode_attr(inode, &flags);
  725. if (status < 0)
  726. return status;
  727. flags &= OCFS2_FL_VISIBLE;
  728. return put_user(flags, (int __user *) arg);
  729. case OCFS2_IOC_SETFLAGS:
  730. if (get_user(flags, (int __user *) arg))
  731. return -EFAULT;
  732. status = mnt_want_write(filp->f_path.mnt);
  733. if (status)
  734. return status;
  735. status = ocfs2_set_inode_attr(inode, flags,
  736. OCFS2_FL_MODIFIABLE);
  737. mnt_drop_write(filp->f_path.mnt);
  738. return status;
  739. case OCFS2_IOC_RESVSP:
  740. case OCFS2_IOC_RESVSP64:
  741. case OCFS2_IOC_UNRESVSP:
  742. case OCFS2_IOC_UNRESVSP64:
  743. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  744. return -EFAULT;
  745. return ocfs2_change_file_space(filp, cmd, &sr);
  746. case OCFS2_IOC_GROUP_EXTEND:
  747. if (!capable(CAP_SYS_RESOURCE))
  748. return -EPERM;
  749. if (get_user(new_clusters, (int __user *)arg))
  750. return -EFAULT;
  751. return ocfs2_group_extend(inode, new_clusters);
  752. case OCFS2_IOC_GROUP_ADD:
  753. case OCFS2_IOC_GROUP_ADD64:
  754. if (!capable(CAP_SYS_RESOURCE))
  755. return -EPERM;
  756. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  757. return -EFAULT;
  758. return ocfs2_group_add(inode, &input);
  759. case OCFS2_IOC_REFLINK:
  760. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  761. sizeof(args)))
  762. return -EFAULT;
  763. old_path = (const char *)(unsigned long)args.old_path;
  764. new_path = (const char *)(unsigned long)args.new_path;
  765. preserve = (args.preserve != 0);
  766. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  767. case OCFS2_IOC_INFO:
  768. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  769. sizeof(struct ocfs2_info)))
  770. return -EFAULT;
  771. return ocfs2_info_handle(inode, &info, 0);
  772. case FITRIM:
  773. {
  774. struct super_block *sb = inode->i_sb;
  775. struct fstrim_range range;
  776. int ret = 0;
  777. if (!capable(CAP_SYS_ADMIN))
  778. return -EPERM;
  779. if (copy_from_user(&range, (struct fstrim_range *)arg,
  780. sizeof(range)))
  781. return -EFAULT;
  782. ret = ocfs2_trim_fs(sb, &range);
  783. if (ret < 0)
  784. return ret;
  785. if (copy_to_user((struct fstrim_range *)arg, &range,
  786. sizeof(range)))
  787. return -EFAULT;
  788. return 0;
  789. }
  790. case OCFS2_IOC_MOVE_EXT:
  791. return ocfs2_ioctl_move_extents(filp, (void __user *)arg);
  792. default:
  793. return -ENOTTY;
  794. }
  795. }
  796. #ifdef CONFIG_COMPAT
  797. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  798. {
  799. bool preserve;
  800. struct reflink_arguments args;
  801. struct inode *inode = file->f_path.dentry->d_inode;
  802. struct ocfs2_info info;
  803. switch (cmd) {
  804. case OCFS2_IOC32_GETFLAGS:
  805. cmd = OCFS2_IOC_GETFLAGS;
  806. break;
  807. case OCFS2_IOC32_SETFLAGS:
  808. cmd = OCFS2_IOC_SETFLAGS;
  809. break;
  810. case OCFS2_IOC_RESVSP:
  811. case OCFS2_IOC_RESVSP64:
  812. case OCFS2_IOC_UNRESVSP:
  813. case OCFS2_IOC_UNRESVSP64:
  814. case OCFS2_IOC_GROUP_EXTEND:
  815. case OCFS2_IOC_GROUP_ADD:
  816. case OCFS2_IOC_GROUP_ADD64:
  817. case FITRIM:
  818. break;
  819. case OCFS2_IOC_REFLINK:
  820. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  821. sizeof(args)))
  822. return -EFAULT;
  823. preserve = (args.preserve != 0);
  824. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  825. compat_ptr(args.new_path), preserve);
  826. case OCFS2_IOC_INFO:
  827. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  828. sizeof(struct ocfs2_info)))
  829. return -EFAULT;
  830. return ocfs2_info_handle(inode, &info, 1);
  831. case OCFS2_IOC_MOVE_EXT:
  832. break;
  833. default:
  834. return -ENOIOCTLCMD;
  835. }
  836. return ocfs2_ioctl(file, cmd, arg);
  837. }
  838. #endif