Commit 8f4e91dee2a245e4be6942f4a8d83a769e13a47d

Authored by Sage Weil
1 parent a8e63b7d51

ceph: ioctls

A few Ceph ioctls for getting and setting file layout (striping)
parameters, and learning the identity and network address of the OSD a
given region of a file is stored on.

Signed-off-by: Sage Weil <sage@newdream.net>

Showing 3 changed files with 197 additions and 0 deletions Side-by-side Diff

Documentation/ioctl/ioctl-number.txt
... ... @@ -182,6 +182,7 @@
182 182 <http://www.proximity.com.au/~brian/winradio/>
183 183 0x90 00 drivers/cdrom/sbpcd.h
184 184 0x93 60-7F linux/auto_fs.h
  185 +0x97 00-7F fs/ceph/ioctl.h Ceph file system
185 186 0x99 00-0F 537-Addinboard driver
186 187 <mailto:buk@buks.ipn.de>
187 188 0xA0 all linux/sdp/sdp.h Industrial Device Project
  1 +#include <linux/in.h>
  2 +
  3 +#include "ioctl.h"
  4 +#include "super.h"
  5 +#include "ceph_debug.h"
  6 +
  7 +
  8 +/*
  9 + * ioctls
  10 + */
  11 +
  12 +/*
  13 + * get and set the file layout
  14 + */
  15 +static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
  16 +{
  17 + struct ceph_inode_info *ci = ceph_inode(file->f_dentry->d_inode);
  18 + struct ceph_ioctl_layout l;
  19 + int err;
  20 +
  21 + err = ceph_do_getattr(file->f_dentry->d_inode, CEPH_STAT_CAP_LAYOUT);
  22 + if (!err) {
  23 + l.stripe_unit = ceph_file_layout_su(ci->i_layout);
  24 + l.stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
  25 + l.object_size = ceph_file_layout_object_size(ci->i_layout);
  26 + l.data_pool = le32_to_cpu(ci->i_layout.fl_pg_pool);
  27 + if (copy_to_user(arg, &l, sizeof(l)))
  28 + return -EFAULT;
  29 + }
  30 +
  31 + return err;
  32 +}
  33 +
  34 +static long ceph_ioctl_set_layout(struct file *file, void __user *arg)
  35 +{
  36 + struct inode *inode = file->f_dentry->d_inode;
  37 + struct inode *parent_inode = file->f_dentry->d_parent->d_inode;
  38 + struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
  39 + struct ceph_mds_request *req;
  40 + struct ceph_ioctl_layout l;
  41 + int err, i;
  42 +
  43 + /* copy and validate */
  44 + if (copy_from_user(&l, arg, sizeof(l)))
  45 + return -EFAULT;
  46 +
  47 + if ((l.object_size & ~PAGE_MASK) ||
  48 + (l.stripe_unit & ~PAGE_MASK) ||
  49 + !l.stripe_unit ||
  50 + (l.object_size &&
  51 + (unsigned)l.object_size % (unsigned)l.stripe_unit))
  52 + return -EINVAL;
  53 +
  54 + /* make sure it's a valid data pool */
  55 + if (l.data_pool > 0) {
  56 + mutex_lock(&mdsc->mutex);
  57 + err = -EINVAL;
  58 + for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
  59 + if (mdsc->mdsmap->m_data_pg_pools[i] == l.data_pool) {
  60 + err = 0;
  61 + break;
  62 + }
  63 + mutex_unlock(&mdsc->mutex);
  64 + if (err)
  65 + return err;
  66 + }
  67 +
  68 + req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
  69 + USE_AUTH_MDS);
  70 + if (IS_ERR(req))
  71 + return PTR_ERR(req);
  72 + req->r_inode = igrab(inode);
  73 + req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
  74 +
  75 + req->r_args.setlayout.layout.fl_stripe_unit =
  76 + cpu_to_le32(l.stripe_unit);
  77 + req->r_args.setlayout.layout.fl_stripe_count =
  78 + cpu_to_le32(l.stripe_count);
  79 + req->r_args.setlayout.layout.fl_object_size =
  80 + cpu_to_le32(l.object_size);
  81 + req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
  82 + req->r_args.setlayout.layout.fl_pg_preferred = cpu_to_le32((s32)-1);
  83 +
  84 + err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  85 + ceph_mdsc_put_request(req);
  86 + return err;
  87 +}
  88 +
  89 +/*
  90 + * Return object name, size/offset information, and location (OSD
  91 + * number, network address) for a given file offset.
  92 + */
  93 +static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
  94 +{
  95 + struct ceph_ioctl_dataloc dl;
  96 + struct inode *inode = file->f_dentry->d_inode;
  97 + struct ceph_inode_info *ci = ceph_inode(inode);
  98 + struct ceph_osd_client *osdc = &ceph_client(inode->i_sb)->osdc;
  99 + u64 len = 1, olen;
  100 + u64 tmp;
  101 + struct ceph_object_layout ol;
  102 + union ceph_pg pgid;
  103 +
  104 + /* copy and validate */
  105 + if (copy_from_user(&dl, arg, sizeof(dl)))
  106 + return -EFAULT;
  107 +
  108 + down_read(&osdc->map_sem);
  109 + ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, &len,
  110 + &dl.object_no, &dl.object_offset, &olen);
  111 + dl.file_offset -= dl.object_offset;
  112 + dl.object_size = ceph_file_layout_object_size(ci->i_layout);
  113 + dl.block_size = ceph_file_layout_su(ci->i_layout);
  114 +
  115 + /* block_offset = object_offset % block_size */
  116 + tmp = dl.object_offset;
  117 + dl.block_offset = do_div(tmp, dl.block_size);
  118 +
  119 + snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
  120 + ceph_ino(inode), dl.object_no);
  121 + ceph_calc_object_layout(&ol, dl.object_name, &ci->i_layout,
  122 + osdc->osdmap);
  123 +
  124 + pgid.pg64 = le64_to_cpu(ol.ol_pgid);
  125 + dl.osd = ceph_calc_pg_primary(osdc->osdmap, pgid);
  126 + if (dl.osd >= 0) {
  127 + struct ceph_entity_addr *a =
  128 + ceph_osd_addr(osdc->osdmap, dl.osd);
  129 + if (a)
  130 + memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
  131 + } else {
  132 + memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
  133 + }
  134 + up_read(&osdc->map_sem);
  135 +
  136 + /* send result back to user */
  137 + if (copy_to_user(arg, &dl, sizeof(dl)))
  138 + return -EFAULT;
  139 +
  140 + return 0;
  141 +}
  142 +
  143 +long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  144 +{
  145 + dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
  146 + switch (cmd) {
  147 + case CEPH_IOC_GET_LAYOUT:
  148 + return ceph_ioctl_get_layout(file, (void __user *)arg);
  149 +
  150 + case CEPH_IOC_SET_LAYOUT:
  151 + return ceph_ioctl_set_layout(file, (void __user *)arg);
  152 +
  153 + case CEPH_IOC_GET_DATALOC:
  154 + return ceph_ioctl_get_dataloc(file, (void __user *)arg);
  155 + }
  156 + return -ENOTTY;
  157 +}
  1 +#ifndef FS_CEPH_IOCTL_H
  2 +#define FS_CEPH_IOCTL_H
  3 +
  4 +#include <linux/ioctl.h>
  5 +#include <linux/types.h>
  6 +
  7 +#define CEPH_IOCTL_MAGIC 0x97
  8 +
  9 +/* just use u64 to align sanely on all archs */
  10 +struct ceph_ioctl_layout {
  11 + __u64 stripe_unit, stripe_count, object_size;
  12 + __u64 data_pool;
  13 +};
  14 +
  15 +#define CEPH_IOC_GET_LAYOUT _IOR(CEPH_IOCTL_MAGIC, 1, \
  16 + struct ceph_ioctl_layout)
  17 +#define CEPH_IOC_SET_LAYOUT _IOW(CEPH_IOCTL_MAGIC, 2, \
  18 + struct ceph_ioctl_layout)
  19 +
  20 +/*
  21 + * Extract identity, address of the OSD and object storing a given
  22 + * file offset.
  23 + */
  24 +struct ceph_ioctl_dataloc {
  25 + __u64 file_offset; /* in+out: file offset */
  26 + __u64 object_offset; /* out: offset in object */
  27 + __u64 object_no; /* out: object # */
  28 + __u64 object_size; /* out: object size */
  29 + char object_name[64]; /* out: object name */
  30 + __u64 block_offset; /* out: offset in block */
  31 + __u64 block_size; /* out: block length */
  32 + __s64 osd; /* out: osd # */
  33 + struct sockaddr_storage osd_addr; /* out: osd address */
  34 +};
  35 +
  36 +#define CEPH_IOC_GET_DATALOC _IOWR(CEPH_IOCTL_MAGIC, 3, \
  37 + struct ceph_ioctl_dataloc)
  38 +
  39 +#endif