Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 3 | * fs/f2fs/xattr.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
| 7 | * |
| 8 | * Portions of this code from linux/fs/ext2/xattr.c |
| 9 | * |
| 10 | * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de> |
| 11 | * |
| 12 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. |
| 13 | * Extended attributes for symlinks and special files added per |
| 14 | * suggestion of Luka Renko <luka.renko@hermes.si>. |
| 15 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, |
| 16 | * Red Hat Inc. |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 17 | */ |
| 18 | #include <linux/rwsem.h> |
| 19 | #include <linux/f2fs_fs.h> |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 20 | #include <linux/security.h> |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 21 | #include <linux/posix_acl_xattr.h> |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 22 | #include "f2fs.h" |
| 23 | #include "xattr.h" |
Chao Yu | 955ebcd | 2019-07-22 17:57:05 +0800 | [diff] [blame] | 24 | #include "segment.h" |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 25 | |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 26 | static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline) |
| 27 | { |
| 28 | if (likely(size == sbi->inline_xattr_slab_size)) { |
| 29 | *is_inline = true; |
| 30 | return kmem_cache_zalloc(sbi->inline_xattr_slab, GFP_NOFS); |
| 31 | } |
| 32 | *is_inline = false; |
| 33 | return f2fs_kzalloc(sbi, size, GFP_NOFS); |
| 34 | } |
| 35 | |
| 36 | static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr, |
| 37 | bool is_inline) |
| 38 | { |
| 39 | if (is_inline) |
| 40 | kmem_cache_free(sbi->inline_xattr_slab, xattr_addr); |
| 41 | else |
| 42 | kvfree(xattr_addr); |
| 43 | } |
| 44 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 45 | static int f2fs_xattr_generic_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 46 | struct dentry *unused, struct inode *inode, |
| 47 | const char *name, void *buffer, size_t size) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 48 | { |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 49 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 50 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 51 | switch (handler->flags) { |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 52 | case F2FS_XATTR_INDEX_USER: |
| 53 | if (!test_opt(sbi, XATTR_USER)) |
| 54 | return -EOPNOTSUPP; |
| 55 | break; |
| 56 | case F2FS_XATTR_INDEX_TRUSTED: |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 57 | case F2FS_XATTR_INDEX_SECURITY: |
| 58 | break; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 59 | default: |
| 60 | return -EINVAL; |
| 61 | } |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 62 | return f2fs_getxattr(inode, handler->flags, name, |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 63 | buffer, size, NULL); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 64 | } |
| 65 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 66 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 67 | struct dentry *unused, struct inode *inode, |
| 68 | const char *name, const void *value, |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 69 | size_t size, int flags) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 70 | { |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 71 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 72 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 73 | switch (handler->flags) { |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 74 | case F2FS_XATTR_INDEX_USER: |
| 75 | if (!test_opt(sbi, XATTR_USER)) |
| 76 | return -EOPNOTSUPP; |
| 77 | break; |
| 78 | case F2FS_XATTR_INDEX_TRUSTED: |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 79 | case F2FS_XATTR_INDEX_SECURITY: |
| 80 | break; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 81 | default: |
| 82 | return -EINVAL; |
| 83 | } |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 84 | return f2fs_setxattr(inode, handler->flags, name, |
Jaegeuk Kim | c02745e | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 85 | value, size, NULL, flags); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 88 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 89 | { |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 90 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 91 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 92 | return test_opt(sbi, XATTR_USER); |
| 93 | } |
| 94 | |
| 95 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
| 96 | { |
| 97 | return capable(CAP_SYS_ADMIN); |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 100 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 101 | struct dentry *unused, struct inode *inode, |
| 102 | const char *name, void *buffer, size_t size) |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 103 | { |
Chao Yu | 84e97c2 | 2015-03-23 10:36:15 +0800 | [diff] [blame] | 104 | if (buffer) |
| 105 | *((char *)buffer) = F2FS_I(inode)->i_advise; |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 106 | return sizeof(char); |
| 107 | } |
| 108 | |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 109 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 110 | struct dentry *unused, struct inode *inode, |
| 111 | const char *name, const void *value, |
Andreas Gruenbacher | d9a82a0 | 2015-10-04 19:18:51 +0200 | [diff] [blame] | 112 | size_t size, int flags) |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 113 | { |
Chao Yu | 797c1cb | 2018-07-19 23:57:54 +0800 | [diff] [blame] | 114 | unsigned char old_advise = F2FS_I(inode)->i_advise; |
| 115 | unsigned char new_advise; |
| 116 | |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 117 | if (!inode_owner_or_capable(inode)) |
| 118 | return -EPERM; |
| 119 | if (value == NULL) |
| 120 | return -EINVAL; |
| 121 | |
Chao Yu | 797c1cb | 2018-07-19 23:57:54 +0800 | [diff] [blame] | 122 | new_advise = *(char *)value; |
| 123 | if (new_advise & ~FADVISE_MODIFIABLE_BITS) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | new_advise = new_advise & FADVISE_MODIFIABLE_BITS; |
| 127 | new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS; |
| 128 | |
| 129 | F2FS_I(inode)->i_advise = new_advise; |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 130 | f2fs_mark_inode_dirty_sync(inode, true); |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 134 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 135 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 136 | void *page) |
| 137 | { |
| 138 | const struct xattr *xattr; |
| 139 | int err = 0; |
| 140 | |
| 141 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
Jaegeuk Kim | d631abd | 2014-06-01 23:24:30 +0900 | [diff] [blame] | 142 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 143 | xattr->name, xattr->value, |
Jaegeuk Kim | c02745e | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 144 | xattr->value_len, (struct page *)page, 0); |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 145 | if (err < 0) |
| 146 | break; |
| 147 | } |
| 148 | return err; |
| 149 | } |
| 150 | |
| 151 | int f2fs_init_security(struct inode *inode, struct inode *dir, |
| 152 | const struct qstr *qstr, struct page *ipage) |
| 153 | { |
| 154 | return security_inode_init_security(inode, dir, qstr, |
| 155 | &f2fs_initxattrs, ipage); |
| 156 | } |
| 157 | #endif |
| 158 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 159 | const struct xattr_handler f2fs_xattr_user_handler = { |
| 160 | .prefix = XATTR_USER_PREFIX, |
| 161 | .flags = F2FS_XATTR_INDEX_USER, |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 162 | .list = f2fs_xattr_user_list, |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 163 | .get = f2fs_xattr_generic_get, |
| 164 | .set = f2fs_xattr_generic_set, |
| 165 | }; |
| 166 | |
| 167 | const struct xattr_handler f2fs_xattr_trusted_handler = { |
| 168 | .prefix = XATTR_TRUSTED_PREFIX, |
| 169 | .flags = F2FS_XATTR_INDEX_TRUSTED, |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 170 | .list = f2fs_xattr_trusted_list, |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 171 | .get = f2fs_xattr_generic_get, |
| 172 | .set = f2fs_xattr_generic_set, |
| 173 | }; |
| 174 | |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 175 | const struct xattr_handler f2fs_xattr_advise_handler = { |
Andreas Gruenbacher | 98e9cb5 | 2015-12-02 14:44:36 +0100 | [diff] [blame] | 176 | .name = F2FS_SYSTEM_ADVISE_NAME, |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 177 | .flags = F2FS_XATTR_INDEX_ADVISE, |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 178 | .get = f2fs_xattr_advise_get, |
| 179 | .set = f2fs_xattr_advise_set, |
| 180 | }; |
| 181 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 182 | const struct xattr_handler f2fs_xattr_security_handler = { |
| 183 | .prefix = XATTR_SECURITY_PREFIX, |
| 184 | .flags = F2FS_XATTR_INDEX_SECURITY, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 185 | .get = f2fs_xattr_generic_get, |
| 186 | .set = f2fs_xattr_generic_set, |
| 187 | }; |
| 188 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 189 | static const struct xattr_handler *f2fs_xattr_handler_map[] = { |
| 190 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, |
| 191 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 192 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
| 193 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 194 | #endif |
| 195 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 196 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 197 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, |
| 198 | #endif |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 199 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
| 200 | }; |
| 201 | |
| 202 | const struct xattr_handler *f2fs_xattr_handlers[] = { |
| 203 | &f2fs_xattr_user_handler, |
| 204 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 205 | &posix_acl_access_xattr_handler, |
| 206 | &posix_acl_default_xattr_handler, |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 207 | #endif |
| 208 | &f2fs_xattr_trusted_handler, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 209 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 210 | &f2fs_xattr_security_handler, |
| 211 | #endif |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 212 | &f2fs_xattr_advise_handler, |
| 213 | NULL, |
| 214 | }; |
| 215 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 216 | static inline const struct xattr_handler *f2fs_xattr_handler(int index) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 217 | { |
| 218 | const struct xattr_handler *handler = NULL; |
| 219 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 220 | if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map)) |
| 221 | handler = f2fs_xattr_handler_map[index]; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 222 | return handler; |
| 223 | } |
| 224 | |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 225 | static struct f2fs_xattr_entry *__find_xattr(void *base_addr, |
| 226 | void *last_base_addr, int index, |
| 227 | size_t len, const char *name) |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 228 | { |
| 229 | struct f2fs_xattr_entry *entry; |
| 230 | |
| 231 | list_for_each_xattr(entry, base_addr) { |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 232 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
| 233 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) |
| 234 | return NULL; |
| 235 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 236 | if (entry->e_name_index != index) |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 237 | continue; |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 238 | if (entry->e_name_len != len) |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 239 | continue; |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 240 | if (!memcmp(entry->e_name, name, len)) |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | return entry; |
| 244 | } |
| 245 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 246 | static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode, |
| 247 | void *base_addr, void **last_addr, int index, |
| 248 | size_t len, const char *name) |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 249 | { |
| 250 | struct f2fs_xattr_entry *entry; |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 251 | unsigned int inline_size = inline_xattr_size(inode); |
Chao Yu | 2c28aba | 2019-03-05 19:32:26 +0800 | [diff] [blame] | 252 | void *max_addr = base_addr + inline_size; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 253 | |
| 254 | list_for_each_xattr(entry, base_addr) { |
Chao Yu | 2c28aba | 2019-03-05 19:32:26 +0800 | [diff] [blame] | 255 | if ((void *)entry + sizeof(__u32) > max_addr || |
| 256 | (void *)XATTR_NEXT_ENTRY(entry) > max_addr) { |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 257 | *last_addr = entry; |
| 258 | return NULL; |
| 259 | } |
| 260 | if (entry->e_name_index != index) |
| 261 | continue; |
| 262 | if (entry->e_name_len != len) |
| 263 | continue; |
| 264 | if (!memcmp(entry->e_name, name, len)) |
| 265 | break; |
| 266 | } |
Chao Yu | 2c28aba | 2019-03-05 19:32:26 +0800 | [diff] [blame] | 267 | |
| 268 | /* inline xattr header or entry across max inline xattr size */ |
| 269 | if (IS_XATTR_LAST_ENTRY(entry) && |
| 270 | (void *)entry + sizeof(__u32) > max_addr) { |
| 271 | *last_addr = entry; |
| 272 | return NULL; |
| 273 | } |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 274 | return entry; |
| 275 | } |
| 276 | |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 277 | static int read_inline_xattr(struct inode *inode, struct page *ipage, |
| 278 | void *txattr_addr) |
| 279 | { |
| 280 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 281 | unsigned int inline_size = inline_xattr_size(inode); |
| 282 | struct page *page = NULL; |
| 283 | void *inline_addr; |
| 284 | |
| 285 | if (ipage) { |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 286 | inline_addr = inline_xattr_addr(inode, ipage); |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 287 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 288 | page = f2fs_get_node_page(sbi, inode->i_ino); |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 289 | if (IS_ERR(page)) |
| 290 | return PTR_ERR(page); |
| 291 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 292 | inline_addr = inline_xattr_addr(inode, page); |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 293 | } |
| 294 | memcpy(txattr_addr, inline_addr, inline_size); |
| 295 | f2fs_put_page(page, 1); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Chao Yu | 6384069 | 2017-09-04 18:58:03 +0800 | [diff] [blame] | 300 | static int read_xattr_block(struct inode *inode, void *txattr_addr) |
| 301 | { |
| 302 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 303 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 304 | unsigned int inline_size = inline_xattr_size(inode); |
| 305 | struct page *xpage; |
| 306 | void *xattr_addr; |
| 307 | |
| 308 | /* The inode already has an extended attribute block. */ |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 309 | xpage = f2fs_get_node_page(sbi, xnid); |
Chao Yu | 6384069 | 2017-09-04 18:58:03 +0800 | [diff] [blame] | 310 | if (IS_ERR(xpage)) |
| 311 | return PTR_ERR(xpage); |
| 312 | |
| 313 | xattr_addr = page_address(xpage); |
| 314 | memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); |
| 315 | f2fs_put_page(xpage, 1); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 320 | static int lookup_all_xattrs(struct inode *inode, struct page *ipage, |
| 321 | unsigned int index, unsigned int len, |
| 322 | const char *name, struct f2fs_xattr_entry **xe, |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 323 | void **base_addr, int *base_size, |
| 324 | bool *is_inline) |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 325 | { |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 326 | void *cur_addr, *txattr_addr, *last_txattr_addr; |
| 327 | void *last_addr = NULL; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 328 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
Chao Yu | 89e9eab | 2017-03-23 13:38:25 +0800 | [diff] [blame] | 329 | unsigned int inline_size = inline_xattr_size(inode); |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 330 | int err = 0; |
| 331 | |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 332 | if (!xnid && !inline_size) |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 333 | return -ENODATA; |
| 334 | |
Chao Yu | ba3b583 | 2020-02-14 17:44:11 +0800 | [diff] [blame] | 335 | *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE; |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 336 | txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline); |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 337 | if (!txattr_addr) |
| 338 | return -ENOMEM; |
| 339 | |
Chao Yu | ba3b583 | 2020-02-14 17:44:11 +0800 | [diff] [blame] | 340 | last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode); |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 341 | |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 342 | /* read from inline xattr */ |
| 343 | if (inline_size) { |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 344 | err = read_inline_xattr(inode, ipage, txattr_addr); |
| 345 | if (err) |
| 346 | goto out; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 347 | |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 348 | *xe = __find_inline_xattr(inode, txattr_addr, &last_addr, |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 349 | index, len, name); |
Jaegeuk Kim | 64beba0 | 2018-12-26 19:54:07 -0800 | [diff] [blame] | 350 | if (*xe) { |
| 351 | *base_size = inline_size; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 352 | goto check; |
Jaegeuk Kim | 64beba0 | 2018-12-26 19:54:07 -0800 | [diff] [blame] | 353 | } |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* read from xattr node block */ |
| 357 | if (xnid) { |
Chao Yu | 6384069 | 2017-09-04 18:58:03 +0800 | [diff] [blame] | 358 | err = read_xattr_block(inode, txattr_addr); |
| 359 | if (err) |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 360 | goto out; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (last_addr) |
| 364 | cur_addr = XATTR_HDR(last_addr) - 1; |
| 365 | else |
| 366 | cur_addr = txattr_addr; |
| 367 | |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 368 | *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name); |
| 369 | if (!*xe) { |
Chao Yu | c83414a | 2019-06-20 11:36:15 +0800 | [diff] [blame] | 370 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", |
| 371 | inode->i_ino); |
| 372 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 373 | err = -EFSCORRUPTED; |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 374 | goto out; |
| 375 | } |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 376 | check: |
| 377 | if (IS_XATTR_LAST_ENTRY(*xe)) { |
| 378 | err = -ENODATA; |
| 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | *base_addr = txattr_addr; |
| 383 | return 0; |
| 384 | out: |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 385 | xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline); |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 386 | return err; |
| 387 | } |
| 388 | |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 389 | static int read_all_xattrs(struct inode *inode, struct page *ipage, |
| 390 | void **base_addr) |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 391 | { |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 392 | struct f2fs_xattr_header *header; |
Chao Yu | 89e9eab | 2017-03-23 13:38:25 +0800 | [diff] [blame] | 393 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 394 | unsigned int size = VALID_XATTR_BLOCK_SIZE; |
| 395 | unsigned int inline_size = inline_xattr_size(inode); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 396 | void *txattr_addr; |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 397 | int err; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 398 | |
Chao Yu | acbf054 | 2017-11-30 19:28:17 +0800 | [diff] [blame] | 399 | txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), |
| 400 | inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 401 | if (!txattr_addr) |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 402 | return -ENOMEM; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 403 | |
| 404 | /* read from inline xattr */ |
| 405 | if (inline_size) { |
Chao Yu | a5f433f | 2017-09-04 18:58:02 +0800 | [diff] [blame] | 406 | err = read_inline_xattr(inode, ipage, txattr_addr); |
| 407 | if (err) |
| 408 | goto fail; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* read from xattr node block */ |
Chao Yu | 89e9eab | 2017-03-23 13:38:25 +0800 | [diff] [blame] | 412 | if (xnid) { |
Chao Yu | 6384069 | 2017-09-04 18:58:03 +0800 | [diff] [blame] | 413 | err = read_xattr_block(inode, txattr_addr); |
| 414 | if (err) |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 415 | goto fail; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | header = XATTR_HDR(txattr_addr); |
| 419 | |
| 420 | /* never been allocated xattrs */ |
| 421 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { |
| 422 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); |
| 423 | header->h_refcount = cpu_to_le32(1); |
| 424 | } |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 425 | *base_addr = txattr_addr; |
| 426 | return 0; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 427 | fail: |
Chao Yu | 2a6a7e7 | 2019-03-05 17:52:33 +0800 | [diff] [blame] | 428 | kvfree(txattr_addr); |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 429 | return err; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, |
| 433 | void *txattr_addr, struct page *ipage) |
| 434 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 435 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 89e9eab | 2017-03-23 13:38:25 +0800 | [diff] [blame] | 436 | size_t inline_size = inline_xattr_size(inode); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 437 | struct page *in_page = NULL; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 438 | void *xattr_addr; |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 439 | void *inline_addr = NULL; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 440 | struct page *xpage; |
| 441 | nid_t new_nid = 0; |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 442 | int err = 0; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 443 | |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 444 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 445 | if (!f2fs_alloc_nid(sbi, &new_nid)) |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 446 | return -ENOSPC; |
| 447 | |
| 448 | /* write to inline xattr */ |
| 449 | if (inline_size) { |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 450 | if (ipage) { |
Chao Yu | 6afc662 | 2017-09-06 21:59:50 +0800 | [diff] [blame] | 451 | inline_addr = inline_xattr_addr(inode, ipage); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 452 | } else { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 453 | in_page = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 454 | if (IS_ERR(in_page)) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 455 | f2fs_alloc_nid_failed(sbi, new_nid); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 456 | return PTR_ERR(in_page); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 457 | } |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 458 | inline_addr = inline_xattr_addr(inode, in_page); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 459 | } |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 460 | |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 461 | f2fs_wait_on_page_writeback(ipage ? ipage : in_page, |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 462 | NODE, true, true); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 463 | /* no need to use xattr node block */ |
| 464 | if (hsize <= inline_size) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 465 | err = f2fs_truncate_xattr_node(inode); |
| 466 | f2fs_alloc_nid_failed(sbi, new_nid); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 467 | if (err) { |
| 468 | f2fs_put_page(in_page, 1); |
| 469 | return err; |
| 470 | } |
| 471 | memcpy(inline_addr, txattr_addr, inline_size); |
| 472 | set_page_dirty(ipage ? ipage : in_page); |
| 473 | goto in_page_out; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | /* write to xattr node block */ |
| 478 | if (F2FS_I(inode)->i_xattr_nid) { |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 479 | xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 480 | if (IS_ERR(xpage)) { |
Jaegeuk Kim | d620439 | 2017-12-28 17:47:19 -0800 | [diff] [blame] | 481 | err = PTR_ERR(xpage); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 482 | f2fs_alloc_nid_failed(sbi, new_nid); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 483 | goto in_page_out; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 484 | } |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 485 | f2fs_bug_on(sbi, new_nid); |
Chao Yu | bae0ee7 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 486 | f2fs_wait_on_page_writeback(xpage, NODE, true, true); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 487 | } else { |
| 488 | struct dnode_of_data dn; |
| 489 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 490 | xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 491 | if (IS_ERR(xpage)) { |
Jaegeuk Kim | d620439 | 2017-12-28 17:47:19 -0800 | [diff] [blame] | 492 | err = PTR_ERR(xpage); |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 493 | f2fs_alloc_nid_failed(sbi, new_nid); |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 494 | goto in_page_out; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 495 | } |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 496 | f2fs_alloc_nid_done(sbi, new_nid); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 497 | } |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 498 | xattr_addr = page_address(xpage); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 499 | |
Jaegeuk Kim | bf9c142 | 2017-10-16 15:05:16 -0700 | [diff] [blame] | 500 | if (inline_size) |
| 501 | memcpy(inline_addr, txattr_addr, inline_size); |
| 502 | memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); |
| 503 | |
| 504 | if (inline_size) |
| 505 | set_page_dirty(ipage ? ipage : in_page); |
| 506 | set_page_dirty(xpage); |
| 507 | |
| 508 | f2fs_put_page(xpage, 1); |
| 509 | in_page_out: |
| 510 | f2fs_put_page(in_page, 1); |
| 511 | return err; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 512 | } |
| 513 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 514 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
Jaegeuk Kim | bce8d11 | 2014-10-13 19:42:53 -0700 | [diff] [blame] | 515 | void *buffer, size_t buffer_size, struct page *ipage) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 516 | { |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 517 | struct f2fs_xattr_entry *entry = NULL; |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 518 | int error = 0; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 519 | unsigned int size, len; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 520 | void *base_addr = NULL; |
Jaegeuk Kim | 64beba0 | 2018-12-26 19:54:07 -0800 | [diff] [blame] | 521 | int base_size; |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 522 | bool is_inline; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 523 | |
| 524 | if (name == NULL) |
| 525 | return -EINVAL; |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 526 | |
| 527 | len = strlen(name); |
| 528 | if (len > F2FS_NAME_LEN) |
Chao Yu | 6e452d6 | 2014-03-22 14:59:45 +0800 | [diff] [blame] | 529 | return -ERANGE; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 530 | |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 531 | down_read(&F2FS_I(inode)->i_xattr_sem); |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 532 | error = lookup_all_xattrs(inode, ipage, index, len, name, |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 533 | &entry, &base_addr, &base_size, &is_inline); |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 534 | up_read(&F2FS_I(inode)->i_xattr_sem); |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 535 | if (error) |
| 536 | return error; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 537 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 538 | size = le16_to_cpu(entry->e_value_size); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 539 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 540 | if (buffer && size > buffer_size) { |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 541 | error = -ERANGE; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 542 | goto out; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | if (buffer) { |
| 546 | char *pval = entry->e_name + entry->e_name_len; |
Jaegeuk Kim | 64beba0 | 2018-12-26 19:54:07 -0800 | [diff] [blame] | 547 | |
| 548 | if (base_size - (pval - (char *)base_addr) < size) { |
| 549 | error = -ERANGE; |
| 550 | goto out; |
| 551 | } |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 552 | memcpy(buffer, pval, size); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 553 | } |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 554 | error = size; |
Chao Yu | ba38c27 | 2017-01-24 20:39:51 +0800 | [diff] [blame] | 555 | out: |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 556 | xattr_free(F2FS_I_SB(inode), base_addr, is_inline); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 557 | return error; |
| 558 | } |
| 559 | |
| 560 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 561 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 562 | struct inode *inode = d_inode(dentry); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 563 | struct f2fs_xattr_entry *entry; |
Randall Huang | 688078e | 2019-10-18 14:56:22 +0800 | [diff] [blame] | 564 | void *base_addr, *last_base_addr; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 565 | int error = 0; |
| 566 | size_t rest = buffer_size; |
| 567 | |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 568 | down_read(&F2FS_I(inode)->i_xattr_sem); |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 569 | error = read_all_xattrs(inode, NULL, &base_addr); |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 570 | up_read(&F2FS_I(inode)->i_xattr_sem); |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 571 | if (error) |
| 572 | return error; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 573 | |
Chao Yu | ba3b583 | 2020-02-14 17:44:11 +0800 | [diff] [blame] | 574 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
Randall Huang | 688078e | 2019-10-18 14:56:22 +0800 | [diff] [blame] | 575 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 576 | list_for_each_xattr(entry, base_addr) { |
| 577 | const struct xattr_handler *handler = |
| 578 | f2fs_xattr_handler(entry->e_name_index); |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 579 | const char *prefix; |
| 580 | size_t prefix_len; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 581 | size_t size; |
| 582 | |
Randall Huang | 688078e | 2019-10-18 14:56:22 +0800 | [diff] [blame] | 583 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
| 584 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { |
| 585 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", |
| 586 | inode->i_ino); |
| 587 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); |
| 588 | error = -EFSCORRUPTED; |
| 589 | goto cleanup; |
| 590 | } |
| 591 | |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 592 | if (!handler || (handler->list && !handler->list(dentry))) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 593 | continue; |
| 594 | |
Gao Xiang | eecfa42 | 2019-01-25 20:11:39 +0800 | [diff] [blame] | 595 | prefix = xattr_prefix(handler); |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 596 | prefix_len = strlen(prefix); |
| 597 | size = prefix_len + entry->e_name_len + 1; |
| 598 | if (buffer) { |
| 599 | if (size > rest) { |
| 600 | error = -ERANGE; |
| 601 | goto cleanup; |
| 602 | } |
| 603 | memcpy(buffer, prefix, prefix_len); |
| 604 | buffer += prefix_len; |
| 605 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 606 | buffer += entry->e_name_len; |
| 607 | *buffer++ = 0; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 608 | } |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 609 | rest -= size; |
| 610 | } |
| 611 | error = buffer_size - rest; |
| 612 | cleanup: |
Chao Yu | 2a6a7e7 | 2019-03-05 17:52:33 +0800 | [diff] [blame] | 613 | kvfree(base_addr); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 614 | return error; |
| 615 | } |
| 616 | |
Kinglong Mee | 5f35a2c | 2017-02-25 19:23:27 +0800 | [diff] [blame] | 617 | static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, |
| 618 | const void *value, size_t size) |
| 619 | { |
| 620 | void *pval = entry->e_name + entry->e_name_len; |
Jaegeuk Kim | b71dead | 2017-03-10 09:36:10 -0800 | [diff] [blame] | 621 | |
| 622 | return (le16_to_cpu(entry->e_value_size) == size) && |
| 623 | !memcmp(pval, value, size); |
Kinglong Mee | 5f35a2c | 2017-02-25 19:23:27 +0800 | [diff] [blame] | 624 | } |
| 625 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 626 | static int __f2fs_setxattr(struct inode *inode, int index, |
| 627 | const char *name, const void *value, size_t size, |
Jaegeuk Kim | c02745e | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 628 | struct page *ipage, int flags) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 629 | { |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 630 | struct f2fs_xattr_entry *here, *last; |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 631 | void *base_addr, *last_base_addr; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 632 | int found, newsize; |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 633 | size_t len; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 634 | __u32 new_hsize; |
Tiezhu Yang | a0995af | 2016-06-28 07:27:59 +0800 | [diff] [blame] | 635 | int error = 0; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 636 | |
| 637 | if (name == NULL) |
| 638 | return -EINVAL; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 639 | |
| 640 | if (value == NULL) |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 641 | size = 0; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 642 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 643 | len = strlen(name); |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 644 | |
Chao Yu | 037fe70 | 2015-07-13 17:45:19 +0800 | [diff] [blame] | 645 | if (len > F2FS_NAME_LEN) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 646 | return -ERANGE; |
| 647 | |
Chao Yu | 037fe70 | 2015-07-13 17:45:19 +0800 | [diff] [blame] | 648 | if (size > MAX_VALUE_LEN(inode)) |
| 649 | return -E2BIG; |
| 650 | |
Chao Yu | 8669696 | 2016-09-18 23:30:04 +0800 | [diff] [blame] | 651 | error = read_all_xattrs(inode, ipage, &base_addr); |
| 652 | if (error) |
| 653 | return error; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 654 | |
Chao Yu | ba3b583 | 2020-02-14 17:44:11 +0800 | [diff] [blame] | 655 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 656 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 657 | /* find entry with wanted name. */ |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 658 | here = __find_xattr(base_addr, last_base_addr, index, len, name); |
| 659 | if (!here) { |
Chao Yu | c83414a | 2019-06-20 11:36:15 +0800 | [diff] [blame] | 660 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", |
| 661 | inode->i_ino); |
| 662 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); |
Chao Yu | 10f966b | 2019-06-20 11:36:14 +0800 | [diff] [blame] | 663 | error = -EFSCORRUPTED; |
Randall Huang | 2777e65 | 2019-04-11 16:26:46 +0800 | [diff] [blame] | 664 | goto exit; |
| 665 | } |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 666 | |
Jaegeuk Kim | dd9cfe2 | 2013-08-13 10:13:55 +0900 | [diff] [blame] | 667 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 668 | |
Kinglong Mee | 5f35a2c | 2017-02-25 19:23:27 +0800 | [diff] [blame] | 669 | if (found) { |
| 670 | if ((flags & XATTR_CREATE)) { |
| 671 | error = -EEXIST; |
| 672 | goto exit; |
| 673 | } |
| 674 | |
Daeho Jeong | b2c4692 | 2018-01-20 15:46:33 +0800 | [diff] [blame] | 675 | if (value && f2fs_xattr_value_same(here, value, size)) |
Kinglong Mee | 5f35a2c | 2017-02-25 19:23:27 +0800 | [diff] [blame] | 676 | goto exit; |
| 677 | } else if ((flags & XATTR_REPLACE)) { |
Jaegeuk Kim | 916decb | 2014-04-23 12:28:18 +0900 | [diff] [blame] | 678 | error = -ENODATA; |
| 679 | goto exit; |
Jaegeuk Kim | 916decb | 2014-04-23 12:28:18 +0900 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | last = here; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 683 | while (!IS_XATTR_LAST_ENTRY(last)) |
| 684 | last = XATTR_NEXT_ENTRY(last); |
| 685 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 686 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 687 | |
| 688 | /* 1. Check space */ |
| 689 | if (value) { |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 690 | int free; |
| 691 | /* |
| 692 | * If value is NULL, it is remove operation. |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 693 | * In case of update operation, we calculate free. |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 694 | */ |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 695 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 696 | if (found) |
Chao Yu | cc3de6a | 2013-10-29 14:17:05 +0800 | [diff] [blame] | 697 | free = free + ENTRY_SIZE(here); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 698 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 699 | if (unlikely(free < newsize)) { |
Jaegeuk Kim | 58457f1 | 2016-04-12 11:52:30 -0700 | [diff] [blame] | 700 | error = -E2BIG; |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 701 | goto exit; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | /* 2. Remove old entry */ |
| 706 | if (found) { |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 707 | /* |
| 708 | * If entry is found, remove old entry. |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 709 | * If not found, remove operation is not needed. |
| 710 | */ |
| 711 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); |
| 712 | int oldsize = ENTRY_SIZE(here); |
| 713 | |
| 714 | memmove(here, next, (char *)last - (char *)next); |
| 715 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); |
| 716 | memset(last, 0, oldsize); |
| 717 | } |
| 718 | |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 719 | new_hsize = (char *)last - (char *)base_addr; |
| 720 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 721 | /* 3. Write new entry */ |
| 722 | if (value) { |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 723 | char *pval; |
| 724 | /* |
| 725 | * Before we come here, old entry is removed. |
| 726 | * We just write new entry. |
| 727 | */ |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 728 | last->e_name_index = index; |
| 729 | last->e_name_len = len; |
| 730 | memcpy(last->e_name, name, len); |
| 731 | pval = last->e_name + len; |
| 732 | memcpy(pval, value, size); |
| 733 | last->e_value_size = cpu_to_le16(size); |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 734 | new_hsize += newsize; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 735 | } |
| 736 | |
Jaegeuk Kim | 65985d9 | 2013-08-14 21:57:27 +0900 | [diff] [blame] | 737 | error = write_all_xattrs(inode, new_hsize, base_addr, ipage); |
| 738 | if (error) |
| 739 | goto exit; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 740 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 741 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { |
| 742 | inode->i_mode = F2FS_I(inode)->i_acl_mode; |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 743 | inode->i_ctime = current_time(inode); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 744 | clear_inode_flag(inode, FI_ACL_MODE); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 745 | } |
Jaegeuk Kim | f424f66 | 2015-04-20 15:19:06 -0700 | [diff] [blame] | 746 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
| 747 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) |
| 748 | f2fs_set_encrypted_inode(inode); |
Jaegeuk Kim | 7c45729 | 2016-10-14 11:51:23 -0700 | [diff] [blame] | 749 | f2fs_mark_inode_dirty_sync(inode, true); |
Jaegeuk Kim | bbf156f | 2016-08-29 18:23:45 -0700 | [diff] [blame] | 750 | if (!error && S_ISDIR(inode->i_mode)) |
| 751 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 752 | exit: |
Chao Yu | 2a6a7e7 | 2019-03-05 17:52:33 +0800 | [diff] [blame] | 753 | kvfree(base_addr); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 754 | return error; |
| 755 | } |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 756 | |
Jaegeuk Kim | e112326 | 2014-04-23 12:17:25 +0900 | [diff] [blame] | 757 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
| 758 | const void *value, size_t size, |
Jaegeuk Kim | c02745e | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 759 | struct page *ipage, int flags) |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 760 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 761 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 762 | int err; |
| 763 | |
Chao Yu | a25c2cd | 2019-07-22 17:57:06 +0800 | [diff] [blame] | 764 | if (unlikely(f2fs_cp_error(sbi))) |
| 765 | return -EIO; |
Chao Yu | 00e09c0 | 2019-08-23 17:58:36 +0800 | [diff] [blame] | 766 | if (!f2fs_is_checkpoint_ready(sbi)) |
| 767 | return -ENOSPC; |
Chao Yu | 955ebcd | 2019-07-22 17:57:05 +0800 | [diff] [blame] | 768 | |
Jaegeuk Kim | d8d1389 | 2017-10-23 23:50:15 +0200 | [diff] [blame] | 769 | err = dquot_initialize(inode); |
| 770 | if (err) |
| 771 | return err; |
| 772 | |
Chao Yu | 4d57b86 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 773 | /* this case is only from f2fs_init_inode_metadata */ |
Jaegeuk Kim | d631abd | 2014-06-01 23:24:30 +0900 | [diff] [blame] | 774 | if (ipage) |
| 775 | return __f2fs_setxattr(inode, index, name, value, |
| 776 | size, ipage, flags); |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 777 | f2fs_balance_fs(sbi, true); |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 778 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 779 | f2fs_lock_op(sbi); |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 780 | down_write(&F2FS_I(inode)->i_xattr_sem); |
Jaegeuk Kim | c02745e | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 781 | err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); |
Yunlei He | 27161f1 | 2017-09-07 10:40:54 +0800 | [diff] [blame] | 782 | up_write(&F2FS_I(inode)->i_xattr_sem); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 783 | f2fs_unlock_op(sbi); |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 784 | |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 785 | f2fs_update_time(sbi, REQ_TIME); |
Russ Knize | 52ab956 | 2013-09-24 15:49:23 -0500 | [diff] [blame] | 786 | return err; |
| 787 | } |
Chao Yu | a999150 | 2020-02-25 18:17:10 +0800 | [diff] [blame] | 788 | |
| 789 | int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) |
| 790 | { |
| 791 | dev_t dev = sbi->sb->s_bdev->bd_dev; |
| 792 | char slab_name[32]; |
| 793 | |
| 794 | sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev)); |
| 795 | |
| 796 | sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size * |
| 797 | sizeof(__le32) + XATTR_PADDING_SIZE; |
| 798 | |
| 799 | sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name, |
| 800 | sbi->inline_xattr_slab_size); |
| 801 | if (!sbi->inline_xattr_slab) |
| 802 | return -ENOMEM; |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) |
| 808 | { |
| 809 | kmem_cache_destroy(sbi->inline_xattr_slab); |
| 810 | } |