ioctl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <linux/in.h>
  2. #include "ioctl.h"
  3. #include "super.h"
  4. #include "ceph_debug.h"
  5. /*
  6. * ioctls
  7. */
  8. /*
  9. * get and set the file layout
  10. */
  11. static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
  12. {
  13. struct ceph_inode_info *ci = ceph_inode(file->f_dentry->d_inode);
  14. struct ceph_ioctl_layout l;
  15. int err;
  16. err = ceph_do_getattr(file->f_dentry->d_inode, CEPH_STAT_CAP_LAYOUT);
  17. if (!err) {
  18. l.stripe_unit = ceph_file_layout_su(ci->i_layout);
  19. l.stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  20. l.object_size = ceph_file_layout_object_size(ci->i_layout);
  21. l.data_pool = le32_to_cpu(ci->i_layout.fl_pg_pool);
  22. l.preferred_osd =
  23. (s32)le32_to_cpu(ci->i_layout.fl_pg_preferred);
  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 = file->f_dentry->d_parent->d_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. int err, i;
  37. /* copy and validate */
  38. if (copy_from_user(&l, arg, sizeof(l)))
  39. return -EFAULT;
  40. if ((l.object_size & ~PAGE_MASK) ||
  41. (l.stripe_unit & ~PAGE_MASK) ||
  42. !l.stripe_unit ||
  43. (l.object_size &&
  44. (unsigned)l.object_size % (unsigned)l.stripe_unit))
  45. return -EINVAL;
  46. /* make sure it's a valid data pool */
  47. if (l.data_pool > 0) {
  48. mutex_lock(&mdsc->mutex);
  49. err = -EINVAL;
  50. for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
  51. if (mdsc->mdsmap->m_data_pg_pools[i] == l.data_pool) {
  52. err = 0;
  53. break;
  54. }
  55. mutex_unlock(&mdsc->mutex);
  56. if (err)
  57. return err;
  58. }
  59. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
  60. USE_AUTH_MDS);
  61. if (IS_ERR(req))
  62. return PTR_ERR(req);
  63. req->r_inode = igrab(inode);
  64. req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
  65. req->r_args.setlayout.layout.fl_stripe_unit =
  66. cpu_to_le32(l.stripe_unit);
  67. req->r_args.setlayout.layout.fl_stripe_count =
  68. cpu_to_le32(l.stripe_count);
  69. req->r_args.setlayout.layout.fl_object_size =
  70. cpu_to_le32(l.object_size);
  71. req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
  72. req->r_args.setlayout.layout.fl_pg_preferred =
  73. cpu_to_le32(l.preferred_osd);
  74. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  75. ceph_mdsc_put_request(req);
  76. return err;
  77. }
  78. /*
  79. * Return object name, size/offset information, and location (OSD
  80. * number, network address) for a given file offset.
  81. */
  82. static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
  83. {
  84. struct ceph_ioctl_dataloc dl;
  85. struct inode *inode = file->f_dentry->d_inode;
  86. struct ceph_inode_info *ci = ceph_inode(inode);
  87. struct ceph_osd_client *osdc = &ceph_sb_to_client(inode->i_sb)->osdc;
  88. u64 len = 1, olen;
  89. u64 tmp;
  90. struct ceph_object_layout ol;
  91. struct ceph_pg pgid;
  92. /* copy and validate */
  93. if (copy_from_user(&dl, arg, sizeof(dl)))
  94. return -EFAULT;
  95. down_read(&osdc->map_sem);
  96. ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, &len,
  97. &dl.object_no, &dl.object_offset, &olen);
  98. dl.file_offset -= dl.object_offset;
  99. dl.object_size = ceph_file_layout_object_size(ci->i_layout);
  100. dl.block_size = ceph_file_layout_su(ci->i_layout);
  101. /* block_offset = object_offset % block_size */
  102. tmp = dl.object_offset;
  103. dl.block_offset = do_div(tmp, dl.block_size);
  104. snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
  105. ceph_ino(inode), dl.object_no);
  106. ceph_calc_object_layout(&ol, dl.object_name, &ci->i_layout,
  107. osdc->osdmap);
  108. pgid = ol.ol_pgid;
  109. dl.osd = ceph_calc_pg_primary(osdc->osdmap, pgid);
  110. if (dl.osd >= 0) {
  111. struct ceph_entity_addr *a =
  112. ceph_osd_addr(osdc->osdmap, dl.osd);
  113. if (a)
  114. memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
  115. } else {
  116. memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
  117. }
  118. up_read(&osdc->map_sem);
  119. /* send result back to user */
  120. if (copy_to_user(arg, &dl, sizeof(dl)))
  121. return -EFAULT;
  122. return 0;
  123. }
  124. static long ceph_ioctl_lazyio(struct file *file)
  125. {
  126. struct ceph_file_info *fi = file->private_data;
  127. struct inode *inode = file->f_dentry->d_inode;
  128. struct ceph_inode_info *ci = ceph_inode(inode);
  129. if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
  130. spin_lock(&inode->i_lock);
  131. ci->i_nr_by_mode[fi->fmode]--;
  132. fi->fmode |= CEPH_FILE_MODE_LAZY;
  133. ci->i_nr_by_mode[fi->fmode]++;
  134. spin_unlock(&inode->i_lock);
  135. dout("ioctl_layzio: file %p marked lazy\n", file);
  136. ceph_check_caps(ci, 0, NULL);
  137. } else {
  138. dout("ioctl_layzio: file %p already lazy\n", file);
  139. }
  140. return 0;
  141. }
  142. long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  143. {
  144. dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
  145. switch (cmd) {
  146. case CEPH_IOC_GET_LAYOUT:
  147. return ceph_ioctl_get_layout(file, (void __user *)arg);
  148. case CEPH_IOC_SET_LAYOUT:
  149. return ceph_ioctl_set_layout(file, (void __user *)arg);
  150. case CEPH_IOC_GET_DATALOC:
  151. return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  152. case CEPH_IOC_LAZYIO:
  153. return ceph_ioctl_lazyio(file);
  154. }
  155. return -ENOTTY;
  156. }