blob: 939eab7aa219d74c652263d4c85c249b7b94898f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Yan, Zheng25e6bae2014-09-16 19:15:28 +08003#include <linux/ceph/pagelist.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004
Sage Weil355da1e2009-10-06 11:31:08 -07005#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07006#include "mds_client.h"
7
8#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07009
10#include <linux/xattr.h>
Yan, Zhengac6713c2019-05-26 16:27:56 +080011#include <linux/security.h>
Linus Torvalds4db658e2014-01-28 18:06:18 -080012#include <linux/posix_acl_xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070014
Alex Elder22891902012-01-23 15:49:28 -060015#define XATTR_CEPH_PREFIX "ceph."
16#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
17
Yan, Zhengbcdfeb22014-02-11 13:04:19 +080018static int __remove_xattr(struct ceph_inode_info *ci,
19 struct ceph_inode_xattr *xattr);
20
Sage Weil355da1e2009-10-06 11:31:08 -070021static bool ceph_is_valid_xattr(const char *name)
22{
Alex Elder22891902012-01-23 15:49:28 -060023 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil355da1e2009-10-06 11:31:08 -070024 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
25 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
26}
27
28/*
29 * These define virtual xattrs exposing the recursive directory
30 * statistics and layout metadata.
31 */
Alex Elder881a5fa2012-01-23 15:49:28 -060032struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070033 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060034 size_t name_size; /* strlen(name) + 1 (for '\0') */
Jeff Laytonf1d1b512019-06-24 07:39:18 -040035 ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
36 size_t size);
Sage Weilf36e4472013-01-20 21:59:29 -080037 bool (*exists_cb)(struct ceph_inode_info *ci);
Yan, Zheng4e9906e2018-04-25 17:14:05 +080038 unsigned int flags;
Sage Weil355da1e2009-10-06 11:31:08 -070039};
40
Yan, Zheng4e9906e2018-04-25 17:14:05 +080041#define VXATTR_FLAG_READONLY (1<<0)
42#define VXATTR_FLAG_HIDDEN (1<<1)
Yan, Zheng49a9f4f2018-04-25 17:30:23 +080043#define VXATTR_FLAG_RSTAT (1<<2)
Yan, Zheng4e9906e2018-04-25 17:14:05 +080044
Sage Weil32ab0bd2013-01-19 16:46:32 -080045/* layouts */
46
47static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
48{
Yan, Zheng779fe0f2016-03-07 09:35:06 +080049 struct ceph_file_layout *fl = &ci->i_layout;
50 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
51 fl->object_size > 0 || fl->pool_id >= 0 ||
52 rcu_dereference_raw(fl->pool_ns) != NULL);
Sage Weil32ab0bd2013-01-19 16:46:32 -080053}
54
Jeff Laytonf1d1b512019-06-24 07:39:18 -040055static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
56 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080057{
Sage Weil32ab0bd2013-01-19 16:46:32 -080058 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
59 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080060 struct ceph_string *pool_ns;
Yan, Zheng76271512016-02-03 21:24:49 +080061 s64 pool = ci->i_layout.pool_id;
Sage Weil32ab0bd2013-01-19 16:46:32 -080062 const char *pool_name;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080063 const char *ns_field = " pool_namespace=";
Yan, Zheng1e5c6642014-03-24 13:00:54 +080064 char buf[128];
Yan, Zheng779fe0f2016-03-07 09:35:06 +080065 size_t len, total_len = 0;
Jeff Layton3b421012019-06-13 15:17:00 -040066 ssize_t ret;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080067
68 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -080069
70 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020071 down_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -080072 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080073 if (pool_name) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080074 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080075 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
76 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
77 ci->i_layout.object_size);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080078 total_len = len + strlen(pool_name);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080079 } else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080080 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080081 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
82 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
Jeff Laytonf1d1b512019-06-24 07:39:18 -040083 ci->i_layout.object_size, pool);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080084 total_len = len;
85 }
86
87 if (pool_ns)
88 total_len += strlen(ns_field) + pool_ns->len;
89
Jeff Layton3b421012019-06-13 15:17:00 -040090 ret = total_len;
91 if (size >= total_len) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080092 memcpy(val, buf, len);
93 ret = len;
94 if (pool_name) {
95 len = strlen(pool_name);
96 memcpy(val + ret, pool_name, len);
97 ret += len;
98 }
99 if (pool_ns) {
100 len = strlen(ns_field);
101 memcpy(val + ret, ns_field, len);
102 ret += len;
103 memcpy(val + ret, pool_ns->str, pool_ns->len);
104 ret += pool_ns->len;
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800105 }
106 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200107 up_read(&osdc->lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800108 ceph_put_string(pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800109 return ret;
110}
111
Jeff Layton26350532019-06-24 07:32:21 -0400112/*
113 * The convention with strings in xattrs is that they should not be NULL
114 * terminated, since we're returning the length with them. snprintf always
115 * NULL terminates however, so call it on a temporary buffer and then memcpy
116 * the result into place.
117 */
118static int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
119{
120 int ret;
121 va_list args;
122 char buf[96]; /* NB: reevaluate size if new vxattrs are added */
123
124 va_start(args, fmt);
125 ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
126 va_end(args);
127
128 /* Sanity check */
129 if (size && ret + 1 > sizeof(buf)) {
130 WARN_ONCE(true, "Returned length too big (%d)", ret);
131 return -E2BIG;
132 }
133
134 if (ret <= size)
135 memcpy(val, buf, ret);
136 return ret;
137}
138
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400139static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
140 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800141{
Jeff Layton26350532019-06-24 07:32:21 -0400142 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
Sage Weil695b7112013-01-20 22:08:33 -0800143}
144
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400145static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
146 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800147{
Jeff Layton26350532019-06-24 07:32:21 -0400148 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
Sage Weil695b7112013-01-20 22:08:33 -0800149}
150
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400151static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
152 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800153{
Jeff Layton26350532019-06-24 07:32:21 -0400154 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
Sage Weil695b7112013-01-20 22:08:33 -0800155}
156
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400157static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
158 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800159{
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400160 ssize_t ret;
Sage Weil695b7112013-01-20 22:08:33 -0800161 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
162 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng76271512016-02-03 21:24:49 +0800163 s64 pool = ci->i_layout.pool_id;
Sage Weil695b7112013-01-20 22:08:33 -0800164 const char *pool_name;
165
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200166 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800167 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Jeff Layton26350532019-06-24 07:32:21 -0400168 if (pool_name) {
169 ret = strlen(pool_name);
170 if (ret <= size)
171 memcpy(val, pool_name, ret);
172 } else {
173 ret = ceph_fmt_xattr(val, size, "%lld", pool);
174 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200175 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800176 return ret;
177}
178
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400179static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
180 char *val, size_t size)
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800181{
Jeff Layton26350532019-06-24 07:32:21 -0400182 ssize_t ret = 0;
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800183 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
Jeff Layton26350532019-06-24 07:32:21 -0400184
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800185 if (ns) {
Jeff Layton26350532019-06-24 07:32:21 -0400186 ret = ns->len;
187 if (ret <= size)
188 memcpy(val, ns->str, ret);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800189 ceph_put_string(ns);
190 }
191 return ret;
192}
193
Sage Weil355da1e2009-10-06 11:31:08 -0700194/* directories */
195
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400196static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
197 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700198{
Jeff Layton26350532019-06-24 07:32:21 -0400199 return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700200}
201
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400202static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
203 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700204{
Jeff Layton26350532019-06-24 07:32:21 -0400205 return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
Sage Weil355da1e2009-10-06 11:31:08 -0700206}
207
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400208static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
209 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700210{
Jeff Layton26350532019-06-24 07:32:21 -0400211 return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700212}
213
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400214static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
215 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700216{
Jeff Layton26350532019-06-24 07:32:21 -0400217 return ceph_fmt_xattr(val, size, "%lld",
218 ci->i_rfiles + ci->i_rsubdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700219}
220
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400221static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
222 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700223{
Jeff Layton26350532019-06-24 07:32:21 -0400224 return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
Sage Weil355da1e2009-10-06 11:31:08 -0700225}
226
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400227static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
228 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700229{
Jeff Layton26350532019-06-24 07:32:21 -0400230 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700231}
232
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400233static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
234 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700235{
Jeff Layton26350532019-06-24 07:32:21 -0400236 return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
Sage Weil355da1e2009-10-06 11:31:08 -0700237}
238
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400239static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
240 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700241{
Jeff Layton26350532019-06-24 07:32:21 -0400242 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
243 ci->i_rctime.tv_nsec);
Sage Weil355da1e2009-10-06 11:31:08 -0700244}
245
Yan, Zheng08796872019-01-09 11:07:02 +0800246/* dir pin */
247static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
248{
249 return ci->i_dir_pin != -ENODATA;
250}
Luis Henriquesfb18a572018-01-05 10:47:18 +0000251
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400252static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
253 size_t size)
Yan, Zheng08796872019-01-09 11:07:02 +0800254{
Jeff Layton26350532019-06-24 07:32:21 -0400255 return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
Yan, Zheng08796872019-01-09 11:07:02 +0800256}
257
258/* quotas */
Luis Henriquesfb18a572018-01-05 10:47:18 +0000259static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
260{
Yan, Zhengf1919822018-04-08 09:54:39 +0800261 bool ret = false;
262 spin_lock(&ci->i_ceph_lock);
263 if ((ci->i_max_files || ci->i_max_bytes) &&
264 ci->i_vino.snap == CEPH_NOSNAP &&
265 ci->i_snap_realm &&
266 ci->i_snap_realm->ino == ci->i_vino.ino)
267 ret = true;
268 spin_unlock(&ci->i_ceph_lock);
269 return ret;
Luis Henriquesfb18a572018-01-05 10:47:18 +0000270}
271
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400272static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
273 size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000274{
Jeff Layton26350532019-06-24 07:32:21 -0400275 return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
276 ci->i_max_bytes, ci->i_max_files);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000277}
278
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400279static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
280 char *val, size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000281{
Jeff Layton26350532019-06-24 07:32:21 -0400282 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000283}
284
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400285static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
286 char *val, size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000287{
Jeff Layton26350532019-06-24 07:32:21 -0400288 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000289}
Sage Weil32ab0bd2013-01-19 16:46:32 -0800290
David Disseldorp100cc612019-04-18 14:15:47 +0200291/* snapshots */
292static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
293{
294 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
295}
296
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400297static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
298 size_t size)
David Disseldorp100cc612019-04-18 14:15:47 +0200299{
Jeff Layton26350532019-06-24 07:32:21 -0400300 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
301 ci->i_snap_btime.tv_nsec);
David Disseldorp100cc612019-04-18 14:15:47 +0200302}
303
Alex Eldereb788082012-01-23 15:49:28 -0600304#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800305#define CEPH_XATTR_NAME2(_type, _name, _name2) \
306 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600307
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800308#define XATTR_NAME_CEPH(_type, _name, _flags) \
Sage Weil88601472013-01-31 11:53:27 -0800309 { \
310 .name = CEPH_XATTR_NAME(_type, _name), \
311 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
312 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800313 .exists_cb = NULL, \
314 .flags = (VXATTR_FLAG_READONLY | _flags), \
Sage Weil88601472013-01-31 11:53:27 -0800315 }
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800316#define XATTR_RSTAT_FIELD(_type, _name) \
317 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
Sage Weil695b7112013-01-20 22:08:33 -0800318#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
319 { \
320 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
321 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
322 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
Sage Weil695b7112013-01-20 22:08:33 -0800323 .exists_cb = ceph_vxattrcb_layout_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800324 .flags = VXATTR_FLAG_HIDDEN, \
Sage Weil695b7112013-01-20 22:08:33 -0800325 }
Luis Henriquesfb18a572018-01-05 10:47:18 +0000326#define XATTR_QUOTA_FIELD(_type, _name) \
327 { \
328 .name = CEPH_XATTR_NAME(_type, _name), \
329 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
330 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000331 .exists_cb = ceph_vxattrcb_quota_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800332 .flags = VXATTR_FLAG_HIDDEN, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000333 }
Alex Eldereb788082012-01-23 15:49:28 -0600334
Alex Elder881a5fa2012-01-23 15:49:28 -0600335static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800336 {
337 .name = "ceph.dir.layout",
338 .name_size = sizeof("ceph.dir.layout"),
339 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800340 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800341 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800342 },
Sage Weil695b7112013-01-20 22:08:33 -0800343 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
344 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
345 XATTR_LAYOUT_FIELD(dir, layout, object_size),
346 XATTR_LAYOUT_FIELD(dir, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800347 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800348 XATTR_NAME_CEPH(dir, entries, 0),
349 XATTR_NAME_CEPH(dir, files, 0),
350 XATTR_NAME_CEPH(dir, subdirs, 0),
351 XATTR_RSTAT_FIELD(dir, rentries),
352 XATTR_RSTAT_FIELD(dir, rfiles),
353 XATTR_RSTAT_FIELD(dir, rsubdirs),
354 XATTR_RSTAT_FIELD(dir, rbytes),
355 XATTR_RSTAT_FIELD(dir, rctime),
Luis Henriquesfb18a572018-01-05 10:47:18 +0000356 {
Yan, Zheng08796872019-01-09 11:07:02 +0800357 .name = "ceph.dir.pin",
David Disseldorpe1b81432019-04-18 14:15:45 +0200358 .name_size = sizeof("ceph.dir.pin"),
Yan, Zheng08796872019-01-09 11:07:02 +0800359 .getxattr_cb = ceph_vxattrcb_dir_pin,
360 .exists_cb = ceph_vxattrcb_dir_pin_exists,
361 .flags = VXATTR_FLAG_HIDDEN,
362 },
363 {
Luis Henriquesfb18a572018-01-05 10:47:18 +0000364 .name = "ceph.quota",
365 .name_size = sizeof("ceph.quota"),
366 .getxattr_cb = ceph_vxattrcb_quota,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000367 .exists_cb = ceph_vxattrcb_quota_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800368 .flags = VXATTR_FLAG_HIDDEN,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000369 },
370 XATTR_QUOTA_FIELD(quota, max_bytes),
371 XATTR_QUOTA_FIELD(quota, max_files),
David Disseldorp100cc612019-04-18 14:15:47 +0200372 {
373 .name = "ceph.snap.btime",
374 .name_size = sizeof("ceph.snap.btime"),
375 .getxattr_cb = ceph_vxattrcb_snap_btime,
376 .exists_cb = ceph_vxattrcb_snap_btime_exists,
377 .flags = VXATTR_FLAG_READONLY,
378 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600379 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700380};
381
382/* files */
383
Alex Elder881a5fa2012-01-23 15:49:28 -0600384static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800385 {
386 .name = "ceph.file.layout",
387 .name_size = sizeof("ceph.file.layout"),
388 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800389 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800390 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800391 },
Sage Weil695b7112013-01-20 22:08:33 -0800392 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
393 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
394 XATTR_LAYOUT_FIELD(file, layout, object_size),
395 XATTR_LAYOUT_FIELD(file, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800396 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
David Disseldorp100cc612019-04-18 14:15:47 +0200397 {
398 .name = "ceph.snap.btime",
399 .name_size = sizeof("ceph.snap.btime"),
400 .getxattr_cb = ceph_vxattrcb_snap_btime,
401 .exists_cb = ceph_vxattrcb_snap_btime_exists,
402 .flags = VXATTR_FLAG_READONLY,
403 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600404 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700405};
406
Alex Elder881a5fa2012-01-23 15:49:28 -0600407static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700408{
409 if (S_ISDIR(inode->i_mode))
410 return ceph_dir_vxattrs;
411 else if (S_ISREG(inode->i_mode))
412 return ceph_file_vxattrs;
413 return NULL;
414}
415
Alex Elder881a5fa2012-01-23 15:49:28 -0600416static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700417 const char *name)
418{
Alex Elder881a5fa2012-01-23 15:49:28 -0600419 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600420
421 if (vxattr) {
422 while (vxattr->name) {
423 if (!strcmp(vxattr->name, name))
424 return vxattr;
425 vxattr++;
426 }
427 }
428
Sage Weil355da1e2009-10-06 11:31:08 -0700429 return NULL;
430}
431
432static int __set_xattr(struct ceph_inode_info *ci,
433 const char *name, int name_len,
434 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800435 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700436 struct ceph_inode_xattr **newxattr)
437{
438 struct rb_node **p;
439 struct rb_node *parent = NULL;
440 struct ceph_inode_xattr *xattr = NULL;
441 int c;
442 int new = 0;
443
444 p = &ci->i_xattrs.index.rb_node;
445 while (*p) {
446 parent = *p;
447 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
448 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
449 if (c < 0)
450 p = &(*p)->rb_left;
451 else if (c > 0)
452 p = &(*p)->rb_right;
453 else {
454 if (name_len == xattr->name_len)
455 break;
456 else if (name_len < xattr->name_len)
457 p = &(*p)->rb_left;
458 else
459 p = &(*p)->rb_right;
460 }
461 xattr = NULL;
462 }
463
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800464 if (update_xattr) {
465 int err = 0;
Luis Henriqueseeca9582017-04-28 11:14:04 +0100466
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800467 if (xattr && (flags & XATTR_CREATE))
468 err = -EEXIST;
469 else if (!xattr && (flags & XATTR_REPLACE))
470 err = -ENODATA;
471 if (err) {
472 kfree(name);
473 kfree(val);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100474 kfree(*newxattr);
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800475 return err;
476 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800477 if (update_xattr < 0) {
478 if (xattr)
479 __remove_xattr(ci, xattr);
480 kfree(name);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100481 kfree(*newxattr);
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800482 return 0;
483 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800484 }
485
Sage Weil355da1e2009-10-06 11:31:08 -0700486 if (!xattr) {
487 new = 1;
488 xattr = *newxattr;
489 xattr->name = name;
490 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800491 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700492
493 ci->i_xattrs.count++;
494 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
495 } else {
496 kfree(*newxattr);
497 *newxattr = NULL;
498 if (xattr->should_free_val)
499 kfree((void *)xattr->val);
500
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800501 if (update_xattr) {
Sage Weil355da1e2009-10-06 11:31:08 -0700502 kfree((void *)name);
503 name = xattr->name;
504 }
505 ci->i_xattrs.names_size -= xattr->name_len;
506 ci->i_xattrs.vals_size -= xattr->val_len;
507 }
Sage Weil355da1e2009-10-06 11:31:08 -0700508 ci->i_xattrs.names_size += name_len;
509 ci->i_xattrs.vals_size += val_len;
510 if (val)
511 xattr->val = val;
512 else
513 xattr->val = "";
514
515 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800516 xattr->dirty = update_xattr;
517 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700518
519 if (new) {
520 rb_link_node(&xattr->node, parent, p);
521 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
522 dout("__set_xattr_val p=%p\n", p);
523 }
524
Yan, Zheng05729782019-05-27 16:15:41 +0800525 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
526 ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
Sage Weil355da1e2009-10-06 11:31:08 -0700527
528 return 0;
529}
530
531static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
532 const char *name)
533{
534 struct rb_node **p;
535 struct rb_node *parent = NULL;
536 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800537 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700538 int c;
539
540 p = &ci->i_xattrs.index.rb_node;
541 while (*p) {
542 parent = *p;
543 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
544 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800545 if (c == 0 && name_len > xattr->name_len)
546 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700547 if (c < 0)
548 p = &(*p)->rb_left;
549 else if (c > 0)
550 p = &(*p)->rb_right;
551 else {
552 dout("__get_xattr %s: found %.*s\n", name,
553 xattr->val_len, xattr->val);
554 return xattr;
555 }
556 }
557
558 dout("__get_xattr %s: not found\n", name);
559
560 return NULL;
561}
562
563static void __free_xattr(struct ceph_inode_xattr *xattr)
564{
565 BUG_ON(!xattr);
566
567 if (xattr->should_free_name)
568 kfree((void *)xattr->name);
569 if (xattr->should_free_val)
570 kfree((void *)xattr->val);
571
572 kfree(xattr);
573}
574
575static int __remove_xattr(struct ceph_inode_info *ci,
576 struct ceph_inode_xattr *xattr)
577{
578 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800579 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700580
581 rb_erase(&xattr->node, &ci->i_xattrs.index);
582
583 if (xattr->should_free_name)
584 kfree((void *)xattr->name);
585 if (xattr->should_free_val)
586 kfree((void *)xattr->val);
587
588 ci->i_xattrs.names_size -= xattr->name_len;
589 ci->i_xattrs.vals_size -= xattr->val_len;
590 ci->i_xattrs.count--;
591 kfree(xattr);
592
593 return 0;
594}
595
Sage Weil355da1e2009-10-06 11:31:08 -0700596static char *__copy_xattr_names(struct ceph_inode_info *ci,
597 char *dest)
598{
599 struct rb_node *p;
600 struct ceph_inode_xattr *xattr = NULL;
601
602 p = rb_first(&ci->i_xattrs.index);
603 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
604
605 while (p) {
606 xattr = rb_entry(p, struct ceph_inode_xattr, node);
607 memcpy(dest, xattr->name, xattr->name_len);
608 dest[xattr->name_len] = '\0';
609
610 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
611 xattr->name_len, ci->i_xattrs.names_size);
612
613 dest += xattr->name_len + 1;
614 p = rb_next(p);
615 }
616
617 return dest;
618}
619
620void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
621{
622 struct rb_node *p, *tmp;
623 struct ceph_inode_xattr *xattr = NULL;
624
625 p = rb_first(&ci->i_xattrs.index);
626
627 dout("__ceph_destroy_xattrs p=%p\n", p);
628
629 while (p) {
630 xattr = rb_entry(p, struct ceph_inode_xattr, node);
631 tmp = p;
632 p = rb_next(tmp);
633 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
634 xattr->name_len, xattr->name);
635 rb_erase(tmp, &ci->i_xattrs.index);
636
637 __free_xattr(xattr);
638 }
639
640 ci->i_xattrs.names_size = 0;
641 ci->i_xattrs.vals_size = 0;
642 ci->i_xattrs.index_version = 0;
643 ci->i_xattrs.count = 0;
644 ci->i_xattrs.index = RB_ROOT;
645}
646
647static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800648 __releases(ci->i_ceph_lock)
649 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700650{
651 u32 namelen;
652 u32 numattr = 0;
653 void *p, *end;
654 u32 len;
655 const char *name, *val;
656 struct ceph_inode_info *ci = ceph_inode(inode);
657 int xattr_version;
658 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800659 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700660 int i;
661
662 dout("__build_xattrs() len=%d\n",
663 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
664
665 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
666 return 0; /* already built */
667
668 __ceph_destroy_xattrs(ci);
669
670start:
671 /* updated internal xattr rb tree */
672 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
673 p = ci->i_xattrs.blob->vec.iov_base;
674 end = p + ci->i_xattrs.blob->vec.iov_len;
675 ceph_decode_32_safe(&p, end, numattr, bad);
676 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800677 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700678
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400679 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700680 GFP_NOFS);
681 err = -ENOMEM;
682 if (!xattrs)
683 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400684
Sage Weil355da1e2009-10-06 11:31:08 -0700685 for (i = 0; i < numattr; i++) {
686 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
687 GFP_NOFS);
688 if (!xattrs[i])
689 goto bad_lock;
690 }
691
Sage Weilbe655592011-11-30 09:47:09 -0800692 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700693 if (ci->i_xattrs.version != xattr_version) {
694 /* lost a race, retry */
695 for (i = 0; i < numattr; i++)
696 kfree(xattrs[i]);
697 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500698 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700699 goto start;
700 }
701 err = -EIO;
702 while (numattr--) {
703 ceph_decode_32_safe(&p, end, len, bad);
704 namelen = len;
705 name = p;
706 p += len;
707 ceph_decode_32_safe(&p, end, len, bad);
708 val = p;
709 p += len;
710
711 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800712 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700713
714 if (err < 0)
715 goto bad;
716 }
717 kfree(xattrs);
718 }
719 ci->i_xattrs.index_version = ci->i_xattrs.version;
720 ci->i_xattrs.dirty = false;
721
722 return err;
723bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800724 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700725bad:
726 if (xattrs) {
727 for (i = 0; i < numattr; i++)
728 kfree(xattrs[i]);
729 kfree(xattrs);
730 }
731 ci->i_xattrs.names_size = 0;
732 return err;
733}
734
735static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
736 int val_size)
737{
738 /*
739 * 4 bytes for the length, and additional 4 bytes per each xattr name,
740 * 4 bytes per each value
741 */
742 int size = 4 + ci->i_xattrs.count*(4 + 4) +
743 ci->i_xattrs.names_size +
744 ci->i_xattrs.vals_size;
745 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
746 ci->i_xattrs.count, ci->i_xattrs.names_size,
747 ci->i_xattrs.vals_size);
748
749 if (name_size)
750 size += 4 + 4 + name_size + val_size;
751
752 return size;
753}
754
755/*
756 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100757 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
758 * that it can be freed by the caller as the i_ceph_lock is likely to be
759 * held.
Sage Weil355da1e2009-10-06 11:31:08 -0700760 */
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100761struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
Sage Weil355da1e2009-10-06 11:31:08 -0700762{
763 struct rb_node *p;
764 struct ceph_inode_xattr *xattr = NULL;
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100765 struct ceph_buffer *old_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700766 void *dest;
767
768 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
769 if (ci->i_xattrs.dirty) {
770 int need = __get_required_blob_size(ci, 0, 0);
771
772 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
773
774 p = rb_first(&ci->i_xattrs.index);
775 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
776
777 ceph_encode_32(&dest, ci->i_xattrs.count);
778 while (p) {
779 xattr = rb_entry(p, struct ceph_inode_xattr, node);
780
781 ceph_encode_32(&dest, xattr->name_len);
782 memcpy(dest, xattr->name, xattr->name_len);
783 dest += xattr->name_len;
784 ceph_encode_32(&dest, xattr->val_len);
785 memcpy(dest, xattr->val, xattr->val_len);
786 dest += xattr->val_len;
787
788 p = rb_next(p);
789 }
790
791 /* adjust buffer len; it may be larger than we need */
792 ci->i_xattrs.prealloc_blob->vec.iov_len =
793 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
794
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800795 if (ci->i_xattrs.blob)
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100796 old_blob = ci->i_xattrs.blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700797 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
798 ci->i_xattrs.prealloc_blob = NULL;
799 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700800 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700801 }
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100802
803 return old_blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700804}
805
Yan, Zheng315f2402016-03-07 10:34:50 +0800806static inline int __get_request_mask(struct inode *in) {
807 struct ceph_mds_request *req = current->journal_info;
808 int mask = 0;
809 if (req && req->r_target_inode == in) {
810 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
811 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
812 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
813 req->r_op == CEPH_MDS_OP_GETATTR) {
814 mask = le32_to_cpu(req->r_args.getattr.mask);
815 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
816 req->r_op == CEPH_MDS_OP_CREATE) {
817 mask = le32_to_cpu(req->r_args.open.mask);
818 }
819 }
820 return mask;
821}
822
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800823ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700824 size_t size)
825{
Sage Weil355da1e2009-10-06 11:31:08 -0700826 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700827 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600828 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800829 int req_mask;
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400830 ssize_t err;
Sage Weil355da1e2009-10-06 11:31:08 -0700831
Sage Weil0bee82f2013-01-20 22:00:58 -0800832 /* let's see if a virtual xattr was requested */
833 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800834 if (vxattr) {
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800835 int mask = 0;
836 if (vxattr->flags & VXATTR_FLAG_RSTAT)
837 mask |= CEPH_STAT_RSTAT;
838 err = ceph_do_getattr(inode, mask, true);
Yan, Zheng1684dd02017-06-14 15:54:56 +0800839 if (err)
840 return err;
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800841 err = -ENODATA;
Jeff Layton3b421012019-06-13 15:17:00 -0400842 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800843 err = vxattr->getxattr_cb(ci, value, size);
Jeff Layton3b421012019-06-13 15:17:00 -0400844 if (size && size < err)
845 err = -ERANGE;
846 }
majianpenga1dc1932013-06-19 14:58:10 +0800847 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800848 }
849
Yan, Zheng315f2402016-03-07 10:34:50 +0800850 req_mask = __get_request_mask(inode);
851
majianpenga1dc1932013-06-19 14:58:10 +0800852 spin_lock(&ci->i_ceph_lock);
853 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
854 ci->i_xattrs.version, ci->i_xattrs.index_version);
855
Yan, Zheng508b32d2014-09-16 21:46:17 +0800856 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800857 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
858 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800859 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800860
861 /* security module gets xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200862 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +0800863 pr_warn_ratelimited("sync getxattr %p "
864 "during filling trace\n", inode);
865 return -EBUSY;
866 }
867
Sage Weil355da1e2009-10-06 11:31:08 -0700868 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800869 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700870 if (err)
871 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800872 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700873 }
874
Sage Weil355da1e2009-10-06 11:31:08 -0700875 err = __build_xattrs(inode);
876 if (err < 0)
877 goto out;
878
Sage Weil355da1e2009-10-06 11:31:08 -0700879 err = -ENODATA; /* == ENOATTR */
880 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800881 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700882 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700883
884 err = -ERANGE;
885 if (size && size < xattr->val_len)
886 goto out;
887
888 err = xattr->val_len;
889 if (size == 0)
890 goto out;
891
892 memcpy(value, xattr->val, xattr->val_len);
893
Markus Elfringd37b1d92017-08-20 20:22:02 +0200894 if (current->journal_info &&
Yan, Zheng315f2402016-03-07 10:34:50 +0800895 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
896 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700897out:
Sage Weilbe655592011-11-30 09:47:09 -0800898 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700899 return err;
900}
901
902ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
903{
David Howells2b0143b2015-03-17 22:25:59 +0000904 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700905 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600906 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
David Disseldorp2b2abca2019-04-18 14:15:48 +0200907 bool len_only = (size == 0);
Sage Weil355da1e2009-10-06 11:31:08 -0700908 u32 namelen;
909 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700910 int i;
911
Sage Weilbe655592011-11-30 09:47:09 -0800912 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700913 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
914 ci->i_xattrs.version, ci->i_xattrs.index_version);
915
Yan, Zheng508b32d2014-09-16 21:46:17 +0800916 if (ci->i_xattrs.version == 0 ||
917 !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800918 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800919 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700920 if (err)
921 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800922 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700923 }
924
Sage Weil355da1e2009-10-06 11:31:08 -0700925 err = __build_xattrs(inode);
926 if (err < 0)
927 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600928
David Disseldorp2b2abca2019-04-18 14:15:48 +0200929 /* add 1 byte for each xattr due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -0800930 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
David Disseldorp2b2abca2019-04-18 14:15:48 +0200931 if (!len_only) {
932 if (namelen > size) {
933 err = -ERANGE;
934 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700935 }
David Disseldorp2b2abca2019-04-18 14:15:48 +0200936 names = __copy_xattr_names(ci, names);
937 size -= namelen;
Sage Weilb65917d2013-01-20 22:02:39 -0800938 }
Sage Weil355da1e2009-10-06 11:31:08 -0700939
David Disseldorp2b2abca2019-04-18 14:15:48 +0200940
941 /* virtual xattr names, too */
942 if (vxattrs) {
943 for (i = 0; vxattrs[i].name; i++) {
944 size_t this_len;
945
946 if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN)
947 continue;
948 if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci))
949 continue;
950
951 this_len = strlen(vxattrs[i].name) + 1;
952 namelen += this_len;
953 if (len_only)
954 continue;
955
956 if (this_len > size) {
957 err = -ERANGE;
958 goto out;
959 }
960
961 memcpy(names, vxattrs[i].name, this_len);
962 names += this_len;
963 size -= this_len;
964 }
965 }
966 err = namelen;
Sage Weil355da1e2009-10-06 11:31:08 -0700967out:
Sage Weilbe655592011-11-30 09:47:09 -0800968 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700969 return err;
970}
971
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200972static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -0700973 const char *value, size_t size, int flags)
974{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +0200975 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700976 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700977 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700978 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800979 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +0800980 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -0700981 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700982
Yan, Zheng0aeff372014-12-17 21:26:47 +0800983 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800984 /* copy value into pagelist */
Ilya Dryomov33165d42018-09-28 15:38:34 +0200985 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800986 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -0700987 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800988
Yan, Zheng25e6bae2014-09-16 19:15:28 +0800989 err = ceph_pagelist_append(pagelist, value, size);
990 if (err)
991 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +0800992 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +0800993 if (flags & CEPH_XATTR_REPLACE)
994 op = CEPH_MDS_OP_RMXATTR;
995 else
996 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -0700997 }
998
999 dout("setxattr value=%.*s\n", (int)size, value);
1000
1001 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +08001002 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +01001003 if (IS_ERR(req)) {
1004 err = PTR_ERR(req);
1005 goto out;
1006 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001007
Sage Weil355da1e2009-10-06 11:31:08 -07001008 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001009 if (!req->r_path2) {
1010 ceph_mdsc_put_request(req);
1011 err = -ENOMEM;
1012 goto out;
1013 }
Sage Weil355da1e2009-10-06 11:31:08 -07001014
Yan, Zheng04303d82016-04-21 12:11:54 +08001015 if (op == CEPH_MDS_OP_SETXATTR) {
1016 req->r_args.setxattr.flags = cpu_to_le32(flags);
1017 req->r_pagelist = pagelist;
1018 pagelist = NULL;
1019 }
Sage Weil355da1e2009-10-06 11:31:08 -07001020
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001021 req->r_inode = inode;
1022 ihold(inode);
1023 req->r_num_caps = 1;
1024 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1025
Sage Weil355da1e2009-10-06 11:31:08 -07001026 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -08001027 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001028 ceph_mdsc_put_request(req);
1029 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1030
1031out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001032 if (pagelist)
1033 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -07001034 return err;
1035}
1036
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001037int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001038 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -07001039{
Alex Elder881a5fa2012-01-23 15:49:28 -06001040 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001041 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001042 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001043 struct ceph_cap_flush *prealloc_cf = NULL;
Luis Henriques86968ef2019-07-19 15:32:20 +01001044 struct ceph_buffer *old_blob = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -06001045 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -07001046 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001047 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001048 int name_len = strlen(name);
1049 int val_len = size;
1050 char *newname = NULL;
1051 char *newval = NULL;
1052 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001053 int required_blob_size;
Yan, Zhengf1919822018-04-08 09:54:39 +08001054 bool check_realm = false;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001055 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001056
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001057 if (ceph_snap(inode) != CEPH_NOSNAP)
1058 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -07001059
Alex Elder06476a62012-01-23 15:49:27 -06001060 vxattr = ceph_match_vxattr(inode, name);
Yan, Zhengf1919822018-04-08 09:54:39 +08001061 if (vxattr) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +08001062 if (vxattr->flags & VXATTR_FLAG_READONLY)
Yan, Zhengf1919822018-04-08 09:54:39 +08001063 return -EOPNOTSUPP;
1064 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1065 check_realm = true;
1066 }
Sage Weil355da1e2009-10-06 11:31:08 -07001067
Sage Weil3adf6542013-01-31 11:53:41 -08001068 /* pass any unhandled ceph.* xattrs through to the MDS */
1069 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1070 goto do_sync_unlocked;
1071
Sage Weil355da1e2009-10-06 11:31:08 -07001072 /* preallocate memory for xattr name, value, index node */
1073 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +02001074 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001075 if (!newname)
1076 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001077
1078 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -06001079 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001080 if (!newval)
1081 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001082 }
1083
1084 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1085 if (!xattr)
1086 goto out;
1087
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001088 prealloc_cf = ceph_alloc_cap_flush();
1089 if (!prealloc_cf)
1090 goto out;
1091
Sage Weilbe655592011-11-30 09:47:09 -08001092 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001093retry:
1094 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001095 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -07001096 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001097
1098 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1099 lock_snap_rwsem = true;
1100 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1101 spin_unlock(&ci->i_ceph_lock);
1102 down_read(&mdsc->snap_rwsem);
1103 spin_lock(&ci->i_ceph_lock);
1104 goto retry;
1105 }
1106 }
1107
1108 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -07001109 __build_xattrs(inode);
1110
1111 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1112
1113 if (!ci->i_xattrs.prealloc_blob ||
1114 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -06001115 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001116
Sage Weilbe655592011-11-30 09:47:09 -08001117 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001118 ceph_buffer_put(old_blob); /* Shouldn't be required */
1119 dout(" pre-allocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001120 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001121 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001122 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001123 spin_lock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001124 /* prealloc_blob can't be released while holding i_ceph_lock */
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001125 if (ci->i_xattrs.prealloc_blob)
Luis Henriques86968ef2019-07-19 15:32:20 +01001126 old_blob = ci->i_xattrs.prealloc_blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001127 ci->i_xattrs.prealloc_blob = blob;
1128 goto retry;
1129 }
1130
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001131 err = __set_xattr(ci, newname, name_len, newval, val_len,
1132 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001133
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001134 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001135 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1136 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001137 ci->i_xattrs.dirty = true;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001138 inode->i_ctime = current_time(inode);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001139 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001140
Sage Weilbe655592011-11-30 09:47:09 -08001141 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001142 ceph_buffer_put(old_blob);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001143 if (lock_snap_rwsem)
1144 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001145 if (dirty)
1146 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001147 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001148 return err;
1149
1150do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001151 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001152do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001153 if (lock_snap_rwsem)
1154 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001155
1156 /* security module set xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001157 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +08001158 pr_warn_ratelimited("sync setxattr %p "
1159 "during filling trace\n", inode);
1160 err = -EBUSY;
1161 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001162 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zhengf1919822018-04-08 09:54:39 +08001163 if (err >= 0 && check_realm) {
1164 /* check if snaprealm was created for quota inode */
1165 spin_lock(&ci->i_ceph_lock);
1166 if ((ci->i_max_files || ci->i_max_bytes) &&
1167 !(ci->i_snap_realm &&
1168 ci->i_snap_realm->ino == ci->i_vino.ino))
1169 err = -EOPNOTSUPP;
1170 spin_unlock(&ci->i_ceph_lock);
1171 }
Yan, Zheng315f2402016-03-07 10:34:50 +08001172 }
Sage Weil355da1e2009-10-06 11:31:08 -07001173out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001174 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001175 kfree(newname);
1176 kfree(newval);
1177 kfree(xattr);
1178 return err;
1179}
1180
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001181static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1182 struct dentry *dentry, struct inode *inode,
1183 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001184{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001185 if (!ceph_is_valid_xattr(name))
1186 return -EOPNOTSUPP;
1187 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001188}
Yan, Zheng315f2402016-03-07 10:34:50 +08001189
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001190static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04001191 struct dentry *unused, struct inode *inode,
1192 const char *name, const void *value,
1193 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001194{
1195 if (!ceph_is_valid_xattr(name))
1196 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001197 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001198}
1199
Wei Yongjun5130cce2016-10-17 15:11:29 +00001200static const struct xattr_handler ceph_other_xattr_handler = {
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001201 .prefix = "", /* match any name => handlers called with full name */
1202 .get = ceph_get_xattr_handler,
1203 .set = ceph_set_xattr_handler,
1204};
1205
Yan, Zheng315f2402016-03-07 10:34:50 +08001206#ifdef CONFIG_SECURITY
1207bool ceph_security_xattr_wanted(struct inode *in)
1208{
1209 return in->i_security != NULL;
1210}
1211
1212bool ceph_security_xattr_deadlock(struct inode *in)
1213{
1214 struct ceph_inode_info *ci;
1215 bool ret;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001216 if (!in->i_security)
Yan, Zheng315f2402016-03-07 10:34:50 +08001217 return false;
1218 ci = ceph_inode(in);
1219 spin_lock(&ci->i_ceph_lock);
1220 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1221 !(ci->i_xattrs.version > 0 &&
1222 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1223 spin_unlock(&ci->i_ceph_lock);
1224 return ret;
1225}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001226
1227#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1228int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1229 struct ceph_acl_sec_ctx *as_ctx)
1230{
1231 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1232 const char *name;
1233 size_t name_len;
1234 int err;
1235
1236 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1237 &as_ctx->sec_ctx,
1238 &as_ctx->sec_ctxlen);
1239 if (err < 0) {
1240 WARN_ON_ONCE(err != -EOPNOTSUPP);
1241 err = 0; /* do nothing */
1242 goto out;
1243 }
1244
1245 err = -ENOMEM;
1246 if (!pagelist) {
1247 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1248 if (!pagelist)
1249 goto out;
1250 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1251 if (err)
1252 goto out;
1253 ceph_pagelist_encode_32(pagelist, 1);
1254 }
1255
1256 /*
1257 * FIXME: Make security_dentry_init_security() generic. Currently
1258 * It only supports single security module and only selinux has
1259 * dentry_init_security hook.
1260 */
1261 name = XATTR_NAME_SELINUX;
1262 name_len = strlen(name);
1263 err = ceph_pagelist_reserve(pagelist,
1264 4 * 2 + name_len + as_ctx->sec_ctxlen);
1265 if (err)
1266 goto out;
1267
1268 if (as_ctx->pagelist) {
1269 /* update count of KV pairs */
1270 BUG_ON(pagelist->length <= sizeof(__le32));
1271 if (list_is_singular(&pagelist->head)) {
1272 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1273 } else {
1274 struct page *page = list_first_entry(&pagelist->head,
1275 struct page, lru);
1276 void *addr = kmap_atomic(page);
1277 le32_add_cpu((__le32*)addr, 1);
1278 kunmap_atomic(addr);
1279 }
1280 } else {
1281 as_ctx->pagelist = pagelist;
1282 }
1283
1284 ceph_pagelist_encode_32(pagelist, name_len);
1285 ceph_pagelist_append(pagelist, name, name_len);
1286
1287 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1288 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1289
1290 err = 0;
1291out:
1292 if (pagelist && !as_ctx->pagelist)
1293 ceph_pagelist_release(pagelist);
1294 return err;
1295}
1296
1297void ceph_security_invalidate_secctx(struct inode *inode)
1298{
1299 security_inode_invalidate_secctx(inode);
1300}
1301
1302static int ceph_xattr_set_security_label(const struct xattr_handler *handler,
1303 struct dentry *unused, struct inode *inode,
1304 const char *key, const void *buf,
1305 size_t buflen, int flags)
1306{
1307 if (security_ismaclabel(key)) {
1308 const char *name = xattr_full_name(handler, key);
1309 return __ceph_setxattr(inode, name, buf, buflen, flags);
1310 }
1311 return -EOPNOTSUPP;
1312}
1313
1314static int ceph_xattr_get_security_label(const struct xattr_handler *handler,
1315 struct dentry *unused, struct inode *inode,
1316 const char *key, void *buf, size_t buflen)
1317{
1318 if (security_ismaclabel(key)) {
1319 const char *name = xattr_full_name(handler, key);
1320 return __ceph_getxattr(inode, name, buf, buflen);
1321 }
1322 return -EOPNOTSUPP;
1323}
1324
1325static const struct xattr_handler ceph_security_label_handler = {
1326 .prefix = XATTR_SECURITY_PREFIX,
1327 .get = ceph_xattr_get_security_label,
1328 .set = ceph_xattr_set_security_label,
1329};
1330#endif
Yan, Zheng315f2402016-03-07 10:34:50 +08001331#endif
Yan, Zheng5c31e922019-05-26 15:35:39 +08001332
1333void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1334{
1335#ifdef CONFIG_CEPH_FS_POSIX_ACL
1336 posix_acl_release(as_ctx->acl);
1337 posix_acl_release(as_ctx->default_acl);
1338#endif
Yan, Zhengac6713c2019-05-26 16:27:56 +08001339#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1340 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1341#endif
Yan, Zheng5c31e922019-05-26 15:35:39 +08001342 if (as_ctx->pagelist)
1343 ceph_pagelist_release(as_ctx->pagelist);
1344}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001345
1346/*
1347 * List of handlers for synthetic system.* attributes. Other
1348 * attributes are handled directly.
1349 */
1350const struct xattr_handler *ceph_xattr_handlers[] = {
1351#ifdef CONFIG_CEPH_FS_POSIX_ACL
1352 &posix_acl_access_xattr_handler,
1353 &posix_acl_default_xattr_handler,
1354#endif
1355#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1356 &ceph_security_label_handler,
1357#endif
1358 &ceph_other_xattr_handler,
1359 NULL,
1360};