ioctl.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ocfs2/ioctl.c
  4. *
  5. * Copyright (C) 2006 Herbert Poetzl
  6. * adapted from Remy Card's ext2/ioctl.c
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/mount.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/compat.h>
  12. #include <cluster/masklog.h>
  13. #include "ocfs2.h"
  14. #include "alloc.h"
  15. #include "dlmglue.h"
  16. #include "file.h"
  17. #include "inode.h"
  18. #include "journal.h"
  19. #include "ocfs2_fs.h"
  20. #include "ioctl.h"
  21. #include "resize.h"
  22. #include "refcounttree.h"
  23. #include "sysfile.h"
  24. #include "dir.h"
  25. #include "buffer_head_io.h"
  26. #include "suballoc.h"
  27. #include "move_extents.h"
  28. #define o2info_from_user(a, b) \
  29. copy_from_user(&(a), (b), sizeof(a))
  30. #define o2info_to_user(a, b) \
  31. copy_to_user((typeof(a) __user *)b, &(a), sizeof(a))
  32. /*
  33. * This is just a best-effort to tell userspace that this request
  34. * 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. inode_lock(inode);
  77. status = ocfs2_inode_lock(inode, &bh, 1);
  78. if (status < 0) {
  79. mlog_errno(status);
  80. goto bail;
  81. }
  82. status = -EACCES;
  83. if (!inode_owner_or_capable(inode))
  84. goto bail_unlock;
  85. if (!S_ISDIR(inode->i_mode))
  86. flags &= ~OCFS2_DIRSYNC_FL;
  87. oldflags = ocfs2_inode->ip_attr;
  88. flags = flags & mask;
  89. flags |= oldflags & ~mask;
  90. /*
  91. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  92. * the relevant capability.
  93. */
  94. status = -EPERM;
  95. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  96. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  97. if (!capable(CAP_LINUX_IMMUTABLE))
  98. goto bail_unlock;
  99. }
  100. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  101. if (IS_ERR(handle)) {
  102. status = PTR_ERR(handle);
  103. mlog_errno(status);
  104. goto bail_unlock;
  105. }
  106. ocfs2_inode->ip_attr = flags;
  107. ocfs2_set_inode_flags(inode);
  108. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  109. if (status < 0)
  110. mlog_errno(status);
  111. ocfs2_commit_trans(osb, handle);
  112. bail_unlock:
  113. ocfs2_inode_unlock(inode, 1);
  114. bail:
  115. inode_unlock(inode);
  116. brelse(bh);
  117. return status;
  118. }
  119. static int ocfs2_info_handle_blocksize(struct inode *inode,
  120. struct ocfs2_info_request __user *req)
  121. {
  122. struct ocfs2_info_blocksize oib;
  123. if (o2info_from_user(oib, req))
  124. return -EFAULT;
  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. return -EFAULT;
  129. return 0;
  130. }
  131. static int ocfs2_info_handle_clustersize(struct inode *inode,
  132. struct ocfs2_info_request __user *req)
  133. {
  134. struct ocfs2_info_clustersize oic;
  135. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  136. if (o2info_from_user(oic, req))
  137. return -EFAULT;
  138. oic.ic_clustersize = osb->s_clustersize;
  139. o2info_set_request_filled(&oic.ic_req);
  140. if (o2info_to_user(oic, req))
  141. return -EFAULT;
  142. return 0;
  143. }
  144. static int ocfs2_info_handle_maxslots(struct inode *inode,
  145. struct ocfs2_info_request __user *req)
  146. {
  147. struct ocfs2_info_maxslots oim;
  148. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  149. if (o2info_from_user(oim, req))
  150. return -EFAULT;
  151. oim.im_max_slots = osb->max_slots;
  152. o2info_set_request_filled(&oim.im_req);
  153. if (o2info_to_user(oim, req))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. static int ocfs2_info_handle_label(struct inode *inode,
  158. struct ocfs2_info_request __user *req)
  159. {
  160. struct ocfs2_info_label oil;
  161. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  162. if (o2info_from_user(oil, req))
  163. return -EFAULT;
  164. memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
  165. o2info_set_request_filled(&oil.il_req);
  166. if (o2info_to_user(oil, req))
  167. return -EFAULT;
  168. return 0;
  169. }
  170. static int ocfs2_info_handle_uuid(struct inode *inode,
  171. struct ocfs2_info_request __user *req)
  172. {
  173. struct ocfs2_info_uuid oiu;
  174. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  175. if (o2info_from_user(oiu, req))
  176. return -EFAULT;
  177. memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
  178. o2info_set_request_filled(&oiu.iu_req);
  179. if (o2info_to_user(oiu, req))
  180. return -EFAULT;
  181. return 0;
  182. }
  183. static int ocfs2_info_handle_fs_features(struct inode *inode,
  184. struct ocfs2_info_request __user *req)
  185. {
  186. struct ocfs2_info_fs_features oif;
  187. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  188. if (o2info_from_user(oif, req))
  189. return -EFAULT;
  190. oif.if_compat_features = osb->s_feature_compat;
  191. oif.if_incompat_features = osb->s_feature_incompat;
  192. oif.if_ro_compat_features = osb->s_feature_ro_compat;
  193. o2info_set_request_filled(&oif.if_req);
  194. if (o2info_to_user(oif, req))
  195. return -EFAULT;
  196. return 0;
  197. }
  198. static int ocfs2_info_handle_journal_size(struct inode *inode,
  199. struct ocfs2_info_request __user *req)
  200. {
  201. struct ocfs2_info_journal_size oij;
  202. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  203. if (o2info_from_user(oij, req))
  204. return -EFAULT;
  205. oij.ij_journal_size = i_size_read(osb->journal->j_inode);
  206. o2info_set_request_filled(&oij.ij_req);
  207. if (o2info_to_user(oij, req))
  208. return -EFAULT;
  209. return 0;
  210. }
  211. static int ocfs2_info_scan_inode_alloc(struct ocfs2_super *osb,
  212. struct inode *inode_alloc, u64 blkno,
  213. struct ocfs2_info_freeinode *fi,
  214. u32 slot)
  215. {
  216. int status = 0, unlock = 0;
  217. struct buffer_head *bh = NULL;
  218. struct ocfs2_dinode *dinode_alloc = NULL;
  219. if (inode_alloc)
  220. inode_lock(inode_alloc);
  221. if (inode_alloc && o2info_coherent(&fi->ifi_req)) {
  222. status = ocfs2_inode_lock(inode_alloc, &bh, 0);
  223. if (status < 0) {
  224. mlog_errno(status);
  225. goto bail;
  226. }
  227. unlock = 1;
  228. } else {
  229. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  230. if (status < 0) {
  231. mlog_errno(status);
  232. goto bail;
  233. }
  234. }
  235. dinode_alloc = (struct ocfs2_dinode *)bh->b_data;
  236. fi->ifi_stat[slot].lfi_total =
  237. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total);
  238. fi->ifi_stat[slot].lfi_free =
  239. le32_to_cpu(dinode_alloc->id1.bitmap1.i_total) -
  240. le32_to_cpu(dinode_alloc->id1.bitmap1.i_used);
  241. bail:
  242. if (unlock)
  243. ocfs2_inode_unlock(inode_alloc, 0);
  244. if (inode_alloc)
  245. inode_unlock(inode_alloc);
  246. brelse(bh);
  247. return status;
  248. }
  249. static int ocfs2_info_handle_freeinode(struct inode *inode,
  250. struct ocfs2_info_request __user *req)
  251. {
  252. u32 i;
  253. u64 blkno = -1;
  254. char namebuf[40];
  255. int status, type = INODE_ALLOC_SYSTEM_INODE;
  256. struct ocfs2_info_freeinode *oifi = NULL;
  257. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  258. struct inode *inode_alloc = NULL;
  259. oifi = kzalloc(sizeof(struct ocfs2_info_freeinode), GFP_KERNEL);
  260. if (!oifi) {
  261. status = -ENOMEM;
  262. mlog_errno(status);
  263. goto out_err;
  264. }
  265. if (o2info_from_user(*oifi, req)) {
  266. status = -EFAULT;
  267. goto out_free;
  268. }
  269. oifi->ifi_slotnum = osb->max_slots;
  270. for (i = 0; i < oifi->ifi_slotnum; i++) {
  271. if (o2info_coherent(&oifi->ifi_req)) {
  272. inode_alloc = ocfs2_get_system_file_inode(osb, type, i);
  273. if (!inode_alloc) {
  274. mlog(ML_ERROR, "unable to get alloc inode in "
  275. "slot %u\n", i);
  276. status = -EIO;
  277. goto bail;
  278. }
  279. } else {
  280. ocfs2_sprintf_system_inode_name(namebuf,
  281. sizeof(namebuf),
  282. type, i);
  283. status = ocfs2_lookup_ino_from_name(osb->sys_root_inode,
  284. namebuf,
  285. strlen(namebuf),
  286. &blkno);
  287. if (status < 0) {
  288. status = -ENOENT;
  289. goto bail;
  290. }
  291. }
  292. status = ocfs2_info_scan_inode_alloc(osb, inode_alloc, blkno, oifi, i);
  293. iput(inode_alloc);
  294. inode_alloc = NULL;
  295. if (status < 0)
  296. goto bail;
  297. }
  298. o2info_set_request_filled(&oifi->ifi_req);
  299. if (o2info_to_user(*oifi, req)) {
  300. status = -EFAULT;
  301. goto out_free;
  302. }
  303. status = 0;
  304. bail:
  305. if (status)
  306. o2info_set_request_error(&oifi->ifi_req, req);
  307. out_free:
  308. kfree(oifi);
  309. out_err:
  310. return status;
  311. }
  312. static void o2ffg_update_histogram(struct ocfs2_info_free_chunk_list *hist,
  313. unsigned int chunksize)
  314. {
  315. u32 index;
  316. index = __ilog2_u32(chunksize);
  317. if (index >= OCFS2_INFO_MAX_HIST)
  318. index = OCFS2_INFO_MAX_HIST - 1;
  319. hist->fc_chunks[index]++;
  320. hist->fc_clusters[index] += chunksize;
  321. }
  322. static void o2ffg_update_stats(struct ocfs2_info_freefrag_stats *stats,
  323. unsigned int chunksize)
  324. {
  325. if (chunksize > stats->ffs_max)
  326. stats->ffs_max = chunksize;
  327. if (chunksize < stats->ffs_min)
  328. stats->ffs_min = chunksize;
  329. stats->ffs_avg += chunksize;
  330. stats->ffs_free_chunks_real++;
  331. }
  332. static void ocfs2_info_update_ffg(struct ocfs2_info_freefrag *ffg,
  333. unsigned int chunksize)
  334. {
  335. o2ffg_update_histogram(&(ffg->iff_ffs.ffs_fc_hist), chunksize);
  336. o2ffg_update_stats(&(ffg->iff_ffs), chunksize);
  337. }
  338. static int ocfs2_info_freefrag_scan_chain(struct ocfs2_super *osb,
  339. struct inode *gb_inode,
  340. struct ocfs2_dinode *gb_dinode,
  341. struct ocfs2_chain_rec *rec,
  342. struct ocfs2_info_freefrag *ffg,
  343. u32 chunks_in_group)
  344. {
  345. int status = 0, used;
  346. u64 blkno;
  347. struct buffer_head *bh = NULL;
  348. struct ocfs2_group_desc *bg = NULL;
  349. unsigned int max_bits, num_clusters;
  350. unsigned int offset = 0, cluster, chunk;
  351. unsigned int chunk_free, last_chunksize = 0;
  352. if (!le32_to_cpu(rec->c_free))
  353. goto bail;
  354. do {
  355. if (!bg)
  356. blkno = le64_to_cpu(rec->c_blkno);
  357. else
  358. blkno = le64_to_cpu(bg->bg_next_group);
  359. if (bh) {
  360. brelse(bh);
  361. bh = NULL;
  362. }
  363. if (o2info_coherent(&ffg->iff_req))
  364. status = ocfs2_read_group_descriptor(gb_inode,
  365. gb_dinode,
  366. blkno, &bh);
  367. else
  368. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  369. if (status < 0) {
  370. mlog(ML_ERROR, "Can't read the group descriptor # "
  371. "%llu from device.", (unsigned long long)blkno);
  372. status = -EIO;
  373. goto bail;
  374. }
  375. bg = (struct ocfs2_group_desc *)bh->b_data;
  376. if (!le16_to_cpu(bg->bg_free_bits_count))
  377. continue;
  378. max_bits = le16_to_cpu(bg->bg_bits);
  379. offset = 0;
  380. for (chunk = 0; chunk < chunks_in_group; chunk++) {
  381. /*
  382. * last chunk may be not an entire one.
  383. */
  384. if ((offset + ffg->iff_chunksize) > max_bits)
  385. num_clusters = max_bits - offset;
  386. else
  387. num_clusters = ffg->iff_chunksize;
  388. chunk_free = 0;
  389. for (cluster = 0; cluster < num_clusters; cluster++) {
  390. used = ocfs2_test_bit(offset,
  391. (unsigned long *)bg->bg_bitmap);
  392. /*
  393. * - chunk_free counts free clusters in #N chunk.
  394. * - last_chunksize records the size(in) clusters
  395. * for the last real free chunk being counted.
  396. */
  397. if (!used) {
  398. last_chunksize++;
  399. chunk_free++;
  400. }
  401. if (used && last_chunksize) {
  402. ocfs2_info_update_ffg(ffg,
  403. last_chunksize);
  404. last_chunksize = 0;
  405. }
  406. offset++;
  407. }
  408. if (chunk_free == ffg->iff_chunksize)
  409. ffg->iff_ffs.ffs_free_chunks++;
  410. }
  411. /*
  412. * need to update the info for last free chunk.
  413. */
  414. if (last_chunksize)
  415. ocfs2_info_update_ffg(ffg, last_chunksize);
  416. } while (le64_to_cpu(bg->bg_next_group));
  417. bail:
  418. brelse(bh);
  419. return status;
  420. }
  421. static int ocfs2_info_freefrag_scan_bitmap(struct ocfs2_super *osb,
  422. struct inode *gb_inode, u64 blkno,
  423. struct ocfs2_info_freefrag *ffg)
  424. {
  425. u32 chunks_in_group;
  426. int status = 0, unlock = 0, i;
  427. struct buffer_head *bh = NULL;
  428. struct ocfs2_chain_list *cl = NULL;
  429. struct ocfs2_chain_rec *rec = NULL;
  430. struct ocfs2_dinode *gb_dinode = NULL;
  431. if (gb_inode)
  432. inode_lock(gb_inode);
  433. if (o2info_coherent(&ffg->iff_req)) {
  434. status = ocfs2_inode_lock(gb_inode, &bh, 0);
  435. if (status < 0) {
  436. mlog_errno(status);
  437. goto bail;
  438. }
  439. unlock = 1;
  440. } else {
  441. status = ocfs2_read_blocks_sync(osb, blkno, 1, &bh);
  442. if (status < 0) {
  443. mlog_errno(status);
  444. goto bail;
  445. }
  446. }
  447. gb_dinode = (struct ocfs2_dinode *)bh->b_data;
  448. cl = &(gb_dinode->id2.i_chain);
  449. /*
  450. * Chunksize(in) clusters from userspace should be
  451. * less than clusters in a group.
  452. */
  453. if (ffg->iff_chunksize > le16_to_cpu(cl->cl_cpg)) {
  454. status = -EINVAL;
  455. goto bail;
  456. }
  457. memset(&ffg->iff_ffs, 0, sizeof(struct ocfs2_info_freefrag_stats));
  458. ffg->iff_ffs.ffs_min = ~0U;
  459. ffg->iff_ffs.ffs_clusters =
  460. le32_to_cpu(gb_dinode->id1.bitmap1.i_total);
  461. ffg->iff_ffs.ffs_free_clusters = ffg->iff_ffs.ffs_clusters -
  462. le32_to_cpu(gb_dinode->id1.bitmap1.i_used);
  463. chunks_in_group = le16_to_cpu(cl->cl_cpg) / ffg->iff_chunksize + 1;
  464. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) {
  465. rec = &(cl->cl_recs[i]);
  466. status = ocfs2_info_freefrag_scan_chain(osb, gb_inode,
  467. gb_dinode,
  468. rec, ffg,
  469. chunks_in_group);
  470. if (status)
  471. goto bail;
  472. }
  473. if (ffg->iff_ffs.ffs_free_chunks_real)
  474. ffg->iff_ffs.ffs_avg = (ffg->iff_ffs.ffs_avg /
  475. ffg->iff_ffs.ffs_free_chunks_real);
  476. bail:
  477. if (unlock)
  478. ocfs2_inode_unlock(gb_inode, 0);
  479. if (gb_inode)
  480. inode_unlock(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. break;
  804. case OCFS2_IOC_REFLINK:
  805. if (copy_from_user(&args, argp, sizeof(args)))
  806. return -EFAULT;
  807. preserve = (args.preserve != 0);
  808. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  809. compat_ptr(args.new_path), preserve);
  810. case OCFS2_IOC_INFO:
  811. if (copy_from_user(&info, argp, sizeof(struct ocfs2_info)))
  812. return -EFAULT;
  813. return ocfs2_info_handle(inode, &info, 1);
  814. case OCFS2_IOC_MOVE_EXT:
  815. break;
  816. default:
  817. return -ENOIOCTLCMD;
  818. }
  819. return ocfs2_ioctl(file, cmd, arg);
  820. }
  821. #endif