ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <linux/ext2_fs.h>
  22. #define o2info_from_user(a, b) \
  23. copy_from_user(&(a), (b), sizeof(a))
  24. #define o2info_to_user(a, b) \
  25. copy_to_user((typeof(a) __user *)b, &(a), sizeof(a))
  26. /*
  27. * This call is void because we are already reporting an error that may
  28. * be -EFAULT. The error will be returned from the ioctl(2) call. It's
  29. * just a best-effort to tell userspace that this request caused the error.
  30. */
  31. static inline void __o2info_set_request_error(struct ocfs2_info_request *kreq,
  32. struct ocfs2_info_request __user *req)
  33. {
  34. kreq->ir_flags |= OCFS2_INFO_FL_ERROR;
  35. (void)put_user(kreq->ir_flags, (__u32 __user *)&(req->ir_flags));
  36. }
  37. #define o2info_set_request_error(a, b) \
  38. __o2info_set_request_error((struct ocfs2_info_request *)&(a), b)
  39. static inline void __o2info_set_request_filled(struct ocfs2_info_request *req)
  40. {
  41. req->ir_flags |= OCFS2_INFO_FL_FILLED;
  42. }
  43. #define o2info_set_request_filled(a) \
  44. __o2info_set_request_filled((struct ocfs2_info_request *)&(a))
  45. static inline void __o2info_clear_request_filled(struct ocfs2_info_request *req)
  46. {
  47. req->ir_flags &= ~OCFS2_INFO_FL_FILLED;
  48. }
  49. #define o2info_clear_request_filled(a) \
  50. __o2info_clear_request_filled((struct ocfs2_info_request *)&(a))
  51. static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
  52. {
  53. int status;
  54. status = ocfs2_inode_lock(inode, NULL, 0);
  55. if (status < 0) {
  56. mlog_errno(status);
  57. return status;
  58. }
  59. ocfs2_get_inode_flags(OCFS2_I(inode));
  60. *flags = OCFS2_I(inode)->ip_attr;
  61. ocfs2_inode_unlock(inode, 0);
  62. return status;
  63. }
  64. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  65. unsigned mask)
  66. {
  67. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  68. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  69. handle_t *handle = NULL;
  70. struct buffer_head *bh = NULL;
  71. unsigned oldflags;
  72. int status;
  73. mutex_lock(&inode->i_mutex);
  74. status = ocfs2_inode_lock(inode, &bh, 1);
  75. if (status < 0) {
  76. mlog_errno(status);
  77. goto bail;
  78. }
  79. status = -EACCES;
  80. if (!inode_owner_or_capable(inode))
  81. goto bail_unlock;
  82. if (!S_ISDIR(inode->i_mode))
  83. flags &= ~OCFS2_DIRSYNC_FL;
  84. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  85. if (IS_ERR(handle)) {
  86. status = PTR_ERR(handle);
  87. mlog_errno(status);
  88. goto bail_unlock;
  89. }
  90. oldflags = ocfs2_inode->ip_attr;
  91. flags = flags & mask;
  92. flags |= oldflags & ~mask;
  93. /*
  94. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  95. * the relevant capability.
  96. */
  97. status = -EPERM;
  98. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  99. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  100. if (!capable(CAP_LINUX_IMMUTABLE))
  101. goto bail_unlock;
  102. }
  103. ocfs2_inode->ip_attr = flags;
  104. ocfs2_set_inode_flags(inode);
  105. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  106. if (status < 0)
  107. mlog_errno(status);
  108. ocfs2_commit_trans(osb, handle);
  109. bail_unlock:
  110. ocfs2_inode_unlock(inode, 1);
  111. bail:
  112. mutex_unlock(&inode->i_mutex);
  113. brelse(bh);
  114. return status;
  115. }
  116. int ocfs2_info_handle_blocksize(struct inode *inode,
  117. struct ocfs2_info_request __user *req)
  118. {
  119. int status = -EFAULT;
  120. struct ocfs2_info_blocksize oib;
  121. if (o2info_from_user(oib, req))
  122. goto bail;
  123. oib.ib_blocksize = inode->i_sb->s_blocksize;
  124. o2info_set_request_filled(oib);
  125. if (o2info_to_user(oib, req))
  126. goto bail;
  127. status = 0;
  128. bail:
  129. if (status)
  130. o2info_set_request_error(oib, req);
  131. return status;
  132. }
  133. int ocfs2_info_handle_clustersize(struct inode *inode,
  134. struct ocfs2_info_request __user *req)
  135. {
  136. int status = -EFAULT;
  137. struct ocfs2_info_clustersize oic;
  138. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  139. if (o2info_from_user(oic, req))
  140. goto bail;
  141. oic.ic_clustersize = osb->s_clustersize;
  142. o2info_set_request_filled(oic);
  143. if (o2info_to_user(oic, req))
  144. goto bail;
  145. status = 0;
  146. bail:
  147. if (status)
  148. o2info_set_request_error(oic, req);
  149. return status;
  150. }
  151. int ocfs2_info_handle_maxslots(struct inode *inode,
  152. struct ocfs2_info_request __user *req)
  153. {
  154. int status = -EFAULT;
  155. struct ocfs2_info_maxslots oim;
  156. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  157. if (o2info_from_user(oim, req))
  158. goto bail;
  159. oim.im_max_slots = osb->max_slots;
  160. o2info_set_request_filled(oim);
  161. if (o2info_to_user(oim, req))
  162. goto bail;
  163. status = 0;
  164. bail:
  165. if (status)
  166. o2info_set_request_error(oim, req);
  167. return status;
  168. }
  169. int ocfs2_info_handle_label(struct inode *inode,
  170. struct ocfs2_info_request __user *req)
  171. {
  172. int status = -EFAULT;
  173. struct ocfs2_info_label oil;
  174. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  175. if (o2info_from_user(oil, req))
  176. goto bail;
  177. memcpy(oil.il_label, osb->vol_label, OCFS2_MAX_VOL_LABEL_LEN);
  178. o2info_set_request_filled(oil);
  179. if (o2info_to_user(oil, req))
  180. goto bail;
  181. status = 0;
  182. bail:
  183. if (status)
  184. o2info_set_request_error(oil, req);
  185. return status;
  186. }
  187. int ocfs2_info_handle_uuid(struct inode *inode,
  188. struct ocfs2_info_request __user *req)
  189. {
  190. int status = -EFAULT;
  191. struct ocfs2_info_uuid oiu;
  192. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  193. if (o2info_from_user(oiu, req))
  194. goto bail;
  195. memcpy(oiu.iu_uuid_str, osb->uuid_str, OCFS2_TEXT_UUID_LEN + 1);
  196. o2info_set_request_filled(oiu);
  197. if (o2info_to_user(oiu, req))
  198. goto bail;
  199. status = 0;
  200. bail:
  201. if (status)
  202. o2info_set_request_error(oiu, req);
  203. return status;
  204. }
  205. int ocfs2_info_handle_fs_features(struct inode *inode,
  206. struct ocfs2_info_request __user *req)
  207. {
  208. int status = -EFAULT;
  209. struct ocfs2_info_fs_features oif;
  210. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  211. if (o2info_from_user(oif, req))
  212. goto bail;
  213. oif.if_compat_features = osb->s_feature_compat;
  214. oif.if_incompat_features = osb->s_feature_incompat;
  215. oif.if_ro_compat_features = osb->s_feature_ro_compat;
  216. o2info_set_request_filled(oif);
  217. if (o2info_to_user(oif, req))
  218. goto bail;
  219. status = 0;
  220. bail:
  221. if (status)
  222. o2info_set_request_error(oif, req);
  223. return status;
  224. }
  225. int ocfs2_info_handle_journal_size(struct inode *inode,
  226. struct ocfs2_info_request __user *req)
  227. {
  228. int status = -EFAULT;
  229. struct ocfs2_info_journal_size oij;
  230. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  231. if (o2info_from_user(oij, req))
  232. goto bail;
  233. oij.ij_journal_size = osb->journal->j_inode->i_size;
  234. o2info_set_request_filled(oij);
  235. if (o2info_to_user(oij, req))
  236. goto bail;
  237. status = 0;
  238. bail:
  239. if (status)
  240. o2info_set_request_error(oij, req);
  241. return status;
  242. }
  243. int ocfs2_info_handle_unknown(struct inode *inode,
  244. struct ocfs2_info_request __user *req)
  245. {
  246. int status = -EFAULT;
  247. struct ocfs2_info_request oir;
  248. if (o2info_from_user(oir, req))
  249. goto bail;
  250. o2info_clear_request_filled(oir);
  251. if (o2info_to_user(oir, req))
  252. goto bail;
  253. status = 0;
  254. bail:
  255. if (status)
  256. o2info_set_request_error(oir, req);
  257. return status;
  258. }
  259. /*
  260. * Validate and distinguish OCFS2_IOC_INFO requests.
  261. *
  262. * - validate the magic number.
  263. * - distinguish different requests.
  264. * - validate size of different requests.
  265. */
  266. int ocfs2_info_handle_request(struct inode *inode,
  267. struct ocfs2_info_request __user *req)
  268. {
  269. int status = -EFAULT;
  270. struct ocfs2_info_request oir;
  271. if (o2info_from_user(oir, req))
  272. goto bail;
  273. status = -EINVAL;
  274. if (oir.ir_magic != OCFS2_INFO_MAGIC)
  275. goto bail;
  276. switch (oir.ir_code) {
  277. case OCFS2_INFO_BLOCKSIZE:
  278. if (oir.ir_size == sizeof(struct ocfs2_info_blocksize))
  279. status = ocfs2_info_handle_blocksize(inode, req);
  280. break;
  281. case OCFS2_INFO_CLUSTERSIZE:
  282. if (oir.ir_size == sizeof(struct ocfs2_info_clustersize))
  283. status = ocfs2_info_handle_clustersize(inode, req);
  284. break;
  285. case OCFS2_INFO_MAXSLOTS:
  286. if (oir.ir_size == sizeof(struct ocfs2_info_maxslots))
  287. status = ocfs2_info_handle_maxslots(inode, req);
  288. break;
  289. case OCFS2_INFO_LABEL:
  290. if (oir.ir_size == sizeof(struct ocfs2_info_label))
  291. status = ocfs2_info_handle_label(inode, req);
  292. break;
  293. case OCFS2_INFO_UUID:
  294. if (oir.ir_size == sizeof(struct ocfs2_info_uuid))
  295. status = ocfs2_info_handle_uuid(inode, req);
  296. break;
  297. case OCFS2_INFO_FS_FEATURES:
  298. if (oir.ir_size == sizeof(struct ocfs2_info_fs_features))
  299. status = ocfs2_info_handle_fs_features(inode, req);
  300. break;
  301. case OCFS2_INFO_JOURNAL_SIZE:
  302. if (oir.ir_size == sizeof(struct ocfs2_info_journal_size))
  303. status = ocfs2_info_handle_journal_size(inode, req);
  304. break;
  305. default:
  306. status = ocfs2_info_handle_unknown(inode, req);
  307. break;
  308. }
  309. bail:
  310. return status;
  311. }
  312. int ocfs2_get_request_ptr(struct ocfs2_info *info, int idx,
  313. u64 *req_addr, int compat_flag)
  314. {
  315. int status = -EFAULT;
  316. u64 __user *bp = NULL;
  317. if (compat_flag) {
  318. #ifdef CONFIG_COMPAT
  319. /*
  320. * pointer bp stores the base address of a pointers array,
  321. * which collects all addresses of separate request.
  322. */
  323. bp = (u64 __user *)(unsigned long)compat_ptr(info->oi_requests);
  324. #else
  325. BUG();
  326. #endif
  327. } else
  328. bp = (u64 __user *)(unsigned long)(info->oi_requests);
  329. if (o2info_from_user(*req_addr, bp + idx))
  330. goto bail;
  331. status = 0;
  332. bail:
  333. return status;
  334. }
  335. /*
  336. * OCFS2_IOC_INFO handles an array of requests passed from userspace.
  337. *
  338. * ocfs2_info_handle() recevies a large info aggregation, grab and
  339. * validate the request count from header, then break it into small
  340. * pieces, later specific handlers can handle them one by one.
  341. *
  342. * Idea here is to make each separate request small enough to ensure
  343. * a better backward&forward compatibility, since a small piece of
  344. * request will be less likely to be broken if disk layout get changed.
  345. */
  346. int ocfs2_info_handle(struct inode *inode, struct ocfs2_info *info,
  347. int compat_flag)
  348. {
  349. int i, status = 0;
  350. u64 req_addr;
  351. struct ocfs2_info_request __user *reqp;
  352. if ((info->oi_count > OCFS2_INFO_MAX_REQUEST) ||
  353. (!info->oi_requests)) {
  354. status = -EINVAL;
  355. goto bail;
  356. }
  357. for (i = 0; i < info->oi_count; i++) {
  358. status = ocfs2_get_request_ptr(info, i, &req_addr, compat_flag);
  359. if (status)
  360. break;
  361. reqp = (struct ocfs2_info_request *)(unsigned long)req_addr;
  362. if (!reqp) {
  363. status = -EINVAL;
  364. goto bail;
  365. }
  366. status = ocfs2_info_handle_request(inode, reqp);
  367. if (status)
  368. break;
  369. }
  370. bail:
  371. return status;
  372. }
  373. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  374. {
  375. struct inode *inode = filp->f_path.dentry->d_inode;
  376. unsigned int flags;
  377. int new_clusters;
  378. int status;
  379. struct ocfs2_space_resv sr;
  380. struct ocfs2_new_group_input input;
  381. struct reflink_arguments args;
  382. const char *old_path, *new_path;
  383. bool preserve;
  384. struct ocfs2_info info;
  385. switch (cmd) {
  386. case OCFS2_IOC_GETFLAGS:
  387. status = ocfs2_get_inode_attr(inode, &flags);
  388. if (status < 0)
  389. return status;
  390. flags &= OCFS2_FL_VISIBLE;
  391. return put_user(flags, (int __user *) arg);
  392. case OCFS2_IOC_SETFLAGS:
  393. if (get_user(flags, (int __user *) arg))
  394. return -EFAULT;
  395. status = mnt_want_write(filp->f_path.mnt);
  396. if (status)
  397. return status;
  398. status = ocfs2_set_inode_attr(inode, flags,
  399. OCFS2_FL_MODIFIABLE);
  400. mnt_drop_write(filp->f_path.mnt);
  401. return status;
  402. case OCFS2_IOC_RESVSP:
  403. case OCFS2_IOC_RESVSP64:
  404. case OCFS2_IOC_UNRESVSP:
  405. case OCFS2_IOC_UNRESVSP64:
  406. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  407. return -EFAULT;
  408. return ocfs2_change_file_space(filp, cmd, &sr);
  409. case OCFS2_IOC_GROUP_EXTEND:
  410. if (!capable(CAP_SYS_RESOURCE))
  411. return -EPERM;
  412. if (get_user(new_clusters, (int __user *)arg))
  413. return -EFAULT;
  414. return ocfs2_group_extend(inode, new_clusters);
  415. case OCFS2_IOC_GROUP_ADD:
  416. case OCFS2_IOC_GROUP_ADD64:
  417. if (!capable(CAP_SYS_RESOURCE))
  418. return -EPERM;
  419. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  420. return -EFAULT;
  421. return ocfs2_group_add(inode, &input);
  422. case OCFS2_IOC_REFLINK:
  423. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  424. sizeof(args)))
  425. return -EFAULT;
  426. old_path = (const char *)(unsigned long)args.old_path;
  427. new_path = (const char *)(unsigned long)args.new_path;
  428. preserve = (args.preserve != 0);
  429. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  430. case OCFS2_IOC_INFO:
  431. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  432. sizeof(struct ocfs2_info)))
  433. return -EFAULT;
  434. return ocfs2_info_handle(inode, &info, 0);
  435. default:
  436. return -ENOTTY;
  437. }
  438. }
  439. #ifdef CONFIG_COMPAT
  440. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  441. {
  442. bool preserve;
  443. struct reflink_arguments args;
  444. struct inode *inode = file->f_path.dentry->d_inode;
  445. struct ocfs2_info info;
  446. switch (cmd) {
  447. case OCFS2_IOC32_GETFLAGS:
  448. cmd = OCFS2_IOC_GETFLAGS;
  449. break;
  450. case OCFS2_IOC32_SETFLAGS:
  451. cmd = OCFS2_IOC_SETFLAGS;
  452. break;
  453. case OCFS2_IOC_RESVSP:
  454. case OCFS2_IOC_RESVSP64:
  455. case OCFS2_IOC_UNRESVSP:
  456. case OCFS2_IOC_UNRESVSP64:
  457. case OCFS2_IOC_GROUP_EXTEND:
  458. case OCFS2_IOC_GROUP_ADD:
  459. case OCFS2_IOC_GROUP_ADD64:
  460. break;
  461. case OCFS2_IOC_REFLINK:
  462. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  463. sizeof(args)))
  464. return -EFAULT;
  465. preserve = (args.preserve != 0);
  466. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  467. compat_ptr(args.new_path), preserve);
  468. case OCFS2_IOC_INFO:
  469. if (copy_from_user(&info, (struct ocfs2_info __user *)arg,
  470. sizeof(struct ocfs2_info)))
  471. return -EFAULT;
  472. return ocfs2_info_handle(inode, &info, 1);
  473. default:
  474. return -ENOIOCTLCMD;
  475. }
  476. return ocfs2_ioctl(file, cmd, arg);
  477. }
  478. #endif