1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 325e6bae3SYan, Zheng #include <linux/ceph/pagelist.h> 43d14c5d2SYehuda Sadeh 5355da1ebSSage Weil #include "super.h" 63d14c5d2SYehuda Sadeh #include "mds_client.h" 73d14c5d2SYehuda Sadeh 83d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 9355da1ebSSage Weil 10355da1ebSSage Weil #include <linux/xattr.h> 11ac6713ccSYan, Zheng #include <linux/security.h> 124db658eaSLinus Torvalds #include <linux/posix_acl_xattr.h> 135a0e3ad6STejun Heo #include <linux/slab.h> 14355da1ebSSage Weil 1522891907SAlex Elder #define XATTR_CEPH_PREFIX "ceph." 1622891907SAlex Elder #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1) 1722891907SAlex Elder 18bcdfeb2eSYan, Zheng static int __remove_xattr(struct ceph_inode_info *ci, 19bcdfeb2eSYan, Zheng struct ceph_inode_xattr *xattr); 20bcdfeb2eSYan, Zheng 21355da1ebSSage Weil static bool ceph_is_valid_xattr(const char *name) 22355da1ebSSage Weil { 2322891907SAlex Elder return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || 24355da1ebSSage Weil !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || 25355da1ebSSage Weil !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); 26355da1ebSSage Weil } 27355da1ebSSage Weil 28355da1ebSSage Weil /* 29355da1ebSSage Weil * These define virtual xattrs exposing the recursive directory 30355da1ebSSage Weil * statistics and layout metadata. 31355da1ebSSage Weil */ 32881a5fa2SAlex Elder struct ceph_vxattr { 33355da1ebSSage Weil char *name; 343ce6cd12SAlex Elder size_t name_size; /* strlen(name) + 1 (for '\0') */ 35*f1d1b51dSJeff Layton ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, 36355da1ebSSage Weil size_t size); 37f36e4472SSage Weil bool (*exists_cb)(struct ceph_inode_info *ci); 384e9906e7SYan, Zheng unsigned int flags; 39355da1ebSSage Weil }; 40355da1ebSSage Weil 414e9906e7SYan, Zheng #define VXATTR_FLAG_READONLY (1<<0) 424e9906e7SYan, Zheng #define VXATTR_FLAG_HIDDEN (1<<1) 4349a9f4f6SYan, Zheng #define VXATTR_FLAG_RSTAT (1<<2) 444e9906e7SYan, Zheng 4532ab0bd7SSage Weil /* layouts */ 4632ab0bd7SSage Weil 4732ab0bd7SSage Weil static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci) 4832ab0bd7SSage Weil { 49779fe0fbSYan, Zheng struct ceph_file_layout *fl = &ci->i_layout; 50779fe0fbSYan, Zheng return (fl->stripe_unit > 0 || fl->stripe_count > 0 || 51779fe0fbSYan, Zheng fl->object_size > 0 || fl->pool_id >= 0 || 52779fe0fbSYan, Zheng rcu_dereference_raw(fl->pool_ns) != NULL); 5332ab0bd7SSage Weil } 5432ab0bd7SSage Weil 55*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, 5632ab0bd7SSage Weil size_t size) 5732ab0bd7SSage Weil { 5832ab0bd7SSage Weil struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 5932ab0bd7SSage Weil struct ceph_osd_client *osdc = &fsc->client->osdc; 60779fe0fbSYan, Zheng struct ceph_string *pool_ns; 617627151eSYan, Zheng s64 pool = ci->i_layout.pool_id; 6232ab0bd7SSage Weil const char *pool_name; 63779fe0fbSYan, Zheng const char *ns_field = " pool_namespace="; 641e5c6649SYan, Zheng char buf[128]; 65779fe0fbSYan, Zheng size_t len, total_len = 0; 66779fe0fbSYan, Zheng int ret; 67779fe0fbSYan, Zheng 68779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 6932ab0bd7SSage Weil 7032ab0bd7SSage Weil dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode); 715aea3dcdSIlya Dryomov down_read(&osdc->lock); 7232ab0bd7SSage Weil pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); 731e5c6649SYan, Zheng if (pool_name) { 74779fe0fbSYan, Zheng len = snprintf(buf, sizeof(buf), 757627151eSYan, Zheng "stripe_unit=%u stripe_count=%u object_size=%u pool=", 767627151eSYan, Zheng ci->i_layout.stripe_unit, ci->i_layout.stripe_count, 777627151eSYan, Zheng ci->i_layout.object_size); 78779fe0fbSYan, Zheng total_len = len + strlen(pool_name); 791e5c6649SYan, Zheng } else { 80779fe0fbSYan, Zheng len = snprintf(buf, sizeof(buf), 817627151eSYan, Zheng "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld", 827627151eSYan, Zheng ci->i_layout.stripe_unit, ci->i_layout.stripe_count, 83*f1d1b51dSJeff Layton ci->i_layout.object_size, pool); 84779fe0fbSYan, Zheng total_len = len; 85779fe0fbSYan, Zheng } 86779fe0fbSYan, Zheng 87779fe0fbSYan, Zheng if (pool_ns) 88779fe0fbSYan, Zheng total_len += strlen(ns_field) + pool_ns->len; 89779fe0fbSYan, Zheng 90779fe0fbSYan, Zheng if (!size) { 91779fe0fbSYan, Zheng ret = total_len; 92779fe0fbSYan, Zheng } else if (total_len > size) { 931e5c6649SYan, Zheng ret = -ERANGE; 94779fe0fbSYan, Zheng } else { 95779fe0fbSYan, Zheng memcpy(val, buf, len); 96779fe0fbSYan, Zheng ret = len; 97779fe0fbSYan, Zheng if (pool_name) { 98779fe0fbSYan, Zheng len = strlen(pool_name); 99779fe0fbSYan, Zheng memcpy(val + ret, pool_name, len); 100779fe0fbSYan, Zheng ret += len; 101779fe0fbSYan, Zheng } 102779fe0fbSYan, Zheng if (pool_ns) { 103779fe0fbSYan, Zheng len = strlen(ns_field); 104779fe0fbSYan, Zheng memcpy(val + ret, ns_field, len); 105779fe0fbSYan, Zheng ret += len; 106779fe0fbSYan, Zheng memcpy(val + ret, pool_ns->str, pool_ns->len); 107779fe0fbSYan, Zheng ret += pool_ns->len; 1081e5c6649SYan, Zheng } 1091e5c6649SYan, Zheng } 1105aea3dcdSIlya Dryomov up_read(&osdc->lock); 111779fe0fbSYan, Zheng ceph_put_string(pool_ns); 11232ab0bd7SSage Weil return ret; 11332ab0bd7SSage Weil } 11432ab0bd7SSage Weil 115*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci, 116695b7119SSage Weil char *val, size_t size) 117695b7119SSage Weil { 1187627151eSYan, Zheng return snprintf(val, size, "%u", ci->i_layout.stripe_unit); 119695b7119SSage Weil } 120695b7119SSage Weil 121*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci, 122695b7119SSage Weil char *val, size_t size) 123695b7119SSage Weil { 1247627151eSYan, Zheng return snprintf(val, size, "%u", ci->i_layout.stripe_count); 125695b7119SSage Weil } 126695b7119SSage Weil 127*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci, 128695b7119SSage Weil char *val, size_t size) 129695b7119SSage Weil { 1307627151eSYan, Zheng return snprintf(val, size, "%u", ci->i_layout.object_size); 131695b7119SSage Weil } 132695b7119SSage Weil 133*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci, 134695b7119SSage Weil char *val, size_t size) 135695b7119SSage Weil { 136*f1d1b51dSJeff Layton ssize_t ret; 137695b7119SSage Weil struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 138695b7119SSage Weil struct ceph_osd_client *osdc = &fsc->client->osdc; 1397627151eSYan, Zheng s64 pool = ci->i_layout.pool_id; 140695b7119SSage Weil const char *pool_name; 141695b7119SSage Weil 1425aea3dcdSIlya Dryomov down_read(&osdc->lock); 143695b7119SSage Weil pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); 144695b7119SSage Weil if (pool_name) 145695b7119SSage Weil ret = snprintf(val, size, "%s", pool_name); 146695b7119SSage Weil else 147*f1d1b51dSJeff Layton ret = snprintf(val, size, "%lld", pool); 1485aea3dcdSIlya Dryomov up_read(&osdc->lock); 149695b7119SSage Weil return ret; 150695b7119SSage Weil } 151695b7119SSage Weil 152*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci, 153779fe0fbSYan, Zheng char *val, size_t size) 154779fe0fbSYan, Zheng { 155779fe0fbSYan, Zheng int ret = 0; 156779fe0fbSYan, Zheng struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns); 157779fe0fbSYan, Zheng if (ns) { 158*f1d1b51dSJeff Layton ret = snprintf(val, size, "%.*s", ns->len, ns->str); 159779fe0fbSYan, Zheng ceph_put_string(ns); 160779fe0fbSYan, Zheng } 161779fe0fbSYan, Zheng return ret; 162779fe0fbSYan, Zheng } 163779fe0fbSYan, Zheng 164355da1ebSSage Weil /* directories */ 165355da1ebSSage Weil 166*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, 167355da1ebSSage Weil size_t size) 168355da1ebSSage Weil { 169355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs); 170355da1ebSSage Weil } 171355da1ebSSage Weil 172*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, 173355da1ebSSage Weil size_t size) 174355da1ebSSage Weil { 175355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_files); 176355da1ebSSage Weil } 177355da1ebSSage Weil 178*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, 179355da1ebSSage Weil size_t size) 180355da1ebSSage Weil { 181355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_subdirs); 182355da1ebSSage Weil } 183355da1ebSSage Weil 184*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, 185355da1ebSSage Weil size_t size) 186355da1ebSSage Weil { 187355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs); 188355da1ebSSage Weil } 189355da1ebSSage Weil 190*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, 191355da1ebSSage Weil size_t size) 192355da1ebSSage Weil { 193355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_rfiles); 194355da1ebSSage Weil } 195355da1ebSSage Weil 196*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, 197355da1ebSSage Weil size_t size) 198355da1ebSSage Weil { 199355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_rsubdirs); 200355da1ebSSage Weil } 201355da1ebSSage Weil 202*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, 203355da1ebSSage Weil size_t size) 204355da1ebSSage Weil { 205355da1ebSSage Weil return snprintf(val, size, "%lld", ci->i_rbytes); 206355da1ebSSage Weil } 207355da1ebSSage Weil 208*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, 209355da1ebSSage Weil size_t size) 210355da1ebSSage Weil { 21171880728SDavid Disseldorp return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec, 2129bbeab41SArnd Bergmann ci->i_rctime.tv_nsec); 213355da1ebSSage Weil } 214355da1ebSSage Weil 21508796873SYan, Zheng /* dir pin */ 21608796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci) 21708796873SYan, Zheng { 21808796873SYan, Zheng return ci->i_dir_pin != -ENODATA; 21908796873SYan, Zheng } 220fb18a575SLuis Henriques 221*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val, 22208796873SYan, Zheng size_t size) 22308796873SYan, Zheng { 22408796873SYan, Zheng return snprintf(val, size, "%d", (int)ci->i_dir_pin); 22508796873SYan, Zheng } 22608796873SYan, Zheng 22708796873SYan, Zheng /* quotas */ 228fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci) 229fb18a575SLuis Henriques { 230f1919826SYan, Zheng bool ret = false; 231f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 232f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 233f1919826SYan, Zheng ci->i_vino.snap == CEPH_NOSNAP && 234f1919826SYan, Zheng ci->i_snap_realm && 235f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino) 236f1919826SYan, Zheng ret = true; 237f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 238f1919826SYan, Zheng return ret; 239fb18a575SLuis Henriques } 240fb18a575SLuis Henriques 241*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val, 242fb18a575SLuis Henriques size_t size) 243fb18a575SLuis Henriques { 244fb18a575SLuis Henriques return snprintf(val, size, "max_bytes=%llu max_files=%llu", 245fb18a575SLuis Henriques ci->i_max_bytes, ci->i_max_files); 246fb18a575SLuis Henriques } 247fb18a575SLuis Henriques 248*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci, 249fb18a575SLuis Henriques char *val, size_t size) 250fb18a575SLuis Henriques { 251fb18a575SLuis Henriques return snprintf(val, size, "%llu", ci->i_max_bytes); 252fb18a575SLuis Henriques } 253fb18a575SLuis Henriques 254*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci, 255fb18a575SLuis Henriques char *val, size_t size) 256fb18a575SLuis Henriques { 257fb18a575SLuis Henriques return snprintf(val, size, "%llu", ci->i_max_files); 258fb18a575SLuis Henriques } 25932ab0bd7SSage Weil 260100cc610SDavid Disseldorp /* snapshots */ 261100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci) 262100cc610SDavid Disseldorp { 263100cc610SDavid Disseldorp return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0); 264100cc610SDavid Disseldorp } 265100cc610SDavid Disseldorp 266*f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val, 267100cc610SDavid Disseldorp size_t size) 268100cc610SDavid Disseldorp { 269100cc610SDavid Disseldorp return snprintf(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec, 270100cc610SDavid Disseldorp ci->i_snap_btime.tv_nsec); 271100cc610SDavid Disseldorp } 272100cc610SDavid Disseldorp 273eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name 274695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2) \ 275695b7119SSage Weil XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 276eb788084SAlex Elder 27749a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags) \ 278eb788084SAlex Elder { \ 279eb788084SAlex Elder .name = CEPH_XATTR_NAME(_type, _name), \ 2803ce6cd12SAlex Elder .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ 281aa4066edSAlex Elder .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 282f36e4472SSage Weil .exists_cb = NULL, \ 28349a9f4f6SYan, Zheng .flags = (VXATTR_FLAG_READONLY | _flags), \ 284eb788084SAlex Elder } 28549a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name) \ 28649a9f4f6SYan, Zheng XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT) 287695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ 288695b7119SSage Weil { \ 289695b7119SSage Weil .name = CEPH_XATTR_NAME2(_type, _name, _field), \ 290695b7119SSage Weil .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ 291695b7119SSage Weil .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ 292695b7119SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, \ 2934e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 294695b7119SSage Weil } 295fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name) \ 296fb18a575SLuis Henriques { \ 297fb18a575SLuis Henriques .name = CEPH_XATTR_NAME(_type, _name), \ 298fb18a575SLuis Henriques .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \ 299fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 300fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, \ 3014e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 302fb18a575SLuis Henriques } 303eb788084SAlex Elder 304881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = { 3051f08f2b0SSage Weil { 3061f08f2b0SSage Weil .name = "ceph.dir.layout", 3071f08f2b0SSage Weil .name_size = sizeof("ceph.dir.layout"), 3081f08f2b0SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 3091f08f2b0SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 3104e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 3111f08f2b0SSage Weil }, 312695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), 313695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_count), 314695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, object_size), 315695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, pool), 316779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(dir, layout, pool_namespace), 31749a9f4f6SYan, Zheng XATTR_NAME_CEPH(dir, entries, 0), 31849a9f4f6SYan, Zheng XATTR_NAME_CEPH(dir, files, 0), 31949a9f4f6SYan, Zheng XATTR_NAME_CEPH(dir, subdirs, 0), 32049a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rentries), 32149a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rfiles), 32249a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rsubdirs), 32349a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rbytes), 32449a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rctime), 325fb18a575SLuis Henriques { 32608796873SYan, Zheng .name = "ceph.dir.pin", 327e1b81439SDavid Disseldorp .name_size = sizeof("ceph.dir.pin"), 32808796873SYan, Zheng .getxattr_cb = ceph_vxattrcb_dir_pin, 32908796873SYan, Zheng .exists_cb = ceph_vxattrcb_dir_pin_exists, 33008796873SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 33108796873SYan, Zheng }, 33208796873SYan, Zheng { 333fb18a575SLuis Henriques .name = "ceph.quota", 334fb18a575SLuis Henriques .name_size = sizeof("ceph.quota"), 335fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_quota, 336fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, 3374e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 338fb18a575SLuis Henriques }, 339fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_bytes), 340fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_files), 341100cc610SDavid Disseldorp { 342100cc610SDavid Disseldorp .name = "ceph.snap.btime", 343100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 344100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 345100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 346100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 347100cc610SDavid Disseldorp }, 3482c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 349355da1ebSSage Weil }; 350355da1ebSSage Weil 351355da1ebSSage Weil /* files */ 352355da1ebSSage Weil 353881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = { 35432ab0bd7SSage Weil { 35532ab0bd7SSage Weil .name = "ceph.file.layout", 35632ab0bd7SSage Weil .name_size = sizeof("ceph.file.layout"), 35732ab0bd7SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 35832ab0bd7SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 3594e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 36032ab0bd7SSage Weil }, 361695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_unit), 362695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_count), 363695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, object_size), 364695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, pool), 365779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(file, layout, pool_namespace), 366100cc610SDavid Disseldorp { 367100cc610SDavid Disseldorp .name = "ceph.snap.btime", 368100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 369100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 370100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 371100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 372100cc610SDavid Disseldorp }, 3732c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 374355da1ebSSage Weil }; 375355da1ebSSage Weil 376881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) 377355da1ebSSage Weil { 378355da1ebSSage Weil if (S_ISDIR(inode->i_mode)) 379355da1ebSSage Weil return ceph_dir_vxattrs; 380355da1ebSSage Weil else if (S_ISREG(inode->i_mode)) 381355da1ebSSage Weil return ceph_file_vxattrs; 382355da1ebSSage Weil return NULL; 383355da1ebSSage Weil } 384355da1ebSSage Weil 385881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, 386355da1ebSSage Weil const char *name) 387355da1ebSSage Weil { 388881a5fa2SAlex Elder struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); 38906476a69SAlex Elder 39006476a69SAlex Elder if (vxattr) { 39106476a69SAlex Elder while (vxattr->name) { 39206476a69SAlex Elder if (!strcmp(vxattr->name, name)) 393355da1ebSSage Weil return vxattr; 394355da1ebSSage Weil vxattr++; 39506476a69SAlex Elder } 39606476a69SAlex Elder } 39706476a69SAlex Elder 398355da1ebSSage Weil return NULL; 399355da1ebSSage Weil } 400355da1ebSSage Weil 401355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci, 402355da1ebSSage Weil const char *name, int name_len, 403355da1ebSSage Weil const char *val, int val_len, 404fbc0b970SYan, Zheng int flags, int update_xattr, 405355da1ebSSage Weil struct ceph_inode_xattr **newxattr) 406355da1ebSSage Weil { 407355da1ebSSage Weil struct rb_node **p; 408355da1ebSSage Weil struct rb_node *parent = NULL; 409355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 410355da1ebSSage Weil int c; 411355da1ebSSage Weil int new = 0; 412355da1ebSSage Weil 413355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 414355da1ebSSage Weil while (*p) { 415355da1ebSSage Weil parent = *p; 416355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 417355da1ebSSage Weil c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); 418355da1ebSSage Weil if (c < 0) 419355da1ebSSage Weil p = &(*p)->rb_left; 420355da1ebSSage Weil else if (c > 0) 421355da1ebSSage Weil p = &(*p)->rb_right; 422355da1ebSSage Weil else { 423355da1ebSSage Weil if (name_len == xattr->name_len) 424355da1ebSSage Weil break; 425355da1ebSSage Weil else if (name_len < xattr->name_len) 426355da1ebSSage Weil p = &(*p)->rb_left; 427355da1ebSSage Weil else 428355da1ebSSage Weil p = &(*p)->rb_right; 429355da1ebSSage Weil } 430355da1ebSSage Weil xattr = NULL; 431355da1ebSSage Weil } 432355da1ebSSage Weil 433fbc0b970SYan, Zheng if (update_xattr) { 434fbc0b970SYan, Zheng int err = 0; 435eeca958dSLuis Henriques 436fbc0b970SYan, Zheng if (xattr && (flags & XATTR_CREATE)) 437fbc0b970SYan, Zheng err = -EEXIST; 438fbc0b970SYan, Zheng else if (!xattr && (flags & XATTR_REPLACE)) 439fbc0b970SYan, Zheng err = -ENODATA; 440fbc0b970SYan, Zheng if (err) { 441fbc0b970SYan, Zheng kfree(name); 442fbc0b970SYan, Zheng kfree(val); 443eeca958dSLuis Henriques kfree(*newxattr); 444fbc0b970SYan, Zheng return err; 445fbc0b970SYan, Zheng } 446bcdfeb2eSYan, Zheng if (update_xattr < 0) { 447bcdfeb2eSYan, Zheng if (xattr) 448bcdfeb2eSYan, Zheng __remove_xattr(ci, xattr); 449bcdfeb2eSYan, Zheng kfree(name); 450eeca958dSLuis Henriques kfree(*newxattr); 451bcdfeb2eSYan, Zheng return 0; 452bcdfeb2eSYan, Zheng } 453fbc0b970SYan, Zheng } 454fbc0b970SYan, Zheng 455355da1ebSSage Weil if (!xattr) { 456355da1ebSSage Weil new = 1; 457355da1ebSSage Weil xattr = *newxattr; 458355da1ebSSage Weil xattr->name = name; 459355da1ebSSage Weil xattr->name_len = name_len; 460fbc0b970SYan, Zheng xattr->should_free_name = update_xattr; 461355da1ebSSage Weil 462355da1ebSSage Weil ci->i_xattrs.count++; 463355da1ebSSage Weil dout("__set_xattr count=%d\n", ci->i_xattrs.count); 464355da1ebSSage Weil } else { 465355da1ebSSage Weil kfree(*newxattr); 466355da1ebSSage Weil *newxattr = NULL; 467355da1ebSSage Weil if (xattr->should_free_val) 468355da1ebSSage Weil kfree((void *)xattr->val); 469355da1ebSSage Weil 470fbc0b970SYan, Zheng if (update_xattr) { 471355da1ebSSage Weil kfree((void *)name); 472355da1ebSSage Weil name = xattr->name; 473355da1ebSSage Weil } 474355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 475355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 476355da1ebSSage Weil } 477355da1ebSSage Weil ci->i_xattrs.names_size += name_len; 478355da1ebSSage Weil ci->i_xattrs.vals_size += val_len; 479355da1ebSSage Weil if (val) 480355da1ebSSage Weil xattr->val = val; 481355da1ebSSage Weil else 482355da1ebSSage Weil xattr->val = ""; 483355da1ebSSage Weil 484355da1ebSSage Weil xattr->val_len = val_len; 485fbc0b970SYan, Zheng xattr->dirty = update_xattr; 486fbc0b970SYan, Zheng xattr->should_free_val = (val && update_xattr); 487355da1ebSSage Weil 488355da1ebSSage Weil if (new) { 489355da1ebSSage Weil rb_link_node(&xattr->node, parent, p); 490355da1ebSSage Weil rb_insert_color(&xattr->node, &ci->i_xattrs.index); 491355da1ebSSage Weil dout("__set_xattr_val p=%p\n", p); 492355da1ebSSage Weil } 493355da1ebSSage Weil 49405729781SYan, Zheng dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n", 49505729781SYan, Zheng ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val); 496355da1ebSSage Weil 497355da1ebSSage Weil return 0; 498355da1ebSSage Weil } 499355da1ebSSage Weil 500355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, 501355da1ebSSage Weil const char *name) 502355da1ebSSage Weil { 503355da1ebSSage Weil struct rb_node **p; 504355da1ebSSage Weil struct rb_node *parent = NULL; 505355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 50617db143fSSage Weil int name_len = strlen(name); 507355da1ebSSage Weil int c; 508355da1ebSSage Weil 509355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 510355da1ebSSage Weil while (*p) { 511355da1ebSSage Weil parent = *p; 512355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 513355da1ebSSage Weil c = strncmp(name, xattr->name, xattr->name_len); 51417db143fSSage Weil if (c == 0 && name_len > xattr->name_len) 51517db143fSSage Weil c = 1; 516355da1ebSSage Weil if (c < 0) 517355da1ebSSage Weil p = &(*p)->rb_left; 518355da1ebSSage Weil else if (c > 0) 519355da1ebSSage Weil p = &(*p)->rb_right; 520355da1ebSSage Weil else { 521355da1ebSSage Weil dout("__get_xattr %s: found %.*s\n", name, 522355da1ebSSage Weil xattr->val_len, xattr->val); 523355da1ebSSage Weil return xattr; 524355da1ebSSage Weil } 525355da1ebSSage Weil } 526355da1ebSSage Weil 527355da1ebSSage Weil dout("__get_xattr %s: not found\n", name); 528355da1ebSSage Weil 529355da1ebSSage Weil return NULL; 530355da1ebSSage Weil } 531355da1ebSSage Weil 532355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr) 533355da1ebSSage Weil { 534355da1ebSSage Weil BUG_ON(!xattr); 535355da1ebSSage Weil 536355da1ebSSage Weil if (xattr->should_free_name) 537355da1ebSSage Weil kfree((void *)xattr->name); 538355da1ebSSage Weil if (xattr->should_free_val) 539355da1ebSSage Weil kfree((void *)xattr->val); 540355da1ebSSage Weil 541355da1ebSSage Weil kfree(xattr); 542355da1ebSSage Weil } 543355da1ebSSage Weil 544355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci, 545355da1ebSSage Weil struct ceph_inode_xattr *xattr) 546355da1ebSSage Weil { 547355da1ebSSage Weil if (!xattr) 548524186acSYan, Zheng return -ENODATA; 549355da1ebSSage Weil 550355da1ebSSage Weil rb_erase(&xattr->node, &ci->i_xattrs.index); 551355da1ebSSage Weil 552355da1ebSSage Weil if (xattr->should_free_name) 553355da1ebSSage Weil kfree((void *)xattr->name); 554355da1ebSSage Weil if (xattr->should_free_val) 555355da1ebSSage Weil kfree((void *)xattr->val); 556355da1ebSSage Weil 557355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 558355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 559355da1ebSSage Weil ci->i_xattrs.count--; 560355da1ebSSage Weil kfree(xattr); 561355da1ebSSage Weil 562355da1ebSSage Weil return 0; 563355da1ebSSage Weil } 564355da1ebSSage Weil 565355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci, 566355da1ebSSage Weil char *dest) 567355da1ebSSage Weil { 568355da1ebSSage Weil struct rb_node *p; 569355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 570355da1ebSSage Weil 571355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 572355da1ebSSage Weil dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); 573355da1ebSSage Weil 574355da1ebSSage Weil while (p) { 575355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 576355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 577355da1ebSSage Weil dest[xattr->name_len] = '\0'; 578355da1ebSSage Weil 579355da1ebSSage Weil dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, 580355da1ebSSage Weil xattr->name_len, ci->i_xattrs.names_size); 581355da1ebSSage Weil 582355da1ebSSage Weil dest += xattr->name_len + 1; 583355da1ebSSage Weil p = rb_next(p); 584355da1ebSSage Weil } 585355da1ebSSage Weil 586355da1ebSSage Weil return dest; 587355da1ebSSage Weil } 588355da1ebSSage Weil 589355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci) 590355da1ebSSage Weil { 591355da1ebSSage Weil struct rb_node *p, *tmp; 592355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 593355da1ebSSage Weil 594355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 595355da1ebSSage Weil 596355da1ebSSage Weil dout("__ceph_destroy_xattrs p=%p\n", p); 597355da1ebSSage Weil 598355da1ebSSage Weil while (p) { 599355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 600355da1ebSSage Weil tmp = p; 601355da1ebSSage Weil p = rb_next(tmp); 602355da1ebSSage Weil dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, 603355da1ebSSage Weil xattr->name_len, xattr->name); 604355da1ebSSage Weil rb_erase(tmp, &ci->i_xattrs.index); 605355da1ebSSage Weil 606355da1ebSSage Weil __free_xattr(xattr); 607355da1ebSSage Weil } 608355da1ebSSage Weil 609355da1ebSSage Weil ci->i_xattrs.names_size = 0; 610355da1ebSSage Weil ci->i_xattrs.vals_size = 0; 611355da1ebSSage Weil ci->i_xattrs.index_version = 0; 612355da1ebSSage Weil ci->i_xattrs.count = 0; 613355da1ebSSage Weil ci->i_xattrs.index = RB_ROOT; 614355da1ebSSage Weil } 615355da1ebSSage Weil 616355da1ebSSage Weil static int __build_xattrs(struct inode *inode) 617be655596SSage Weil __releases(ci->i_ceph_lock) 618be655596SSage Weil __acquires(ci->i_ceph_lock) 619355da1ebSSage Weil { 620355da1ebSSage Weil u32 namelen; 621355da1ebSSage Weil u32 numattr = 0; 622355da1ebSSage Weil void *p, *end; 623355da1ebSSage Weil u32 len; 624355da1ebSSage Weil const char *name, *val; 625355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 626355da1ebSSage Weil int xattr_version; 627355da1ebSSage Weil struct ceph_inode_xattr **xattrs = NULL; 62863ff78b2SSage Weil int err = 0; 629355da1ebSSage Weil int i; 630355da1ebSSage Weil 631355da1ebSSage Weil dout("__build_xattrs() len=%d\n", 632355da1ebSSage Weil ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); 633355da1ebSSage Weil 634355da1ebSSage Weil if (ci->i_xattrs.index_version >= ci->i_xattrs.version) 635355da1ebSSage Weil return 0; /* already built */ 636355da1ebSSage Weil 637355da1ebSSage Weil __ceph_destroy_xattrs(ci); 638355da1ebSSage Weil 639355da1ebSSage Weil start: 640355da1ebSSage Weil /* updated internal xattr rb tree */ 641355da1ebSSage Weil if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { 642355da1ebSSage Weil p = ci->i_xattrs.blob->vec.iov_base; 643355da1ebSSage Weil end = p + ci->i_xattrs.blob->vec.iov_len; 644355da1ebSSage Weil ceph_decode_32_safe(&p, end, numattr, bad); 645355da1ebSSage Weil xattr_version = ci->i_xattrs.version; 646be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 647355da1ebSSage Weil 6487e8a2952SIlya Dryomov xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), 649355da1ebSSage Weil GFP_NOFS); 650355da1ebSSage Weil err = -ENOMEM; 651355da1ebSSage Weil if (!xattrs) 652355da1ebSSage Weil goto bad_lock; 6531a295bd8SIlya Dryomov 654355da1ebSSage Weil for (i = 0; i < numattr; i++) { 655355da1ebSSage Weil xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), 656355da1ebSSage Weil GFP_NOFS); 657355da1ebSSage Weil if (!xattrs[i]) 658355da1ebSSage Weil goto bad_lock; 659355da1ebSSage Weil } 660355da1ebSSage Weil 661be655596SSage Weil spin_lock(&ci->i_ceph_lock); 662355da1ebSSage Weil if (ci->i_xattrs.version != xattr_version) { 663355da1ebSSage Weil /* lost a race, retry */ 664355da1ebSSage Weil for (i = 0; i < numattr; i++) 665355da1ebSSage Weil kfree(xattrs[i]); 666355da1ebSSage Weil kfree(xattrs); 66721ec6ffaSAlan Cox xattrs = NULL; 668355da1ebSSage Weil goto start; 669355da1ebSSage Weil } 670355da1ebSSage Weil err = -EIO; 671355da1ebSSage Weil while (numattr--) { 672355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 673355da1ebSSage Weil namelen = len; 674355da1ebSSage Weil name = p; 675355da1ebSSage Weil p += len; 676355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 677355da1ebSSage Weil val = p; 678355da1ebSSage Weil p += len; 679355da1ebSSage Weil 680355da1ebSSage Weil err = __set_xattr(ci, name, namelen, val, len, 681fbc0b970SYan, Zheng 0, 0, &xattrs[numattr]); 682355da1ebSSage Weil 683355da1ebSSage Weil if (err < 0) 684355da1ebSSage Weil goto bad; 685355da1ebSSage Weil } 686355da1ebSSage Weil kfree(xattrs); 687355da1ebSSage Weil } 688355da1ebSSage Weil ci->i_xattrs.index_version = ci->i_xattrs.version; 689355da1ebSSage Weil ci->i_xattrs.dirty = false; 690355da1ebSSage Weil 691355da1ebSSage Weil return err; 692355da1ebSSage Weil bad_lock: 693be655596SSage Weil spin_lock(&ci->i_ceph_lock); 694355da1ebSSage Weil bad: 695355da1ebSSage Weil if (xattrs) { 696355da1ebSSage Weil for (i = 0; i < numattr; i++) 697355da1ebSSage Weil kfree(xattrs[i]); 698355da1ebSSage Weil kfree(xattrs); 699355da1ebSSage Weil } 700355da1ebSSage Weil ci->i_xattrs.names_size = 0; 701355da1ebSSage Weil return err; 702355da1ebSSage Weil } 703355da1ebSSage Weil 704355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, 705355da1ebSSage Weil int val_size) 706355da1ebSSage Weil { 707355da1ebSSage Weil /* 708355da1ebSSage Weil * 4 bytes for the length, and additional 4 bytes per each xattr name, 709355da1ebSSage Weil * 4 bytes per each value 710355da1ebSSage Weil */ 711355da1ebSSage Weil int size = 4 + ci->i_xattrs.count*(4 + 4) + 712355da1ebSSage Weil ci->i_xattrs.names_size + 713355da1ebSSage Weil ci->i_xattrs.vals_size; 714355da1ebSSage Weil dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", 715355da1ebSSage Weil ci->i_xattrs.count, ci->i_xattrs.names_size, 716355da1ebSSage Weil ci->i_xattrs.vals_size); 717355da1ebSSage Weil 718355da1ebSSage Weil if (name_size) 719355da1ebSSage Weil size += 4 + 4 + name_size + val_size; 720355da1ebSSage Weil 721355da1ebSSage Weil return size; 722355da1ebSSage Weil } 723355da1ebSSage Weil 724355da1ebSSage Weil /* 725355da1ebSSage Weil * If there are dirty xattrs, reencode xattrs into the prealloc_blob 726355da1ebSSage Weil * and swap into place. 727355da1ebSSage Weil */ 728355da1ebSSage Weil void __ceph_build_xattrs_blob(struct ceph_inode_info *ci) 729355da1ebSSage Weil { 730355da1ebSSage Weil struct rb_node *p; 731355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 732355da1ebSSage Weil void *dest; 733355da1ebSSage Weil 734355da1ebSSage Weil dout("__build_xattrs_blob %p\n", &ci->vfs_inode); 735355da1ebSSage Weil if (ci->i_xattrs.dirty) { 736355da1ebSSage Weil int need = __get_required_blob_size(ci, 0, 0); 737355da1ebSSage Weil 738355da1ebSSage Weil BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); 739355da1ebSSage Weil 740355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 741355da1ebSSage Weil dest = ci->i_xattrs.prealloc_blob->vec.iov_base; 742355da1ebSSage Weil 743355da1ebSSage Weil ceph_encode_32(&dest, ci->i_xattrs.count); 744355da1ebSSage Weil while (p) { 745355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 746355da1ebSSage Weil 747355da1ebSSage Weil ceph_encode_32(&dest, xattr->name_len); 748355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 749355da1ebSSage Weil dest += xattr->name_len; 750355da1ebSSage Weil ceph_encode_32(&dest, xattr->val_len); 751355da1ebSSage Weil memcpy(dest, xattr->val, xattr->val_len); 752355da1ebSSage Weil dest += xattr->val_len; 753355da1ebSSage Weil 754355da1ebSSage Weil p = rb_next(p); 755355da1ebSSage Weil } 756355da1ebSSage Weil 757355da1ebSSage Weil /* adjust buffer len; it may be larger than we need */ 758355da1ebSSage Weil ci->i_xattrs.prealloc_blob->vec.iov_len = 759355da1ebSSage Weil dest - ci->i_xattrs.prealloc_blob->vec.iov_base; 760355da1ebSSage Weil 761b6c1d5b8SSage Weil if (ci->i_xattrs.blob) 762355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.blob); 763355da1ebSSage Weil ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; 764355da1ebSSage Weil ci->i_xattrs.prealloc_blob = NULL; 765355da1ebSSage Weil ci->i_xattrs.dirty = false; 7664a625be4SSage Weil ci->i_xattrs.version++; 767355da1ebSSage Weil } 768355da1ebSSage Weil } 769355da1ebSSage Weil 770315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) { 771315f2408SYan, Zheng struct ceph_mds_request *req = current->journal_info; 772315f2408SYan, Zheng int mask = 0; 773315f2408SYan, Zheng if (req && req->r_target_inode == in) { 774315f2408SYan, Zheng if (req->r_op == CEPH_MDS_OP_LOOKUP || 775315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPINO || 776315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPPARENT || 777315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_GETATTR) { 778315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.getattr.mask); 779315f2408SYan, Zheng } else if (req->r_op == CEPH_MDS_OP_OPEN || 780315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_CREATE) { 781315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.open.mask); 782315f2408SYan, Zheng } 783315f2408SYan, Zheng } 784315f2408SYan, Zheng return mask; 785315f2408SYan, Zheng } 786315f2408SYan, Zheng 7877221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, 788355da1ebSSage Weil size_t size) 789355da1ebSSage Weil { 790355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 791355da1ebSSage Weil struct ceph_inode_xattr *xattr; 792881a5fa2SAlex Elder struct ceph_vxattr *vxattr = NULL; 793315f2408SYan, Zheng int req_mask; 794*f1d1b51dSJeff Layton ssize_t err; 795355da1ebSSage Weil 7960bee82fbSSage Weil /* let's see if a virtual xattr was requested */ 7970bee82fbSSage Weil vxattr = ceph_match_vxattr(inode, name); 79829dccfa5SYan, Zheng if (vxattr) { 79949a9f4f6SYan, Zheng int mask = 0; 80049a9f4f6SYan, Zheng if (vxattr->flags & VXATTR_FLAG_RSTAT) 80149a9f4f6SYan, Zheng mask |= CEPH_STAT_RSTAT; 80249a9f4f6SYan, Zheng err = ceph_do_getattr(inode, mask, true); 8031684dd03SYan, Zheng if (err) 8041684dd03SYan, Zheng return err; 80529dccfa5SYan, Zheng err = -ENODATA; 80629dccfa5SYan, Zheng if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) 8070bee82fbSSage Weil err = vxattr->getxattr_cb(ci, value, size); 808a1dc1937Smajianpeng return err; 8090bee82fbSSage Weil } 8100bee82fbSSage Weil 811315f2408SYan, Zheng req_mask = __get_request_mask(inode); 812315f2408SYan, Zheng 813a1dc1937Smajianpeng spin_lock(&ci->i_ceph_lock); 814a1dc1937Smajianpeng dout("getxattr %p ver=%lld index_ver=%lld\n", inode, 815a1dc1937Smajianpeng ci->i_xattrs.version, ci->i_xattrs.index_version); 816a1dc1937Smajianpeng 817508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 818315f2408SYan, Zheng !((req_mask & CEPH_CAP_XATTR_SHARED) || 819315f2408SYan, Zheng __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) { 820be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 821315f2408SYan, Zheng 822315f2408SYan, Zheng /* security module gets xattr while filling trace */ 823d37b1d99SMarkus Elfring if (current->journal_info) { 824315f2408SYan, Zheng pr_warn_ratelimited("sync getxattr %p " 825315f2408SYan, Zheng "during filling trace\n", inode); 826315f2408SYan, Zheng return -EBUSY; 827315f2408SYan, Zheng } 828315f2408SYan, Zheng 829355da1ebSSage Weil /* get xattrs from mds (if we don't already have them) */ 830508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); 831355da1ebSSage Weil if (err) 832355da1ebSSage Weil return err; 833be655596SSage Weil spin_lock(&ci->i_ceph_lock); 834508b32d8SYan, Zheng } 835355da1ebSSage Weil 836355da1ebSSage Weil err = __build_xattrs(inode); 837355da1ebSSage Weil if (err < 0) 838355da1ebSSage Weil goto out; 839355da1ebSSage Weil 840355da1ebSSage Weil err = -ENODATA; /* == ENOATTR */ 841355da1ebSSage Weil xattr = __get_xattr(ci, name); 8420bee82fbSSage Weil if (!xattr) 843355da1ebSSage Weil goto out; 844355da1ebSSage Weil 845355da1ebSSage Weil err = -ERANGE; 846355da1ebSSage Weil if (size && size < xattr->val_len) 847355da1ebSSage Weil goto out; 848355da1ebSSage Weil 849355da1ebSSage Weil err = xattr->val_len; 850355da1ebSSage Weil if (size == 0) 851355da1ebSSage Weil goto out; 852355da1ebSSage Weil 853355da1ebSSage Weil memcpy(value, xattr->val, xattr->val_len); 854355da1ebSSage Weil 855d37b1d99SMarkus Elfring if (current->journal_info && 856315f2408SYan, Zheng !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) 857315f2408SYan, Zheng ci->i_ceph_flags |= CEPH_I_SEC_INITED; 858355da1ebSSage Weil out: 859be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 860355da1ebSSage Weil return err; 861355da1ebSSage Weil } 862355da1ebSSage Weil 863355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) 864355da1ebSSage Weil { 8652b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 866355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 867881a5fa2SAlex Elder struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode); 8682b2abcacSDavid Disseldorp bool len_only = (size == 0); 869355da1ebSSage Weil u32 namelen; 870355da1ebSSage Weil int err; 871355da1ebSSage Weil int i; 872355da1ebSSage Weil 873be655596SSage Weil spin_lock(&ci->i_ceph_lock); 874355da1ebSSage Weil dout("listxattr %p ver=%lld index_ver=%lld\n", inode, 875355da1ebSSage Weil ci->i_xattrs.version, ci->i_xattrs.index_version); 876355da1ebSSage Weil 877508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 878508b32d8SYan, Zheng !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) { 879be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 880508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); 881355da1ebSSage Weil if (err) 882355da1ebSSage Weil return err; 883be655596SSage Weil spin_lock(&ci->i_ceph_lock); 884508b32d8SYan, Zheng } 885355da1ebSSage Weil 886355da1ebSSage Weil err = __build_xattrs(inode); 887355da1ebSSage Weil if (err < 0) 888355da1ebSSage Weil goto out; 8893ce6cd12SAlex Elder 8902b2abcacSDavid Disseldorp /* add 1 byte for each xattr due to the null termination */ 891b65917ddSSage Weil namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; 8922b2abcacSDavid Disseldorp if (!len_only) { 8932b2abcacSDavid Disseldorp if (namelen > size) { 894355da1ebSSage Weil err = -ERANGE; 895355da1ebSSage Weil goto out; 8962b2abcacSDavid Disseldorp } 897355da1ebSSage Weil names = __copy_xattr_names(ci, names); 8982b2abcacSDavid Disseldorp size -= namelen; 8992b2abcacSDavid Disseldorp } 9002b2abcacSDavid Disseldorp 901355da1ebSSage Weil 902355da1ebSSage Weil /* virtual xattr names, too */ 903b65917ddSSage Weil if (vxattrs) { 904355da1ebSSage Weil for (i = 0; vxattrs[i].name; i++) { 9052b2abcacSDavid Disseldorp size_t this_len; 9062b2abcacSDavid Disseldorp 9072b2abcacSDavid Disseldorp if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN) 9082b2abcacSDavid Disseldorp continue; 9092b2abcacSDavid Disseldorp if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci)) 9102b2abcacSDavid Disseldorp continue; 9112b2abcacSDavid Disseldorp 9122b2abcacSDavid Disseldorp this_len = strlen(vxattrs[i].name) + 1; 9132b2abcacSDavid Disseldorp namelen += this_len; 9142b2abcacSDavid Disseldorp if (len_only) 9152b2abcacSDavid Disseldorp continue; 9162b2abcacSDavid Disseldorp 9172b2abcacSDavid Disseldorp if (this_len > size) { 9182b2abcacSDavid Disseldorp err = -ERANGE; 9192b2abcacSDavid Disseldorp goto out; 920355da1ebSSage Weil } 921355da1ebSSage Weil 9222b2abcacSDavid Disseldorp memcpy(names, vxattrs[i].name, this_len); 9232b2abcacSDavid Disseldorp names += this_len; 9242b2abcacSDavid Disseldorp size -= this_len; 9252b2abcacSDavid Disseldorp } 9262b2abcacSDavid Disseldorp } 9272b2abcacSDavid Disseldorp err = namelen; 928355da1ebSSage Weil out: 929be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 930355da1ebSSage Weil return err; 931355da1ebSSage Weil } 932355da1ebSSage Weil 933a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name, 934355da1ebSSage Weil const char *value, size_t size, int flags) 935355da1ebSSage Weil { 936a26feccaSAndreas Gruenbacher struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 937355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 938355da1ebSSage Weil struct ceph_mds_request *req; 9393d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 94025e6bae3SYan, Zheng struct ceph_pagelist *pagelist = NULL; 94104303d8aSYan, Zheng int op = CEPH_MDS_OP_SETXATTR; 942355da1ebSSage Weil int err; 943355da1ebSSage Weil 9440aeff37aSYan, Zheng if (size > 0) { 94525e6bae3SYan, Zheng /* copy value into pagelist */ 94633165d47SIlya Dryomov pagelist = ceph_pagelist_alloc(GFP_NOFS); 94725e6bae3SYan, Zheng if (!pagelist) 948355da1ebSSage Weil return -ENOMEM; 94925e6bae3SYan, Zheng 95025e6bae3SYan, Zheng err = ceph_pagelist_append(pagelist, value, size); 95125e6bae3SYan, Zheng if (err) 952355da1ebSSage Weil goto out; 9530aeff37aSYan, Zheng } else if (!value) { 95404303d8aSYan, Zheng if (flags & CEPH_XATTR_REPLACE) 95504303d8aSYan, Zheng op = CEPH_MDS_OP_RMXATTR; 95604303d8aSYan, Zheng else 95725e6bae3SYan, Zheng flags |= CEPH_XATTR_REMOVE; 958355da1ebSSage Weil } 959355da1ebSSage Weil 960355da1ebSSage Weil dout("setxattr value=%.*s\n", (int)size, value); 961355da1ebSSage Weil 962355da1ebSSage Weil /* do request */ 96304303d8aSYan, Zheng req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); 96460d87733SJulia Lawall if (IS_ERR(req)) { 96560d87733SJulia Lawall err = PTR_ERR(req); 96660d87733SJulia Lawall goto out; 96760d87733SJulia Lawall } 968a149bb9aSSanidhya Kashyap 969355da1ebSSage Weil req->r_path2 = kstrdup(name, GFP_NOFS); 970a149bb9aSSanidhya Kashyap if (!req->r_path2) { 971a149bb9aSSanidhya Kashyap ceph_mdsc_put_request(req); 972a149bb9aSSanidhya Kashyap err = -ENOMEM; 973a149bb9aSSanidhya Kashyap goto out; 974a149bb9aSSanidhya Kashyap } 975355da1ebSSage Weil 97604303d8aSYan, Zheng if (op == CEPH_MDS_OP_SETXATTR) { 97704303d8aSYan, Zheng req->r_args.setxattr.flags = cpu_to_le32(flags); 97825e6bae3SYan, Zheng req->r_pagelist = pagelist; 97925e6bae3SYan, Zheng pagelist = NULL; 98004303d8aSYan, Zheng } 981355da1ebSSage Weil 982a149bb9aSSanidhya Kashyap req->r_inode = inode; 983a149bb9aSSanidhya Kashyap ihold(inode); 984a149bb9aSSanidhya Kashyap req->r_num_caps = 1; 985a149bb9aSSanidhya Kashyap req->r_inode_drop = CEPH_CAP_XATTR_SHARED; 986a149bb9aSSanidhya Kashyap 987355da1ebSSage Weil dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); 988752c8bdcSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 989355da1ebSSage Weil ceph_mdsc_put_request(req); 990355da1ebSSage Weil dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); 991355da1ebSSage Weil 992355da1ebSSage Weil out: 99325e6bae3SYan, Zheng if (pagelist) 99425e6bae3SYan, Zheng ceph_pagelist_release(pagelist); 995355da1ebSSage Weil return err; 996355da1ebSSage Weil } 997355da1ebSSage Weil 998a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name, 999355da1ebSSage Weil const void *value, size_t size, int flags) 1000355da1ebSSage Weil { 1001881a5fa2SAlex Elder struct ceph_vxattr *vxattr; 1002355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1003a26feccaSAndreas Gruenbacher struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 1004f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf = NULL; 100518fa8b3fSAlex Elder int issued; 1006355da1ebSSage Weil int err; 1007fbc0b970SYan, Zheng int dirty = 0; 1008355da1ebSSage Weil int name_len = strlen(name); 1009355da1ebSSage Weil int val_len = size; 1010355da1ebSSage Weil char *newname = NULL; 1011355da1ebSSage Weil char *newval = NULL; 1012355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 1013355da1ebSSage Weil int required_blob_size; 1014f1919826SYan, Zheng bool check_realm = false; 1015604d1b02SYan, Zheng bool lock_snap_rwsem = false; 1016355da1ebSSage Weil 10172cdeb1e4SAndreas Gruenbacher if (ceph_snap(inode) != CEPH_NOSNAP) 10182cdeb1e4SAndreas Gruenbacher return -EROFS; 1019355da1ebSSage Weil 102006476a69SAlex Elder vxattr = ceph_match_vxattr(inode, name); 1021f1919826SYan, Zheng if (vxattr) { 10224e9906e7SYan, Zheng if (vxattr->flags & VXATTR_FLAG_READONLY) 1023355da1ebSSage Weil return -EOPNOTSUPP; 1024f1919826SYan, Zheng if (value && !strncmp(vxattr->name, "ceph.quota", 10)) 1025f1919826SYan, Zheng check_realm = true; 1026f1919826SYan, Zheng } 1027355da1ebSSage Weil 10283adf654dSSage Weil /* pass any unhandled ceph.* xattrs through to the MDS */ 10293adf654dSSage Weil if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) 10303adf654dSSage Weil goto do_sync_unlocked; 10313adf654dSSage Weil 1032355da1ebSSage Weil /* preallocate memory for xattr name, value, index node */ 1033355da1ebSSage Weil err = -ENOMEM; 103461413c2fSJulia Lawall newname = kmemdup(name, name_len + 1, GFP_NOFS); 1035355da1ebSSage Weil if (!newname) 1036355da1ebSSage Weil goto out; 1037355da1ebSSage Weil 1038355da1ebSSage Weil if (val_len) { 1039b829c195SAlex Elder newval = kmemdup(value, val_len, GFP_NOFS); 1040355da1ebSSage Weil if (!newval) 1041355da1ebSSage Weil goto out; 1042355da1ebSSage Weil } 1043355da1ebSSage Weil 1044355da1ebSSage Weil xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); 1045355da1ebSSage Weil if (!xattr) 1046355da1ebSSage Weil goto out; 1047355da1ebSSage Weil 1048f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1049f66fd9f0SYan, Zheng if (!prealloc_cf) 1050f66fd9f0SYan, Zheng goto out; 1051f66fd9f0SYan, Zheng 1052be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1053355da1ebSSage Weil retry: 1054355da1ebSSage Weil issued = __ceph_caps_issued(ci, NULL); 1055508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) 1056355da1ebSSage Weil goto do_sync; 1057604d1b02SYan, Zheng 1058604d1b02SYan, Zheng if (!lock_snap_rwsem && !ci->i_head_snapc) { 1059604d1b02SYan, Zheng lock_snap_rwsem = true; 1060604d1b02SYan, Zheng if (!down_read_trylock(&mdsc->snap_rwsem)) { 1061604d1b02SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1062604d1b02SYan, Zheng down_read(&mdsc->snap_rwsem); 1063604d1b02SYan, Zheng spin_lock(&ci->i_ceph_lock); 1064604d1b02SYan, Zheng goto retry; 1065604d1b02SYan, Zheng } 1066604d1b02SYan, Zheng } 1067604d1b02SYan, Zheng 1068604d1b02SYan, Zheng dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); 1069355da1ebSSage Weil __build_xattrs(inode); 1070355da1ebSSage Weil 1071355da1ebSSage Weil required_blob_size = __get_required_blob_size(ci, name_len, val_len); 1072355da1ebSSage Weil 1073355da1ebSSage Weil if (!ci->i_xattrs.prealloc_blob || 1074355da1ebSSage Weil required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { 107518fa8b3fSAlex Elder struct ceph_buffer *blob; 1076355da1ebSSage Weil 1077be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1078355da1ebSSage Weil dout(" preaallocating new blob size=%d\n", required_blob_size); 1079b6c1d5b8SSage Weil blob = ceph_buffer_new(required_blob_size, GFP_NOFS); 1080355da1ebSSage Weil if (!blob) 1081604d1b02SYan, Zheng goto do_sync_unlocked; 1082be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1083b6c1d5b8SSage Weil if (ci->i_xattrs.prealloc_blob) 1084355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.prealloc_blob); 1085355da1ebSSage Weil ci->i_xattrs.prealloc_blob = blob; 1086355da1ebSSage Weil goto retry; 1087355da1ebSSage Weil } 1088355da1ebSSage Weil 1089bcdfeb2eSYan, Zheng err = __set_xattr(ci, newname, name_len, newval, val_len, 1090bcdfeb2eSYan, Zheng flags, value ? 1 : -1, &xattr); 109118fa8b3fSAlex Elder 1092fbc0b970SYan, Zheng if (!err) { 1093f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, 1094f66fd9f0SYan, Zheng &prealloc_cf); 1095355da1ebSSage Weil ci->i_xattrs.dirty = true; 1096c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 1097fbc0b970SYan, Zheng } 109818fa8b3fSAlex Elder 1099be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1100604d1b02SYan, Zheng if (lock_snap_rwsem) 1101604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1102fca65b4aSSage Weil if (dirty) 1103fca65b4aSSage Weil __mark_inode_dirty(inode, dirty); 1104f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1105355da1ebSSage Weil return err; 1106355da1ebSSage Weil 1107355da1ebSSage Weil do_sync: 1108be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 11093adf654dSSage Weil do_sync_unlocked: 1110604d1b02SYan, Zheng if (lock_snap_rwsem) 1111604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1112315f2408SYan, Zheng 1113315f2408SYan, Zheng /* security module set xattr while filling trace */ 1114d37b1d99SMarkus Elfring if (current->journal_info) { 1115315f2408SYan, Zheng pr_warn_ratelimited("sync setxattr %p " 1116315f2408SYan, Zheng "during filling trace\n", inode); 1117315f2408SYan, Zheng err = -EBUSY; 1118315f2408SYan, Zheng } else { 1119a26feccaSAndreas Gruenbacher err = ceph_sync_setxattr(inode, name, value, size, flags); 1120f1919826SYan, Zheng if (err >= 0 && check_realm) { 1121f1919826SYan, Zheng /* check if snaprealm was created for quota inode */ 1122f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 1123f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 1124f1919826SYan, Zheng !(ci->i_snap_realm && 1125f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino)) 1126f1919826SYan, Zheng err = -EOPNOTSUPP; 1127f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1128f1919826SYan, Zheng } 1129315f2408SYan, Zheng } 1130355da1ebSSage Weil out: 1131f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1132355da1ebSSage Weil kfree(newname); 1133355da1ebSSage Weil kfree(newval); 1134355da1ebSSage Weil kfree(xattr); 1135355da1ebSSage Weil return err; 1136355da1ebSSage Weil } 1137355da1ebSSage Weil 11382cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler, 11392cdeb1e4SAndreas Gruenbacher struct dentry *dentry, struct inode *inode, 11402cdeb1e4SAndreas Gruenbacher const char *name, void *value, size_t size) 11417221fe4cSGuangliang Zhao { 11422cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 11432cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 11442cdeb1e4SAndreas Gruenbacher return __ceph_getxattr(inode, name, value, size); 11457221fe4cSGuangliang Zhao } 1146315f2408SYan, Zheng 11472cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler, 114859301226SAl Viro struct dentry *unused, struct inode *inode, 114959301226SAl Viro const char *name, const void *value, 115059301226SAl Viro size_t size, int flags) 11512cdeb1e4SAndreas Gruenbacher { 11522cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 11532cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 115459301226SAl Viro return __ceph_setxattr(inode, name, value, size, flags); 11552cdeb1e4SAndreas Gruenbacher } 11562cdeb1e4SAndreas Gruenbacher 11575130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = { 11582cdeb1e4SAndreas Gruenbacher .prefix = "", /* match any name => handlers called with full name */ 11592cdeb1e4SAndreas Gruenbacher .get = ceph_get_xattr_handler, 11602cdeb1e4SAndreas Gruenbacher .set = ceph_set_xattr_handler, 11612cdeb1e4SAndreas Gruenbacher }; 11622cdeb1e4SAndreas Gruenbacher 1163315f2408SYan, Zheng #ifdef CONFIG_SECURITY 1164315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in) 1165315f2408SYan, Zheng { 1166315f2408SYan, Zheng return in->i_security != NULL; 1167315f2408SYan, Zheng } 1168315f2408SYan, Zheng 1169315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in) 1170315f2408SYan, Zheng { 1171315f2408SYan, Zheng struct ceph_inode_info *ci; 1172315f2408SYan, Zheng bool ret; 1173d37b1d99SMarkus Elfring if (!in->i_security) 1174315f2408SYan, Zheng return false; 1175315f2408SYan, Zheng ci = ceph_inode(in); 1176315f2408SYan, Zheng spin_lock(&ci->i_ceph_lock); 1177315f2408SYan, Zheng ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && 1178315f2408SYan, Zheng !(ci->i_xattrs.version > 0 && 1179315f2408SYan, Zheng __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); 1180315f2408SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1181315f2408SYan, Zheng return ret; 1182315f2408SYan, Zheng } 1183ac6713ccSYan, Zheng 1184ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1185ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode, 1186ac6713ccSYan, Zheng struct ceph_acl_sec_ctx *as_ctx) 1187ac6713ccSYan, Zheng { 1188ac6713ccSYan, Zheng struct ceph_pagelist *pagelist = as_ctx->pagelist; 1189ac6713ccSYan, Zheng const char *name; 1190ac6713ccSYan, Zheng size_t name_len; 1191ac6713ccSYan, Zheng int err; 1192ac6713ccSYan, Zheng 1193ac6713ccSYan, Zheng err = security_dentry_init_security(dentry, mode, &dentry->d_name, 1194ac6713ccSYan, Zheng &as_ctx->sec_ctx, 1195ac6713ccSYan, Zheng &as_ctx->sec_ctxlen); 1196ac6713ccSYan, Zheng if (err < 0) { 1197ac6713ccSYan, Zheng WARN_ON_ONCE(err != -EOPNOTSUPP); 1198ac6713ccSYan, Zheng err = 0; /* do nothing */ 1199ac6713ccSYan, Zheng goto out; 1200ac6713ccSYan, Zheng } 1201ac6713ccSYan, Zheng 1202ac6713ccSYan, Zheng err = -ENOMEM; 1203ac6713ccSYan, Zheng if (!pagelist) { 1204ac6713ccSYan, Zheng pagelist = ceph_pagelist_alloc(GFP_KERNEL); 1205ac6713ccSYan, Zheng if (!pagelist) 1206ac6713ccSYan, Zheng goto out; 1207ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 1208ac6713ccSYan, Zheng if (err) 1209ac6713ccSYan, Zheng goto out; 1210ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, 1); 1211ac6713ccSYan, Zheng } 1212ac6713ccSYan, Zheng 1213ac6713ccSYan, Zheng /* 1214ac6713ccSYan, Zheng * FIXME: Make security_dentry_init_security() generic. Currently 1215ac6713ccSYan, Zheng * It only supports single security module and only selinux has 1216ac6713ccSYan, Zheng * dentry_init_security hook. 1217ac6713ccSYan, Zheng */ 1218ac6713ccSYan, Zheng name = XATTR_NAME_SELINUX; 1219ac6713ccSYan, Zheng name_len = strlen(name); 1220ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, 1221ac6713ccSYan, Zheng 4 * 2 + name_len + as_ctx->sec_ctxlen); 1222ac6713ccSYan, Zheng if (err) 1223ac6713ccSYan, Zheng goto out; 1224ac6713ccSYan, Zheng 1225ac6713ccSYan, Zheng if (as_ctx->pagelist) { 1226ac6713ccSYan, Zheng /* update count of KV pairs */ 1227ac6713ccSYan, Zheng BUG_ON(pagelist->length <= sizeof(__le32)); 1228ac6713ccSYan, Zheng if (list_is_singular(&pagelist->head)) { 1229ac6713ccSYan, Zheng le32_add_cpu((__le32*)pagelist->mapped_tail, 1); 1230ac6713ccSYan, Zheng } else { 1231ac6713ccSYan, Zheng struct page *page = list_first_entry(&pagelist->head, 1232ac6713ccSYan, Zheng struct page, lru); 1233ac6713ccSYan, Zheng void *addr = kmap_atomic(page); 1234ac6713ccSYan, Zheng le32_add_cpu((__le32*)addr, 1); 1235ac6713ccSYan, Zheng kunmap_atomic(addr); 1236ac6713ccSYan, Zheng } 1237ac6713ccSYan, Zheng } else { 1238ac6713ccSYan, Zheng as_ctx->pagelist = pagelist; 1239ac6713ccSYan, Zheng } 1240ac6713ccSYan, Zheng 1241ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, name_len); 1242ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, name, name_len); 1243ac6713ccSYan, Zheng 1244ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen); 1245ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1246ac6713ccSYan, Zheng 1247ac6713ccSYan, Zheng err = 0; 1248ac6713ccSYan, Zheng out: 1249ac6713ccSYan, Zheng if (pagelist && !as_ctx->pagelist) 1250ac6713ccSYan, Zheng ceph_pagelist_release(pagelist); 1251ac6713ccSYan, Zheng return err; 1252ac6713ccSYan, Zheng } 1253ac6713ccSYan, Zheng 1254ac6713ccSYan, Zheng void ceph_security_invalidate_secctx(struct inode *inode) 1255ac6713ccSYan, Zheng { 1256ac6713ccSYan, Zheng security_inode_invalidate_secctx(inode); 1257ac6713ccSYan, Zheng } 1258ac6713ccSYan, Zheng 1259ac6713ccSYan, Zheng static int ceph_xattr_set_security_label(const struct xattr_handler *handler, 1260ac6713ccSYan, Zheng struct dentry *unused, struct inode *inode, 1261ac6713ccSYan, Zheng const char *key, const void *buf, 1262ac6713ccSYan, Zheng size_t buflen, int flags) 1263ac6713ccSYan, Zheng { 1264ac6713ccSYan, Zheng if (security_ismaclabel(key)) { 1265ac6713ccSYan, Zheng const char *name = xattr_full_name(handler, key); 1266ac6713ccSYan, Zheng return __ceph_setxattr(inode, name, buf, buflen, flags); 1267ac6713ccSYan, Zheng } 1268ac6713ccSYan, Zheng return -EOPNOTSUPP; 1269ac6713ccSYan, Zheng } 1270ac6713ccSYan, Zheng 1271ac6713ccSYan, Zheng static int ceph_xattr_get_security_label(const struct xattr_handler *handler, 1272ac6713ccSYan, Zheng struct dentry *unused, struct inode *inode, 1273ac6713ccSYan, Zheng const char *key, void *buf, size_t buflen) 1274ac6713ccSYan, Zheng { 1275ac6713ccSYan, Zheng if (security_ismaclabel(key)) { 1276ac6713ccSYan, Zheng const char *name = xattr_full_name(handler, key); 1277ac6713ccSYan, Zheng return __ceph_getxattr(inode, name, buf, buflen); 1278ac6713ccSYan, Zheng } 1279ac6713ccSYan, Zheng return -EOPNOTSUPP; 1280ac6713ccSYan, Zheng } 1281ac6713ccSYan, Zheng 1282ac6713ccSYan, Zheng static const struct xattr_handler ceph_security_label_handler = { 1283ac6713ccSYan, Zheng .prefix = XATTR_SECURITY_PREFIX, 1284ac6713ccSYan, Zheng .get = ceph_xattr_get_security_label, 1285ac6713ccSYan, Zheng .set = ceph_xattr_set_security_label, 1286ac6713ccSYan, Zheng }; 1287ac6713ccSYan, Zheng #endif 1288315f2408SYan, Zheng #endif 12895c31e92dSYan, Zheng 12905c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx) 12915c31e92dSYan, Zheng { 12925c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL 12935c31e92dSYan, Zheng posix_acl_release(as_ctx->acl); 12945c31e92dSYan, Zheng posix_acl_release(as_ctx->default_acl); 12955c31e92dSYan, Zheng #endif 1296ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1297ac6713ccSYan, Zheng security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1298ac6713ccSYan, Zheng #endif 12995c31e92dSYan, Zheng if (as_ctx->pagelist) 13005c31e92dSYan, Zheng ceph_pagelist_release(as_ctx->pagelist); 13015c31e92dSYan, Zheng } 1302ac6713ccSYan, Zheng 1303ac6713ccSYan, Zheng /* 1304ac6713ccSYan, Zheng * List of handlers for synthetic system.* attributes. Other 1305ac6713ccSYan, Zheng * attributes are handled directly. 1306ac6713ccSYan, Zheng */ 1307ac6713ccSYan, Zheng const struct xattr_handler *ceph_xattr_handlers[] = { 1308ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL 1309ac6713ccSYan, Zheng &posix_acl_access_xattr_handler, 1310ac6713ccSYan, Zheng &posix_acl_default_xattr_handler, 1311ac6713ccSYan, Zheng #endif 1312ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1313ac6713ccSYan, Zheng &ceph_security_label_handler, 1314ac6713ccSYan, Zheng #endif 1315ac6713ccSYan, Zheng &ceph_other_xattr_handler, 1316ac6713ccSYan, Zheng NULL, 1317ac6713ccSYan, Zheng }; 1318