blob: e4f99eff5d938e2dcd906eccba31a99464c672bc [file] [log] [blame]
Sage Weil8f4e91d2009-10-06 11:31:14 -07001#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 */
15static 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
34static 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 */
93static 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
143long 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}