1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/xattr.c
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of the Linux
11 * distribution for more details.
13 #include <linux/security.h>
17 struct super_block *sb;
25 static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
27 /* only init_inode_xattrs use non-atomic once */
28 if (unlikely(!atomic))
31 kunmap_atomic(it->kaddr);
32 unlock_page(it->page);
36 static void init_inode_xattrs(struct inode *inode)
40 struct erofs_xattr_ibody_header *ih;
41 struct erofs_sb_info *sbi;
42 struct erofs_vnode *vi;
45 if (likely(inode_has_inited_xattr(inode)))
49 BUG_ON(!vi->xattr_isize);
51 sbi = EROFS_I_SB(inode);
52 it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
53 it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
55 it.page = erofs_get_inline_page(inode, it.blkaddr);
56 BUG_ON(IS_ERR(it.page));
58 /* read in shared xattr array (non-atomic, see kmalloc below) */
59 it.kaddr = kmap(it.page);
62 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
64 vi->xattr_shared_count = ih->h_shared_count;
65 vi->xattr_shared_xattrs = (unsigned *)kmalloc_array(
66 vi->xattr_shared_count, sizeof(unsigned),
67 GFP_KERNEL | __GFP_NOFAIL);
69 /* let's skip ibody header */
70 it.ofs += sizeof(struct erofs_xattr_ibody_header);
72 for (i = 0; i < vi->xattr_shared_count; ++i) {
73 if (unlikely(it.ofs >= EROFS_BLKSIZ)) {
74 /* cannot be unaligned */
75 BUG_ON(it.ofs != EROFS_BLKSIZ);
76 xattr_iter_end(&it, atomic_map);
78 it.page = erofs_get_meta_page(inode->i_sb,
79 ++it.blkaddr, S_ISDIR(inode->i_mode));
80 BUG_ON(IS_ERR(it.page));
82 it.kaddr = kmap_atomic(it.page);
86 vi->xattr_shared_xattrs[i] =
87 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
88 it.ofs += sizeof(__le32);
90 xattr_iter_end(&it, atomic_map);
92 inode_set_inited_xattr(inode);
95 struct xattr_iter_handlers {
96 int (*entry)(struct xattr_iter *, struct erofs_xattr_entry *);
97 int (*name)(struct xattr_iter *, unsigned, char *, unsigned);
98 int (*alloc_buffer)(struct xattr_iter *, unsigned);
99 void (*value)(struct xattr_iter *, unsigned, char *, unsigned);
102 static void xattr_iter_fixup(struct xattr_iter *it)
104 if (unlikely(it->ofs >= EROFS_BLKSIZ)) {
105 xattr_iter_end(it, true);
107 it->blkaddr += erofs_blknr(it->ofs);
108 it->page = erofs_get_meta_page(it->sb, it->blkaddr, false);
109 BUG_ON(IS_ERR(it->page));
111 it->kaddr = kmap_atomic(it->page);
112 it->ofs = erofs_blkoff(it->ofs);
116 static int inline_xattr_iter_begin(struct xattr_iter *it,
119 struct erofs_vnode *const vi = EROFS_V(inode);
120 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
121 unsigned xattr_header_sz, inline_xattr_ofs;
123 xattr_header_sz = inlinexattr_header_size(inode);
124 if (unlikely(xattr_header_sz >= vi->xattr_isize)) {
125 BUG_ON(xattr_header_sz > vi->xattr_isize);
129 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
131 it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
132 it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
134 it->page = erofs_get_inline_page(inode, it->blkaddr);
135 BUG_ON(IS_ERR(it->page));
136 it->kaddr = kmap_atomic(it->page);
138 return vi->xattr_isize - xattr_header_sz;
141 static int xattr_foreach(struct xattr_iter *it,
142 struct xattr_iter_handlers *op, unsigned *tlimit)
144 struct erofs_xattr_entry entry;
145 unsigned value_sz, processed, slice;
148 /* 0. fixup blkaddr, ofs, ipage */
149 xattr_iter_fixup(it);
152 * 1. read xattr entry to the memory,
153 * since we do EROFS_XATTR_ALIGN
154 * therefore entry should be in the page
156 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
157 if (tlimit != NULL) {
158 unsigned entry_sz = EROFS_XATTR_ENTRY_SIZE(&entry);
160 BUG_ON(*tlimit < entry_sz);
164 it->ofs += sizeof(struct erofs_xattr_entry);
165 value_sz = le16_to_cpu(entry.e_value_size);
168 err = op->entry(it, &entry);
170 it->ofs += entry.e_name_len + value_sz;
174 /* 2. handle xattr name (ofs will finally be at the end of name) */
177 while (processed < entry.e_name_len) {
178 if (it->ofs >= EROFS_BLKSIZ) {
179 BUG_ON(it->ofs > EROFS_BLKSIZ);
181 xattr_iter_fixup(it);
185 slice = min_t(unsigned, PAGE_SIZE - it->ofs,
186 entry.e_name_len - processed);
189 err = op->name(it, processed, it->kaddr + it->ofs, slice);
191 it->ofs += entry.e_name_len - processed + value_sz;
199 /* 3. handle xattr value */
202 if (op->alloc_buffer != NULL) {
203 err = op->alloc_buffer(it, value_sz);
210 while (processed < value_sz) {
211 if (it->ofs >= EROFS_BLKSIZ) {
212 BUG_ON(it->ofs > EROFS_BLKSIZ);
213 xattr_iter_fixup(it);
217 slice = min_t(unsigned, PAGE_SIZE - it->ofs,
218 value_sz - processed);
219 op->value(it, processed, it->kaddr + it->ofs, slice);
225 /* we assume that ofs is aligned with 4 bytes */
226 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
230 struct getxattr_iter {
231 struct xattr_iter it;
234 int buffer_size, index;
238 static int xattr_entrymatch(struct xattr_iter *_it,
239 struct erofs_xattr_entry *entry)
241 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
243 return (it->index != entry->e_name_index ||
244 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
247 static int xattr_namematch(struct xattr_iter *_it,
248 unsigned processed, char *buf, unsigned len)
250 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
252 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
255 static int xattr_checkbuffer(struct xattr_iter *_it,
258 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
259 int err = it->buffer_size < value_sz ? -ERANGE : 0;
261 it->buffer_size = value_sz;
262 return it->buffer == NULL ? 1 : err;
265 static void xattr_copyvalue(struct xattr_iter *_it,
266 unsigned processed, char *buf, unsigned len)
268 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
270 memcpy(it->buffer + processed, buf, len);
273 static struct xattr_iter_handlers find_xattr_handlers = {
274 .entry = xattr_entrymatch,
275 .name = xattr_namematch,
276 .alloc_buffer = xattr_checkbuffer,
277 .value = xattr_copyvalue
280 static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
285 ret = inline_xattr_iter_begin(&it->it, inode);
291 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
295 xattr_iter_end(&it->it, true);
297 return ret < 0 ? ret : it->buffer_size;
300 static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
302 struct erofs_vnode *const vi = EROFS_V(inode);
303 struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb);
307 for (i = 0; i < vi->xattr_shared_count; ++i) {
308 erofs_blk_t blkaddr =
309 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
311 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
313 if (!i || blkaddr != it->it.blkaddr) {
315 xattr_iter_end(&it->it, true);
317 it->it.page = erofs_get_meta_page(inode->i_sb,
319 BUG_ON(IS_ERR(it->it.page));
320 it->it.kaddr = kmap_atomic(it->it.page);
321 it->it.blkaddr = blkaddr;
324 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
328 if (vi->xattr_shared_count)
329 xattr_iter_end(&it->it, true);
331 return ret < 0 ? ret : it->buffer_size;
334 static bool erofs_xattr_user_list(struct dentry *dentry)
336 return test_opt(EROFS_SB(dentry->d_sb), XATTR_USER);
339 static bool erofs_xattr_trusted_list(struct dentry *dentry)
341 return capable(CAP_SYS_ADMIN);
344 int erofs_getxattr(struct inode *inode, int index,
346 void *buffer, size_t buffer_size)
349 struct getxattr_iter it;
351 if (unlikely(name == NULL))
354 init_inode_xattrs(inode);
358 it.name.len = strlen(name);
359 if (it.name.len > EROFS_NAME_LEN)
364 it.buffer_size = buffer_size;
366 it.it.sb = inode->i_sb;
367 ret = inline_getxattr(inode, &it);
369 ret = shared_getxattr(inode, &it);
373 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
374 struct dentry *unused, struct inode *inode,
375 const char *name, void *buffer, size_t size)
377 struct erofs_vnode *const vi = EROFS_V(inode);
378 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
380 switch (handler->flags) {
381 case EROFS_XATTR_INDEX_USER:
382 if (!test_opt(sbi, XATTR_USER))
385 case EROFS_XATTR_INDEX_TRUSTED:
386 if (!capable(CAP_SYS_ADMIN))
389 case EROFS_XATTR_INDEX_SECURITY:
395 if (!vi->xattr_isize)
398 return erofs_getxattr(inode, handler->flags, name, buffer, size);
401 const struct xattr_handler erofs_xattr_user_handler = {
402 .prefix = XATTR_USER_PREFIX,
403 .flags = EROFS_XATTR_INDEX_USER,
404 .list = erofs_xattr_user_list,
405 .get = erofs_xattr_generic_get,
408 const struct xattr_handler erofs_xattr_trusted_handler = {
409 .prefix = XATTR_TRUSTED_PREFIX,
410 .flags = EROFS_XATTR_INDEX_TRUSTED,
411 .list = erofs_xattr_trusted_list,
412 .get = erofs_xattr_generic_get,
415 #ifdef CONFIG_EROFS_FS_SECURITY
416 const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
417 .prefix = XATTR_SECURITY_PREFIX,
418 .flags = EROFS_XATTR_INDEX_SECURITY,
419 .get = erofs_xattr_generic_get,
423 const struct xattr_handler *erofs_xattr_handlers[] = {
424 &erofs_xattr_user_handler,
425 #ifdef CONFIG_EROFS_FS_POSIX_ACL
426 &posix_acl_access_xattr_handler,
427 &posix_acl_default_xattr_handler,
429 &erofs_xattr_trusted_handler,
430 #ifdef CONFIG_EROFS_FS_SECURITY
431 &erofs_xattr_security_handler,
436 struct listxattr_iter {
437 struct xattr_iter it;
439 struct dentry *dentry;
441 int buffer_size, buffer_ofs;
444 static int xattr_entrylist(struct xattr_iter *_it,
445 struct erofs_xattr_entry *entry)
447 struct listxattr_iter *it =
448 container_of(_it, struct listxattr_iter, it);
452 const struct xattr_handler *h =
453 erofs_xattr_handler(entry->e_name_index);
455 if (h == NULL || (h->list != NULL && !h->list(it->dentry)))
458 /* Note that at least one of 'prefix' and 'name' should be non-NULL */
459 prefix = h->prefix != NULL ? h->prefix : h->name;
460 prefix_len = strlen(prefix);
462 if (it->buffer == NULL) {
463 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
467 if (it->buffer_ofs + prefix_len
468 + entry->e_name_len + 1 > it->buffer_size)
471 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
472 it->buffer_ofs += prefix_len;
476 static int xattr_namelist(struct xattr_iter *_it,
477 unsigned processed, char *buf, unsigned len)
479 struct listxattr_iter *it =
480 container_of(_it, struct listxattr_iter, it);
482 memcpy(it->buffer + it->buffer_ofs, buf, len);
483 it->buffer_ofs += len;
487 static int xattr_skipvalue(struct xattr_iter *_it,
490 struct listxattr_iter *it =
491 container_of(_it, struct listxattr_iter, it);
493 it->buffer[it->buffer_ofs++] = '\0';
497 static struct xattr_iter_handlers list_xattr_handlers = {
498 .entry = xattr_entrylist,
499 .name = xattr_namelist,
500 .alloc_buffer = xattr_skipvalue,
504 static int inline_listxattr(struct listxattr_iter *it)
509 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
515 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
519 xattr_iter_end(&it->it, true);
520 return ret < 0 ? ret : it->buffer_ofs;
523 static int shared_listxattr(struct listxattr_iter *it)
525 struct inode *const inode = d_inode(it->dentry);
526 struct erofs_vnode *const vi = EROFS_V(inode);
527 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
531 for (i = 0; i < vi->xattr_shared_count; ++i) {
532 erofs_blk_t blkaddr =
533 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
535 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
536 if (!i || blkaddr != it->it.blkaddr) {
538 xattr_iter_end(&it->it, true);
540 it->it.page = erofs_get_meta_page(inode->i_sb,
542 BUG_ON(IS_ERR(it->it.page));
543 it->it.kaddr = kmap_atomic(it->it.page);
544 it->it.blkaddr = blkaddr;
547 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
551 if (vi->xattr_shared_count)
552 xattr_iter_end(&it->it, true);
554 return ret < 0 ? ret : it->buffer_ofs;
557 ssize_t erofs_listxattr(struct dentry *dentry,
558 char *buffer, size_t buffer_size)
561 struct listxattr_iter it;
563 init_inode_xattrs(d_inode(dentry));
567 it.buffer_size = buffer_size;
570 it.it.sb = dentry->d_sb;
572 ret = inline_listxattr(&it);
573 if (ret < 0 && ret != -ENOATTR)
575 return shared_listxattr(&it);