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