ioctl.c 13 KB

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