]>
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/ |
b17500a0 | 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
b17500a0 GX |
6 | */ |
7 | #include <linux/security.h> | |
8 | #include "xattr.h" | |
9 | ||
10 | struct xattr_iter { | |
11 | struct super_block *sb; | |
12 | struct page *page; | |
13 | void *kaddr; | |
14 | ||
15 | erofs_blk_t blkaddr; | |
7dd68b14 | 16 | unsigned int ofs; |
b17500a0 GX |
17 | }; |
18 | ||
19 | static inline void xattr_iter_end(struct xattr_iter *it, bool atomic) | |
20 | { | |
cadf1ccf | 21 | /* the only user of kunmap() is 'init_inode_xattrs' */ |
8d8a09b0 | 22 | if (!atomic) |
b17500a0 GX |
23 | kunmap(it->page); |
24 | else | |
25 | kunmap_atomic(it->kaddr); | |
cadf1ccf | 26 | |
b17500a0 GX |
27 | unlock_page(it->page); |
28 | put_page(it->page); | |
29 | } | |
30 | ||
cadf1ccf GX |
31 | static inline void xattr_iter_end_final(struct xattr_iter *it) |
32 | { | |
561fb35a | 33 | if (!it->page) |
cadf1ccf GX |
34 | return; |
35 | ||
36 | xattr_iter_end(it, true); | |
37 | } | |
38 | ||
39 | static int init_inode_xattrs(struct inode *inode) | |
b17500a0 | 40 | { |
a5876e24 | 41 | struct erofs_inode *const vi = EROFS_I(inode); |
b17500a0 | 42 | struct xattr_iter it; |
7dd68b14 | 43 | unsigned int i; |
b17500a0 | 44 | struct erofs_xattr_ibody_header *ih; |
6e78901a | 45 | struct super_block *sb; |
b17500a0 | 46 | struct erofs_sb_info *sbi; |
b17500a0 | 47 | bool atomic_map; |
62dc4597 | 48 | int ret = 0; |
b17500a0 | 49 | |
62dc4597 | 50 | /* the most case is that xattrs of this inode are initialized. */ |
a5876e24 | 51 | if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) |
cadf1ccf | 52 | return 0; |
b17500a0 | 53 | |
a5876e24 | 54 | if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE)) |
62dc4597 GX |
55 | return -ERESTARTSYS; |
56 | ||
57 | /* someone has initialized xattrs for us? */ | |
a5876e24 | 58 | if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) |
62dc4597 | 59 | goto out_unlock; |
7077fffc GX |
60 | |
61 | /* | |
62 | * bypass all xattr operations if ->xattr_isize is not greater than | |
63 | * sizeof(struct erofs_xattr_ibody_header), in detail: | |
64 | * 1) it is not enough to contain erofs_xattr_ibody_header then | |
65 | * ->xattr_isize should be 0 (it means no xattr); | |
66 | * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk | |
67 | * undefined right now (maybe use later with some new sb feature). | |
68 | */ | |
69 | if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) { | |
4f761fa2 GX |
70 | erofs_err(inode->i_sb, |
71 | "xattr_isize %d of nid %llu is not supported yet", | |
72 | vi->xattr_isize, vi->nid); | |
ff784a78 | 73 | ret = -EOPNOTSUPP; |
62dc4597 | 74 | goto out_unlock; |
7077fffc | 75 | } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) { |
8d8a09b0 | 76 | if (vi->xattr_isize) { |
4f761fa2 GX |
77 | erofs_err(inode->i_sb, |
78 | "bogus xattr ibody @ nid %llu", vi->nid); | |
7077fffc | 79 | DBG_BUGON(1); |
a6b9b1d5 | 80 | ret = -EFSCORRUPTED; |
62dc4597 | 81 | goto out_unlock; /* xattr ondisk layout error */ |
7077fffc | 82 | } |
62dc4597 GX |
83 | ret = -ENOATTR; |
84 | goto out_unlock; | |
7077fffc | 85 | } |
b17500a0 | 86 | |
6e78901a GX |
87 | sb = inode->i_sb; |
88 | sbi = EROFS_SB(sb); | |
b17500a0 GX |
89 | it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize); |
90 | it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize); | |
91 | ||
e655b5b3 | 92 | it.page = erofs_get_meta_page(sb, it.blkaddr); |
62dc4597 GX |
93 | if (IS_ERR(it.page)) { |
94 | ret = PTR_ERR(it.page); | |
95 | goto out_unlock; | |
96 | } | |
b17500a0 GX |
97 | |
98 | /* read in shared xattr array (non-atomic, see kmalloc below) */ | |
99 | it.kaddr = kmap(it.page); | |
100 | atomic_map = false; | |
101 | ||
102 | ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); | |
103 | ||
104 | vi->xattr_shared_count = ih->h_shared_count; | |
cadf1ccf GX |
105 | vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count, |
106 | sizeof(uint), GFP_KERNEL); | |
561fb35a | 107 | if (!vi->xattr_shared_xattrs) { |
cadf1ccf | 108 | xattr_iter_end(&it, atomic_map); |
62dc4597 GX |
109 | ret = -ENOMEM; |
110 | goto out_unlock; | |
cadf1ccf | 111 | } |
b17500a0 GX |
112 | |
113 | /* let's skip ibody header */ | |
114 | it.ofs += sizeof(struct erofs_xattr_ibody_header); | |
115 | ||
116 | for (i = 0; i < vi->xattr_shared_count; ++i) { | |
8d8a09b0 | 117 | if (it.ofs >= EROFS_BLKSIZ) { |
b17500a0 | 118 | /* cannot be unaligned */ |
9ddc7004 | 119 | DBG_BUGON(it.ofs != EROFS_BLKSIZ); |
b17500a0 GX |
120 | xattr_iter_end(&it, atomic_map); |
121 | ||
e655b5b3 | 122 | it.page = erofs_get_meta_page(sb, ++it.blkaddr); |
3b1b5291 SY |
123 | if (IS_ERR(it.page)) { |
124 | kfree(vi->xattr_shared_xattrs); | |
125 | vi->xattr_shared_xattrs = NULL; | |
62dc4597 GX |
126 | ret = PTR_ERR(it.page); |
127 | goto out_unlock; | |
3b1b5291 | 128 | } |
b17500a0 GX |
129 | |
130 | it.kaddr = kmap_atomic(it.page); | |
131 | atomic_map = true; | |
132 | it.ofs = 0; | |
133 | } | |
134 | vi->xattr_shared_xattrs[i] = | |
135 | le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs)); | |
136 | it.ofs += sizeof(__le32); | |
137 | } | |
138 | xattr_iter_end(&it, atomic_map); | |
139 | ||
a5876e24 | 140 | set_bit(EROFS_I_EA_INITED_BIT, &vi->flags); |
62dc4597 GX |
141 | |
142 | out_unlock: | |
a5876e24 | 143 | clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags); |
62dc4597 | 144 | return ret; |
b17500a0 GX |
145 | } |
146 | ||
bdf30cef GX |
147 | /* |
148 | * the general idea for these return values is | |
149 | * if 0 is returned, go on processing the current xattr; | |
150 | * 1 (> 0) is returned, skip this round to process the next xattr; | |
151 | * -err (< 0) is returned, an error (maybe ENOXATTR) occurred | |
152 | * and need to be handled | |
153 | */ | |
b17500a0 | 154 | struct xattr_iter_handlers { |
4b03f3f4 SY |
155 | int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry); |
156 | int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf, | |
157 | unsigned int len); | |
158 | int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz); | |
159 | void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf, | |
160 | unsigned int len); | |
b17500a0 GX |
161 | }; |
162 | ||
cadf1ccf | 163 | static inline int xattr_iter_fixup(struct xattr_iter *it) |
b17500a0 | 164 | { |
cadf1ccf GX |
165 | if (it->ofs < EROFS_BLKSIZ) |
166 | return 0; | |
167 | ||
168 | xattr_iter_end(it, true); | |
b17500a0 | 169 | |
cadf1ccf | 170 | it->blkaddr += erofs_blknr(it->ofs); |
b17500a0 | 171 | |
e655b5b3 | 172 | it->page = erofs_get_meta_page(it->sb, it->blkaddr); |
cadf1ccf GX |
173 | if (IS_ERR(it->page)) { |
174 | int err = PTR_ERR(it->page); | |
175 | ||
176 | it->page = NULL; | |
177 | return err; | |
b17500a0 | 178 | } |
cadf1ccf GX |
179 | |
180 | it->kaddr = kmap_atomic(it->page); | |
181 | it->ofs = erofs_blkoff(it->ofs); | |
182 | return 0; | |
b17500a0 GX |
183 | } |
184 | ||
185 | static int inline_xattr_iter_begin(struct xattr_iter *it, | |
447a3621 | 186 | struct inode *inode) |
b17500a0 | 187 | { |
a5876e24 | 188 | struct erofs_inode *const vi = EROFS_I(inode); |
b17500a0 | 189 | struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb); |
7dd68b14 | 190 | unsigned int xattr_header_sz, inline_xattr_ofs; |
b17500a0 GX |
191 | |
192 | xattr_header_sz = inlinexattr_header_size(inode); | |
8d8a09b0 | 193 | if (xattr_header_sz >= vi->xattr_isize) { |
9ddc7004 | 194 | DBG_BUGON(xattr_header_sz > vi->xattr_isize); |
b17500a0 GX |
195 | return -ENOATTR; |
196 | } | |
197 | ||
198 | inline_xattr_ofs = vi->inode_isize + xattr_header_sz; | |
199 | ||
200 | it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs); | |
201 | it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs); | |
202 | ||
e655b5b3 | 203 | it->page = erofs_get_meta_page(inode->i_sb, it->blkaddr); |
cadf1ccf GX |
204 | if (IS_ERR(it->page)) |
205 | return PTR_ERR(it->page); | |
b17500a0 | 206 | |
cadf1ccf | 207 | it->kaddr = kmap_atomic(it->page); |
b17500a0 GX |
208 | return vi->xattr_isize - xattr_header_sz; |
209 | } | |
210 | ||
bdf30cef GX |
211 | /* |
212 | * Regardless of success or failure, `xattr_foreach' will end up with | |
213 | * `ofs' pointing to the next xattr item rather than an arbitrary position. | |
214 | */ | |
b17500a0 | 215 | static int xattr_foreach(struct xattr_iter *it, |
447a3621 JM |
216 | const struct xattr_iter_handlers *op, |
217 | unsigned int *tlimit) | |
b17500a0 GX |
218 | { |
219 | struct erofs_xattr_entry entry; | |
7dd68b14 | 220 | unsigned int value_sz, processed, slice; |
b17500a0 GX |
221 | int err; |
222 | ||
223 | /* 0. fixup blkaddr, ofs, ipage */ | |
cadf1ccf GX |
224 | err = xattr_iter_fixup(it); |
225 | if (err) | |
226 | return err; | |
b17500a0 GX |
227 | |
228 | /* | |
229 | * 1. read xattr entry to the memory, | |
230 | * since we do EROFS_XATTR_ALIGN | |
231 | * therefore entry should be in the page | |
232 | */ | |
233 | entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs); | |
561fb35a | 234 | if (tlimit) { |
b6796abd | 235 | unsigned int entry_sz = erofs_xattr_entry_size(&entry); |
b17500a0 | 236 | |
9ddc7004 | 237 | /* xattr on-disk corruption: xattr entry beyond xattr_isize */ |
8d8a09b0 | 238 | if (*tlimit < entry_sz) { |
9ddc7004 | 239 | DBG_BUGON(1); |
a6b9b1d5 | 240 | return -EFSCORRUPTED; |
9ddc7004 | 241 | } |
b17500a0 GX |
242 | *tlimit -= entry_sz; |
243 | } | |
244 | ||
245 | it->ofs += sizeof(struct erofs_xattr_entry); | |
246 | value_sz = le16_to_cpu(entry.e_value_size); | |
247 | ||
248 | /* handle entry */ | |
249 | err = op->entry(it, &entry); | |
250 | if (err) { | |
251 | it->ofs += entry.e_name_len + value_sz; | |
252 | goto out; | |
253 | } | |
254 | ||
255 | /* 2. handle xattr name (ofs will finally be at the end of name) */ | |
256 | processed = 0; | |
257 | ||
258 | while (processed < entry.e_name_len) { | |
259 | if (it->ofs >= EROFS_BLKSIZ) { | |
9ddc7004 | 260 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
b17500a0 | 261 | |
cadf1ccf GX |
262 | err = xattr_iter_fixup(it); |
263 | if (err) | |
264 | goto out; | |
b17500a0 GX |
265 | it->ofs = 0; |
266 | } | |
267 | ||
7dd68b14 TW |
268 | slice = min_t(unsigned int, PAGE_SIZE - it->ofs, |
269 | entry.e_name_len - processed); | |
b17500a0 GX |
270 | |
271 | /* handle name */ | |
272 | err = op->name(it, processed, it->kaddr + it->ofs, slice); | |
273 | if (err) { | |
274 | it->ofs += entry.e_name_len - processed + value_sz; | |
275 | goto out; | |
276 | } | |
277 | ||
278 | it->ofs += slice; | |
279 | processed += slice; | |
280 | } | |
281 | ||
282 | /* 3. handle xattr value */ | |
283 | processed = 0; | |
284 | ||
561fb35a | 285 | if (op->alloc_buffer) { |
b17500a0 GX |
286 | err = op->alloc_buffer(it, value_sz); |
287 | if (err) { | |
288 | it->ofs += value_sz; | |
289 | goto out; | |
290 | } | |
291 | } | |
292 | ||
293 | while (processed < value_sz) { | |
294 | if (it->ofs >= EROFS_BLKSIZ) { | |
9ddc7004 | 295 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
cadf1ccf GX |
296 | |
297 | err = xattr_iter_fixup(it); | |
298 | if (err) | |
299 | goto out; | |
b17500a0 GX |
300 | it->ofs = 0; |
301 | } | |
302 | ||
7dd68b14 TW |
303 | slice = min_t(unsigned int, PAGE_SIZE - it->ofs, |
304 | value_sz - processed); | |
b17500a0 GX |
305 | op->value(it, processed, it->kaddr + it->ofs, slice); |
306 | it->ofs += slice; | |
307 | processed += slice; | |
308 | } | |
309 | ||
310 | out: | |
bdf30cef | 311 | /* xattrs should be 4-byte aligned (on-disk constraint) */ |
b17500a0 | 312 | it->ofs = EROFS_XATTR_ALIGN(it->ofs); |
6614f765 | 313 | return err < 0 ? err : 0; |
b17500a0 GX |
314 | } |
315 | ||
316 | struct getxattr_iter { | |
317 | struct xattr_iter it; | |
318 | ||
319 | char *buffer; | |
320 | int buffer_size, index; | |
321 | struct qstr name; | |
322 | }; | |
323 | ||
324 | static int xattr_entrymatch(struct xattr_iter *_it, | |
447a3621 | 325 | struct erofs_xattr_entry *entry) |
b17500a0 GX |
326 | { |
327 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); | |
328 | ||
329 | return (it->index != entry->e_name_index || | |
330 | it->name.len != entry->e_name_len) ? -ENOATTR : 0; | |
331 | } | |
332 | ||
333 | static int xattr_namematch(struct xattr_iter *_it, | |
447a3621 | 334 | unsigned int processed, char *buf, unsigned int len) |
b17500a0 GX |
335 | { |
336 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); | |
337 | ||
338 | return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0; | |
339 | } | |
340 | ||
341 | static int xattr_checkbuffer(struct xattr_iter *_it, | |
447a3621 | 342 | unsigned int value_sz) |
b17500a0 GX |
343 | { |
344 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); | |
345 | int err = it->buffer_size < value_sz ? -ERANGE : 0; | |
346 | ||
347 | it->buffer_size = value_sz; | |
561fb35a | 348 | return !it->buffer ? 1 : err; |
b17500a0 GX |
349 | } |
350 | ||
351 | static void xattr_copyvalue(struct xattr_iter *_it, | |
447a3621 JM |
352 | unsigned int processed, |
353 | char *buf, unsigned int len) | |
b17500a0 GX |
354 | { |
355 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); | |
356 | ||
357 | memcpy(it->buffer + processed, buf, len); | |
358 | } | |
359 | ||
cadf1ccf | 360 | static const struct xattr_iter_handlers find_xattr_handlers = { |
b17500a0 GX |
361 | .entry = xattr_entrymatch, |
362 | .name = xattr_namematch, | |
363 | .alloc_buffer = xattr_checkbuffer, | |
364 | .value = xattr_copyvalue | |
365 | }; | |
366 | ||
367 | static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) | |
368 | { | |
369 | int ret; | |
7dd68b14 | 370 | unsigned int remaining; |
b17500a0 GX |
371 | |
372 | ret = inline_xattr_iter_begin(&it->it, inode); | |
373 | if (ret < 0) | |
374 | return ret; | |
375 | ||
376 | remaining = ret; | |
377 | while (remaining) { | |
2bc75964 | 378 | ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); |
6614f765 | 379 | if (ret != -ENOATTR) |
cadf1ccf | 380 | break; |
b17500a0 | 381 | } |
cadf1ccf | 382 | xattr_iter_end_final(&it->it); |
b17500a0 | 383 | |
6614f765 | 384 | return ret ? ret : it->buffer_size; |
b17500a0 GX |
385 | } |
386 | ||
387 | static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) | |
388 | { | |
a5876e24 | 389 | struct erofs_inode *const vi = EROFS_I(inode); |
6e78901a GX |
390 | struct super_block *const sb = inode->i_sb; |
391 | struct erofs_sb_info *const sbi = EROFS_SB(sb); | |
7dd68b14 | 392 | unsigned int i; |
b17500a0 GX |
393 | int ret = -ENOATTR; |
394 | ||
395 | for (i = 0; i < vi->xattr_shared_count; ++i) { | |
396 | erofs_blk_t blkaddr = | |
397 | xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]); | |
398 | ||
399 | it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]); | |
400 | ||
401 | if (!i || blkaddr != it->it.blkaddr) { | |
402 | if (i) | |
403 | xattr_iter_end(&it->it, true); | |
404 | ||
e655b5b3 | 405 | it->it.page = erofs_get_meta_page(sb, blkaddr); |
cadf1ccf GX |
406 | if (IS_ERR(it->it.page)) |
407 | return PTR_ERR(it->it.page); | |
408 | ||
b17500a0 GX |
409 | it->it.kaddr = kmap_atomic(it->it.page); |
410 | it->it.blkaddr = blkaddr; | |
411 | } | |
412 | ||
2bc75964 | 413 | ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); |
6614f765 | 414 | if (ret != -ENOATTR) |
cadf1ccf | 415 | break; |
b17500a0 GX |
416 | } |
417 | if (vi->xattr_shared_count) | |
cadf1ccf | 418 | xattr_iter_end_final(&it->it); |
b17500a0 | 419 | |
6614f765 | 420 | return ret ? ret : it->buffer_size; |
b17500a0 GX |
421 | } |
422 | ||
423 | static bool erofs_xattr_user_list(struct dentry *dentry) | |
424 | { | |
f57a3fe4 | 425 | return test_opt(&EROFS_SB(dentry->d_sb)->ctx, XATTR_USER); |
b17500a0 GX |
426 | } |
427 | ||
428 | static bool erofs_xattr_trusted_list(struct dentry *dentry) | |
429 | { | |
430 | return capable(CAP_SYS_ADMIN); | |
431 | } | |
432 | ||
433 | int erofs_getxattr(struct inode *inode, int index, | |
447a3621 JM |
434 | const char *name, |
435 | void *buffer, size_t buffer_size) | |
b17500a0 GX |
436 | { |
437 | int ret; | |
438 | struct getxattr_iter it; | |
439 | ||
8d8a09b0 | 440 | if (!name) |
b17500a0 GX |
441 | return -EINVAL; |
442 | ||
cadf1ccf GX |
443 | ret = init_inode_xattrs(inode); |
444 | if (ret) | |
445 | return ret; | |
b17500a0 GX |
446 | |
447 | it.index = index; | |
448 | ||
449 | it.name.len = strlen(name); | |
450 | if (it.name.len > EROFS_NAME_LEN) | |
451 | return -ERANGE; | |
452 | it.name.name = name; | |
453 | ||
454 | it.buffer = buffer; | |
455 | it.buffer_size = buffer_size; | |
456 | ||
457 | it.it.sb = inode->i_sb; | |
458 | ret = inline_getxattr(inode, &it); | |
459 | if (ret == -ENOATTR) | |
460 | ret = shared_getxattr(inode, &it); | |
461 | return ret; | |
462 | } | |
463 | ||
464 | static int erofs_xattr_generic_get(const struct xattr_handler *handler, | |
447a3621 JM |
465 | struct dentry *unused, struct inode *inode, |
466 | const char *name, void *buffer, size_t size) | |
b17500a0 | 467 | { |
b17500a0 GX |
468 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
469 | ||
470 | switch (handler->flags) { | |
471 | case EROFS_XATTR_INDEX_USER: | |
f57a3fe4 | 472 | if (!test_opt(&sbi->ctx, XATTR_USER)) |
b17500a0 GX |
473 | return -EOPNOTSUPP; |
474 | break; | |
475 | case EROFS_XATTR_INDEX_TRUSTED: | |
b17500a0 GX |
476 | break; |
477 | case EROFS_XATTR_INDEX_SECURITY: | |
478 | break; | |
479 | default: | |
480 | return -EINVAL; | |
481 | } | |
482 | ||
b17500a0 GX |
483 | return erofs_getxattr(inode, handler->flags, name, buffer, size); |
484 | } | |
485 | ||
486 | const struct xattr_handler erofs_xattr_user_handler = { | |
487 | .prefix = XATTR_USER_PREFIX, | |
488 | .flags = EROFS_XATTR_INDEX_USER, | |
489 | .list = erofs_xattr_user_list, | |
490 | .get = erofs_xattr_generic_get, | |
491 | }; | |
492 | ||
493 | const struct xattr_handler erofs_xattr_trusted_handler = { | |
494 | .prefix = XATTR_TRUSTED_PREFIX, | |
495 | .flags = EROFS_XATTR_INDEX_TRUSTED, | |
496 | .list = erofs_xattr_trusted_list, | |
497 | .get = erofs_xattr_generic_get, | |
498 | }; | |
499 | ||
500 | #ifdef CONFIG_EROFS_FS_SECURITY | |
501 | const struct xattr_handler __maybe_unused erofs_xattr_security_handler = { | |
502 | .prefix = XATTR_SECURITY_PREFIX, | |
503 | .flags = EROFS_XATTR_INDEX_SECURITY, | |
504 | .get = erofs_xattr_generic_get, | |
505 | }; | |
506 | #endif | |
507 | ||
b17500a0 GX |
508 | const struct xattr_handler *erofs_xattr_handlers[] = { |
509 | &erofs_xattr_user_handler, | |
510 | #ifdef CONFIG_EROFS_FS_POSIX_ACL | |
511 | &posix_acl_access_xattr_handler, | |
512 | &posix_acl_default_xattr_handler, | |
513 | #endif | |
514 | &erofs_xattr_trusted_handler, | |
515 | #ifdef CONFIG_EROFS_FS_SECURITY | |
516 | &erofs_xattr_security_handler, | |
517 | #endif | |
518 | NULL, | |
519 | }; | |
b17500a0 GX |
520 | |
521 | struct listxattr_iter { | |
522 | struct xattr_iter it; | |
523 | ||
524 | struct dentry *dentry; | |
525 | char *buffer; | |
526 | int buffer_size, buffer_ofs; | |
527 | }; | |
528 | ||
529 | static int xattr_entrylist(struct xattr_iter *_it, | |
447a3621 | 530 | struct erofs_xattr_entry *entry) |
b17500a0 GX |
531 | { |
532 | struct listxattr_iter *it = | |
533 | container_of(_it, struct listxattr_iter, it); | |
7dd68b14 | 534 | unsigned int prefix_len; |
b17500a0 GX |
535 | const char *prefix; |
536 | ||
537 | const struct xattr_handler *h = | |
538 | erofs_xattr_handler(entry->e_name_index); | |
539 | ||
561fb35a | 540 | if (!h || (h->list && !h->list(it->dentry))) |
b17500a0 GX |
541 | return 1; |
542 | ||
a24df1f6 | 543 | prefix = xattr_prefix(h); |
b17500a0 GX |
544 | prefix_len = strlen(prefix); |
545 | ||
561fb35a | 546 | if (!it->buffer) { |
b17500a0 GX |
547 | it->buffer_ofs += prefix_len + entry->e_name_len + 1; |
548 | return 1; | |
549 | } | |
550 | ||
551 | if (it->buffer_ofs + prefix_len | |
552 | + entry->e_name_len + 1 > it->buffer_size) | |
553 | return -ERANGE; | |
554 | ||
555 | memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len); | |
556 | it->buffer_ofs += prefix_len; | |
557 | return 0; | |
558 | } | |
559 | ||
560 | static int xattr_namelist(struct xattr_iter *_it, | |
447a3621 | 561 | unsigned int processed, char *buf, unsigned int len) |
b17500a0 GX |
562 | { |
563 | struct listxattr_iter *it = | |
564 | container_of(_it, struct listxattr_iter, it); | |
565 | ||
566 | memcpy(it->buffer + it->buffer_ofs, buf, len); | |
567 | it->buffer_ofs += len; | |
568 | return 0; | |
569 | } | |
570 | ||
571 | static int xattr_skipvalue(struct xattr_iter *_it, | |
447a3621 | 572 | unsigned int value_sz) |
b17500a0 GX |
573 | { |
574 | struct listxattr_iter *it = | |
575 | container_of(_it, struct listxattr_iter, it); | |
576 | ||
577 | it->buffer[it->buffer_ofs++] = '\0'; | |
578 | return 1; | |
579 | } | |
580 | ||
cadf1ccf | 581 | static const struct xattr_iter_handlers list_xattr_handlers = { |
b17500a0 GX |
582 | .entry = xattr_entrylist, |
583 | .name = xattr_namelist, | |
584 | .alloc_buffer = xattr_skipvalue, | |
585 | .value = NULL | |
586 | }; | |
587 | ||
588 | static int inline_listxattr(struct listxattr_iter *it) | |
589 | { | |
590 | int ret; | |
7dd68b14 | 591 | unsigned int remaining; |
b17500a0 GX |
592 | |
593 | ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry)); | |
594 | if (ret < 0) | |
595 | return ret; | |
596 | ||
597 | remaining = ret; | |
598 | while (remaining) { | |
2bc75964 | 599 | ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); |
6614f765 | 600 | if (ret) |
b17500a0 GX |
601 | break; |
602 | } | |
cadf1ccf | 603 | xattr_iter_end_final(&it->it); |
6614f765 | 604 | return ret ? ret : it->buffer_ofs; |
b17500a0 GX |
605 | } |
606 | ||
607 | static int shared_listxattr(struct listxattr_iter *it) | |
608 | { | |
609 | struct inode *const inode = d_inode(it->dentry); | |
a5876e24 | 610 | struct erofs_inode *const vi = EROFS_I(inode); |
6e78901a GX |
611 | struct super_block *const sb = inode->i_sb; |
612 | struct erofs_sb_info *const sbi = EROFS_SB(sb); | |
7dd68b14 | 613 | unsigned int i; |
b17500a0 GX |
614 | int ret = 0; |
615 | ||
616 | for (i = 0; i < vi->xattr_shared_count; ++i) { | |
617 | erofs_blk_t blkaddr = | |
618 | xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]); | |
619 | ||
620 | it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]); | |
621 | if (!i || blkaddr != it->it.blkaddr) { | |
622 | if (i) | |
623 | xattr_iter_end(&it->it, true); | |
624 | ||
e655b5b3 | 625 | it->it.page = erofs_get_meta_page(sb, blkaddr); |
cadf1ccf GX |
626 | if (IS_ERR(it->it.page)) |
627 | return PTR_ERR(it->it.page); | |
628 | ||
b17500a0 GX |
629 | it->it.kaddr = kmap_atomic(it->it.page); |
630 | it->it.blkaddr = blkaddr; | |
631 | } | |
632 | ||
2bc75964 | 633 | ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL); |
6614f765 | 634 | if (ret) |
b17500a0 GX |
635 | break; |
636 | } | |
637 | if (vi->xattr_shared_count) | |
cadf1ccf | 638 | xattr_iter_end_final(&it->it); |
b17500a0 | 639 | |
6614f765 | 640 | return ret ? ret : it->buffer_ofs; |
b17500a0 GX |
641 | } |
642 | ||
643 | ssize_t erofs_listxattr(struct dentry *dentry, | |
447a3621 | 644 | char *buffer, size_t buffer_size) |
b17500a0 GX |
645 | { |
646 | int ret; | |
647 | struct listxattr_iter it; | |
648 | ||
cadf1ccf | 649 | ret = init_inode_xattrs(d_inode(dentry)); |
926d1650 GX |
650 | if (ret == -ENOATTR) |
651 | return 0; | |
cadf1ccf GX |
652 | if (ret) |
653 | return ret; | |
b17500a0 GX |
654 | |
655 | it.dentry = dentry; | |
656 | it.buffer = buffer; | |
657 | it.buffer_size = buffer_size; | |
658 | it.buffer_ofs = 0; | |
659 | ||
660 | it.it.sb = dentry->d_sb; | |
661 | ||
662 | ret = inline_listxattr(&it); | |
663 | if (ret < 0 && ret != -ENOATTR) | |
664 | return ret; | |
665 | return shared_listxattr(&it); | |
666 | } | |
667 | ||
516c115c GX |
668 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
669 | struct posix_acl *erofs_get_acl(struct inode *inode, int type) | |
670 | { | |
671 | struct posix_acl *acl; | |
672 | int prefix, rc; | |
673 | char *value = NULL; | |
674 | ||
675 | switch (type) { | |
676 | case ACL_TYPE_ACCESS: | |
677 | prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS; | |
678 | break; | |
679 | case ACL_TYPE_DEFAULT: | |
680 | prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT; | |
681 | break; | |
682 | default: | |
683 | return ERR_PTR(-EINVAL); | |
684 | } | |
685 | ||
686 | rc = erofs_getxattr(inode, prefix, "", NULL, 0); | |
687 | if (rc > 0) { | |
688 | value = kmalloc(rc, GFP_KERNEL); | |
689 | if (!value) | |
690 | return ERR_PTR(-ENOMEM); | |
691 | rc = erofs_getxattr(inode, prefix, "", value, rc); | |
692 | } | |
693 | ||
694 | if (rc == -ENOATTR) | |
695 | acl = NULL; | |
696 | else if (rc < 0) | |
697 | acl = ERR_PTR(rc); | |
698 | else | |
699 | acl = posix_acl_from_xattr(&init_user_ns, value, rc); | |
700 | kfree(value); | |
701 | return acl; | |
702 | } | |
703 | #endif | |
704 |