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