blob: 778f2c52295d1ed8901651ceaf2d3e1acba48ec1 [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 Xiangb17500a2018-07-26 20:21:52 +08005 */
6#include <linux/security.h>
7#include "xattr.h"
8
9struct xattr_iter {
10 struct super_block *sb;
11 struct page *page;
12 void *kaddr;
13
14 erofs_blk_t blkaddr;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020015 unsigned int ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +080016};
17
18static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
19{
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080020 /* the only user of kunmap() is 'init_inode_xattrs' */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080021 if (!atomic)
Gao Xiangb17500a2018-07-26 20:21:52 +080022 kunmap(it->page);
23 else
24 kunmap_atomic(it->kaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080025
Gao Xiangb17500a2018-07-26 20:21:52 +080026 unlock_page(it->page);
27 put_page(it->page);
28}
29
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080030static inline void xattr_iter_end_final(struct xattr_iter *it)
31{
Bhanusree Pola561fb352019-03-22 10:38:16 +080032 if (!it->page)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080033 return;
34
35 xattr_iter_end(it, true);
36}
37
38static int init_inode_xattrs(struct inode *inode)
Gao Xiangb17500a2018-07-26 20:21:52 +080039{
Gao Xianga5876e22019-09-04 10:08:56 +080040 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangb17500a2018-07-26 20:21:52 +080041 struct xattr_iter it;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020042 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +080043 struct erofs_xattr_ibody_header *ih;
Gao Xiang6e789012018-08-21 22:49:30 +080044 struct super_block *sb;
Gao Xiangb17500a2018-07-26 20:21:52 +080045 struct erofs_sb_info *sbi;
Gao Xiangb17500a2018-07-26 20:21:52 +080046 bool atomic_map;
Gao Xiang62dc4592019-02-18 15:19:04 +080047 int ret = 0;
Gao Xiangb17500a2018-07-26 20:21:52 +080048
Gao Xiang62dc4592019-02-18 15:19:04 +080049 /* the most case is that xattrs of this inode are initialized. */
Gao Xiangce0631292021-02-09 21:06:18 +080050 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
51 /*
52 * paired with smp_mb() at the end of the function to ensure
53 * fields will only be observed after the bit is set.
54 */
55 smp_mb();
Gao Xiangcadf1ccf2018-08-21 22:49:31 +080056 return 0;
Gao Xiangce0631292021-02-09 21:06:18 +080057 }
Gao Xiangb17500a2018-07-26 20:21:52 +080058
Gao Xianga5876e22019-09-04 10:08:56 +080059 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
Gao Xiang62dc4592019-02-18 15:19:04 +080060 return -ERESTARTSYS;
61
62 /* someone has initialized xattrs for us? */
Gao Xianga5876e22019-09-04 10:08:56 +080063 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
Gao Xiang62dc4592019-02-18 15:19:04 +080064 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080065
66 /*
67 * bypass all xattr operations if ->xattr_isize is not greater than
68 * sizeof(struct erofs_xattr_ibody_header), in detail:
69 * 1) it is not enough to contain erofs_xattr_ibody_header then
70 * ->xattr_isize should be 0 (it means no xattr);
71 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
72 * undefined right now (maybe use later with some new sb feature).
73 */
74 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080075 erofs_err(inode->i_sb,
76 "xattr_isize %d of nid %llu is not supported yet",
77 vi->xattr_isize, vi->nid);
Gao Xiangff784a72019-08-14 18:37:05 +080078 ret = -EOPNOTSUPP;
Gao Xiang62dc4592019-02-18 15:19:04 +080079 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080080 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +080081 if (vi->xattr_isize) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080082 erofs_err(inode->i_sb,
83 "bogus xattr ibody @ nid %llu", vi->nid);
Gao Xiang7077fff2019-01-14 19:40:23 +080084 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +080085 ret = -EFSCORRUPTED;
Gao Xiang62dc4592019-02-18 15:19:04 +080086 goto out_unlock; /* xattr ondisk layout error */
Gao Xiang7077fff2019-01-14 19:40:23 +080087 }
Gao Xiang62dc4592019-02-18 15:19:04 +080088 ret = -ENOATTR;
89 goto out_unlock;
Gao Xiang7077fff2019-01-14 19:40:23 +080090 }
Gao Xiangb17500a2018-07-26 20:21:52 +080091
Gao Xiang6e789012018-08-21 22:49:30 +080092 sb = inode->i_sb;
93 sbi = EROFS_SB(sb);
Gao Xiangb17500a2018-07-26 20:21:52 +080094 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
95 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
96
Gao Xiange655b5b2019-09-04 10:09:03 +080097 it.page = erofs_get_meta_page(sb, it.blkaddr);
Gao Xiang62dc4592019-02-18 15:19:04 +080098 if (IS_ERR(it.page)) {
99 ret = PTR_ERR(it.page);
100 goto out_unlock;
101 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800102
103 /* read in shared xattr array (non-atomic, see kmalloc below) */
104 it.kaddr = kmap(it.page);
105 atomic_map = false;
106
107 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
108
109 vi->xattr_shared_count = ih->h_shared_count;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800110 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
111 sizeof(uint), GFP_KERNEL);
Bhanusree Pola561fb352019-03-22 10:38:16 +0800112 if (!vi->xattr_shared_xattrs) {
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800113 xattr_iter_end(&it, atomic_map);
Gao Xiang62dc4592019-02-18 15:19:04 +0800114 ret = -ENOMEM;
115 goto out_unlock;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800116 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800117
118 /* let's skip ibody header */
119 it.ofs += sizeof(struct erofs_xattr_ibody_header);
120
121 for (i = 0; i < vi->xattr_shared_count; ++i) {
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800122 if (it.ofs >= EROFS_BLKSIZ) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800123 /* cannot be unaligned */
Gao Xiang9ddc7002019-08-13 10:30:54 +0800124 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
Gao Xiangb17500a2018-07-26 20:21:52 +0800125 xattr_iter_end(&it, atomic_map);
126
Gao Xiange655b5b2019-09-04 10:09:03 +0800127 it.page = erofs_get_meta_page(sb, ++it.blkaddr);
Sheng Yong3b1b5292019-02-14 14:46:36 +0800128 if (IS_ERR(it.page)) {
129 kfree(vi->xattr_shared_xattrs);
130 vi->xattr_shared_xattrs = NULL;
Gao Xiang62dc4592019-02-18 15:19:04 +0800131 ret = PTR_ERR(it.page);
132 goto out_unlock;
Sheng Yong3b1b5292019-02-14 14:46:36 +0800133 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800134
135 it.kaddr = kmap_atomic(it.page);
136 atomic_map = true;
137 it.ofs = 0;
138 }
139 vi->xattr_shared_xattrs[i] =
140 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
141 it.ofs += sizeof(__le32);
142 }
143 xattr_iter_end(&it, atomic_map);
144
Gao Xiangce0631292021-02-09 21:06:18 +0800145 /* paired with smp_mb() at the beginning of the function. */
146 smp_mb();
Gao Xianga5876e22019-09-04 10:08:56 +0800147 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
Gao Xiang62dc4592019-02-18 15:19:04 +0800148
149out_unlock:
Gao Xianga5876e22019-09-04 10:08:56 +0800150 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
Gao Xiang62dc4592019-02-18 15:19:04 +0800151 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800152}
153
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800154/*
155 * the general idea for these return values is
156 * if 0 is returned, go on processing the current xattr;
157 * 1 (> 0) is returned, skip this round to process the next xattr;
158 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
159 * and need to be handled
160 */
Gao Xiangb17500a2018-07-26 20:21:52 +0800161struct xattr_iter_handlers {
Sidong Yang4b03f3f2019-01-08 13:24:54 +0000162 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
163 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
164 unsigned int len);
165 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
166 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
167 unsigned int len);
Gao Xiangb17500a2018-07-26 20:21:52 +0800168};
169
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800170static inline int xattr_iter_fixup(struct xattr_iter *it)
Gao Xiangb17500a2018-07-26 20:21:52 +0800171{
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800172 if (it->ofs < EROFS_BLKSIZ)
173 return 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800174
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800175 xattr_iter_end(it, true);
Gao Xiangb17500a2018-07-26 20:21:52 +0800176
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800177 it->blkaddr += erofs_blknr(it->ofs);
178
Gao Xiange655b5b2019-09-04 10:09:03 +0800179 it->page = erofs_get_meta_page(it->sb, it->blkaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800180 if (IS_ERR(it->page)) {
181 int err = PTR_ERR(it->page);
182
183 it->page = NULL;
184 return err;
Gao Xiangb17500a2018-07-26 20:21:52 +0800185 }
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800186
187 it->kaddr = kmap_atomic(it->page);
188 it->ofs = erofs_blkoff(it->ofs);
189 return 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800190}
191
192static int inline_xattr_iter_begin(struct xattr_iter *it,
Julian Merida447a3622019-03-18 20:58:41 -0300193 struct inode *inode)
Gao Xiangb17500a2018-07-26 20:21:52 +0800194{
Gao Xianga5876e22019-09-04 10:08:56 +0800195 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiangb17500a2018-07-26 20:21:52 +0800196 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200197 unsigned int xattr_header_sz, inline_xattr_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800198
199 xattr_header_sz = inlinexattr_header_size(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800200 if (xattr_header_sz >= vi->xattr_isize) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800201 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
Gao Xiangb17500a2018-07-26 20:21:52 +0800202 return -ENOATTR;
203 }
204
205 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
206
207 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
208 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
209
Gao Xiange655b5b2019-09-04 10:09:03 +0800210 it->page = erofs_get_meta_page(inode->i_sb, it->blkaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800211 if (IS_ERR(it->page))
212 return PTR_ERR(it->page);
Gao Xiangb17500a2018-07-26 20:21:52 +0800213
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800214 it->kaddr = kmap_atomic(it->page);
Gao Xiangb17500a2018-07-26 20:21:52 +0800215 return vi->xattr_isize - xattr_header_sz;
216}
217
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800218/*
219 * Regardless of success or failure, `xattr_foreach' will end up with
220 * `ofs' pointing to the next xattr item rather than an arbitrary position.
221 */
Gao Xiangb17500a2018-07-26 20:21:52 +0800222static int xattr_foreach(struct xattr_iter *it,
Julian Merida447a3622019-03-18 20:58:41 -0300223 const struct xattr_iter_handlers *op,
224 unsigned int *tlimit)
Gao Xiangb17500a2018-07-26 20:21:52 +0800225{
226 struct erofs_xattr_entry entry;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200227 unsigned int value_sz, processed, slice;
Gao Xiangb17500a2018-07-26 20:21:52 +0800228 int err;
229
230 /* 0. fixup blkaddr, ofs, ipage */
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800231 err = xattr_iter_fixup(it);
232 if (err)
233 return err;
Gao Xiangb17500a2018-07-26 20:21:52 +0800234
235 /*
236 * 1. read xattr entry to the memory,
237 * since we do EROFS_XATTR_ALIGN
238 * therefore entry should be in the page
239 */
240 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
Bhanusree Pola561fb352019-03-22 10:38:16 +0800241 if (tlimit) {
Gao Xiangb6796ab2019-09-04 10:08:50 +0800242 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
Gao Xiangb17500a2018-07-26 20:21:52 +0800243
Gao Xiang9ddc7002019-08-13 10:30:54 +0800244 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800245 if (*tlimit < entry_sz) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800246 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800247 return -EFSCORRUPTED;
Gao Xiang9ddc7002019-08-13 10:30:54 +0800248 }
Gao Xiangb17500a2018-07-26 20:21:52 +0800249 *tlimit -= entry_sz;
250 }
251
252 it->ofs += sizeof(struct erofs_xattr_entry);
253 value_sz = le16_to_cpu(entry.e_value_size);
254
255 /* handle entry */
256 err = op->entry(it, &entry);
257 if (err) {
258 it->ofs += entry.e_name_len + value_sz;
259 goto out;
260 }
261
262 /* 2. handle xattr name (ofs will finally be at the end of name) */
263 processed = 0;
264
265 while (processed < entry.e_name_len) {
266 if (it->ofs >= EROFS_BLKSIZ) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800267 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
Gao Xiangb17500a2018-07-26 20:21:52 +0800268
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800269 err = xattr_iter_fixup(it);
270 if (err)
271 goto out;
Gao Xiangb17500a2018-07-26 20:21:52 +0800272 it->ofs = 0;
273 }
274
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200275 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
276 entry.e_name_len - processed);
Gao Xiangb17500a2018-07-26 20:21:52 +0800277
278 /* handle name */
279 err = op->name(it, processed, it->kaddr + it->ofs, slice);
280 if (err) {
281 it->ofs += entry.e_name_len - processed + value_sz;
282 goto out;
283 }
284
285 it->ofs += slice;
286 processed += slice;
287 }
288
289 /* 3. handle xattr value */
290 processed = 0;
291
Bhanusree Pola561fb352019-03-22 10:38:16 +0800292 if (op->alloc_buffer) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800293 err = op->alloc_buffer(it, value_sz);
294 if (err) {
295 it->ofs += value_sz;
296 goto out;
297 }
298 }
299
300 while (processed < value_sz) {
301 if (it->ofs >= EROFS_BLKSIZ) {
Gao Xiang9ddc7002019-08-13 10:30:54 +0800302 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800303
304 err = xattr_iter_fixup(it);
305 if (err)
306 goto out;
Gao Xiangb17500a2018-07-26 20:21:52 +0800307 it->ofs = 0;
308 }
309
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200310 slice = min_t(unsigned int, PAGE_SIZE - it->ofs,
311 value_sz - processed);
Gao Xiangb17500a2018-07-26 20:21:52 +0800312 op->value(it, processed, it->kaddr + it->ofs, slice);
313 it->ofs += slice;
314 processed += slice;
315 }
316
317out:
Gao Xiangbdf30ce2018-09-19 13:49:09 +0800318 /* xattrs should be 4-byte aligned (on-disk constraint) */
Gao Xiangb17500a2018-07-26 20:21:52 +0800319 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
Gao Xiang6614f762018-09-19 13:49:10 +0800320 return err < 0 ? err : 0;
Gao Xiangb17500a2018-07-26 20:21:52 +0800321}
322
323struct getxattr_iter {
324 struct xattr_iter it;
325
326 char *buffer;
327 int buffer_size, index;
328 struct qstr name;
329};
330
331static int xattr_entrymatch(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300332 struct erofs_xattr_entry *entry)
Gao Xiangb17500a2018-07-26 20:21:52 +0800333{
334 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
335
336 return (it->index != entry->e_name_index ||
337 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
338}
339
340static int xattr_namematch(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300341 unsigned int processed, char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800342{
343 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
344
345 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
346}
347
348static int xattr_checkbuffer(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300349 unsigned int value_sz)
Gao Xiangb17500a2018-07-26 20:21:52 +0800350{
351 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
352 int err = it->buffer_size < value_sz ? -ERANGE : 0;
353
354 it->buffer_size = value_sz;
Bhanusree Pola561fb352019-03-22 10:38:16 +0800355 return !it->buffer ? 1 : err;
Gao Xiangb17500a2018-07-26 20:21:52 +0800356}
357
358static void xattr_copyvalue(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300359 unsigned int processed,
360 char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800361{
362 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
363
364 memcpy(it->buffer + processed, buf, len);
365}
366
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800367static const struct xattr_iter_handlers find_xattr_handlers = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800368 .entry = xattr_entrymatch,
369 .name = xattr_namematch,
370 .alloc_buffer = xattr_checkbuffer,
371 .value = xattr_copyvalue
372};
373
374static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
375{
376 int ret;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200377 unsigned int remaining;
Gao Xiangb17500a2018-07-26 20:21:52 +0800378
379 ret = inline_xattr_iter_begin(&it->it, inode);
380 if (ret < 0)
381 return ret;
382
383 remaining = ret;
384 while (remaining) {
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300385 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
Gao Xiang6614f762018-09-19 13:49:10 +0800386 if (ret != -ENOATTR)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800387 break;
Gao Xiangb17500a2018-07-26 20:21:52 +0800388 }
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800389 xattr_iter_end_final(&it->it);
Gao Xiangb17500a2018-07-26 20:21:52 +0800390
Gao Xiang6614f762018-09-19 13:49:10 +0800391 return ret ? ret : it->buffer_size;
Gao Xiangb17500a2018-07-26 20:21:52 +0800392}
393
394static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
395{
Gao Xianga5876e22019-09-04 10:08:56 +0800396 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang6e789012018-08-21 22:49:30 +0800397 struct super_block *const sb = inode->i_sb;
398 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200399 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +0800400 int ret = -ENOATTR;
401
402 for (i = 0; i < vi->xattr_shared_count; ++i) {
403 erofs_blk_t blkaddr =
404 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
405
406 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
407
408 if (!i || blkaddr != it->it.blkaddr) {
409 if (i)
410 xattr_iter_end(&it->it, true);
411
Gao Xiange655b5b2019-09-04 10:09:03 +0800412 it->it.page = erofs_get_meta_page(sb, blkaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800413 if (IS_ERR(it->it.page))
414 return PTR_ERR(it->it.page);
415
Gao Xiangb17500a2018-07-26 20:21:52 +0800416 it->it.kaddr = kmap_atomic(it->it.page);
417 it->it.blkaddr = blkaddr;
418 }
419
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300420 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
Gao Xiang6614f762018-09-19 13:49:10 +0800421 if (ret != -ENOATTR)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800422 break;
Gao Xiangb17500a2018-07-26 20:21:52 +0800423 }
424 if (vi->xattr_shared_count)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800425 xattr_iter_end_final(&it->it);
Gao Xiangb17500a2018-07-26 20:21:52 +0800426
Gao Xiang6614f762018-09-19 13:49:10 +0800427 return ret ? ret : it->buffer_size;
Gao Xiangb17500a2018-07-26 20:21:52 +0800428}
429
430static bool erofs_xattr_user_list(struct dentry *dentry)
431{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800432 return test_opt(&EROFS_SB(dentry->d_sb)->ctx, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800433}
434
435static bool erofs_xattr_trusted_list(struct dentry *dentry)
436{
437 return capable(CAP_SYS_ADMIN);
438}
439
440int erofs_getxattr(struct inode *inode, int index,
Julian Merida447a3622019-03-18 20:58:41 -0300441 const char *name,
442 void *buffer, size_t buffer_size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800443{
444 int ret;
445 struct getxattr_iter it;
446
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800447 if (!name)
Gao Xiangb17500a2018-07-26 20:21:52 +0800448 return -EINVAL;
449
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800450 ret = init_inode_xattrs(inode);
451 if (ret)
452 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800453
454 it.index = index;
455
456 it.name.len = strlen(name);
457 if (it.name.len > EROFS_NAME_LEN)
458 return -ERANGE;
459 it.name.name = name;
460
461 it.buffer = buffer;
462 it.buffer_size = buffer_size;
463
464 it.it.sb = inode->i_sb;
465 ret = inline_getxattr(inode, &it);
466 if (ret == -ENOATTR)
467 ret = shared_getxattr(inode, &it);
468 return ret;
469}
470
471static int erofs_xattr_generic_get(const struct xattr_handler *handler,
Julian Merida447a3622019-03-18 20:58:41 -0300472 struct dentry *unused, struct inode *inode,
473 const char *name, void *buffer, size_t size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800474{
Gao Xiangb17500a2018-07-26 20:21:52 +0800475 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
476
477 switch (handler->flags) {
478 case EROFS_XATTR_INDEX_USER:
Chao Yuf57a3fe2020-05-29 18:48:36 +0800479 if (!test_opt(&sbi->ctx, XATTR_USER))
Gao Xiangb17500a2018-07-26 20:21:52 +0800480 return -EOPNOTSUPP;
481 break;
482 case EROFS_XATTR_INDEX_TRUSTED:
Gao Xiangb17500a2018-07-26 20:21:52 +0800483 break;
484 case EROFS_XATTR_INDEX_SECURITY:
485 break;
486 default:
487 return -EINVAL;
488 }
489
Gao Xiangb17500a2018-07-26 20:21:52 +0800490 return erofs_getxattr(inode, handler->flags, name, buffer, size);
491}
492
493const struct xattr_handler erofs_xattr_user_handler = {
494 .prefix = XATTR_USER_PREFIX,
495 .flags = EROFS_XATTR_INDEX_USER,
496 .list = erofs_xattr_user_list,
497 .get = erofs_xattr_generic_get,
498};
499
500const struct xattr_handler erofs_xattr_trusted_handler = {
501 .prefix = XATTR_TRUSTED_PREFIX,
502 .flags = EROFS_XATTR_INDEX_TRUSTED,
503 .list = erofs_xattr_trusted_list,
504 .get = erofs_xattr_generic_get,
505};
506
507#ifdef CONFIG_EROFS_FS_SECURITY
508const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
509 .prefix = XATTR_SECURITY_PREFIX,
510 .flags = EROFS_XATTR_INDEX_SECURITY,
511 .get = erofs_xattr_generic_get,
512};
513#endif
514
Gao Xiangb17500a2018-07-26 20:21:52 +0800515const struct xattr_handler *erofs_xattr_handlers[] = {
516 &erofs_xattr_user_handler,
517#ifdef CONFIG_EROFS_FS_POSIX_ACL
518 &posix_acl_access_xattr_handler,
519 &posix_acl_default_xattr_handler,
520#endif
521 &erofs_xattr_trusted_handler,
522#ifdef CONFIG_EROFS_FS_SECURITY
523 &erofs_xattr_security_handler,
524#endif
525 NULL,
526};
Gao Xiangb17500a2018-07-26 20:21:52 +0800527
528struct listxattr_iter {
529 struct xattr_iter it;
530
531 struct dentry *dentry;
532 char *buffer;
533 int buffer_size, buffer_ofs;
534};
535
536static int xattr_entrylist(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300537 struct erofs_xattr_entry *entry)
Gao Xiangb17500a2018-07-26 20:21:52 +0800538{
539 struct listxattr_iter *it =
540 container_of(_it, struct listxattr_iter, it);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200541 unsigned int prefix_len;
Gao Xiangb17500a2018-07-26 20:21:52 +0800542 const char *prefix;
543
544 const struct xattr_handler *h =
545 erofs_xattr_handler(entry->e_name_index);
546
Bhanusree Pola561fb352019-03-22 10:38:16 +0800547 if (!h || (h->list && !h->list(it->dentry)))
Gao Xiangb17500a2018-07-26 20:21:52 +0800548 return 1;
549
Gao Xianga24df1f2019-01-29 16:35:19 +0800550 prefix = xattr_prefix(h);
Gao Xiangb17500a2018-07-26 20:21:52 +0800551 prefix_len = strlen(prefix);
552
Bhanusree Pola561fb352019-03-22 10:38:16 +0800553 if (!it->buffer) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800554 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
555 return 1;
556 }
557
558 if (it->buffer_ofs + prefix_len
559 + entry->e_name_len + 1 > it->buffer_size)
560 return -ERANGE;
561
562 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
563 it->buffer_ofs += prefix_len;
564 return 0;
565}
566
567static int xattr_namelist(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300568 unsigned int processed, char *buf, unsigned int len)
Gao Xiangb17500a2018-07-26 20:21:52 +0800569{
570 struct listxattr_iter *it =
571 container_of(_it, struct listxattr_iter, it);
572
573 memcpy(it->buffer + it->buffer_ofs, buf, len);
574 it->buffer_ofs += len;
575 return 0;
576}
577
578static int xattr_skipvalue(struct xattr_iter *_it,
Julian Merida447a3622019-03-18 20:58:41 -0300579 unsigned int value_sz)
Gao Xiangb17500a2018-07-26 20:21:52 +0800580{
581 struct listxattr_iter *it =
582 container_of(_it, struct listxattr_iter, it);
583
584 it->buffer[it->buffer_ofs++] = '\0';
585 return 1;
586}
587
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800588static const struct xattr_iter_handlers list_xattr_handlers = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800589 .entry = xattr_entrylist,
590 .name = xattr_namelist,
591 .alloc_buffer = xattr_skipvalue,
592 .value = NULL
593};
594
595static int inline_listxattr(struct listxattr_iter *it)
596{
597 int ret;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200598 unsigned int remaining;
Gao Xiangb17500a2018-07-26 20:21:52 +0800599
600 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
601 if (ret < 0)
602 return ret;
603
604 remaining = ret;
605 while (remaining) {
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300606 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
Gao Xiang6614f762018-09-19 13:49:10 +0800607 if (ret)
Gao Xiangb17500a2018-07-26 20:21:52 +0800608 break;
609 }
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800610 xattr_iter_end_final(&it->it);
Gao Xiang6614f762018-09-19 13:49:10 +0800611 return ret ? ret : it->buffer_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800612}
613
614static int shared_listxattr(struct listxattr_iter *it)
615{
616 struct inode *const inode = d_inode(it->dentry);
Gao Xianga5876e22019-09-04 10:08:56 +0800617 struct erofs_inode *const vi = EROFS_I(inode);
Gao Xiang6e789012018-08-21 22:49:30 +0800618 struct super_block *const sb = inode->i_sb;
619 struct erofs_sb_info *const sbi = EROFS_SB(sb);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200620 unsigned int i;
Gao Xiangb17500a2018-07-26 20:21:52 +0800621 int ret = 0;
622
623 for (i = 0; i < vi->xattr_shared_count; ++i) {
624 erofs_blk_t blkaddr =
625 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
626
627 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
628 if (!i || blkaddr != it->it.blkaddr) {
629 if (i)
630 xattr_iter_end(&it->it, true);
631
Gao Xiange655b5b2019-09-04 10:09:03 +0800632 it->it.page = erofs_get_meta_page(sb, blkaddr);
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800633 if (IS_ERR(it->it.page))
634 return PTR_ERR(it->it.page);
635
Gao Xiangb17500a2018-07-26 20:21:52 +0800636 it->it.kaddr = kmap_atomic(it->it.page);
637 it->it.blkaddr = blkaddr;
638 }
639
Kristaps Čivkulis2bc75962018-08-05 18:21:01 +0300640 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
Gao Xiang6614f762018-09-19 13:49:10 +0800641 if (ret)
Gao Xiangb17500a2018-07-26 20:21:52 +0800642 break;
643 }
644 if (vi->xattr_shared_count)
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800645 xattr_iter_end_final(&it->it);
Gao Xiangb17500a2018-07-26 20:21:52 +0800646
Gao Xiang6614f762018-09-19 13:49:10 +0800647 return ret ? ret : it->buffer_ofs;
Gao Xiangb17500a2018-07-26 20:21:52 +0800648}
649
650ssize_t erofs_listxattr(struct dentry *dentry,
Julian Merida447a3622019-03-18 20:58:41 -0300651 char *buffer, size_t buffer_size)
Gao Xiangb17500a2018-07-26 20:21:52 +0800652{
653 int ret;
654 struct listxattr_iter it;
655
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800656 ret = init_inode_xattrs(d_inode(dentry));
Gao Xiang926d1652019-12-01 16:01:09 +0800657 if (ret == -ENOATTR)
658 return 0;
Gao Xiangcadf1ccf2018-08-21 22:49:31 +0800659 if (ret)
660 return ret;
Gao Xiangb17500a2018-07-26 20:21:52 +0800661
662 it.dentry = dentry;
663 it.buffer = buffer;
664 it.buffer_size = buffer_size;
665 it.buffer_ofs = 0;
666
667 it.it.sb = dentry->d_sb;
668
669 ret = inline_listxattr(&it);
670 if (ret < 0 && ret != -ENOATTR)
671 return ret;
672 return shared_listxattr(&it);
673}
674
Gao Xiang516c115c2019-01-29 16:35:20 +0800675#ifdef CONFIG_EROFS_FS_POSIX_ACL
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200676struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
Gao Xiang516c115c2019-01-29 16:35:20 +0800677{
678 struct posix_acl *acl;
679 int prefix, rc;
680 char *value = NULL;
681
Miklos Szeredi0cad6242021-08-18 22:08:24 +0200682 if (rcu)
683 return ERR_PTR(-ECHILD);
684
Gao Xiang516c115c2019-01-29 16:35:20 +0800685 switch (type) {
686 case ACL_TYPE_ACCESS:
687 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
688 break;
689 case ACL_TYPE_DEFAULT:
690 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
691 break;
692 default:
693 return ERR_PTR(-EINVAL);
694 }
695
696 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
697 if (rc > 0) {
698 value = kmalloc(rc, GFP_KERNEL);
699 if (!value)
700 return ERR_PTR(-ENOMEM);
701 rc = erofs_getxattr(inode, prefix, "", value, rc);
702 }
703
704 if (rc == -ENOATTR)
705 acl = NULL;
706 else if (rc < 0)
707 acl = ERR_PTR(rc);
708 else
709 acl = posix_acl_from_xattr(&init_user_ns, value, rc);
710 kfree(value);
711 return acl;
712}
713#endif