]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/f2fs/xattr.c
vfs: Distinguish between full xattr names and proper prefixes
[mirror_ubuntu-bionic-kernel.git] / fs / f2fs / xattr.c
1 /*
2 * fs/f2fs/xattr.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Portions of this code from linux/fs/ext2/xattr.c
8 *
9 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
10 *
11 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
12 * Extended attributes for symlinks and special files added per
13 * suggestion of Luka Renko <luka.renko@hermes.si>.
14 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
15 * Red Hat Inc.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21 #include <linux/rwsem.h>
22 #include <linux/f2fs_fs.h>
23 #include <linux/security.h>
24 #include <linux/posix_acl_xattr.h>
25 #include "f2fs.h"
26 #include "xattr.h"
27
28 static size_t f2fs_xattr_generic_list(const struct xattr_handler *handler,
29 struct dentry *dentry, char *list, size_t list_size,
30 const char *name, size_t len)
31 {
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33 const char *prefix;
34 int total_len, prefix_len;
35
36 switch (handler->flags) {
37 case F2FS_XATTR_INDEX_USER:
38 if (!test_opt(sbi, XATTR_USER))
39 return -EOPNOTSUPP;
40 break;
41 case F2FS_XATTR_INDEX_TRUSTED:
42 if (!capable(CAP_SYS_ADMIN))
43 return -EPERM;
44 break;
45 case F2FS_XATTR_INDEX_SECURITY:
46 break;
47 default:
48 return -EINVAL;
49 }
50
51 prefix = xattr_prefix(handler);
52 prefix_len = strlen(prefix);
53 total_len = prefix_len + len + 1;
54 if (list && total_len <= list_size) {
55 memcpy(list, prefix, prefix_len);
56 memcpy(list + prefix_len, name, len);
57 list[prefix_len + len] = '\0';
58 }
59 return total_len;
60 }
61
62 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
63 struct dentry *dentry, const char *name, void *buffer,
64 size_t size)
65 {
66 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
67
68 switch (handler->flags) {
69 case F2FS_XATTR_INDEX_USER:
70 if (!test_opt(sbi, XATTR_USER))
71 return -EOPNOTSUPP;
72 break;
73 case F2FS_XATTR_INDEX_TRUSTED:
74 if (!capable(CAP_SYS_ADMIN))
75 return -EPERM;
76 break;
77 case F2FS_XATTR_INDEX_SECURITY:
78 break;
79 default:
80 return -EINVAL;
81 }
82 return f2fs_getxattr(d_inode(dentry), handler->flags, name,
83 buffer, size, NULL);
84 }
85
86 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
87 struct dentry *dentry, const char *name, const void *value,
88 size_t size, int flags)
89 {
90 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
91
92 switch (handler->flags) {
93 case F2FS_XATTR_INDEX_USER:
94 if (!test_opt(sbi, XATTR_USER))
95 return -EOPNOTSUPP;
96 break;
97 case F2FS_XATTR_INDEX_TRUSTED:
98 if (!capable(CAP_SYS_ADMIN))
99 return -EPERM;
100 break;
101 case F2FS_XATTR_INDEX_SECURITY:
102 break;
103 default:
104 return -EINVAL;
105 }
106 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
107 value, size, NULL, flags);
108 }
109
110 static size_t f2fs_xattr_advise_list(const struct xattr_handler *handler,
111 struct dentry *dentry, char *list, size_t list_size,
112 const char *name, size_t len)
113 {
114 const char *xname = F2FS_SYSTEM_ADVISE_NAME;
115 size_t size;
116
117 size = strlen(xname) + 1;
118 if (list && size <= list_size)
119 memcpy(list, xname, size);
120 return size;
121 }
122
123 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
124 struct dentry *dentry, const char *name, void *buffer,
125 size_t size)
126 {
127 struct inode *inode = d_inode(dentry);
128
129 if (buffer)
130 *((char *)buffer) = F2FS_I(inode)->i_advise;
131 return sizeof(char);
132 }
133
134 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
135 struct dentry *dentry, const char *name, const void *value,
136 size_t size, int flags)
137 {
138 struct inode *inode = d_inode(dentry);
139
140 if (!inode_owner_or_capable(inode))
141 return -EPERM;
142 if (value == NULL)
143 return -EINVAL;
144
145 F2FS_I(inode)->i_advise |= *(char *)value;
146 mark_inode_dirty(inode);
147 return 0;
148 }
149
150 #ifdef CONFIG_F2FS_FS_SECURITY
151 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
152 void *page)
153 {
154 const struct xattr *xattr;
155 int err = 0;
156
157 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
158 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
159 xattr->name, xattr->value,
160 xattr->value_len, (struct page *)page, 0);
161 if (err < 0)
162 break;
163 }
164 return err;
165 }
166
167 int f2fs_init_security(struct inode *inode, struct inode *dir,
168 const struct qstr *qstr, struct page *ipage)
169 {
170 return security_inode_init_security(inode, dir, qstr,
171 &f2fs_initxattrs, ipage);
172 }
173 #endif
174
175 const struct xattr_handler f2fs_xattr_user_handler = {
176 .prefix = XATTR_USER_PREFIX,
177 .flags = F2FS_XATTR_INDEX_USER,
178 .list = f2fs_xattr_generic_list,
179 .get = f2fs_xattr_generic_get,
180 .set = f2fs_xattr_generic_set,
181 };
182
183 const struct xattr_handler f2fs_xattr_trusted_handler = {
184 .prefix = XATTR_TRUSTED_PREFIX,
185 .flags = F2FS_XATTR_INDEX_TRUSTED,
186 .list = f2fs_xattr_generic_list,
187 .get = f2fs_xattr_generic_get,
188 .set = f2fs_xattr_generic_set,
189 };
190
191 const struct xattr_handler f2fs_xattr_advise_handler = {
192 .name = F2FS_SYSTEM_ADVISE_NAME,
193 .flags = F2FS_XATTR_INDEX_ADVISE,
194 .list = f2fs_xattr_advise_list,
195 .get = f2fs_xattr_advise_get,
196 .set = f2fs_xattr_advise_set,
197 };
198
199 const struct xattr_handler f2fs_xattr_security_handler = {
200 .prefix = XATTR_SECURITY_PREFIX,
201 .flags = F2FS_XATTR_INDEX_SECURITY,
202 .list = f2fs_xattr_generic_list,
203 .get = f2fs_xattr_generic_get,
204 .set = f2fs_xattr_generic_set,
205 };
206
207 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
208 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
209 #ifdef CONFIG_F2FS_FS_POSIX_ACL
210 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
211 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
212 #endif
213 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
214 #ifdef CONFIG_F2FS_FS_SECURITY
215 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
216 #endif
217 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
218 };
219
220 const struct xattr_handler *f2fs_xattr_handlers[] = {
221 &f2fs_xattr_user_handler,
222 #ifdef CONFIG_F2FS_FS_POSIX_ACL
223 &posix_acl_access_xattr_handler,
224 &posix_acl_default_xattr_handler,
225 #endif
226 &f2fs_xattr_trusted_handler,
227 #ifdef CONFIG_F2FS_FS_SECURITY
228 &f2fs_xattr_security_handler,
229 #endif
230 &f2fs_xattr_advise_handler,
231 NULL,
232 };
233
234 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
235 {
236 const struct xattr_handler *handler = NULL;
237
238 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
239 handler = f2fs_xattr_handler_map[index];
240 return handler;
241 }
242
243 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
244 size_t len, const char *name)
245 {
246 struct f2fs_xattr_entry *entry;
247
248 list_for_each_xattr(entry, base_addr) {
249 if (entry->e_name_index != index)
250 continue;
251 if (entry->e_name_len != len)
252 continue;
253 if (!memcmp(entry->e_name, name, len))
254 break;
255 }
256 return entry;
257 }
258
259 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
260 {
261 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
262 struct f2fs_xattr_header *header;
263 size_t size = PAGE_SIZE, inline_size = 0;
264 void *txattr_addr;
265
266 inline_size = inline_xattr_size(inode);
267
268 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
269 if (!txattr_addr)
270 return NULL;
271
272 /* read from inline xattr */
273 if (inline_size) {
274 struct page *page = NULL;
275 void *inline_addr;
276
277 if (ipage) {
278 inline_addr = inline_xattr_addr(ipage);
279 } else {
280 page = get_node_page(sbi, inode->i_ino);
281 if (IS_ERR(page))
282 goto fail;
283 inline_addr = inline_xattr_addr(page);
284 }
285 memcpy(txattr_addr, inline_addr, inline_size);
286 f2fs_put_page(page, 1);
287 }
288
289 /* read from xattr node block */
290 if (F2FS_I(inode)->i_xattr_nid) {
291 struct page *xpage;
292 void *xattr_addr;
293
294 /* The inode already has an extended attribute block. */
295 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
296 if (IS_ERR(xpage))
297 goto fail;
298
299 xattr_addr = page_address(xpage);
300 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
301 f2fs_put_page(xpage, 1);
302 }
303
304 header = XATTR_HDR(txattr_addr);
305
306 /* never been allocated xattrs */
307 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
308 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
309 header->h_refcount = cpu_to_le32(1);
310 }
311 return txattr_addr;
312 fail:
313 kzfree(txattr_addr);
314 return NULL;
315 }
316
317 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
318 void *txattr_addr, struct page *ipage)
319 {
320 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
321 size_t inline_size = 0;
322 void *xattr_addr;
323 struct page *xpage;
324 nid_t new_nid = 0;
325 int err;
326
327 inline_size = inline_xattr_size(inode);
328
329 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
330 if (!alloc_nid(sbi, &new_nid))
331 return -ENOSPC;
332
333 /* write to inline xattr */
334 if (inline_size) {
335 struct page *page = NULL;
336 void *inline_addr;
337
338 if (ipage) {
339 inline_addr = inline_xattr_addr(ipage);
340 f2fs_wait_on_page_writeback(ipage, NODE);
341 } else {
342 page = get_node_page(sbi, inode->i_ino);
343 if (IS_ERR(page)) {
344 alloc_nid_failed(sbi, new_nid);
345 return PTR_ERR(page);
346 }
347 inline_addr = inline_xattr_addr(page);
348 f2fs_wait_on_page_writeback(page, NODE);
349 }
350 memcpy(inline_addr, txattr_addr, inline_size);
351 f2fs_put_page(page, 1);
352
353 /* no need to use xattr node block */
354 if (hsize <= inline_size) {
355 err = truncate_xattr_node(inode, ipage);
356 alloc_nid_failed(sbi, new_nid);
357 return err;
358 }
359 }
360
361 /* write to xattr node block */
362 if (F2FS_I(inode)->i_xattr_nid) {
363 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
364 if (IS_ERR(xpage)) {
365 alloc_nid_failed(sbi, new_nid);
366 return PTR_ERR(xpage);
367 }
368 f2fs_bug_on(sbi, new_nid);
369 f2fs_wait_on_page_writeback(xpage, NODE);
370 } else {
371 struct dnode_of_data dn;
372 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
373 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
374 if (IS_ERR(xpage)) {
375 alloc_nid_failed(sbi, new_nid);
376 return PTR_ERR(xpage);
377 }
378 alloc_nid_done(sbi, new_nid);
379 }
380
381 xattr_addr = page_address(xpage);
382 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
383 sizeof(struct node_footer));
384 set_page_dirty(xpage);
385 f2fs_put_page(xpage, 1);
386
387 /* need to checkpoint during fsync */
388 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
389 return 0;
390 }
391
392 int f2fs_getxattr(struct inode *inode, int index, const char *name,
393 void *buffer, size_t buffer_size, struct page *ipage)
394 {
395 struct f2fs_xattr_entry *entry;
396 void *base_addr;
397 int error = 0;
398 size_t size, len;
399
400 if (name == NULL)
401 return -EINVAL;
402
403 len = strlen(name);
404 if (len > F2FS_NAME_LEN)
405 return -ERANGE;
406
407 base_addr = read_all_xattrs(inode, ipage);
408 if (!base_addr)
409 return -ENOMEM;
410
411 entry = __find_xattr(base_addr, index, len, name);
412 if (IS_XATTR_LAST_ENTRY(entry)) {
413 error = -ENODATA;
414 goto cleanup;
415 }
416
417 size = le16_to_cpu(entry->e_value_size);
418
419 if (buffer && size > buffer_size) {
420 error = -ERANGE;
421 goto cleanup;
422 }
423
424 if (buffer) {
425 char *pval = entry->e_name + entry->e_name_len;
426 memcpy(buffer, pval, size);
427 }
428 error = size;
429
430 cleanup:
431 kzfree(base_addr);
432 return error;
433 }
434
435 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
436 {
437 struct inode *inode = d_inode(dentry);
438 struct f2fs_xattr_entry *entry;
439 void *base_addr;
440 int error = 0;
441 size_t rest = buffer_size;
442
443 base_addr = read_all_xattrs(inode, NULL);
444 if (!base_addr)
445 return -ENOMEM;
446
447 list_for_each_xattr(entry, base_addr) {
448 const struct xattr_handler *handler =
449 f2fs_xattr_handler(entry->e_name_index);
450 size_t size;
451
452 if (!handler)
453 continue;
454
455 size = handler->list(handler, dentry, buffer, rest,
456 entry->e_name, entry->e_name_len);
457 if (buffer && size > rest) {
458 error = -ERANGE;
459 goto cleanup;
460 }
461
462 if (buffer)
463 buffer += size;
464 rest -= size;
465 }
466 error = buffer_size - rest;
467 cleanup:
468 kzfree(base_addr);
469 return error;
470 }
471
472 static int __f2fs_setxattr(struct inode *inode, int index,
473 const char *name, const void *value, size_t size,
474 struct page *ipage, int flags)
475 {
476 struct f2fs_inode_info *fi = F2FS_I(inode);
477 struct f2fs_xattr_entry *here, *last;
478 void *base_addr;
479 int found, newsize;
480 size_t len;
481 __u32 new_hsize;
482 int error = -ENOMEM;
483
484 if (name == NULL)
485 return -EINVAL;
486
487 if (value == NULL)
488 size = 0;
489
490 len = strlen(name);
491
492 if (len > F2FS_NAME_LEN)
493 return -ERANGE;
494
495 if (size > MAX_VALUE_LEN(inode))
496 return -E2BIG;
497
498 base_addr = read_all_xattrs(inode, ipage);
499 if (!base_addr)
500 goto exit;
501
502 /* find entry with wanted name. */
503 here = __find_xattr(base_addr, index, len, name);
504
505 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
506
507 if ((flags & XATTR_REPLACE) && !found) {
508 error = -ENODATA;
509 goto exit;
510 } else if ((flags & XATTR_CREATE) && found) {
511 error = -EEXIST;
512 goto exit;
513 }
514
515 last = here;
516 while (!IS_XATTR_LAST_ENTRY(last))
517 last = XATTR_NEXT_ENTRY(last);
518
519 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
520
521 /* 1. Check space */
522 if (value) {
523 int free;
524 /*
525 * If value is NULL, it is remove operation.
526 * In case of update operation, we calculate free.
527 */
528 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
529 if (found)
530 free = free + ENTRY_SIZE(here);
531
532 if (unlikely(free < newsize)) {
533 error = -ENOSPC;
534 goto exit;
535 }
536 }
537
538 /* 2. Remove old entry */
539 if (found) {
540 /*
541 * If entry is found, remove old entry.
542 * If not found, remove operation is not needed.
543 */
544 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
545 int oldsize = ENTRY_SIZE(here);
546
547 memmove(here, next, (char *)last - (char *)next);
548 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
549 memset(last, 0, oldsize);
550 }
551
552 new_hsize = (char *)last - (char *)base_addr;
553
554 /* 3. Write new entry */
555 if (value) {
556 char *pval;
557 /*
558 * Before we come here, old entry is removed.
559 * We just write new entry.
560 */
561 memset(last, 0, newsize);
562 last->e_name_index = index;
563 last->e_name_len = len;
564 memcpy(last->e_name, name, len);
565 pval = last->e_name + len;
566 memcpy(pval, value, size);
567 last->e_value_size = cpu_to_le16(size);
568 new_hsize += newsize;
569 }
570
571 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
572 if (error)
573 goto exit;
574
575 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
576 inode->i_mode = fi->i_acl_mode;
577 inode->i_ctime = CURRENT_TIME;
578 clear_inode_flag(fi, FI_ACL_MODE);
579 }
580 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
581 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
582 f2fs_set_encrypted_inode(inode);
583
584 if (ipage)
585 update_inode(inode, ipage);
586 else
587 update_inode_page(inode);
588 exit:
589 kzfree(base_addr);
590 return error;
591 }
592
593 int f2fs_setxattr(struct inode *inode, int index, const char *name,
594 const void *value, size_t size,
595 struct page *ipage, int flags)
596 {
597 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
598 int err;
599
600 /* this case is only from init_inode_metadata */
601 if (ipage)
602 return __f2fs_setxattr(inode, index, name, value,
603 size, ipage, flags);
604 f2fs_balance_fs(sbi);
605
606 f2fs_lock_op(sbi);
607 /* protect xattr_ver */
608 down_write(&F2FS_I(inode)->i_sem);
609 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
610 up_write(&F2FS_I(inode)->i_sem);
611 f2fs_unlock_op(sbi);
612
613 return err;
614 }