blob: 1242db8d3444acc523daf5cd7011aab22d8f28a7 [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{
Jeff Laytonb8fe9182019-08-06 10:55:41 -040023 return !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
24 !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil355da1e2009-10-06 11:31:08 -070025 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
26 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
27}
28
29/*
30 * These define virtual xattrs exposing the recursive directory
31 * statistics and layout metadata.
32 */
Alex Elder881a5fa2012-01-23 15:49:28 -060033struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070034 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060035 size_t name_size; /* strlen(name) + 1 (for '\0') */
Jeff Laytonf1d1b512019-06-24 07:39:18 -040036 ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
37 size_t size);
Sage Weilf36e4472013-01-20 21:59:29 -080038 bool (*exists_cb)(struct ceph_inode_info *ci);
Yan, Zheng4e9906e2018-04-25 17:14:05 +080039 unsigned int flags;
Sage Weil355da1e2009-10-06 11:31:08 -070040};
41
Yan, Zheng4e9906e2018-04-25 17:14:05 +080042#define VXATTR_FLAG_READONLY (1<<0)
43#define VXATTR_FLAG_HIDDEN (1<<1)
Yan, Zheng49a9f4f2018-04-25 17:30:23 +080044#define VXATTR_FLAG_RSTAT (1<<2)
Jeff Layton81048c02020-11-03 14:00:26 -050045#define VXATTR_FLAG_DIRSTAT (1<<3)
Yan, Zheng4e9906e2018-04-25 17:14:05 +080046
Sage Weil32ab0bd2013-01-19 16:46:32 -080047/* layouts */
48
49static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
50{
Yan, Zheng779fe0f2016-03-07 09:35:06 +080051 struct ceph_file_layout *fl = &ci->i_layout;
52 return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
53 fl->object_size > 0 || fl->pool_id >= 0 ||
54 rcu_dereference_raw(fl->pool_ns) != NULL);
Sage Weil32ab0bd2013-01-19 16:46:32 -080055}
56
Jeff Laytonf1d1b512019-06-24 07:39:18 -040057static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
58 size_t size)
Sage Weil32ab0bd2013-01-19 16:46:32 -080059{
Sage Weil32ab0bd2013-01-19 16:46:32 -080060 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
61 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080062 struct ceph_string *pool_ns;
Yan, Zheng76271512016-02-03 21:24:49 +080063 s64 pool = ci->i_layout.pool_id;
Sage Weil32ab0bd2013-01-19 16:46:32 -080064 const char *pool_name;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080065 const char *ns_field = " pool_namespace=";
Yan, Zheng1e5c6642014-03-24 13:00:54 +080066 char buf[128];
Yan, Zheng779fe0f2016-03-07 09:35:06 +080067 size_t len, total_len = 0;
Jeff Layton3b421012019-06-13 15:17:00 -040068 ssize_t ret;
Yan, Zheng779fe0f2016-03-07 09:35:06 +080069
70 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -080071
72 dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +020073 down_read(&osdc->lock);
Sage Weil32ab0bd2013-01-19 16:46:32 -080074 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080075 if (pool_name) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080076 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080077 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
78 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
79 ci->i_layout.object_size);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080080 total_len = len + strlen(pool_name);
Yan, Zheng1e5c6642014-03-24 13:00:54 +080081 } else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080082 len = snprintf(buf, sizeof(buf),
Yan, Zheng76271512016-02-03 21:24:49 +080083 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
84 ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
Jeff Laytonf1d1b512019-06-24 07:39:18 -040085 ci->i_layout.object_size, pool);
Yan, Zheng779fe0f2016-03-07 09:35:06 +080086 total_len = len;
87 }
88
89 if (pool_ns)
90 total_len += strlen(ns_field) + pool_ns->len;
91
Jeff Layton3b421012019-06-13 15:17:00 -040092 ret = total_len;
93 if (size >= total_len) {
Yan, Zheng779fe0f2016-03-07 09:35:06 +080094 memcpy(val, buf, len);
95 ret = len;
96 if (pool_name) {
97 len = strlen(pool_name);
98 memcpy(val + ret, pool_name, len);
99 ret += len;
100 }
101 if (pool_ns) {
102 len = strlen(ns_field);
103 memcpy(val + ret, ns_field, len);
104 ret += len;
105 memcpy(val + ret, pool_ns->str, pool_ns->len);
106 ret += pool_ns->len;
Yan, Zheng1e5c6642014-03-24 13:00:54 +0800107 }
108 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200109 up_read(&osdc->lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800110 ceph_put_string(pool_ns);
Sage Weil32ab0bd2013-01-19 16:46:32 -0800111 return ret;
112}
113
Jeff Layton26350532019-06-24 07:32:21 -0400114/*
115 * The convention with strings in xattrs is that they should not be NULL
116 * terminated, since we're returning the length with them. snprintf always
117 * NULL terminates however, so call it on a temporary buffer and then memcpy
118 * the result into place.
119 */
Ilya Dryomovf6fbdcd2020-09-17 18:45:44 +0200120static __printf(3, 4)
121int ceph_fmt_xattr(char *val, size_t size, const char *fmt, ...)
Jeff Layton26350532019-06-24 07:32:21 -0400122{
123 int ret;
124 va_list args;
125 char buf[96]; /* NB: reevaluate size if new vxattrs are added */
126
127 va_start(args, fmt);
128 ret = vsnprintf(buf, size ? sizeof(buf) : 0, fmt, args);
129 va_end(args);
130
131 /* Sanity check */
132 if (size && ret + 1 > sizeof(buf)) {
133 WARN_ONCE(true, "Returned length too big (%d)", ret);
134 return -E2BIG;
135 }
136
137 if (ret <= size)
138 memcpy(val, buf, ret);
139 return ret;
140}
141
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400142static ssize_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
143 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800144{
Jeff Layton26350532019-06-24 07:32:21 -0400145 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_unit);
Sage Weil695b7112013-01-20 22:08:33 -0800146}
147
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400148static ssize_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
149 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800150{
Jeff Layton26350532019-06-24 07:32:21 -0400151 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.stripe_count);
Sage Weil695b7112013-01-20 22:08:33 -0800152}
153
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400154static ssize_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
155 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800156{
Jeff Layton26350532019-06-24 07:32:21 -0400157 return ceph_fmt_xattr(val, size, "%u", ci->i_layout.object_size);
Sage Weil695b7112013-01-20 22:08:33 -0800158}
159
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400160static ssize_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
161 char *val, size_t size)
Sage Weil695b7112013-01-20 22:08:33 -0800162{
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400163 ssize_t ret;
Sage Weil695b7112013-01-20 22:08:33 -0800164 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
165 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng76271512016-02-03 21:24:49 +0800166 s64 pool = ci->i_layout.pool_id;
Sage Weil695b7112013-01-20 22:08:33 -0800167 const char *pool_name;
168
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200169 down_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800170 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
Jeff Layton26350532019-06-24 07:32:21 -0400171 if (pool_name) {
172 ret = strlen(pool_name);
173 if (ret <= size)
174 memcpy(val, pool_name, ret);
175 } else {
176 ret = ceph_fmt_xattr(val, size, "%lld", pool);
177 }
Ilya Dryomov5aea3dc2016-04-28 16:07:26 +0200178 up_read(&osdc->lock);
Sage Weil695b7112013-01-20 22:08:33 -0800179 return ret;
180}
181
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400182static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
183 char *val, size_t size)
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800184{
Jeff Layton26350532019-06-24 07:32:21 -0400185 ssize_t ret = 0;
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800186 struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
Jeff Layton26350532019-06-24 07:32:21 -0400187
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800188 if (ns) {
Jeff Layton26350532019-06-24 07:32:21 -0400189 ret = ns->len;
190 if (ret <= size)
191 memcpy(val, ns->str, ret);
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800192 ceph_put_string(ns);
193 }
194 return ret;
195}
196
Sage Weil355da1e2009-10-06 11:31:08 -0700197/* directories */
198
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400199static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
200 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700201{
Jeff Layton26350532019-06-24 07:32:21 -0400202 return ceph_fmt_xattr(val, size, "%lld", ci->i_files + ci->i_subdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700203}
204
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400205static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
206 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700207{
Jeff Layton26350532019-06-24 07:32:21 -0400208 return ceph_fmt_xattr(val, size, "%lld", ci->i_files);
Sage Weil355da1e2009-10-06 11:31:08 -0700209}
210
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400211static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
212 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700213{
Jeff Layton26350532019-06-24 07:32:21 -0400214 return ceph_fmt_xattr(val, size, "%lld", ci->i_subdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700215}
216
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400217static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
218 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700219{
Jeff Layton26350532019-06-24 07:32:21 -0400220 return ceph_fmt_xattr(val, size, "%lld",
221 ci->i_rfiles + ci->i_rsubdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700222}
223
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400224static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
225 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700226{
Jeff Layton26350532019-06-24 07:32:21 -0400227 return ceph_fmt_xattr(val, size, "%lld", ci->i_rfiles);
Sage Weil355da1e2009-10-06 11:31:08 -0700228}
229
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400230static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
231 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700232{
Jeff Layton26350532019-06-24 07:32:21 -0400233 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsubdirs);
Sage Weil355da1e2009-10-06 11:31:08 -0700234}
235
Yanhu Caoe7f72952020-08-28 09:28:44 +0800236static ssize_t ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info *ci, char *val,
237 size_t size)
238{
239 return ceph_fmt_xattr(val, size, "%lld", ci->i_rsnaps);
240}
241
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400242static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
243 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700244{
Jeff Layton26350532019-06-24 07:32:21 -0400245 return ceph_fmt_xattr(val, size, "%lld", ci->i_rbytes);
Sage Weil355da1e2009-10-06 11:31:08 -0700246}
247
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400248static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
249 size_t size)
Sage Weil355da1e2009-10-06 11:31:08 -0700250{
Jeff Layton26350532019-06-24 07:32:21 -0400251 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
252 ci->i_rctime.tv_nsec);
Sage Weil355da1e2009-10-06 11:31:08 -0700253}
254
Yan, Zheng08796872019-01-09 11:07:02 +0800255/* dir pin */
256static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
257{
258 return ci->i_dir_pin != -ENODATA;
259}
Luis Henriquesfb18a572018-01-05 10:47:18 +0000260
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400261static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
262 size_t size)
Yan, Zheng08796872019-01-09 11:07:02 +0800263{
Jeff Layton26350532019-06-24 07:32:21 -0400264 return ceph_fmt_xattr(val, size, "%d", (int)ci->i_dir_pin);
Yan, Zheng08796872019-01-09 11:07:02 +0800265}
266
267/* quotas */
Luis Henriquesfb18a572018-01-05 10:47:18 +0000268static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
269{
Yan, Zhengf1919822018-04-08 09:54:39 +0800270 bool ret = false;
271 spin_lock(&ci->i_ceph_lock);
272 if ((ci->i_max_files || ci->i_max_bytes) &&
273 ci->i_vino.snap == CEPH_NOSNAP &&
274 ci->i_snap_realm &&
275 ci->i_snap_realm->ino == ci->i_vino.ino)
276 ret = true;
277 spin_unlock(&ci->i_ceph_lock);
278 return ret;
Luis Henriquesfb18a572018-01-05 10:47:18 +0000279}
280
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400281static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
282 size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000283{
Jeff Layton26350532019-06-24 07:32:21 -0400284 return ceph_fmt_xattr(val, size, "max_bytes=%llu max_files=%llu",
285 ci->i_max_bytes, ci->i_max_files);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000286}
287
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400288static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
289 char *val, size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000290{
Jeff Layton26350532019-06-24 07:32:21 -0400291 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_bytes);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000292}
293
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400294static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
295 char *val, size_t size)
Luis Henriquesfb18a572018-01-05 10:47:18 +0000296{
Jeff Layton26350532019-06-24 07:32:21 -0400297 return ceph_fmt_xattr(val, size, "%llu", ci->i_max_files);
Luis Henriquesfb18a572018-01-05 10:47:18 +0000298}
Sage Weil32ab0bd2013-01-19 16:46:32 -0800299
David Disseldorp100cc612019-04-18 14:15:47 +0200300/* snapshots */
301static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
302{
303 return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
304}
305
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400306static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
307 size_t size)
David Disseldorp100cc612019-04-18 14:15:47 +0200308{
Jeff Layton26350532019-06-24 07:32:21 -0400309 return ceph_fmt_xattr(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
310 ci->i_snap_btime.tv_nsec);
David Disseldorp100cc612019-04-18 14:15:47 +0200311}
312
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800313static ssize_t ceph_vxattrcb_cluster_fsid(struct ceph_inode_info *ci,
314 char *val, size_t size)
315{
316 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
317
318 return ceph_fmt_xattr(val, size, "%pU", &fsc->client->fsid);
319}
320
321static ssize_t ceph_vxattrcb_client_id(struct ceph_inode_info *ci,
322 char *val, size_t size)
323{
324 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
325
326 return ceph_fmt_xattr(val, size, "client%lld",
327 ceph_client_gid(fsc->client));
328}
329
Luis Henriquesdd980fc2020-11-23 17:38:46 +0000330static ssize_t ceph_vxattrcb_caps(struct ceph_inode_info *ci, char *val,
331 size_t size)
332{
333 int issued;
334
335 spin_lock(&ci->i_ceph_lock);
336 issued = __ceph_caps_issued(ci, NULL);
337 spin_unlock(&ci->i_ceph_lock);
338
339 return ceph_fmt_xattr(val, size, "%s/0x%x",
340 ceph_cap_string(issued), issued);
341}
342
Alex Eldereb788082012-01-23 15:49:28 -0600343#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800344#define CEPH_XATTR_NAME2(_type, _name, _name2) \
345 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600346
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800347#define XATTR_NAME_CEPH(_type, _name, _flags) \
Sage Weil88601472013-01-31 11:53:27 -0800348 { \
349 .name = CEPH_XATTR_NAME(_type, _name), \
350 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
351 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800352 .exists_cb = NULL, \
353 .flags = (VXATTR_FLAG_READONLY | _flags), \
Sage Weil88601472013-01-31 11:53:27 -0800354 }
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800355#define XATTR_RSTAT_FIELD(_type, _name) \
356 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
Sage Weil695b7112013-01-20 22:08:33 -0800357#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
358 { \
359 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
360 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
361 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
Sage Weil695b7112013-01-20 22:08:33 -0800362 .exists_cb = ceph_vxattrcb_layout_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800363 .flags = VXATTR_FLAG_HIDDEN, \
Sage Weil695b7112013-01-20 22:08:33 -0800364 }
Luis Henriquesfb18a572018-01-05 10:47:18 +0000365#define XATTR_QUOTA_FIELD(_type, _name) \
366 { \
367 .name = CEPH_XATTR_NAME(_type, _name), \
368 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
369 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000370 .exists_cb = ceph_vxattrcb_quota_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800371 .flags = VXATTR_FLAG_HIDDEN, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000372 }
Alex Eldereb788082012-01-23 15:49:28 -0600373
Alex Elder881a5fa2012-01-23 15:49:28 -0600374static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800375 {
376 .name = "ceph.dir.layout",
377 .name_size = sizeof("ceph.dir.layout"),
378 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800379 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800380 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800381 },
Sage Weil695b7112013-01-20 22:08:33 -0800382 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
383 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
384 XATTR_LAYOUT_FIELD(dir, layout, object_size),
385 XATTR_LAYOUT_FIELD(dir, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800386 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
Jeff Layton81048c02020-11-03 14:00:26 -0500387 XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT),
388 XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT),
389 XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800390 XATTR_RSTAT_FIELD(dir, rentries),
391 XATTR_RSTAT_FIELD(dir, rfiles),
392 XATTR_RSTAT_FIELD(dir, rsubdirs),
Yanhu Caoe7f72952020-08-28 09:28:44 +0800393 XATTR_RSTAT_FIELD(dir, rsnaps),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800394 XATTR_RSTAT_FIELD(dir, rbytes),
395 XATTR_RSTAT_FIELD(dir, rctime),
Luis Henriquesfb18a572018-01-05 10:47:18 +0000396 {
Yan, Zheng08796872019-01-09 11:07:02 +0800397 .name = "ceph.dir.pin",
David Disseldorpe1b81432019-04-18 14:15:45 +0200398 .name_size = sizeof("ceph.dir.pin"),
Yan, Zheng08796872019-01-09 11:07:02 +0800399 .getxattr_cb = ceph_vxattrcb_dir_pin,
400 .exists_cb = ceph_vxattrcb_dir_pin_exists,
401 .flags = VXATTR_FLAG_HIDDEN,
402 },
403 {
Luis Henriquesfb18a572018-01-05 10:47:18 +0000404 .name = "ceph.quota",
405 .name_size = sizeof("ceph.quota"),
406 .getxattr_cb = ceph_vxattrcb_quota,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000407 .exists_cb = ceph_vxattrcb_quota_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800408 .flags = VXATTR_FLAG_HIDDEN,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000409 },
410 XATTR_QUOTA_FIELD(quota, max_bytes),
411 XATTR_QUOTA_FIELD(quota, max_files),
David Disseldorp100cc612019-04-18 14:15:47 +0200412 {
413 .name = "ceph.snap.btime",
414 .name_size = sizeof("ceph.snap.btime"),
415 .getxattr_cb = ceph_vxattrcb_snap_btime,
416 .exists_cb = ceph_vxattrcb_snap_btime_exists,
417 .flags = VXATTR_FLAG_READONLY,
418 },
Luis Henriquesdd980fc2020-11-23 17:38:46 +0000419 {
420 .name = "ceph.caps",
421 .name_size = sizeof("ceph.caps"),
422 .getxattr_cb = ceph_vxattrcb_caps,
423 .exists_cb = NULL,
424 .flags = VXATTR_FLAG_HIDDEN,
425 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600426 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700427};
428
429/* files */
430
Alex Elder881a5fa2012-01-23 15:49:28 -0600431static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800432 {
433 .name = "ceph.file.layout",
434 .name_size = sizeof("ceph.file.layout"),
435 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800436 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800437 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800438 },
Sage Weil695b7112013-01-20 22:08:33 -0800439 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
440 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
441 XATTR_LAYOUT_FIELD(file, layout, object_size),
442 XATTR_LAYOUT_FIELD(file, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800443 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
David Disseldorp100cc612019-04-18 14:15:47 +0200444 {
445 .name = "ceph.snap.btime",
446 .name_size = sizeof("ceph.snap.btime"),
447 .getxattr_cb = ceph_vxattrcb_snap_btime,
448 .exists_cb = ceph_vxattrcb_snap_btime_exists,
449 .flags = VXATTR_FLAG_READONLY,
450 },
Luis Henriquesdd980fc2020-11-23 17:38:46 +0000451 {
452 .name = "ceph.caps",
453 .name_size = sizeof("ceph.caps"),
454 .getxattr_cb = ceph_vxattrcb_caps,
455 .exists_cb = NULL,
456 .flags = VXATTR_FLAG_HIDDEN,
457 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600458 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700459};
460
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800461static struct ceph_vxattr ceph_common_vxattrs[] = {
462 {
463 .name = "ceph.cluster_fsid",
464 .name_size = sizeof("ceph.cluster_fsid"),
465 .getxattr_cb = ceph_vxattrcb_cluster_fsid,
466 .exists_cb = NULL,
467 .flags = VXATTR_FLAG_READONLY,
468 },
469 {
470 .name = "ceph.client_id",
471 .name_size = sizeof("ceph.client_id"),
472 .getxattr_cb = ceph_vxattrcb_client_id,
473 .exists_cb = NULL,
474 .flags = VXATTR_FLAG_READONLY,
475 },
476 { .name = NULL, 0 } /* Required table terminator */
477};
478
Alex Elder881a5fa2012-01-23 15:49:28 -0600479static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700480{
481 if (S_ISDIR(inode->i_mode))
482 return ceph_dir_vxattrs;
483 else if (S_ISREG(inode->i_mode))
484 return ceph_file_vxattrs;
485 return NULL;
486}
487
Alex Elder881a5fa2012-01-23 15:49:28 -0600488static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700489 const char *name)
490{
Alex Elder881a5fa2012-01-23 15:49:28 -0600491 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600492
493 if (vxattr) {
494 while (vxattr->name) {
495 if (!strcmp(vxattr->name, name))
496 return vxattr;
497 vxattr++;
498 }
499 }
500
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800501 vxattr = ceph_common_vxattrs;
502 while (vxattr->name) {
503 if (!strcmp(vxattr->name, name))
504 return vxattr;
505 vxattr++;
506 }
507
Sage Weil355da1e2009-10-06 11:31:08 -0700508 return NULL;
509}
510
511static int __set_xattr(struct ceph_inode_info *ci,
512 const char *name, int name_len,
513 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800514 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700515 struct ceph_inode_xattr **newxattr)
516{
517 struct rb_node **p;
518 struct rb_node *parent = NULL;
519 struct ceph_inode_xattr *xattr = NULL;
520 int c;
521 int new = 0;
522
523 p = &ci->i_xattrs.index.rb_node;
524 while (*p) {
525 parent = *p;
526 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
527 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
528 if (c < 0)
529 p = &(*p)->rb_left;
530 else if (c > 0)
531 p = &(*p)->rb_right;
532 else {
533 if (name_len == xattr->name_len)
534 break;
535 else if (name_len < xattr->name_len)
536 p = &(*p)->rb_left;
537 else
538 p = &(*p)->rb_right;
539 }
540 xattr = NULL;
541 }
542
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800543 if (update_xattr) {
544 int err = 0;
Luis Henriqueseeca9582017-04-28 11:14:04 +0100545
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800546 if (xattr && (flags & XATTR_CREATE))
547 err = -EEXIST;
548 else if (!xattr && (flags & XATTR_REPLACE))
549 err = -ENODATA;
550 if (err) {
551 kfree(name);
552 kfree(val);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100553 kfree(*newxattr);
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800554 return err;
555 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800556 if (update_xattr < 0) {
557 if (xattr)
558 __remove_xattr(ci, xattr);
559 kfree(name);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100560 kfree(*newxattr);
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800561 return 0;
562 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800563 }
564
Sage Weil355da1e2009-10-06 11:31:08 -0700565 if (!xattr) {
566 new = 1;
567 xattr = *newxattr;
568 xattr->name = name;
569 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800570 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700571
572 ci->i_xattrs.count++;
573 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
574 } else {
575 kfree(*newxattr);
576 *newxattr = NULL;
577 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000578 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700579
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800580 if (update_xattr) {
Xu Wangc00e4522020-07-08 07:03:22 +0000581 kfree(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700582 name = xattr->name;
583 }
584 ci->i_xattrs.names_size -= xattr->name_len;
585 ci->i_xattrs.vals_size -= xattr->val_len;
586 }
Sage Weil355da1e2009-10-06 11:31:08 -0700587 ci->i_xattrs.names_size += name_len;
588 ci->i_xattrs.vals_size += val_len;
589 if (val)
590 xattr->val = val;
591 else
592 xattr->val = "";
593
594 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800595 xattr->dirty = update_xattr;
596 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700597
598 if (new) {
599 rb_link_node(&xattr->node, parent, p);
600 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
601 dout("__set_xattr_val p=%p\n", p);
602 }
603
Yan, Zheng05729782019-05-27 16:15:41 +0800604 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
605 ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
Sage Weil355da1e2009-10-06 11:31:08 -0700606
607 return 0;
608}
609
610static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
611 const char *name)
612{
613 struct rb_node **p;
614 struct rb_node *parent = NULL;
615 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800616 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700617 int c;
618
619 p = &ci->i_xattrs.index.rb_node;
620 while (*p) {
621 parent = *p;
622 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
623 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800624 if (c == 0 && name_len > xattr->name_len)
625 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700626 if (c < 0)
627 p = &(*p)->rb_left;
628 else if (c > 0)
629 p = &(*p)->rb_right;
630 else {
631 dout("__get_xattr %s: found %.*s\n", name,
632 xattr->val_len, xattr->val);
633 return xattr;
634 }
635 }
636
637 dout("__get_xattr %s: not found\n", name);
638
639 return NULL;
640}
641
642static void __free_xattr(struct ceph_inode_xattr *xattr)
643{
644 BUG_ON(!xattr);
645
646 if (xattr->should_free_name)
Xu Wangc00e4522020-07-08 07:03:22 +0000647 kfree(xattr->name);
Sage Weil355da1e2009-10-06 11:31:08 -0700648 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000649 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700650
651 kfree(xattr);
652}
653
654static int __remove_xattr(struct ceph_inode_info *ci,
655 struct ceph_inode_xattr *xattr)
656{
657 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800658 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700659
660 rb_erase(&xattr->node, &ci->i_xattrs.index);
661
662 if (xattr->should_free_name)
Xu Wangc00e4522020-07-08 07:03:22 +0000663 kfree(xattr->name);
Sage Weil355da1e2009-10-06 11:31:08 -0700664 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000665 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700666
667 ci->i_xattrs.names_size -= xattr->name_len;
668 ci->i_xattrs.vals_size -= xattr->val_len;
669 ci->i_xattrs.count--;
670 kfree(xattr);
671
672 return 0;
673}
674
Sage Weil355da1e2009-10-06 11:31:08 -0700675static char *__copy_xattr_names(struct ceph_inode_info *ci,
676 char *dest)
677{
678 struct rb_node *p;
679 struct ceph_inode_xattr *xattr = NULL;
680
681 p = rb_first(&ci->i_xattrs.index);
682 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
683
684 while (p) {
685 xattr = rb_entry(p, struct ceph_inode_xattr, node);
686 memcpy(dest, xattr->name, xattr->name_len);
687 dest[xattr->name_len] = '\0';
688
689 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
690 xattr->name_len, ci->i_xattrs.names_size);
691
692 dest += xattr->name_len + 1;
693 p = rb_next(p);
694 }
695
696 return dest;
697}
698
699void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
700{
701 struct rb_node *p, *tmp;
702 struct ceph_inode_xattr *xattr = NULL;
703
704 p = rb_first(&ci->i_xattrs.index);
705
706 dout("__ceph_destroy_xattrs p=%p\n", p);
707
708 while (p) {
709 xattr = rb_entry(p, struct ceph_inode_xattr, node);
710 tmp = p;
711 p = rb_next(tmp);
712 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
713 xattr->name_len, xattr->name);
714 rb_erase(tmp, &ci->i_xattrs.index);
715
716 __free_xattr(xattr);
717 }
718
719 ci->i_xattrs.names_size = 0;
720 ci->i_xattrs.vals_size = 0;
721 ci->i_xattrs.index_version = 0;
722 ci->i_xattrs.count = 0;
723 ci->i_xattrs.index = RB_ROOT;
724}
725
726static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800727 __releases(ci->i_ceph_lock)
728 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700729{
730 u32 namelen;
731 u32 numattr = 0;
732 void *p, *end;
733 u32 len;
734 const char *name, *val;
735 struct ceph_inode_info *ci = ceph_inode(inode);
Xiubo Li0eb30852019-12-18 21:15:18 -0500736 u64 xattr_version;
Sage Weil355da1e2009-10-06 11:31:08 -0700737 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800738 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700739 int i;
740
741 dout("__build_xattrs() len=%d\n",
742 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
743
744 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
745 return 0; /* already built */
746
747 __ceph_destroy_xattrs(ci);
748
749start:
750 /* updated internal xattr rb tree */
751 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
752 p = ci->i_xattrs.blob->vec.iov_base;
753 end = p + ci->i_xattrs.blob->vec.iov_len;
754 ceph_decode_32_safe(&p, end, numattr, bad);
755 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800756 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700757
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400758 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700759 GFP_NOFS);
760 err = -ENOMEM;
761 if (!xattrs)
762 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400763
Sage Weil355da1e2009-10-06 11:31:08 -0700764 for (i = 0; i < numattr; i++) {
765 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
766 GFP_NOFS);
767 if (!xattrs[i])
768 goto bad_lock;
769 }
770
Sage Weilbe655592011-11-30 09:47:09 -0800771 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700772 if (ci->i_xattrs.version != xattr_version) {
773 /* lost a race, retry */
774 for (i = 0; i < numattr; i++)
775 kfree(xattrs[i]);
776 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500777 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700778 goto start;
779 }
780 err = -EIO;
781 while (numattr--) {
782 ceph_decode_32_safe(&p, end, len, bad);
783 namelen = len;
784 name = p;
785 p += len;
786 ceph_decode_32_safe(&p, end, len, bad);
787 val = p;
788 p += len;
789
790 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800791 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700792
793 if (err < 0)
794 goto bad;
795 }
796 kfree(xattrs);
797 }
798 ci->i_xattrs.index_version = ci->i_xattrs.version;
799 ci->i_xattrs.dirty = false;
800
801 return err;
802bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800803 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700804bad:
805 if (xattrs) {
806 for (i = 0; i < numattr; i++)
807 kfree(xattrs[i]);
808 kfree(xattrs);
809 }
810 ci->i_xattrs.names_size = 0;
811 return err;
812}
813
814static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
815 int val_size)
816{
817 /*
818 * 4 bytes for the length, and additional 4 bytes per each xattr name,
819 * 4 bytes per each value
820 */
821 int size = 4 + ci->i_xattrs.count*(4 + 4) +
822 ci->i_xattrs.names_size +
823 ci->i_xattrs.vals_size;
824 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
825 ci->i_xattrs.count, ci->i_xattrs.names_size,
826 ci->i_xattrs.vals_size);
827
828 if (name_size)
829 size += 4 + 4 + name_size + val_size;
830
831 return size;
832}
833
834/*
835 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100836 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
837 * that it can be freed by the caller as the i_ceph_lock is likely to be
838 * held.
Sage Weil355da1e2009-10-06 11:31:08 -0700839 */
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100840struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
Sage Weil355da1e2009-10-06 11:31:08 -0700841{
842 struct rb_node *p;
843 struct ceph_inode_xattr *xattr = NULL;
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100844 struct ceph_buffer *old_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700845 void *dest;
846
847 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
848 if (ci->i_xattrs.dirty) {
849 int need = __get_required_blob_size(ci, 0, 0);
850
851 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
852
853 p = rb_first(&ci->i_xattrs.index);
854 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
855
856 ceph_encode_32(&dest, ci->i_xattrs.count);
857 while (p) {
858 xattr = rb_entry(p, struct ceph_inode_xattr, node);
859
860 ceph_encode_32(&dest, xattr->name_len);
861 memcpy(dest, xattr->name, xattr->name_len);
862 dest += xattr->name_len;
863 ceph_encode_32(&dest, xattr->val_len);
864 memcpy(dest, xattr->val, xattr->val_len);
865 dest += xattr->val_len;
866
867 p = rb_next(p);
868 }
869
870 /* adjust buffer len; it may be larger than we need */
871 ci->i_xattrs.prealloc_blob->vec.iov_len =
872 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
873
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800874 if (ci->i_xattrs.blob)
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100875 old_blob = ci->i_xattrs.blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700876 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
877 ci->i_xattrs.prealloc_blob = NULL;
878 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700879 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700880 }
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100881
882 return old_blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700883}
884
Yan, Zheng315f2402016-03-07 10:34:50 +0800885static inline int __get_request_mask(struct inode *in) {
886 struct ceph_mds_request *req = current->journal_info;
887 int mask = 0;
888 if (req && req->r_target_inode == in) {
889 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
890 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
891 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
892 req->r_op == CEPH_MDS_OP_GETATTR) {
893 mask = le32_to_cpu(req->r_args.getattr.mask);
894 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
895 req->r_op == CEPH_MDS_OP_CREATE) {
896 mask = le32_to_cpu(req->r_args.open.mask);
897 }
898 }
899 return mask;
900}
901
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800902ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700903 size_t size)
904{
Sage Weil355da1e2009-10-06 11:31:08 -0700905 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700906 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600907 struct ceph_vxattr *vxattr = NULL;
Yan, Zheng315f2402016-03-07 10:34:50 +0800908 int req_mask;
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400909 ssize_t err;
Sage Weil355da1e2009-10-06 11:31:08 -0700910
Sage Weil0bee82f2013-01-20 22:00:58 -0800911 /* let's see if a virtual xattr was requested */
912 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800913 if (vxattr) {
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800914 int mask = 0;
915 if (vxattr->flags & VXATTR_FLAG_RSTAT)
916 mask |= CEPH_STAT_RSTAT;
Jeff Layton81048c02020-11-03 14:00:26 -0500917 if (vxattr->flags & VXATTR_FLAG_DIRSTAT)
918 mask |= CEPH_CAP_FILE_SHARED;
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800919 err = ceph_do_getattr(inode, mask, true);
Yan, Zheng1684dd02017-06-14 15:54:56 +0800920 if (err)
921 return err;
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800922 err = -ENODATA;
Jeff Layton3b421012019-06-13 15:17:00 -0400923 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800924 err = vxattr->getxattr_cb(ci, value, size);
Jeff Layton3b421012019-06-13 15:17:00 -0400925 if (size && size < err)
926 err = -ERANGE;
927 }
majianpenga1dc1932013-06-19 14:58:10 +0800928 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800929 }
930
Yan, Zheng315f2402016-03-07 10:34:50 +0800931 req_mask = __get_request_mask(inode);
932
majianpenga1dc1932013-06-19 14:58:10 +0800933 spin_lock(&ci->i_ceph_lock);
Jeff Laytond36e0b62020-01-07 13:12:57 -0500934 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name,
majianpenga1dc1932013-06-19 14:58:10 +0800935 ci->i_xattrs.version, ci->i_xattrs.index_version);
936
Yan, Zheng508b32d2014-09-16 21:46:17 +0800937 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800938 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
Xiubo Li1af16d52020-03-19 23:45:00 -0400939 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800940 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800941
942 /* security module gets xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200943 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +0800944 pr_warn_ratelimited("sync getxattr %p "
945 "during filling trace\n", inode);
946 return -EBUSY;
947 }
948
Sage Weil355da1e2009-10-06 11:31:08 -0700949 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800950 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700951 if (err)
952 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800953 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700954 }
955
Sage Weil355da1e2009-10-06 11:31:08 -0700956 err = __build_xattrs(inode);
957 if (err < 0)
958 goto out;
959
Sage Weil355da1e2009-10-06 11:31:08 -0700960 err = -ENODATA; /* == ENOATTR */
961 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800962 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700963 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700964
965 err = -ERANGE;
966 if (size && size < xattr->val_len)
967 goto out;
968
969 err = xattr->val_len;
970 if (size == 0)
971 goto out;
972
973 memcpy(value, xattr->val, xattr->val_len);
974
Markus Elfringd37b1d92017-08-20 20:22:02 +0200975 if (current->journal_info &&
Jeff Layton026105e2019-08-06 10:41:40 -0400976 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
977 security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
Yan, Zheng315f2402016-03-07 10:34:50 +0800978 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -0700979out:
Sage Weilbe655592011-11-30 09:47:09 -0800980 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700981 return err;
982}
983
984ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
985{
David Howells2b0143b2015-03-17 22:25:59 +0000986 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700987 struct ceph_inode_info *ci = ceph_inode(inode);
David Disseldorp2b2abca2019-04-18 14:15:48 +0200988 bool len_only = (size == 0);
Sage Weil355da1e2009-10-06 11:31:08 -0700989 u32 namelen;
990 int err;
Sage Weil355da1e2009-10-06 11:31:08 -0700991
Sage Weilbe655592011-11-30 09:47:09 -0800992 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700993 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
994 ci->i_xattrs.version, ci->i_xattrs.index_version);
995
Yan, Zheng508b32d2014-09-16 21:46:17 +0800996 if (ci->i_xattrs.version == 0 ||
Xiubo Li1af16d52020-03-19 23:45:00 -0400997 !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -0800998 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +0800999 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001000 if (err)
1001 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +08001002 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001003 }
1004
Sage Weil355da1e2009-10-06 11:31:08 -07001005 err = __build_xattrs(inode);
1006 if (err < 0)
1007 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -06001008
David Disseldorp2b2abca2019-04-18 14:15:48 +02001009 /* add 1 byte for each xattr due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -08001010 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
David Disseldorp2b2abca2019-04-18 14:15:48 +02001011 if (!len_only) {
1012 if (namelen > size) {
1013 err = -ERANGE;
1014 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001015 }
David Disseldorp2b2abca2019-04-18 14:15:48 +02001016 names = __copy_xattr_names(ci, names);
1017 size -= namelen;
Sage Weilb65917d2013-01-20 22:02:39 -08001018 }
David Disseldorp2b2abca2019-04-18 14:15:48 +02001019 err = namelen;
Sage Weil355da1e2009-10-06 11:31:08 -07001020out:
Sage Weilbe655592011-11-30 09:47:09 -08001021 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001022 return err;
1023}
1024
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001025static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -07001026 const char *value, size_t size, int flags)
1027{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001028 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -07001029 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001030 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001031 struct ceph_mds_client *mdsc = fsc->mdsc;
Xiubo Li968cd142020-12-09 10:52:20 +08001032 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001033 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +08001034 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -07001035 int err;
Sage Weil355da1e2009-10-06 11:31:08 -07001036
Yan, Zheng0aeff372014-12-17 21:26:47 +08001037 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001038 /* copy value into pagelist */
Ilya Dryomov33165d42018-09-28 15:38:34 +02001039 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001040 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -07001041 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001042
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001043 err = ceph_pagelist_append(pagelist, value, size);
1044 if (err)
1045 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +08001046 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +08001047 if (flags & CEPH_XATTR_REPLACE)
1048 op = CEPH_MDS_OP_RMXATTR;
1049 else
1050 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -07001051 }
1052
1053 dout("setxattr value=%.*s\n", (int)size, value);
1054
1055 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +08001056 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +01001057 if (IS_ERR(req)) {
1058 err = PTR_ERR(req);
1059 goto out;
1060 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001061
Sage Weil355da1e2009-10-06 11:31:08 -07001062 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001063 if (!req->r_path2) {
1064 ceph_mdsc_put_request(req);
1065 err = -ENOMEM;
1066 goto out;
1067 }
Sage Weil355da1e2009-10-06 11:31:08 -07001068
Yan, Zheng04303d82016-04-21 12:11:54 +08001069 if (op == CEPH_MDS_OP_SETXATTR) {
1070 req->r_args.setxattr.flags = cpu_to_le32(flags);
Xiubo Li968cd142020-12-09 10:52:20 +08001071 req->r_args.setxattr.osdmap_epoch =
1072 cpu_to_le32(osdc->osdmap->epoch);
Yan, Zheng04303d82016-04-21 12:11:54 +08001073 req->r_pagelist = pagelist;
1074 pagelist = NULL;
1075 }
Sage Weil355da1e2009-10-06 11:31:08 -07001076
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001077 req->r_inode = inode;
1078 ihold(inode);
1079 req->r_num_caps = 1;
1080 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1081
Sage Weil355da1e2009-10-06 11:31:08 -07001082 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -08001083 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001084 ceph_mdsc_put_request(req);
1085 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1086
1087out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001088 if (pagelist)
1089 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -07001090 return err;
1091}
1092
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001093int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001094 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -07001095{
Alex Elder881a5fa2012-01-23 15:49:28 -06001096 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001097 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001098 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001099 struct ceph_cap_flush *prealloc_cf = NULL;
Luis Henriques86968ef2019-07-19 15:32:20 +01001100 struct ceph_buffer *old_blob = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -06001101 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -07001102 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001103 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001104 int name_len = strlen(name);
1105 int val_len = size;
1106 char *newname = NULL;
1107 char *newval = NULL;
1108 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001109 int required_blob_size;
Yan, Zhengf1919822018-04-08 09:54:39 +08001110 bool check_realm = false;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001111 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001112
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001113 if (ceph_snap(inode) != CEPH_NOSNAP)
1114 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -07001115
Alex Elder06476a62012-01-23 15:49:27 -06001116 vxattr = ceph_match_vxattr(inode, name);
Yan, Zhengf1919822018-04-08 09:54:39 +08001117 if (vxattr) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +08001118 if (vxattr->flags & VXATTR_FLAG_READONLY)
Yan, Zhengf1919822018-04-08 09:54:39 +08001119 return -EOPNOTSUPP;
1120 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1121 check_realm = true;
1122 }
Sage Weil355da1e2009-10-06 11:31:08 -07001123
Sage Weil3adf6542013-01-31 11:53:41 -08001124 /* pass any unhandled ceph.* xattrs through to the MDS */
1125 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1126 goto do_sync_unlocked;
1127
Sage Weil355da1e2009-10-06 11:31:08 -07001128 /* preallocate memory for xattr name, value, index node */
1129 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +02001130 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001131 if (!newname)
1132 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001133
1134 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -06001135 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001136 if (!newval)
1137 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001138 }
1139
1140 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1141 if (!xattr)
1142 goto out;
1143
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001144 prealloc_cf = ceph_alloc_cap_flush();
1145 if (!prealloc_cf)
1146 goto out;
1147
Sage Weilbe655592011-11-30 09:47:09 -08001148 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001149retry:
1150 issued = __ceph_caps_issued(ci, NULL);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001151 if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
Sage Weil355da1e2009-10-06 11:31:08 -07001152 goto do_sync;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001153
1154 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1155 lock_snap_rwsem = true;
1156 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1157 spin_unlock(&ci->i_ceph_lock);
1158 down_read(&mdsc->snap_rwsem);
1159 spin_lock(&ci->i_ceph_lock);
1160 goto retry;
1161 }
1162 }
1163
Jeff Laytond36e0b62020-01-07 13:12:57 -05001164 dout("setxattr %p name '%s' issued %s\n", inode, name,
1165 ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -07001166 __build_xattrs(inode);
1167
1168 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1169
1170 if (!ci->i_xattrs.prealloc_blob ||
1171 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -06001172 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001173
Sage Weilbe655592011-11-30 09:47:09 -08001174 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001175 ceph_buffer_put(old_blob); /* Shouldn't be required */
1176 dout(" pre-allocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001177 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001178 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001179 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001180 spin_lock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001181 /* prealloc_blob can't be released while holding i_ceph_lock */
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001182 if (ci->i_xattrs.prealloc_blob)
Luis Henriques86968ef2019-07-19 15:32:20 +01001183 old_blob = ci->i_xattrs.prealloc_blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001184 ci->i_xattrs.prealloc_blob = blob;
1185 goto retry;
1186 }
1187
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001188 err = __set_xattr(ci, newname, name_len, newval, val_len,
1189 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001190
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001191 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001192 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1193 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001194 ci->i_xattrs.dirty = true;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001195 inode->i_ctime = current_time(inode);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001196 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001197
Sage Weilbe655592011-11-30 09:47:09 -08001198 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001199 ceph_buffer_put(old_blob);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001200 if (lock_snap_rwsem)
1201 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001202 if (dirty)
1203 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001204 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001205 return err;
1206
1207do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001208 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001209do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001210 if (lock_snap_rwsem)
1211 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001212
1213 /* security module set xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001214 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +08001215 pr_warn_ratelimited("sync setxattr %p "
1216 "during filling trace\n", inode);
1217 err = -EBUSY;
1218 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001219 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zhengf1919822018-04-08 09:54:39 +08001220 if (err >= 0 && check_realm) {
1221 /* check if snaprealm was created for quota inode */
1222 spin_lock(&ci->i_ceph_lock);
1223 if ((ci->i_max_files || ci->i_max_bytes) &&
1224 !(ci->i_snap_realm &&
1225 ci->i_snap_realm->ino == ci->i_vino.ino))
1226 err = -EOPNOTSUPP;
1227 spin_unlock(&ci->i_ceph_lock);
1228 }
Yan, Zheng315f2402016-03-07 10:34:50 +08001229 }
Sage Weil355da1e2009-10-06 11:31:08 -07001230out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001231 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001232 kfree(newname);
1233 kfree(newval);
1234 kfree(xattr);
1235 return err;
1236}
1237
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001238static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1239 struct dentry *dentry, struct inode *inode,
1240 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001241{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001242 if (!ceph_is_valid_xattr(name))
1243 return -EOPNOTSUPP;
1244 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001245}
Yan, Zheng315f2402016-03-07 10:34:50 +08001246
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001247static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01001248 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04001249 struct dentry *unused, struct inode *inode,
1250 const char *name, const void *value,
1251 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001252{
1253 if (!ceph_is_valid_xattr(name))
1254 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001255 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001256}
1257
Wei Yongjun5130cce2016-10-17 15:11:29 +00001258static const struct xattr_handler ceph_other_xattr_handler = {
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001259 .prefix = "", /* match any name => handlers called with full name */
1260 .get = ceph_get_xattr_handler,
1261 .set = ceph_set_xattr_handler,
1262};
1263
Yan, Zheng315f2402016-03-07 10:34:50 +08001264#ifdef CONFIG_SECURITY
1265bool ceph_security_xattr_wanted(struct inode *in)
1266{
1267 return in->i_security != NULL;
1268}
1269
1270bool ceph_security_xattr_deadlock(struct inode *in)
1271{
1272 struct ceph_inode_info *ci;
1273 bool ret;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001274 if (!in->i_security)
Yan, Zheng315f2402016-03-07 10:34:50 +08001275 return false;
1276 ci = ceph_inode(in);
1277 spin_lock(&ci->i_ceph_lock);
1278 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1279 !(ci->i_xattrs.version > 0 &&
1280 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1281 spin_unlock(&ci->i_ceph_lock);
1282 return ret;
1283}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001284
1285#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1286int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1287 struct ceph_acl_sec_ctx *as_ctx)
1288{
1289 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1290 const char *name;
1291 size_t name_len;
1292 int err;
1293
1294 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
1295 &as_ctx->sec_ctx,
1296 &as_ctx->sec_ctxlen);
1297 if (err < 0) {
1298 WARN_ON_ONCE(err != -EOPNOTSUPP);
1299 err = 0; /* do nothing */
1300 goto out;
1301 }
1302
1303 err = -ENOMEM;
1304 if (!pagelist) {
1305 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1306 if (!pagelist)
1307 goto out;
1308 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1309 if (err)
1310 goto out;
1311 ceph_pagelist_encode_32(pagelist, 1);
1312 }
1313
1314 /*
1315 * FIXME: Make security_dentry_init_security() generic. Currently
1316 * It only supports single security module and only selinux has
1317 * dentry_init_security hook.
1318 */
1319 name = XATTR_NAME_SELINUX;
1320 name_len = strlen(name);
1321 err = ceph_pagelist_reserve(pagelist,
1322 4 * 2 + name_len + as_ctx->sec_ctxlen);
1323 if (err)
1324 goto out;
1325
1326 if (as_ctx->pagelist) {
1327 /* update count of KV pairs */
1328 BUG_ON(pagelist->length <= sizeof(__le32));
1329 if (list_is_singular(&pagelist->head)) {
1330 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1331 } else {
1332 struct page *page = list_first_entry(&pagelist->head,
1333 struct page, lru);
1334 void *addr = kmap_atomic(page);
1335 le32_add_cpu((__le32*)addr, 1);
1336 kunmap_atomic(addr);
1337 }
1338 } else {
1339 as_ctx->pagelist = pagelist;
1340 }
1341
1342 ceph_pagelist_encode_32(pagelist, name_len);
1343 ceph_pagelist_append(pagelist, name, name_len);
1344
1345 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1346 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1347
1348 err = 0;
1349out:
1350 if (pagelist && !as_ctx->pagelist)
1351 ceph_pagelist_release(pagelist);
1352 return err;
1353}
Jeff Layton668959a2019-08-06 09:07:51 -04001354#endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1355#endif /* CONFIG_SECURITY */
Yan, Zheng5c31e922019-05-26 15:35:39 +08001356
1357void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1358{
1359#ifdef CONFIG_CEPH_FS_POSIX_ACL
1360 posix_acl_release(as_ctx->acl);
1361 posix_acl_release(as_ctx->default_acl);
1362#endif
Yan, Zhengac6713c2019-05-26 16:27:56 +08001363#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1364 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1365#endif
Yan, Zheng5c31e922019-05-26 15:35:39 +08001366 if (as_ctx->pagelist)
1367 ceph_pagelist_release(as_ctx->pagelist);
1368}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001369
1370/*
1371 * List of handlers for synthetic system.* attributes. Other
1372 * attributes are handled directly.
1373 */
1374const struct xattr_handler *ceph_xattr_handlers[] = {
1375#ifdef CONFIG_CEPH_FS_POSIX_ACL
1376 &posix_acl_access_xattr_handler,
1377 &posix_acl_default_xattr_handler,
1378#endif
Yan, Zhengac6713c2019-05-26 16:27:56 +08001379 &ceph_other_xattr_handler,
1380 NULL,
1381};