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