blob: 8106bcb5a38d1199d410f48ac7a343ec2987c834 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiangb17500a2018-07-26 20:21:52 +08002/*
Gao Xiangb17500a2018-07-26 20:21:52 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiangbb88e8d2022-01-02 12:00:16 +08005 * Copyright (C) 2021-2022, Alibaba Cloud
Gao Xiangb17500a2018-07-26 20:21:52 +08006 */
7#include <linux/security.h>
8#include "xattr.h"
9
10struct xattr_iter {
11 struct super_block *sb;
Gao Xiangbb88e8d2022-01-02 12:00:16 +080012 struct erofs_buf buf;
Gao Xiangb17500a2018-07-26 20:21:52 +080013 void *kaddr;
14
15 erofs_blk_t blkaddr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020016 unsigned int ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +080017};
18
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080019static int init_inode_xattrs(struct inode *inode)
Gao Xiangb17500a2018-07-26 20:21:52 +080020{
Gao Xianga5876e22019-09-04 10:08:56 +080021 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangb17500a2018-07-26 20:21:52 +080022 struct xattr_iter it;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020023 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +080024 struct erofs_xattr_ibody_header *ih;
Gao Xiang6e789012018-08-21 22:49:30 +080025 struct super_block *sb;
Gao Xiangb17500a2018-07-26 20:21:52 +080026 struct erofs_sb_info *sbi;
Gao Xiang62dc4592019-02-18 15:19:04 +080027 int ret = 0;
Gao Xiangb17500a2018-07-26 20:21:52 +080028
Gao Xiang62dc4592019-02-18 15:19:04 +080029 /* the most case is that xattrs of this inode are initialized. */
Gao Xiangce0631292021-02-09 21:06:18 +080030 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
31 /*
32 * paired with smp_mb() at the end of the function to ensure
33 * fields will only be observed after the bit is set.
34 */
35 smp_mb();
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080036 return 0;
Gao Xiangce0631292021-02-09 21:06:18 +080037 }
Gao Xiangb17500a2018-07-26 20:21:52 +080038
Gao Xianga5876e22019-09-04 10:08:56 +080039 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
Gao Xiang62dc4592019-02-18 15:19:04 +080040 return -ERESTARTSYS;
41
42 /* someone has initialized xattrs for us? */
Gao Xianga5876e22019-09-04 10:08:56 +080043 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
Gao Xiang62dc4592019-02-18 15:19:04 +080044 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080045
46 /*
47 * bypass all xattr operations if ->xattr_isize is not greater than
48 * sizeof(struct erofs_xattr_ibody_header), in detail:
49 * 1) it is not enough to contain erofs_xattr_ibody_header then
50 * ->xattr_isize should be 0 (it means no xattr);
51 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
52 * undefined right now (maybe use later with some new sb feature).
53 */
54 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080055 erofs_err(inode->i_sb,
56 "xattr_isize %d of nid %llu is not supported yet",
57 vi->xattr_isize, vi->nid);
Gao Xiangff784a72019-08-14 18:37:05 +080058 ret = -EOPNOTSUPP;
Gao Xiang62dc4592019-02-18 15:19:04 +080059 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080060 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +080061 if (vi->xattr_isize) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080062 erofs_err(inode->i_sb,
63 "bogus xattr ibody @ nid %llu", vi->nid);
Gao Xiang7077fff2019-01-14 19:40:23 +080064 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +080065 ret = -EFSCORRUPTED;
Gao Xiang62dc4592019-02-18 15:19:04 +080066 goto out_unlock; /* xattr ondisk layout error */
Gao Xiang7077fff2019-01-14 19:40:23 +080067 }
Gao Xiang62dc4592019-02-18 15:19:04 +080068 ret = -ENOATTR;
69 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080070 }
Gao Xiangb17500a2018-07-26 20:21:52 +080071
Gao Xiang6e789012018-08-21 22:49:30 +080072 sb = inode->i_sb;
73 sbi = EROFS_SB(sb);
Gao Xiangbb88e8d2022-01-02 12:00:16 +080074 it.buf = __EROFS_BUF_INITIALIZER;
Gao Xiangb17500a2018-07-26 20:21:52 +080075 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
76 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
77
Gao Xiangbb88e8d2022-01-02 12:00:16 +080078 /* read in shared xattr array (non-atomic, see kmalloc below) */
79 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
80 if (IS_ERR(it.kaddr)) {
81 ret = PTR_ERR(it.kaddr);
Gao Xiang62dc4592019-02-18 15:19:04 +080082 goto out_unlock;
83 }
Gao Xiangb17500a2018-07-26 20:21:52 +080084
Gao Xiangb17500a2018-07-26 20:21:52 +080085 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
Gao Xiangb17500a2018-07-26 20:21:52 +080086 vi->xattr_shared_count = ih->h_shared_count;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080087 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
88 sizeof(uint), GFP_KERNEL);
Bhanusree Pola561fb352019-03-22 10:38:16 +080089 if (!vi->xattr_shared_xattrs) {
Gao Xiangbb88e8d2022-01-02 12:00:16 +080090 erofs_put_metabuf(&it.buf);
Gao Xiang62dc4592019-02-18 15:19:04 +080091 ret = -ENOMEM;
92 goto out_unlock;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080093 }
Gao Xiangb17500a2018-07-26 20:21:52 +080094
95 /* let's skip ibody header */
96 it.ofs += sizeof(struct erofs_xattr_ibody_header);
97
98 for (i = 0; i < vi->xattr_shared_count; ++i) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +080099 if (it.ofs >= EROFS_BLKSIZ) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800100 /* cannot be unaligned */
Gao Xiang9ddc7002019-08-13 10:30:54 +0800101 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
Gao Xiangb17500a2018-07-26 20:21:52 +0800102
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800103 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
104 EROFS_KMAP);
105 if (IS_ERR(it.kaddr)) {
Sheng Yong3b1b5292019-02-14 14:46:36 +0800106 kfree(vi->xattr_shared_xattrs);
107 vi->xattr_shared_xattrs = NULL;
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800108 ret = PTR_ERR(it.kaddr);
Gao Xiang62dc4592019-02-18 15:19:04 +0800109 goto out_unlock;
Sheng Yong3b1b5292019-02-14 14:46:36 +0800110 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800111 it.ofs = 0;
112 }
113 vi->xattr_shared_xattrs[i] =
114 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
115 it.ofs += sizeof(__le32);
116 }
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800117 erofs_put_metabuf(&it.buf);
Gao Xiangb17500a2018-07-26 20:21:52 +0800118
Gao Xiangce0631292021-02-09 21:06:18 +0800119 /* paired with smp_mb() at the beginning of the function. */
120 smp_mb();
Gao Xianga5876e22019-09-04 10:08:56 +0800121 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
Gao Xiang62dc4592019-02-18 15:19:04 +0800122
123out_unlock:
Gao Xianga5876e22019-09-04 10:08:56 +0800124 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
Gao Xiang62dc4592019-02-18 15:19:04 +0800125 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800126}
127
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800128/*
129 * the general idea for these return values is
130 * if 0 is returned, go on processing the current xattr;
131 * 1 (> 0) is returned, skip this round to process the next xattr;
132 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
133 * and need to be handled
134 */
Gao Xiangb17500a2018-07-26 20:21:52 +0800135struct xattr_iter_handlers {
Sidong Yang4b03f3f2019-01-08 13:24:54 +0000136 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
137 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
138 unsigned int len);
139 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
140 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
141 unsigned int len);
Gao Xiangb17500a2018-07-26 20:21:52 +0800142};
143
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800144static inline int xattr_iter_fixup(struct xattr_iter *it)
Gao Xiangb17500a2018-07-26 20:21:52 +0800145{
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800146 if (it->ofs < EROFS_BLKSIZ)
147 return 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800148
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800149 it->blkaddr += erofs_blknr(it->ofs);
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800150 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
151 EROFS_KMAP_ATOMIC);
152 if (IS_ERR(it->kaddr))
153 return PTR_ERR(it->kaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800154 it->ofs = erofs_blkoff(it->ofs);
155 return 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800156}
157
158static int inline_xattr_iter_begin(struct xattr_iter *it,
Julian Merida447a3622019-03-18 20:58:41 -0300159 struct inode *inode)
Gao Xiangb17500a2018-07-26 20:21:52 +0800160{
Gao Xianga5876e22019-09-04 10:08:56 +0800161 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangb17500a2018-07-26 20:21:52 +0800162 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200163 unsigned int xattr_header_sz, inline_xattr_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800164
165 xattr_header_sz = inlinexattr_header_size(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800166 if (xattr_header_sz >= vi->xattr_isize) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800167 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
Gao Xiangb17500a2018-07-26 20:21:52 +0800168 return -ENOATTR;
169 }
170
171 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
172
173 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
174 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
175
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800176 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
177 EROFS_KMAP_ATOMIC);
178 if (IS_ERR(it->kaddr))
179 return PTR_ERR(it->kaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800180 return vi->xattr_isize - xattr_header_sz;
181}
182
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800183/*
184 * Regardless of success or failure, `xattr_foreach' will end up with
185 * `ofs' pointing to the next xattr item rather than an arbitrary position.
186 */
Gao Xiangb17500a2018-07-26 20:21:52 +0800187static int xattr_foreach(struct xattr_iter *it,
Julian Merida447a3622019-03-18 20:58:41 -0300188 const struct xattr_iter_handlers *op,
189 unsigned int *tlimit)
Gao Xiangb17500a2018-07-26 20:21:52 +0800190{
191 struct erofs_xattr_entry entry;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200192 unsigned int value_sz, processed, slice;
Gao Xiangb17500a2018-07-26 20:21:52 +0800193 int err;
194
195 /* 0. fixup blkaddr, ofs, ipage */
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800196 err = xattr_iter_fixup(it);
197 if (err)
198 return err;
Gao Xiangb17500a2018-07-26 20:21:52 +0800199
200 /*
201 * 1. read xattr entry to the memory,
202 * since we do EROFS_XATTR_ALIGN
203 * therefore entry should be in the page
204 */
205 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
Bhanusree Pola561fb352019-03-22 10:38:16 +0800206 if (tlimit) {
Gao Xiangb6796ab2019-09-04 10:08:50 +0800207 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
Gao Xiangb17500a2018-07-26 20:21:52 +0800208
Gao Xiang9ddc7002019-08-13 10:30:54 +0800209 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800210 if (*tlimit < entry_sz) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800211 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800212 return -EFSCORRUPTED;
Gao Xiang9ddc7002019-08-13 10:30:54 +0800213 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800214 *tlimit -= entry_sz;
215 }
216
217 it->ofs += sizeof(struct erofs_xattr_entry);
218 value_sz = le16_to_cpu(entry.e_value_size);
219
220 /* handle entry */
221 err = op->entry(it, &entry);
222 if (err) {
223 it->ofs += entry.e_name_len + value_sz;
224 goto out;
225 }
226
227 /* 2. handle xattr name (ofs will finally be at the end of name) */
228 processed = 0;
229
230 while (processed < entry.e_name_len) {
231 if (it->ofs >= EROFS_BLKSIZ) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800232 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
Gao Xiangb17500a2018-07-26 20:21:52 +0800233
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800234 err = xattr_iter_fixup(it);
235 if (err)
236 goto out;
Gao Xiangb17500a2018-07-26 20:21:52 +0800237 it->ofs = 0;
238 }
239
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800240 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200241 entry.e_name_len - processed);
Gao Xiangb17500a2018-07-26 20:21:52 +0800242
243 /* handle name */
244 err = op->name(it, processed, it->kaddr + it->ofs, slice);
245 if (err) {
246 it->ofs += entry.e_name_len - processed + value_sz;
247 goto out;
248 }
249
250 it->ofs += slice;
251 processed += slice;
252 }
253
254 /* 3. handle xattr value */
255 processed = 0;
256
Bhanusree Pola561fb352019-03-22 10:38:16 +0800257 if (op->alloc_buffer) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800258 err = op->alloc_buffer(it, value_sz);
259 if (err) {
260 it->ofs += value_sz;
261 goto out;
262 }
263 }
264
265 while (processed < value_sz) {
266 if (it->ofs >= EROFS_BLKSIZ) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800267 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800268
269 err = xattr_iter_fixup(it);
270 if (err)
271 goto out;
Gao Xiangb17500a2018-07-26 20:21:52 +0800272 it->ofs = 0;
273 }
274
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800275 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200276 value_sz - processed);
Gao Xiangb17500a2018-07-26 20:21:52 +0800277 op->value(it, processed, it->kaddr + it->ofs, slice);
278 it->ofs += slice;
279 processed += slice;
280 }
281
282out:
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800283 /* xattrs should be 4-byte aligned (on-disk constraint) */
Gao Xiangb17500a2018-07-26 20:21:52 +0800284 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
Gao Xiang6614f762018-09-19 13:49:10 +0800285 return err < 0 ? err : 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800286}
287
288struct getxattr_iter {
289 struct xattr_iter it;
290
291 char *buffer;
292 int buffer_size, index;
293 struct qstr name;
294};
295
296static int xattr_entrymatch(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300297 struct erofs_xattr_entry *entry)
Gao Xiangb17500a2018-07-26 20:21:52 +0800298{
299 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
300
301 return (it->index != entry->e_name_index ||
302 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
303}
304
305static int xattr_namematch(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300306 unsigned int processed, char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800307{
308 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
309
310 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
311}
312
313static int xattr_checkbuffer(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300314 unsigned int value_sz)
Gao Xiangb17500a2018-07-26 20:21:52 +0800315{
316 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
317 int err = it->buffer_size < value_sz ? -ERANGE : 0;
318
319 it->buffer_size = value_sz;
Bhanusree Pola561fb352019-03-22 10:38:16 +0800320 return !it->buffer ? 1 : err;
Gao Xiangb17500a2018-07-26 20:21:52 +0800321}
322
323static void xattr_copyvalue(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300324 unsigned int processed,
325 char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800326{
327 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
328
329 memcpy(it->buffer + processed, buf, len);
330}
331
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800332static const struct xattr_iter_handlers find_xattr_handlers = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800333 .entry = xattr_entrymatch,
334 .name = xattr_namematch,
335 .alloc_buffer = xattr_checkbuffer,
336 .value = xattr_copyvalue
337};
338
339static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
340{
341 int ret;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200342 unsigned int remaining;
Gao Xiangb17500a2018-07-26 20:21:52 +0800343
344 ret = inline_xattr_iter_begin(&it->it, inode);
345 if (ret < 0)
346 return ret;
347
348 remaining = ret;
349 while (remaining) {
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300350 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
Gao Xiang6614f762018-09-19 13:49:10 +0800351 if (ret != -ENOATTR)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800352 break;
Gao Xiangb17500a2018-07-26 20:21:52 +0800353 }
Gao Xiang6614f762018-09-19 13:49:10 +0800354 return ret ? ret : it->buffer_size;
Gao Xiangb17500a2018-07-26 20:21:52 +0800355}
356
357static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
358{
Gao Xianga5876e22019-09-04 10:08:56 +0800359 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang6e789012018-08-21 22:49:30 +0800360 struct super_block *const sb = inode->i_sb;
361 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200362 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +0800363 int ret = -ENOATTR;
364
365 for (i = 0; i < vi->xattr_shared_count; ++i) {
366 erofs_blk_t blkaddr =
367 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
368
369 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800370 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
371 EROFS_KMAP_ATOMIC);
372 if (IS_ERR(it->it.kaddr))
373 return PTR_ERR(it->it.kaddr);
374 it->it.blkaddr = blkaddr;
Gao Xiangb17500a2018-07-26 20:21:52 +0800375
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300376 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
Gao Xiang6614f762018-09-19 13:49:10 +0800377 if (ret != -ENOATTR)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800378 break;
Gao Xiangb17500a2018-07-26 20:21:52 +0800379 }
Gao Xiang6614f762018-09-19 13:49:10 +0800380 return ret ? ret : it->buffer_size;
Gao Xiangb17500a2018-07-26 20:21:52 +0800381}
382
383static bool erofs_xattr_user_list(struct dentry *dentry)
384{
Gao Xiange6242462021-10-07 15:02:23 +0800385 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800386}
387
388static bool erofs_xattr_trusted_list(struct dentry *dentry)
389{
390 return capable(CAP_SYS_ADMIN);
391}
392
393int erofs_getxattr(struct inode *inode, int index,
Julian Merida447a3622019-03-18 20:58:41 -0300394 const char *name,
395 void *buffer, size_t buffer_size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800396{
397 int ret;
398 struct getxattr_iter it;
399
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800400 if (!name)
Gao Xiangb17500a2018-07-26 20:21:52 +0800401 return -EINVAL;
402
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800403 ret = init_inode_xattrs(inode);
404 if (ret)
405 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800406
407 it.index = index;
Gao Xiangb17500a2018-07-26 20:21:52 +0800408 it.name.len = strlen(name);
409 if (it.name.len > EROFS_NAME_LEN)
410 return -ERANGE;
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800411
412 it.it.buf = __EROFS_BUF_INITIALIZER;
Gao Xiangb17500a2018-07-26 20:21:52 +0800413 it.name.name = name;
414
415 it.buffer = buffer;
416 it.buffer_size = buffer_size;
417
418 it.it.sb = inode->i_sb;
419 ret = inline_getxattr(inode, &it);
420 if (ret == -ENOATTR)
421 ret = shared_getxattr(inode, &it);
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800422 erofs_put_metabuf(&it.it.buf);
Gao Xiangb17500a2018-07-26 20:21:52 +0800423 return ret;
424}
425
426static int erofs_xattr_generic_get(const struct xattr_handler *handler,
Julian Merida447a3622019-03-18 20:58:41 -0300427 struct dentry *unused, struct inode *inode,
428 const char *name, void *buffer, size_t size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800429{
Gao Xiangb17500a2018-07-26 20:21:52 +0800430 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
431
432 switch (handler->flags) {
433 case EROFS_XATTR_INDEX_USER:
Gao Xiange6242462021-10-07 15:02:23 +0800434 if (!test_opt(&sbi->opt, XATTR_USER))
Gao Xiangb17500a2018-07-26 20:21:52 +0800435 return -EOPNOTSUPP;
436 break;
437 case EROFS_XATTR_INDEX_TRUSTED:
Gao Xiangb17500a2018-07-26 20:21:52 +0800438 break;
439 case EROFS_XATTR_INDEX_SECURITY:
440 break;
441 default:
442 return -EINVAL;
443 }
444
Gao Xiangb17500a2018-07-26 20:21:52 +0800445 return erofs_getxattr(inode, handler->flags, name, buffer, size);
446}
447
448const struct xattr_handler erofs_xattr_user_handler = {
449 .prefix = XATTR_USER_PREFIX,
450 .flags = EROFS_XATTR_INDEX_USER,
451 .list = erofs_xattr_user_list,
452 .get = erofs_xattr_generic_get,
453};
454
455const struct xattr_handler erofs_xattr_trusted_handler = {
456 .prefix = XATTR_TRUSTED_PREFIX,
457 .flags = EROFS_XATTR_INDEX_TRUSTED,
458 .list = erofs_xattr_trusted_list,
459 .get = erofs_xattr_generic_get,
460};
461
462#ifdef CONFIG_EROFS_FS_SECURITY
463const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
464 .prefix = XATTR_SECURITY_PREFIX,
465 .flags = EROFS_XATTR_INDEX_SECURITY,
466 .get = erofs_xattr_generic_get,
467};
468#endif
469
Gao Xiangb17500a2018-07-26 20:21:52 +0800470const struct xattr_handler *erofs_xattr_handlers[] = {
471 &erofs_xattr_user_handler,
472#ifdef CONFIG_EROFS_FS_POSIX_ACL
473 &posix_acl_access_xattr_handler,
474 &posix_acl_default_xattr_handler,
475#endif
476 &erofs_xattr_trusted_handler,
477#ifdef CONFIG_EROFS_FS_SECURITY
478 &erofs_xattr_security_handler,
479#endif
480 NULL,
481};
Gao Xiangb17500a2018-07-26 20:21:52 +0800482
483struct listxattr_iter {
484 struct xattr_iter it;
485
486 struct dentry *dentry;
487 char *buffer;
488 int buffer_size, buffer_ofs;
489};
490
491static int xattr_entrylist(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300492 struct erofs_xattr_entry *entry)
Gao Xiangb17500a2018-07-26 20:21:52 +0800493{
494 struct listxattr_iter *it =
495 container_of(_it, struct listxattr_iter, it);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200496 unsigned int prefix_len;
Gao Xiangb17500a2018-07-26 20:21:52 +0800497 const char *prefix;
498
499 const struct xattr_handler *h =
500 erofs_xattr_handler(entry->e_name_index);
501
Bhanusree Pola561fb352019-03-22 10:38:16 +0800502 if (!h || (h->list && !h->list(it->dentry)))
Gao Xiangb17500a2018-07-26 20:21:52 +0800503 return 1;
504
Gao Xianga24df1f2019-01-29 16:35:19 +0800505 prefix = xattr_prefix(h);
Gao Xiangb17500a2018-07-26 20:21:52 +0800506 prefix_len = strlen(prefix);
507
Bhanusree Pola561fb352019-03-22 10:38:16 +0800508 if (!it->buffer) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800509 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
510 return 1;
511 }
512
513 if (it->buffer_ofs + prefix_len
514 + entry->e_name_len + 1 > it->buffer_size)
515 return -ERANGE;
516
517 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
518 it->buffer_ofs += prefix_len;
519 return 0;
520}
521
522static int xattr_namelist(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300523 unsigned int processed, char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800524{
525 struct listxattr_iter *it =
526 container_of(_it, struct listxattr_iter, it);
527
528 memcpy(it->buffer + it->buffer_ofs, buf, len);
529 it->buffer_ofs += len;
530 return 0;
531}
532
533static int xattr_skipvalue(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300534 unsigned int value_sz)
Gao Xiangb17500a2018-07-26 20:21:52 +0800535{
536 struct listxattr_iter *it =
537 container_of(_it, struct listxattr_iter, it);
538
539 it->buffer[it->buffer_ofs++] = '\0';
540 return 1;
541}
542
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800543static const struct xattr_iter_handlers list_xattr_handlers = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800544 .entry = xattr_entrylist,
545 .name = xattr_namelist,
546 .alloc_buffer = xattr_skipvalue,
547 .value = NULL
548};
549
550static int inline_listxattr(struct listxattr_iter *it)
551{
552 int ret;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200553 unsigned int remaining;
Gao Xiangb17500a2018-07-26 20:21:52 +0800554
555 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
556 if (ret < 0)
557 return ret;
558
559 remaining = ret;
560 while (remaining) {
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300561 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
Gao Xiang6614f762018-09-19 13:49:10 +0800562 if (ret)
Gao Xiangb17500a2018-07-26 20:21:52 +0800563 break;
564 }
Gao Xiang6614f762018-09-19 13:49:10 +0800565 return ret ? ret : it->buffer_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800566}
567
568static int shared_listxattr(struct listxattr_iter *it)
569{
570 struct inode *const inode = d_inode(it->dentry);
Gao Xianga5876e22019-09-04 10:08:56 +0800571 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang6e789012018-08-21 22:49:30 +0800572 struct super_block *const sb = inode->i_sb;
573 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200574 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +0800575 int ret = 0;
576
577 for (i = 0; i < vi->xattr_shared_count; ++i) {
578 erofs_blk_t blkaddr =
579 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
580
581 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800582 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
583 EROFS_KMAP_ATOMIC);
584 if (IS_ERR(it->it.kaddr))
585 return PTR_ERR(it->it.kaddr);
586 it->it.blkaddr = blkaddr;
Gao Xiangb17500a2018-07-26 20:21:52 +0800587
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300588 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
Gao Xiang6614f762018-09-19 13:49:10 +0800589 if (ret)
Gao Xiangb17500a2018-07-26 20:21:52 +0800590 break;
591 }
Gao Xiang6614f762018-09-19 13:49:10 +0800592 return ret ? ret : it->buffer_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800593}
594
595ssize_t erofs_listxattr(struct dentry *dentry,
Julian Merida447a3622019-03-18 20:58:41 -0300596 char *buffer, size_t buffer_size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800597{
598 int ret;
599 struct listxattr_iter it;
600
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800601 ret = init_inode_xattrs(d_inode(dentry));
Gao Xiang926d1652019-12-01 16:01:09 +0800602 if (ret == -ENOATTR)
603 return 0;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800604 if (ret)
605 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800606
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800607 it.it.buf = __EROFS_BUF_INITIALIZER;
Gao Xiangb17500a2018-07-26 20:21:52 +0800608 it.dentry = dentry;
609 it.buffer = buffer;
610 it.buffer_size = buffer_size;
611 it.buffer_ofs = 0;
612
613 it.it.sb = dentry->d_sb;
614
615 ret = inline_listxattr(&it);
Gao Xiangbb88e8d2022-01-02 12:00:16 +0800616 if (ret >= 0 || ret == -ENOATTR)
617 ret = shared_listxattr(&it);
618 erofs_put_metabuf(&it.it.buf);
619 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800620}
621
Gao Xiang516c115c2019-01-29 16:35:20 +0800622#ifdef CONFIG_EROFS_FS_POSIX_ACL
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200623struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
Gao Xiang516c115c2019-01-29 16:35:20 +0800624{
625 struct posix_acl *acl;
626 int prefix, rc;
627 char *value = NULL;
628
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200629 if (rcu)
630 return ERR_PTR(-ECHILD);
631
Gao Xiang516c115c2019-01-29 16:35:20 +0800632 switch (type) {
633 case ACL_TYPE_ACCESS:
634 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
635 break;
636 case ACL_TYPE_DEFAULT:
637 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
638 break;
639 default:
640 return ERR_PTR(-EINVAL);
641 }
642
643 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
644 if (rc > 0) {
645 value = kmalloc(rc, GFP_KERNEL);
646 if (!value)
647 return ERR_PTR(-ENOMEM);
648 rc = erofs_getxattr(inode, prefix, "", value, rc);
649 }
650
651 if (rc == -ENOATTR)
652 acl = NULL;
653 else if (rc < 0)
654 acl = ERR_PTR(rc);
655 else
656 acl = posix_acl_from_xattr(&init_user_ns, value, rc);
657 kfree(value);
658 return acl;
659}
660#endif