ioctl.c 7.4 KB

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