blob: f31350cda960bb087fd3bfc1163f4db15f23858d [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{
David Howells874c8ca12022-06-09 21:46:04 +010060 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
Sage Weil32ab0bd2013-01-19 16:46:32 -080061 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
David Howells874c8ca12022-06-09 21:46:04 +010072 dout("ceph_vxattrcb_layout %p\n", &ci->netfs.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;
David Howells874c8ca12022-06-09 21:46:04 +0100164 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
Sage Weil695b7112013-01-20 22:08:33 -0800165 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{
David Howells874c8ca12022-06-09 21:46:04 +0100316 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800317
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{
David Howells874c8ca12022-06-09 21:46:04 +0100324 struct ceph_fs_client *fsc = ceph_sb_to_client(ci->netfs.inode.i_sb);
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800325
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
Jeff Layton40e309d2021-07-26 07:07:50 -0400343static ssize_t ceph_vxattrcb_auth_mds(struct ceph_inode_info *ci,
344 char *val, size_t size)
345{
346 int ret;
347
348 spin_lock(&ci->i_ceph_lock);
349 ret = ceph_fmt_xattr(val, size, "%d",
350 ci->i_auth_cap ? ci->i_auth_cap->session->s_mds : -1);
351 spin_unlock(&ci->i_ceph_lock);
352 return ret;
353}
354
Alex Eldereb788082012-01-23 15:49:28 -0600355#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
Sage Weil695b7112013-01-20 22:08:33 -0800356#define CEPH_XATTR_NAME2(_type, _name, _name2) \
357 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
Alex Eldereb788082012-01-23 15:49:28 -0600358
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800359#define XATTR_NAME_CEPH(_type, _name, _flags) \
Sage Weil88601472013-01-31 11:53:27 -0800360 { \
361 .name = CEPH_XATTR_NAME(_type, _name), \
362 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
363 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800364 .exists_cb = NULL, \
365 .flags = (VXATTR_FLAG_READONLY | _flags), \
Sage Weil88601472013-01-31 11:53:27 -0800366 }
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800367#define XATTR_RSTAT_FIELD(_type, _name) \
368 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
Venky Shankard7a2dc52022-03-10 09:34:19 -0500369#define XATTR_RSTAT_FIELD_UPDATABLE(_type, _name) \
370 { \
371 .name = CEPH_XATTR_NAME(_type, _name), \
372 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
373 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
374 .exists_cb = NULL, \
375 .flags = VXATTR_FLAG_RSTAT, \
376 }
Sage Weil695b7112013-01-20 22:08:33 -0800377#define XATTR_LAYOUT_FIELD(_type, _name, _field) \
378 { \
379 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
380 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
381 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
Sage Weil695b7112013-01-20 22:08:33 -0800382 .exists_cb = ceph_vxattrcb_layout_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800383 .flags = VXATTR_FLAG_HIDDEN, \
Sage Weil695b7112013-01-20 22:08:33 -0800384 }
Luis Henriquesfb18a572018-01-05 10:47:18 +0000385#define XATTR_QUOTA_FIELD(_type, _name) \
386 { \
387 .name = CEPH_XATTR_NAME(_type, _name), \
388 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
389 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000390 .exists_cb = ceph_vxattrcb_quota_exists, \
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800391 .flags = VXATTR_FLAG_HIDDEN, \
Luis Henriquesfb18a572018-01-05 10:47:18 +0000392 }
Alex Eldereb788082012-01-23 15:49:28 -0600393
Alex Elder881a5fa2012-01-23 15:49:28 -0600394static struct ceph_vxattr ceph_dir_vxattrs[] = {
Sage Weil1f08f2b2013-01-20 22:07:12 -0800395 {
396 .name = "ceph.dir.layout",
397 .name_size = sizeof("ceph.dir.layout"),
398 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800399 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800400 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil1f08f2b2013-01-20 22:07:12 -0800401 },
Sage Weil695b7112013-01-20 22:08:33 -0800402 XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
403 XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
404 XATTR_LAYOUT_FIELD(dir, layout, object_size),
405 XATTR_LAYOUT_FIELD(dir, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800406 XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
Jeff Layton81048c02020-11-03 14:00:26 -0500407 XATTR_NAME_CEPH(dir, entries, VXATTR_FLAG_DIRSTAT),
408 XATTR_NAME_CEPH(dir, files, VXATTR_FLAG_DIRSTAT),
409 XATTR_NAME_CEPH(dir, subdirs, VXATTR_FLAG_DIRSTAT),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800410 XATTR_RSTAT_FIELD(dir, rentries),
411 XATTR_RSTAT_FIELD(dir, rfiles),
412 XATTR_RSTAT_FIELD(dir, rsubdirs),
Yanhu Caoe7f72952020-08-28 09:28:44 +0800413 XATTR_RSTAT_FIELD(dir, rsnaps),
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800414 XATTR_RSTAT_FIELD(dir, rbytes),
Venky Shankard7a2dc52022-03-10 09:34:19 -0500415 XATTR_RSTAT_FIELD_UPDATABLE(dir, rctime),
Luis Henriquesfb18a572018-01-05 10:47:18 +0000416 {
Yan, Zheng08796872019-01-09 11:07:02 +0800417 .name = "ceph.dir.pin",
David Disseldorpe1b81432019-04-18 14:15:45 +0200418 .name_size = sizeof("ceph.dir.pin"),
Yan, Zheng08796872019-01-09 11:07:02 +0800419 .getxattr_cb = ceph_vxattrcb_dir_pin,
420 .exists_cb = ceph_vxattrcb_dir_pin_exists,
421 .flags = VXATTR_FLAG_HIDDEN,
422 },
423 {
Luis Henriquesfb18a572018-01-05 10:47:18 +0000424 .name = "ceph.quota",
425 .name_size = sizeof("ceph.quota"),
426 .getxattr_cb = ceph_vxattrcb_quota,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000427 .exists_cb = ceph_vxattrcb_quota_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800428 .flags = VXATTR_FLAG_HIDDEN,
Luis Henriquesfb18a572018-01-05 10:47:18 +0000429 },
430 XATTR_QUOTA_FIELD(quota, max_bytes),
431 XATTR_QUOTA_FIELD(quota, max_files),
David Disseldorp100cc612019-04-18 14:15:47 +0200432 {
433 .name = "ceph.snap.btime",
434 .name_size = sizeof("ceph.snap.btime"),
435 .getxattr_cb = ceph_vxattrcb_snap_btime,
436 .exists_cb = ceph_vxattrcb_snap_btime_exists,
437 .flags = VXATTR_FLAG_READONLY,
438 },
Luis Henriquesdd980fc2020-11-23 17:38:46 +0000439 {
440 .name = "ceph.caps",
441 .name_size = sizeof("ceph.caps"),
442 .getxattr_cb = ceph_vxattrcb_caps,
443 .exists_cb = NULL,
444 .flags = VXATTR_FLAG_HIDDEN,
445 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600446 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700447};
448
449/* files */
450
Alex Elder881a5fa2012-01-23 15:49:28 -0600451static struct ceph_vxattr ceph_file_vxattrs[] = {
Sage Weil32ab0bd2013-01-19 16:46:32 -0800452 {
453 .name = "ceph.file.layout",
454 .name_size = sizeof("ceph.file.layout"),
455 .getxattr_cb = ceph_vxattrcb_layout,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800456 .exists_cb = ceph_vxattrcb_layout_exists,
Yan, Zheng4e9906e2018-04-25 17:14:05 +0800457 .flags = VXATTR_FLAG_HIDDEN,
Sage Weil32ab0bd2013-01-19 16:46:32 -0800458 },
Sage Weil695b7112013-01-20 22:08:33 -0800459 XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
460 XATTR_LAYOUT_FIELD(file, layout, stripe_count),
461 XATTR_LAYOUT_FIELD(file, layout, object_size),
462 XATTR_LAYOUT_FIELD(file, layout, pool),
Yan, Zheng779fe0f2016-03-07 09:35:06 +0800463 XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
David Disseldorp100cc612019-04-18 14:15:47 +0200464 {
465 .name = "ceph.snap.btime",
466 .name_size = sizeof("ceph.snap.btime"),
467 .getxattr_cb = ceph_vxattrcb_snap_btime,
468 .exists_cb = ceph_vxattrcb_snap_btime_exists,
469 .flags = VXATTR_FLAG_READONLY,
470 },
Luis Henriquesdd980fc2020-11-23 17:38:46 +0000471 {
472 .name = "ceph.caps",
473 .name_size = sizeof("ceph.caps"),
474 .getxattr_cb = ceph_vxattrcb_caps,
475 .exists_cb = NULL,
476 .flags = VXATTR_FLAG_HIDDEN,
477 },
Alex Elder2c3dd4f2013-02-19 12:25:56 -0600478 { .name = NULL, 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700479};
480
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800481static struct ceph_vxattr ceph_common_vxattrs[] = {
482 {
483 .name = "ceph.cluster_fsid",
484 .name_size = sizeof("ceph.cluster_fsid"),
485 .getxattr_cb = ceph_vxattrcb_cluster_fsid,
486 .exists_cb = NULL,
487 .flags = VXATTR_FLAG_READONLY,
488 },
489 {
490 .name = "ceph.client_id",
491 .name_size = sizeof("ceph.client_id"),
492 .getxattr_cb = ceph_vxattrcb_client_id,
493 .exists_cb = NULL,
494 .flags = VXATTR_FLAG_READONLY,
495 },
Jeff Layton40e309d2021-07-26 07:07:50 -0400496 {
497 .name = "ceph.auth_mds",
498 .name_size = sizeof("ceph.auth_mds"),
499 .getxattr_cb = ceph_vxattrcb_auth_mds,
500 .exists_cb = NULL,
501 .flags = VXATTR_FLAG_READONLY,
502 },
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800503 { .name = NULL, 0 } /* Required table terminator */
504};
505
Alex Elder881a5fa2012-01-23 15:49:28 -0600506static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700507{
508 if (S_ISDIR(inode->i_mode))
509 return ceph_dir_vxattrs;
510 else if (S_ISREG(inode->i_mode))
511 return ceph_file_vxattrs;
512 return NULL;
513}
514
Alex Elder881a5fa2012-01-23 15:49:28 -0600515static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700516 const char *name)
517{
Alex Elder881a5fa2012-01-23 15:49:28 -0600518 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600519
520 if (vxattr) {
521 while (vxattr->name) {
522 if (!strcmp(vxattr->name, name))
523 return vxattr;
524 vxattr++;
525 }
526 }
527
Xiubo Li5a9e2f52020-11-11 09:29:40 +0800528 vxattr = ceph_common_vxattrs;
529 while (vxattr->name) {
530 if (!strcmp(vxattr->name, name))
531 return vxattr;
532 vxattr++;
533 }
534
Sage Weil355da1e2009-10-06 11:31:08 -0700535 return NULL;
536}
537
538static int __set_xattr(struct ceph_inode_info *ci,
539 const char *name, int name_len,
540 const char *val, int val_len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800541 int flags, int update_xattr,
Sage Weil355da1e2009-10-06 11:31:08 -0700542 struct ceph_inode_xattr **newxattr)
543{
544 struct rb_node **p;
545 struct rb_node *parent = NULL;
546 struct ceph_inode_xattr *xattr = NULL;
547 int c;
548 int new = 0;
549
550 p = &ci->i_xattrs.index.rb_node;
551 while (*p) {
552 parent = *p;
553 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
554 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
555 if (c < 0)
556 p = &(*p)->rb_left;
557 else if (c > 0)
558 p = &(*p)->rb_right;
559 else {
560 if (name_len == xattr->name_len)
561 break;
562 else if (name_len < xattr->name_len)
563 p = &(*p)->rb_left;
564 else
565 p = &(*p)->rb_right;
566 }
567 xattr = NULL;
568 }
569
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800570 if (update_xattr) {
571 int err = 0;
Luis Henriqueseeca9582017-04-28 11:14:04 +0100572
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800573 if (xattr && (flags & XATTR_CREATE))
574 err = -EEXIST;
575 else if (!xattr && (flags & XATTR_REPLACE))
576 err = -ENODATA;
577 if (err) {
578 kfree(name);
579 kfree(val);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100580 kfree(*newxattr);
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800581 return err;
582 }
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800583 if (update_xattr < 0) {
584 if (xattr)
585 __remove_xattr(ci, xattr);
586 kfree(name);
Luis Henriqueseeca9582017-04-28 11:14:04 +0100587 kfree(*newxattr);
Yan, Zhengbcdfeb22014-02-11 13:04:19 +0800588 return 0;
589 }
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800590 }
591
Sage Weil355da1e2009-10-06 11:31:08 -0700592 if (!xattr) {
593 new = 1;
594 xattr = *newxattr;
595 xattr->name = name;
596 xattr->name_len = name_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800597 xattr->should_free_name = update_xattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700598
599 ci->i_xattrs.count++;
600 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
601 } else {
602 kfree(*newxattr);
603 *newxattr = NULL;
604 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000605 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700606
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800607 if (update_xattr) {
Xu Wangc00e4522020-07-08 07:03:22 +0000608 kfree(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700609 name = xattr->name;
610 }
611 ci->i_xattrs.names_size -= xattr->name_len;
612 ci->i_xattrs.vals_size -= xattr->val_len;
613 }
Sage Weil355da1e2009-10-06 11:31:08 -0700614 ci->i_xattrs.names_size += name_len;
615 ci->i_xattrs.vals_size += val_len;
616 if (val)
617 xattr->val = val;
618 else
619 xattr->val = "";
620
621 xattr->val_len = val_len;
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800622 xattr->dirty = update_xattr;
623 xattr->should_free_val = (val && update_xattr);
Sage Weil355da1e2009-10-06 11:31:08 -0700624
625 if (new) {
626 rb_link_node(&xattr->node, parent, p);
627 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
628 dout("__set_xattr_val p=%p\n", p);
629 }
630
Yan, Zheng05729782019-05-27 16:15:41 +0800631 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
David Howells874c8ca12022-06-09 21:46:04 +0100632 ceph_vinop(&ci->netfs.inode), xattr, name_len, name, val_len, val);
Sage Weil355da1e2009-10-06 11:31:08 -0700633
634 return 0;
635}
636
637static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
638 const char *name)
639{
640 struct rb_node **p;
641 struct rb_node *parent = NULL;
642 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800643 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700644 int c;
645
646 p = &ci->i_xattrs.index.rb_node;
647 while (*p) {
648 parent = *p;
649 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
650 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800651 if (c == 0 && name_len > xattr->name_len)
652 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700653 if (c < 0)
654 p = &(*p)->rb_left;
655 else if (c > 0)
656 p = &(*p)->rb_right;
657 else {
658 dout("__get_xattr %s: found %.*s\n", name,
659 xattr->val_len, xattr->val);
660 return xattr;
661 }
662 }
663
664 dout("__get_xattr %s: not found\n", name);
665
666 return NULL;
667}
668
669static void __free_xattr(struct ceph_inode_xattr *xattr)
670{
671 BUG_ON(!xattr);
672
673 if (xattr->should_free_name)
Xu Wangc00e4522020-07-08 07:03:22 +0000674 kfree(xattr->name);
Sage Weil355da1e2009-10-06 11:31:08 -0700675 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000676 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700677
678 kfree(xattr);
679}
680
681static int __remove_xattr(struct ceph_inode_info *ci,
682 struct ceph_inode_xattr *xattr)
683{
684 if (!xattr)
Yan, Zheng524186a2014-02-11 13:23:09 +0800685 return -ENODATA;
Sage Weil355da1e2009-10-06 11:31:08 -0700686
687 rb_erase(&xattr->node, &ci->i_xattrs.index);
688
689 if (xattr->should_free_name)
Xu Wangc00e4522020-07-08 07:03:22 +0000690 kfree(xattr->name);
Sage Weil355da1e2009-10-06 11:31:08 -0700691 if (xattr->should_free_val)
Xu Wangc00e4522020-07-08 07:03:22 +0000692 kfree(xattr->val);
Sage Weil355da1e2009-10-06 11:31:08 -0700693
694 ci->i_xattrs.names_size -= xattr->name_len;
695 ci->i_xattrs.vals_size -= xattr->val_len;
696 ci->i_xattrs.count--;
697 kfree(xattr);
698
699 return 0;
700}
701
Sage Weil355da1e2009-10-06 11:31:08 -0700702static char *__copy_xattr_names(struct ceph_inode_info *ci,
703 char *dest)
704{
705 struct rb_node *p;
706 struct ceph_inode_xattr *xattr = NULL;
707
708 p = rb_first(&ci->i_xattrs.index);
709 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
710
711 while (p) {
712 xattr = rb_entry(p, struct ceph_inode_xattr, node);
713 memcpy(dest, xattr->name, xattr->name_len);
714 dest[xattr->name_len] = '\0';
715
716 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
717 xattr->name_len, ci->i_xattrs.names_size);
718
719 dest += xattr->name_len + 1;
720 p = rb_next(p);
721 }
722
723 return dest;
724}
725
726void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
727{
728 struct rb_node *p, *tmp;
729 struct ceph_inode_xattr *xattr = NULL;
730
731 p = rb_first(&ci->i_xattrs.index);
732
733 dout("__ceph_destroy_xattrs p=%p\n", p);
734
735 while (p) {
736 xattr = rb_entry(p, struct ceph_inode_xattr, node);
737 tmp = p;
738 p = rb_next(tmp);
739 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
740 xattr->name_len, xattr->name);
741 rb_erase(tmp, &ci->i_xattrs.index);
742
743 __free_xattr(xattr);
744 }
745
746 ci->i_xattrs.names_size = 0;
747 ci->i_xattrs.vals_size = 0;
748 ci->i_xattrs.index_version = 0;
749 ci->i_xattrs.count = 0;
750 ci->i_xattrs.index = RB_ROOT;
751}
752
753static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800754 __releases(ci->i_ceph_lock)
755 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700756{
757 u32 namelen;
758 u32 numattr = 0;
759 void *p, *end;
760 u32 len;
761 const char *name, *val;
762 struct ceph_inode_info *ci = ceph_inode(inode);
Xiubo Li0eb308532019-12-18 21:15:18 -0500763 u64 xattr_version;
Sage Weil355da1e2009-10-06 11:31:08 -0700764 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800765 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700766 int i;
767
768 dout("__build_xattrs() len=%d\n",
769 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
770
771 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
772 return 0; /* already built */
773
774 __ceph_destroy_xattrs(ci);
775
776start:
777 /* updated internal xattr rb tree */
778 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
779 p = ci->i_xattrs.blob->vec.iov_base;
780 end = p + ci->i_xattrs.blob->vec.iov_len;
781 ceph_decode_32_safe(&p, end, numattr, bad);
782 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800783 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700784
Ilya Dryomov7e8a2952014-07-25 11:47:21 +0400785 xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
Sage Weil355da1e2009-10-06 11:31:08 -0700786 GFP_NOFS);
787 err = -ENOMEM;
788 if (!xattrs)
789 goto bad_lock;
Ilya Dryomov1a295bd2014-07-25 12:44:58 +0400790
Sage Weil355da1e2009-10-06 11:31:08 -0700791 for (i = 0; i < numattr; i++) {
792 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
793 GFP_NOFS);
794 if (!xattrs[i])
795 goto bad_lock;
796 }
797
Sage Weilbe655592011-11-30 09:47:09 -0800798 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700799 if (ci->i_xattrs.version != xattr_version) {
800 /* lost a race, retry */
801 for (i = 0; i < numattr; i++)
802 kfree(xattrs[i]);
803 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500804 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700805 goto start;
806 }
807 err = -EIO;
808 while (numattr--) {
809 ceph_decode_32_safe(&p, end, len, bad);
810 namelen = len;
811 name = p;
812 p += len;
813 ceph_decode_32_safe(&p, end, len, bad);
814 val = p;
815 p += len;
816
817 err = __set_xattr(ci, name, namelen, val, len,
Yan, Zhengfbc0b972014-02-11 13:01:19 +0800818 0, 0, &xattrs[numattr]);
Sage Weil355da1e2009-10-06 11:31:08 -0700819
820 if (err < 0)
821 goto bad;
822 }
823 kfree(xattrs);
824 }
825 ci->i_xattrs.index_version = ci->i_xattrs.version;
826 ci->i_xattrs.dirty = false;
827
828 return err;
829bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800830 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700831bad:
832 if (xattrs) {
833 for (i = 0; i < numattr; i++)
834 kfree(xattrs[i]);
835 kfree(xattrs);
836 }
837 ci->i_xattrs.names_size = 0;
838 return err;
839}
840
841static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
842 int val_size)
843{
844 /*
845 * 4 bytes for the length, and additional 4 bytes per each xattr name,
846 * 4 bytes per each value
847 */
848 int size = 4 + ci->i_xattrs.count*(4 + 4) +
849 ci->i_xattrs.names_size +
850 ci->i_xattrs.vals_size;
851 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
852 ci->i_xattrs.count, ci->i_xattrs.names_size,
853 ci->i_xattrs.vals_size);
854
855 if (name_size)
856 size += 4 + 4 + name_size + val_size;
857
858 return size;
859}
860
861/*
862 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100863 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
864 * that it can be freed by the caller as the i_ceph_lock is likely to be
865 * held.
Sage Weil355da1e2009-10-06 11:31:08 -0700866 */
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100867struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
Sage Weil355da1e2009-10-06 11:31:08 -0700868{
869 struct rb_node *p;
870 struct ceph_inode_xattr *xattr = NULL;
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100871 struct ceph_buffer *old_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700872 void *dest;
873
David Howells874c8ca12022-06-09 21:46:04 +0100874 dout("__build_xattrs_blob %p\n", &ci->netfs.inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700875 if (ci->i_xattrs.dirty) {
876 int need = __get_required_blob_size(ci, 0, 0);
877
878 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
879
880 p = rb_first(&ci->i_xattrs.index);
881 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
882
883 ceph_encode_32(&dest, ci->i_xattrs.count);
884 while (p) {
885 xattr = rb_entry(p, struct ceph_inode_xattr, node);
886
887 ceph_encode_32(&dest, xattr->name_len);
888 memcpy(dest, xattr->name, xattr->name_len);
889 dest += xattr->name_len;
890 ceph_encode_32(&dest, xattr->val_len);
891 memcpy(dest, xattr->val, xattr->val_len);
892 dest += xattr->val_len;
893
894 p = rb_next(p);
895 }
896
897 /* adjust buffer len; it may be larger than we need */
898 ci->i_xattrs.prealloc_blob->vec.iov_len =
899 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
900
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800901 if (ci->i_xattrs.blob)
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100902 old_blob = ci->i_xattrs.blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700903 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
904 ci->i_xattrs.prealloc_blob = NULL;
905 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700906 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700907 }
Luis Henriques12fe3dd2019-07-19 15:32:21 +0100908
909 return old_blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700910}
911
Yan, Zheng315f2402016-03-07 10:34:50 +0800912static inline int __get_request_mask(struct inode *in) {
913 struct ceph_mds_request *req = current->journal_info;
914 int mask = 0;
915 if (req && req->r_target_inode == in) {
916 if (req->r_op == CEPH_MDS_OP_LOOKUP ||
917 req->r_op == CEPH_MDS_OP_LOOKUPINO ||
918 req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
919 req->r_op == CEPH_MDS_OP_GETATTR) {
920 mask = le32_to_cpu(req->r_args.getattr.mask);
921 } else if (req->r_op == CEPH_MDS_OP_OPEN ||
922 req->r_op == CEPH_MDS_OP_CREATE) {
923 mask = le32_to_cpu(req->r_args.open.mask);
924 }
925 }
926 return mask;
927}
928
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800929ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
Sage Weil355da1e2009-10-06 11:31:08 -0700930 size_t size)
931{
Sage Weil355da1e2009-10-06 11:31:08 -0700932 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700933 struct ceph_inode_xattr *xattr;
Milind Changire6ddf5f12022-02-14 05:01:01 +0000934 struct ceph_vxattr *vxattr;
Yan, Zheng315f2402016-03-07 10:34:50 +0800935 int req_mask;
Jeff Laytonf1d1b512019-06-24 07:39:18 -0400936 ssize_t err;
Sage Weil355da1e2009-10-06 11:31:08 -0700937
Milind Changire6ddf5f12022-02-14 05:01:01 +0000938 if (strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
939 goto handle_non_vxattrs;
940
Sage Weil0bee82f2013-01-20 22:00:58 -0800941 /* let's see if a virtual xattr was requested */
942 vxattr = ceph_match_vxattr(inode, name);
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800943 if (vxattr) {
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800944 int mask = 0;
945 if (vxattr->flags & VXATTR_FLAG_RSTAT)
946 mask |= CEPH_STAT_RSTAT;
Jeff Layton81048c02020-11-03 14:00:26 -0500947 if (vxattr->flags & VXATTR_FLAG_DIRSTAT)
948 mask |= CEPH_CAP_FILE_SHARED;
Yan, Zheng49a9f4f2018-04-25 17:30:23 +0800949 err = ceph_do_getattr(inode, mask, true);
Yan, Zheng1684dd02017-06-14 15:54:56 +0800950 if (err)
951 return err;
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800952 err = -ENODATA;
Jeff Layton3b421012019-06-13 15:17:00 -0400953 if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
Yan, Zheng29dccfa2016-03-12 13:32:16 +0800954 err = vxattr->getxattr_cb(ci, value, size);
Jeff Layton3b421012019-06-13 15:17:00 -0400955 if (size && size < err)
956 err = -ERANGE;
957 }
majianpenga1dc1932013-06-19 14:58:10 +0800958 return err;
Milind Changire6ddf5f12022-02-14 05:01:01 +0000959 } else {
960 err = ceph_do_getvxattr(inode, name, value, size);
961 /* this would happen with a new client and old server combo */
962 if (err == -EOPNOTSUPP)
963 err = -ENODATA;
964 return err;
Sage Weil0bee82f2013-01-20 22:00:58 -0800965 }
Milind Changire6ddf5f12022-02-14 05:01:01 +0000966handle_non_vxattrs:
Yan, Zheng315f2402016-03-07 10:34:50 +0800967 req_mask = __get_request_mask(inode);
968
majianpenga1dc1932013-06-19 14:58:10 +0800969 spin_lock(&ci->i_ceph_lock);
Jeff Laytond36e0b62020-01-07 13:12:57 -0500970 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode, name,
majianpenga1dc1932013-06-19 14:58:10 +0800971 ci->i_xattrs.version, ci->i_xattrs.index_version);
972
Yan, Zheng508b32d2014-09-16 21:46:17 +0800973 if (ci->i_xattrs.version == 0 ||
Yan, Zheng315f2402016-03-07 10:34:50 +0800974 !((req_mask & CEPH_CAP_XATTR_SHARED) ||
Xiubo Li1af16d52020-03-19 23:45:00 -0400975 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800976 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng315f2402016-03-07 10:34:50 +0800977
978 /* security module gets xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +0200979 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +0800980 pr_warn_ratelimited("sync getxattr %p "
981 "during filling trace\n", inode);
982 return -EBUSY;
983 }
984
Sage Weil355da1e2009-10-06 11:31:08 -0700985 /* get xattrs from mds (if we don't already have them) */
Yan, Zheng508b32d2014-09-16 21:46:17 +0800986 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -0700987 if (err)
988 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +0800989 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700990 }
991
Sage Weil355da1e2009-10-06 11:31:08 -0700992 err = __build_xattrs(inode);
993 if (err < 0)
994 goto out;
995
Sage Weil355da1e2009-10-06 11:31:08 -0700996 err = -ENODATA; /* == ENOATTR */
997 xattr = __get_xattr(ci, name);
Sage Weil0bee82f2013-01-20 22:00:58 -0800998 if (!xattr)
Sage Weil355da1e2009-10-06 11:31:08 -0700999 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001000
1001 err = -ERANGE;
1002 if (size && size < xattr->val_len)
1003 goto out;
1004
1005 err = xattr->val_len;
1006 if (size == 0)
1007 goto out;
1008
1009 memcpy(value, xattr->val, xattr->val_len);
1010
Markus Elfringd37b1d92017-08-20 20:22:02 +02001011 if (current->journal_info &&
Jeff Layton026105e2019-08-06 10:41:40 -04001012 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
1013 security_ismaclabel(name + XATTR_SECURITY_PREFIX_LEN))
Yan, Zheng315f2402016-03-07 10:34:50 +08001014 ci->i_ceph_flags |= CEPH_I_SEC_INITED;
Sage Weil355da1e2009-10-06 11:31:08 -07001015out:
Sage Weilbe655592011-11-30 09:47:09 -08001016 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001017 return err;
1018}
1019
1020ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
1021{
David Howells2b0143b2015-03-17 22:25:59 +00001022 struct inode *inode = d_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -07001023 struct ceph_inode_info *ci = ceph_inode(inode);
David Disseldorp2b2abca2019-04-18 14:15:48 +02001024 bool len_only = (size == 0);
Sage Weil355da1e2009-10-06 11:31:08 -07001025 u32 namelen;
1026 int err;
Sage Weil355da1e2009-10-06 11:31:08 -07001027
Sage Weilbe655592011-11-30 09:47:09 -08001028 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001029 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
1030 ci->i_xattrs.version, ci->i_xattrs.index_version);
1031
Yan, Zheng508b32d2014-09-16 21:46:17 +08001032 if (ci->i_xattrs.version == 0 ||
Xiubo Li1af16d52020-03-19 23:45:00 -04001033 !__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 1)) {
Sage Weilbe655592011-11-30 09:47:09 -08001034 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng508b32d2014-09-16 21:46:17 +08001035 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001036 if (err)
1037 return err;
Yan, Zheng508b32d2014-09-16 21:46:17 +08001038 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001039 }
1040
Sage Weil355da1e2009-10-06 11:31:08 -07001041 err = __build_xattrs(inode);
1042 if (err < 0)
1043 goto out;
Alex Elder3ce6cd12012-01-23 15:49:28 -06001044
David Disseldorp2b2abca2019-04-18 14:15:48 +02001045 /* add 1 byte for each xattr due to the null termination */
Sage Weilb65917d2013-01-20 22:02:39 -08001046 namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
David Disseldorp2b2abca2019-04-18 14:15:48 +02001047 if (!len_only) {
1048 if (namelen > size) {
1049 err = -ERANGE;
1050 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001051 }
David Disseldorp2b2abca2019-04-18 14:15:48 +02001052 names = __copy_xattr_names(ci, names);
1053 size -= namelen;
Sage Weilb65917d2013-01-20 22:02:39 -08001054 }
David Disseldorp2b2abca2019-04-18 14:15:48 +02001055 err = namelen;
Sage Weil355da1e2009-10-06 11:31:08 -07001056out:
Sage Weilbe655592011-11-30 09:47:09 -08001057 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001058 return err;
1059}
1060
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001061static int ceph_sync_setxattr(struct inode *inode, const char *name,
Sage Weil355da1e2009-10-06 11:31:08 -07001062 const char *value, size_t size, int flags)
1063{
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001064 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
Sage Weil355da1e2009-10-06 11:31:08 -07001065 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001066 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001067 struct ceph_mds_client *mdsc = fsc->mdsc;
Xiubo Li968cd142020-12-09 10:52:20 +08001068 struct ceph_osd_client *osdc = &fsc->client->osdc;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001069 struct ceph_pagelist *pagelist = NULL;
Yan, Zheng04303d82016-04-21 12:11:54 +08001070 int op = CEPH_MDS_OP_SETXATTR;
Sage Weil355da1e2009-10-06 11:31:08 -07001071 int err;
Sage Weil355da1e2009-10-06 11:31:08 -07001072
Yan, Zheng0aeff372014-12-17 21:26:47 +08001073 if (size > 0) {
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001074 /* copy value into pagelist */
Ilya Dryomov33165d42018-09-28 15:38:34 +02001075 pagelist = ceph_pagelist_alloc(GFP_NOFS);
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001076 if (!pagelist)
Sage Weil355da1e2009-10-06 11:31:08 -07001077 return -ENOMEM;
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001078
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001079 err = ceph_pagelist_append(pagelist, value, size);
1080 if (err)
1081 goto out;
Yan, Zheng0aeff372014-12-17 21:26:47 +08001082 } else if (!value) {
Yan, Zheng04303d82016-04-21 12:11:54 +08001083 if (flags & CEPH_XATTR_REPLACE)
1084 op = CEPH_MDS_OP_RMXATTR;
1085 else
1086 flags |= CEPH_XATTR_REMOVE;
Sage Weil355da1e2009-10-06 11:31:08 -07001087 }
1088
Luís Henriquesd93231a2022-06-03 14:29:09 +01001089 dout("setxattr value size: %zu\n", size);
Sage Weil355da1e2009-10-06 11:31:08 -07001090
1091 /* do request */
Yan, Zheng04303d82016-04-21 12:11:54 +08001092 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +01001093 if (IS_ERR(req)) {
1094 err = PTR_ERR(req);
1095 goto out;
1096 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001097
Sage Weil355da1e2009-10-06 11:31:08 -07001098 req->r_path2 = kstrdup(name, GFP_NOFS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001099 if (!req->r_path2) {
1100 ceph_mdsc_put_request(req);
1101 err = -ENOMEM;
1102 goto out;
1103 }
Sage Weil355da1e2009-10-06 11:31:08 -07001104
Yan, Zheng04303d82016-04-21 12:11:54 +08001105 if (op == CEPH_MDS_OP_SETXATTR) {
1106 req->r_args.setxattr.flags = cpu_to_le32(flags);
Xiubo Li968cd142020-12-09 10:52:20 +08001107 req->r_args.setxattr.osdmap_epoch =
1108 cpu_to_le32(osdc->osdmap->epoch);
Yan, Zheng04303d82016-04-21 12:11:54 +08001109 req->r_pagelist = pagelist;
1110 pagelist = NULL;
1111 }
Sage Weil355da1e2009-10-06 11:31:08 -07001112
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -04001113 req->r_inode = inode;
1114 ihold(inode);
1115 req->r_num_caps = 1;
1116 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1117
Sage Weil355da1e2009-10-06 11:31:08 -07001118 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil752c8bd2013-02-05 13:52:29 -08001119 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil355da1e2009-10-06 11:31:08 -07001120 ceph_mdsc_put_request(req);
1121 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
1122
1123out:
Yan, Zheng25e6bae2014-09-16 19:15:28 +08001124 if (pagelist)
1125 ceph_pagelist_release(pagelist);
Sage Weil355da1e2009-10-06 11:31:08 -07001126 return err;
1127}
1128
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001129int __ceph_setxattr(struct inode *inode, const char *name,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001130 const void *value, size_t size, int flags)
Sage Weil355da1e2009-10-06 11:31:08 -07001131{
Alex Elder881a5fa2012-01-23 15:49:28 -06001132 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -07001133 struct ceph_inode_info *ci = ceph_inode(inode);
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001134 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001135 struct ceph_cap_flush *prealloc_cf = NULL;
Luis Henriques86968ef2019-07-19 15:32:20 +01001136 struct ceph_buffer *old_blob = NULL;
Alex Elder18fa8b32012-01-23 15:49:28 -06001137 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -07001138 int err;
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001139 int dirty = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001140 int name_len = strlen(name);
1141 int val_len = size;
1142 char *newname = NULL;
1143 char *newval = NULL;
1144 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001145 int required_blob_size;
Yan, Zhengf1919822018-04-08 09:54:39 +08001146 bool check_realm = false;
Yan, Zheng604d1b02015-05-01 17:49:16 +08001147 bool lock_snap_rwsem = false;
Sage Weil355da1e2009-10-06 11:31:08 -07001148
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001149 if (ceph_snap(inode) != CEPH_NOSNAP)
1150 return -EROFS;
Sage Weil355da1e2009-10-06 11:31:08 -07001151
Alex Elder06476a62012-01-23 15:49:27 -06001152 vxattr = ceph_match_vxattr(inode, name);
Yan, Zhengf1919822018-04-08 09:54:39 +08001153 if (vxattr) {
Yan, Zheng4e9906e2018-04-25 17:14:05 +08001154 if (vxattr->flags & VXATTR_FLAG_READONLY)
Yan, Zhengf1919822018-04-08 09:54:39 +08001155 return -EOPNOTSUPP;
1156 if (value && !strncmp(vxattr->name, "ceph.quota", 10))
1157 check_realm = true;
1158 }
Sage Weil355da1e2009-10-06 11:31:08 -07001159
Sage Weil3adf6542013-01-31 11:53:41 -08001160 /* pass any unhandled ceph.* xattrs through to the MDS */
1161 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1162 goto do_sync_unlocked;
1163
Sage Weil355da1e2009-10-06 11:31:08 -07001164 /* preallocate memory for xattr name, value, index node */
1165 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +02001166 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001167 if (!newname)
1168 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001169
1170 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -06001171 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001172 if (!newval)
1173 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -07001174 }
1175
1176 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
1177 if (!xattr)
1178 goto out;
1179
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001180 prealloc_cf = ceph_alloc_cap_flush();
1181 if (!prealloc_cf)
1182 goto out;
1183
Sage Weilbe655592011-11-30 09:47:09 -08001184 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001185retry:
1186 issued = __ceph_caps_issued(ci, NULL);
Luís Henriquesd93231a2022-06-03 14:29:09 +01001187 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
1188 if ((ci->i_xattrs.version == 0) || !(issued & CEPH_CAP_XATTR_EXCL) ||
1189 (required_blob_size > mdsc->mdsmap->m_max_xattr_size)) {
1190 dout("%s do sync setxattr: version: %llu size: %d max: %llu\n",
1191 __func__, ci->i_xattrs.version, required_blob_size,
1192 mdsc->mdsmap->m_max_xattr_size);
Sage Weil355da1e2009-10-06 11:31:08 -07001193 goto do_sync;
Luís Henriquesd93231a2022-06-03 14:29:09 +01001194 }
Yan, Zheng604d1b02015-05-01 17:49:16 +08001195
1196 if (!lock_snap_rwsem && !ci->i_head_snapc) {
1197 lock_snap_rwsem = true;
1198 if (!down_read_trylock(&mdsc->snap_rwsem)) {
1199 spin_unlock(&ci->i_ceph_lock);
1200 down_read(&mdsc->snap_rwsem);
1201 spin_lock(&ci->i_ceph_lock);
1202 goto retry;
1203 }
1204 }
1205
Jeff Laytond36e0b62020-01-07 13:12:57 -05001206 dout("setxattr %p name '%s' issued %s\n", inode, name,
1207 ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -07001208 __build_xattrs(inode);
1209
Sage Weil355da1e2009-10-06 11:31:08 -07001210 if (!ci->i_xattrs.prealloc_blob ||
1211 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -06001212 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001213
Sage Weilbe655592011-11-30 09:47:09 -08001214 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001215 ceph_buffer_put(old_blob); /* Shouldn't be required */
1216 dout(" pre-allocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001217 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -07001218 if (!blob)
Yan, Zheng604d1b02015-05-01 17:49:16 +08001219 goto do_sync_unlocked;
Sage Weilbe655592011-11-30 09:47:09 -08001220 spin_lock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001221 /* prealloc_blob can't be released while holding i_ceph_lock */
Sage Weilb6c1d5b2009-12-07 12:17:17 -08001222 if (ci->i_xattrs.prealloc_blob)
Luis Henriques86968ef2019-07-19 15:32:20 +01001223 old_blob = ci->i_xattrs.prealloc_blob;
Sage Weil355da1e2009-10-06 11:31:08 -07001224 ci->i_xattrs.prealloc_blob = blob;
1225 goto retry;
1226 }
1227
Yan, Zhengbcdfeb22014-02-11 13:04:19 +08001228 err = __set_xattr(ci, newname, name_len, newval, val_len,
1229 flags, value ? 1 : -1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -06001230
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001231 if (!err) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001232 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
1233 &prealloc_cf);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001234 ci->i_xattrs.dirty = true;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001235 inode->i_ctime = current_time(inode);
Yan, Zhengfbc0b972014-02-11 13:01:19 +08001236 }
Alex Elder18fa8b32012-01-23 15:49:28 -06001237
Sage Weilbe655592011-11-30 09:47:09 -08001238 spin_unlock(&ci->i_ceph_lock);
Luis Henriques86968ef2019-07-19 15:32:20 +01001239 ceph_buffer_put(old_blob);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001240 if (lock_snap_rwsem)
1241 up_read(&mdsc->snap_rwsem);
Sage Weilfca65b42011-05-04 11:33:47 -07001242 if (dirty)
1243 __mark_inode_dirty(inode, dirty);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001244 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001245 return err;
1246
1247do_sync:
Sage Weilbe655592011-11-30 09:47:09 -08001248 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -08001249do_sync_unlocked:
Yan, Zheng604d1b02015-05-01 17:49:16 +08001250 if (lock_snap_rwsem)
1251 up_read(&mdsc->snap_rwsem);
Yan, Zheng315f2402016-03-07 10:34:50 +08001252
1253 /* security module set xattr while filling trace */
Markus Elfringd37b1d92017-08-20 20:22:02 +02001254 if (current->journal_info) {
Yan, Zheng315f2402016-03-07 10:34:50 +08001255 pr_warn_ratelimited("sync setxattr %p "
1256 "during filling trace\n", inode);
1257 err = -EBUSY;
1258 } else {
Andreas Gruenbachera26fecc2016-04-14 00:30:16 +02001259 err = ceph_sync_setxattr(inode, name, value, size, flags);
Yan, Zhengf1919822018-04-08 09:54:39 +08001260 if (err >= 0 && check_realm) {
1261 /* check if snaprealm was created for quota inode */
1262 spin_lock(&ci->i_ceph_lock);
1263 if ((ci->i_max_files || ci->i_max_bytes) &&
1264 !(ci->i_snap_realm &&
1265 ci->i_snap_realm->ino == ci->i_vino.ino))
1266 err = -EOPNOTSUPP;
1267 spin_unlock(&ci->i_ceph_lock);
1268 }
Yan, Zheng315f2402016-03-07 10:34:50 +08001269 }
Sage Weil355da1e2009-10-06 11:31:08 -07001270out:
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001271 ceph_free_cap_flush(prealloc_cf);
Sage Weil355da1e2009-10-06 11:31:08 -07001272 kfree(newname);
1273 kfree(newval);
1274 kfree(xattr);
1275 return err;
1276}
1277
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001278static int ceph_get_xattr_handler(const struct xattr_handler *handler,
1279 struct dentry *dentry, struct inode *inode,
1280 const char *name, void *value, size_t size)
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001281{
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001282 if (!ceph_is_valid_xattr(name))
1283 return -EOPNOTSUPP;
1284 return __ceph_getxattr(inode, name, value, size);
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001285}
Yan, Zheng315f2402016-03-07 10:34:50 +08001286
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001287static int ceph_set_xattr_handler(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01001288 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04001289 struct dentry *unused, struct inode *inode,
1290 const char *name, const void *value,
1291 size_t size, int flags)
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001292{
1293 if (!ceph_is_valid_xattr(name))
1294 return -EOPNOTSUPP;
Al Viro59301222016-05-27 10:19:30 -04001295 return __ceph_setxattr(inode, name, value, size, flags);
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001296}
1297
Wei Yongjun5130cce2016-10-17 15:11:29 +00001298static const struct xattr_handler ceph_other_xattr_handler = {
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02001299 .prefix = "", /* match any name => handlers called with full name */
1300 .get = ceph_get_xattr_handler,
1301 .set = ceph_set_xattr_handler,
1302};
1303
Yan, Zheng315f2402016-03-07 10:34:50 +08001304#ifdef CONFIG_SECURITY
1305bool ceph_security_xattr_wanted(struct inode *in)
1306{
1307 return in->i_security != NULL;
1308}
1309
1310bool ceph_security_xattr_deadlock(struct inode *in)
1311{
1312 struct ceph_inode_info *ci;
1313 bool ret;
Markus Elfringd37b1d92017-08-20 20:22:02 +02001314 if (!in->i_security)
Yan, Zheng315f2402016-03-07 10:34:50 +08001315 return false;
1316 ci = ceph_inode(in);
1317 spin_lock(&ci->i_ceph_lock);
1318 ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
1319 !(ci->i_xattrs.version > 0 &&
1320 __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
1321 spin_unlock(&ci->i_ceph_lock);
1322 return ret;
1323}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001324
1325#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1326int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
1327 struct ceph_acl_sec_ctx *as_ctx)
1328{
1329 struct ceph_pagelist *pagelist = as_ctx->pagelist;
1330 const char *name;
1331 size_t name_len;
1332 int err;
1333
1334 err = security_dentry_init_security(dentry, mode, &dentry->d_name,
Vivek Goyal15bf3232021-10-12 09:23:07 -04001335 &name, &as_ctx->sec_ctx,
Yan, Zhengac6713c2019-05-26 16:27:56 +08001336 &as_ctx->sec_ctxlen);
1337 if (err < 0) {
1338 WARN_ON_ONCE(err != -EOPNOTSUPP);
1339 err = 0; /* do nothing */
1340 goto out;
1341 }
1342
1343 err = -ENOMEM;
1344 if (!pagelist) {
1345 pagelist = ceph_pagelist_alloc(GFP_KERNEL);
1346 if (!pagelist)
1347 goto out;
1348 err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
1349 if (err)
1350 goto out;
1351 ceph_pagelist_encode_32(pagelist, 1);
1352 }
1353
1354 /*
1355 * FIXME: Make security_dentry_init_security() generic. Currently
1356 * It only supports single security module and only selinux has
1357 * dentry_init_security hook.
1358 */
Yan, Zhengac6713c2019-05-26 16:27:56 +08001359 name_len = strlen(name);
1360 err = ceph_pagelist_reserve(pagelist,
1361 4 * 2 + name_len + as_ctx->sec_ctxlen);
1362 if (err)
1363 goto out;
1364
1365 if (as_ctx->pagelist) {
1366 /* update count of KV pairs */
1367 BUG_ON(pagelist->length <= sizeof(__le32));
1368 if (list_is_singular(&pagelist->head)) {
1369 le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
1370 } else {
1371 struct page *page = list_first_entry(&pagelist->head,
1372 struct page, lru);
1373 void *addr = kmap_atomic(page);
1374 le32_add_cpu((__le32*)addr, 1);
1375 kunmap_atomic(addr);
1376 }
1377 } else {
1378 as_ctx->pagelist = pagelist;
1379 }
1380
1381 ceph_pagelist_encode_32(pagelist, name_len);
1382 ceph_pagelist_append(pagelist, name, name_len);
1383
1384 ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
1385 ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1386
1387 err = 0;
1388out:
1389 if (pagelist && !as_ctx->pagelist)
1390 ceph_pagelist_release(pagelist);
1391 return err;
1392}
Jeff Layton668959a2019-08-06 09:07:51 -04001393#endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1394#endif /* CONFIG_SECURITY */
Yan, Zheng5c31e922019-05-26 15:35:39 +08001395
1396void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
1397{
1398#ifdef CONFIG_CEPH_FS_POSIX_ACL
1399 posix_acl_release(as_ctx->acl);
1400 posix_acl_release(as_ctx->default_acl);
1401#endif
Yan, Zhengac6713c2019-05-26 16:27:56 +08001402#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1403 security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1404#endif
Yan, Zheng5c31e922019-05-26 15:35:39 +08001405 if (as_ctx->pagelist)
1406 ceph_pagelist_release(as_ctx->pagelist);
1407}
Yan, Zhengac6713c2019-05-26 16:27:56 +08001408
1409/*
1410 * List of handlers for synthetic system.* attributes. Other
1411 * attributes are handled directly.
1412 */
1413const struct xattr_handler *ceph_xattr_handlers[] = {
1414#ifdef CONFIG_CEPH_FS_POSIX_ACL
1415 &posix_acl_access_xattr_handler,
1416 &posix_acl_default_xattr_handler,
1417#endif
Yan, Zhengac6713c2019-05-26 16:27:56 +08001418 &ceph_other_xattr_handler,
1419 NULL,
1420};