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