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 { 23b8fe918bSJeff Layton return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) || 24b8fe918bSJeff Layton !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || 25355da1ebSSage Weil !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || 26355da1ebSSage Weil !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); 27355da1ebSSage Weil } 28355da1ebSSage Weil 29355da1ebSSage Weil /* 30355da1ebSSage Weil * These define virtual xattrs exposing the recursive directory 31355da1ebSSage Weil * statistics and layout metadata. 32355da1ebSSage Weil */ 33881a5fa2SAlex Elder struct ceph_vxattr { 34355da1ebSSage Weil char *name; 353ce6cd12SAlex Elder size_t name_size; /* strlen(name) + 1 (for '\0') */ 36f1d1b51dSJeff Layton ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, 37355da1ebSSage Weil size_t size); 38f36e4472SSage Weil bool (*exists_cb)(struct ceph_inode_info *ci); 394e9906e7SYan, Zheng unsigned int flags; 40355da1ebSSage Weil }; 41355da1ebSSage Weil 424e9906e7SYan, Zheng #define VXATTR_FLAG_READONLY (1<<0) 434e9906e7SYan, Zheng #define VXATTR_FLAG_HIDDEN (1<<1) 4449a9f4f6SYan, Zheng #define VXATTR_FLAG_RSTAT (1<<2) 4581048c00SJeff Layton #define VXATTR_FLAG_DIRSTAT (1<<3) 464e9906e7SYan, Zheng 4732ab0bd7SSage Weil /* layouts */ 4832ab0bd7SSage Weil 4932ab0bd7SSage Weil static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci) 5032ab0bd7SSage Weil { 51779fe0fbSYan, Zheng struct ceph_file_layout *fl = &ci->i_layout; 52779fe0fbSYan, Zheng return (fl->stripe_unit > 0 || fl->stripe_count > 0 || 53779fe0fbSYan, Zheng fl->object_size > 0 || fl->pool_id >= 0 || 54779fe0fbSYan, Zheng rcu_dereference_raw(fl->pool_ns) != NULL); 5532ab0bd7SSage Weil } 5632ab0bd7SSage Weil 57f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, 5832ab0bd7SSage Weil size_t size) 5932ab0bd7SSage Weil { 6032ab0bd7SSage Weil struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 6132ab0bd7SSage Weil struct ceph_osd_client *osdc = &fsc->client->osdc; 62779fe0fbSYan, Zheng struct ceph_string *pool_ns; 637627151eSYan, Zheng s64 pool = ci->i_layout.pool_id; 6432ab0bd7SSage Weil const char *pool_name; 65779fe0fbSYan, Zheng const char *ns_field = " pool_namespace="; 661e5c6649SYan, Zheng char buf[128]; 67779fe0fbSYan, Zheng size_t len, total_len = 0; 683b421018SJeff Layton ssize_t ret; 69779fe0fbSYan, Zheng 70779fe0fbSYan, Zheng pool_ns = ceph_try_get_string(ci->i_layout.pool_ns); 7132ab0bd7SSage Weil 7232ab0bd7SSage Weil dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode); 735aea3dcdSIlya Dryomov down_read(&osdc->lock); 7432ab0bd7SSage Weil pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); 751e5c6649SYan, Zheng if (pool_name) { 76779fe0fbSYan, Zheng len = snprintf(buf, sizeof(buf), 777627151eSYan, Zheng "stripe_unit=%u stripe_count=%u object_size=%u pool=", 787627151eSYan, Zheng ci->i_layout.stripe_unit, ci->i_layout.stripe_count, 797627151eSYan, Zheng ci->i_layout.object_size); 80779fe0fbSYan, Zheng total_len = len + strlen(pool_name); 811e5c6649SYan, Zheng } else { 82779fe0fbSYan, Zheng len = snprintf(buf, sizeof(buf), 837627151eSYan, Zheng "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld", 847627151eSYan, Zheng ci->i_layout.stripe_unit, ci->i_layout.stripe_count, 85f1d1b51dSJeff Layton ci->i_layout.object_size, pool); 86779fe0fbSYan, Zheng total_len = len; 87779fe0fbSYan, Zheng } 88779fe0fbSYan, Zheng 89779fe0fbSYan, Zheng if (pool_ns) 90779fe0fbSYan, Zheng total_len += strlen(ns_field) + pool_ns->len; 91779fe0fbSYan, Zheng 92779fe0fbSYan, Zheng ret = total_len; 933b421018SJeff Layton if (size >= total_len) { 94779fe0fbSYan, Zheng memcpy(val, buf, len); 95779fe0fbSYan, Zheng ret = len; 96779fe0fbSYan, Zheng if (pool_name) { 97779fe0fbSYan, Zheng len = strlen(pool_name); 98779fe0fbSYan, Zheng memcpy(val + ret, pool_name, len); 99779fe0fbSYan, Zheng ret += len; 100779fe0fbSYan, Zheng } 101779fe0fbSYan, Zheng if (pool_ns) { 102779fe0fbSYan, Zheng len = strlen(ns_field); 103779fe0fbSYan, Zheng memcpy(val + ret, ns_field, len); 104779fe0fbSYan, Zheng ret += len; 105779fe0fbSYan, Zheng memcpy(val + ret, pool_ns->str, pool_ns->len); 106779fe0fbSYan, Zheng ret += pool_ns->len; 1071e5c6649SYan, Zheng } 1081e5c6649SYan, Zheng } 1095aea3dcdSIlya Dryomov up_read(&osdc->lock); 110779fe0fbSYan, Zheng ceph_put_string(pool_ns); 11132ab0bd7SSage Weil return ret; 11232ab0bd7SSage Weil } 11332ab0bd7SSage Weil 11426350535SJeff Layton /* 11526350535SJeff Layton * The convention with strings in xattrs is that they should not be NULL 11626350535SJeff Layton * terminated, since we're returning the length with them. snprintf always 11726350535SJeff Layton * NULL terminates however, so call it on a temporary buffer and then memcpy 11826350535SJeff Layton * the result into place. 11926350535SJeff Layton */ 120f6fbdcd9SIlya Dryomov static __printf(3, 4) 121f6fbdcd9SIlya Dryomov int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...) 12226350535SJeff Layton { 12326350535SJeff Layton int ret; 12426350535SJeff Layton va_list args; 12526350535SJeff Layton char buf[96]; /* NB: reevaluate size if new vxattrs are added */ 12626350535SJeff Layton 12726350535SJeff Layton va_start(args, fmt); 12826350535SJeff Layton ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args); 12926350535SJeff Layton va_end(args); 13026350535SJeff Layton 13126350535SJeff Layton /* Sanity check */ 13226350535SJeff Layton if (size && ret + 1 > sizeof(buf)) { 13326350535SJeff Layton WARN_ONCE(true, "Returned length too big (%d)", ret); 13426350535SJeff Layton return -E2BIG; 13526350535SJeff Layton } 13626350535SJeff Layton 13726350535SJeff Layton if (ret <= size) 13826350535SJeff Layton memcpy(val, buf, ret); 13926350535SJeff Layton return ret; 14026350535SJeff Layton } 14126350535SJeff Layton 142f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci, 143695b7119SSage Weil char *val, size_t size) 144695b7119SSage Weil { 14526350535SJeff Layton return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit); 146695b7119SSage Weil } 147695b7119SSage Weil 148f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci, 149695b7119SSage Weil char *val, size_t size) 150695b7119SSage Weil { 15126350535SJeff Layton return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count); 152695b7119SSage Weil } 153695b7119SSage Weil 154f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci, 155695b7119SSage Weil char *val, size_t size) 156695b7119SSage Weil { 15726350535SJeff Layton return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size); 158695b7119SSage Weil } 159695b7119SSage Weil 160f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci, 161695b7119SSage Weil char *val, size_t size) 162695b7119SSage Weil { 163f1d1b51dSJeff Layton ssize_t ret; 164695b7119SSage Weil struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 165695b7119SSage Weil struct ceph_osd_client *osdc = &fsc->client->osdc; 1667627151eSYan, Zheng s64 pool = ci->i_layout.pool_id; 167695b7119SSage Weil const char *pool_name; 168695b7119SSage Weil 1695aea3dcdSIlya Dryomov down_read(&osdc->lock); 170695b7119SSage Weil pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); 17126350535SJeff Layton if (pool_name) { 17226350535SJeff Layton ret = strlen(pool_name); 17326350535SJeff Layton if (ret <= size) 17426350535SJeff Layton memcpy(val, pool_name, ret); 17526350535SJeff Layton } else { 17626350535SJeff Layton ret = ceph_fmt_xattr(val, size, "%lld", pool); 17726350535SJeff Layton } 1785aea3dcdSIlya Dryomov up_read(&osdc->lock); 179695b7119SSage Weil return ret; 180695b7119SSage Weil } 181695b7119SSage Weil 182f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci, 183779fe0fbSYan, Zheng char *val, size_t size) 184779fe0fbSYan, Zheng { 18526350535SJeff Layton ssize_t ret = 0; 186779fe0fbSYan, Zheng struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns); 18726350535SJeff Layton 188779fe0fbSYan, Zheng if (ns) { 18926350535SJeff Layton ret = ns->len; 19026350535SJeff Layton if (ret <= size) 19126350535SJeff Layton memcpy(val, ns->str, ret); 192779fe0fbSYan, Zheng ceph_put_string(ns); 193779fe0fbSYan, Zheng } 194779fe0fbSYan, Zheng return ret; 195779fe0fbSYan, Zheng } 196779fe0fbSYan, Zheng 197355da1ebSSage Weil /* directories */ 198355da1ebSSage Weil 199f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, 200355da1ebSSage Weil size_t size) 201355da1ebSSage Weil { 20226350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs); 203355da1ebSSage Weil } 204355da1ebSSage Weil 205f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, 206355da1ebSSage Weil size_t size) 207355da1ebSSage Weil { 20826350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_files); 209355da1ebSSage Weil } 210355da1ebSSage Weil 211f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, 212355da1ebSSage Weil size_t size) 213355da1ebSSage Weil { 21426350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs); 215355da1ebSSage Weil } 216355da1ebSSage Weil 217f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, 218355da1ebSSage Weil size_t size) 219355da1ebSSage Weil { 22026350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", 22126350535SJeff Layton ci->i_rfiles + ci->i_rsubdirs); 222355da1ebSSage Weil } 223355da1ebSSage Weil 224f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, 225355da1ebSSage Weil size_t size) 226355da1ebSSage Weil { 22726350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles); 228355da1ebSSage Weil } 229355da1ebSSage Weil 230f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, 231355da1ebSSage Weil size_t size) 232355da1ebSSage Weil { 23326350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs); 234355da1ebSSage Weil } 235355da1ebSSage Weil 236f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, 237355da1ebSSage Weil size_t size) 238355da1ebSSage Weil { 23926350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes); 240355da1ebSSage Weil } 241355da1ebSSage Weil 242f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, 243355da1ebSSage Weil size_t size) 244355da1ebSSage Weil { 24526350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec, 2469bbeab41SArnd Bergmann ci->i_rctime.tv_nsec); 247355da1ebSSage Weil } 248355da1ebSSage Weil 24908796873SYan, Zheng /* dir pin */ 25008796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci) 25108796873SYan, Zheng { 25208796873SYan, Zheng return ci->i_dir_pin != -ENODATA; 25308796873SYan, Zheng } 254fb18a575SLuis Henriques 255f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val, 25608796873SYan, Zheng size_t size) 25708796873SYan, Zheng { 25826350535SJeff Layton return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin); 25908796873SYan, Zheng } 26008796873SYan, Zheng 26108796873SYan, Zheng /* quotas */ 262fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci) 263fb18a575SLuis Henriques { 264f1919826SYan, Zheng bool ret = false; 265f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 266f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 267f1919826SYan, Zheng ci->i_vino.snap == CEPH_NOSNAP && 268f1919826SYan, Zheng ci->i_snap_realm && 269f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino) 270f1919826SYan, Zheng ret = true; 271f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 272f1919826SYan, Zheng return ret; 273fb18a575SLuis Henriques } 274fb18a575SLuis Henriques 275f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val, 276fb18a575SLuis Henriques size_t size) 277fb18a575SLuis Henriques { 27826350535SJeff Layton return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu", 279fb18a575SLuis Henriques ci->i_max_bytes, ci->i_max_files); 280fb18a575SLuis Henriques } 281fb18a575SLuis Henriques 282f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci, 283fb18a575SLuis Henriques char *val, size_t size) 284fb18a575SLuis Henriques { 28526350535SJeff Layton return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes); 286fb18a575SLuis Henriques } 287fb18a575SLuis Henriques 288f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci, 289fb18a575SLuis Henriques char *val, size_t size) 290fb18a575SLuis Henriques { 29126350535SJeff Layton return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files); 292fb18a575SLuis Henriques } 29332ab0bd7SSage Weil 294100cc610SDavid Disseldorp /* snapshots */ 295100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci) 296100cc610SDavid Disseldorp { 297100cc610SDavid Disseldorp return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0); 298100cc610SDavid Disseldorp } 299100cc610SDavid Disseldorp 300f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val, 301100cc610SDavid Disseldorp size_t size) 302100cc610SDavid Disseldorp { 30326350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec, 304100cc610SDavid Disseldorp ci->i_snap_btime.tv_nsec); 305100cc610SDavid Disseldorp } 306100cc610SDavid Disseldorp 3075a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci, 3085a9e2f5dSXiubo Li char *val, size_t size) 3095a9e2f5dSXiubo Li { 3105a9e2f5dSXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 3115a9e2f5dSXiubo Li 3125a9e2f5dSXiubo Li return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid); 3135a9e2f5dSXiubo Li } 3145a9e2f5dSXiubo Li 3155a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci, 3165a9e2f5dSXiubo Li char *val, size_t size) 3175a9e2f5dSXiubo Li { 3185a9e2f5dSXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); 3195a9e2f5dSXiubo Li 3205a9e2f5dSXiubo Li return ceph_fmt_xattr(val, size, "client%lld", 3215a9e2f5dSXiubo Li ceph_client_gid(fsc->client)); 3225a9e2f5dSXiubo Li } 3235a9e2f5dSXiubo Li 324dd980fc0SLuis Henriques static ssize_t ceph_vxattrcb_caps(struct ceph_inode_info *ci, char *val, 325dd980fc0SLuis Henriques size_t size) 326dd980fc0SLuis Henriques { 327dd980fc0SLuis Henriques int issued; 328dd980fc0SLuis Henriques 329dd980fc0SLuis Henriques spin_lock(&ci->i_ceph_lock); 330dd980fc0SLuis Henriques issued = __ceph_caps_issued(ci, NULL); 331dd980fc0SLuis Henriques spin_unlock(&ci->i_ceph_lock); 332dd980fc0SLuis Henriques 333dd980fc0SLuis Henriques return ceph_fmt_xattr(val, size, "%s/0x%x", 334dd980fc0SLuis Henriques ceph_cap_string(issued), issued); 335dd980fc0SLuis Henriques } 336dd980fc0SLuis Henriques 337eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name 338695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2) \ 339695b7119SSage Weil XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 340eb788084SAlex Elder 34149a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags) \ 342eb788084SAlex Elder { \ 343eb788084SAlex Elder .name = CEPH_XATTR_NAME(_type, _name), \ 3443ce6cd12SAlex Elder .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ 345aa4066edSAlex Elder .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 346f36e4472SSage Weil .exists_cb = NULL, \ 34749a9f4f6SYan, Zheng .flags = (VXATTR_FLAG_READONLY | _flags), \ 348eb788084SAlex Elder } 34949a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name) \ 35049a9f4f6SYan, Zheng XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT) 351695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ 352695b7119SSage Weil { \ 353695b7119SSage Weil .name = CEPH_XATTR_NAME2(_type, _name, _field), \ 354695b7119SSage Weil .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ 355695b7119SSage Weil .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ 356695b7119SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, \ 3574e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 358695b7119SSage Weil } 359fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name) \ 360fb18a575SLuis Henriques { \ 361fb18a575SLuis Henriques .name = CEPH_XATTR_NAME(_type, _name), \ 362fb18a575SLuis Henriques .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \ 363fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 364fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, \ 3654e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 366fb18a575SLuis Henriques } 367eb788084SAlex Elder 368881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = { 3691f08f2b0SSage Weil { 3701f08f2b0SSage Weil .name = "ceph.dir.layout", 3711f08f2b0SSage Weil .name_size = sizeof("ceph.dir.layout"), 3721f08f2b0SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 3731f08f2b0SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 3744e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 3751f08f2b0SSage Weil }, 376695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), 377695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_count), 378695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, object_size), 379695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, pool), 380779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(dir, layout, pool_namespace), 38181048c00SJeff Layton XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT), 38281048c00SJeff Layton XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT), 38381048c00SJeff Layton XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT), 38449a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rentries), 38549a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rfiles), 38649a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rsubdirs), 38749a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rbytes), 38849a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rctime), 389fb18a575SLuis Henriques { 39008796873SYan, Zheng .name = "ceph.dir.pin", 391e1b81439SDavid Disseldorp .name_size = sizeof("ceph.dir.pin"), 39208796873SYan, Zheng .getxattr_cb = ceph_vxattrcb_dir_pin, 39308796873SYan, Zheng .exists_cb = ceph_vxattrcb_dir_pin_exists, 39408796873SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 39508796873SYan, Zheng }, 39608796873SYan, Zheng { 397fb18a575SLuis Henriques .name = "ceph.quota", 398fb18a575SLuis Henriques .name_size = sizeof("ceph.quota"), 399fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_quota, 400fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, 4014e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 402fb18a575SLuis Henriques }, 403fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_bytes), 404fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_files), 405100cc610SDavid Disseldorp { 406100cc610SDavid Disseldorp .name = "ceph.snap.btime", 407100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 408100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 409100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 410100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 411100cc610SDavid Disseldorp }, 412dd980fc0SLuis Henriques { 413dd980fc0SLuis Henriques .name = "ceph.caps", 414dd980fc0SLuis Henriques .name_size = sizeof("ceph.caps"), 415dd980fc0SLuis Henriques .getxattr_cb = ceph_vxattrcb_caps, 416dd980fc0SLuis Henriques .exists_cb = NULL, 417dd980fc0SLuis Henriques .flags = VXATTR_FLAG_HIDDEN, 418dd980fc0SLuis Henriques }, 4192c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 420355da1ebSSage Weil }; 421355da1ebSSage Weil 422355da1ebSSage Weil /* files */ 423355da1ebSSage Weil 424881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = { 42532ab0bd7SSage Weil { 42632ab0bd7SSage Weil .name = "ceph.file.layout", 42732ab0bd7SSage Weil .name_size = sizeof("ceph.file.layout"), 42832ab0bd7SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 42932ab0bd7SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 4304e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 43132ab0bd7SSage Weil }, 432695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_unit), 433695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_count), 434695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, object_size), 435695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, pool), 436779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(file, layout, pool_namespace), 437100cc610SDavid Disseldorp { 438100cc610SDavid Disseldorp .name = "ceph.snap.btime", 439100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 440100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 441100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 442100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 443100cc610SDavid Disseldorp }, 444dd980fc0SLuis Henriques { 445dd980fc0SLuis Henriques .name = "ceph.caps", 446dd980fc0SLuis Henriques .name_size = sizeof("ceph.caps"), 447dd980fc0SLuis Henriques .getxattr_cb = ceph_vxattrcb_caps, 448dd980fc0SLuis Henriques .exists_cb = NULL, 449dd980fc0SLuis Henriques .flags = VXATTR_FLAG_HIDDEN, 450dd980fc0SLuis Henriques }, 4512c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 452355da1ebSSage Weil }; 453355da1ebSSage Weil 4545a9e2f5dSXiubo Li static struct ceph_vxattr ceph_common_vxattrs[] = { 4555a9e2f5dSXiubo Li { 4565a9e2f5dSXiubo Li .name = "ceph.cluster_fsid", 4575a9e2f5dSXiubo Li .name_size = sizeof("ceph.cluster_fsid"), 4585a9e2f5dSXiubo Li .getxattr_cb = ceph_vxattrcb_cluster_fsid, 4595a9e2f5dSXiubo Li .exists_cb = NULL, 4605a9e2f5dSXiubo Li .flags = VXATTR_FLAG_READONLY, 4615a9e2f5dSXiubo Li }, 4625a9e2f5dSXiubo Li { 4635a9e2f5dSXiubo Li .name = "ceph.client_id", 4645a9e2f5dSXiubo Li .name_size = sizeof("ceph.client_id"), 4655a9e2f5dSXiubo Li .getxattr_cb = ceph_vxattrcb_client_id, 4665a9e2f5dSXiubo Li .exists_cb = NULL, 4675a9e2f5dSXiubo Li .flags = VXATTR_FLAG_READONLY, 4685a9e2f5dSXiubo Li }, 4695a9e2f5dSXiubo Li { .name = NULL, 0 } /* Required table terminator */ 4705a9e2f5dSXiubo Li }; 4715a9e2f5dSXiubo Li 472881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) 473355da1ebSSage Weil { 474355da1ebSSage Weil if (S_ISDIR(inode->i_mode)) 475355da1ebSSage Weil return ceph_dir_vxattrs; 476355da1ebSSage Weil else if (S_ISREG(inode->i_mode)) 477355da1ebSSage Weil return ceph_file_vxattrs; 478355da1ebSSage Weil return NULL; 479355da1ebSSage Weil } 480355da1ebSSage Weil 481881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, 482355da1ebSSage Weil const char *name) 483355da1ebSSage Weil { 484881a5fa2SAlex Elder struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); 48506476a69SAlex Elder 48606476a69SAlex Elder if (vxattr) { 48706476a69SAlex Elder while (vxattr->name) { 48806476a69SAlex Elder if (!strcmp(vxattr->name, name)) 489355da1ebSSage Weil return vxattr; 490355da1ebSSage Weil vxattr++; 49106476a69SAlex Elder } 49206476a69SAlex Elder } 49306476a69SAlex Elder 4945a9e2f5dSXiubo Li vxattr = ceph_common_vxattrs; 4955a9e2f5dSXiubo Li while (vxattr->name) { 4965a9e2f5dSXiubo Li if (!strcmp(vxattr->name, name)) 4975a9e2f5dSXiubo Li return vxattr; 4985a9e2f5dSXiubo Li vxattr++; 4995a9e2f5dSXiubo Li } 5005a9e2f5dSXiubo Li 501355da1ebSSage Weil return NULL; 502355da1ebSSage Weil } 503355da1ebSSage Weil 504355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci, 505355da1ebSSage Weil const char *name, int name_len, 506355da1ebSSage Weil const char *val, int val_len, 507fbc0b970SYan, Zheng int flags, int update_xattr, 508355da1ebSSage Weil struct ceph_inode_xattr **newxattr) 509355da1ebSSage Weil { 510355da1ebSSage Weil struct rb_node **p; 511355da1ebSSage Weil struct rb_node *parent = NULL; 512355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 513355da1ebSSage Weil int c; 514355da1ebSSage Weil int new = 0; 515355da1ebSSage Weil 516355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 517355da1ebSSage Weil while (*p) { 518355da1ebSSage Weil parent = *p; 519355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 520355da1ebSSage Weil c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); 521355da1ebSSage Weil if (c < 0) 522355da1ebSSage Weil p = &(*p)->rb_left; 523355da1ebSSage Weil else if (c > 0) 524355da1ebSSage Weil p = &(*p)->rb_right; 525355da1ebSSage Weil else { 526355da1ebSSage Weil if (name_len == xattr->name_len) 527355da1ebSSage Weil break; 528355da1ebSSage Weil else if (name_len < xattr->name_len) 529355da1ebSSage Weil p = &(*p)->rb_left; 530355da1ebSSage Weil else 531355da1ebSSage Weil p = &(*p)->rb_right; 532355da1ebSSage Weil } 533355da1ebSSage Weil xattr = NULL; 534355da1ebSSage Weil } 535355da1ebSSage Weil 536fbc0b970SYan, Zheng if (update_xattr) { 537fbc0b970SYan, Zheng int err = 0; 538eeca958dSLuis Henriques 539fbc0b970SYan, Zheng if (xattr && (flags & XATTR_CREATE)) 540fbc0b970SYan, Zheng err = -EEXIST; 541fbc0b970SYan, Zheng else if (!xattr && (flags & XATTR_REPLACE)) 542fbc0b970SYan, Zheng err = -ENODATA; 543fbc0b970SYan, Zheng if (err) { 544fbc0b970SYan, Zheng kfree(name); 545fbc0b970SYan, Zheng kfree(val); 546eeca958dSLuis Henriques kfree(*newxattr); 547fbc0b970SYan, Zheng return err; 548fbc0b970SYan, Zheng } 549bcdfeb2eSYan, Zheng if (update_xattr < 0) { 550bcdfeb2eSYan, Zheng if (xattr) 551bcdfeb2eSYan, Zheng __remove_xattr(ci, xattr); 552bcdfeb2eSYan, Zheng kfree(name); 553eeca958dSLuis Henriques kfree(*newxattr); 554bcdfeb2eSYan, Zheng return 0; 555bcdfeb2eSYan, Zheng } 556fbc0b970SYan, Zheng } 557fbc0b970SYan, Zheng 558355da1ebSSage Weil if (!xattr) { 559355da1ebSSage Weil new = 1; 560355da1ebSSage Weil xattr = *newxattr; 561355da1ebSSage Weil xattr->name = name; 562355da1ebSSage Weil xattr->name_len = name_len; 563fbc0b970SYan, Zheng xattr->should_free_name = update_xattr; 564355da1ebSSage Weil 565355da1ebSSage Weil ci->i_xattrs.count++; 566355da1ebSSage Weil dout("__set_xattr count=%d\n", ci->i_xattrs.count); 567355da1ebSSage Weil } else { 568355da1ebSSage Weil kfree(*newxattr); 569355da1ebSSage Weil *newxattr = NULL; 570355da1ebSSage Weil if (xattr->should_free_val) 571c00e4522SXu Wang kfree(xattr->val); 572355da1ebSSage Weil 573fbc0b970SYan, Zheng if (update_xattr) { 574c00e4522SXu Wang kfree(name); 575355da1ebSSage Weil name = xattr->name; 576355da1ebSSage Weil } 577355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 578355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 579355da1ebSSage Weil } 580355da1ebSSage Weil ci->i_xattrs.names_size += name_len; 581355da1ebSSage Weil ci->i_xattrs.vals_size += val_len; 582355da1ebSSage Weil if (val) 583355da1ebSSage Weil xattr->val = val; 584355da1ebSSage Weil else 585355da1ebSSage Weil xattr->val = ""; 586355da1ebSSage Weil 587355da1ebSSage Weil xattr->val_len = val_len; 588fbc0b970SYan, Zheng xattr->dirty = update_xattr; 589fbc0b970SYan, Zheng xattr->should_free_val = (val && update_xattr); 590355da1ebSSage Weil 591355da1ebSSage Weil if (new) { 592355da1ebSSage Weil rb_link_node(&xattr->node, parent, p); 593355da1ebSSage Weil rb_insert_color(&xattr->node, &ci->i_xattrs.index); 594355da1ebSSage Weil dout("__set_xattr_val p=%p\n", p); 595355da1ebSSage Weil } 596355da1ebSSage Weil 59705729781SYan, Zheng dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n", 59805729781SYan, Zheng ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val); 599355da1ebSSage Weil 600355da1ebSSage Weil return 0; 601355da1ebSSage Weil } 602355da1ebSSage Weil 603355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, 604355da1ebSSage Weil const char *name) 605355da1ebSSage Weil { 606355da1ebSSage Weil struct rb_node **p; 607355da1ebSSage Weil struct rb_node *parent = NULL; 608355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 60917db143fSSage Weil int name_len = strlen(name); 610355da1ebSSage Weil int c; 611355da1ebSSage Weil 612355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 613355da1ebSSage Weil while (*p) { 614355da1ebSSage Weil parent = *p; 615355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 616355da1ebSSage Weil c = strncmp(name, xattr->name, xattr->name_len); 61717db143fSSage Weil if (c == 0 && name_len > xattr->name_len) 61817db143fSSage Weil c = 1; 619355da1ebSSage Weil if (c < 0) 620355da1ebSSage Weil p = &(*p)->rb_left; 621355da1ebSSage Weil else if (c > 0) 622355da1ebSSage Weil p = &(*p)->rb_right; 623355da1ebSSage Weil else { 624355da1ebSSage Weil dout("__get_xattr %s: found %.*s\n", name, 625355da1ebSSage Weil xattr->val_len, xattr->val); 626355da1ebSSage Weil return xattr; 627355da1ebSSage Weil } 628355da1ebSSage Weil } 629355da1ebSSage Weil 630355da1ebSSage Weil dout("__get_xattr %s: not found\n", name); 631355da1ebSSage Weil 632355da1ebSSage Weil return NULL; 633355da1ebSSage Weil } 634355da1ebSSage Weil 635355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr) 636355da1ebSSage Weil { 637355da1ebSSage Weil BUG_ON(!xattr); 638355da1ebSSage Weil 639355da1ebSSage Weil if (xattr->should_free_name) 640c00e4522SXu Wang kfree(xattr->name); 641355da1ebSSage Weil if (xattr->should_free_val) 642c00e4522SXu Wang kfree(xattr->val); 643355da1ebSSage Weil 644355da1ebSSage Weil kfree(xattr); 645355da1ebSSage Weil } 646355da1ebSSage Weil 647355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci, 648355da1ebSSage Weil struct ceph_inode_xattr *xattr) 649355da1ebSSage Weil { 650355da1ebSSage Weil if (!xattr) 651524186acSYan, Zheng return -ENODATA; 652355da1ebSSage Weil 653355da1ebSSage Weil rb_erase(&xattr->node, &ci->i_xattrs.index); 654355da1ebSSage Weil 655355da1ebSSage Weil if (xattr->should_free_name) 656c00e4522SXu Wang kfree(xattr->name); 657355da1ebSSage Weil if (xattr->should_free_val) 658c00e4522SXu Wang kfree(xattr->val); 659355da1ebSSage Weil 660355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 661355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 662355da1ebSSage Weil ci->i_xattrs.count--; 663355da1ebSSage Weil kfree(xattr); 664355da1ebSSage Weil 665355da1ebSSage Weil return 0; 666355da1ebSSage Weil } 667355da1ebSSage Weil 668355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci, 669355da1ebSSage Weil char *dest) 670355da1ebSSage Weil { 671355da1ebSSage Weil struct rb_node *p; 672355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 673355da1ebSSage Weil 674355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 675355da1ebSSage Weil dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); 676355da1ebSSage Weil 677355da1ebSSage Weil while (p) { 678355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 679355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 680355da1ebSSage Weil dest[xattr->name_len] = '\0'; 681355da1ebSSage Weil 682355da1ebSSage Weil dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, 683355da1ebSSage Weil xattr->name_len, ci->i_xattrs.names_size); 684355da1ebSSage Weil 685355da1ebSSage Weil dest += xattr->name_len + 1; 686355da1ebSSage Weil p = rb_next(p); 687355da1ebSSage Weil } 688355da1ebSSage Weil 689355da1ebSSage Weil return dest; 690355da1ebSSage Weil } 691355da1ebSSage Weil 692355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci) 693355da1ebSSage Weil { 694355da1ebSSage Weil struct rb_node *p, *tmp; 695355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 696355da1ebSSage Weil 697355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 698355da1ebSSage Weil 699355da1ebSSage Weil dout("__ceph_destroy_xattrs p=%p\n", p); 700355da1ebSSage Weil 701355da1ebSSage Weil while (p) { 702355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 703355da1ebSSage Weil tmp = p; 704355da1ebSSage Weil p = rb_next(tmp); 705355da1ebSSage Weil dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, 706355da1ebSSage Weil xattr->name_len, xattr->name); 707355da1ebSSage Weil rb_erase(tmp, &ci->i_xattrs.index); 708355da1ebSSage Weil 709355da1ebSSage Weil __free_xattr(xattr); 710355da1ebSSage Weil } 711355da1ebSSage Weil 712355da1ebSSage Weil ci->i_xattrs.names_size = 0; 713355da1ebSSage Weil ci->i_xattrs.vals_size = 0; 714355da1ebSSage Weil ci->i_xattrs.index_version = 0; 715355da1ebSSage Weil ci->i_xattrs.count = 0; 716355da1ebSSage Weil ci->i_xattrs.index = RB_ROOT; 717355da1ebSSage Weil } 718355da1ebSSage Weil 719355da1ebSSage Weil static int __build_xattrs(struct inode *inode) 720be655596SSage Weil __releases(ci->i_ceph_lock) 721be655596SSage Weil __acquires(ci->i_ceph_lock) 722355da1ebSSage Weil { 723355da1ebSSage Weil u32 namelen; 724355da1ebSSage Weil u32 numattr = 0; 725355da1ebSSage Weil void *p, *end; 726355da1ebSSage Weil u32 len; 727355da1ebSSage Weil const char *name, *val; 728355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 7290eb30853SXiubo Li u64 xattr_version; 730355da1ebSSage Weil struct ceph_inode_xattr **xattrs = NULL; 73163ff78b2SSage Weil int err = 0; 732355da1ebSSage Weil int i; 733355da1ebSSage Weil 734355da1ebSSage Weil dout("__build_xattrs() len=%d\n", 735355da1ebSSage Weil ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); 736355da1ebSSage Weil 737355da1ebSSage Weil if (ci->i_xattrs.index_version >= ci->i_xattrs.version) 738355da1ebSSage Weil return 0; /* already built */ 739355da1ebSSage Weil 740355da1ebSSage Weil __ceph_destroy_xattrs(ci); 741355da1ebSSage Weil 742355da1ebSSage Weil start: 743355da1ebSSage Weil /* updated internal xattr rb tree */ 744355da1ebSSage Weil if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { 745355da1ebSSage Weil p = ci->i_xattrs.blob->vec.iov_base; 746355da1ebSSage Weil end = p + ci->i_xattrs.blob->vec.iov_len; 747355da1ebSSage Weil ceph_decode_32_safe(&p, end, numattr, bad); 748355da1ebSSage Weil xattr_version = ci->i_xattrs.version; 749be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 750355da1ebSSage Weil 7517e8a2952SIlya Dryomov xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), 752355da1ebSSage Weil GFP_NOFS); 753355da1ebSSage Weil err = -ENOMEM; 754355da1ebSSage Weil if (!xattrs) 755355da1ebSSage Weil goto bad_lock; 7561a295bd8SIlya Dryomov 757355da1ebSSage Weil for (i = 0; i < numattr; i++) { 758355da1ebSSage Weil xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), 759355da1ebSSage Weil GFP_NOFS); 760355da1ebSSage Weil if (!xattrs[i]) 761355da1ebSSage Weil goto bad_lock; 762355da1ebSSage Weil } 763355da1ebSSage Weil 764be655596SSage Weil spin_lock(&ci->i_ceph_lock); 765355da1ebSSage Weil if (ci->i_xattrs.version != xattr_version) { 766355da1ebSSage Weil /* lost a race, retry */ 767355da1ebSSage Weil for (i = 0; i < numattr; i++) 768355da1ebSSage Weil kfree(xattrs[i]); 769355da1ebSSage Weil kfree(xattrs); 77021ec6ffaSAlan Cox xattrs = NULL; 771355da1ebSSage Weil goto start; 772355da1ebSSage Weil } 773355da1ebSSage Weil err = -EIO; 774355da1ebSSage Weil while (numattr--) { 775355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 776355da1ebSSage Weil namelen = len; 777355da1ebSSage Weil name = p; 778355da1ebSSage Weil p += len; 779355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 780355da1ebSSage Weil val = p; 781355da1ebSSage Weil p += len; 782355da1ebSSage Weil 783355da1ebSSage Weil err = __set_xattr(ci, name, namelen, val, len, 784fbc0b970SYan, Zheng 0, 0, &xattrs[numattr]); 785355da1ebSSage Weil 786355da1ebSSage Weil if (err < 0) 787355da1ebSSage Weil goto bad; 788355da1ebSSage Weil } 789355da1ebSSage Weil kfree(xattrs); 790355da1ebSSage Weil } 791355da1ebSSage Weil ci->i_xattrs.index_version = ci->i_xattrs.version; 792355da1ebSSage Weil ci->i_xattrs.dirty = false; 793355da1ebSSage Weil 794355da1ebSSage Weil return err; 795355da1ebSSage Weil bad_lock: 796be655596SSage Weil spin_lock(&ci->i_ceph_lock); 797355da1ebSSage Weil bad: 798355da1ebSSage Weil if (xattrs) { 799355da1ebSSage Weil for (i = 0; i < numattr; i++) 800355da1ebSSage Weil kfree(xattrs[i]); 801355da1ebSSage Weil kfree(xattrs); 802355da1ebSSage Weil } 803355da1ebSSage Weil ci->i_xattrs.names_size = 0; 804355da1ebSSage Weil return err; 805355da1ebSSage Weil } 806355da1ebSSage Weil 807355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, 808355da1ebSSage Weil int val_size) 809355da1ebSSage Weil { 810355da1ebSSage Weil /* 811355da1ebSSage Weil * 4 bytes for the length, and additional 4 bytes per each xattr name, 812355da1ebSSage Weil * 4 bytes per each value 813355da1ebSSage Weil */ 814355da1ebSSage Weil int size = 4 + ci->i_xattrs.count*(4 + 4) + 815355da1ebSSage Weil ci->i_xattrs.names_size + 816355da1ebSSage Weil ci->i_xattrs.vals_size; 817355da1ebSSage Weil dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", 818355da1ebSSage Weil ci->i_xattrs.count, ci->i_xattrs.names_size, 819355da1ebSSage Weil ci->i_xattrs.vals_size); 820355da1ebSSage Weil 821355da1ebSSage Weil if (name_size) 822355da1ebSSage Weil size += 4 + 4 + name_size + val_size; 823355da1ebSSage Weil 824355da1ebSSage Weil return size; 825355da1ebSSage Weil } 826355da1ebSSage Weil 827355da1ebSSage Weil /* 828355da1ebSSage Weil * If there are dirty xattrs, reencode xattrs into the prealloc_blob 82912fe3ddaSLuis Henriques * and swap into place. It returns the old i_xattrs.blob (or NULL) so 83012fe3ddaSLuis Henriques * that it can be freed by the caller as the i_ceph_lock is likely to be 83112fe3ddaSLuis Henriques * held. 832355da1ebSSage Weil */ 83312fe3ddaSLuis Henriques struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci) 834355da1ebSSage Weil { 835355da1ebSSage Weil struct rb_node *p; 836355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 83712fe3ddaSLuis Henriques struct ceph_buffer *old_blob = NULL; 838355da1ebSSage Weil void *dest; 839355da1ebSSage Weil 840355da1ebSSage Weil dout("__build_xattrs_blob %p\n", &ci->vfs_inode); 841355da1ebSSage Weil if (ci->i_xattrs.dirty) { 842355da1ebSSage Weil int need = __get_required_blob_size(ci, 0, 0); 843355da1ebSSage Weil 844355da1ebSSage Weil BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); 845355da1ebSSage Weil 846355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 847355da1ebSSage Weil dest = ci->i_xattrs.prealloc_blob->vec.iov_base; 848355da1ebSSage Weil 849355da1ebSSage Weil ceph_encode_32(&dest, ci->i_xattrs.count); 850355da1ebSSage Weil while (p) { 851355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 852355da1ebSSage Weil 853355da1ebSSage Weil ceph_encode_32(&dest, xattr->name_len); 854355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 855355da1ebSSage Weil dest += xattr->name_len; 856355da1ebSSage Weil ceph_encode_32(&dest, xattr->val_len); 857355da1ebSSage Weil memcpy(dest, xattr->val, xattr->val_len); 858355da1ebSSage Weil dest += xattr->val_len; 859355da1ebSSage Weil 860355da1ebSSage Weil p = rb_next(p); 861355da1ebSSage Weil } 862355da1ebSSage Weil 863355da1ebSSage Weil /* adjust buffer len; it may be larger than we need */ 864355da1ebSSage Weil ci->i_xattrs.prealloc_blob->vec.iov_len = 865355da1ebSSage Weil dest - ci->i_xattrs.prealloc_blob->vec.iov_base; 866355da1ebSSage Weil 867b6c1d5b8SSage Weil if (ci->i_xattrs.blob) 86812fe3ddaSLuis Henriques old_blob = ci->i_xattrs.blob; 869355da1ebSSage Weil ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; 870355da1ebSSage Weil ci->i_xattrs.prealloc_blob = NULL; 871355da1ebSSage Weil ci->i_xattrs.dirty = false; 8724a625be4SSage Weil ci->i_xattrs.version++; 873355da1ebSSage Weil } 87412fe3ddaSLuis Henriques 87512fe3ddaSLuis Henriques return old_blob; 876355da1ebSSage Weil } 877355da1ebSSage Weil 878315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) { 879315f2408SYan, Zheng struct ceph_mds_request *req = current->journal_info; 880315f2408SYan, Zheng int mask = 0; 881315f2408SYan, Zheng if (req && req->r_target_inode == in) { 882315f2408SYan, Zheng if (req->r_op == CEPH_MDS_OP_LOOKUP || 883315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPINO || 884315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPPARENT || 885315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_GETATTR) { 886315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.getattr.mask); 887315f2408SYan, Zheng } else if (req->r_op == CEPH_MDS_OP_OPEN || 888315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_CREATE) { 889315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.open.mask); 890315f2408SYan, Zheng } 891315f2408SYan, Zheng } 892315f2408SYan, Zheng return mask; 893315f2408SYan, Zheng } 894315f2408SYan, Zheng 8957221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, 896355da1ebSSage Weil size_t size) 897355da1ebSSage Weil { 898355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 899355da1ebSSage Weil struct ceph_inode_xattr *xattr; 900881a5fa2SAlex Elder struct ceph_vxattr *vxattr = NULL; 901315f2408SYan, Zheng int req_mask; 902f1d1b51dSJeff Layton ssize_t err; 903355da1ebSSage Weil 9040bee82fbSSage Weil /* let's see if a virtual xattr was requested */ 9050bee82fbSSage Weil vxattr = ceph_match_vxattr(inode, name); 90629dccfa5SYan, Zheng if (vxattr) { 90749a9f4f6SYan, Zheng int mask = 0; 90849a9f4f6SYan, Zheng if (vxattr->flags & VXATTR_FLAG_RSTAT) 90949a9f4f6SYan, Zheng mask |= CEPH_STAT_RSTAT; 91081048c00SJeff Layton if (vxattr->flags & VXATTR_FLAG_DIRSTAT) 91181048c00SJeff Layton mask |= CEPH_CAP_FILE_SHARED; 91249a9f4f6SYan, Zheng err = ceph_do_getattr(inode, mask, true); 9131684dd03SYan, Zheng if (err) 9141684dd03SYan, Zheng return err; 91529dccfa5SYan, Zheng err = -ENODATA; 9163b421018SJeff Layton if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) { 9170bee82fbSSage Weil err = vxattr->getxattr_cb(ci, value, size); 9183b421018SJeff Layton if (size && size < err) 9193b421018SJeff Layton err = -ERANGE; 9203b421018SJeff Layton } 921a1dc1937Smajianpeng return err; 9220bee82fbSSage Weil } 9230bee82fbSSage Weil 924315f2408SYan, Zheng req_mask = __get_request_mask(inode); 925315f2408SYan, Zheng 926a1dc1937Smajianpeng spin_lock(&ci->i_ceph_lock); 927d36e0b62SJeff Layton dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name, 928a1dc1937Smajianpeng ci->i_xattrs.version, ci->i_xattrs.index_version); 929a1dc1937Smajianpeng 930508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 931315f2408SYan, Zheng !((req_mask & CEPH_CAP_XATTR_SHARED) || 9321af16d54SXiubo Li __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) { 933be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 934315f2408SYan, Zheng 935315f2408SYan, Zheng /* security module gets xattr while filling trace */ 936d37b1d99SMarkus Elfring if (current->journal_info) { 937315f2408SYan, Zheng pr_warn_ratelimited("sync getxattr %p " 938315f2408SYan, Zheng "during filling trace\n", inode); 939315f2408SYan, Zheng return -EBUSY; 940315f2408SYan, Zheng } 941315f2408SYan, Zheng 942355da1ebSSage Weil /* get xattrs from mds (if we don't already have them) */ 943508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); 944355da1ebSSage Weil if (err) 945355da1ebSSage Weil return err; 946be655596SSage Weil spin_lock(&ci->i_ceph_lock); 947508b32d8SYan, Zheng } 948355da1ebSSage Weil 949355da1ebSSage Weil err = __build_xattrs(inode); 950355da1ebSSage Weil if (err < 0) 951355da1ebSSage Weil goto out; 952355da1ebSSage Weil 953355da1ebSSage Weil err = -ENODATA; /* == ENOATTR */ 954355da1ebSSage Weil xattr = __get_xattr(ci, name); 9550bee82fbSSage Weil if (!xattr) 956355da1ebSSage Weil goto out; 957355da1ebSSage Weil 958355da1ebSSage Weil err = -ERANGE; 959355da1ebSSage Weil if (size && size < xattr->val_len) 960355da1ebSSage Weil goto out; 961355da1ebSSage Weil 962355da1ebSSage Weil err = xattr->val_len; 963355da1ebSSage Weil if (size == 0) 964355da1ebSSage Weil goto out; 965355da1ebSSage Weil 966355da1ebSSage Weil memcpy(value, xattr->val, xattr->val_len); 967355da1ebSSage Weil 968d37b1d99SMarkus Elfring if (current->journal_info && 969026105ebSJeff Layton !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) && 970026105ebSJeff Layton security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN)) 971315f2408SYan, Zheng ci->i_ceph_flags |= CEPH_I_SEC_INITED; 972355da1ebSSage Weil out: 973be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 974355da1ebSSage Weil return err; 975355da1ebSSage Weil } 976355da1ebSSage Weil 977355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) 978355da1ebSSage Weil { 9792b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 980355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 9812b2abcacSDavid Disseldorp bool len_only = (size == 0); 982355da1ebSSage Weil u32 namelen; 983355da1ebSSage Weil int err; 984355da1ebSSage Weil 985be655596SSage Weil spin_lock(&ci->i_ceph_lock); 986355da1ebSSage Weil dout("listxattr %p ver=%lld index_ver=%lld\n", inode, 987355da1ebSSage Weil ci->i_xattrs.version, ci->i_xattrs.index_version); 988355da1ebSSage Weil 989508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 9901af16d54SXiubo Li !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) { 991be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 992508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); 993355da1ebSSage Weil if (err) 994355da1ebSSage Weil return err; 995be655596SSage Weil spin_lock(&ci->i_ceph_lock); 996508b32d8SYan, Zheng } 997355da1ebSSage Weil 998355da1ebSSage Weil err = __build_xattrs(inode); 999355da1ebSSage Weil if (err < 0) 1000355da1ebSSage Weil goto out; 10013ce6cd12SAlex Elder 10022b2abcacSDavid Disseldorp /* add 1 byte for each xattr due to the null termination */ 1003b65917ddSSage Weil namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; 10042b2abcacSDavid Disseldorp if (!len_only) { 10052b2abcacSDavid Disseldorp if (namelen > size) { 1006355da1ebSSage Weil err = -ERANGE; 1007355da1ebSSage Weil goto out; 10082b2abcacSDavid Disseldorp } 1009355da1ebSSage Weil names = __copy_xattr_names(ci, names); 10102b2abcacSDavid Disseldorp size -= namelen; 10112b2abcacSDavid Disseldorp } 10122b2abcacSDavid Disseldorp err = namelen; 1013355da1ebSSage Weil out: 1014be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1015355da1ebSSage Weil return err; 1016355da1ebSSage Weil } 1017355da1ebSSage Weil 1018a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name, 1019355da1ebSSage Weil const char *value, size_t size, int flags) 1020355da1ebSSage Weil { 1021a26feccaSAndreas Gruenbacher struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 1022355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1023355da1ebSSage Weil struct ceph_mds_request *req; 10243d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 1025*968cd14eSXiubo Li struct ceph_osd_client *osdc = &fsc->client->osdc; 102625e6bae3SYan, Zheng struct ceph_pagelist *pagelist = NULL; 102704303d8aSYan, Zheng int op = CEPH_MDS_OP_SETXATTR; 1028355da1ebSSage Weil int err; 1029355da1ebSSage Weil 10300aeff37aSYan, Zheng if (size > 0) { 103125e6bae3SYan, Zheng /* copy value into pagelist */ 103233165d47SIlya Dryomov pagelist = ceph_pagelist_alloc(GFP_NOFS); 103325e6bae3SYan, Zheng if (!pagelist) 1034355da1ebSSage Weil return -ENOMEM; 103525e6bae3SYan, Zheng 103625e6bae3SYan, Zheng err = ceph_pagelist_append(pagelist, value, size); 103725e6bae3SYan, Zheng if (err) 1038355da1ebSSage Weil goto out; 10390aeff37aSYan, Zheng } else if (!value) { 104004303d8aSYan, Zheng if (flags & CEPH_XATTR_REPLACE) 104104303d8aSYan, Zheng op = CEPH_MDS_OP_RMXATTR; 104204303d8aSYan, Zheng else 104325e6bae3SYan, Zheng flags |= CEPH_XATTR_REMOVE; 1044355da1ebSSage Weil } 1045355da1ebSSage Weil 1046355da1ebSSage Weil dout("setxattr value=%.*s\n", (int)size, value); 1047355da1ebSSage Weil 1048355da1ebSSage Weil /* do request */ 104904303d8aSYan, Zheng req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); 105060d87733SJulia Lawall if (IS_ERR(req)) { 105160d87733SJulia Lawall err = PTR_ERR(req); 105260d87733SJulia Lawall goto out; 105360d87733SJulia Lawall } 1054a149bb9aSSanidhya Kashyap 1055355da1ebSSage Weil req->r_path2 = kstrdup(name, GFP_NOFS); 1056a149bb9aSSanidhya Kashyap if (!req->r_path2) { 1057a149bb9aSSanidhya Kashyap ceph_mdsc_put_request(req); 1058a149bb9aSSanidhya Kashyap err = -ENOMEM; 1059a149bb9aSSanidhya Kashyap goto out; 1060a149bb9aSSanidhya Kashyap } 1061355da1ebSSage Weil 106204303d8aSYan, Zheng if (op == CEPH_MDS_OP_SETXATTR) { 106304303d8aSYan, Zheng req->r_args.setxattr.flags = cpu_to_le32(flags); 1064*968cd14eSXiubo Li req->r_args.setxattr.osdmap_epoch = 1065*968cd14eSXiubo Li cpu_to_le32(osdc->osdmap->epoch); 106625e6bae3SYan, Zheng req->r_pagelist = pagelist; 106725e6bae3SYan, Zheng pagelist = NULL; 106804303d8aSYan, Zheng } 1069355da1ebSSage Weil 1070a149bb9aSSanidhya Kashyap req->r_inode = inode; 1071a149bb9aSSanidhya Kashyap ihold(inode); 1072a149bb9aSSanidhya Kashyap req->r_num_caps = 1; 1073a149bb9aSSanidhya Kashyap req->r_inode_drop = CEPH_CAP_XATTR_SHARED; 1074a149bb9aSSanidhya Kashyap 1075355da1ebSSage Weil dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); 1076752c8bdcSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 1077355da1ebSSage Weil ceph_mdsc_put_request(req); 1078355da1ebSSage Weil dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); 1079355da1ebSSage Weil 1080355da1ebSSage Weil out: 108125e6bae3SYan, Zheng if (pagelist) 108225e6bae3SYan, Zheng ceph_pagelist_release(pagelist); 1083355da1ebSSage Weil return err; 1084355da1ebSSage Weil } 1085355da1ebSSage Weil 1086a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name, 1087355da1ebSSage Weil const void *value, size_t size, int flags) 1088355da1ebSSage Weil { 1089881a5fa2SAlex Elder struct ceph_vxattr *vxattr; 1090355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1091a26feccaSAndreas Gruenbacher struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 1092f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf = NULL; 109386968ef2SLuis Henriques struct ceph_buffer *old_blob = NULL; 109418fa8b3fSAlex Elder int issued; 1095355da1ebSSage Weil int err; 1096fbc0b970SYan, Zheng int dirty = 0; 1097355da1ebSSage Weil int name_len = strlen(name); 1098355da1ebSSage Weil int val_len = size; 1099355da1ebSSage Weil char *newname = NULL; 1100355da1ebSSage Weil char *newval = NULL; 1101355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 1102355da1ebSSage Weil int required_blob_size; 1103f1919826SYan, Zheng bool check_realm = false; 1104604d1b02SYan, Zheng bool lock_snap_rwsem = false; 1105355da1ebSSage Weil 11062cdeb1e4SAndreas Gruenbacher if (ceph_snap(inode) != CEPH_NOSNAP) 11072cdeb1e4SAndreas Gruenbacher return -EROFS; 1108355da1ebSSage Weil 110906476a69SAlex Elder vxattr = ceph_match_vxattr(inode, name); 1110f1919826SYan, Zheng if (vxattr) { 11114e9906e7SYan, Zheng if (vxattr->flags & VXATTR_FLAG_READONLY) 1112355da1ebSSage Weil return -EOPNOTSUPP; 1113f1919826SYan, Zheng if (value && !strncmp(vxattr->name, "ceph.quota", 10)) 1114f1919826SYan, Zheng check_realm = true; 1115f1919826SYan, Zheng } 1116355da1ebSSage Weil 11173adf654dSSage Weil /* pass any unhandled ceph.* xattrs through to the MDS */ 11183adf654dSSage Weil if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) 11193adf654dSSage Weil goto do_sync_unlocked; 11203adf654dSSage Weil 1121355da1ebSSage Weil /* preallocate memory for xattr name, value, index node */ 1122355da1ebSSage Weil err = -ENOMEM; 112361413c2fSJulia Lawall newname = kmemdup(name, name_len + 1, GFP_NOFS); 1124355da1ebSSage Weil if (!newname) 1125355da1ebSSage Weil goto out; 1126355da1ebSSage Weil 1127355da1ebSSage Weil if (val_len) { 1128b829c195SAlex Elder newval = kmemdup(value, val_len, GFP_NOFS); 1129355da1ebSSage Weil if (!newval) 1130355da1ebSSage Weil goto out; 1131355da1ebSSage Weil } 1132355da1ebSSage Weil 1133355da1ebSSage Weil xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); 1134355da1ebSSage Weil if (!xattr) 1135355da1ebSSage Weil goto out; 1136355da1ebSSage Weil 1137f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1138f66fd9f0SYan, Zheng if (!prealloc_cf) 1139f66fd9f0SYan, Zheng goto out; 1140f66fd9f0SYan, Zheng 1141be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1142355da1ebSSage Weil retry: 1143355da1ebSSage Weil issued = __ceph_caps_issued(ci, NULL); 1144508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) 1145355da1ebSSage Weil goto do_sync; 1146604d1b02SYan, Zheng 1147604d1b02SYan, Zheng if (!lock_snap_rwsem && !ci->i_head_snapc) { 1148604d1b02SYan, Zheng lock_snap_rwsem = true; 1149604d1b02SYan, Zheng if (!down_read_trylock(&mdsc->snap_rwsem)) { 1150604d1b02SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1151604d1b02SYan, Zheng down_read(&mdsc->snap_rwsem); 1152604d1b02SYan, Zheng spin_lock(&ci->i_ceph_lock); 1153604d1b02SYan, Zheng goto retry; 1154604d1b02SYan, Zheng } 1155604d1b02SYan, Zheng } 1156604d1b02SYan, Zheng 1157d36e0b62SJeff Layton dout("setxattr %p name '%s' issued %s\n", inode, name, 1158d36e0b62SJeff Layton ceph_cap_string(issued)); 1159355da1ebSSage Weil __build_xattrs(inode); 1160355da1ebSSage Weil 1161355da1ebSSage Weil required_blob_size = __get_required_blob_size(ci, name_len, val_len); 1162355da1ebSSage Weil 1163355da1ebSSage Weil if (!ci->i_xattrs.prealloc_blob || 1164355da1ebSSage Weil required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { 116518fa8b3fSAlex Elder struct ceph_buffer *blob; 1166355da1ebSSage Weil 1167be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 116886968ef2SLuis Henriques ceph_buffer_put(old_blob); /* Shouldn't be required */ 116986968ef2SLuis Henriques dout(" pre-allocating new blob size=%d\n", required_blob_size); 1170b6c1d5b8SSage Weil blob = ceph_buffer_new(required_blob_size, GFP_NOFS); 1171355da1ebSSage Weil if (!blob) 1172604d1b02SYan, Zheng goto do_sync_unlocked; 1173be655596SSage Weil spin_lock(&ci->i_ceph_lock); 117486968ef2SLuis Henriques /* prealloc_blob can't be released while holding i_ceph_lock */ 1175b6c1d5b8SSage Weil if (ci->i_xattrs.prealloc_blob) 117686968ef2SLuis Henriques old_blob = ci->i_xattrs.prealloc_blob; 1177355da1ebSSage Weil ci->i_xattrs.prealloc_blob = blob; 1178355da1ebSSage Weil goto retry; 1179355da1ebSSage Weil } 1180355da1ebSSage Weil 1181bcdfeb2eSYan, Zheng err = __set_xattr(ci, newname, name_len, newval, val_len, 1182bcdfeb2eSYan, Zheng flags, value ? 1 : -1, &xattr); 118318fa8b3fSAlex Elder 1184fbc0b970SYan, Zheng if (!err) { 1185f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, 1186f66fd9f0SYan, Zheng &prealloc_cf); 1187355da1ebSSage Weil ci->i_xattrs.dirty = true; 1188c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 1189fbc0b970SYan, Zheng } 119018fa8b3fSAlex Elder 1191be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 119286968ef2SLuis Henriques ceph_buffer_put(old_blob); 1193604d1b02SYan, Zheng if (lock_snap_rwsem) 1194604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1195fca65b4aSSage Weil if (dirty) 1196fca65b4aSSage Weil __mark_inode_dirty(inode, dirty); 1197f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1198355da1ebSSage Weil return err; 1199355da1ebSSage Weil 1200355da1ebSSage Weil do_sync: 1201be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 12023adf654dSSage Weil do_sync_unlocked: 1203604d1b02SYan, Zheng if (lock_snap_rwsem) 1204604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1205315f2408SYan, Zheng 1206315f2408SYan, Zheng /* security module set xattr while filling trace */ 1207d37b1d99SMarkus Elfring if (current->journal_info) { 1208315f2408SYan, Zheng pr_warn_ratelimited("sync setxattr %p " 1209315f2408SYan, Zheng "during filling trace\n", inode); 1210315f2408SYan, Zheng err = -EBUSY; 1211315f2408SYan, Zheng } else { 1212a26feccaSAndreas Gruenbacher err = ceph_sync_setxattr(inode, name, value, size, flags); 1213f1919826SYan, Zheng if (err >= 0 && check_realm) { 1214f1919826SYan, Zheng /* check if snaprealm was created for quota inode */ 1215f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 1216f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 1217f1919826SYan, Zheng !(ci->i_snap_realm && 1218f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino)) 1219f1919826SYan, Zheng err = -EOPNOTSUPP; 1220f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1221f1919826SYan, Zheng } 1222315f2408SYan, Zheng } 1223355da1ebSSage Weil out: 1224f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1225355da1ebSSage Weil kfree(newname); 1226355da1ebSSage Weil kfree(newval); 1227355da1ebSSage Weil kfree(xattr); 1228355da1ebSSage Weil return err; 1229355da1ebSSage Weil } 1230355da1ebSSage Weil 12312cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler, 12322cdeb1e4SAndreas Gruenbacher struct dentry *dentry, struct inode *inode, 12332cdeb1e4SAndreas Gruenbacher const char *name, void *value, size_t size) 12347221fe4cSGuangliang Zhao { 12352cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 12362cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 12372cdeb1e4SAndreas Gruenbacher return __ceph_getxattr(inode, name, value, size); 12387221fe4cSGuangliang Zhao } 1239315f2408SYan, Zheng 12402cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler, 124159301226SAl Viro struct dentry *unused, struct inode *inode, 124259301226SAl Viro const char *name, const void *value, 124359301226SAl Viro size_t size, int flags) 12442cdeb1e4SAndreas Gruenbacher { 12452cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 12462cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 124759301226SAl Viro return __ceph_setxattr(inode, name, value, size, flags); 12482cdeb1e4SAndreas Gruenbacher } 12492cdeb1e4SAndreas Gruenbacher 12505130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = { 12512cdeb1e4SAndreas Gruenbacher .prefix = "", /* match any name => handlers called with full name */ 12522cdeb1e4SAndreas Gruenbacher .get = ceph_get_xattr_handler, 12532cdeb1e4SAndreas Gruenbacher .set = ceph_set_xattr_handler, 12542cdeb1e4SAndreas Gruenbacher }; 12552cdeb1e4SAndreas Gruenbacher 1256315f2408SYan, Zheng #ifdef CONFIG_SECURITY 1257315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in) 1258315f2408SYan, Zheng { 1259315f2408SYan, Zheng return in->i_security != NULL; 1260315f2408SYan, Zheng } 1261315f2408SYan, Zheng 1262315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in) 1263315f2408SYan, Zheng { 1264315f2408SYan, Zheng struct ceph_inode_info *ci; 1265315f2408SYan, Zheng bool ret; 1266d37b1d99SMarkus Elfring if (!in->i_security) 1267315f2408SYan, Zheng return false; 1268315f2408SYan, Zheng ci = ceph_inode(in); 1269315f2408SYan, Zheng spin_lock(&ci->i_ceph_lock); 1270315f2408SYan, Zheng ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && 1271315f2408SYan, Zheng !(ci->i_xattrs.version > 0 && 1272315f2408SYan, Zheng __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); 1273315f2408SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1274315f2408SYan, Zheng return ret; 1275315f2408SYan, Zheng } 1276ac6713ccSYan, Zheng 1277ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1278ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode, 1279ac6713ccSYan, Zheng struct ceph_acl_sec_ctx *as_ctx) 1280ac6713ccSYan, Zheng { 1281ac6713ccSYan, Zheng struct ceph_pagelist *pagelist = as_ctx->pagelist; 1282ac6713ccSYan, Zheng const char *name; 1283ac6713ccSYan, Zheng size_t name_len; 1284ac6713ccSYan, Zheng int err; 1285ac6713ccSYan, Zheng 1286ac6713ccSYan, Zheng err = security_dentry_init_security(dentry, mode, &dentry->d_name, 1287ac6713ccSYan, Zheng &as_ctx->sec_ctx, 1288ac6713ccSYan, Zheng &as_ctx->sec_ctxlen); 1289ac6713ccSYan, Zheng if (err < 0) { 1290ac6713ccSYan, Zheng WARN_ON_ONCE(err != -EOPNOTSUPP); 1291ac6713ccSYan, Zheng err = 0; /* do nothing */ 1292ac6713ccSYan, Zheng goto out; 1293ac6713ccSYan, Zheng } 1294ac6713ccSYan, Zheng 1295ac6713ccSYan, Zheng err = -ENOMEM; 1296ac6713ccSYan, Zheng if (!pagelist) { 1297ac6713ccSYan, Zheng pagelist = ceph_pagelist_alloc(GFP_KERNEL); 1298ac6713ccSYan, Zheng if (!pagelist) 1299ac6713ccSYan, Zheng goto out; 1300ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 1301ac6713ccSYan, Zheng if (err) 1302ac6713ccSYan, Zheng goto out; 1303ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, 1); 1304ac6713ccSYan, Zheng } 1305ac6713ccSYan, Zheng 1306ac6713ccSYan, Zheng /* 1307ac6713ccSYan, Zheng * FIXME: Make security_dentry_init_security() generic. Currently 1308ac6713ccSYan, Zheng * It only supports single security module and only selinux has 1309ac6713ccSYan, Zheng * dentry_init_security hook. 1310ac6713ccSYan, Zheng */ 1311ac6713ccSYan, Zheng name = XATTR_NAME_SELINUX; 1312ac6713ccSYan, Zheng name_len = strlen(name); 1313ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, 1314ac6713ccSYan, Zheng 4 * 2 + name_len + as_ctx->sec_ctxlen); 1315ac6713ccSYan, Zheng if (err) 1316ac6713ccSYan, Zheng goto out; 1317ac6713ccSYan, Zheng 1318ac6713ccSYan, Zheng if (as_ctx->pagelist) { 1319ac6713ccSYan, Zheng /* update count of KV pairs */ 1320ac6713ccSYan, Zheng BUG_ON(pagelist->length <= sizeof(__le32)); 1321ac6713ccSYan, Zheng if (list_is_singular(&pagelist->head)) { 1322ac6713ccSYan, Zheng le32_add_cpu((__le32*)pagelist->mapped_tail, 1); 1323ac6713ccSYan, Zheng } else { 1324ac6713ccSYan, Zheng struct page *page = list_first_entry(&pagelist->head, 1325ac6713ccSYan, Zheng struct page, lru); 1326ac6713ccSYan, Zheng void *addr = kmap_atomic(page); 1327ac6713ccSYan, Zheng le32_add_cpu((__le32*)addr, 1); 1328ac6713ccSYan, Zheng kunmap_atomic(addr); 1329ac6713ccSYan, Zheng } 1330ac6713ccSYan, Zheng } else { 1331ac6713ccSYan, Zheng as_ctx->pagelist = pagelist; 1332ac6713ccSYan, Zheng } 1333ac6713ccSYan, Zheng 1334ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, name_len); 1335ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, name, name_len); 1336ac6713ccSYan, Zheng 1337ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen); 1338ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1339ac6713ccSYan, Zheng 1340ac6713ccSYan, Zheng err = 0; 1341ac6713ccSYan, Zheng out: 1342ac6713ccSYan, Zheng if (pagelist && !as_ctx->pagelist) 1343ac6713ccSYan, Zheng ceph_pagelist_release(pagelist); 1344ac6713ccSYan, Zheng return err; 1345ac6713ccSYan, Zheng } 1346668959a5SJeff Layton #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */ 1347668959a5SJeff Layton #endif /* CONFIG_SECURITY */ 13485c31e92dSYan, Zheng 13495c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx) 13505c31e92dSYan, Zheng { 13515c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL 13525c31e92dSYan, Zheng posix_acl_release(as_ctx->acl); 13535c31e92dSYan, Zheng posix_acl_release(as_ctx->default_acl); 13545c31e92dSYan, Zheng #endif 1355ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1356ac6713ccSYan, Zheng security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1357ac6713ccSYan, Zheng #endif 13585c31e92dSYan, Zheng if (as_ctx->pagelist) 13595c31e92dSYan, Zheng ceph_pagelist_release(as_ctx->pagelist); 13605c31e92dSYan, Zheng } 1361ac6713ccSYan, Zheng 1362ac6713ccSYan, Zheng /* 1363ac6713ccSYan, Zheng * List of handlers for synthetic system.* attributes. Other 1364ac6713ccSYan, Zheng * attributes are handled directly. 1365ac6713ccSYan, Zheng */ 1366ac6713ccSYan, Zheng const struct xattr_handler *ceph_xattr_handlers[] = { 1367ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL 1368ac6713ccSYan, Zheng &posix_acl_access_xattr_handler, 1369ac6713ccSYan, Zheng &posix_acl_default_xattr_handler, 1370ac6713ccSYan, Zheng #endif 1371ac6713ccSYan, Zheng &ceph_other_xattr_handler, 1372ac6713ccSYan, Zheng NULL, 1373ac6713ccSYan, Zheng }; 1374