ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/in.h>
  4. #include "super.h"
  5. #include "mds_client.h"
  6. #include "ioctl.h"
  7. #include <linux/ceph/striper.h>
  8. #include <linux/fscrypt.h>
  9. /*
  10. * ioctls
  11. */
  12. /*
  13. * get and set the file layout
  14. */
  15. static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
  16. {
  17. struct ceph_inode_info *ci = ceph_inode(file_inode(file));
  18. struct ceph_ioctl_layout l;
  19. int err;
  20. err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
  21. if (!err) {
  22. l.stripe_unit = ci->i_layout.stripe_unit;
  23. l.stripe_count = ci->i_layout.stripe_count;
  24. l.object_size = ci->i_layout.object_size;
  25. l.data_pool = ci->i_layout.pool_id;
  26. l.preferred_osd = -1;
  27. if (copy_to_user(arg, &l, sizeof(l)))
  28. return -EFAULT;
  29. }
  30. return err;
  31. }
  32. static long __validate_layout(struct ceph_mds_client *mdsc,
  33. struct ceph_ioctl_layout *l)
  34. {
  35. int i, err;
  36. /* validate striping parameters */
  37. if ((l->object_size & ~PAGE_MASK) ||
  38. (l->stripe_unit & ~PAGE_MASK) ||
  39. ((unsigned)l->stripe_unit != 0 &&
  40. ((unsigned)l->object_size % (unsigned)l->stripe_unit)))
  41. return -EINVAL;
  42. /* make sure it's a valid data pool */
  43. mutex_lock(&mdsc->mutex);
  44. err = -EINVAL;
  45. for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
  46. if (mdsc->mdsmap->m_data_pg_pools[i] == l->data_pool) {
  47. err = 0;
  48. break;
  49. }
  50. mutex_unlock(&mdsc->mutex);
  51. if (err)
  52. return err;
  53. return 0;
  54. }
  55. static long ceph_ioctl_set_layout(struct file *file, void __user *arg)
  56. {
  57. struct inode *inode = file_inode(file);
  58. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  59. struct ceph_mds_request *req;
  60. struct ceph_ioctl_layout l;
  61. struct ceph_inode_info *ci = ceph_inode(file_inode(file));
  62. struct ceph_ioctl_layout nl;
  63. int err;
  64. if (copy_from_user(&l, arg, sizeof(l)))
  65. return -EFAULT;
  66. /* validate changed params against current layout */
  67. err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
  68. if (err)
  69. return err;
  70. memset(&nl, 0, sizeof(nl));
  71. if (l.stripe_count)
  72. nl.stripe_count = l.stripe_count;
  73. else
  74. nl.stripe_count = ci->i_layout.stripe_count;
  75. if (l.stripe_unit)
  76. nl.stripe_unit = l.stripe_unit;
  77. else
  78. nl.stripe_unit = ci->i_layout.stripe_unit;
  79. if (l.object_size)
  80. nl.object_size = l.object_size;
  81. else
  82. nl.object_size = ci->i_layout.object_size;
  83. if (l.data_pool)
  84. nl.data_pool = l.data_pool;
  85. else
  86. nl.data_pool = ci->i_layout.pool_id;
  87. /* this is obsolete, and always -1 */
  88. nl.preferred_osd = -1;
  89. err = __validate_layout(mdsc, &nl);
  90. if (err)
  91. return err;
  92. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
  93. USE_AUTH_MDS);
  94. if (IS_ERR(req))
  95. return PTR_ERR(req);
  96. req->r_inode = inode;
  97. ihold(inode);
  98. req->r_num_caps = 1;
  99. req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
  100. req->r_args.setlayout.layout.fl_stripe_unit =
  101. cpu_to_le32(l.stripe_unit);
  102. req->r_args.setlayout.layout.fl_stripe_count =
  103. cpu_to_le32(l.stripe_count);
  104. req->r_args.setlayout.layout.fl_object_size =
  105. cpu_to_le32(l.object_size);
  106. req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
  107. err = ceph_mdsc_do_request(mdsc, NULL, req);
  108. ceph_mdsc_put_request(req);
  109. return err;
  110. }
  111. /*
  112. * Set a layout policy on a directory inode. All items in the tree
  113. * rooted at this inode will inherit this layout on creation,
  114. * (It doesn't apply retroactively )
  115. * unless a subdirectory has its own layout policy.
  116. */
  117. static long ceph_ioctl_set_layout_policy (struct file *file, void __user *arg)
  118. {
  119. struct inode *inode = file_inode(file);
  120. struct ceph_mds_request *req;
  121. struct ceph_ioctl_layout l;
  122. int err;
  123. struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
  124. /* copy and validate */
  125. if (copy_from_user(&l, arg, sizeof(l)))
  126. return -EFAULT;
  127. err = __validate_layout(mdsc, &l);
  128. if (err)
  129. return err;
  130. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETDIRLAYOUT,
  131. USE_AUTH_MDS);
  132. if (IS_ERR(req))
  133. return PTR_ERR(req);
  134. req->r_inode = inode;
  135. ihold(inode);
  136. req->r_num_caps = 1;
  137. req->r_args.setlayout.layout.fl_stripe_unit =
  138. cpu_to_le32(l.stripe_unit);
  139. req->r_args.setlayout.layout.fl_stripe_count =
  140. cpu_to_le32(l.stripe_count);
  141. req->r_args.setlayout.layout.fl_object_size =
  142. cpu_to_le32(l.object_size);
  143. req->r_args.setlayout.layout.fl_pg_pool =
  144. cpu_to_le32(l.data_pool);
  145. err = ceph_mdsc_do_request(mdsc, inode, req);
  146. ceph_mdsc_put_request(req);
  147. return err;
  148. }
  149. /*
  150. * Return object name, size/offset information, and location (OSD
  151. * number, network address) for a given file offset.
  152. */
  153. static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
  154. {
  155. struct ceph_ioctl_dataloc dl;
  156. struct inode *inode = file_inode(file);
  157. struct ceph_inode_info *ci = ceph_inode(inode);
  158. struct ceph_osd_client *osdc =
  159. &ceph_sb_to_fs_client(inode->i_sb)->client->osdc;
  160. struct ceph_object_locator oloc;
  161. CEPH_DEFINE_OID_ONSTACK(oid);
  162. u32 xlen;
  163. u64 tmp;
  164. struct ceph_pg pgid;
  165. int r;
  166. /* copy and validate */
  167. if (copy_from_user(&dl, arg, sizeof(dl)))
  168. return -EFAULT;
  169. down_read(&osdc->lock);
  170. ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, 1,
  171. &dl.object_no, &dl.object_offset, &xlen);
  172. dl.file_offset -= dl.object_offset;
  173. dl.object_size = ci->i_layout.object_size;
  174. dl.block_size = ci->i_layout.stripe_unit;
  175. /* block_offset = object_offset % block_size */
  176. tmp = dl.object_offset;
  177. dl.block_offset = do_div(tmp, dl.block_size);
  178. snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
  179. ceph_ino(inode), dl.object_no);
  180. oloc.pool = ci->i_layout.pool_id;
  181. oloc.pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
  182. ceph_oid_printf(&oid, "%s", dl.object_name);
  183. r = ceph_object_locator_to_pg(osdc->osdmap, &oid, &oloc, &pgid);
  184. ceph_oloc_destroy(&oloc);
  185. if (r < 0) {
  186. up_read(&osdc->lock);
  187. return r;
  188. }
  189. dl.osd = ceph_pg_to_acting_primary(osdc->osdmap, &pgid);
  190. if (dl.osd >= 0) {
  191. struct ceph_entity_addr *a =
  192. ceph_osd_addr(osdc->osdmap, dl.osd);
  193. if (a)
  194. memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
  195. } else {
  196. memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
  197. }
  198. up_read(&osdc->lock);
  199. /* send result back to user */
  200. if (copy_to_user(arg, &dl, sizeof(dl)))
  201. return -EFAULT;
  202. return 0;
  203. }
  204. static long ceph_ioctl_lazyio(struct file *file)
  205. {
  206. struct ceph_file_info *fi = file->private_data;
  207. struct inode *inode = file_inode(file);
  208. struct ceph_inode_info *ci = ceph_inode(inode);
  209. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  210. struct ceph_client *cl = mdsc->fsc->client;
  211. bool is_file_already_lazy = false;
  212. spin_lock(&ci->i_ceph_lock);
  213. if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
  214. fi->fmode |= CEPH_FILE_MODE_LAZY;
  215. ci->i_nr_by_mode[ffs(CEPH_FILE_MODE_LAZY)]++;
  216. __ceph_touch_fmode(ci, mdsc, fi->fmode);
  217. } else {
  218. is_file_already_lazy = true;
  219. }
  220. spin_unlock(&ci->i_ceph_lock);
  221. if (is_file_already_lazy) {
  222. doutc(cl, "file %p %p %llx.%llx already lazy\n", file, inode,
  223. ceph_vinop(inode));
  224. } else {
  225. doutc(cl, "file %p %p %llx.%llx marked lazy\n", file, inode,
  226. ceph_vinop(inode));
  227. ceph_check_caps(ci, 0);
  228. }
  229. return 0;
  230. }
  231. static long ceph_ioctl_syncio(struct file *file)
  232. {
  233. struct ceph_file_info *fi = file->private_data;
  234. fi->flags |= CEPH_F_SYNC;
  235. return 0;
  236. }
  237. static int vet_mds_for_fscrypt(struct file *file)
  238. {
  239. int i, ret = -EOPNOTSUPP;
  240. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(file_inode(file)->i_sb);
  241. mutex_lock(&mdsc->mutex);
  242. for (i = 0; i < mdsc->max_sessions; i++) {
  243. struct ceph_mds_session *s = mdsc->sessions[i];
  244. if (!s)
  245. continue;
  246. if (test_bit(CEPHFS_FEATURE_ALTERNATE_NAME, &s->s_features))
  247. ret = 0;
  248. break;
  249. }
  250. mutex_unlock(&mdsc->mutex);
  251. return ret;
  252. }
  253. static long ceph_set_encryption_policy(struct file *file, unsigned long arg)
  254. {
  255. int ret, got = 0;
  256. struct inode *inode = file_inode(file);
  257. struct ceph_inode_info *ci = ceph_inode(inode);
  258. /* encrypted directories can't have striped layout */
  259. if (ci->i_layout.stripe_count > 1)
  260. return -EINVAL;
  261. ret = vet_mds_for_fscrypt(file);
  262. if (ret)
  263. return ret;
  264. /*
  265. * Ensure we hold these caps so that we _know_ that the rstats check
  266. * in the empty_dir check is reliable.
  267. */
  268. ret = ceph_get_caps(file, CEPH_CAP_FILE_SHARED, 0, -1, &got);
  269. if (ret)
  270. return ret;
  271. ret = fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  272. if (got)
  273. ceph_put_cap_refs(ci, got);
  274. return ret;
  275. }
  276. static const char *ceph_ioctl_cmd_name(const unsigned int cmd)
  277. {
  278. switch (cmd) {
  279. case CEPH_IOC_GET_LAYOUT:
  280. return "get_layout";
  281. case CEPH_IOC_SET_LAYOUT:
  282. return "set_layout";
  283. case CEPH_IOC_SET_LAYOUT_POLICY:
  284. return "set_layout_policy";
  285. case CEPH_IOC_GET_DATALOC:
  286. return "get_dataloc";
  287. case CEPH_IOC_LAZYIO:
  288. return "lazyio";
  289. case CEPH_IOC_SYNCIO:
  290. return "syncio";
  291. case FS_IOC_SET_ENCRYPTION_POLICY:
  292. return "set_encryption_policy";
  293. case FS_IOC_GET_ENCRYPTION_POLICY:
  294. return "get_encryption_policy";
  295. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  296. return "get_encryption_policy_ex";
  297. case FS_IOC_ADD_ENCRYPTION_KEY:
  298. return "add_encryption_key";
  299. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  300. return "remove_encryption_key";
  301. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  302. return "remove_encryption_key_all_users";
  303. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  304. return "get_encryption_key_status";
  305. case FS_IOC_GET_ENCRYPTION_NONCE:
  306. return "get_encryption_nonce";
  307. default:
  308. return "unknown";
  309. }
  310. }
  311. long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  312. {
  313. struct inode *inode = file_inode(file);
  314. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  315. int ret;
  316. doutc(fsc->client, "file %p %p %llx.%llx cmd %s arg %lu\n", file,
  317. inode, ceph_vinop(inode), ceph_ioctl_cmd_name(cmd), arg);
  318. switch (cmd) {
  319. case CEPH_IOC_GET_LAYOUT:
  320. return ceph_ioctl_get_layout(file, (void __user *)arg);
  321. case CEPH_IOC_SET_LAYOUT:
  322. return ceph_ioctl_set_layout(file, (void __user *)arg);
  323. case CEPH_IOC_SET_LAYOUT_POLICY:
  324. return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
  325. case CEPH_IOC_GET_DATALOC:
  326. return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  327. case CEPH_IOC_LAZYIO:
  328. return ceph_ioctl_lazyio(file);
  329. case CEPH_IOC_SYNCIO:
  330. return ceph_ioctl_syncio(file);
  331. case FS_IOC_SET_ENCRYPTION_POLICY:
  332. return ceph_set_encryption_policy(file, arg);
  333. case FS_IOC_GET_ENCRYPTION_POLICY:
  334. ret = vet_mds_for_fscrypt(file);
  335. if (ret)
  336. return ret;
  337. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  338. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  339. ret = vet_mds_for_fscrypt(file);
  340. if (ret)
  341. return ret;
  342. return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
  343. case FS_IOC_ADD_ENCRYPTION_KEY:
  344. ret = vet_mds_for_fscrypt(file);
  345. if (ret)
  346. return ret;
  347. return fscrypt_ioctl_add_key(file, (void __user *)arg);
  348. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  349. return fscrypt_ioctl_remove_key(file, (void __user *)arg);
  350. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  351. return fscrypt_ioctl_remove_key_all_users(file,
  352. (void __user *)arg);
  353. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  354. return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
  355. case FS_IOC_GET_ENCRYPTION_NONCE:
  356. ret = vet_mds_for_fscrypt(file);
  357. if (ret)
  358. return ret;
  359. return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
  360. }
  361. return -ENOTTY;
  362. }