1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 296c57adeSLinus Torvalds #include <linux/ceph/ceph_debug.h> 38f4e91deSSage Weil #include <linux/in.h> 48f4e91deSSage Weil 58f4e91deSSage Weil #include "super.h" 63d14c5d2SYehuda Sadeh #include "mds_client.h" 73d14c5d2SYehuda Sadeh #include "ioctl.h" 88f4e91deSSage Weil 98f4e91deSSage Weil 108f4e91deSSage Weil /* 118f4e91deSSage Weil * ioctls 128f4e91deSSage Weil */ 138f4e91deSSage Weil 148f4e91deSSage Weil /* 158f4e91deSSage Weil * get and set the file layout 168f4e91deSSage Weil */ 178f4e91deSSage Weil static long ceph_ioctl_get_layout(struct file *file, void __user *arg) 188f4e91deSSage Weil { 19496ad9aaSAl Viro struct ceph_inode_info *ci = ceph_inode(file_inode(file)); 208f4e91deSSage Weil struct ceph_ioctl_layout l; 218f4e91deSSage Weil int err; 228f4e91deSSage Weil 23508b32d8SYan, Zheng err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false); 248f4e91deSSage Weil if (!err) { 257627151eSYan, Zheng l.stripe_unit = ci->i_layout.stripe_unit; 267627151eSYan, Zheng l.stripe_count = ci->i_layout.stripe_count; 277627151eSYan, Zheng l.object_size = ci->i_layout.object_size; 287627151eSYan, Zheng l.data_pool = ci->i_layout.pool_id; 2924c149adSJeff Layton l.preferred_osd = -1; 308f4e91deSSage Weil if (copy_to_user(arg, &l, sizeof(l))) 318f4e91deSSage Weil return -EFAULT; 328f4e91deSSage Weil } 338f4e91deSSage Weil 348f4e91deSSage Weil return err; 358f4e91deSSage Weil } 368f4e91deSSage Weil 37e49bf4c5SSage Weil static long __validate_layout(struct ceph_mds_client *mdsc, 38e49bf4c5SSage Weil struct ceph_ioctl_layout *l) 39e49bf4c5SSage Weil { 40e49bf4c5SSage Weil int i, err; 41e49bf4c5SSage Weil 42e49bf4c5SSage Weil /* validate striping parameters */ 43e49bf4c5SSage Weil if ((l->object_size & ~PAGE_MASK) || 44e49bf4c5SSage Weil (l->stripe_unit & ~PAGE_MASK) || 450bc62284SYan, Zheng ((unsigned)l->stripe_unit != 0 && 4645f2e081SSage Weil ((unsigned)l->object_size % (unsigned)l->stripe_unit))) 47e49bf4c5SSage Weil return -EINVAL; 48e49bf4c5SSage Weil 49e49bf4c5SSage Weil /* make sure it's a valid data pool */ 50e49bf4c5SSage Weil mutex_lock(&mdsc->mutex); 51e49bf4c5SSage Weil err = -EINVAL; 52e49bf4c5SSage Weil for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++) 53e49bf4c5SSage Weil if (mdsc->mdsmap->m_data_pg_pools[i] == l->data_pool) { 54e49bf4c5SSage Weil err = 0; 55e49bf4c5SSage Weil break; 56e49bf4c5SSage Weil } 57e49bf4c5SSage Weil mutex_unlock(&mdsc->mutex); 58e49bf4c5SSage Weil if (err) 59e49bf4c5SSage Weil return err; 60e49bf4c5SSage Weil 61e49bf4c5SSage Weil return 0; 62e49bf4c5SSage Weil } 63e49bf4c5SSage Weil 648f4e91deSSage Weil static long ceph_ioctl_set_layout(struct file *file, void __user *arg) 658f4e91deSSage Weil { 66496ad9aaSAl Viro struct inode *inode = file_inode(file); 673d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 688f4e91deSSage Weil struct ceph_mds_request *req; 698f4e91deSSage Weil struct ceph_ioctl_layout l; 70496ad9aaSAl Viro struct ceph_inode_info *ci = ceph_inode(file_inode(file)); 71a35eca95SGreg Farnum struct ceph_ioctl_layout nl; 72e49bf4c5SSage Weil int err; 738f4e91deSSage Weil 748f4e91deSSage Weil if (copy_from_user(&l, arg, sizeof(l))) 758f4e91deSSage Weil return -EFAULT; 768f4e91deSSage Weil 77a35eca95SGreg Farnum /* validate changed params against current layout */ 78508b32d8SYan, Zheng err = ceph_do_getattr(file_inode(file), CEPH_STAT_CAP_LAYOUT, false); 79702aeb1fSSage Weil if (err) 80a35eca95SGreg Farnum return err; 81a35eca95SGreg Farnum 82702aeb1fSSage Weil memset(&nl, 0, sizeof(nl)); 83a35eca95SGreg Farnum if (l.stripe_count) 84a35eca95SGreg Farnum nl.stripe_count = l.stripe_count; 85702aeb1fSSage Weil else 867627151eSYan, Zheng nl.stripe_count = ci->i_layout.stripe_count; 87a35eca95SGreg Farnum if (l.stripe_unit) 88a35eca95SGreg Farnum nl.stripe_unit = l.stripe_unit; 89702aeb1fSSage Weil else 907627151eSYan, Zheng nl.stripe_unit = ci->i_layout.stripe_unit; 91a35eca95SGreg Farnum if (l.object_size) 92a35eca95SGreg Farnum nl.object_size = l.object_size; 93702aeb1fSSage Weil else 947627151eSYan, Zheng nl.object_size = ci->i_layout.object_size; 95a35eca95SGreg Farnum if (l.data_pool) 96a35eca95SGreg Farnum nl.data_pool = l.data_pool; 97702aeb1fSSage Weil else 987627151eSYan, Zheng nl.data_pool = ci->i_layout.pool_id; 99702aeb1fSSage Weil 100702aeb1fSSage Weil /* this is obsolete, and always -1 */ 10124c149adSJeff Layton nl.preferred_osd = -1; 102a35eca95SGreg Farnum 103e49bf4c5SSage Weil err = __validate_layout(mdsc, &nl); 1048f4e91deSSage Weil if (err) 1058f4e91deSSage Weil return err; 1068f4e91deSSage Weil 1078f4e91deSSage Weil req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT, 1088f4e91deSSage Weil USE_AUTH_MDS); 1098f4e91deSSage Weil if (IS_ERR(req)) 1108f4e91deSSage Weil return PTR_ERR(req); 11170b666c3SSage Weil req->r_inode = inode; 11270b666c3SSage Weil ihold(inode); 1133bd58143SYan, Zheng req->r_num_caps = 1; 1143bd58143SYan, Zheng 1158f4e91deSSage Weil req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL; 1168f4e91deSSage Weil 1178f4e91deSSage Weil req->r_args.setlayout.layout.fl_stripe_unit = 1188f4e91deSSage Weil cpu_to_le32(l.stripe_unit); 1198f4e91deSSage Weil req->r_args.setlayout.layout.fl_stripe_count = 1208f4e91deSSage Weil cpu_to_le32(l.stripe_count); 1218f4e91deSSage Weil req->r_args.setlayout.layout.fl_object_size = 1228f4e91deSSage Weil cpu_to_le32(l.object_size); 1238f4e91deSSage Weil req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool); 1248f4e91deSSage Weil 125752c8bdcSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 1268f4e91deSSage Weil ceph_mdsc_put_request(req); 1278f4e91deSSage Weil return err; 1288f4e91deSSage Weil } 1298f4e91deSSage Weil 1308f4e91deSSage Weil /* 131571dba52SGreg Farnum * Set a layout policy on a directory inode. All items in the tree 132571dba52SGreg Farnum * rooted at this inode will inherit this layout on creation, 133571dba52SGreg Farnum * (It doesn't apply retroactively ) 134571dba52SGreg Farnum * unless a subdirectory has its own layout policy. 135571dba52SGreg Farnum */ 136571dba52SGreg Farnum static long ceph_ioctl_set_layout_policy (struct file *file, void __user *arg) 137571dba52SGreg Farnum { 138496ad9aaSAl Viro struct inode *inode = file_inode(file); 139571dba52SGreg Farnum struct ceph_mds_request *req; 140571dba52SGreg Farnum struct ceph_ioctl_layout l; 141e49bf4c5SSage Weil int err; 142571dba52SGreg Farnum struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 143571dba52SGreg Farnum 144571dba52SGreg Farnum /* copy and validate */ 145571dba52SGreg Farnum if (copy_from_user(&l, arg, sizeof(l))) 146571dba52SGreg Farnum return -EFAULT; 147571dba52SGreg Farnum 148e49bf4c5SSage Weil err = __validate_layout(mdsc, &l); 149571dba52SGreg Farnum if (err) 150571dba52SGreg Farnum return err; 151571dba52SGreg Farnum 152571dba52SGreg Farnum req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETDIRLAYOUT, 153571dba52SGreg Farnum USE_AUTH_MDS); 154571dba52SGreg Farnum 155571dba52SGreg Farnum if (IS_ERR(req)) 156571dba52SGreg Farnum return PTR_ERR(req); 15770b666c3SSage Weil req->r_inode = inode; 15870b666c3SSage Weil ihold(inode); 1593bd58143SYan, Zheng req->r_num_caps = 1; 160571dba52SGreg Farnum 161571dba52SGreg Farnum req->r_args.setlayout.layout.fl_stripe_unit = 162571dba52SGreg Farnum cpu_to_le32(l.stripe_unit); 163571dba52SGreg Farnum req->r_args.setlayout.layout.fl_stripe_count = 164571dba52SGreg Farnum cpu_to_le32(l.stripe_count); 165571dba52SGreg Farnum req->r_args.setlayout.layout.fl_object_size = 166571dba52SGreg Farnum cpu_to_le32(l.object_size); 167571dba52SGreg Farnum req->r_args.setlayout.layout.fl_pg_pool = 168571dba52SGreg Farnum cpu_to_le32(l.data_pool); 169571dba52SGreg Farnum 170571dba52SGreg Farnum err = ceph_mdsc_do_request(mdsc, inode, req); 171571dba52SGreg Farnum ceph_mdsc_put_request(req); 172571dba52SGreg Farnum return err; 173571dba52SGreg Farnum } 174571dba52SGreg Farnum 175571dba52SGreg Farnum /* 1768f4e91deSSage Weil * Return object name, size/offset information, and location (OSD 1778f4e91deSSage Weil * number, network address) for a given file offset. 1788f4e91deSSage Weil */ 1798f4e91deSSage Weil static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg) 1808f4e91deSSage Weil { 1818f4e91deSSage Weil struct ceph_ioctl_dataloc dl; 182496ad9aaSAl Viro struct inode *inode = file_inode(file); 1838f4e91deSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1843d14c5d2SYehuda Sadeh struct ceph_osd_client *osdc = 1853d14c5d2SYehuda Sadeh &ceph_sb_to_client(inode->i_sb)->client->osdc; 1867c13cb64SIlya Dryomov struct ceph_object_locator oloc; 187281dbe5dSIlya Dryomov CEPH_DEFINE_OID_ONSTACK(oid); 188*dccbf080SIlya Dryomov u32 xlen; 1898f4e91deSSage Weil u64 tmp; 19051042122SSage Weil struct ceph_pg pgid; 191457712a0SSage Weil int r; 1928f4e91deSSage Weil 1938f4e91deSSage Weil /* copy and validate */ 1948f4e91deSSage Weil if (copy_from_user(&dl, arg, sizeof(dl))) 1958f4e91deSSage Weil return -EFAULT; 1968f4e91deSSage Weil 1975aea3dcdSIlya Dryomov down_read(&osdc->lock); 198*dccbf080SIlya Dryomov ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, 1, 199*dccbf080SIlya Dryomov &dl.object_no, &dl.object_offset, &xlen); 2008f4e91deSSage Weil dl.file_offset -= dl.object_offset; 2017627151eSYan, Zheng dl.object_size = ci->i_layout.object_size; 2027627151eSYan, Zheng dl.block_size = ci->i_layout.stripe_unit; 2038f4e91deSSage Weil 2048f4e91deSSage Weil /* block_offset = object_offset % block_size */ 2058f4e91deSSage Weil tmp = dl.object_offset; 2068f4e91deSSage Weil dl.block_offset = do_div(tmp, dl.block_size); 2078f4e91deSSage Weil 2088f4e91deSSage Weil snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx", 2098f4e91deSSage Weil ceph_ino(inode), dl.object_no); 21041766f87SAlex Elder 2117627151eSYan, Zheng oloc.pool = ci->i_layout.pool_id; 212779fe0fbSYan, Zheng oloc.pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 213d30291b9SIlya Dryomov ceph_oid_printf(&oid, "%s", dl.object_name); 2147c13cb64SIlya Dryomov 215d9591f5eSIlya Dryomov r = ceph_object_locator_to_pg(osdc->osdmap, &oid, &oloc, &pgid); 216779fe0fbSYan, Zheng 217779fe0fbSYan, Zheng ceph_oloc_destroy(&oloc); 2182fbcbff1Smajianpeng if (r < 0) { 2195aea3dcdSIlya Dryomov up_read(&osdc->lock); 2202fbcbff1Smajianpeng return r; 2212fbcbff1Smajianpeng } 2228f4e91deSSage Weil 223f81f1633SIlya Dryomov dl.osd = ceph_pg_to_acting_primary(osdc->osdmap, &pgid); 2248f4e91deSSage Weil if (dl.osd >= 0) { 2258f4e91deSSage Weil struct ceph_entity_addr *a = 2268f4e91deSSage Weil ceph_osd_addr(osdc->osdmap, dl.osd); 2278f4e91deSSage Weil if (a) 2288f4e91deSSage Weil memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr)); 2298f4e91deSSage Weil } else { 2308f4e91deSSage Weil memset(&dl.osd_addr, 0, sizeof(dl.osd_addr)); 2318f4e91deSSage Weil } 2325aea3dcdSIlya Dryomov up_read(&osdc->lock); 2338f4e91deSSage Weil 2348f4e91deSSage Weil /* send result back to user */ 2358f4e91deSSage Weil if (copy_to_user(arg, &dl, sizeof(dl))) 2368f4e91deSSage Weil return -EFAULT; 2378f4e91deSSage Weil 2388f4e91deSSage Weil return 0; 2398f4e91deSSage Weil } 2408f4e91deSSage Weil 2418c6e9229SSage Weil static long ceph_ioctl_lazyio(struct file *file) 2428c6e9229SSage Weil { 2438c6e9229SSage Weil struct ceph_file_info *fi = file->private_data; 244496ad9aaSAl Viro struct inode *inode = file_inode(file); 2458c6e9229SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2468c6e9229SSage Weil 2478c6e9229SSage Weil if ((fi->fmode & CEPH_FILE_MODE_LAZY) == 0) { 248be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2498c6e9229SSage Weil fi->fmode |= CEPH_FILE_MODE_LAZY; 250774a6a11SYan, Zheng ci->i_nr_by_mode[ffs(CEPH_FILE_MODE_LAZY)]++; 251be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2528c6e9229SSage Weil dout("ioctl_layzio: file %p marked lazy\n", file); 2538c6e9229SSage Weil 2548c6e9229SSage Weil ceph_check_caps(ci, 0, NULL); 2558c6e9229SSage Weil } else { 2568c6e9229SSage Weil dout("ioctl_layzio: file %p already lazy\n", file); 2578c6e9229SSage Weil } 2588c6e9229SSage Weil return 0; 2598c6e9229SSage Weil } 2608c6e9229SSage Weil 2614918b6d1SSage Weil static long ceph_ioctl_syncio(struct file *file) 2624918b6d1SSage Weil { 2634918b6d1SSage Weil struct ceph_file_info *fi = file->private_data; 2644918b6d1SSage Weil 2654918b6d1SSage Weil fi->flags |= CEPH_F_SYNC; 2664918b6d1SSage Weil return 0; 2674918b6d1SSage Weil } 2684918b6d1SSage Weil 2698f4e91deSSage Weil long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2708f4e91deSSage Weil { 2718f4e91deSSage Weil dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg); 2728f4e91deSSage Weil switch (cmd) { 2738f4e91deSSage Weil case CEPH_IOC_GET_LAYOUT: 2748f4e91deSSage Weil return ceph_ioctl_get_layout(file, (void __user *)arg); 2758f4e91deSSage Weil 2768f4e91deSSage Weil case CEPH_IOC_SET_LAYOUT: 2778f4e91deSSage Weil return ceph_ioctl_set_layout(file, (void __user *)arg); 2788f4e91deSSage Weil 279571dba52SGreg Farnum case CEPH_IOC_SET_LAYOUT_POLICY: 280571dba52SGreg Farnum return ceph_ioctl_set_layout_policy(file, (void __user *)arg); 281571dba52SGreg Farnum 2828f4e91deSSage Weil case CEPH_IOC_GET_DATALOC: 2838f4e91deSSage Weil return ceph_ioctl_get_dataloc(file, (void __user *)arg); 2848c6e9229SSage Weil 2858c6e9229SSage Weil case CEPH_IOC_LAZYIO: 2868c6e9229SSage Weil return ceph_ioctl_lazyio(file); 2874918b6d1SSage Weil 2884918b6d1SSage Weil case CEPH_IOC_SYNCIO: 2894918b6d1SSage Weil return ceph_ioctl_syncio(file); 2908f4e91deSSage Weil } 291571dba52SGreg Farnum 2928f4e91deSSage Weil return -ENOTTY; 2938f4e91deSSage Weil } 294