ioctl.c 7.3 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_inode(file));
  15. struct ceph_ioctl_layout l;
  16. int err;
  17. err = ceph_do_getattr(file_inode(file), 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 __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. (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 inode *parent_inode;
  56. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  57. struct ceph_mds_request *req;
  58. struct ceph_ioctl_layout l;
  59. struct ceph_inode_info *ci = ceph_inode(file_inode(file));
  60. struct ceph_ioctl_layout nl;
  61. int err;
  62. if (copy_from_user(&l, arg, sizeof(l)))
  63. return -EFAULT;
  64. /* validate changed params against current layout */
  65. err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT);
  66. if (err)
  67. return err;
  68. memset(&nl, 0, sizeof(nl));
  69. if (l.stripe_count)
  70. nl.stripe_count = l.stripe_count;
  71. else
  72. nl.stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  73. if (l.stripe_unit)
  74. nl.stripe_unit = l.stripe_unit;
  75. else
  76. nl.stripe_unit = ceph_file_layout_su(ci->i_layout);
  77. if (l.object_size)
  78. nl.object_size = l.object_size;
  79. else
  80. nl.object_size = ceph_file_layout_object_size(ci->i_layout);
  81. if (l.data_pool)
  82. nl.data_pool = l.data_pool;
  83. else
  84. nl.data_pool = ceph_file_layout_pg_pool(ci->i_layout);
  85. /* this is obsolete, and always -1 */
  86. nl.preferred_osd = le64_to_cpu(-1);
  87. err = __validate_layout(mdsc, &nl);
  88. if (err)
  89. return err;
  90. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
  91. USE_AUTH_MDS);
  92. if (IS_ERR(req))
  93. return PTR_ERR(req);
  94. req->r_inode = inode;
  95. ihold(inode);
  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. parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
  105. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  106. iput(parent_inode);
  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_args.setlayout.layout.fl_stripe_unit =
  136. cpu_to_le32(l.stripe_unit);
  137. req->r_args.setlayout.layout.fl_stripe_count =
  138. cpu_to_le32(l.stripe_count);
  139. req->r_args.setlayout.layout.fl_object_size =
  140. cpu_to_le32(l.object_size);
  141. req->r_args.setlayout.layout.fl_pg_pool =
  142. cpu_to_le32(l.data_pool);
  143. err = ceph_mdsc_do_request(mdsc, inode, req);
  144. ceph_mdsc_put_request(req);
  145. return err;
  146. }
  147. /*
  148. * Return object name, size/offset information, and location (OSD
  149. * number, network address) for a given file offset.
  150. */
  151. static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
  152. {
  153. struct ceph_ioctl_dataloc dl;
  154. struct inode *inode = file_inode(file);
  155. struct ceph_inode_info *ci = ceph_inode(inode);
  156. struct ceph_osd_client *osdc =
  157. &ceph_sb_to_client(inode->i_sb)->client->osdc;
  158. u64 len = 1, olen;
  159. u64 tmp;
  160. struct ceph_pg pgid;
  161. int r;
  162. /* copy and validate */
  163. if (copy_from_user(&dl, arg, sizeof(dl)))
  164. return -EFAULT;
  165. down_read(&osdc->map_sem);
  166. r = ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, len,
  167. &dl.object_no, &dl.object_offset,
  168. &olen);
  169. if (r < 0)
  170. return -EIO;
  171. dl.file_offset -= dl.object_offset;
  172. dl.object_size = ceph_file_layout_object_size(ci->i_layout);
  173. dl.block_size = ceph_file_layout_su(ci->i_layout);
  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. ceph_calc_object_layout(&pgid, dl.object_name, &ci->i_layout,
  180. osdc->osdmap);
  181. dl.osd = ceph_calc_pg_primary(osdc->osdmap, pgid);
  182. if (dl.osd >= 0) {
  183. struct ceph_entity_addr *a =
  184. ceph_osd_addr(osdc->osdmap, dl.osd);
  185. if (a)
  186. memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
  187. } else {
  188. memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
  189. }
  190. up_read(&osdc->map_sem);
  191. /* send result back to user */
  192. if (copy_to_user(arg, &dl, sizeof(dl)))
  193. return -EFAULT;
  194. return 0;
  195. }
  196. static long ceph_ioctl_lazyio(struct file *file)
  197. {
  198. struct ceph_file_info *fi = file->private_data;
  199. struct inode *inode = file_inode(file);
  200. struct ceph_inode_info *ci = ceph_inode(inode);
  201. if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
  202. spin_lock(&ci->i_ceph_lock);
  203. ci->i_nr_by_mode[fi->fmode]--;
  204. fi->fmode |= CEPH_FILE_MODE_LAZY;
  205. ci->i_nr_by_mode[fi->fmode]++;
  206. spin_unlock(&ci->i_ceph_lock);
  207. dout("ioctl_layzio: file %p marked lazy\n", file);
  208. ceph_check_caps(ci, 0, NULL);
  209. } else {
  210. dout("ioctl_layzio: file %p already lazy\n", file);
  211. }
  212. return 0;
  213. }
  214. static long ceph_ioctl_syncio(struct file *file)
  215. {
  216. struct ceph_file_info *fi = file->private_data;
  217. fi->flags |= CEPH_F_SYNC;
  218. return 0;
  219. }
  220. long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  221. {
  222. dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
  223. switch (cmd) {
  224. case CEPH_IOC_GET_LAYOUT:
  225. return ceph_ioctl_get_layout(file, (void __user *)arg);
  226. case CEPH_IOC_SET_LAYOUT:
  227. return ceph_ioctl_set_layout(file, (void __user *)arg);
  228. case CEPH_IOC_SET_LAYOUT_POLICY:
  229. return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
  230. case CEPH_IOC_GET_DATALOC:
  231. return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  232. case CEPH_IOC_LAZYIO:
  233. return ceph_ioctl_lazyio(file);
  234. case CEPH_IOC_SYNCIO:
  235. return ceph_ioctl_syncio(file);
  236. }
  237. return -ENOTTY;
  238. }