blob: 4feab52c5bff6b4b3b4905fb5170e29804982bfd [file] [log] [blame]
Sage Weil8f4e91d2009-10-06 11:31:14 -07001#include <linux/in.h>
2
Sage Weil8f4e91d2009-10-06 11:31:14 -07003#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include "mds_client.h"
5#include <linux/ceph/ceph_debug.h>
6
7#include "ioctl.h"
Sage Weil8f4e91d2009-10-06 11:31:14 -07008
9
10/*
11 * ioctls
12 */
13
14/*
15 * get and set the file layout
16 */
17static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
18{
19 struct ceph_inode_info *ci = ceph_inode(file->f_dentry->d_inode);
20 struct ceph_ioctl_layout l;
21 int err;
22
23 err = ceph_do_getattr(file->f_dentry->d_inode, CEPH_STAT_CAP_LAYOUT);
24 if (!err) {
25 l.stripe_unit = ceph_file_layout_su(ci->i_layout);
26 l.stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
27 l.object_size = ceph_file_layout_object_size(ci->i_layout);
28 l.data_pool = le32_to_cpu(ci->i_layout.fl_pg_pool);
Sage Weil3469ac12012-05-07 15:33:36 -070029 l.preferred_osd = (s32)-1;
Sage Weil8f4e91d2009-10-06 11:31:14 -070030 if (copy_to_user(arg, &l, sizeof(l)))
31 return -EFAULT;
32 }
33
34 return err;
35}
36
37static long ceph_ioctl_set_layout(struct file *file, void __user *arg)
38{
39 struct inode *inode = file->f_dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -070040 struct inode *parent_inode;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070041 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weil8f4e91d2009-10-06 11:31:14 -070042 struct ceph_mds_request *req;
43 struct ceph_ioctl_layout l;
Greg Farnuma35eca92011-08-25 12:43:06 -070044 struct ceph_inode_info *ci = ceph_inode(file->f_dentry->d_inode);
45 struct ceph_ioctl_layout nl;
Sage Weil8f4e91d2009-10-06 11:31:14 -070046 int err, i;
47
Sage Weil8f4e91d2009-10-06 11:31:14 -070048 if (copy_from_user(&l, arg, sizeof(l)))
49 return -EFAULT;
50
Sage Weil3469ac12012-05-07 15:33:36 -070051 /* preferred_osd is no longer supported */
52 if (l.preferred_osd != -1)
53 return -EINVAL;
54
Greg Farnuma35eca92011-08-25 12:43:06 -070055 /* validate changed params against current layout */
56 err = ceph_do_getattr(file->f_dentry->d_inode, CEPH_STAT_CAP_LAYOUT);
57 if (!err) {
58 nl.stripe_unit = ceph_file_layout_su(ci->i_layout);
59 nl.stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
60 nl.object_size = ceph_file_layout_object_size(ci->i_layout);
61 nl.data_pool = le32_to_cpu(ci->i_layout.fl_pg_pool);
Greg Farnuma35eca92011-08-25 12:43:06 -070062 } else
63 return err;
64
65 if (l.stripe_count)
66 nl.stripe_count = l.stripe_count;
67 if (l.stripe_unit)
68 nl.stripe_unit = l.stripe_unit;
69 if (l.object_size)
70 nl.object_size = l.object_size;
71 if (l.data_pool)
72 nl.data_pool = l.data_pool;
Greg Farnuma35eca92011-08-25 12:43:06 -070073
74 if ((nl.object_size & ~PAGE_MASK) ||
75 (nl.stripe_unit & ~PAGE_MASK) ||
76 ((unsigned)nl.object_size % (unsigned)nl.stripe_unit))
Sage Weil8f4e91d2009-10-06 11:31:14 -070077 return -EINVAL;
78
79 /* make sure it's a valid data pool */
80 if (l.data_pool > 0) {
81 mutex_lock(&mdsc->mutex);
82 err = -EINVAL;
83 for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
84 if (mdsc->mdsmap->m_data_pg_pools[i] == l.data_pool) {
85 err = 0;
86 break;
87 }
88 mutex_unlock(&mdsc->mutex);
89 if (err)
90 return err;
91 }
92
93 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
94 USE_AUTH_MDS);
95 if (IS_ERR(req))
96 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -070097 req->r_inode = inode;
98 ihold(inode);
Sage Weil8f4e91d2009-10-06 11:31:14 -070099 req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
100
101 req->r_args.setlayout.layout.fl_stripe_unit =
102 cpu_to_le32(l.stripe_unit);
103 req->r_args.setlayout.layout.fl_stripe_count =
104 cpu_to_le32(l.stripe_count);
105 req->r_args.setlayout.layout.fl_object_size =
106 cpu_to_le32(l.object_size);
107 req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
Sage Weil8f4e91d2009-10-06 11:31:14 -0700108
Sage Weil5f21c962011-07-26 11:30:29 -0700109 parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
Sage Weil8f4e91d2009-10-06 11:31:14 -0700110 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700111 iput(parent_inode);
Sage Weil8f4e91d2009-10-06 11:31:14 -0700112 ceph_mdsc_put_request(req);
113 return err;
114}
115
116/*
Greg Farnum571dba52010-09-24 14:56:40 -0700117 * Set a layout policy on a directory inode. All items in the tree
118 * rooted at this inode will inherit this layout on creation,
119 * (It doesn't apply retroactively )
120 * unless a subdirectory has its own layout policy.
121 */
122static long ceph_ioctl_set_layout_policy (struct file *file, void __user *arg)
123{
124 struct inode *inode = file->f_dentry->d_inode;
125 struct ceph_mds_request *req;
126 struct ceph_ioctl_layout l;
127 int err, i;
128 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
129
130 /* copy and validate */
131 if (copy_from_user(&l, arg, sizeof(l)))
132 return -EFAULT;
133
134 if ((l.object_size & ~PAGE_MASK) ||
135 (l.stripe_unit & ~PAGE_MASK) ||
136 !l.stripe_unit ||
137 (l.object_size &&
138 (unsigned)l.object_size % (unsigned)l.stripe_unit))
139 return -EINVAL;
140
141 /* make sure it's a valid data pool */
142 if (l.data_pool > 0) {
143 mutex_lock(&mdsc->mutex);
144 err = -EINVAL;
145 for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
146 if (mdsc->mdsmap->m_data_pg_pools[i] == l.data_pool) {
147 err = 0;
148 break;
149 }
150 mutex_unlock(&mdsc->mutex);
151 if (err)
152 return err;
153 }
154
155 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETDIRLAYOUT,
156 USE_AUTH_MDS);
157
158 if (IS_ERR(req))
159 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700160 req->r_inode = inode;
161 ihold(inode);
Greg Farnum571dba52010-09-24 14:56:40 -0700162
163 req->r_args.setlayout.layout.fl_stripe_unit =
164 cpu_to_le32(l.stripe_unit);
165 req->r_args.setlayout.layout.fl_stripe_count =
166 cpu_to_le32(l.stripe_count);
167 req->r_args.setlayout.layout.fl_object_size =
168 cpu_to_le32(l.object_size);
169 req->r_args.setlayout.layout.fl_pg_pool =
170 cpu_to_le32(l.data_pool);
Greg Farnum571dba52010-09-24 14:56:40 -0700171
172 err = ceph_mdsc_do_request(mdsc, inode, req);
173 ceph_mdsc_put_request(req);
174 return err;
175}
176
177/*
Sage Weil8f4e91d2009-10-06 11:31:14 -0700178 * Return object name, size/offset information, and location (OSD
179 * number, network address) for a given file offset.
180 */
181static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
182{
183 struct ceph_ioctl_dataloc dl;
184 struct inode *inode = file->f_dentry->d_inode;
185 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700186 struct ceph_osd_client *osdc =
187 &ceph_sb_to_client(inode->i_sb)->client->osdc;
Sage Weil8f4e91d2009-10-06 11:31:14 -0700188 u64 len = 1, olen;
189 u64 tmp;
190 struct ceph_object_layout ol;
Sage Weil51042122009-11-04 11:39:12 -0800191 struct ceph_pg pgid;
Sage Weil8f4e91d2009-10-06 11:31:14 -0700192
193 /* copy and validate */
194 if (copy_from_user(&dl, arg, sizeof(dl)))
195 return -EFAULT;
196
197 down_read(&osdc->map_sem);
198 ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, &len,
199 &dl.object_no, &dl.object_offset, &olen);
200 dl.file_offset -= dl.object_offset;
201 dl.object_size = ceph_file_layout_object_size(ci->i_layout);
202 dl.block_size = ceph_file_layout_su(ci->i_layout);
203
204 /* block_offset = object_offset % block_size */
205 tmp = dl.object_offset;
206 dl.block_offset = do_div(tmp, dl.block_size);
207
208 snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
209 ceph_ino(inode), dl.object_no);
210 ceph_calc_object_layout(&ol, dl.object_name, &ci->i_layout,
211 osdc->osdmap);
212
Sage Weil51042122009-11-04 11:39:12 -0800213 pgid = ol.ol_pgid;
Sage Weil8f4e91d2009-10-06 11:31:14 -0700214 dl.osd = ceph_calc_pg_primary(osdc->osdmap, pgid);
215 if (dl.osd >= 0) {
216 struct ceph_entity_addr *a =
217 ceph_osd_addr(osdc->osdmap, dl.osd);
218 if (a)
219 memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
220 } else {
221 memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
222 }
223 up_read(&osdc->map_sem);
224
225 /* send result back to user */
226 if (copy_to_user(arg, &dl, sizeof(dl)))
227 return -EFAULT;
228
229 return 0;
230}
231
Sage Weil8c6e9222010-04-16 09:53:43 -0700232static long ceph_ioctl_lazyio(struct file *file)
233{
234 struct ceph_file_info *fi = file->private_data;
235 struct inode *inode = file->f_dentry->d_inode;
236 struct ceph_inode_info *ci = ceph_inode(inode);
237
238 if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
Sage Weilbe655592011-11-30 09:47:09 -0800239 spin_lock(&ci->i_ceph_lock);
Sage Weil8c6e9222010-04-16 09:53:43 -0700240 ci->i_nr_by_mode[fi->fmode]--;
241 fi->fmode |= CEPH_FILE_MODE_LAZY;
242 ci->i_nr_by_mode[fi->fmode]++;
Sage Weilbe655592011-11-30 09:47:09 -0800243 spin_unlock(&ci->i_ceph_lock);
Sage Weil8c6e9222010-04-16 09:53:43 -0700244 dout("ioctl_layzio: file %p marked lazy\n", file);
245
246 ceph_check_caps(ci, 0, NULL);
247 } else {
248 dout("ioctl_layzio: file %p already lazy\n", file);
249 }
250 return 0;
251}
252
Sage Weil4918b6d2011-07-26 11:26:07 -0700253static long ceph_ioctl_syncio(struct file *file)
254{
255 struct ceph_file_info *fi = file->private_data;
256
257 fi->flags |= CEPH_F_SYNC;
258 return 0;
259}
260
Sage Weil8f4e91d2009-10-06 11:31:14 -0700261long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
262{
263 dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
264 switch (cmd) {
265 case CEPH_IOC_GET_LAYOUT:
266 return ceph_ioctl_get_layout(file, (void __user *)arg);
267
268 case CEPH_IOC_SET_LAYOUT:
269 return ceph_ioctl_set_layout(file, (void __user *)arg);
270
Greg Farnum571dba52010-09-24 14:56:40 -0700271 case CEPH_IOC_SET_LAYOUT_POLICY:
272 return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
273
Sage Weil8f4e91d2009-10-06 11:31:14 -0700274 case CEPH_IOC_GET_DATALOC:
275 return ceph_ioctl_get_dataloc(file, (void __user *)arg);
Sage Weil8c6e9222010-04-16 09:53:43 -0700276
277 case CEPH_IOC_LAZYIO:
278 return ceph_ioctl_lazyio(file);
Sage Weil4918b6d2011-07-26 11:26:07 -0700279
280 case CEPH_IOC_SYNCIO:
281 return ceph_ioctl_syncio(file);
Sage Weil8f4e91d2009-10-06 11:31:14 -0700282 }
Greg Farnum571dba52010-09-24 14:56:40 -0700283
Sage Weil8f4e91d2009-10-06 11:31:14 -0700284 return -ENOTTY;
285}