0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/rwsem.h>
0019 #include <linux/f2fs_fs.h>
0020 #include <linux/security.h>
0021 #include <linux/posix_acl_xattr.h>
0022 #include "f2fs.h"
0023 #include "xattr.h"
0024 #include "segment.h"
0025
0026 static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
0027 {
0028 if (likely(size == sbi->inline_xattr_slab_size)) {
0029 *is_inline = true;
0030 return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab,
0031 GFP_F2FS_ZERO, false, sbi);
0032 }
0033 *is_inline = false;
0034 return f2fs_kzalloc(sbi, size, GFP_NOFS);
0035 }
0036
0037 static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
0038 bool is_inline)
0039 {
0040 if (is_inline)
0041 kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
0042 else
0043 kfree(xattr_addr);
0044 }
0045
0046 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
0047 struct dentry *unused, struct inode *inode,
0048 const char *name, void *buffer, size_t size)
0049 {
0050 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
0051
0052 switch (handler->flags) {
0053 case F2FS_XATTR_INDEX_USER:
0054 if (!test_opt(sbi, XATTR_USER))
0055 return -EOPNOTSUPP;
0056 break;
0057 case F2FS_XATTR_INDEX_TRUSTED:
0058 case F2FS_XATTR_INDEX_SECURITY:
0059 break;
0060 default:
0061 return -EINVAL;
0062 }
0063 return f2fs_getxattr(inode, handler->flags, name,
0064 buffer, size, NULL);
0065 }
0066
0067 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
0068 struct user_namespace *mnt_userns,
0069 struct dentry *unused, struct inode *inode,
0070 const char *name, const void *value,
0071 size_t size, int flags)
0072 {
0073 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
0074
0075 switch (handler->flags) {
0076 case F2FS_XATTR_INDEX_USER:
0077 if (!test_opt(sbi, XATTR_USER))
0078 return -EOPNOTSUPP;
0079 break;
0080 case F2FS_XATTR_INDEX_TRUSTED:
0081 case F2FS_XATTR_INDEX_SECURITY:
0082 break;
0083 default:
0084 return -EINVAL;
0085 }
0086 return f2fs_setxattr(inode, handler->flags, name,
0087 value, size, NULL, flags);
0088 }
0089
0090 static bool f2fs_xattr_user_list(struct dentry *dentry)
0091 {
0092 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
0093
0094 return test_opt(sbi, XATTR_USER);
0095 }
0096
0097 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
0098 {
0099 return capable(CAP_SYS_ADMIN);
0100 }
0101
0102 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
0103 struct dentry *unused, struct inode *inode,
0104 const char *name, void *buffer, size_t size)
0105 {
0106 if (buffer)
0107 *((char *)buffer) = F2FS_I(inode)->i_advise;
0108 return sizeof(char);
0109 }
0110
0111 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
0112 struct user_namespace *mnt_userns,
0113 struct dentry *unused, struct inode *inode,
0114 const char *name, const void *value,
0115 size_t size, int flags)
0116 {
0117 unsigned char old_advise = F2FS_I(inode)->i_advise;
0118 unsigned char new_advise;
0119
0120 if (!inode_owner_or_capable(&init_user_ns, inode))
0121 return -EPERM;
0122 if (value == NULL)
0123 return -EINVAL;
0124
0125 new_advise = *(char *)value;
0126 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
0127 return -EINVAL;
0128
0129 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
0130 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
0131
0132 F2FS_I(inode)->i_advise = new_advise;
0133 f2fs_mark_inode_dirty_sync(inode, true);
0134 return 0;
0135 }
0136
0137 #ifdef CONFIG_F2FS_FS_SECURITY
0138 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
0139 void *page)
0140 {
0141 const struct xattr *xattr;
0142 int err = 0;
0143
0144 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
0145 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
0146 xattr->name, xattr->value,
0147 xattr->value_len, (struct page *)page, 0);
0148 if (err < 0)
0149 break;
0150 }
0151 return err;
0152 }
0153
0154 int f2fs_init_security(struct inode *inode, struct inode *dir,
0155 const struct qstr *qstr, struct page *ipage)
0156 {
0157 return security_inode_init_security(inode, dir, qstr,
0158 &f2fs_initxattrs, ipage);
0159 }
0160 #endif
0161
0162 const struct xattr_handler f2fs_xattr_user_handler = {
0163 .prefix = XATTR_USER_PREFIX,
0164 .flags = F2FS_XATTR_INDEX_USER,
0165 .list = f2fs_xattr_user_list,
0166 .get = f2fs_xattr_generic_get,
0167 .set = f2fs_xattr_generic_set,
0168 };
0169
0170 const struct xattr_handler f2fs_xattr_trusted_handler = {
0171 .prefix = XATTR_TRUSTED_PREFIX,
0172 .flags = F2FS_XATTR_INDEX_TRUSTED,
0173 .list = f2fs_xattr_trusted_list,
0174 .get = f2fs_xattr_generic_get,
0175 .set = f2fs_xattr_generic_set,
0176 };
0177
0178 const struct xattr_handler f2fs_xattr_advise_handler = {
0179 .name = F2FS_SYSTEM_ADVISE_NAME,
0180 .flags = F2FS_XATTR_INDEX_ADVISE,
0181 .get = f2fs_xattr_advise_get,
0182 .set = f2fs_xattr_advise_set,
0183 };
0184
0185 const struct xattr_handler f2fs_xattr_security_handler = {
0186 .prefix = XATTR_SECURITY_PREFIX,
0187 .flags = F2FS_XATTR_INDEX_SECURITY,
0188 .get = f2fs_xattr_generic_get,
0189 .set = f2fs_xattr_generic_set,
0190 };
0191
0192 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
0193 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
0194 #ifdef CONFIG_F2FS_FS_POSIX_ACL
0195 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
0196 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
0197 #endif
0198 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
0199 #ifdef CONFIG_F2FS_FS_SECURITY
0200 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
0201 #endif
0202 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
0203 };
0204
0205 const struct xattr_handler *f2fs_xattr_handlers[] = {
0206 &f2fs_xattr_user_handler,
0207 #ifdef CONFIG_F2FS_FS_POSIX_ACL
0208 &posix_acl_access_xattr_handler,
0209 &posix_acl_default_xattr_handler,
0210 #endif
0211 &f2fs_xattr_trusted_handler,
0212 #ifdef CONFIG_F2FS_FS_SECURITY
0213 &f2fs_xattr_security_handler,
0214 #endif
0215 &f2fs_xattr_advise_handler,
0216 NULL,
0217 };
0218
0219 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
0220 {
0221 const struct xattr_handler *handler = NULL;
0222
0223 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
0224 handler = f2fs_xattr_handler_map[index];
0225 return handler;
0226 }
0227
0228 static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
0229 void *last_base_addr, void **last_addr,
0230 int index, size_t len, const char *name)
0231 {
0232 struct f2fs_xattr_entry *entry;
0233
0234 list_for_each_xattr(entry, base_addr) {
0235 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
0236 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
0237 if (last_addr)
0238 *last_addr = entry;
0239 return NULL;
0240 }
0241
0242 if (entry->e_name_index != index)
0243 continue;
0244 if (entry->e_name_len != len)
0245 continue;
0246 if (!memcmp(entry->e_name, name, len))
0247 break;
0248 }
0249 return entry;
0250 }
0251
0252 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
0253 void *base_addr, void **last_addr, int index,
0254 size_t len, const char *name)
0255 {
0256 struct f2fs_xattr_entry *entry;
0257 unsigned int inline_size = inline_xattr_size(inode);
0258 void *max_addr = base_addr + inline_size;
0259
0260 entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name);
0261 if (!entry)
0262 return NULL;
0263
0264
0265 if (IS_XATTR_LAST_ENTRY(entry) &&
0266 (void *)entry + sizeof(__u32) > max_addr) {
0267 *last_addr = entry;
0268 return NULL;
0269 }
0270 return entry;
0271 }
0272
0273 static int read_inline_xattr(struct inode *inode, struct page *ipage,
0274 void *txattr_addr)
0275 {
0276 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
0277 unsigned int inline_size = inline_xattr_size(inode);
0278 struct page *page = NULL;
0279 void *inline_addr;
0280
0281 if (ipage) {
0282 inline_addr = inline_xattr_addr(inode, ipage);
0283 } else {
0284 page = f2fs_get_node_page(sbi, inode->i_ino);
0285 if (IS_ERR(page))
0286 return PTR_ERR(page);
0287
0288 inline_addr = inline_xattr_addr(inode, page);
0289 }
0290 memcpy(txattr_addr, inline_addr, inline_size);
0291 f2fs_put_page(page, 1);
0292
0293 return 0;
0294 }
0295
0296 static int read_xattr_block(struct inode *inode, void *txattr_addr)
0297 {
0298 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
0299 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
0300 unsigned int inline_size = inline_xattr_size(inode);
0301 struct page *xpage;
0302 void *xattr_addr;
0303
0304
0305 xpage = f2fs_get_node_page(sbi, xnid);
0306 if (IS_ERR(xpage))
0307 return PTR_ERR(xpage);
0308
0309 xattr_addr = page_address(xpage);
0310 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
0311 f2fs_put_page(xpage, 1);
0312
0313 return 0;
0314 }
0315
0316 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
0317 unsigned int index, unsigned int len,
0318 const char *name, struct f2fs_xattr_entry **xe,
0319 void **base_addr, int *base_size,
0320 bool *is_inline)
0321 {
0322 void *cur_addr, *txattr_addr, *last_txattr_addr;
0323 void *last_addr = NULL;
0324 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
0325 unsigned int inline_size = inline_xattr_size(inode);
0326 int err;
0327
0328 if (!xnid && !inline_size)
0329 return -ENODATA;
0330
0331 *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
0332 txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
0333 if (!txattr_addr)
0334 return -ENOMEM;
0335
0336 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
0337
0338
0339 if (inline_size) {
0340 err = read_inline_xattr(inode, ipage, txattr_addr);
0341 if (err)
0342 goto out;
0343
0344 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
0345 index, len, name);
0346 if (*xe) {
0347 *base_size = inline_size;
0348 goto check;
0349 }
0350 }
0351
0352
0353 if (xnid) {
0354 err = read_xattr_block(inode, txattr_addr);
0355 if (err)
0356 goto out;
0357 }
0358
0359 if (last_addr)
0360 cur_addr = XATTR_HDR(last_addr) - 1;
0361 else
0362 cur_addr = txattr_addr;
0363
0364 *xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name);
0365 if (!*xe) {
0366 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
0367 inode->i_ino);
0368 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
0369 err = -EFSCORRUPTED;
0370 goto out;
0371 }
0372 check:
0373 if (IS_XATTR_LAST_ENTRY(*xe)) {
0374 err = -ENODATA;
0375 goto out;
0376 }
0377
0378 *base_addr = txattr_addr;
0379 return 0;
0380 out:
0381 xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
0382 return err;
0383 }
0384
0385 static int read_all_xattrs(struct inode *inode, struct page *ipage,
0386 void **base_addr)
0387 {
0388 struct f2fs_xattr_header *header;
0389 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
0390 unsigned int size = VALID_XATTR_BLOCK_SIZE;
0391 unsigned int inline_size = inline_xattr_size(inode);
0392 void *txattr_addr;
0393 int err;
0394
0395 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
0396 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
0397 if (!txattr_addr)
0398 return -ENOMEM;
0399
0400
0401 if (inline_size) {
0402 err = read_inline_xattr(inode, ipage, txattr_addr);
0403 if (err)
0404 goto fail;
0405 }
0406
0407
0408 if (xnid) {
0409 err = read_xattr_block(inode, txattr_addr);
0410 if (err)
0411 goto fail;
0412 }
0413
0414 header = XATTR_HDR(txattr_addr);
0415
0416
0417 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
0418 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
0419 header->h_refcount = cpu_to_le32(1);
0420 }
0421 *base_addr = txattr_addr;
0422 return 0;
0423 fail:
0424 kfree(txattr_addr);
0425 return err;
0426 }
0427
0428 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
0429 void *txattr_addr, struct page *ipage)
0430 {
0431 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
0432 size_t inline_size = inline_xattr_size(inode);
0433 struct page *in_page = NULL;
0434 void *xattr_addr;
0435 void *inline_addr = NULL;
0436 struct page *xpage;
0437 nid_t new_nid = 0;
0438 int err = 0;
0439
0440 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
0441 if (!f2fs_alloc_nid(sbi, &new_nid))
0442 return -ENOSPC;
0443
0444
0445 if (inline_size) {
0446 if (ipage) {
0447 inline_addr = inline_xattr_addr(inode, ipage);
0448 } else {
0449 in_page = f2fs_get_node_page(sbi, inode->i_ino);
0450 if (IS_ERR(in_page)) {
0451 f2fs_alloc_nid_failed(sbi, new_nid);
0452 return PTR_ERR(in_page);
0453 }
0454 inline_addr = inline_xattr_addr(inode, in_page);
0455 }
0456
0457 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
0458 NODE, true, true);
0459
0460 if (hsize <= inline_size) {
0461 err = f2fs_truncate_xattr_node(inode);
0462 f2fs_alloc_nid_failed(sbi, new_nid);
0463 if (err) {
0464 f2fs_put_page(in_page, 1);
0465 return err;
0466 }
0467 memcpy(inline_addr, txattr_addr, inline_size);
0468 set_page_dirty(ipage ? ipage : in_page);
0469 goto in_page_out;
0470 }
0471 }
0472
0473
0474 if (F2FS_I(inode)->i_xattr_nid) {
0475 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
0476 if (IS_ERR(xpage)) {
0477 err = PTR_ERR(xpage);
0478 f2fs_alloc_nid_failed(sbi, new_nid);
0479 goto in_page_out;
0480 }
0481 f2fs_bug_on(sbi, new_nid);
0482 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
0483 } else {
0484 struct dnode_of_data dn;
0485
0486 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
0487 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
0488 if (IS_ERR(xpage)) {
0489 err = PTR_ERR(xpage);
0490 f2fs_alloc_nid_failed(sbi, new_nid);
0491 goto in_page_out;
0492 }
0493 f2fs_alloc_nid_done(sbi, new_nid);
0494 }
0495 xattr_addr = page_address(xpage);
0496
0497 if (inline_size)
0498 memcpy(inline_addr, txattr_addr, inline_size);
0499 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
0500
0501 if (inline_size)
0502 set_page_dirty(ipage ? ipage : in_page);
0503 set_page_dirty(xpage);
0504
0505 f2fs_put_page(xpage, 1);
0506 in_page_out:
0507 f2fs_put_page(in_page, 1);
0508 return err;
0509 }
0510
0511 int f2fs_getxattr(struct inode *inode, int index, const char *name,
0512 void *buffer, size_t buffer_size, struct page *ipage)
0513 {
0514 struct f2fs_xattr_entry *entry = NULL;
0515 int error;
0516 unsigned int size, len;
0517 void *base_addr = NULL;
0518 int base_size;
0519 bool is_inline;
0520
0521 if (name == NULL)
0522 return -EINVAL;
0523
0524 len = strlen(name);
0525 if (len > F2FS_NAME_LEN)
0526 return -ERANGE;
0527
0528 f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
0529 error = lookup_all_xattrs(inode, ipage, index, len, name,
0530 &entry, &base_addr, &base_size, &is_inline);
0531 f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
0532 if (error)
0533 return error;
0534
0535 size = le16_to_cpu(entry->e_value_size);
0536
0537 if (buffer && size > buffer_size) {
0538 error = -ERANGE;
0539 goto out;
0540 }
0541
0542 if (buffer) {
0543 char *pval = entry->e_name + entry->e_name_len;
0544
0545 if (base_size - (pval - (char *)base_addr) < size) {
0546 error = -ERANGE;
0547 goto out;
0548 }
0549 memcpy(buffer, pval, size);
0550 }
0551 error = size;
0552 out:
0553 xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
0554 return error;
0555 }
0556
0557 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
0558 {
0559 struct inode *inode = d_inode(dentry);
0560 struct f2fs_xattr_entry *entry;
0561 void *base_addr, *last_base_addr;
0562 int error;
0563 size_t rest = buffer_size;
0564
0565 f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
0566 error = read_all_xattrs(inode, NULL, &base_addr);
0567 f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
0568 if (error)
0569 return error;
0570
0571 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
0572
0573 list_for_each_xattr(entry, base_addr) {
0574 const struct xattr_handler *handler =
0575 f2fs_xattr_handler(entry->e_name_index);
0576 const char *prefix;
0577 size_t prefix_len;
0578 size_t size;
0579
0580 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
0581 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
0582 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
0583 inode->i_ino);
0584 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
0585 error = -EFSCORRUPTED;
0586 goto cleanup;
0587 }
0588
0589 if (!handler || (handler->list && !handler->list(dentry)))
0590 continue;
0591
0592 prefix = xattr_prefix(handler);
0593 prefix_len = strlen(prefix);
0594 size = prefix_len + entry->e_name_len + 1;
0595 if (buffer) {
0596 if (size > rest) {
0597 error = -ERANGE;
0598 goto cleanup;
0599 }
0600 memcpy(buffer, prefix, prefix_len);
0601 buffer += prefix_len;
0602 memcpy(buffer, entry->e_name, entry->e_name_len);
0603 buffer += entry->e_name_len;
0604 *buffer++ = 0;
0605 }
0606 rest -= size;
0607 }
0608 error = buffer_size - rest;
0609 cleanup:
0610 kfree(base_addr);
0611 return error;
0612 }
0613
0614 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
0615 const void *value, size_t size)
0616 {
0617 void *pval = entry->e_name + entry->e_name_len;
0618
0619 return (le16_to_cpu(entry->e_value_size) == size) &&
0620 !memcmp(pval, value, size);
0621 }
0622
0623 static int __f2fs_setxattr(struct inode *inode, int index,
0624 const char *name, const void *value, size_t size,
0625 struct page *ipage, int flags)
0626 {
0627 struct f2fs_xattr_entry *here, *last;
0628 void *base_addr, *last_base_addr;
0629 int found, newsize;
0630 size_t len;
0631 __u32 new_hsize;
0632 int error;
0633
0634 if (name == NULL)
0635 return -EINVAL;
0636
0637 if (value == NULL)
0638 size = 0;
0639
0640 len = strlen(name);
0641
0642 if (len > F2FS_NAME_LEN)
0643 return -ERANGE;
0644
0645 if (size > MAX_VALUE_LEN(inode))
0646 return -E2BIG;
0647
0648 error = read_all_xattrs(inode, ipage, &base_addr);
0649 if (error)
0650 return error;
0651
0652 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
0653
0654
0655 here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name);
0656 if (!here) {
0657 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
0658 inode->i_ino);
0659 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
0660 error = -EFSCORRUPTED;
0661 goto exit;
0662 }
0663
0664 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
0665
0666 if (found) {
0667 if ((flags & XATTR_CREATE)) {
0668 error = -EEXIST;
0669 goto exit;
0670 }
0671
0672 if (value && f2fs_xattr_value_same(here, value, size))
0673 goto same;
0674 } else if ((flags & XATTR_REPLACE)) {
0675 error = -ENODATA;
0676 goto exit;
0677 }
0678
0679 last = here;
0680 while (!IS_XATTR_LAST_ENTRY(last)) {
0681 if ((void *)(last) + sizeof(__u32) > last_base_addr ||
0682 (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
0683 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu",
0684 inode->i_ino, ENTRY_SIZE(last));
0685 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
0686 error = -EFSCORRUPTED;
0687 goto exit;
0688 }
0689 last = XATTR_NEXT_ENTRY(last);
0690 }
0691
0692 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
0693
0694
0695 if (value) {
0696 int free;
0697
0698
0699
0700
0701 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
0702 if (found)
0703 free = free + ENTRY_SIZE(here);
0704
0705 if (unlikely(free < newsize)) {
0706 error = -E2BIG;
0707 goto exit;
0708 }
0709 }
0710
0711
0712 if (found) {
0713
0714
0715
0716
0717 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
0718 int oldsize = ENTRY_SIZE(here);
0719
0720 memmove(here, next, (char *)last - (char *)next);
0721 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
0722 memset(last, 0, oldsize);
0723 }
0724
0725 new_hsize = (char *)last - (char *)base_addr;
0726
0727
0728 if (value) {
0729 char *pval;
0730
0731
0732
0733
0734 last->e_name_index = index;
0735 last->e_name_len = len;
0736 memcpy(last->e_name, name, len);
0737 pval = last->e_name + len;
0738 memcpy(pval, value, size);
0739 last->e_value_size = cpu_to_le16(size);
0740 new_hsize += newsize;
0741 }
0742
0743 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
0744 if (error)
0745 goto exit;
0746
0747 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
0748 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
0749 f2fs_set_encrypted_inode(inode);
0750 f2fs_mark_inode_dirty_sync(inode, true);
0751 if (!error && S_ISDIR(inode->i_mode))
0752 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
0753
0754 same:
0755 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
0756 inode->i_mode = F2FS_I(inode)->i_acl_mode;
0757 inode->i_ctime = current_time(inode);
0758 clear_inode_flag(inode, FI_ACL_MODE);
0759 }
0760
0761 exit:
0762 kfree(base_addr);
0763 return error;
0764 }
0765
0766 int f2fs_setxattr(struct inode *inode, int index, const char *name,
0767 const void *value, size_t size,
0768 struct page *ipage, int flags)
0769 {
0770 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
0771 int err;
0772
0773 if (unlikely(f2fs_cp_error(sbi)))
0774 return -EIO;
0775 if (!f2fs_is_checkpoint_ready(sbi))
0776 return -ENOSPC;
0777
0778 err = f2fs_dquot_initialize(inode);
0779 if (err)
0780 return err;
0781
0782
0783 if (ipage)
0784 return __f2fs_setxattr(inode, index, name, value,
0785 size, ipage, flags);
0786 f2fs_balance_fs(sbi, true);
0787
0788 f2fs_lock_op(sbi);
0789 f2fs_down_write(&F2FS_I(inode)->i_xattr_sem);
0790 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
0791 f2fs_up_write(&F2FS_I(inode)->i_xattr_sem);
0792 f2fs_unlock_op(sbi);
0793
0794 f2fs_update_time(sbi, REQ_TIME);
0795 return err;
0796 }
0797
0798 int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
0799 {
0800 dev_t dev = sbi->sb->s_bdev->bd_dev;
0801 char slab_name[32];
0802
0803 sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
0804
0805 sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
0806 sizeof(__le32) + XATTR_PADDING_SIZE;
0807
0808 sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
0809 sbi->inline_xattr_slab_size);
0810 if (!sbi->inline_xattr_slab)
0811 return -ENOMEM;
0812
0813 return 0;
0814 }
0815
0816 void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
0817 {
0818 kmem_cache_destroy(sbi->inline_xattr_slab);
0819 }