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