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> |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 24 | #include "f2fs.h" |
| 25 | #include "xattr.h" |
| 26 | |
| 27 | static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list, |
| 28 | size_t list_size, const char *name, size_t name_len, int type) |
| 29 | { |
| 30 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 31 | int total_len, prefix_len = 0; |
| 32 | const char *prefix = NULL; |
| 33 | |
| 34 | switch (type) { |
| 35 | case F2FS_XATTR_INDEX_USER: |
| 36 | if (!test_opt(sbi, XATTR_USER)) |
| 37 | return -EOPNOTSUPP; |
| 38 | prefix = XATTR_USER_PREFIX; |
| 39 | prefix_len = XATTR_USER_PREFIX_LEN; |
| 40 | break; |
| 41 | case F2FS_XATTR_INDEX_TRUSTED: |
| 42 | if (!capable(CAP_SYS_ADMIN)) |
| 43 | return -EPERM; |
| 44 | prefix = XATTR_TRUSTED_PREFIX; |
| 45 | prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
| 46 | break; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 47 | case F2FS_XATTR_INDEX_SECURITY: |
| 48 | prefix = XATTR_SECURITY_PREFIX; |
| 49 | prefix_len = XATTR_SECURITY_PREFIX_LEN; |
| 50 | break; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 51 | default: |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | total_len = prefix_len + name_len + 1; |
| 56 | if (list && total_len <= list_size) { |
| 57 | memcpy(list, prefix, prefix_len); |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 58 | memcpy(list + prefix_len, name, name_len); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 59 | list[prefix_len + name_len] = '\0'; |
| 60 | } |
| 61 | return total_len; |
| 62 | } |
| 63 | |
| 64 | static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name, |
| 65 | void *buffer, size_t size, int type) |
| 66 | { |
| 67 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 68 | |
| 69 | switch (type) { |
| 70 | case F2FS_XATTR_INDEX_USER: |
| 71 | if (!test_opt(sbi, XATTR_USER)) |
| 72 | return -EOPNOTSUPP; |
| 73 | break; |
| 74 | case F2FS_XATTR_INDEX_TRUSTED: |
| 75 | if (!capable(CAP_SYS_ADMIN)) |
| 76 | return -EPERM; |
| 77 | break; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 78 | case F2FS_XATTR_INDEX_SECURITY: |
| 79 | break; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 80 | default: |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | if (strcmp(name, "") == 0) |
| 84 | return -EINVAL; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 85 | return f2fs_getxattr(dentry->d_inode, type, name, buffer, size); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name, |
| 89 | const void *value, size_t size, int flags, int type) |
| 90 | { |
| 91 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 92 | |
| 93 | switch (type) { |
| 94 | case F2FS_XATTR_INDEX_USER: |
| 95 | if (!test_opt(sbi, XATTR_USER)) |
| 96 | return -EOPNOTSUPP; |
| 97 | break; |
| 98 | case F2FS_XATTR_INDEX_TRUSTED: |
| 99 | if (!capable(CAP_SYS_ADMIN)) |
| 100 | return -EPERM; |
| 101 | break; |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 102 | case F2FS_XATTR_INDEX_SECURITY: |
| 103 | break; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 104 | default: |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | if (strcmp(name, "") == 0) |
| 108 | return -EINVAL; |
| 109 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 110 | return f2fs_setxattr(dentry->d_inode, type, name, value, size, NULL); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 111 | } |
| 112 | |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 113 | static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list, |
| 114 | size_t list_size, const char *name, size_t name_len, int type) |
| 115 | { |
| 116 | const char *xname = F2FS_SYSTEM_ADVISE_PREFIX; |
| 117 | size_t size; |
| 118 | |
| 119 | if (type != F2FS_XATTR_INDEX_ADVISE) |
| 120 | return 0; |
| 121 | |
| 122 | size = strlen(xname) + 1; |
| 123 | if (list && size <= list_size) |
| 124 | memcpy(list, xname, size); |
| 125 | return size; |
| 126 | } |
| 127 | |
| 128 | static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name, |
| 129 | void *buffer, size_t size, int type) |
| 130 | { |
| 131 | struct inode *inode = dentry->d_inode; |
| 132 | |
| 133 | if (strcmp(name, "") != 0) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | *((char *)buffer) = F2FS_I(inode)->i_advise; |
| 137 | return sizeof(char); |
| 138 | } |
| 139 | |
| 140 | static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name, |
| 141 | const void *value, size_t size, int flags, int type) |
| 142 | { |
| 143 | struct inode *inode = dentry->d_inode; |
| 144 | |
| 145 | if (strcmp(name, "") != 0) |
| 146 | return -EINVAL; |
| 147 | if (!inode_owner_or_capable(inode)) |
| 148 | return -EPERM; |
| 149 | if (value == NULL) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | F2FS_I(inode)->i_advise |= *(char *)value; |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 156 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 157 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 158 | void *page) |
| 159 | { |
| 160 | const struct xattr *xattr; |
| 161 | int err = 0; |
| 162 | |
| 163 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
| 164 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
| 165 | xattr->name, xattr->value, |
| 166 | xattr->value_len, (struct page *)page); |
| 167 | if (err < 0) |
| 168 | break; |
| 169 | } |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | int f2fs_init_security(struct inode *inode, struct inode *dir, |
| 174 | const struct qstr *qstr, struct page *ipage) |
| 175 | { |
| 176 | return security_inode_init_security(inode, dir, qstr, |
| 177 | &f2fs_initxattrs, ipage); |
| 178 | } |
| 179 | #endif |
| 180 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 181 | const struct xattr_handler f2fs_xattr_user_handler = { |
| 182 | .prefix = XATTR_USER_PREFIX, |
| 183 | .flags = F2FS_XATTR_INDEX_USER, |
| 184 | .list = f2fs_xattr_generic_list, |
| 185 | .get = f2fs_xattr_generic_get, |
| 186 | .set = f2fs_xattr_generic_set, |
| 187 | }; |
| 188 | |
| 189 | const struct xattr_handler f2fs_xattr_trusted_handler = { |
| 190 | .prefix = XATTR_TRUSTED_PREFIX, |
| 191 | .flags = F2FS_XATTR_INDEX_TRUSTED, |
| 192 | .list = f2fs_xattr_generic_list, |
| 193 | .get = f2fs_xattr_generic_get, |
| 194 | .set = f2fs_xattr_generic_set, |
| 195 | }; |
| 196 | |
Jaegeuk Kim | 573ea5f | 2012-11-30 17:32:08 +0900 | [diff] [blame] | 197 | const struct xattr_handler f2fs_xattr_advise_handler = { |
| 198 | .prefix = F2FS_SYSTEM_ADVISE_PREFIX, |
| 199 | .flags = F2FS_XATTR_INDEX_ADVISE, |
| 200 | .list = f2fs_xattr_advise_list, |
| 201 | .get = f2fs_xattr_advise_get, |
| 202 | .set = f2fs_xattr_advise_set, |
| 203 | }; |
| 204 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 205 | const struct xattr_handler f2fs_xattr_security_handler = { |
| 206 | .prefix = XATTR_SECURITY_PREFIX, |
| 207 | .flags = F2FS_XATTR_INDEX_SECURITY, |
| 208 | .list = f2fs_xattr_generic_list, |
| 209 | .get = f2fs_xattr_generic_get, |
| 210 | .set = f2fs_xattr_generic_set, |
| 211 | }; |
| 212 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 213 | static const struct xattr_handler *f2fs_xattr_handler_map[] = { |
| 214 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, |
| 215 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 216 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler, |
| 217 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler, |
| 218 | #endif |
| 219 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 220 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 221 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, |
| 222 | #endif |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 223 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
| 224 | }; |
| 225 | |
| 226 | const struct xattr_handler *f2fs_xattr_handlers[] = { |
| 227 | &f2fs_xattr_user_handler, |
| 228 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 229 | &f2fs_xattr_acl_access_handler, |
| 230 | &f2fs_xattr_acl_default_handler, |
| 231 | #endif |
| 232 | &f2fs_xattr_trusted_handler, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 233 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 234 | &f2fs_xattr_security_handler, |
| 235 | #endif |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 236 | &f2fs_xattr_advise_handler, |
| 237 | NULL, |
| 238 | }; |
| 239 | |
| 240 | static inline const struct xattr_handler *f2fs_xattr_handler(int name_index) |
| 241 | { |
| 242 | const struct xattr_handler *handler = NULL; |
| 243 | |
| 244 | if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map)) |
| 245 | handler = f2fs_xattr_handler_map[name_index]; |
| 246 | return handler; |
| 247 | } |
| 248 | |
| 249 | int f2fs_getxattr(struct inode *inode, int name_index, const char *name, |
| 250 | void *buffer, size_t buffer_size) |
| 251 | { |
| 252 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 253 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 254 | struct f2fs_xattr_entry *entry; |
| 255 | struct page *page; |
| 256 | void *base_addr; |
| 257 | int error = 0, found = 0; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 258 | size_t value_len, name_len; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 259 | |
| 260 | if (name == NULL) |
| 261 | return -EINVAL; |
| 262 | name_len = strlen(name); |
| 263 | |
| 264 | if (!fi->i_xattr_nid) |
| 265 | return -ENODATA; |
| 266 | |
| 267 | page = get_node_page(sbi, fi->i_xattr_nid); |
Jason Hrycay | 1e03e38 | 2013-05-31 12:45:11 -0500 | [diff] [blame] | 268 | if (IS_ERR(page)) |
| 269 | return PTR_ERR(page); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 270 | base_addr = page_address(page); |
| 271 | |
| 272 | list_for_each_xattr(entry, base_addr) { |
| 273 | if (entry->e_name_index != name_index) |
| 274 | continue; |
| 275 | if (entry->e_name_len != name_len) |
| 276 | continue; |
| 277 | if (!memcmp(entry->e_name, name, name_len)) { |
| 278 | found = 1; |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | if (!found) { |
| 283 | error = -ENODATA; |
| 284 | goto cleanup; |
| 285 | } |
| 286 | |
| 287 | value_len = le16_to_cpu(entry->e_value_size); |
| 288 | |
| 289 | if (buffer && value_len > buffer_size) { |
| 290 | error = -ERANGE; |
| 291 | goto cleanup; |
| 292 | } |
| 293 | |
| 294 | if (buffer) { |
| 295 | char *pval = entry->e_name + entry->e_name_len; |
| 296 | memcpy(buffer, pval, value_len); |
| 297 | } |
| 298 | error = value_len; |
| 299 | |
| 300 | cleanup: |
| 301 | f2fs_put_page(page, 1); |
| 302 | return error; |
| 303 | } |
| 304 | |
| 305 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 306 | { |
| 307 | struct inode *inode = dentry->d_inode; |
| 308 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 309 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 310 | struct f2fs_xattr_entry *entry; |
| 311 | struct page *page; |
| 312 | void *base_addr; |
| 313 | int error = 0; |
| 314 | size_t rest = buffer_size; |
| 315 | |
| 316 | if (!fi->i_xattr_nid) |
| 317 | return 0; |
| 318 | |
| 319 | page = get_node_page(sbi, fi->i_xattr_nid); |
Jason Hrycay | 1e03e38 | 2013-05-31 12:45:11 -0500 | [diff] [blame] | 320 | if (IS_ERR(page)) |
| 321 | return PTR_ERR(page); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 322 | base_addr = page_address(page); |
| 323 | |
| 324 | list_for_each_xattr(entry, base_addr) { |
| 325 | const struct xattr_handler *handler = |
| 326 | f2fs_xattr_handler(entry->e_name_index); |
| 327 | size_t size; |
| 328 | |
| 329 | if (!handler) |
| 330 | continue; |
| 331 | |
| 332 | size = handler->list(dentry, buffer, rest, entry->e_name, |
| 333 | entry->e_name_len, handler->flags); |
| 334 | if (buffer && size > rest) { |
| 335 | error = -ERANGE; |
| 336 | goto cleanup; |
| 337 | } |
| 338 | |
| 339 | if (buffer) |
| 340 | buffer += size; |
| 341 | rest -= size; |
| 342 | } |
| 343 | error = buffer_size - rest; |
| 344 | cleanup: |
| 345 | f2fs_put_page(page, 1); |
| 346 | return error; |
| 347 | } |
| 348 | |
| 349 | int f2fs_setxattr(struct inode *inode, int name_index, const char *name, |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 350 | const void *value, size_t value_len, struct page *ipage) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 351 | { |
| 352 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 353 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 354 | struct f2fs_xattr_header *header = NULL; |
| 355 | struct f2fs_xattr_entry *here, *last; |
| 356 | struct page *page; |
| 357 | void *base_addr; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 358 | int error, found, free, newsize; |
| 359 | size_t name_len; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 360 | char *pval; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 361 | int ilock; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 362 | |
| 363 | if (name == NULL) |
| 364 | return -EINVAL; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 365 | |
| 366 | if (value == NULL) |
| 367 | value_len = 0; |
| 368 | |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 369 | name_len = strlen(name); |
| 370 | |
| 371 | if (name_len > F2FS_NAME_LEN || value_len > MAX_VALUE_LEN) |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 372 | return -ERANGE; |
| 373 | |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 374 | f2fs_balance_fs(sbi); |
| 375 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 376 | ilock = mutex_lock_op(sbi); |
| 377 | |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 378 | if (!fi->i_xattr_nid) { |
| 379 | /* Allocate new attribute block */ |
| 380 | struct dnode_of_data dn; |
| 381 | |
| 382 | if (!alloc_nid(sbi, &fi->i_xattr_nid)) { |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 383 | error = -ENOSPC; |
| 384 | goto exit; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 385 | } |
| 386 | set_new_dnode(&dn, inode, NULL, NULL, fi->i_xattr_nid); |
| 387 | mark_inode_dirty(inode); |
| 388 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 389 | page = new_node_page(&dn, XATTR_NODE_OFFSET, ipage); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 390 | if (IS_ERR(page)) { |
| 391 | alloc_nid_failed(sbi, fi->i_xattr_nid); |
| 392 | fi->i_xattr_nid = 0; |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 393 | error = PTR_ERR(page); |
| 394 | goto exit; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | alloc_nid_done(sbi, fi->i_xattr_nid); |
| 398 | base_addr = page_address(page); |
| 399 | header = XATTR_HDR(base_addr); |
| 400 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); |
| 401 | header->h_refcount = cpu_to_le32(1); |
| 402 | } else { |
| 403 | /* The inode already has an extended attribute block. */ |
| 404 | page = get_node_page(sbi, fi->i_xattr_nid); |
| 405 | if (IS_ERR(page)) { |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 406 | error = PTR_ERR(page); |
| 407 | goto exit; |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | base_addr = page_address(page); |
| 411 | header = XATTR_HDR(base_addr); |
| 412 | } |
| 413 | |
| 414 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { |
| 415 | error = -EIO; |
| 416 | goto cleanup; |
| 417 | } |
| 418 | |
| 419 | /* find entry with wanted name. */ |
| 420 | found = 0; |
| 421 | list_for_each_xattr(here, base_addr) { |
| 422 | if (here->e_name_index != name_index) |
| 423 | continue; |
| 424 | if (here->e_name_len != name_len) |
| 425 | continue; |
| 426 | if (!memcmp(here->e_name, name, name_len)) { |
| 427 | found = 1; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | last = here; |
| 433 | |
| 434 | while (!IS_XATTR_LAST_ENTRY(last)) |
| 435 | last = XATTR_NEXT_ENTRY(last); |
| 436 | |
| 437 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + |
| 438 | name_len + value_len); |
| 439 | |
| 440 | /* 1. Check space */ |
| 441 | if (value) { |
| 442 | /* If value is NULL, it is remove operation. |
| 443 | * In case of update operation, we caculate free. |
| 444 | */ |
| 445 | free = MIN_OFFSET - ((char *)last - (char *)header); |
| 446 | if (found) |
| 447 | free = free - ENTRY_SIZE(here); |
| 448 | |
| 449 | if (free < newsize) { |
| 450 | error = -ENOSPC; |
| 451 | goto cleanup; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* 2. Remove old entry */ |
| 456 | if (found) { |
| 457 | /* If entry is found, remove old entry. |
| 458 | * If not found, remove operation is not needed. |
| 459 | */ |
| 460 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); |
| 461 | int oldsize = ENTRY_SIZE(here); |
| 462 | |
| 463 | memmove(here, next, (char *)last - (char *)next); |
| 464 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); |
| 465 | memset(last, 0, oldsize); |
| 466 | } |
| 467 | |
| 468 | /* 3. Write new entry */ |
| 469 | if (value) { |
| 470 | /* Before we come here, old entry is removed. |
| 471 | * We just write new entry. */ |
| 472 | memset(last, 0, newsize); |
| 473 | last->e_name_index = name_index; |
| 474 | last->e_name_len = name_len; |
| 475 | memcpy(last->e_name, name, name_len); |
| 476 | pval = last->e_name + name_len; |
| 477 | memcpy(pval, value, value_len); |
| 478 | last->e_value_size = cpu_to_le16(value_len); |
| 479 | } |
| 480 | |
| 481 | set_page_dirty(page); |
| 482 | f2fs_put_page(page, 1); |
| 483 | |
| 484 | if (is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 485 | inode->i_mode = fi->i_acl_mode; |
| 486 | inode->i_ctime = CURRENT_TIME; |
| 487 | clear_inode_flag(fi, FI_ACL_MODE); |
| 488 | } |
Jaegeuk Kim | e518ff8 | 2013-08-09 14:46:15 +0900 | [diff] [blame] | 489 | |
| 490 | /* store checkpoint version for conducting checkpoint during fsync */ |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame^] | 491 | fi->xattr_ver = cur_cp_version(F2FS_CKPT(sbi)); |
Jaegeuk Kim | e518ff8 | 2013-08-09 14:46:15 +0900 | [diff] [blame] | 492 | |
Jaegeuk Kim | 8ae8f16 | 2013-06-03 19:46:19 +0900 | [diff] [blame] | 493 | if (ipage) |
| 494 | update_inode(inode, ipage); |
| 495 | else |
| 496 | update_inode_page(inode); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 497 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 498 | |
| 499 | return 0; |
| 500 | cleanup: |
| 501 | f2fs_put_page(page, 1); |
Namjae Jeon | 7c90977 | 2013-03-17 17:26:39 +0900 | [diff] [blame] | 502 | exit: |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 503 | mutex_unlock_op(sbi, ilock); |
Jaegeuk Kim | af48b85 | 2012-11-02 17:12:17 +0900 | [diff] [blame] | 504 | return error; |
| 505 | } |