ioctl.c 6.8 KB

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