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