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