ioctl.c 7.6 KB

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