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 { 60874c8ca1SDavid Howells struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.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 72874c8ca1SDavid Howells dout("ceph_vxattrcb_layout %p\n", &ci->netfs.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; 164874c8ca1SDavid Howells struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.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 236e7f72952SYanhu Cao static ssize_t ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info *ci, char *val, 237e7f72952SYanhu Cao size_t size) 238e7f72952SYanhu Cao { 239e7f72952SYanhu Cao return ceph_fmt_xattr(val, size, "%lld", ci->i_rsnaps); 240e7f72952SYanhu Cao } 241e7f72952SYanhu Cao 242f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, 243355da1ebSSage Weil size_t size) 244355da1ebSSage Weil { 24526350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes); 246355da1ebSSage Weil } 247355da1ebSSage Weil 248f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, 249355da1ebSSage Weil size_t size) 250355da1ebSSage Weil { 25126350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec, 2529bbeab41SArnd Bergmann ci->i_rctime.tv_nsec); 253355da1ebSSage Weil } 254355da1ebSSage Weil 25508796873SYan, Zheng /* dir pin */ 25608796873SYan, Zheng static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci) 25708796873SYan, Zheng { 25808796873SYan, Zheng return ci->i_dir_pin != -ENODATA; 25908796873SYan, Zheng } 260fb18a575SLuis Henriques 261f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val, 26208796873SYan, Zheng size_t size) 26308796873SYan, Zheng { 26426350535SJeff Layton return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin); 26508796873SYan, Zheng } 26608796873SYan, Zheng 26708796873SYan, Zheng /* quotas */ 268fb18a575SLuis Henriques static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci) 269fb18a575SLuis Henriques { 270f1919826SYan, Zheng bool ret = false; 271f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 272f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 273f1919826SYan, Zheng ci->i_vino.snap == CEPH_NOSNAP && 274f1919826SYan, Zheng ci->i_snap_realm && 275f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino) 276f1919826SYan, Zheng ret = true; 277f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 278f1919826SYan, Zheng return ret; 279fb18a575SLuis Henriques } 280fb18a575SLuis Henriques 281f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val, 282fb18a575SLuis Henriques size_t size) 283fb18a575SLuis Henriques { 28426350535SJeff Layton return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu", 285fb18a575SLuis Henriques ci->i_max_bytes, ci->i_max_files); 286fb18a575SLuis Henriques } 287fb18a575SLuis Henriques 288f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_bytes(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_bytes); 292fb18a575SLuis Henriques } 293fb18a575SLuis Henriques 294f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci, 295fb18a575SLuis Henriques char *val, size_t size) 296fb18a575SLuis Henriques { 29726350535SJeff Layton return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files); 298fb18a575SLuis Henriques } 29932ab0bd7SSage Weil 300100cc610SDavid Disseldorp /* snapshots */ 301100cc610SDavid Disseldorp static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci) 302100cc610SDavid Disseldorp { 303100cc610SDavid Disseldorp return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0); 304100cc610SDavid Disseldorp } 305100cc610SDavid Disseldorp 306f1d1b51dSJeff Layton static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val, 307100cc610SDavid Disseldorp size_t size) 308100cc610SDavid Disseldorp { 30926350535SJeff Layton return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec, 310100cc610SDavid Disseldorp ci->i_snap_btime.tv_nsec); 311100cc610SDavid Disseldorp } 312100cc610SDavid Disseldorp 3135a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci, 3145a9e2f5dSXiubo Li char *val, size_t size) 3155a9e2f5dSXiubo Li { 316874c8ca1SDavid Howells struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb); 3175a9e2f5dSXiubo Li 3185a9e2f5dSXiubo Li return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid); 3195a9e2f5dSXiubo Li } 3205a9e2f5dSXiubo Li 3215a9e2f5dSXiubo Li static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci, 3225a9e2f5dSXiubo Li char *val, size_t size) 3235a9e2f5dSXiubo Li { 324874c8ca1SDavid Howells struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb); 3255a9e2f5dSXiubo Li 3265a9e2f5dSXiubo Li return ceph_fmt_xattr(val, size, "client%lld", 3275a9e2f5dSXiubo Li ceph_client_gid(fsc->client)); 3285a9e2f5dSXiubo Li } 3295a9e2f5dSXiubo Li 330dd980fc0SLuis Henriques static ssize_t ceph_vxattrcb_caps(struct ceph_inode_info *ci, char *val, 331dd980fc0SLuis Henriques size_t size) 332dd980fc0SLuis Henriques { 333dd980fc0SLuis Henriques int issued; 334dd980fc0SLuis Henriques 335dd980fc0SLuis Henriques spin_lock(&ci->i_ceph_lock); 336dd980fc0SLuis Henriques issued = __ceph_caps_issued(ci, NULL); 337dd980fc0SLuis Henriques spin_unlock(&ci->i_ceph_lock); 338dd980fc0SLuis Henriques 339dd980fc0SLuis Henriques return ceph_fmt_xattr(val, size, "%s/0x%x", 340dd980fc0SLuis Henriques ceph_cap_string(issued), issued); 341dd980fc0SLuis Henriques } 342dd980fc0SLuis Henriques 34340e309deSJeff Layton static ssize_t ceph_vxattrcb_auth_mds(struct ceph_inode_info *ci, 34440e309deSJeff Layton char *val, size_t size) 34540e309deSJeff Layton { 34640e309deSJeff Layton int ret; 34740e309deSJeff Layton 34840e309deSJeff Layton spin_lock(&ci->i_ceph_lock); 34940e309deSJeff Layton ret = ceph_fmt_xattr(val, size, "%d", 35040e309deSJeff Layton ci->i_auth_cap ? ci->i_auth_cap->session->s_mds : -1); 35140e309deSJeff Layton spin_unlock(&ci->i_ceph_lock); 35240e309deSJeff Layton return ret; 35340e309deSJeff Layton } 35440e309deSJeff Layton 355eb788084SAlex Elder #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name 356695b7119SSage Weil #define CEPH_XATTR_NAME2(_type, _name, _name2) \ 357695b7119SSage Weil XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 358eb788084SAlex Elder 35949a9f4f6SYan, Zheng #define XATTR_NAME_CEPH(_type, _name, _flags) \ 360eb788084SAlex Elder { \ 361eb788084SAlex Elder .name = CEPH_XATTR_NAME(_type, _name), \ 3623ce6cd12SAlex Elder .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ 363aa4066edSAlex Elder .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 364f36e4472SSage Weil .exists_cb = NULL, \ 36549a9f4f6SYan, Zheng .flags = (VXATTR_FLAG_READONLY | _flags), \ 366eb788084SAlex Elder } 36749a9f4f6SYan, Zheng #define XATTR_RSTAT_FIELD(_type, _name) \ 36849a9f4f6SYan, Zheng XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT) 369d7a2dc52SVenky Shankar #define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name) \ 370d7a2dc52SVenky Shankar { \ 371d7a2dc52SVenky Shankar .name = CEPH_XATTR_NAME(_type, _name), \ 372d7a2dc52SVenky Shankar .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ 373d7a2dc52SVenky Shankar .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 374d7a2dc52SVenky Shankar .exists_cb = NULL, \ 375d7a2dc52SVenky Shankar .flags = VXATTR_FLAG_RSTAT, \ 376d7a2dc52SVenky Shankar } 377695b7119SSage Weil #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ 378695b7119SSage Weil { \ 379695b7119SSage Weil .name = CEPH_XATTR_NAME2(_type, _name, _field), \ 380695b7119SSage Weil .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ 381695b7119SSage Weil .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ 382695b7119SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, \ 3834e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 384695b7119SSage Weil } 385fb18a575SLuis Henriques #define XATTR_QUOTA_FIELD(_type, _name) \ 386fb18a575SLuis Henriques { \ 387fb18a575SLuis Henriques .name = CEPH_XATTR_NAME(_type, _name), \ 388fb18a575SLuis Henriques .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \ 389fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ 390fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, \ 3914e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, \ 392fb18a575SLuis Henriques } 393eb788084SAlex Elder 394881a5fa2SAlex Elder static struct ceph_vxattr ceph_dir_vxattrs[] = { 3951f08f2b0SSage Weil { 3961f08f2b0SSage Weil .name = "ceph.dir.layout", 3971f08f2b0SSage Weil .name_size = sizeof("ceph.dir.layout"), 3981f08f2b0SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 3991f08f2b0SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 4004e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 4011f08f2b0SSage Weil }, 402695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), 403695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, stripe_count), 404695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, object_size), 405695b7119SSage Weil XATTR_LAYOUT_FIELD(dir, layout, pool), 406779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(dir, layout, pool_namespace), 40781048c00SJeff Layton XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT), 40881048c00SJeff Layton XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT), 40981048c00SJeff Layton XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT), 41049a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rentries), 41149a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rfiles), 41249a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rsubdirs), 413e7f72952SYanhu Cao XATTR_RSTAT_FIELD(dir, rsnaps), 41449a9f4f6SYan, Zheng XATTR_RSTAT_FIELD(dir, rbytes), 415d7a2dc52SVenky Shankar XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime), 416fb18a575SLuis Henriques { 41708796873SYan, Zheng .name = "ceph.dir.pin", 418e1b81439SDavid Disseldorp .name_size = sizeof("ceph.dir.pin"), 41908796873SYan, Zheng .getxattr_cb = ceph_vxattrcb_dir_pin, 42008796873SYan, Zheng .exists_cb = ceph_vxattrcb_dir_pin_exists, 42108796873SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 42208796873SYan, Zheng }, 42308796873SYan, Zheng { 424fb18a575SLuis Henriques .name = "ceph.quota", 425fb18a575SLuis Henriques .name_size = sizeof("ceph.quota"), 426fb18a575SLuis Henriques .getxattr_cb = ceph_vxattrcb_quota, 427fb18a575SLuis Henriques .exists_cb = ceph_vxattrcb_quota_exists, 4284e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 429fb18a575SLuis Henriques }, 430fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_bytes), 431fb18a575SLuis Henriques XATTR_QUOTA_FIELD(quota, max_files), 432100cc610SDavid Disseldorp { 433100cc610SDavid Disseldorp .name = "ceph.snap.btime", 434100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 435100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 436100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 437100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 438100cc610SDavid Disseldorp }, 439dd980fc0SLuis Henriques { 440dd980fc0SLuis Henriques .name = "ceph.caps", 441dd980fc0SLuis Henriques .name_size = sizeof("ceph.caps"), 442dd980fc0SLuis Henriques .getxattr_cb = ceph_vxattrcb_caps, 443dd980fc0SLuis Henriques .exists_cb = NULL, 444dd980fc0SLuis Henriques .flags = VXATTR_FLAG_HIDDEN, 445dd980fc0SLuis Henriques }, 4462c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 447355da1ebSSage Weil }; 448355da1ebSSage Weil 449355da1ebSSage Weil /* files */ 450355da1ebSSage Weil 451881a5fa2SAlex Elder static struct ceph_vxattr ceph_file_vxattrs[] = { 45232ab0bd7SSage Weil { 45332ab0bd7SSage Weil .name = "ceph.file.layout", 45432ab0bd7SSage Weil .name_size = sizeof("ceph.file.layout"), 45532ab0bd7SSage Weil .getxattr_cb = ceph_vxattrcb_layout, 45632ab0bd7SSage Weil .exists_cb = ceph_vxattrcb_layout_exists, 4574e9906e7SYan, Zheng .flags = VXATTR_FLAG_HIDDEN, 45832ab0bd7SSage Weil }, 459695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_unit), 460695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, stripe_count), 461695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, object_size), 462695b7119SSage Weil XATTR_LAYOUT_FIELD(file, layout, pool), 463779fe0fbSYan, Zheng XATTR_LAYOUT_FIELD(file, layout, pool_namespace), 464100cc610SDavid Disseldorp { 465100cc610SDavid Disseldorp .name = "ceph.snap.btime", 466100cc610SDavid Disseldorp .name_size = sizeof("ceph.snap.btime"), 467100cc610SDavid Disseldorp .getxattr_cb = ceph_vxattrcb_snap_btime, 468100cc610SDavid Disseldorp .exists_cb = ceph_vxattrcb_snap_btime_exists, 469100cc610SDavid Disseldorp .flags = VXATTR_FLAG_READONLY, 470100cc610SDavid Disseldorp }, 471dd980fc0SLuis Henriques { 472dd980fc0SLuis Henriques .name = "ceph.caps", 473dd980fc0SLuis Henriques .name_size = sizeof("ceph.caps"), 474dd980fc0SLuis Henriques .getxattr_cb = ceph_vxattrcb_caps, 475dd980fc0SLuis Henriques .exists_cb = NULL, 476dd980fc0SLuis Henriques .flags = VXATTR_FLAG_HIDDEN, 477dd980fc0SLuis Henriques }, 4782c3dd4ffSAlex Elder { .name = NULL, 0 } /* Required table terminator */ 479355da1ebSSage Weil }; 480355da1ebSSage Weil 4815a9e2f5dSXiubo Li static struct ceph_vxattr ceph_common_vxattrs[] = { 4825a9e2f5dSXiubo Li { 4835a9e2f5dSXiubo Li .name = "ceph.cluster_fsid", 4845a9e2f5dSXiubo Li .name_size = sizeof("ceph.cluster_fsid"), 4855a9e2f5dSXiubo Li .getxattr_cb = ceph_vxattrcb_cluster_fsid, 4865a9e2f5dSXiubo Li .exists_cb = NULL, 4875a9e2f5dSXiubo Li .flags = VXATTR_FLAG_READONLY, 4885a9e2f5dSXiubo Li }, 4895a9e2f5dSXiubo Li { 4905a9e2f5dSXiubo Li .name = "ceph.client_id", 4915a9e2f5dSXiubo Li .name_size = sizeof("ceph.client_id"), 4925a9e2f5dSXiubo Li .getxattr_cb = ceph_vxattrcb_client_id, 4935a9e2f5dSXiubo Li .exists_cb = NULL, 4945a9e2f5dSXiubo Li .flags = VXATTR_FLAG_READONLY, 4955a9e2f5dSXiubo Li }, 49640e309deSJeff Layton { 49740e309deSJeff Layton .name = "ceph.auth_mds", 49840e309deSJeff Layton .name_size = sizeof("ceph.auth_mds"), 49940e309deSJeff Layton .getxattr_cb = ceph_vxattrcb_auth_mds, 50040e309deSJeff Layton .exists_cb = NULL, 50140e309deSJeff Layton .flags = VXATTR_FLAG_READONLY, 50240e309deSJeff Layton }, 5035a9e2f5dSXiubo Li { .name = NULL, 0 } /* Required table terminator */ 5045a9e2f5dSXiubo Li }; 5055a9e2f5dSXiubo Li 506881a5fa2SAlex Elder static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) 507355da1ebSSage Weil { 508355da1ebSSage Weil if (S_ISDIR(inode->i_mode)) 509355da1ebSSage Weil return ceph_dir_vxattrs; 510355da1ebSSage Weil else if (S_ISREG(inode->i_mode)) 511355da1ebSSage Weil return ceph_file_vxattrs; 512355da1ebSSage Weil return NULL; 513355da1ebSSage Weil } 514355da1ebSSage Weil 515881a5fa2SAlex Elder static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, 516355da1ebSSage Weil const char *name) 517355da1ebSSage Weil { 518881a5fa2SAlex Elder struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); 51906476a69SAlex Elder 52006476a69SAlex Elder if (vxattr) { 52106476a69SAlex Elder while (vxattr->name) { 52206476a69SAlex Elder if (!strcmp(vxattr->name, name)) 523355da1ebSSage Weil return vxattr; 524355da1ebSSage Weil vxattr++; 52506476a69SAlex Elder } 52606476a69SAlex Elder } 52706476a69SAlex Elder 5285a9e2f5dSXiubo Li vxattr = ceph_common_vxattrs; 5295a9e2f5dSXiubo Li while (vxattr->name) { 5305a9e2f5dSXiubo Li if (!strcmp(vxattr->name, name)) 5315a9e2f5dSXiubo Li return vxattr; 5325a9e2f5dSXiubo Li vxattr++; 5335a9e2f5dSXiubo Li } 5345a9e2f5dSXiubo Li 535355da1ebSSage Weil return NULL; 536355da1ebSSage Weil } 537355da1ebSSage Weil 5387a6c3a03SXiubo Li #define MAX_XATTR_VAL_PRINT_LEN 256 5397a6c3a03SXiubo Li 540355da1ebSSage Weil static int __set_xattr(struct ceph_inode_info *ci, 541355da1ebSSage Weil const char *name, int name_len, 542355da1ebSSage Weil const char *val, int val_len, 543fbc0b970SYan, Zheng int flags, int update_xattr, 544355da1ebSSage Weil struct ceph_inode_xattr **newxattr) 545355da1ebSSage Weil { 546355da1ebSSage Weil struct rb_node **p; 547355da1ebSSage Weil struct rb_node *parent = NULL; 548355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 549355da1ebSSage Weil int c; 550355da1ebSSage Weil int new = 0; 551355da1ebSSage Weil 552355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 553355da1ebSSage Weil while (*p) { 554355da1ebSSage Weil parent = *p; 555355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 556355da1ebSSage Weil c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); 557355da1ebSSage Weil if (c < 0) 558355da1ebSSage Weil p = &(*p)->rb_left; 559355da1ebSSage Weil else if (c > 0) 560355da1ebSSage Weil p = &(*p)->rb_right; 561355da1ebSSage Weil else { 562355da1ebSSage Weil if (name_len == xattr->name_len) 563355da1ebSSage Weil break; 564355da1ebSSage Weil else if (name_len < xattr->name_len) 565355da1ebSSage Weil p = &(*p)->rb_left; 566355da1ebSSage Weil else 567355da1ebSSage Weil p = &(*p)->rb_right; 568355da1ebSSage Weil } 569355da1ebSSage Weil xattr = NULL; 570355da1ebSSage Weil } 571355da1ebSSage Weil 572fbc0b970SYan, Zheng if (update_xattr) { 573fbc0b970SYan, Zheng int err = 0; 574eeca958dSLuis Henriques 575fbc0b970SYan, Zheng if (xattr && (flags & XATTR_CREATE)) 576fbc0b970SYan, Zheng err = -EEXIST; 577fbc0b970SYan, Zheng else if (!xattr && (flags & XATTR_REPLACE)) 578fbc0b970SYan, Zheng err = -ENODATA; 579fbc0b970SYan, Zheng if (err) { 580fbc0b970SYan, Zheng kfree(name); 581fbc0b970SYan, Zheng kfree(val); 582eeca958dSLuis Henriques kfree(*newxattr); 583fbc0b970SYan, Zheng return err; 584fbc0b970SYan, Zheng } 585bcdfeb2eSYan, Zheng if (update_xattr < 0) { 586bcdfeb2eSYan, Zheng if (xattr) 587bcdfeb2eSYan, Zheng __remove_xattr(ci, xattr); 588bcdfeb2eSYan, Zheng kfree(name); 589eeca958dSLuis Henriques kfree(*newxattr); 590bcdfeb2eSYan, Zheng return 0; 591bcdfeb2eSYan, Zheng } 592fbc0b970SYan, Zheng } 593fbc0b970SYan, Zheng 594355da1ebSSage Weil if (!xattr) { 595355da1ebSSage Weil new = 1; 596355da1ebSSage Weil xattr = *newxattr; 597355da1ebSSage Weil xattr->name = name; 598355da1ebSSage Weil xattr->name_len = name_len; 599fbc0b970SYan, Zheng xattr->should_free_name = update_xattr; 600355da1ebSSage Weil 601355da1ebSSage Weil ci->i_xattrs.count++; 6027a6c3a03SXiubo Li dout("%s count=%d\n", __func__, ci->i_xattrs.count); 603355da1ebSSage Weil } else { 604355da1ebSSage Weil kfree(*newxattr); 605355da1ebSSage Weil *newxattr = NULL; 606355da1ebSSage Weil if (xattr->should_free_val) 607c00e4522SXu Wang kfree(xattr->val); 608355da1ebSSage Weil 609fbc0b970SYan, Zheng if (update_xattr) { 610c00e4522SXu Wang kfree(name); 611355da1ebSSage Weil name = xattr->name; 612355da1ebSSage Weil } 613355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 614355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 615355da1ebSSage Weil } 616355da1ebSSage Weil ci->i_xattrs.names_size += name_len; 617355da1ebSSage Weil ci->i_xattrs.vals_size += val_len; 618355da1ebSSage Weil if (val) 619355da1ebSSage Weil xattr->val = val; 620355da1ebSSage Weil else 621355da1ebSSage Weil xattr->val = ""; 622355da1ebSSage Weil 623355da1ebSSage Weil xattr->val_len = val_len; 624fbc0b970SYan, Zheng xattr->dirty = update_xattr; 625fbc0b970SYan, Zheng xattr->should_free_val = (val && update_xattr); 626355da1ebSSage Weil 627355da1ebSSage Weil if (new) { 628355da1ebSSage Weil rb_link_node(&xattr->node, parent, p); 629355da1ebSSage Weil rb_insert_color(&xattr->node, &ci->i_xattrs.index); 6307a6c3a03SXiubo Li dout("%s p=%p\n", __func__, p); 631355da1ebSSage Weil } 632355da1ebSSage Weil 6337a6c3a03SXiubo Li dout("%s added %llx.%llx xattr %p %.*s=%.*s%s\n", __func__, 6347a6c3a03SXiubo Li ceph_vinop(&ci->netfs.inode), xattr, name_len, name, 6357a6c3a03SXiubo Li min(val_len, MAX_XATTR_VAL_PRINT_LEN), val, 6367a6c3a03SXiubo Li val_len > MAX_XATTR_VAL_PRINT_LEN ? "..." : ""); 637355da1ebSSage Weil 638355da1ebSSage Weil return 0; 639355da1ebSSage Weil } 640355da1ebSSage Weil 641355da1ebSSage Weil static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, 642355da1ebSSage Weil const char *name) 643355da1ebSSage Weil { 644355da1ebSSage Weil struct rb_node **p; 645355da1ebSSage Weil struct rb_node *parent = NULL; 646355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 64717db143fSSage Weil int name_len = strlen(name); 648355da1ebSSage Weil int c; 649355da1ebSSage Weil 650355da1ebSSage Weil p = &ci->i_xattrs.index.rb_node; 651355da1ebSSage Weil while (*p) { 652355da1ebSSage Weil parent = *p; 653355da1ebSSage Weil xattr = rb_entry(parent, struct ceph_inode_xattr, node); 654355da1ebSSage Weil c = strncmp(name, xattr->name, xattr->name_len); 65517db143fSSage Weil if (c == 0 && name_len > xattr->name_len) 65617db143fSSage Weil c = 1; 657355da1ebSSage Weil if (c < 0) 658355da1ebSSage Weil p = &(*p)->rb_left; 659355da1ebSSage Weil else if (c > 0) 660355da1ebSSage Weil p = &(*p)->rb_right; 661355da1ebSSage Weil else { 6627a6c3a03SXiubo Li int len = min(xattr->val_len, MAX_XATTR_VAL_PRINT_LEN); 6637a6c3a03SXiubo Li 6647a6c3a03SXiubo Li dout("%s %s: found %.*s%s\n", __func__, name, len, 6657a6c3a03SXiubo Li xattr->val, xattr->val_len > len ? "..." : ""); 666355da1ebSSage Weil return xattr; 667355da1ebSSage Weil } 668355da1ebSSage Weil } 669355da1ebSSage Weil 6707a6c3a03SXiubo Li dout("%s %s: not found\n", __func__, name); 671355da1ebSSage Weil 672355da1ebSSage Weil return NULL; 673355da1ebSSage Weil } 674355da1ebSSage Weil 675355da1ebSSage Weil static void __free_xattr(struct ceph_inode_xattr *xattr) 676355da1ebSSage Weil { 677355da1ebSSage Weil BUG_ON(!xattr); 678355da1ebSSage Weil 679355da1ebSSage Weil if (xattr->should_free_name) 680c00e4522SXu Wang kfree(xattr->name); 681355da1ebSSage Weil if (xattr->should_free_val) 682c00e4522SXu Wang kfree(xattr->val); 683355da1ebSSage Weil 684355da1ebSSage Weil kfree(xattr); 685355da1ebSSage Weil } 686355da1ebSSage Weil 687355da1ebSSage Weil static int __remove_xattr(struct ceph_inode_info *ci, 688355da1ebSSage Weil struct ceph_inode_xattr *xattr) 689355da1ebSSage Weil { 690355da1ebSSage Weil if (!xattr) 691524186acSYan, Zheng return -ENODATA; 692355da1ebSSage Weil 693355da1ebSSage Weil rb_erase(&xattr->node, &ci->i_xattrs.index); 694355da1ebSSage Weil 695355da1ebSSage Weil if (xattr->should_free_name) 696c00e4522SXu Wang kfree(xattr->name); 697355da1ebSSage Weil if (xattr->should_free_val) 698c00e4522SXu Wang kfree(xattr->val); 699355da1ebSSage Weil 700355da1ebSSage Weil ci->i_xattrs.names_size -= xattr->name_len; 701355da1ebSSage Weil ci->i_xattrs.vals_size -= xattr->val_len; 702355da1ebSSage Weil ci->i_xattrs.count--; 703355da1ebSSage Weil kfree(xattr); 704355da1ebSSage Weil 705355da1ebSSage Weil return 0; 706355da1ebSSage Weil } 707355da1ebSSage Weil 708355da1ebSSage Weil static char *__copy_xattr_names(struct ceph_inode_info *ci, 709355da1ebSSage Weil char *dest) 710355da1ebSSage Weil { 711355da1ebSSage Weil struct rb_node *p; 712355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 713355da1ebSSage Weil 714355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 715355da1ebSSage Weil dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); 716355da1ebSSage Weil 717355da1ebSSage Weil while (p) { 718355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 719355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 720355da1ebSSage Weil dest[xattr->name_len] = '\0'; 721355da1ebSSage Weil 722355da1ebSSage Weil dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, 723355da1ebSSage Weil xattr->name_len, ci->i_xattrs.names_size); 724355da1ebSSage Weil 725355da1ebSSage Weil dest += xattr->name_len + 1; 726355da1ebSSage Weil p = rb_next(p); 727355da1ebSSage Weil } 728355da1ebSSage Weil 729355da1ebSSage Weil return dest; 730355da1ebSSage Weil } 731355da1ebSSage Weil 732355da1ebSSage Weil void __ceph_destroy_xattrs(struct ceph_inode_info *ci) 733355da1ebSSage Weil { 734355da1ebSSage Weil struct rb_node *p, *tmp; 735355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 736355da1ebSSage Weil 737355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 738355da1ebSSage Weil 739355da1ebSSage Weil dout("__ceph_destroy_xattrs p=%p\n", p); 740355da1ebSSage Weil 741355da1ebSSage Weil while (p) { 742355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 743355da1ebSSage Weil tmp = p; 744355da1ebSSage Weil p = rb_next(tmp); 745355da1ebSSage Weil dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, 746355da1ebSSage Weil xattr->name_len, xattr->name); 747355da1ebSSage Weil rb_erase(tmp, &ci->i_xattrs.index); 748355da1ebSSage Weil 749355da1ebSSage Weil __free_xattr(xattr); 750355da1ebSSage Weil } 751355da1ebSSage Weil 752355da1ebSSage Weil ci->i_xattrs.names_size = 0; 753355da1ebSSage Weil ci->i_xattrs.vals_size = 0; 754355da1ebSSage Weil ci->i_xattrs.index_version = 0; 755355da1ebSSage Weil ci->i_xattrs.count = 0; 756355da1ebSSage Weil ci->i_xattrs.index = RB_ROOT; 757355da1ebSSage Weil } 758355da1ebSSage Weil 759355da1ebSSage Weil static int __build_xattrs(struct inode *inode) 760be655596SSage Weil __releases(ci->i_ceph_lock) 761be655596SSage Weil __acquires(ci->i_ceph_lock) 762355da1ebSSage Weil { 763355da1ebSSage Weil u32 namelen; 764355da1ebSSage Weil u32 numattr = 0; 765355da1ebSSage Weil void *p, *end; 766355da1ebSSage Weil u32 len; 767355da1ebSSage Weil const char *name, *val; 768355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 7690eb30853SXiubo Li u64 xattr_version; 770355da1ebSSage Weil struct ceph_inode_xattr **xattrs = NULL; 77163ff78b2SSage Weil int err = 0; 772355da1ebSSage Weil int i; 773355da1ebSSage Weil 774355da1ebSSage Weil dout("__build_xattrs() len=%d\n", 775355da1ebSSage Weil ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); 776355da1ebSSage Weil 777355da1ebSSage Weil if (ci->i_xattrs.index_version >= ci->i_xattrs.version) 778355da1ebSSage Weil return 0; /* already built */ 779355da1ebSSage Weil 780355da1ebSSage Weil __ceph_destroy_xattrs(ci); 781355da1ebSSage Weil 782355da1ebSSage Weil start: 783355da1ebSSage Weil /* updated internal xattr rb tree */ 784355da1ebSSage Weil if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { 785355da1ebSSage Weil p = ci->i_xattrs.blob->vec.iov_base; 786355da1ebSSage Weil end = p + ci->i_xattrs.blob->vec.iov_len; 787355da1ebSSage Weil ceph_decode_32_safe(&p, end, numattr, bad); 788355da1ebSSage Weil xattr_version = ci->i_xattrs.version; 789be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 790355da1ebSSage Weil 7917e8a2952SIlya Dryomov xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), 792355da1ebSSage Weil GFP_NOFS); 793355da1ebSSage Weil err = -ENOMEM; 794355da1ebSSage Weil if (!xattrs) 795355da1ebSSage Weil goto bad_lock; 7961a295bd8SIlya Dryomov 797355da1ebSSage Weil for (i = 0; i < numattr; i++) { 798355da1ebSSage Weil xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), 799355da1ebSSage Weil GFP_NOFS); 800355da1ebSSage Weil if (!xattrs[i]) 801355da1ebSSage Weil goto bad_lock; 802355da1ebSSage Weil } 803355da1ebSSage Weil 804be655596SSage Weil spin_lock(&ci->i_ceph_lock); 805355da1ebSSage Weil if (ci->i_xattrs.version != xattr_version) { 806355da1ebSSage Weil /* lost a race, retry */ 807355da1ebSSage Weil for (i = 0; i < numattr; i++) 808355da1ebSSage Weil kfree(xattrs[i]); 809355da1ebSSage Weil kfree(xattrs); 81021ec6ffaSAlan Cox xattrs = NULL; 811355da1ebSSage Weil goto start; 812355da1ebSSage Weil } 813355da1ebSSage Weil err = -EIO; 814355da1ebSSage Weil while (numattr--) { 815355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 816355da1ebSSage Weil namelen = len; 817355da1ebSSage Weil name = p; 818355da1ebSSage Weil p += len; 819355da1ebSSage Weil ceph_decode_32_safe(&p, end, len, bad); 820355da1ebSSage Weil val = p; 821355da1ebSSage Weil p += len; 822355da1ebSSage Weil 823355da1ebSSage Weil err = __set_xattr(ci, name, namelen, val, len, 824fbc0b970SYan, Zheng 0, 0, &xattrs[numattr]); 825355da1ebSSage Weil 826355da1ebSSage Weil if (err < 0) 827355da1ebSSage Weil goto bad; 828355da1ebSSage Weil } 829355da1ebSSage Weil kfree(xattrs); 830355da1ebSSage Weil } 831355da1ebSSage Weil ci->i_xattrs.index_version = ci->i_xattrs.version; 832355da1ebSSage Weil ci->i_xattrs.dirty = false; 833355da1ebSSage Weil 834355da1ebSSage Weil return err; 835355da1ebSSage Weil bad_lock: 836be655596SSage Weil spin_lock(&ci->i_ceph_lock); 837355da1ebSSage Weil bad: 838355da1ebSSage Weil if (xattrs) { 839355da1ebSSage Weil for (i = 0; i < numattr; i++) 840355da1ebSSage Weil kfree(xattrs[i]); 841355da1ebSSage Weil kfree(xattrs); 842355da1ebSSage Weil } 843355da1ebSSage Weil ci->i_xattrs.names_size = 0; 844355da1ebSSage Weil return err; 845355da1ebSSage Weil } 846355da1ebSSage Weil 847355da1ebSSage Weil static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, 848355da1ebSSage Weil int val_size) 849355da1ebSSage Weil { 850355da1ebSSage Weil /* 851355da1ebSSage Weil * 4 bytes for the length, and additional 4 bytes per each xattr name, 852355da1ebSSage Weil * 4 bytes per each value 853355da1ebSSage Weil */ 854355da1ebSSage Weil int size = 4 + ci->i_xattrs.count*(4 + 4) + 855355da1ebSSage Weil ci->i_xattrs.names_size + 856355da1ebSSage Weil ci->i_xattrs.vals_size; 857355da1ebSSage Weil dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", 858355da1ebSSage Weil ci->i_xattrs.count, ci->i_xattrs.names_size, 859355da1ebSSage Weil ci->i_xattrs.vals_size); 860355da1ebSSage Weil 861355da1ebSSage Weil if (name_size) 862355da1ebSSage Weil size += 4 + 4 + name_size + val_size; 863355da1ebSSage Weil 864355da1ebSSage Weil return size; 865355da1ebSSage Weil } 866355da1ebSSage Weil 867355da1ebSSage Weil /* 868355da1ebSSage Weil * If there are dirty xattrs, reencode xattrs into the prealloc_blob 86912fe3ddaSLuis Henriques * and swap into place. It returns the old i_xattrs.blob (or NULL) so 87012fe3ddaSLuis Henriques * that it can be freed by the caller as the i_ceph_lock is likely to be 87112fe3ddaSLuis Henriques * held. 872355da1ebSSage Weil */ 87312fe3ddaSLuis Henriques struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci) 874355da1ebSSage Weil { 875355da1ebSSage Weil struct rb_node *p; 876355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 87712fe3ddaSLuis Henriques struct ceph_buffer *old_blob = NULL; 878355da1ebSSage Weil void *dest; 879355da1ebSSage Weil 880874c8ca1SDavid Howells dout("__build_xattrs_blob %p\n", &ci->netfs.inode); 881355da1ebSSage Weil if (ci->i_xattrs.dirty) { 882355da1ebSSage Weil int need = __get_required_blob_size(ci, 0, 0); 883355da1ebSSage Weil 884355da1ebSSage Weil BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); 885355da1ebSSage Weil 886355da1ebSSage Weil p = rb_first(&ci->i_xattrs.index); 887355da1ebSSage Weil dest = ci->i_xattrs.prealloc_blob->vec.iov_base; 888355da1ebSSage Weil 889355da1ebSSage Weil ceph_encode_32(&dest, ci->i_xattrs.count); 890355da1ebSSage Weil while (p) { 891355da1ebSSage Weil xattr = rb_entry(p, struct ceph_inode_xattr, node); 892355da1ebSSage Weil 893355da1ebSSage Weil ceph_encode_32(&dest, xattr->name_len); 894355da1ebSSage Weil memcpy(dest, xattr->name, xattr->name_len); 895355da1ebSSage Weil dest += xattr->name_len; 896355da1ebSSage Weil ceph_encode_32(&dest, xattr->val_len); 897355da1ebSSage Weil memcpy(dest, xattr->val, xattr->val_len); 898355da1ebSSage Weil dest += xattr->val_len; 899355da1ebSSage Weil 900355da1ebSSage Weil p = rb_next(p); 901355da1ebSSage Weil } 902355da1ebSSage Weil 903355da1ebSSage Weil /* adjust buffer len; it may be larger than we need */ 904355da1ebSSage Weil ci->i_xattrs.prealloc_blob->vec.iov_len = 905355da1ebSSage Weil dest - ci->i_xattrs.prealloc_blob->vec.iov_base; 906355da1ebSSage Weil 907b6c1d5b8SSage Weil if (ci->i_xattrs.blob) 90812fe3ddaSLuis Henriques old_blob = ci->i_xattrs.blob; 909355da1ebSSage Weil ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; 910355da1ebSSage Weil ci->i_xattrs.prealloc_blob = NULL; 911355da1ebSSage Weil ci->i_xattrs.dirty = false; 9124a625be4SSage Weil ci->i_xattrs.version++; 913355da1ebSSage Weil } 91412fe3ddaSLuis Henriques 91512fe3ddaSLuis Henriques return old_blob; 916355da1ebSSage Weil } 917355da1ebSSage Weil 918315f2408SYan, Zheng static inline int __get_request_mask(struct inode *in) { 919315f2408SYan, Zheng struct ceph_mds_request *req = current->journal_info; 920315f2408SYan, Zheng int mask = 0; 921315f2408SYan, Zheng if (req && req->r_target_inode == in) { 922315f2408SYan, Zheng if (req->r_op == CEPH_MDS_OP_LOOKUP || 923315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPINO || 924315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_LOOKUPPARENT || 925315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_GETATTR) { 926315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.getattr.mask); 927315f2408SYan, Zheng } else if (req->r_op == CEPH_MDS_OP_OPEN || 928315f2408SYan, Zheng req->r_op == CEPH_MDS_OP_CREATE) { 929315f2408SYan, Zheng mask = le32_to_cpu(req->r_args.open.mask); 930315f2408SYan, Zheng } 931315f2408SYan, Zheng } 932315f2408SYan, Zheng return mask; 933315f2408SYan, Zheng } 934315f2408SYan, Zheng 9357221fe4cSGuangliang Zhao ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, 936355da1ebSSage Weil size_t size) 937355da1ebSSage Weil { 938355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 939355da1ebSSage Weil struct ceph_inode_xattr *xattr; 9406ddf5f16SMilind Changire struct ceph_vxattr *vxattr; 941315f2408SYan, Zheng int req_mask; 942f1d1b51dSJeff Layton ssize_t err; 943355da1ebSSage Weil 9446ddf5f16SMilind Changire if (strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) 9456ddf5f16SMilind Changire goto handle_non_vxattrs; 9466ddf5f16SMilind Changire 9470bee82fbSSage Weil /* let's see if a virtual xattr was requested */ 9480bee82fbSSage Weil vxattr = ceph_match_vxattr(inode, name); 94929dccfa5SYan, Zheng if (vxattr) { 95049a9f4f6SYan, Zheng int mask = 0; 95149a9f4f6SYan, Zheng if (vxattr->flags & VXATTR_FLAG_RSTAT) 95249a9f4f6SYan, Zheng mask |= CEPH_STAT_RSTAT; 95381048c00SJeff Layton if (vxattr->flags & VXATTR_FLAG_DIRSTAT) 95481048c00SJeff Layton mask |= CEPH_CAP_FILE_SHARED; 95549a9f4f6SYan, Zheng err = ceph_do_getattr(inode, mask, true); 9561684dd03SYan, Zheng if (err) 9571684dd03SYan, Zheng return err; 95829dccfa5SYan, Zheng err = -ENODATA; 9593b421018SJeff Layton if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) { 9600bee82fbSSage Weil err = vxattr->getxattr_cb(ci, value, size); 9613b421018SJeff Layton if (size && size < err) 9623b421018SJeff Layton err = -ERANGE; 9633b421018SJeff Layton } 964a1dc1937Smajianpeng return err; 9656ddf5f16SMilind Changire } else { 9666ddf5f16SMilind Changire err = ceph_do_getvxattr(inode, name, value, size); 9676ddf5f16SMilind Changire /* this would happen with a new client and old server combo */ 9686ddf5f16SMilind Changire if (err == -EOPNOTSUPP) 9696ddf5f16SMilind Changire err = -ENODATA; 9706ddf5f16SMilind Changire return err; 9710bee82fbSSage Weil } 9726ddf5f16SMilind Changire handle_non_vxattrs: 973315f2408SYan, Zheng req_mask = __get_request_mask(inode); 974315f2408SYan, Zheng 975a1dc1937Smajianpeng spin_lock(&ci->i_ceph_lock); 976d36e0b62SJeff Layton dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name, 977a1dc1937Smajianpeng ci->i_xattrs.version, ci->i_xattrs.index_version); 978a1dc1937Smajianpeng 979508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 980315f2408SYan, Zheng !((req_mask & CEPH_CAP_XATTR_SHARED) || 9811af16d54SXiubo Li __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) { 982be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 983315f2408SYan, Zheng 984315f2408SYan, Zheng /* security module gets xattr while filling trace */ 985d37b1d99SMarkus Elfring if (current->journal_info) { 986315f2408SYan, Zheng pr_warn_ratelimited("sync getxattr %p " 987315f2408SYan, Zheng "during filling trace\n", inode); 988315f2408SYan, Zheng return -EBUSY; 989315f2408SYan, Zheng } 990315f2408SYan, Zheng 991355da1ebSSage Weil /* get xattrs from mds (if we don't already have them) */ 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; 1001355da1ebSSage Weil 1002355da1ebSSage Weil err = -ENODATA; /* == ENOATTR */ 1003355da1ebSSage Weil xattr = __get_xattr(ci, name); 10040bee82fbSSage Weil if (!xattr) 1005355da1ebSSage Weil goto out; 1006355da1ebSSage Weil 1007355da1ebSSage Weil err = -ERANGE; 1008355da1ebSSage Weil if (size && size < xattr->val_len) 1009355da1ebSSage Weil goto out; 1010355da1ebSSage Weil 1011355da1ebSSage Weil err = xattr->val_len; 1012355da1ebSSage Weil if (size == 0) 1013355da1ebSSage Weil goto out; 1014355da1ebSSage Weil 1015355da1ebSSage Weil memcpy(value, xattr->val, xattr->val_len); 1016355da1ebSSage Weil 1017d37b1d99SMarkus Elfring if (current->journal_info && 1018026105ebSJeff Layton !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) && 1019026105ebSJeff Layton security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN)) 1020315f2408SYan, Zheng ci->i_ceph_flags |= CEPH_I_SEC_INITED; 1021355da1ebSSage Weil out: 1022be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1023355da1ebSSage Weil return err; 1024355da1ebSSage Weil } 1025355da1ebSSage Weil 1026355da1ebSSage Weil ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) 1027355da1ebSSage Weil { 10282b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 1029355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 10302b2abcacSDavid Disseldorp bool len_only = (size == 0); 1031355da1ebSSage Weil u32 namelen; 1032355da1ebSSage Weil int err; 1033355da1ebSSage Weil 1034be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1035355da1ebSSage Weil dout("listxattr %p ver=%lld index_ver=%lld\n", inode, 1036355da1ebSSage Weil ci->i_xattrs.version, ci->i_xattrs.index_version); 1037355da1ebSSage Weil 1038508b32d8SYan, Zheng if (ci->i_xattrs.version == 0 || 10391af16d54SXiubo Li !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) { 1040be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1041508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); 1042355da1ebSSage Weil if (err) 1043355da1ebSSage Weil return err; 1044be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1045508b32d8SYan, Zheng } 1046355da1ebSSage Weil 1047355da1ebSSage Weil err = __build_xattrs(inode); 1048355da1ebSSage Weil if (err < 0) 1049355da1ebSSage Weil goto out; 10503ce6cd12SAlex Elder 10512b2abcacSDavid Disseldorp /* add 1 byte for each xattr due to the null termination */ 1052b65917ddSSage Weil namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; 10532b2abcacSDavid Disseldorp if (!len_only) { 10542b2abcacSDavid Disseldorp if (namelen > size) { 1055355da1ebSSage Weil err = -ERANGE; 1056355da1ebSSage Weil goto out; 10572b2abcacSDavid Disseldorp } 1058355da1ebSSage Weil names = __copy_xattr_names(ci, names); 10592b2abcacSDavid Disseldorp size -= namelen; 10602b2abcacSDavid Disseldorp } 10612b2abcacSDavid Disseldorp err = namelen; 1062355da1ebSSage Weil out: 1063be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1064355da1ebSSage Weil return err; 1065355da1ebSSage Weil } 1066355da1ebSSage Weil 1067a26feccaSAndreas Gruenbacher static int ceph_sync_setxattr(struct inode *inode, const char *name, 1068355da1ebSSage Weil const char *value, size_t size, int flags) 1069355da1ebSSage Weil { 1070a26feccaSAndreas Gruenbacher struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); 1071355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1072355da1ebSSage Weil struct ceph_mds_request *req; 10733d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 1074968cd14eSXiubo Li struct ceph_osd_client *osdc = &fsc->client->osdc; 107525e6bae3SYan, Zheng struct ceph_pagelist *pagelist = NULL; 107604303d8aSYan, Zheng int op = CEPH_MDS_OP_SETXATTR; 1077355da1ebSSage Weil int err; 1078355da1ebSSage Weil 10790aeff37aSYan, Zheng if (size > 0) { 108025e6bae3SYan, Zheng /* copy value into pagelist */ 108133165d47SIlya Dryomov pagelist = ceph_pagelist_alloc(GFP_NOFS); 108225e6bae3SYan, Zheng if (!pagelist) 1083355da1ebSSage Weil return -ENOMEM; 108425e6bae3SYan, Zheng 108525e6bae3SYan, Zheng err = ceph_pagelist_append(pagelist, value, size); 108625e6bae3SYan, Zheng if (err) 1087355da1ebSSage Weil goto out; 10880aeff37aSYan, Zheng } else if (!value) { 108904303d8aSYan, Zheng if (flags & CEPH_XATTR_REPLACE) 109004303d8aSYan, Zheng op = CEPH_MDS_OP_RMXATTR; 109104303d8aSYan, Zheng else 109225e6bae3SYan, Zheng flags |= CEPH_XATTR_REMOVE; 1093355da1ebSSage Weil } 1094355da1ebSSage Weil 1095d93231a6SLuís Henriques dout("setxattr value size: %zu\n", size); 1096355da1ebSSage Weil 1097355da1ebSSage Weil /* do request */ 109804303d8aSYan, Zheng req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); 109960d87733SJulia Lawall if (IS_ERR(req)) { 110060d87733SJulia Lawall err = PTR_ERR(req); 110160d87733SJulia Lawall goto out; 110260d87733SJulia Lawall } 1103a149bb9aSSanidhya Kashyap 1104355da1ebSSage Weil req->r_path2 = kstrdup(name, GFP_NOFS); 1105a149bb9aSSanidhya Kashyap if (!req->r_path2) { 1106a149bb9aSSanidhya Kashyap ceph_mdsc_put_request(req); 1107a149bb9aSSanidhya Kashyap err = -ENOMEM; 1108a149bb9aSSanidhya Kashyap goto out; 1109a149bb9aSSanidhya Kashyap } 1110355da1ebSSage Weil 111104303d8aSYan, Zheng if (op == CEPH_MDS_OP_SETXATTR) { 111204303d8aSYan, Zheng req->r_args.setxattr.flags = cpu_to_le32(flags); 1113968cd14eSXiubo Li req->r_args.setxattr.osdmap_epoch = 1114968cd14eSXiubo Li cpu_to_le32(osdc->osdmap->epoch); 111525e6bae3SYan, Zheng req->r_pagelist = pagelist; 111625e6bae3SYan, Zheng pagelist = NULL; 111704303d8aSYan, Zheng } 1118355da1ebSSage Weil 1119a149bb9aSSanidhya Kashyap req->r_inode = inode; 1120a149bb9aSSanidhya Kashyap ihold(inode); 1121a149bb9aSSanidhya Kashyap req->r_num_caps = 1; 1122a149bb9aSSanidhya Kashyap req->r_inode_drop = CEPH_CAP_XATTR_SHARED; 1123a149bb9aSSanidhya Kashyap 1124355da1ebSSage Weil dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); 1125752c8bdcSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req); 1126355da1ebSSage Weil ceph_mdsc_put_request(req); 1127355da1ebSSage Weil dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); 1128355da1ebSSage Weil 1129355da1ebSSage Weil out: 113025e6bae3SYan, Zheng if (pagelist) 113125e6bae3SYan, Zheng ceph_pagelist_release(pagelist); 1132355da1ebSSage Weil return err; 1133355da1ebSSage Weil } 1134355da1ebSSage Weil 1135a26feccaSAndreas Gruenbacher int __ceph_setxattr(struct inode *inode, const char *name, 1136355da1ebSSage Weil const void *value, size_t size, int flags) 1137355da1ebSSage Weil { 1138881a5fa2SAlex Elder struct ceph_vxattr *vxattr; 1139355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1140a26feccaSAndreas Gruenbacher struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 1141f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf = NULL; 114286968ef2SLuis Henriques struct ceph_buffer *old_blob = NULL; 114318fa8b3fSAlex Elder int issued; 1144355da1ebSSage Weil int err; 1145fbc0b970SYan, Zheng int dirty = 0; 1146355da1ebSSage Weil int name_len = strlen(name); 1147355da1ebSSage Weil int val_len = size; 1148355da1ebSSage Weil char *newname = NULL; 1149355da1ebSSage Weil char *newval = NULL; 1150355da1ebSSage Weil struct ceph_inode_xattr *xattr = NULL; 1151355da1ebSSage Weil int required_blob_size; 1152f1919826SYan, Zheng bool check_realm = false; 1153604d1b02SYan, Zheng bool lock_snap_rwsem = false; 1154355da1ebSSage Weil 11552cdeb1e4SAndreas Gruenbacher if (ceph_snap(inode) != CEPH_NOSNAP) 11562cdeb1e4SAndreas Gruenbacher return -EROFS; 1157355da1ebSSage Weil 115806476a69SAlex Elder vxattr = ceph_match_vxattr(inode, name); 1159f1919826SYan, Zheng if (vxattr) { 11604e9906e7SYan, Zheng if (vxattr->flags & VXATTR_FLAG_READONLY) 1161355da1ebSSage Weil return -EOPNOTSUPP; 1162f1919826SYan, Zheng if (value && !strncmp(vxattr->name, "ceph.quota", 10)) 1163f1919826SYan, Zheng check_realm = true; 1164f1919826SYan, Zheng } 1165355da1ebSSage Weil 11663adf654dSSage Weil /* pass any unhandled ceph.* xattrs through to the MDS */ 11673adf654dSSage Weil if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) 11683adf654dSSage Weil goto do_sync_unlocked; 11693adf654dSSage Weil 1170355da1ebSSage Weil /* preallocate memory for xattr name, value, index node */ 1171355da1ebSSage Weil err = -ENOMEM; 117261413c2fSJulia Lawall newname = kmemdup(name, name_len + 1, GFP_NOFS); 1173355da1ebSSage Weil if (!newname) 1174355da1ebSSage Weil goto out; 1175355da1ebSSage Weil 1176355da1ebSSage Weil if (val_len) { 1177b829c195SAlex Elder newval = kmemdup(value, val_len, GFP_NOFS); 1178355da1ebSSage Weil if (!newval) 1179355da1ebSSage Weil goto out; 1180355da1ebSSage Weil } 1181355da1ebSSage Weil 1182355da1ebSSage Weil xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); 1183355da1ebSSage Weil if (!xattr) 1184355da1ebSSage Weil goto out; 1185355da1ebSSage Weil 1186f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush(); 1187f66fd9f0SYan, Zheng if (!prealloc_cf) 1188f66fd9f0SYan, Zheng goto out; 1189f66fd9f0SYan, Zheng 1190be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1191355da1ebSSage Weil retry: 1192355da1ebSSage Weil issued = __ceph_caps_issued(ci, NULL); 1193d93231a6SLuís Henriques required_blob_size = __get_required_blob_size(ci, name_len, val_len); 1194d93231a6SLuís Henriques if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) || 1195d93231a6SLuís Henriques (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) { 1196d93231a6SLuís Henriques dout("%s do sync setxattr: version: %llu size: %d max: %llu\n", 1197d93231a6SLuís Henriques __func__, ci->i_xattrs.version, required_blob_size, 1198d93231a6SLuís Henriques mdsc->mdsmap->m_max_xattr_size); 1199355da1ebSSage Weil goto do_sync; 1200d93231a6SLuís Henriques } 1201604d1b02SYan, Zheng 1202604d1b02SYan, Zheng if (!lock_snap_rwsem && !ci->i_head_snapc) { 1203604d1b02SYan, Zheng lock_snap_rwsem = true; 1204604d1b02SYan, Zheng if (!down_read_trylock(&mdsc->snap_rwsem)) { 1205604d1b02SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1206604d1b02SYan, Zheng down_read(&mdsc->snap_rwsem); 1207604d1b02SYan, Zheng spin_lock(&ci->i_ceph_lock); 1208604d1b02SYan, Zheng goto retry; 1209604d1b02SYan, Zheng } 1210604d1b02SYan, Zheng } 1211604d1b02SYan, Zheng 1212d36e0b62SJeff Layton dout("setxattr %p name '%s' issued %s\n", inode, name, 1213d36e0b62SJeff Layton ceph_cap_string(issued)); 1214355da1ebSSage Weil __build_xattrs(inode); 1215355da1ebSSage Weil 1216355da1ebSSage Weil if (!ci->i_xattrs.prealloc_blob || 1217355da1ebSSage Weil required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { 121818fa8b3fSAlex Elder struct ceph_buffer *blob; 1219355da1ebSSage Weil 1220be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 122186968ef2SLuis Henriques ceph_buffer_put(old_blob); /* Shouldn't be required */ 122286968ef2SLuis Henriques dout(" pre-allocating new blob size=%d\n", required_blob_size); 1223b6c1d5b8SSage Weil blob = ceph_buffer_new(required_blob_size, GFP_NOFS); 1224355da1ebSSage Weil if (!blob) 1225604d1b02SYan, Zheng goto do_sync_unlocked; 1226be655596SSage Weil spin_lock(&ci->i_ceph_lock); 122786968ef2SLuis Henriques /* prealloc_blob can't be released while holding i_ceph_lock */ 1228b6c1d5b8SSage Weil if (ci->i_xattrs.prealloc_blob) 122986968ef2SLuis Henriques old_blob = ci->i_xattrs.prealloc_blob; 1230355da1ebSSage Weil ci->i_xattrs.prealloc_blob = blob; 1231355da1ebSSage Weil goto retry; 1232355da1ebSSage Weil } 1233355da1ebSSage Weil 1234bcdfeb2eSYan, Zheng err = __set_xattr(ci, newname, name_len, newval, val_len, 1235bcdfeb2eSYan, Zheng flags, value ? 1 : -1, &xattr); 123618fa8b3fSAlex Elder 1237fbc0b970SYan, Zheng if (!err) { 1238f66fd9f0SYan, Zheng dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, 1239f66fd9f0SYan, Zheng &prealloc_cf); 1240355da1ebSSage Weil ci->i_xattrs.dirty = true; 1241*7795aef0SJeff Layton inode_set_ctime_current(inode); 1242fbc0b970SYan, Zheng } 124318fa8b3fSAlex Elder 1244be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 124586968ef2SLuis Henriques ceph_buffer_put(old_blob); 1246604d1b02SYan, Zheng if (lock_snap_rwsem) 1247604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1248fca65b4aSSage Weil if (dirty) 1249fca65b4aSSage Weil __mark_inode_dirty(inode, dirty); 1250f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1251355da1ebSSage Weil return err; 1252355da1ebSSage Weil 1253355da1ebSSage Weil do_sync: 1254be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 12553adf654dSSage Weil do_sync_unlocked: 1256604d1b02SYan, Zheng if (lock_snap_rwsem) 1257604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem); 1258315f2408SYan, Zheng 1259315f2408SYan, Zheng /* security module set xattr while filling trace */ 1260d37b1d99SMarkus Elfring if (current->journal_info) { 1261315f2408SYan, Zheng pr_warn_ratelimited("sync setxattr %p " 1262315f2408SYan, Zheng "during filling trace\n", inode); 1263315f2408SYan, Zheng err = -EBUSY; 1264315f2408SYan, Zheng } else { 1265a26feccaSAndreas Gruenbacher err = ceph_sync_setxattr(inode, name, value, size, flags); 1266f1919826SYan, Zheng if (err >= 0 && check_realm) { 1267f1919826SYan, Zheng /* check if snaprealm was created for quota inode */ 1268f1919826SYan, Zheng spin_lock(&ci->i_ceph_lock); 1269f1919826SYan, Zheng if ((ci->i_max_files || ci->i_max_bytes) && 1270f1919826SYan, Zheng !(ci->i_snap_realm && 1271f1919826SYan, Zheng ci->i_snap_realm->ino == ci->i_vino.ino)) 1272f1919826SYan, Zheng err = -EOPNOTSUPP; 1273f1919826SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1274f1919826SYan, Zheng } 1275315f2408SYan, Zheng } 1276355da1ebSSage Weil out: 1277f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf); 1278355da1ebSSage Weil kfree(newname); 1279355da1ebSSage Weil kfree(newval); 1280355da1ebSSage Weil kfree(xattr); 1281355da1ebSSage Weil return err; 1282355da1ebSSage Weil } 1283355da1ebSSage Weil 12842cdeb1e4SAndreas Gruenbacher static int ceph_get_xattr_handler(const struct xattr_handler *handler, 12852cdeb1e4SAndreas Gruenbacher struct dentry *dentry, struct inode *inode, 12862cdeb1e4SAndreas Gruenbacher const char *name, void *value, size_t size) 12877221fe4cSGuangliang Zhao { 12882cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 12892cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 12902cdeb1e4SAndreas Gruenbacher return __ceph_getxattr(inode, name, value, size); 12917221fe4cSGuangliang Zhao } 1292315f2408SYan, Zheng 12932cdeb1e4SAndreas Gruenbacher static int ceph_set_xattr_handler(const struct xattr_handler *handler, 129439f60c1cSChristian Brauner struct mnt_idmap *idmap, 129559301226SAl Viro struct dentry *unused, struct inode *inode, 129659301226SAl Viro const char *name, const void *value, 129759301226SAl Viro size_t size, int flags) 12982cdeb1e4SAndreas Gruenbacher { 12992cdeb1e4SAndreas Gruenbacher if (!ceph_is_valid_xattr(name)) 13002cdeb1e4SAndreas Gruenbacher return -EOPNOTSUPP; 130159301226SAl Viro return __ceph_setxattr(inode, name, value, size, flags); 13022cdeb1e4SAndreas Gruenbacher } 13032cdeb1e4SAndreas Gruenbacher 13045130cceaSWei Yongjun static const struct xattr_handler ceph_other_xattr_handler = { 13052cdeb1e4SAndreas Gruenbacher .prefix = "", /* match any name => handlers called with full name */ 13062cdeb1e4SAndreas Gruenbacher .get = ceph_get_xattr_handler, 13072cdeb1e4SAndreas Gruenbacher .set = ceph_set_xattr_handler, 13082cdeb1e4SAndreas Gruenbacher }; 13092cdeb1e4SAndreas Gruenbacher 1310315f2408SYan, Zheng #ifdef CONFIG_SECURITY 1311315f2408SYan, Zheng bool ceph_security_xattr_wanted(struct inode *in) 1312315f2408SYan, Zheng { 1313315f2408SYan, Zheng return in->i_security != NULL; 1314315f2408SYan, Zheng } 1315315f2408SYan, Zheng 1316315f2408SYan, Zheng bool ceph_security_xattr_deadlock(struct inode *in) 1317315f2408SYan, Zheng { 1318315f2408SYan, Zheng struct ceph_inode_info *ci; 1319315f2408SYan, Zheng bool ret; 1320d37b1d99SMarkus Elfring if (!in->i_security) 1321315f2408SYan, Zheng return false; 1322315f2408SYan, Zheng ci = ceph_inode(in); 1323315f2408SYan, Zheng spin_lock(&ci->i_ceph_lock); 1324315f2408SYan, Zheng ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && 1325315f2408SYan, Zheng !(ci->i_xattrs.version > 0 && 1326315f2408SYan, Zheng __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); 1327315f2408SYan, Zheng spin_unlock(&ci->i_ceph_lock); 1328315f2408SYan, Zheng return ret; 1329315f2408SYan, Zheng } 1330ac6713ccSYan, Zheng 1331ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1332ac6713ccSYan, Zheng int ceph_security_init_secctx(struct dentry *dentry, umode_t mode, 1333ac6713ccSYan, Zheng struct ceph_acl_sec_ctx *as_ctx) 1334ac6713ccSYan, Zheng { 1335ac6713ccSYan, Zheng struct ceph_pagelist *pagelist = as_ctx->pagelist; 1336ac6713ccSYan, Zheng const char *name; 1337ac6713ccSYan, Zheng size_t name_len; 1338ac6713ccSYan, Zheng int err; 1339ac6713ccSYan, Zheng 1340ac6713ccSYan, Zheng err = security_dentry_init_security(dentry, mode, &dentry->d_name, 134115bf3239SVivek Goyal &name, &as_ctx->sec_ctx, 1342ac6713ccSYan, Zheng &as_ctx->sec_ctxlen); 1343ac6713ccSYan, Zheng if (err < 0) { 1344ac6713ccSYan, Zheng WARN_ON_ONCE(err != -EOPNOTSUPP); 1345ac6713ccSYan, Zheng err = 0; /* do nothing */ 1346ac6713ccSYan, Zheng goto out; 1347ac6713ccSYan, Zheng } 1348ac6713ccSYan, Zheng 1349ac6713ccSYan, Zheng err = -ENOMEM; 1350ac6713ccSYan, Zheng if (!pagelist) { 1351ac6713ccSYan, Zheng pagelist = ceph_pagelist_alloc(GFP_KERNEL); 1352ac6713ccSYan, Zheng if (!pagelist) 1353ac6713ccSYan, Zheng goto out; 1354ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 1355ac6713ccSYan, Zheng if (err) 1356ac6713ccSYan, Zheng goto out; 1357ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, 1); 1358ac6713ccSYan, Zheng } 1359ac6713ccSYan, Zheng 1360ac6713ccSYan, Zheng /* 1361ac6713ccSYan, Zheng * FIXME: Make security_dentry_init_security() generic. Currently 1362ac6713ccSYan, Zheng * It only supports single security module and only selinux has 1363ac6713ccSYan, Zheng * dentry_init_security hook. 1364ac6713ccSYan, Zheng */ 1365ac6713ccSYan, Zheng name_len = strlen(name); 1366ac6713ccSYan, Zheng err = ceph_pagelist_reserve(pagelist, 1367ac6713ccSYan, Zheng 4 * 2 + name_len + as_ctx->sec_ctxlen); 1368ac6713ccSYan, Zheng if (err) 1369ac6713ccSYan, Zheng goto out; 1370ac6713ccSYan, Zheng 1371ac6713ccSYan, Zheng if (as_ctx->pagelist) { 1372ac6713ccSYan, Zheng /* update count of KV pairs */ 1373ac6713ccSYan, Zheng BUG_ON(pagelist->length <= sizeof(__le32)); 1374ac6713ccSYan, Zheng if (list_is_singular(&pagelist->head)) { 1375ac6713ccSYan, Zheng le32_add_cpu((__le32*)pagelist->mapped_tail, 1); 1376ac6713ccSYan, Zheng } else { 1377ac6713ccSYan, Zheng struct page *page = list_first_entry(&pagelist->head, 1378ac6713ccSYan, Zheng struct page, lru); 1379ac6713ccSYan, Zheng void *addr = kmap_atomic(page); 1380ac6713ccSYan, Zheng le32_add_cpu((__le32*)addr, 1); 1381ac6713ccSYan, Zheng kunmap_atomic(addr); 1382ac6713ccSYan, Zheng } 1383ac6713ccSYan, Zheng } else { 1384ac6713ccSYan, Zheng as_ctx->pagelist = pagelist; 1385ac6713ccSYan, Zheng } 1386ac6713ccSYan, Zheng 1387ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, name_len); 1388ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, name, name_len); 1389ac6713ccSYan, Zheng 1390ac6713ccSYan, Zheng ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen); 1391ac6713ccSYan, Zheng ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1392ac6713ccSYan, Zheng 1393ac6713ccSYan, Zheng err = 0; 1394ac6713ccSYan, Zheng out: 1395ac6713ccSYan, Zheng if (pagelist && !as_ctx->pagelist) 1396ac6713ccSYan, Zheng ceph_pagelist_release(pagelist); 1397ac6713ccSYan, Zheng return err; 1398ac6713ccSYan, Zheng } 1399668959a5SJeff Layton #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */ 1400668959a5SJeff Layton #endif /* CONFIG_SECURITY */ 14015c31e92dSYan, Zheng 14025c31e92dSYan, Zheng void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx) 14035c31e92dSYan, Zheng { 14045c31e92dSYan, Zheng #ifdef CONFIG_CEPH_FS_POSIX_ACL 14055c31e92dSYan, Zheng posix_acl_release(as_ctx->acl); 14065c31e92dSYan, Zheng posix_acl_release(as_ctx->default_acl); 14075c31e92dSYan, Zheng #endif 1408ac6713ccSYan, Zheng #ifdef CONFIG_CEPH_FS_SECURITY_LABEL 1409ac6713ccSYan, Zheng security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen); 1410ac6713ccSYan, Zheng #endif 14115c31e92dSYan, Zheng if (as_ctx->pagelist) 14125c31e92dSYan, Zheng ceph_pagelist_release(as_ctx->pagelist); 14135c31e92dSYan, Zheng } 1414ac6713ccSYan, Zheng 1415ac6713ccSYan, Zheng /* 1416ac6713ccSYan, Zheng * List of handlers for synthetic system.* attributes. Other 1417ac6713ccSYan, Zheng * attributes are handled directly. 1418ac6713ccSYan, Zheng */ 1419ac6713ccSYan, Zheng const struct xattr_handler *ceph_xattr_handlers[] = { 1420ac6713ccSYan, Zheng &ceph_other_xattr_handler, 1421ac6713ccSYan, Zheng NULL, 1422ac6713ccSYan, Zheng }; 1423