Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 5 | * Copyright (C) 2021-2022, Alibaba Cloud |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/security.h> |
| 8 | #include "xattr.h" |
| 9 | |
| 10 | struct xattr_iter { |
| 11 | struct super_block *sb; |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 12 | struct erofs_buf buf; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 13 | void *kaddr; |
| 14 | |
| 15 | erofs_blk_t blkaddr; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 16 | unsigned int ofs; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 17 | }; |
| 18 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 19 | static int init_inode_xattrs(struct inode *inode) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 20 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 21 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 22 | struct xattr_iter it; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 23 | unsigned int i; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 24 | struct erofs_xattr_ibody_header *ih; |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 25 | struct super_block *sb; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 26 | struct erofs_sb_info *sbi; |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 27 | int ret = 0; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 28 | |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 29 | /* the most case is that xattrs of this inode are initialized. */ |
Gao Xiang | ce063129 | 2021-02-09 21:06:18 +0800 | [diff] [blame] | 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(); |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 36 | return 0; |
Gao Xiang | ce063129 | 2021-02-09 21:06:18 +0800 | [diff] [blame] | 37 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 38 | |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 39 | if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE)) |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 40 | return -ERESTARTSYS; |
| 41 | |
| 42 | /* someone has initialized xattrs for us? */ |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 43 | if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 44 | goto out_unlock; |
Gao Xiang | 7077fff | 2019-01-14 19:40:23 +0800 | [diff] [blame] | 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)) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 55 | erofs_err(inode->i_sb, |
| 56 | "xattr_isize %d of nid %llu is not supported yet", |
| 57 | vi->xattr_isize, vi->nid); |
Gao Xiang | ff784a7 | 2019-08-14 18:37:05 +0800 | [diff] [blame] | 58 | ret = -EOPNOTSUPP; |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 59 | goto out_unlock; |
Gao Xiang | 7077fff | 2019-01-14 19:40:23 +0800 | [diff] [blame] | 60 | } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) { |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 61 | if (vi->xattr_isize) { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 62 | erofs_err(inode->i_sb, |
| 63 | "bogus xattr ibody @ nid %llu", vi->nid); |
Gao Xiang | 7077fff | 2019-01-14 19:40:23 +0800 | [diff] [blame] | 64 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 65 | ret = -EFSCORRUPTED; |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 66 | goto out_unlock; /* xattr ondisk layout error */ |
Gao Xiang | 7077fff | 2019-01-14 19:40:23 +0800 | [diff] [blame] | 67 | } |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 68 | ret = -ENOATTR; |
| 69 | goto out_unlock; |
Gao Xiang | 7077fff | 2019-01-14 19:40:23 +0800 | [diff] [blame] | 70 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 71 | |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 72 | sb = inode->i_sb; |
| 73 | sbi = EROFS_SB(sb); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 74 | it.buf = __EROFS_BUF_INITIALIZER; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 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); |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 82 | goto out_unlock; |
| 83 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 84 | |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 85 | ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 86 | vi->xattr_shared_count = ih->h_shared_count; |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 87 | vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count, |
| 88 | sizeof(uint), GFP_KERNEL); |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 89 | if (!vi->xattr_shared_xattrs) { |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 90 | erofs_put_metabuf(&it.buf); |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 91 | ret = -ENOMEM; |
| 92 | goto out_unlock; |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 93 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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) { |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 99 | if (it.ofs >= EROFS_BLKSIZ) { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 100 | /* cannot be unaligned */ |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 101 | DBG_BUGON(it.ofs != EROFS_BLKSIZ); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 102 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 103 | it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr, |
| 104 | EROFS_KMAP); |
| 105 | if (IS_ERR(it.kaddr)) { |
Sheng Yong | 3b1b529 | 2019-02-14 14:46:36 +0800 | [diff] [blame] | 106 | kfree(vi->xattr_shared_xattrs); |
| 107 | vi->xattr_shared_xattrs = NULL; |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 108 | ret = PTR_ERR(it.kaddr); |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 109 | goto out_unlock; |
Sheng Yong | 3b1b529 | 2019-02-14 14:46:36 +0800 | [diff] [blame] | 110 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | } |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 117 | erofs_put_metabuf(&it.buf); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 118 | |
Gao Xiang | ce063129 | 2021-02-09 21:06:18 +0800 | [diff] [blame] | 119 | /* paired with smp_mb() at the beginning of the function. */ |
| 120 | smp_mb(); |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 121 | set_bit(EROFS_I_EA_INITED_BIT, &vi->flags); |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 122 | |
| 123 | out_unlock: |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 124 | clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags); |
Gao Xiang | 62dc459 | 2019-02-18 15:19:04 +0800 | [diff] [blame] | 125 | return ret; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Gao Xiang | bdf30ce | 2018-09-19 13:49:09 +0800 | [diff] [blame] | 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 | */ |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 135 | struct xattr_iter_handlers { |
Sidong Yang | 4b03f3f | 2019-01-08 13:24:54 +0000 | [diff] [blame] | 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); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 142 | }; |
| 143 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 144 | static inline int xattr_iter_fixup(struct xattr_iter *it) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 145 | { |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 146 | if (it->ofs < EROFS_BLKSIZ) |
| 147 | return 0; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 148 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 149 | it->blkaddr += erofs_blknr(it->ofs); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 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); |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 154 | it->ofs = erofs_blkoff(it->ofs); |
| 155 | return 0; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int inline_xattr_iter_begin(struct xattr_iter *it, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 159 | struct inode *inode) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 160 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 161 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 162 | struct erofs_sb_info *const sbi = EROFS_SB(inode->i_sb); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 163 | unsigned int xattr_header_sz, inline_xattr_ofs; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 164 | |
| 165 | xattr_header_sz = inlinexattr_header_size(inode); |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 166 | if (xattr_header_sz >= vi->xattr_isize) { |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 167 | DBG_BUGON(xattr_header_sz > vi->xattr_isize); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 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); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 180 | return vi->xattr_isize - xattr_header_sz; |
| 181 | } |
| 182 | |
Gao Xiang | bdf30ce | 2018-09-19 13:49:09 +0800 | [diff] [blame] | 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 | */ |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 187 | static int xattr_foreach(struct xattr_iter *it, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 188 | const struct xattr_iter_handlers *op, |
| 189 | unsigned int *tlimit) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 190 | { |
| 191 | struct erofs_xattr_entry entry; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 192 | unsigned int value_sz, processed, slice; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 193 | int err; |
| 194 | |
| 195 | /* 0. fixup blkaddr, ofs, ipage */ |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 196 | err = xattr_iter_fixup(it); |
| 197 | if (err) |
| 198 | return err; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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); |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 206 | if (tlimit) { |
Gao Xiang | b6796ab | 2019-09-04 10:08:50 +0800 | [diff] [blame] | 207 | unsigned int entry_sz = erofs_xattr_entry_size(&entry); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 208 | |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 209 | /* xattr on-disk corruption: xattr entry beyond xattr_isize */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 210 | if (*tlimit < entry_sz) { |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 211 | DBG_BUGON(1); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 212 | return -EFSCORRUPTED; |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 213 | } |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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) { |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 232 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 233 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 234 | err = xattr_iter_fixup(it); |
| 235 | if (err) |
| 236 | goto out; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 237 | it->ofs = 0; |
| 238 | } |
| 239 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 240 | slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 241 | entry.e_name_len - processed); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 257 | if (op->alloc_buffer) { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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) { |
Gao Xiang | 9ddc700 | 2019-08-13 10:30:54 +0800 | [diff] [blame] | 267 | DBG_BUGON(it->ofs > EROFS_BLKSIZ); |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 268 | |
| 269 | err = xattr_iter_fixup(it); |
| 270 | if (err) |
| 271 | goto out; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 272 | it->ofs = 0; |
| 273 | } |
| 274 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 275 | slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs, |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 276 | value_sz - processed); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 277 | op->value(it, processed, it->kaddr + it->ofs, slice); |
| 278 | it->ofs += slice; |
| 279 | processed += slice; |
| 280 | } |
| 281 | |
| 282 | out: |
Gao Xiang | bdf30ce | 2018-09-19 13:49:09 +0800 | [diff] [blame] | 283 | /* xattrs should be 4-byte aligned (on-disk constraint) */ |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 284 | it->ofs = EROFS_XATTR_ALIGN(it->ofs); |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 285 | return err < 0 ? err : 0; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 297 | struct erofs_xattr_entry *entry) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 306 | unsigned int processed, char *buf, unsigned int len) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 314 | unsigned int value_sz) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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; |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 320 | return !it->buffer ? 1 : err; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static void xattr_copyvalue(struct xattr_iter *_it, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 324 | unsigned int processed, |
| 325 | char *buf, unsigned int len) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 326 | { |
| 327 | struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it); |
| 328 | |
| 329 | memcpy(it->buffer + processed, buf, len); |
| 330 | } |
| 331 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 332 | static const struct xattr_iter_handlers find_xattr_handlers = { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 342 | unsigned int remaining; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 343 | |
| 344 | ret = inline_xattr_iter_begin(&it->it, inode); |
| 345 | if (ret < 0) |
| 346 | return ret; |
| 347 | |
| 348 | remaining = ret; |
| 349 | while (remaining) { |
Kristaps Čivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 350 | ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 351 | if (ret != -ENOATTR) |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 352 | break; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 353 | } |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 354 | return ret ? ret : it->buffer_size; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) |
| 358 | { |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 359 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 360 | struct super_block *const sb = inode->i_sb; |
| 361 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 362 | unsigned int i; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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]); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 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; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 375 | |
Kristaps Čivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 376 | ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 377 | if (ret != -ENOATTR) |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 378 | break; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 379 | } |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 380 | return ret ? ret : it->buffer_size; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static bool erofs_xattr_user_list(struct dentry *dentry) |
| 384 | { |
Gao Xiang | e624246 | 2021-10-07 15:02:23 +0800 | [diff] [blame] | 385 | return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 394 | const char *name, |
| 395 | void *buffer, size_t buffer_size) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 396 | { |
| 397 | int ret; |
| 398 | struct getxattr_iter it; |
| 399 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 400 | if (!name) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 401 | return -EINVAL; |
| 402 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 403 | ret = init_inode_xattrs(inode); |
| 404 | if (ret) |
| 405 | return ret; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 406 | |
| 407 | it.index = index; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 408 | it.name.len = strlen(name); |
| 409 | if (it.name.len > EROFS_NAME_LEN) |
| 410 | return -ERANGE; |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 411 | |
| 412 | it.it.buf = __EROFS_BUF_INITIALIZER; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 422 | erofs_put_metabuf(&it.it.buf); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static int erofs_xattr_generic_get(const struct xattr_handler *handler, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 427 | struct dentry *unused, struct inode *inode, |
| 428 | const char *name, void *buffer, size_t size) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 429 | { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 430 | struct erofs_sb_info *const sbi = EROFS_I_SB(inode); |
| 431 | |
| 432 | switch (handler->flags) { |
| 433 | case EROFS_XATTR_INDEX_USER: |
Gao Xiang | e624246 | 2021-10-07 15:02:23 +0800 | [diff] [blame] | 434 | if (!test_opt(&sbi->opt, XATTR_USER)) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 435 | return -EOPNOTSUPP; |
| 436 | break; |
| 437 | case EROFS_XATTR_INDEX_TRUSTED: |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 438 | break; |
| 439 | case EROFS_XATTR_INDEX_SECURITY: |
| 440 | break; |
| 441 | default: |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | }; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 492 | struct erofs_xattr_entry *entry) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 493 | { |
| 494 | struct listxattr_iter *it = |
| 495 | container_of(_it, struct listxattr_iter, it); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 496 | unsigned int prefix_len; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 497 | const char *prefix; |
| 498 | |
| 499 | const struct xattr_handler *h = |
| 500 | erofs_xattr_handler(entry->e_name_index); |
| 501 | |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 502 | if (!h || (h->list && !h->list(it->dentry))) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 503 | return 1; |
| 504 | |
Gao Xiang | a24df1f | 2019-01-29 16:35:19 +0800 | [diff] [blame] | 505 | prefix = xattr_prefix(h); |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 506 | prefix_len = strlen(prefix); |
| 507 | |
Bhanusree Pola | 561fb35 | 2019-03-22 10:38:16 +0800 | [diff] [blame] | 508 | if (!it->buffer) { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 523 | unsigned int processed, char *buf, unsigned int len) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 534 | unsigned int value_sz) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 543 | static const struct xattr_iter_handlers list_xattr_handlers = { |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 553 | unsigned int remaining; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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) { |
Kristaps Čivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 561 | ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 562 | if (ret) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 563 | break; |
| 564 | } |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 565 | return ret ? ret : it->buffer_ofs; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static int shared_listxattr(struct listxattr_iter *it) |
| 569 | { |
| 570 | struct inode *const inode = d_inode(it->dentry); |
Gao Xiang | a5876e2 | 2019-09-04 10:08:56 +0800 | [diff] [blame] | 571 | struct erofs_inode *const vi = EROFS_I(inode); |
Gao Xiang | 6e78901 | 2018-08-21 22:49:30 +0800 | [diff] [blame] | 572 | struct super_block *const sb = inode->i_sb; |
| 573 | struct erofs_sb_info *const sbi = EROFS_SB(sb); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 574 | unsigned int i; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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]); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 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; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 587 | |
Kristaps Čivkulis | 2bc7596 | 2018-08-05 18:21:01 +0300 | [diff] [blame] | 588 | ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL); |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 589 | if (ret) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 590 | break; |
| 591 | } |
Gao Xiang | 6614f76 | 2018-09-19 13:49:10 +0800 | [diff] [blame] | 592 | return ret ? ret : it->buffer_ofs; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | ssize_t erofs_listxattr(struct dentry *dentry, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 596 | char *buffer, size_t buffer_size) |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 597 | { |
| 598 | int ret; |
| 599 | struct listxattr_iter it; |
| 600 | |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 601 | ret = init_inode_xattrs(d_inode(dentry)); |
Gao Xiang | 926d165 | 2019-12-01 16:01:09 +0800 | [diff] [blame] | 602 | if (ret == -ENOATTR) |
| 603 | return 0; |
Gao Xiang | cadf1ccf | 2018-08-21 22:49:31 +0800 | [diff] [blame] | 604 | if (ret) |
| 605 | return ret; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 606 | |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 607 | it.it.buf = __EROFS_BUF_INITIALIZER; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 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); |
Gao Xiang | bb88e8d | 2022-01-02 12:00:16 +0800 | [diff] [blame] | 616 | if (ret >= 0 || ret == -ENOATTR) |
| 617 | ret = shared_listxattr(&it); |
| 618 | erofs_put_metabuf(&it.it.buf); |
| 619 | return ret; |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 620 | } |
| 621 | |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 622 | #ifdef CONFIG_EROFS_FS_POSIX_ACL |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 623 | struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu) |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 624 | { |
| 625 | struct posix_acl *acl; |
| 626 | int prefix, rc; |
| 627 | char *value = NULL; |
| 628 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 629 | if (rcu) |
| 630 | return ERR_PTR(-ECHILD); |
| 631 | |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 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 |