ioctl.c 13 KB

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