| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * fs/f2fs/xattr.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
| 7 | * |
| 8 | * Portions of this code from linux/fs/ext2/xattr.c |
| 9 | * |
| 10 | * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de> |
| 11 | * |
| 12 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. |
| 13 | * Extended attributes for symlinks and special files added per |
| 14 | * suggestion of Luka Renko <luka.renko@hermes.si>. |
| 15 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, |
| 16 | * Red Hat Inc. |
| 17 | */ |
| 18 | #include <linux/rwsem.h> |
| 19 | #include <linux/f2fs_fs.h> |
| 20 | #include <linux/security.h> |
| 21 | #include <linux/posix_acl_xattr.h> |
| 22 | #include "f2fs.h" |
| 23 | #include "xattr.h" |
| 24 | #include "segment.h" |
| 25 | |
| 26 | static struct kmem_cache *inline_xattr_slab; |
| 27 | static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline) |
| 28 | { |
| 29 | if (likely(size == DEFAULT_XATTR_SLAB_SIZE)) { |
| 30 | *is_inline = true; |
| 31 | return f2fs_kmem_cache_alloc(cachep: inline_xattr_slab, |
| 32 | GFP_F2FS_ZERO, nofail: false, sbi); |
| 33 | } |
| 34 | *is_inline = false; |
| 35 | return f2fs_kzalloc(sbi, size, GFP_NOFS); |
| 36 | } |
| 37 | |
| 38 | static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr, |
| 39 | bool is_inline) |
| 40 | { |
| 41 | if (is_inline) |
| 42 | kmem_cache_free(s: inline_xattr_slab, objp: xattr_addr); |
| 43 | else |
| 44 | kfree(objp: xattr_addr); |
| 45 | } |
| 46 | |
| 47 | static int f2fs_xattr_generic_get(const struct xattr_handler *handler, |
| 48 | struct dentry *unused, struct inode *inode, |
| 49 | const char *name, void *buffer, size_t size) |
| 50 | { |
| 51 | struct f2fs_sb_info *sbi = F2FS_SB(sb: inode->i_sb); |
| 52 | |
| 53 | switch (handler->flags) { |
| 54 | case F2FS_XATTR_INDEX_USER: |
| 55 | if (!test_opt(sbi, XATTR_USER)) |
| 56 | return -EOPNOTSUPP; |
| 57 | break; |
| 58 | case F2FS_XATTR_INDEX_TRUSTED: |
| 59 | case F2FS_XATTR_INDEX_SECURITY: |
| 60 | break; |
| 61 | default: |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | return f2fs_getxattr(inode, handler->flags, name, |
| 65 | buffer, size, NULL); |
| 66 | } |
| 67 | |
| 68 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
| 69 | struct mnt_idmap *idmap, |
| 70 | struct dentry *unused, struct inode *inode, |
| 71 | const char *name, const void *value, |
| 72 | size_t size, int flags) |
| 73 | { |
| 74 | struct f2fs_sb_info *sbi = F2FS_SB(sb: inode->i_sb); |
| 75 | |
| 76 | switch (handler->flags) { |
| 77 | case F2FS_XATTR_INDEX_USER: |
| 78 | if (!test_opt(sbi, XATTR_USER)) |
| 79 | return -EOPNOTSUPP; |
| 80 | break; |
| 81 | case F2FS_XATTR_INDEX_TRUSTED: |
| 82 | case F2FS_XATTR_INDEX_SECURITY: |
| 83 | break; |
| 84 | default: |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | return f2fs_setxattr(inode, handler->flags, name, |
| 88 | value, size, NULL, flags); |
| 89 | } |
| 90 | |
| 91 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
| 92 | { |
| 93 | struct f2fs_sb_info *sbi = F2FS_SB(sb: dentry->d_sb); |
| 94 | |
| 95 | return test_opt(sbi, XATTR_USER); |
| 96 | } |
| 97 | |
| 98 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
| 99 | { |
| 100 | return capable(CAP_SYS_ADMIN); |
| 101 | } |
| 102 | |
| 103 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
| 104 | struct dentry *unused, struct inode *inode, |
| 105 | const char *name, void *buffer, size_t size) |
| 106 | { |
| 107 | if (buffer) |
| 108 | *((char *)buffer) = F2FS_I(inode)->i_advise; |
| 109 | return sizeof(char); |
| 110 | } |
| 111 | |
| 112 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
| 113 | struct mnt_idmap *idmap, |
| 114 | struct dentry *unused, struct inode *inode, |
| 115 | const char *name, const void *value, |
| 116 | size_t size, int flags) |
| 117 | { |
| 118 | unsigned char old_advise = F2FS_I(inode)->i_advise; |
| 119 | unsigned char new_advise; |
| 120 | |
| 121 | if (!inode_owner_or_capable(idmap: &nop_mnt_idmap, inode)) |
| 122 | return -EPERM; |
| 123 | if (value == NULL) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | new_advise = *(char *)value; |
| 127 | if (new_advise & ~FADVISE_MODIFIABLE_BITS) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | new_advise = new_advise & FADVISE_MODIFIABLE_BITS; |
| 131 | new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS; |
| 132 | |
| 133 | F2FS_I(inode)->i_advise = new_advise; |
| 134 | f2fs_mark_inode_dirty_sync(inode, sync: true); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 139 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, |
| 140 | void *folio) |
| 141 | { |
| 142 | const struct xattr *xattr; |
| 143 | int err = 0; |
| 144 | |
| 145 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
| 146 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
| 147 | xattr->name, xattr->value, |
| 148 | xattr->value_len, folio, 0); |
| 149 | if (err < 0) |
| 150 | break; |
| 151 | } |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | int f2fs_init_security(struct inode *inode, struct inode *dir, |
| 156 | const struct qstr *qstr, struct folio *ifolio) |
| 157 | { |
| 158 | return security_inode_init_security(inode, dir, qstr, |
| 159 | initxattrs: f2fs_initxattrs, fs_data: ifolio); |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | const struct xattr_handler f2fs_xattr_user_handler = { |
| 164 | .prefix = XATTR_USER_PREFIX, |
| 165 | .flags = F2FS_XATTR_INDEX_USER, |
| 166 | .list = f2fs_xattr_user_list, |
| 167 | .get = f2fs_xattr_generic_get, |
| 168 | .set = f2fs_xattr_generic_set, |
| 169 | }; |
| 170 | |
| 171 | const struct xattr_handler f2fs_xattr_trusted_handler = { |
| 172 | .prefix = XATTR_TRUSTED_PREFIX, |
| 173 | .flags = F2FS_XATTR_INDEX_TRUSTED, |
| 174 | .list = f2fs_xattr_trusted_list, |
| 175 | .get = f2fs_xattr_generic_get, |
| 176 | .set = f2fs_xattr_generic_set, |
| 177 | }; |
| 178 | |
| 179 | const struct xattr_handler f2fs_xattr_advise_handler = { |
| 180 | .name = F2FS_SYSTEM_ADVISE_NAME, |
| 181 | .flags = F2FS_XATTR_INDEX_ADVISE, |
| 182 | .get = f2fs_xattr_advise_get, |
| 183 | .set = f2fs_xattr_advise_set, |
| 184 | }; |
| 185 | |
| 186 | const struct xattr_handler f2fs_xattr_security_handler = { |
| 187 | .prefix = XATTR_SECURITY_PREFIX, |
| 188 | .flags = F2FS_XATTR_INDEX_SECURITY, |
| 189 | .get = f2fs_xattr_generic_get, |
| 190 | .set = f2fs_xattr_generic_set, |
| 191 | }; |
| 192 | |
| 193 | static const struct xattr_handler * const f2fs_xattr_handler_map[] = { |
| 194 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, |
| 195 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 196 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access, |
| 197 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default, |
| 198 | #endif |
| 199 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, |
| 200 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 201 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, |
| 202 | #endif |
| 203 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
| 204 | }; |
| 205 | |
| 206 | const struct xattr_handler * const f2fs_xattr_handlers[] = { |
| 207 | &f2fs_xattr_user_handler, |
| 208 | &f2fs_xattr_trusted_handler, |
| 209 | #ifdef CONFIG_F2FS_FS_SECURITY |
| 210 | &f2fs_xattr_security_handler, |
| 211 | #endif |
| 212 | &f2fs_xattr_advise_handler, |
| 213 | NULL, |
| 214 | }; |
| 215 | |
| 216 | static inline const char *f2fs_xattr_prefix(int index, |
| 217 | struct dentry *dentry) |
| 218 | { |
| 219 | const struct xattr_handler *handler = NULL; |
| 220 | |
| 221 | if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map)) |
| 222 | handler = f2fs_xattr_handler_map[index]; |
| 223 | |
| 224 | if (!xattr_handler_can_list(handler, dentry)) |
| 225 | return NULL; |
| 226 | |
| 227 | return xattr_prefix(handler); |
| 228 | } |
| 229 | |
| 230 | static struct f2fs_xattr_entry *__find_xattr(void *base_addr, |
| 231 | void *last_base_addr, void **last_addr, |
| 232 | int index, size_t len, const char *name) |
| 233 | { |
| 234 | struct f2fs_xattr_entry *entry; |
| 235 | |
| 236 | list_for_each_xattr(entry, base_addr) { |
| 237 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
| 238 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { |
| 239 | if (last_addr) |
| 240 | *last_addr = entry; |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | if (entry->e_name_index != index) |
| 245 | continue; |
| 246 | if (entry->e_name_len != len) |
| 247 | continue; |
| 248 | if (!memcmp(p: entry->e_name, q: name, size: len)) |
| 249 | break; |
| 250 | } |
| 251 | return entry; |
| 252 | } |
| 253 | |
| 254 | static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode, |
| 255 | void *base_addr, void **last_addr, int index, |
| 256 | size_t len, const char *name) |
| 257 | { |
| 258 | struct f2fs_xattr_entry *entry; |
| 259 | unsigned int inline_size = inline_xattr_size(inode); |
| 260 | void *max_addr = base_addr + inline_size; |
| 261 | |
| 262 | entry = __find_xattr(base_addr, last_base_addr: max_addr, last_addr, index, len, name); |
| 263 | if (!entry) |
| 264 | return NULL; |
| 265 | |
| 266 | /* inline xattr header or entry across max inline xattr size */ |
| 267 | if (IS_XATTR_LAST_ENTRY(entry) && |
| 268 | (void *)entry + sizeof(__u32) > max_addr) { |
| 269 | *last_addr = entry; |
| 270 | return NULL; |
| 271 | } |
| 272 | return entry; |
| 273 | } |
| 274 | |
| 275 | static int read_inline_xattr(struct inode *inode, struct folio *ifolio, |
| 276 | void *txattr_addr) |
| 277 | { |
| 278 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 279 | unsigned int inline_size = inline_xattr_size(inode); |
| 280 | struct folio *folio = NULL; |
| 281 | void *inline_addr; |
| 282 | |
| 283 | if (ifolio) { |
| 284 | inline_addr = inline_xattr_addr(inode, folio: ifolio); |
| 285 | } else { |
| 286 | folio = f2fs_get_inode_folio(sbi, ino: inode->i_ino); |
| 287 | if (IS_ERR(ptr: folio)) |
| 288 | return PTR_ERR(ptr: folio); |
| 289 | |
| 290 | inline_addr = inline_xattr_addr(inode, folio); |
| 291 | } |
| 292 | memcpy(txattr_addr, inline_addr, inline_size); |
| 293 | f2fs_folio_put(folio, unlock: true); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int read_xattr_block(struct inode *inode, void *txattr_addr) |
| 299 | { |
| 300 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 301 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 302 | unsigned int inline_size = inline_xattr_size(inode); |
| 303 | struct folio *xfolio; |
| 304 | void *xattr_addr; |
| 305 | |
| 306 | /* The inode already has an extended attribute block. */ |
| 307 | xfolio = f2fs_get_xnode_folio(sbi, xnid); |
| 308 | if (IS_ERR(ptr: xfolio)) |
| 309 | return PTR_ERR(ptr: xfolio); |
| 310 | |
| 311 | xattr_addr = folio_address(folio: xfolio); |
| 312 | memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); |
| 313 | f2fs_folio_put(folio: xfolio, unlock: true); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int lookup_all_xattrs(struct inode *inode, struct folio *ifolio, |
| 319 | unsigned int index, unsigned int len, |
| 320 | const char *name, struct f2fs_xattr_entry **xe, |
| 321 | void **base_addr, int *base_size, |
| 322 | bool *is_inline) |
| 323 | { |
| 324 | void *cur_addr, *txattr_addr, *last_txattr_addr; |
| 325 | void *last_addr = NULL; |
| 326 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 327 | unsigned int inline_size = inline_xattr_size(inode); |
| 328 | int err; |
| 329 | |
| 330 | if (!xnid && !inline_size) |
| 331 | return -ENODATA; |
| 332 | |
| 333 | *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE; |
| 334 | txattr_addr = xattr_alloc(sbi: F2FS_I_SB(inode), size: *base_size, is_inline); |
| 335 | if (!txattr_addr) |
| 336 | return -ENOMEM; |
| 337 | |
| 338 | last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode); |
| 339 | |
| 340 | /* read from inline xattr */ |
| 341 | if (inline_size) { |
| 342 | err = read_inline_xattr(inode, ifolio, txattr_addr); |
| 343 | if (err) |
| 344 | goto out; |
| 345 | |
| 346 | *xe = __find_inline_xattr(inode, base_addr: txattr_addr, last_addr: &last_addr, |
| 347 | index, len, name); |
| 348 | if (*xe) { |
| 349 | *base_size = inline_size; |
| 350 | goto check; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /* read from xattr node block */ |
| 355 | if (xnid) { |
| 356 | err = read_xattr_block(inode, txattr_addr); |
| 357 | if (err) |
| 358 | goto out; |
| 359 | } |
| 360 | |
| 361 | if (last_addr) |
| 362 | cur_addr = XATTR_HDR(last_addr) - 1; |
| 363 | else |
| 364 | cur_addr = txattr_addr; |
| 365 | |
| 366 | *xe = __find_xattr(base_addr: cur_addr, last_base_addr: last_txattr_addr, NULL, index, len, name); |
| 367 | if (!*xe) { |
| 368 | f2fs_err(F2FS_I_SB(inode), "lookup inode (%lu) has corrupted xattr" , |
| 369 | inode->i_ino); |
| 370 | set_sbi_flag(sbi: F2FS_I_SB(inode), type: SBI_NEED_FSCK); |
| 371 | err = -ENODATA; |
| 372 | f2fs_handle_error(sbi: F2FS_I_SB(inode), |
| 373 | error: ERROR_CORRUPTED_XATTR); |
| 374 | goto out; |
| 375 | } |
| 376 | check: |
| 377 | if (IS_XATTR_LAST_ENTRY(*xe)) { |
| 378 | err = -ENODATA; |
| 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | *base_addr = txattr_addr; |
| 383 | return 0; |
| 384 | out: |
| 385 | xattr_free(sbi: F2FS_I_SB(inode), xattr_addr: txattr_addr, is_inline: *is_inline); |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | static int read_all_xattrs(struct inode *inode, struct folio *ifolio, |
| 390 | void **base_addr) |
| 391 | { |
| 392 | struct f2fs_xattr_header *; |
| 393 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 394 | unsigned int size = VALID_XATTR_BLOCK_SIZE; |
| 395 | unsigned int inline_size = inline_xattr_size(inode); |
| 396 | void *txattr_addr; |
| 397 | int err; |
| 398 | |
| 399 | txattr_addr = f2fs_kzalloc(sbi: F2FS_I_SB(inode), |
| 400 | size: inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS); |
| 401 | if (!txattr_addr) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | /* read from inline xattr */ |
| 405 | if (inline_size) { |
| 406 | err = read_inline_xattr(inode, ifolio, txattr_addr); |
| 407 | if (err) |
| 408 | goto fail; |
| 409 | } |
| 410 | |
| 411 | /* read from xattr node block */ |
| 412 | if (xnid) { |
| 413 | err = read_xattr_block(inode, txattr_addr); |
| 414 | if (err) |
| 415 | goto fail; |
| 416 | } |
| 417 | |
| 418 | header = XATTR_HDR(txattr_addr); |
| 419 | |
| 420 | /* never been allocated xattrs */ |
| 421 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { |
| 422 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); |
| 423 | header->h_refcount = cpu_to_le32(1); |
| 424 | } |
| 425 | *base_addr = txattr_addr; |
| 426 | return 0; |
| 427 | fail: |
| 428 | kfree(objp: txattr_addr); |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, |
| 433 | void *txattr_addr, struct folio *ifolio) |
| 434 | { |
| 435 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 436 | size_t inline_size = inline_xattr_size(inode); |
| 437 | struct folio *in_folio = NULL; |
| 438 | void *xattr_addr; |
| 439 | void *inline_addr = NULL; |
| 440 | struct folio *xfolio; |
| 441 | nid_t new_nid = 0; |
| 442 | int err = 0; |
| 443 | |
| 444 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) |
| 445 | if (!f2fs_alloc_nid(sbi, nid: &new_nid)) |
| 446 | return -ENOSPC; |
| 447 | |
| 448 | /* write to inline xattr */ |
| 449 | if (inline_size) { |
| 450 | if (ifolio) { |
| 451 | inline_addr = inline_xattr_addr(inode, folio: ifolio); |
| 452 | } else { |
| 453 | in_folio = f2fs_get_inode_folio(sbi, ino: inode->i_ino); |
| 454 | if (IS_ERR(ptr: in_folio)) { |
| 455 | f2fs_alloc_nid_failed(sbi, nid: new_nid); |
| 456 | return PTR_ERR(ptr: in_folio); |
| 457 | } |
| 458 | inline_addr = inline_xattr_addr(inode, folio: in_folio); |
| 459 | } |
| 460 | |
| 461 | f2fs_folio_wait_writeback(folio: ifolio ? ifolio : in_folio, |
| 462 | type: NODE, ordered: true, locked: true); |
| 463 | /* no need to use xattr node block */ |
| 464 | if (hsize <= inline_size) { |
| 465 | err = f2fs_truncate_xattr_node(inode); |
| 466 | f2fs_alloc_nid_failed(sbi, nid: new_nid); |
| 467 | if (err) { |
| 468 | f2fs_folio_put(folio: in_folio, unlock: true); |
| 469 | return err; |
| 470 | } |
| 471 | memcpy(inline_addr, txattr_addr, inline_size); |
| 472 | folio_mark_dirty(folio: ifolio ? ifolio : in_folio); |
| 473 | goto in_page_out; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* write to xattr node block */ |
| 478 | if (F2FS_I(inode)->i_xattr_nid) { |
| 479 | xfolio = f2fs_get_xnode_folio(sbi, xnid: F2FS_I(inode)->i_xattr_nid); |
| 480 | if (IS_ERR(ptr: xfolio)) { |
| 481 | err = PTR_ERR(ptr: xfolio); |
| 482 | f2fs_alloc_nid_failed(sbi, nid: new_nid); |
| 483 | goto in_page_out; |
| 484 | } |
| 485 | f2fs_bug_on(sbi, new_nid); |
| 486 | f2fs_folio_wait_writeback(folio: xfolio, type: NODE, ordered: true, locked: true); |
| 487 | } else { |
| 488 | struct dnode_of_data dn; |
| 489 | |
| 490 | set_new_dnode(dn: &dn, inode, NULL, NULL, nid: new_nid); |
| 491 | xfolio = f2fs_new_node_folio(dn: &dn, XATTR_NODE_OFFSET); |
| 492 | if (IS_ERR(ptr: xfolio)) { |
| 493 | err = PTR_ERR(ptr: xfolio); |
| 494 | f2fs_alloc_nid_failed(sbi, nid: new_nid); |
| 495 | goto in_page_out; |
| 496 | } |
| 497 | f2fs_alloc_nid_done(sbi, nid: new_nid); |
| 498 | } |
| 499 | xattr_addr = folio_address(folio: xfolio); |
| 500 | |
| 501 | if (inline_size) |
| 502 | memcpy(inline_addr, txattr_addr, inline_size); |
| 503 | memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); |
| 504 | |
| 505 | if (inline_size) |
| 506 | folio_mark_dirty(folio: ifolio ? ifolio : in_folio); |
| 507 | folio_mark_dirty(folio: xfolio); |
| 508 | |
| 509 | f2fs_folio_put(folio: xfolio, unlock: true); |
| 510 | in_page_out: |
| 511 | f2fs_folio_put(folio: in_folio, unlock: true); |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
| 516 | void *buffer, size_t buffer_size, struct folio *ifolio) |
| 517 | { |
| 518 | struct f2fs_xattr_entry *entry = NULL; |
| 519 | int error; |
| 520 | unsigned int size, len; |
| 521 | void *base_addr = NULL; |
| 522 | int base_size; |
| 523 | bool is_inline; |
| 524 | |
| 525 | if (name == NULL) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | len = strlen(name); |
| 529 | if (len > F2FS_NAME_LEN) |
| 530 | return -ERANGE; |
| 531 | |
| 532 | if (!ifolio) |
| 533 | f2fs_down_read(sem: &F2FS_I(inode)->i_xattr_sem); |
| 534 | error = lookup_all_xattrs(inode, ifolio, index, len, name, |
| 535 | xe: &entry, base_addr: &base_addr, base_size: &base_size, is_inline: &is_inline); |
| 536 | if (!ifolio) |
| 537 | f2fs_up_read(sem: &F2FS_I(inode)->i_xattr_sem); |
| 538 | if (error) |
| 539 | return error; |
| 540 | |
| 541 | size = le16_to_cpu(entry->e_value_size); |
| 542 | |
| 543 | if (buffer && size > buffer_size) { |
| 544 | error = -ERANGE; |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | if (buffer) { |
| 549 | char *pval = entry->e_name + entry->e_name_len; |
| 550 | |
| 551 | if (base_size - (pval - (char *)base_addr) < size) { |
| 552 | error = -ERANGE; |
| 553 | goto out; |
| 554 | } |
| 555 | memcpy(buffer, pval, size); |
| 556 | } |
| 557 | error = size; |
| 558 | out: |
| 559 | xattr_free(sbi: F2FS_I_SB(inode), xattr_addr: base_addr, is_inline); |
| 560 | return error; |
| 561 | } |
| 562 | |
| 563 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 564 | { |
| 565 | struct inode *inode = d_inode(dentry); |
| 566 | struct f2fs_xattr_entry *entry; |
| 567 | void *base_addr, *last_base_addr; |
| 568 | int error; |
| 569 | size_t rest = buffer_size; |
| 570 | |
| 571 | f2fs_down_read(sem: &F2FS_I(inode)->i_xattr_sem); |
| 572 | error = read_all_xattrs(inode, NULL, base_addr: &base_addr); |
| 573 | f2fs_up_read(sem: &F2FS_I(inode)->i_xattr_sem); |
| 574 | if (error) |
| 575 | return error; |
| 576 | |
| 577 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
| 578 | |
| 579 | list_for_each_xattr(entry, base_addr) { |
| 580 | const char *prefix; |
| 581 | size_t prefix_len; |
| 582 | size_t size; |
| 583 | |
| 584 | prefix = f2fs_xattr_prefix(index: entry->e_name_index, dentry); |
| 585 | |
| 586 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
| 587 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { |
| 588 | f2fs_err(F2FS_I_SB(inode), "list inode (%lu) has corrupted xattr" , |
| 589 | inode->i_ino); |
| 590 | set_sbi_flag(sbi: F2FS_I_SB(inode), type: SBI_NEED_FSCK); |
| 591 | f2fs_handle_error(sbi: F2FS_I_SB(inode), |
| 592 | error: ERROR_CORRUPTED_XATTR); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (!prefix) |
| 597 | continue; |
| 598 | |
| 599 | prefix_len = strlen(prefix); |
| 600 | size = prefix_len + entry->e_name_len + 1; |
| 601 | if (buffer) { |
| 602 | if (size > rest) { |
| 603 | error = -ERANGE; |
| 604 | goto cleanup; |
| 605 | } |
| 606 | memcpy(buffer, prefix, prefix_len); |
| 607 | buffer += prefix_len; |
| 608 | memcpy(buffer, entry->e_name, entry->e_name_len); |
| 609 | buffer += entry->e_name_len; |
| 610 | *buffer++ = 0; |
| 611 | } |
| 612 | rest -= size; |
| 613 | } |
| 614 | error = buffer_size - rest; |
| 615 | cleanup: |
| 616 | kfree(objp: base_addr); |
| 617 | return error; |
| 618 | } |
| 619 | |
| 620 | static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, |
| 621 | const void *value, size_t size) |
| 622 | { |
| 623 | void *pval = entry->e_name + entry->e_name_len; |
| 624 | |
| 625 | return (le16_to_cpu(entry->e_value_size) == size) && |
| 626 | !memcmp(p: pval, q: value, size); |
| 627 | } |
| 628 | |
| 629 | static int __f2fs_setxattr(struct inode *inode, int index, |
| 630 | const char *name, const void *value, size_t size, |
| 631 | struct folio *ifolio, int flags) |
| 632 | { |
| 633 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 634 | struct f2fs_xattr_entry *here, *last; |
| 635 | void *base_addr, *last_base_addr; |
| 636 | int found, newsize; |
| 637 | size_t len; |
| 638 | __u32 new_hsize; |
| 639 | int error; |
| 640 | |
| 641 | if (name == NULL) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | if (value == NULL) |
| 645 | size = 0; |
| 646 | |
| 647 | len = strlen(name); |
| 648 | |
| 649 | if (len > F2FS_NAME_LEN) |
| 650 | return -ERANGE; |
| 651 | |
| 652 | if (size > MAX_VALUE_LEN(inode)) |
| 653 | return -E2BIG; |
| 654 | retry: |
| 655 | error = read_all_xattrs(inode, ifolio, base_addr: &base_addr); |
| 656 | if (error) |
| 657 | return error; |
| 658 | |
| 659 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
| 660 | |
| 661 | /* find entry with wanted name. */ |
| 662 | here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name); |
| 663 | if (!here) { |
| 664 | if (!F2FS_I(inode)->i_xattr_nid) { |
| 665 | error = f2fs_recover_xattr_data(inode, NULL); |
| 666 | f2fs_notice(F2FS_I_SB(inode), |
| 667 | "recover xattr in inode (%lu), error(%d)" , |
| 668 | inode->i_ino, error); |
| 669 | if (!error) { |
| 670 | kfree(objp: base_addr); |
| 671 | goto retry; |
| 672 | } |
| 673 | } |
| 674 | f2fs_err(F2FS_I_SB(inode), "set inode (%lu) has corrupted xattr" , |
| 675 | inode->i_ino); |
| 676 | set_sbi_flag(sbi: F2FS_I_SB(inode), type: SBI_NEED_FSCK); |
| 677 | error = -EFSCORRUPTED; |
| 678 | f2fs_handle_error(sbi: F2FS_I_SB(inode), |
| 679 | error: ERROR_CORRUPTED_XATTR); |
| 680 | goto exit; |
| 681 | } |
| 682 | |
| 683 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
| 684 | |
| 685 | if (found) { |
| 686 | if ((flags & XATTR_CREATE)) { |
| 687 | error = -EEXIST; |
| 688 | goto exit; |
| 689 | } |
| 690 | |
| 691 | if (value && f2fs_xattr_value_same(entry: here, value, size)) |
| 692 | goto same; |
| 693 | } else if ((flags & XATTR_REPLACE)) { |
| 694 | error = -ENODATA; |
| 695 | goto exit; |
| 696 | } |
| 697 | |
| 698 | last = here; |
| 699 | while (!IS_XATTR_LAST_ENTRY(last)) { |
| 700 | if ((void *)(last) + sizeof(__u32) > last_base_addr || |
| 701 | (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) { |
| 702 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu" , |
| 703 | inode->i_ino, ENTRY_SIZE(last)); |
| 704 | set_sbi_flag(sbi: F2FS_I_SB(inode), type: SBI_NEED_FSCK); |
| 705 | error = -EFSCORRUPTED; |
| 706 | f2fs_handle_error(sbi: F2FS_I_SB(inode), |
| 707 | error: ERROR_CORRUPTED_XATTR); |
| 708 | goto exit; |
| 709 | } |
| 710 | last = XATTR_NEXT_ENTRY(last); |
| 711 | } |
| 712 | |
| 713 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
| 714 | |
| 715 | /* 1. Check space */ |
| 716 | if (value) { |
| 717 | int free; |
| 718 | /* |
| 719 | * If value is NULL, it is remove operation. |
| 720 | * In case of update operation, we calculate free. |
| 721 | */ |
| 722 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
| 723 | if (found) |
| 724 | free = free + ENTRY_SIZE(here); |
| 725 | |
| 726 | if (unlikely(free < newsize)) { |
| 727 | error = -E2BIG; |
| 728 | goto exit; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* 2. Remove old entry */ |
| 733 | if (found) { |
| 734 | /* |
| 735 | * If entry is found, remove old entry. |
| 736 | * If not found, remove operation is not needed. |
| 737 | */ |
| 738 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); |
| 739 | int oldsize = ENTRY_SIZE(here); |
| 740 | |
| 741 | memmove(here, next, (char *)last - (char *)next); |
| 742 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); |
| 743 | memset(last, 0, oldsize); |
| 744 | } |
| 745 | |
| 746 | new_hsize = (char *)last - (char *)base_addr; |
| 747 | |
| 748 | /* 3. Write new entry */ |
| 749 | if (value) { |
| 750 | char *pval; |
| 751 | /* |
| 752 | * Before we come here, old entry is removed. |
| 753 | * We just write new entry. |
| 754 | */ |
| 755 | last->e_name_index = index; |
| 756 | last->e_name_len = len; |
| 757 | memcpy(last->e_name, name, len); |
| 758 | pval = last->e_name + len; |
| 759 | memcpy(pval, value, size); |
| 760 | last->e_value_size = cpu_to_le16(size); |
| 761 | new_hsize += newsize; |
| 762 | /* |
| 763 | * Explicitly add the null terminator. The unused xattr space |
| 764 | * is supposed to always be zeroed, which would make this |
| 765 | * unnecessary, but don't depend on that. |
| 766 | */ |
| 767 | *(u32 *)((u8 *)last + newsize) = 0; |
| 768 | } |
| 769 | |
| 770 | error = write_all_xattrs(inode, hsize: new_hsize, txattr_addr: base_addr, ifolio); |
| 771 | if (error) |
| 772 | goto exit; |
| 773 | |
| 774 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
| 775 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) |
| 776 | f2fs_set_encrypted_inode(inode); |
| 777 | |
| 778 | if (!S_ISDIR(inode->i_mode)) |
| 779 | goto same; |
| 780 | /* |
| 781 | * In restrict mode, fsync() always try to trigger checkpoint for all |
| 782 | * metadata consistency, in other mode, it triggers checkpoint when |
| 783 | * parent's xattr metadata was updated. |
| 784 | */ |
| 785 | if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) |
| 786 | set_sbi_flag(sbi, type: SBI_NEED_CP); |
| 787 | else |
| 788 | f2fs_add_ino_entry(sbi, ino: inode->i_ino, type: XATTR_DIR_INO); |
| 789 | same: |
| 790 | if (is_inode_flag_set(inode, flag: FI_ACL_MODE)) { |
| 791 | inode->i_mode = F2FS_I(inode)->i_acl_mode; |
| 792 | clear_inode_flag(inode, flag: FI_ACL_MODE); |
| 793 | } |
| 794 | |
| 795 | inode_set_ctime_current(inode); |
| 796 | f2fs_mark_inode_dirty_sync(inode, sync: true); |
| 797 | exit: |
| 798 | kfree(objp: base_addr); |
| 799 | return error; |
| 800 | } |
| 801 | |
| 802 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
| 803 | const void *value, size_t size, |
| 804 | struct folio *ifolio, int flags) |
| 805 | { |
| 806 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 807 | int err; |
| 808 | |
| 809 | if (unlikely(f2fs_cp_error(sbi))) |
| 810 | return -EIO; |
| 811 | if (!f2fs_is_checkpoint_ready(sbi)) |
| 812 | return -ENOSPC; |
| 813 | |
| 814 | err = f2fs_dquot_initialize(inode); |
| 815 | if (err) |
| 816 | return err; |
| 817 | |
| 818 | /* this case is only from f2fs_init_inode_metadata */ |
| 819 | if (ifolio) |
| 820 | return __f2fs_setxattr(inode, index, name, value, |
| 821 | size, ifolio, flags); |
| 822 | f2fs_balance_fs(sbi, need: true); |
| 823 | |
| 824 | f2fs_lock_op(sbi); |
| 825 | f2fs_down_write(sem: &F2FS_I(inode)->i_xattr_sem); |
| 826 | err = __f2fs_setxattr(inode, index, name, value, size, NULL, flags); |
| 827 | f2fs_up_write(sem: &F2FS_I(inode)->i_xattr_sem); |
| 828 | f2fs_unlock_op(sbi); |
| 829 | |
| 830 | f2fs_update_time(sbi, type: REQ_TIME); |
| 831 | return err; |
| 832 | } |
| 833 | |
| 834 | int __init f2fs_init_xattr_cache(void) |
| 835 | { |
| 836 | inline_xattr_slab = f2fs_kmem_cache_create(name: "f2fs_xattr_entry" , |
| 837 | DEFAULT_XATTR_SLAB_SIZE); |
| 838 | return inline_xattr_slab ? 0 : -ENOMEM; |
| 839 | } |
| 840 | |
| 841 | void f2fs_destroy_xattr_cache(void) |
| 842 | { |
| 843 | kmem_cache_destroy(s: inline_xattr_slab); |
| 844 | } |