ioctl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /*
  9. * ioctls
  10. */
  11. /*
  12. * get and set the file layout
  13. */
  14. static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
  15. {
  16. struct ceph_inode_info *ci = ceph_inode(file_inode(file));
  17. struct ceph_ioctl_layout l;
  18. int err;
  19. err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
  20. if (!err) {
  21. l.stripe_unit = ci->i_layout.stripe_unit;
  22. l.stripe_count = ci->i_layout.stripe_count;
  23. l.object_size = ci->i_layout.object_size;
  24. l.data_pool = ci->i_layout.pool_id;
  25. l.preferred_osd = -1;
  26. if (copy_to_user(arg, &l, sizeof(l)))
  27. return -EFAULT;
  28. }
  29. return err;
  30. }
  31. static long __validate_layout(struct ceph_mds_client *mdsc,
  32. struct ceph_ioctl_layout *l)
  33. {
  34. int i, err;
  35. /* validate striping parameters */
  36. if ((l->object_size & ~PAGE_MASK) ||
  37. (l->stripe_unit & ~PAGE_MASK) ||
  38. ((unsigned)l->stripe_unit != 0 &&
  39. ((unsigned)l->object_size % (unsigned)l->stripe_unit)))
  40. return -EINVAL;
  41. /* make sure it's a valid data pool */
  42. mutex_lock(&mdsc->mutex);
  43. err = -EINVAL;
  44. for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
  45. if (mdsc->mdsmap->m_data_pg_pools[i] == l->data_pool) {
  46. err = 0;
  47. break;
  48. }
  49. mutex_unlock(&mdsc->mutex);
  50. if (err)
  51. return err;
  52. return 0;
  53. }
  54. static long ceph_ioctl_set_layout(struct file *file, void __user *arg)
  55. {
  56. struct inode *inode = file_inode(file);
  57. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  58. struct ceph_mds_request *req;
  59. struct ceph_ioctl_layout l;
  60. struct ceph_inode_info *ci = ceph_inode(file_inode(file));
  61. struct ceph_ioctl_layout nl;
  62. int err;
  63. if (copy_from_user(&l, arg, sizeof(l)))
  64. return -EFAULT;
  65. /* validate changed params against current layout */
  66. err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
  67. if (err)
  68. return err;
  69. memset(&nl, 0, sizeof(nl));
  70. if (l.stripe_count)
  71. nl.stripe_count = l.stripe_count;
  72. else
  73. nl.stripe_count = ci->i_layout.stripe_count;
  74. if (l.stripe_unit)
  75. nl.stripe_unit = l.stripe_unit;
  76. else
  77. nl.stripe_unit = ci->i_layout.stripe_unit;
  78. if (l.object_size)
  79. nl.object_size = l.object_size;
  80. else
  81. nl.object_size = ci->i_layout.object_size;
  82. if (l.data_pool)
  83. nl.data_pool = l.data_pool;
  84. else
  85. nl.data_pool = ci->i_layout.pool_id;
  86. /* this is obsolete, and always -1 */
  87. nl.preferred_osd = -1;
  88. err = __validate_layout(mdsc, &nl);
  89. if (err)
  90. return err;
  91. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
  92. USE_AUTH_MDS);
  93. if (IS_ERR(req))
  94. return PTR_ERR(req);
  95. req->r_inode = inode;
  96. ihold(inode);
  97. req->r_num_caps = 1;
  98. req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
  99. req->r_args.setlayout.layout.fl_stripe_unit =
  100. cpu_to_le32(l.stripe_unit);
  101. req->r_args.setlayout.layout.fl_stripe_count =
  102. cpu_to_le32(l.stripe_count);
  103. req->r_args.setlayout.layout.fl_object_size =
  104. cpu_to_le32(l.object_size);
  105. req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
  106. err = ceph_mdsc_do_request(mdsc, NULL, req);
  107. ceph_mdsc_put_request(req);
  108. return err;
  109. }
  110. /*
  111. * Set a layout policy on a directory inode. All items in the tree
  112. * rooted at this inode will inherit this layout on creation,
  113. * (It doesn't apply retroactively )
  114. * unless a subdirectory has its own layout policy.
  115. */
  116. static long ceph_ioctl_set_layout_policy (struct file *file, void __user *arg)
  117. {
  118. struct inode *inode = file_inode(file);
  119. struct ceph_mds_request *req;
  120. struct ceph_ioctl_layout l;
  121. int err;
  122. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  123. /* copy and validate */
  124. if (copy_from_user(&l, arg, sizeof(l)))
  125. return -EFAULT;
  126. err = __validate_layout(mdsc, &l);
  127. if (err)
  128. return err;
  129. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETDIRLAYOUT,
  130. USE_AUTH_MDS);
  131. if (IS_ERR(req))
  132. return PTR_ERR(req);
  133. req->r_inode = inode;
  134. ihold(inode);
  135. req->r_num_caps = 1;
  136. req->r_args.setlayout.layout.fl_stripe_unit =
  137. cpu_to_le32(l.stripe_unit);
  138. req->r_args.setlayout.layout.fl_stripe_count =
  139. cpu_to_le32(l.stripe_count);
  140. req->r_args.setlayout.layout.fl_object_size =
  141. cpu_to_le32(l.object_size);
  142. req->r_args.setlayout.layout.fl_pg_pool =
  143. cpu_to_le32(l.data_pool);
  144. err = ceph_mdsc_do_request(mdsc, inode, req);
  145. ceph_mdsc_put_request(req);
  146. return err;
  147. }
  148. /*
  149. * Return object name, size/offset information, and location (OSD
  150. * number, network address) for a given file offset.
  151. */
  152. static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
  153. {
  154. struct ceph_ioctl_dataloc dl;
  155. struct inode *inode = file_inode(file);
  156. struct ceph_inode_info *ci = ceph_inode(inode);
  157. struct ceph_osd_client *osdc =
  158. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  159. struct ceph_object_locator oloc;
  160. CEPH_DEFINE_OID_ONSTACK(oid);
  161. u32 xlen;
  162. u64 tmp;
  163. struct ceph_pg pgid;
  164. int r;
  165. /* copy and validate */
  166. if (copy_from_user(&dl, arg, sizeof(dl)))
  167. return -EFAULT;
  168. down_read(&osdc->lock);
  169. ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, 1,
  170. &dl.object_no, &dl.object_offset, &xlen);
  171. dl.file_offset -= dl.object_offset;
  172. dl.object_size = ci->i_layout.object_size;
  173. dl.block_size = ci->i_layout.stripe_unit;
  174. /* block_offset = object_offset % block_size */
  175. tmp = dl.object_offset;
  176. dl.block_offset = do_div(tmp, dl.block_size);
  177. snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
  178. ceph_ino(inode), dl.object_no);
  179. oloc.pool = ci->i_layout.pool_id;
  180. oloc.pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
  181. ceph_oid_printf(&oid, "%s", dl.object_name);
  182. r = ceph_object_locator_to_pg(osdc->osdmap, &oid, &oloc, &pgid);
  183. ceph_oloc_destroy(&oloc);
  184. if (r < 0) {
  185. up_read(&osdc->lock);
  186. return r;
  187. }
  188. dl.osd = ceph_pg_to_acting_primary(osdc->osdmap, &pgid);
  189. if (dl.osd >= 0) {
  190. struct ceph_entity_addr *a =
  191. ceph_osd_addr(osdc->osdmap, dl.osd);
  192. if (a)
  193. memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
  194. } else {
  195. memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
  196. }
  197. up_read(&osdc->lock);
  198. /* send result back to user */
  199. if (copy_to_user(arg, &dl, sizeof(dl)))
  200. return -EFAULT;
  201. return 0;
  202. }
  203. static long ceph_ioctl_lazyio(struct file *file)
  204. {
  205. struct ceph_file_info *fi = file->private_data;
  206. struct inode *inode = file_inode(file);
  207. struct ceph_inode_info *ci = ceph_inode(inode);
  208. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  209. if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
  210. spin_lock(&ci->i_ceph_lock);
  211. fi->fmode |= CEPH_FILE_MODE_LAZY;
  212. ci->i_nr_by_mode[ffs(CEPH_FILE_MODE_LAZY)]++;
  213. __ceph_touch_fmode(ci, mdsc, fi->fmode);
  214. spin_unlock(&ci->i_ceph_lock);
  215. dout("ioctl_layzio: file %p marked lazy\n", file);
  216. ceph_check_caps(ci, 0, NULL);
  217. } else {
  218. dout("ioctl_layzio: file %p already lazy\n", file);
  219. }
  220. return 0;
  221. }
  222. static long ceph_ioctl_syncio(struct file *file)
  223. {
  224. struct ceph_file_info *fi = file->private_data;
  225. fi->flags |= CEPH_F_SYNC;
  226. return 0;
  227. }
  228. long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  229. {
  230. dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
  231. switch (cmd) {
  232. case CEPH_IOC_GET_LAYOUT:
  233. return ceph_ioctl_get_layout(file, (void __user *)arg);
  234. case CEPH_IOC_SET_LAYOUT:
  235. return ceph_ioctl_set_layout(file, (void __user *)arg);
  236. case CEPH_IOC_SET_LAYOUT_POLICY:
  237. return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
  238. case CEPH_IOC_GET_DATALOC:
  239. return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  240. case CEPH_IOC_LAZYIO:
  241. return ceph_ioctl_lazyio(file);
  242. case CEPH_IOC_SYNCIO:
  243. return ceph_ioctl_syncio(file);
  244. }
  245. return -ENOTTY;
  246. }