ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
  212. spin_lock(&ci->i_ceph_lock);
  213. fi->fmode |= CEPH_FILE_MODE_LAZY;
  214. ci->i_nr_by_mode[ffs(CEPH_FILE_MODE_LAZY)]++;
  215. __ceph_touch_fmode(ci, mdsc, fi->fmode);
  216. spin_unlock(&ci->i_ceph_lock);
  217. doutc(cl, "file %p %p %llx.%llx marked lazy\n", file, inode,
  218. ceph_vinop(inode));
  219. ceph_check_caps(ci, 0);
  220. } else {
  221. doutc(cl, "file %p %p %llx.%llx already lazy\n", file, inode,
  222. ceph_vinop(inode));
  223. }
  224. return 0;
  225. }
  226. static long ceph_ioctl_syncio(struct file *file)
  227. {
  228. struct ceph_file_info *fi = file->private_data;
  229. fi->flags |= CEPH_F_SYNC;
  230. return 0;
  231. }
  232. static int vet_mds_for_fscrypt(struct file *file)
  233. {
  234. int i, ret = -EOPNOTSUPP;
  235. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(file_inode(file)->i_sb);
  236. mutex_lock(&mdsc->mutex);
  237. for (i = 0; i < mdsc->max_sessions; i++) {
  238. struct ceph_mds_session *s = mdsc->sessions[i];
  239. if (!s)
  240. continue;
  241. if (test_bit(CEPHFS_FEATURE_ALTERNATE_NAME, &s->s_features))
  242. ret = 0;
  243. break;
  244. }
  245. mutex_unlock(&mdsc->mutex);
  246. return ret;
  247. }
  248. static long ceph_set_encryption_policy(struct file *file, unsigned long arg)
  249. {
  250. int ret, got = 0;
  251. struct inode *inode = file_inode(file);
  252. struct ceph_inode_info *ci = ceph_inode(inode);
  253. /* encrypted directories can't have striped layout */
  254. if (ci->i_layout.stripe_count > 1)
  255. return -EINVAL;
  256. ret = vet_mds_for_fscrypt(file);
  257. if (ret)
  258. return ret;
  259. /*
  260. * Ensure we hold these caps so that we _know_ that the rstats check
  261. * in the empty_dir check is reliable.
  262. */
  263. ret = ceph_get_caps(file, CEPH_CAP_FILE_SHARED, 0, -1, &got);
  264. if (ret)
  265. return ret;
  266. ret = fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  267. if (got)
  268. ceph_put_cap_refs(ci, got);
  269. return ret;
  270. }
  271. static const char *ceph_ioctl_cmd_name(const unsigned int cmd)
  272. {
  273. switch (cmd) {
  274. case CEPH_IOC_GET_LAYOUT:
  275. return "get_layout";
  276. case CEPH_IOC_SET_LAYOUT:
  277. return "set_layout";
  278. case CEPH_IOC_SET_LAYOUT_POLICY:
  279. return "set_layout_policy";
  280. case CEPH_IOC_GET_DATALOC:
  281. return "get_dataloc";
  282. case CEPH_IOC_LAZYIO:
  283. return "lazyio";
  284. case CEPH_IOC_SYNCIO:
  285. return "syncio";
  286. case FS_IOC_SET_ENCRYPTION_POLICY:
  287. return "set_encryption_policy";
  288. case FS_IOC_GET_ENCRYPTION_POLICY:
  289. return "get_encryption_policy";
  290. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  291. return "get_encryption_policy_ex";
  292. case FS_IOC_ADD_ENCRYPTION_KEY:
  293. return "add_encryption_key";
  294. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  295. return "remove_encryption_key";
  296. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  297. return "remove_encryption_key_all_users";
  298. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  299. return "get_encryption_key_status";
  300. case FS_IOC_GET_ENCRYPTION_NONCE:
  301. return "get_encryption_nonce";
  302. default:
  303. return "unknown";
  304. }
  305. }
  306. long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  307. {
  308. struct inode *inode = file_inode(file);
  309. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  310. int ret;
  311. doutc(fsc->client, "file %p %p %llx.%llx cmd %s arg %lu\n", file,
  312. inode, ceph_vinop(inode), ceph_ioctl_cmd_name(cmd), arg);
  313. switch (cmd) {
  314. case CEPH_IOC_GET_LAYOUT:
  315. return ceph_ioctl_get_layout(file, (void __user *)arg);
  316. case CEPH_IOC_SET_LAYOUT:
  317. return ceph_ioctl_set_layout(file, (void __user *)arg);
  318. case CEPH_IOC_SET_LAYOUT_POLICY:
  319. return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
  320. case CEPH_IOC_GET_DATALOC:
  321. return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  322. case CEPH_IOC_LAZYIO:
  323. return ceph_ioctl_lazyio(file);
  324. case CEPH_IOC_SYNCIO:
  325. return ceph_ioctl_syncio(file);
  326. case FS_IOC_SET_ENCRYPTION_POLICY:
  327. return ceph_set_encryption_policy(file, arg);
  328. case FS_IOC_GET_ENCRYPTION_POLICY:
  329. ret = vet_mds_for_fscrypt(file);
  330. if (ret)
  331. return ret;
  332. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  333. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  334. ret = vet_mds_for_fscrypt(file);
  335. if (ret)
  336. return ret;
  337. return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
  338. case FS_IOC_ADD_ENCRYPTION_KEY:
  339. ret = vet_mds_for_fscrypt(file);
  340. if (ret)
  341. return ret;
  342. return fscrypt_ioctl_add_key(file, (void __user *)arg);
  343. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  344. return fscrypt_ioctl_remove_key(file, (void __user *)arg);
  345. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  346. return fscrypt_ioctl_remove_key_all_users(file,
  347. (void __user *)arg);
  348. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  349. return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
  350. case FS_IOC_GET_ENCRYPTION_NONCE:
  351. ret = vet_mds_for_fscrypt(file);
  352. if (ret)
  353. return ret;
  354. return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
  355. }
  356. return -ENOTTY;
  357. }