Commit | Line | Data |
---|---|---|
29b24f6c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b17500a0 | 2 | /* |
b17500a0 | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
592e7cd0 | 4 | * https://www.huawei.com/ |
bb88e8da | 5 | * Copyright (C) 2021-2022, Alibaba Cloud |
b17500a0 GX |
6 | */ |
7 | #include <linux/security.h> | |
8 | #include "xattr.h" | |
9 | ||
10 | struct xattr_iter { | |
11 | struct super_block *sb; | |
bb88e8da | 12 | struct erofs_buf buf; |
b17500a0 GX |
13 | void *kaddr; |
14 | ||
15 | erofs_blk_t blkaddr; | |
7dd68b14 | 16 | unsigned int ofs; |
b17500a0 GX |
17 | }; |
18 | ||
cadf1ccf | 19 | static int init_inode_xattrs(struct inode *inode) |
b17500a0 | 20 | { |
a5876e24 | 21 | struct erofs_inode *const vi = EROFS_I(inode); |
b17500a0 | 22 | struct xattr_iter it; |
7dd68b14 | 23 | unsigned int i; |
b17500a0 | 24 | struct erofs_xattr_ibody_header *ih; |
6e78901a | 25 | struct super_block *sb; |
b17500a0 | 26 | struct erofs_sb_info *sbi; |
62dc4597 | 27 | int ret = 0; |
b17500a0 | 28 | |
62dc4597 | 29 | /* the most case is that xattrs of this inode are initialized. */ |
ce063129 GX |
30 | 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(); | |
cadf1ccf | 36 | return 0; |
ce063129 | 37 | } |
b17500a0 | 38 | |
a5876e24 | 39 | if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE)) |
62dc4597 GX |
40 | return -ERESTARTSYS; |
41 | ||
42 | /* someone has initialized xattrs for us? */ | |
a5876e24 | 43 | if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) |
62dc4597 | 44 | goto out_unlock; |
7077fffc GX |
45 | |
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)) { | |
4f761fa2 GX |
55 | erofs_err(inode->i_sb, |
56 | "xattr_isize %d of nid %llu is not supported yet", | |
57 | vi->xattr_isize, vi->nid); | |
ff784a78 | 58 | ret = -EOPNOTSUPP; |
62dc4597 | 59 | goto out_unlock; |
7077fffc | 60 | } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) { |
8d8a09b0 | 61 | if (vi->xattr_isize) { |
4f761fa2 GX |
62 | erofs_err(inode->i_sb, |
63 | "bogus xattr ibody @ nid %llu", vi->nid); | |
7077fffc | 64 | DBG_BUGON(1); |
a6b9b1d5 | 65 | ret = -EFSCORRUPTED; |
62dc4597 | 66 | goto out_unlock; /* xattr ondisk layout error */ |
7077fffc | 67 | } |
62dc4597 GX |
68 | ret = -ENOATTR; |
69 | goto out_unlock; | |
7077fffc | 70 | } |
b17500a0 | 71 | |
6e78901a GX |
72 | sb = inode->i_sb; |
73 | sbi = EROFS_SB(sb); | |
bb88e8da | 74 | it.buf = __EROFS_BUF_INITIALIZER; |
b17500a0 GX |
75 | it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize); |
76 | it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize); | |
77 | ||
bb88e8da GX |
78 | /* 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); | |
62dc4597 GX |
82 | goto out_unlock; |
83 | } | |
b17500a0 | 84 | |
b17500a0 | 85 | ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); |
b17500a0 | 86 | vi->xattr_shared_count = ih->h_shared_count; |
cadf1ccf GX |
87 | vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count, |
88 | sizeof(uint), GFP_KERNEL); | |
561fb35a | 89 | if (!vi->xattr_shared_xattrs) { |
bb88e8da | 90 | erofs_put_metabuf(&it.buf); |
62dc4597 GX |
91 | ret = -ENOMEM; |
92 | goto out_unlock; | |
cadf1ccf | 93 | } |
b17500a0 GX |
94 | |
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) { | |
8d8a09b0 | 99 | if (it.ofs >= EROFS_BLKSIZ) { |
b17500a0 | 100 | /* cannot be unaligned */ |
9ddc7004 | 101 | DBG_BUGON(it.ofs != EROFS_BLKSIZ); |
b17500a0 | 102 | |
bb88e8da GX |
103 | it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr, |
104 | EROFS_KMAP); | |
105 | if (IS_ERR(it.kaddr)) { | |
3b1b5291 SY |
106 | kfree(vi->xattr_shared_xattrs); |
107 | vi->xattr_shared_xattrs = NULL; | |
bb88e8da | 108 | ret = PTR_ERR(it.kaddr); |
62dc4597 | 109 | goto out_unlock; |
3b1b5291 | 110 | } |
b17500a0 GX |
111 | 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 | } | |
bb88e8da | 117 | erofs_put_metabuf(&it.buf); |
b17500a0 | 118 | |
ce063129 GX |
119 | /* paired with smp_mb() at the beginning of the function. */ |
120 | smp_mb(); | |
a5876e24 | 121 | set_bit(EROFS_I_EA_INITED_BIT, &vi->flags); |
62dc4597 GX |
122 | |
123 | out_unlock: | |
a5876e24 | 124 | clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags); |
62dc4597 | 125 | return ret; |
b17500a0 GX |
126 | } |
127 | ||
bdf30cef GX |
128 | /* |
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 | */ | |
b17500a0 | 135 | struct xattr_iter_handlers { |
4b03f3f4 SY |
136 | 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); | |
b17500a0 GX |
142 | }; |
143 | ||
cadf1ccf | 144 | static inline int xattr_iter_fixup(struct xattr_iter *it) |
b17500a0 | 145 | { |
cadf1ccf GX |
146 | if (it->ofs < EROFS_BLKSIZ) |
147 | return 0; | |
148 | ||
cadf1ccf | 149 | it->blkaddr += erofs_blknr(it->ofs); |
bb88e8da GX |
150 | 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); | |
cadf1ccf GX |
154 | it->ofs = erofs_blkoff(it->ofs); |
155 | return 0; | |
b17500a0 GX |
156 | } |
157 | ||
158 | static int inline_xattr_iter_begin(struct xattr_iter *it, | |
447a3621 | 159 | struct inode *inode) |
b17500a0 | 160 | { |
a5876e24 | 161 | struct erofs_inode *const vi = EROFS_I(inode); |
b17500a0 | 162 | struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb); |
7dd68b14 | 163 | unsigned int xattr_header_sz, inline_xattr_ofs; |
b17500a0 GX |
164 | |
165 | xattr_header_sz = inlinexattr_header_size(inode); | |
8d8a09b0 | 166 | if (xattr_header_sz >= vi->xattr_isize) { |
9ddc7004 | 167 | DBG_BUGON(xattr_header_sz > vi->xattr_isize); |
b17500a0 GX |
168 | 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 | ||
bb88e8da GX |
176 | 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); | |
b17500a0 GX |
180 | return vi->xattr_isize - xattr_header_sz; |
181 | } | |
182 | ||
bdf30cef GX |
183 | /* |
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 | */ | |
b17500a0 | 187 | static int xattr_foreach(struct xattr_iter *it, |
447a3621 JM |
188 | const struct xattr_iter_handlers *op, |
189 | unsigned int *tlimit) | |
b17500a0 GX |
190 | { |
191 | struct erofs_xattr_entry entry; | |
7dd68b14 | 192 | unsigned int value_sz, processed, slice; |
b17500a0 GX |
193 | int err; |
194 | ||
195 | /* 0. fixup blkaddr, ofs, ipage */ | |
cadf1ccf GX |
196 | err = xattr_iter_fixup(it); |
197 | if (err) | |
198 | return err; | |
b17500a0 GX |
199 | |
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); | |
561fb35a | 206 | if (tlimit) { |
b6796abd | 207 | unsigned int entry_sz = erofs_xattr_entry_size(&entry); |
b17500a0 | 208 | |
9ddc7004 | 209 | /* xattr on-disk corruption: xattr entry beyond xattr_isize */ |
8d8a09b0 | 210 | if (*tlimit < entry_sz) { |
9ddc7004 | 211 | DBG_BUGON(1); |
a6b9b1d5 | 212 | return -EFSCORRUPTED; |
9ddc7004 | 213 | } |
b17500a0 GX |
214 | *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) { | |
9ddc7004 | 232 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
b17500a0 | 233 | |
cadf1ccf GX |
234 | err = xattr_iter_fixup(it); |
235 | if (err) | |
236 | goto out; | |
b17500a0 GX |
237 | it->ofs = 0; |
238 | } | |
239 | ||
bb88e8da | 240 | slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, |
7dd68b14 | 241 | entry.e_name_len - processed); |
b17500a0 GX |
242 | |
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 | ||
561fb35a | 257 | if (op->alloc_buffer) { |
b17500a0 GX |
258 | 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) { | |
9ddc7004 | 267 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
cadf1ccf GX |
268 | |
269 | err = xattr_iter_fixup(it); | |
270 | if (err) | |
271 | goto out; | |
b17500a0 GX |
272 | it->ofs = 0; |
273 | } | |
274 | ||
bb88e8da | 275 | slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, |
7dd68b14 | 276 | value_sz - processed); |
b17500a0 GX |
277 | op->value(it, processed, it->kaddr + it->ofs, slice); |
278 | it->ofs += slice; | |
279 | processed += slice; | |
280 | } | |
281 | ||
282 | out: | |
bdf30cef | 283 | /* xattrs should be 4-byte aligned (on-disk constraint) */ |
b17500a0 | 284 | it->ofs = EROFS_XATTR_ALIGN(it->ofs); |
6614f765 | 285 | return err < 0 ? err : 0; |
b17500a0 GX |
286 | } |
287 | ||
288 | struct getxattr_iter { | |
289 | struct xattr_iter it; | |
290 | ||
291 | char *buffer; | |
292 | int buffer_size, index; | |
293 | struct qstr name; | |
294 | }; | |
295 | ||
296 | static int xattr_entrymatch(struct xattr_iter *_it, | |
447a3621 | 297 | struct erofs_xattr_entry *entry) |
b17500a0 GX |
298 | { |
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 | ||
305 | static int xattr_namematch(struct xattr_iter *_it, | |
447a3621 | 306 | unsigned int processed, char *buf, unsigned int len) |
b17500a0 GX |
307 | { |
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 | ||
313 | static int xattr_checkbuffer(struct xattr_iter *_it, | |
447a3621 | 314 | unsigned int value_sz) |
b17500a0 GX |
315 | { |
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; | |
561fb35a | 320 | return !it->buffer ? 1 : err; |
b17500a0 GX |
321 | } |
322 | ||
323 | static void xattr_copyvalue(struct xattr_iter *_it, | |
447a3621 JM |
324 | unsigned int processed, |
325 | char *buf, unsigned int len) | |
b17500a0 GX |
326 | { |
327 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); | |
328 | ||
329 | memcpy(it->buffer + processed, buf, len); | |
330 | } | |
331 | ||
cadf1ccf | 332 | static const struct xattr_iter_handlers find_xattr_handlers = { |
b17500a0 GX |
333 | .entry = xattr_entrymatch, |
334 | .name = xattr_namematch, | |
335 | .alloc_buffer = xattr_checkbuffer, | |
336 | .value = xattr_copyvalue | |
337 | }; | |
338 | ||
339 | static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) | |
340 | { | |
341 | int ret; | |
7dd68b14 | 342 | unsigned int remaining; |
b17500a0 GX |
343 | |
344 | ret = inline_xattr_iter_begin(&it->it, inode); | |
345 | if (ret < 0) | |
346 | return ret; | |
347 | ||
348 | remaining = ret; | |
349 | while (remaining) { | |
2bc75964 | 350 | ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); |
6614f765 | 351 | if (ret != -ENOATTR) |
cadf1ccf | 352 | break; |
b17500a0 | 353 | } |
6614f765 | 354 | return ret ? ret : it->buffer_size; |
b17500a0 GX |
355 | } |
356 | ||
357 | static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) | |
358 | { | |
a5876e24 | 359 | struct erofs_inode *const vi = EROFS_I(inode); |
6e78901a GX |
360 | struct super_block *const sb = inode->i_sb; |
361 | struct erofs_sb_info *const sbi = EROFS_SB(sb); | |
7dd68b14 | 362 | unsigned int i; |
b17500a0 GX |
363 | 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]); | |
bb88e8da GX |
370 | 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; | |
b17500a0 | 375 | |
2bc75964 | 376 | ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); |
6614f765 | 377 | if (ret != -ENOATTR) |
cadf1ccf | 378 | break; |
b17500a0 | 379 | } |
6614f765 | 380 | return ret ? ret : it->buffer_size; |
b17500a0 GX |
381 | } |
382 | ||
383 | static bool erofs_xattr_user_list(struct dentry *dentry) | |
384 | { | |
e6242465 | 385 | return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); |
b17500a0 GX |
386 | } |
387 | ||
388 | static bool erofs_xattr_trusted_list(struct dentry *dentry) | |
389 | { | |
390 | return capable(CAP_SYS_ADMIN); | |
391 | } | |
392 | ||
393 | int erofs_getxattr(struct inode *inode, int index, | |
447a3621 JM |
394 | const char *name, |
395 | void *buffer, size_t buffer_size) | |
b17500a0 GX |
396 | { |
397 | int ret; | |
398 | struct getxattr_iter it; | |
399 | ||
8d8a09b0 | 400 | if (!name) |
b17500a0 GX |
401 | return -EINVAL; |
402 | ||
cadf1ccf GX |
403 | ret = init_inode_xattrs(inode); |
404 | if (ret) | |
405 | return ret; | |
b17500a0 GX |
406 | |
407 | it.index = index; | |
b17500a0 GX |
408 | it.name.len = strlen(name); |
409 | if (it.name.len > EROFS_NAME_LEN) | |
410 | return -ERANGE; | |
bb88e8da GX |
411 | |
412 | it.it.buf = __EROFS_BUF_INITIALIZER; | |
b17500a0 GX |
413 | 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); | |
bb88e8da | 422 | erofs_put_metabuf(&it.it.buf); |
b17500a0 GX |
423 | return ret; |
424 | } | |
425 | ||
426 | static int erofs_xattr_generic_get(const struct xattr_handler *handler, | |
447a3621 JM |
427 | struct dentry *unused, struct inode *inode, |
428 | const char *name, void *buffer, size_t size) | |
b17500a0 | 429 | { |
b17500a0 GX |
430 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
431 | ||
432 | switch (handler->flags) { | |
433 | case EROFS_XATTR_INDEX_USER: | |
e6242465 | 434 | if (!test_opt(&sbi->opt, XATTR_USER)) |
b17500a0 GX |
435 | return -EOPNOTSUPP; |
436 | break; | |
437 | case EROFS_XATTR_INDEX_TRUSTED: | |
b17500a0 GX |
438 | break; |
439 | case EROFS_XATTR_INDEX_SECURITY: | |
440 | break; | |
441 | default: | |
442 | return -EINVAL; | |
443 | } | |
444 | ||
b17500a0 GX |
445 | return erofs_getxattr(inode, handler->flags, name, buffer, size); |
446 | } | |
447 | ||
448 | const 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 | ||
455 | const 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 | |
463 | const 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 | ||
b17500a0 GX |
470 | const 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 | }; | |
b17500a0 GX |
482 | |
483 | struct listxattr_iter { | |
484 | struct xattr_iter it; | |
485 | ||
486 | struct dentry *dentry; | |
487 | char *buffer; | |
488 | int buffer_size, buffer_ofs; | |
489 | }; | |
490 | ||
491 | static int xattr_entrylist(struct xattr_iter *_it, | |
447a3621 | 492 | struct erofs_xattr_entry *entry) |
b17500a0 GX |
493 | { |
494 | struct listxattr_iter *it = | |
495 | container_of(_it, struct listxattr_iter, it); | |
7dd68b14 | 496 | unsigned int prefix_len; |
b17500a0 GX |
497 | const char *prefix; |
498 | ||
499 | const struct xattr_handler *h = | |
500 | erofs_xattr_handler(entry->e_name_index); | |
501 | ||
561fb35a | 502 | if (!h || (h->list && !h->list(it->dentry))) |
b17500a0 GX |
503 | return 1; |
504 | ||
a24df1f6 | 505 | prefix = xattr_prefix(h); |
b17500a0 GX |
506 | prefix_len = strlen(prefix); |
507 | ||
561fb35a | 508 | if (!it->buffer) { |
b17500a0 GX |
509 | 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 | ||
522 | static int xattr_namelist(struct xattr_iter *_it, | |
447a3621 | 523 | unsigned int processed, char *buf, unsigned int len) |
b17500a0 GX |
524 | { |
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 | ||
533 | static int xattr_skipvalue(struct xattr_iter *_it, | |
447a3621 | 534 | unsigned int value_sz) |
b17500a0 GX |
535 | { |
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 | ||
cadf1ccf | 543 | static const struct xattr_iter_handlers list_xattr_handlers = { |
b17500a0 GX |
544 | .entry = xattr_entrylist, |
545 | .name = xattr_namelist, | |
546 | .alloc_buffer = xattr_skipvalue, | |
547 | .value = NULL | |
548 | }; | |
549 | ||
550 | static int inline_listxattr(struct listxattr_iter *it) | |
551 | { | |
552 | int ret; | |
7dd68b14 | 553 | unsigned int remaining; |
b17500a0 GX |
554 | |
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) { | |
2bc75964 | 561 | ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); |
6614f765 | 562 | if (ret) |
b17500a0 GX |
563 | break; |
564 | } | |
6614f765 | 565 | return ret ? ret : it->buffer_ofs; |
b17500a0 GX |
566 | } |
567 | ||
568 | static int shared_listxattr(struct listxattr_iter *it) | |
569 | { | |
570 | struct inode *const inode = d_inode(it->dentry); | |
a5876e24 | 571 | struct erofs_inode *const vi = EROFS_I(inode); |
6e78901a GX |
572 | struct super_block *const sb = inode->i_sb; |
573 | struct erofs_sb_info *const sbi = EROFS_SB(sb); | |
7dd68b14 | 574 | unsigned int i; |
b17500a0 GX |
575 | 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]); | |
bb88e8da GX |
582 | 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; | |
b17500a0 | 587 | |
2bc75964 | 588 | ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL); |
6614f765 | 589 | if (ret) |
b17500a0 GX |
590 | break; |
591 | } | |
6614f765 | 592 | return ret ? ret : it->buffer_ofs; |
b17500a0 GX |
593 | } |
594 | ||
595 | ssize_t erofs_listxattr(struct dentry *dentry, | |
447a3621 | 596 | char *buffer, size_t buffer_size) |
b17500a0 GX |
597 | { |
598 | int ret; | |
599 | struct listxattr_iter it; | |
600 | ||
cadf1ccf | 601 | ret = init_inode_xattrs(d_inode(dentry)); |
926d1650 GX |
602 | if (ret == -ENOATTR) |
603 | return 0; | |
cadf1ccf GX |
604 | if (ret) |
605 | return ret; | |
b17500a0 | 606 | |
bb88e8da | 607 | it.it.buf = __EROFS_BUF_INITIALIZER; |
b17500a0 GX |
608 | 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); | |
bb88e8da GX |
616 | if (ret >= 0 || ret == -ENOATTR) |
617 | ret = shared_listxattr(&it); | |
618 | erofs_put_metabuf(&it.it.buf); | |
619 | return ret; | |
b17500a0 GX |
620 | } |
621 | ||
516c115c | 622 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
0cad6246 | 623 | struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu) |
516c115c GX |
624 | { |
625 | struct posix_acl *acl; | |
626 | int prefix, rc; | |
627 | char *value = NULL; | |
628 | ||
0cad6246 MS |
629 | if (rcu) |
630 | return ERR_PTR(-ECHILD); | |
631 | ||
516c115c GX |
632 | 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 |