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