xref: /openbmc/linux/fs/ceph/ioctl.c (revision 779fe0fb8e1883d5c479ac6bd85fbd237deed1f7)
196c57adeSLinus Torvalds #include <linux/ceph/ceph_debug.h>
28f4e91deSSage Weil #include <linux/in.h>
38f4e91deSSage Weil 
48f4e91deSSage Weil #include "super.h"
53d14c5d2SYehuda Sadeh #include "mds_client.h"
63d14c5d2SYehuda Sadeh #include "ioctl.h"
78f4e91deSSage Weil 
88f4e91deSSage Weil 
98f4e91deSSage Weil /*
108f4e91deSSage Weil  * ioctls
118f4e91deSSage Weil  */
128f4e91deSSage Weil 
138f4e91deSSage Weil /*
148f4e91deSSage Weil  * get and set the file layout
158f4e91deSSage Weil  */
168f4e91deSSage Weil static long ceph_ioctl_get_layout(struct file *file, void __user *arg)
178f4e91deSSage Weil {
18496ad9aaSAl Viro 	struct ceph_inode_info *ci = ceph_inode(file_inode(file));
198f4e91deSSage Weil 	struct ceph_ioctl_layout l;
208f4e91deSSage Weil 	int err;
218f4e91deSSage Weil 
22508b32d8SYan, Zheng 	err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
238f4e91deSSage Weil 	if (!err) {
247627151eSYan, Zheng 		l.stripe_unit = ci->i_layout.stripe_unit;
257627151eSYan, Zheng 		l.stripe_count = ci->i_layout.stripe_count;
267627151eSYan, Zheng 		l.object_size = ci->i_layout.object_size;
277627151eSYan, Zheng 		l.data_pool = ci->i_layout.pool_id;
283469ac1aSSage Weil 		l.preferred_osd = (s32)-1;
298f4e91deSSage Weil 		if (copy_to_user(arg, &l, sizeof(l)))
308f4e91deSSage Weil 			return -EFAULT;
318f4e91deSSage Weil 	}
328f4e91deSSage Weil 
338f4e91deSSage Weil 	return err;
348f4e91deSSage Weil }
358f4e91deSSage Weil 
36e49bf4c5SSage Weil static long __validate_layout(struct ceph_mds_client *mdsc,
37e49bf4c5SSage Weil 			      struct ceph_ioctl_layout *l)
38e49bf4c5SSage Weil {
39e49bf4c5SSage Weil 	int i, err;
40e49bf4c5SSage Weil 
41e49bf4c5SSage Weil 	/* validate striping parameters */
42e49bf4c5SSage Weil 	if ((l->object_size & ~PAGE_MASK) ||
43e49bf4c5SSage Weil 	    (l->stripe_unit & ~PAGE_MASK) ||
440bc62284SYan, Zheng 	    ((unsigned)l->stripe_unit != 0 &&
4545f2e081SSage Weil 	     ((unsigned)l->object_size % (unsigned)l->stripe_unit)))
46e49bf4c5SSage Weil 		return -EINVAL;
47e49bf4c5SSage Weil 
48e49bf4c5SSage Weil 	/* make sure it's a valid data pool */
49e49bf4c5SSage Weil 	mutex_lock(&mdsc->mutex);
50e49bf4c5SSage Weil 	err = -EINVAL;
51e49bf4c5SSage Weil 	for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++)
52e49bf4c5SSage Weil 		if (mdsc->mdsmap->m_data_pg_pools[i] == l->data_pool) {
53e49bf4c5SSage Weil 			err = 0;
54e49bf4c5SSage Weil 			break;
55e49bf4c5SSage Weil 		}
56e49bf4c5SSage Weil 	mutex_unlock(&mdsc->mutex);
57e49bf4c5SSage Weil 	if (err)
58e49bf4c5SSage Weil 		return err;
59e49bf4c5SSage Weil 
60e49bf4c5SSage Weil 	return 0;
61e49bf4c5SSage Weil }
62e49bf4c5SSage Weil 
638f4e91deSSage Weil static long ceph_ioctl_set_layout(struct file *file, void __user *arg)
648f4e91deSSage Weil {
65496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
663d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
678f4e91deSSage Weil 	struct ceph_mds_request *req;
688f4e91deSSage Weil 	struct ceph_ioctl_layout l;
69496ad9aaSAl Viro 	struct ceph_inode_info *ci = ceph_inode(file_inode(file));
70a35eca95SGreg Farnum 	struct ceph_ioctl_layout nl;
71e49bf4c5SSage Weil 	int err;
728f4e91deSSage Weil 
738f4e91deSSage Weil 	if (copy_from_user(&l, arg, sizeof(l)))
748f4e91deSSage Weil 		return -EFAULT;
758f4e91deSSage Weil 
76a35eca95SGreg Farnum 	/* validate changed params against current layout */
77508b32d8SYan, Zheng 	err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false);
78702aeb1fSSage Weil 	if (err)
79a35eca95SGreg Farnum 		return err;
80a35eca95SGreg Farnum 
81702aeb1fSSage Weil 	memset(&nl, 0, sizeof(nl));
82a35eca95SGreg Farnum 	if (l.stripe_count)
83a35eca95SGreg Farnum 		nl.stripe_count = l.stripe_count;
84702aeb1fSSage Weil 	else
857627151eSYan, Zheng 		nl.stripe_count = ci->i_layout.stripe_count;
86a35eca95SGreg Farnum 	if (l.stripe_unit)
87a35eca95SGreg Farnum 		nl.stripe_unit = l.stripe_unit;
88702aeb1fSSage Weil 	else
897627151eSYan, Zheng 		nl.stripe_unit = ci->i_layout.stripe_unit;
90a35eca95SGreg Farnum 	if (l.object_size)
91a35eca95SGreg Farnum 		nl.object_size = l.object_size;
92702aeb1fSSage Weil 	else
937627151eSYan, Zheng 		nl.object_size = ci->i_layout.object_size;
94a35eca95SGreg Farnum 	if (l.data_pool)
95a35eca95SGreg Farnum 		nl.data_pool = l.data_pool;
96702aeb1fSSage Weil 	else
977627151eSYan, Zheng 		nl.data_pool = ci->i_layout.pool_id;
98702aeb1fSSage Weil 
99702aeb1fSSage Weil 	/* this is obsolete, and always -1 */
100702aeb1fSSage Weil 	nl.preferred_osd = le64_to_cpu(-1);
101a35eca95SGreg Farnum 
102e49bf4c5SSage Weil 	err = __validate_layout(mdsc, &nl);
1038f4e91deSSage Weil 	if (err)
1048f4e91deSSage Weil 		return err;
1058f4e91deSSage Weil 
1068f4e91deSSage Weil 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT,
1078f4e91deSSage Weil 				       USE_AUTH_MDS);
1088f4e91deSSage Weil 	if (IS_ERR(req))
1098f4e91deSSage Weil 		return PTR_ERR(req);
11070b666c3SSage Weil 	req->r_inode = inode;
11170b666c3SSage Weil 	ihold(inode);
1123bd58143SYan, Zheng 	req->r_num_caps = 1;
1133bd58143SYan, Zheng 
1148f4e91deSSage Weil 	req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL;
1158f4e91deSSage Weil 
1168f4e91deSSage Weil 	req->r_args.setlayout.layout.fl_stripe_unit =
1178f4e91deSSage Weil 		cpu_to_le32(l.stripe_unit);
1188f4e91deSSage Weil 	req->r_args.setlayout.layout.fl_stripe_count =
1198f4e91deSSage Weil 		cpu_to_le32(l.stripe_count);
1208f4e91deSSage Weil 	req->r_args.setlayout.layout.fl_object_size =
1218f4e91deSSage Weil 		cpu_to_le32(l.object_size);
1228f4e91deSSage Weil 	req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool);
1238f4e91deSSage Weil 
124752c8bdcSSage Weil 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1258f4e91deSSage Weil 	ceph_mdsc_put_request(req);
1268f4e91deSSage Weil 	return err;
1278f4e91deSSage Weil }
1288f4e91deSSage Weil 
1298f4e91deSSage Weil /*
130571dba52SGreg Farnum  * Set a layout policy on a directory inode. All items in the tree
131571dba52SGreg Farnum  * rooted at this inode will inherit this layout on creation,
132571dba52SGreg Farnum  * (It doesn't apply retroactively )
133571dba52SGreg Farnum  * unless a subdirectory has its own layout policy.
134571dba52SGreg Farnum  */
135571dba52SGreg Farnum static long ceph_ioctl_set_layout_policy (struct file *file, void __user *arg)
136571dba52SGreg Farnum {
137496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
138571dba52SGreg Farnum 	struct ceph_mds_request *req;
139571dba52SGreg Farnum 	struct ceph_ioctl_layout l;
140e49bf4c5SSage Weil 	int err;
141571dba52SGreg Farnum 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
142571dba52SGreg Farnum 
143571dba52SGreg Farnum 	/* copy and validate */
144571dba52SGreg Farnum 	if (copy_from_user(&l, arg, sizeof(l)))
145571dba52SGreg Farnum 		return -EFAULT;
146571dba52SGreg Farnum 
147e49bf4c5SSage Weil 	err = __validate_layout(mdsc, &l);
148571dba52SGreg Farnum 	if (err)
149571dba52SGreg Farnum 		return err;
150571dba52SGreg Farnum 
151571dba52SGreg Farnum 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETDIRLAYOUT,
152571dba52SGreg Farnum 				       USE_AUTH_MDS);
153571dba52SGreg Farnum 
154571dba52SGreg Farnum 	if (IS_ERR(req))
155571dba52SGreg Farnum 		return PTR_ERR(req);
15670b666c3SSage Weil 	req->r_inode = inode;
15770b666c3SSage Weil 	ihold(inode);
1583bd58143SYan, Zheng 	req->r_num_caps = 1;
159571dba52SGreg Farnum 
160571dba52SGreg Farnum 	req->r_args.setlayout.layout.fl_stripe_unit =
161571dba52SGreg Farnum 			cpu_to_le32(l.stripe_unit);
162571dba52SGreg Farnum 	req->r_args.setlayout.layout.fl_stripe_count =
163571dba52SGreg Farnum 			cpu_to_le32(l.stripe_count);
164571dba52SGreg Farnum 	req->r_args.setlayout.layout.fl_object_size =
165571dba52SGreg Farnum 			cpu_to_le32(l.object_size);
166571dba52SGreg Farnum 	req->r_args.setlayout.layout.fl_pg_pool =
167571dba52SGreg Farnum 			cpu_to_le32(l.data_pool);
168571dba52SGreg Farnum 
169571dba52SGreg Farnum 	err = ceph_mdsc_do_request(mdsc, inode, req);
170571dba52SGreg Farnum 	ceph_mdsc_put_request(req);
171571dba52SGreg Farnum 	return err;
172571dba52SGreg Farnum }
173571dba52SGreg Farnum 
174571dba52SGreg Farnum /*
1758f4e91deSSage Weil  * Return object name, size/offset information, and location (OSD
1768f4e91deSSage Weil  * number, network address) for a given file offset.
1778f4e91deSSage Weil  */
1788f4e91deSSage Weil static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg)
1798f4e91deSSage Weil {
1808f4e91deSSage Weil 	struct ceph_ioctl_dataloc dl;
181496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1828f4e91deSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1833d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
1843d14c5d2SYehuda Sadeh 		&ceph_sb_to_client(inode->i_sb)->client->osdc;
1857c13cb64SIlya Dryomov 	struct ceph_object_locator oloc;
186281dbe5dSIlya Dryomov 	CEPH_DEFINE_OID_ONSTACK(oid);
1878f4e91deSSage Weil 	u64 len = 1, olen;
1888f4e91deSSage Weil 	u64 tmp;
18951042122SSage Weil 	struct ceph_pg pgid;
190457712a0SSage Weil 	int r;
1918f4e91deSSage Weil 
1928f4e91deSSage Weil 	/* copy and validate */
1938f4e91deSSage Weil 	if (copy_from_user(&dl, arg, sizeof(dl)))
1948f4e91deSSage Weil 		return -EFAULT;
1958f4e91deSSage Weil 
1965aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
197e8afad65SAlex Elder 	r = ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, len,
198457712a0SSage Weil 					  &dl.object_no, &dl.object_offset,
199457712a0SSage Weil 					  &olen);
200494ddd11Smajianpeng 	if (r < 0) {
2015aea3dcdSIlya Dryomov 		up_read(&osdc->lock);
202457712a0SSage Weil 		return -EIO;
203494ddd11Smajianpeng 	}
2048f4e91deSSage Weil 	dl.file_offset -= dl.object_offset;
2057627151eSYan, Zheng 	dl.object_size = ci->i_layout.object_size;
2067627151eSYan, Zheng 	dl.block_size = ci->i_layout.stripe_unit;
2078f4e91deSSage Weil 
2088f4e91deSSage Weil 	/* block_offset = object_offset % block_size */
2098f4e91deSSage Weil 	tmp = dl.object_offset;
2108f4e91deSSage Weil 	dl.block_offset = do_div(tmp, dl.block_size);
2118f4e91deSSage Weil 
2128f4e91deSSage Weil 	snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx",
2138f4e91deSSage Weil 		 ceph_ino(inode), dl.object_no);
21441766f87SAlex Elder 
2157627151eSYan, Zheng 	oloc.pool = ci->i_layout.pool_id;
216*779fe0fbSYan, Zheng 	oloc.pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
217d30291b9SIlya Dryomov 	ceph_oid_printf(&oid, "%s", dl.object_name);
2187c13cb64SIlya Dryomov 
219d9591f5eSIlya Dryomov 	r = ceph_object_locator_to_pg(osdc->osdmap, &oid, &oloc, &pgid);
220*779fe0fbSYan, Zheng 
221*779fe0fbSYan, Zheng 	ceph_oloc_destroy(&oloc);
2222fbcbff1Smajianpeng 	if (r < 0) {
2235aea3dcdSIlya Dryomov 		up_read(&osdc->lock);
2242fbcbff1Smajianpeng 		return r;
2252fbcbff1Smajianpeng 	}
2268f4e91deSSage Weil 
227f81f1633SIlya Dryomov 	dl.osd = ceph_pg_to_acting_primary(osdc->osdmap, &pgid);
2288f4e91deSSage Weil 	if (dl.osd >= 0) {
2298f4e91deSSage Weil 		struct ceph_entity_addr *a =
2308f4e91deSSage Weil 			ceph_osd_addr(osdc->osdmap, dl.osd);
2318f4e91deSSage Weil 		if (a)
2328f4e91deSSage Weil 			memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr));
2338f4e91deSSage Weil 	} else {
2348f4e91deSSage Weil 		memset(&dl.osd_addr, 0, sizeof(dl.osd_addr));
2358f4e91deSSage Weil 	}
2365aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
2378f4e91deSSage Weil 
2388f4e91deSSage Weil 	/* send result back to user */
2398f4e91deSSage Weil 	if (copy_to_user(arg, &dl, sizeof(dl)))
2408f4e91deSSage Weil 		return -EFAULT;
2418f4e91deSSage Weil 
2428f4e91deSSage Weil 	return 0;
2438f4e91deSSage Weil }
2448f4e91deSSage Weil 
2458c6e9229SSage Weil static long ceph_ioctl_lazyio(struct file *file)
2468c6e9229SSage Weil {
2478c6e9229SSage Weil 	struct ceph_file_info *fi = file->private_data;
248496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
2498c6e9229SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2508c6e9229SSage Weil 
2518c6e9229SSage Weil 	if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) {
252be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
2538c6e9229SSage Weil 		ci->i_nr_by_mode[fi->fmode]--;
2548c6e9229SSage Weil 		fi->fmode |= CEPH_FILE_MODE_LAZY;
2558c6e9229SSage Weil 		ci->i_nr_by_mode[fi->fmode]++;
256be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
2578c6e9229SSage Weil 		dout("ioctl_layzio: file %p marked lazy\n", file);
2588c6e9229SSage Weil 
2598c6e9229SSage Weil 		ceph_check_caps(ci, 0, NULL);
2608c6e9229SSage Weil 	} else {
2618c6e9229SSage Weil 		dout("ioctl_layzio: file %p already lazy\n", file);
2628c6e9229SSage Weil 	}
2638c6e9229SSage Weil 	return 0;
2648c6e9229SSage Weil }
2658c6e9229SSage Weil 
2664918b6d1SSage Weil static long ceph_ioctl_syncio(struct file *file)
2674918b6d1SSage Weil {
2684918b6d1SSage Weil 	struct ceph_file_info *fi = file->private_data;
2694918b6d1SSage Weil 
2704918b6d1SSage Weil 	fi->flags |= CEPH_F_SYNC;
2714918b6d1SSage Weil 	return 0;
2724918b6d1SSage Weil }
2734918b6d1SSage Weil 
2748f4e91deSSage Weil long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2758f4e91deSSage Weil {
2768f4e91deSSage Weil 	dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg);
2778f4e91deSSage Weil 	switch (cmd) {
2788f4e91deSSage Weil 	case CEPH_IOC_GET_LAYOUT:
2798f4e91deSSage Weil 		return ceph_ioctl_get_layout(file, (void __user *)arg);
2808f4e91deSSage Weil 
2818f4e91deSSage Weil 	case CEPH_IOC_SET_LAYOUT:
2828f4e91deSSage Weil 		return ceph_ioctl_set_layout(file, (void __user *)arg);
2838f4e91deSSage Weil 
284571dba52SGreg Farnum 	case CEPH_IOC_SET_LAYOUT_POLICY:
285571dba52SGreg Farnum 		return ceph_ioctl_set_layout_policy(file, (void __user *)arg);
286571dba52SGreg Farnum 
2878f4e91deSSage Weil 	case CEPH_IOC_GET_DATALOC:
2888f4e91deSSage Weil 		return ceph_ioctl_get_dataloc(file, (void __user *)arg);
2898c6e9229SSage Weil 
2908c6e9229SSage Weil 	case CEPH_IOC_LAZYIO:
2918c6e9229SSage Weil 		return ceph_ioctl_lazyio(file);
2924918b6d1SSage Weil 
2934918b6d1SSage Weil 	case CEPH_IOC_SYNCIO:
2944918b6d1SSage Weil 		return ceph_ioctl_syncio(file);
2958f4e91deSSage Weil 	}
296571dba52SGreg Farnum 
2978f4e91deSSage Weil 	return -ENOTTY;
2988f4e91deSSage Weil }
299