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