]>
Commit | Line | Data |
---|---|---|
0a8165d7 | 1 | /* |
af48b85b JK |
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 <[email protected]> | |
10 | * | |
11 | * Fix by Harrison Xing <[email protected]>. | |
12 | * Extended attributes for symlinks and special files added per | |
13 | * suggestion of Luka Renko <[email protected]>. | |
14 | * xattr consolidation Copyright (c) 2004 James Morris <[email protected]>, | |
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> | |
8ae8f162 | 23 | #include <linux/security.h> |
a6dda0e6 | 24 | #include <linux/posix_acl_xattr.h> |
af48b85b JK |
25 | #include "f2fs.h" |
26 | #include "xattr.h" | |
27 | ||
d9a82a04 | 28 | static int f2fs_xattr_generic_get(const struct xattr_handler *handler, |
b296821a AV |
29 | struct dentry *unused, struct inode *inode, |
30 | const char *name, void *buffer, size_t size) | |
af48b85b | 31 | { |
b296821a | 32 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 33 | |
d9a82a04 | 34 | switch (handler->flags) { |
af48b85b JK |
35 | case F2FS_XATTR_INDEX_USER: |
36 | if (!test_opt(sbi, XATTR_USER)) | |
37 | return -EOPNOTSUPP; | |
38 | break; | |
39 | case F2FS_XATTR_INDEX_TRUSTED: | |
40 | if (!capable(CAP_SYS_ADMIN)) | |
41 | return -EPERM; | |
42 | break; | |
8ae8f162 JK |
43 | case F2FS_XATTR_INDEX_SECURITY: |
44 | break; | |
af48b85b JK |
45 | default: |
46 | return -EINVAL; | |
47 | } | |
b296821a | 48 | return f2fs_getxattr(inode, handler->flags, name, |
d9a82a04 | 49 | buffer, size, NULL); |
af48b85b JK |
50 | } |
51 | ||
d9a82a04 | 52 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
59301226 AV |
53 | struct dentry *unused, struct inode *inode, |
54 | const char *name, const void *value, | |
d9a82a04 | 55 | size_t size, int flags) |
af48b85b | 56 | { |
59301226 | 57 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 58 | |
d9a82a04 | 59 | switch (handler->flags) { |
af48b85b JK |
60 | case F2FS_XATTR_INDEX_USER: |
61 | if (!test_opt(sbi, XATTR_USER)) | |
62 | return -EOPNOTSUPP; | |
63 | break; | |
64 | case F2FS_XATTR_INDEX_TRUSTED: | |
65 | if (!capable(CAP_SYS_ADMIN)) | |
66 | return -EPERM; | |
67 | break; | |
8ae8f162 JK |
68 | case F2FS_XATTR_INDEX_SECURITY: |
69 | break; | |
af48b85b JK |
70 | default: |
71 | return -EINVAL; | |
72 | } | |
59301226 | 73 | return f2fs_setxattr(inode, handler->flags, name, |
c02745ef | 74 | value, size, NULL, flags); |
af48b85b JK |
75 | } |
76 | ||
764a5c6b | 77 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
573ea5fc | 78 | { |
764a5c6b AG |
79 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
80 | ||
81 | return test_opt(sbi, XATTR_USER); | |
82 | } | |
573ea5fc | 83 | |
764a5c6b AG |
84 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
85 | { | |
86 | return capable(CAP_SYS_ADMIN); | |
573ea5fc JK |
87 | } |
88 | ||
d9a82a04 | 89 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
b296821a AV |
90 | struct dentry *unused, struct inode *inode, |
91 | const char *name, void *buffer, size_t size) | |
573ea5fc | 92 | { |
84e97c27 CY |
93 | if (buffer) |
94 | *((char *)buffer) = F2FS_I(inode)->i_advise; | |
573ea5fc JK |
95 | return sizeof(char); |
96 | } | |
97 | ||
d9a82a04 | 98 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
59301226 AV |
99 | struct dentry *unused, struct inode *inode, |
100 | const char *name, const void *value, | |
d9a82a04 | 101 | size_t size, int flags) |
573ea5fc | 102 | { |
573ea5fc JK |
103 | if (!inode_owner_or_capable(inode)) |
104 | return -EPERM; | |
105 | if (value == NULL) | |
106 | return -EINVAL; | |
107 | ||
108 | F2FS_I(inode)->i_advise |= *(char *)value; | |
7c45729a | 109 | f2fs_mark_inode_dirty_sync(inode, true); |
573ea5fc JK |
110 | return 0; |
111 | } | |
112 | ||
8ae8f162 JK |
113 | #ifdef CONFIG_F2FS_FS_SECURITY |
114 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, | |
115 | void *page) | |
116 | { | |
117 | const struct xattr *xattr; | |
118 | int err = 0; | |
119 | ||
120 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
d631abda | 121 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
8ae8f162 | 122 | xattr->name, xattr->value, |
c02745ef | 123 | xattr->value_len, (struct page *)page, 0); |
8ae8f162 JK |
124 | if (err < 0) |
125 | break; | |
126 | } | |
127 | return err; | |
128 | } | |
129 | ||
130 | int f2fs_init_security(struct inode *inode, struct inode *dir, | |
131 | const struct qstr *qstr, struct page *ipage) | |
132 | { | |
133 | return security_inode_init_security(inode, dir, qstr, | |
134 | &f2fs_initxattrs, ipage); | |
135 | } | |
136 | #endif | |
137 | ||
af48b85b JK |
138 | const struct xattr_handler f2fs_xattr_user_handler = { |
139 | .prefix = XATTR_USER_PREFIX, | |
140 | .flags = F2FS_XATTR_INDEX_USER, | |
764a5c6b | 141 | .list = f2fs_xattr_user_list, |
af48b85b JK |
142 | .get = f2fs_xattr_generic_get, |
143 | .set = f2fs_xattr_generic_set, | |
144 | }; | |
145 | ||
146 | const struct xattr_handler f2fs_xattr_trusted_handler = { | |
147 | .prefix = XATTR_TRUSTED_PREFIX, | |
148 | .flags = F2FS_XATTR_INDEX_TRUSTED, | |
764a5c6b | 149 | .list = f2fs_xattr_trusted_list, |
af48b85b JK |
150 | .get = f2fs_xattr_generic_get, |
151 | .set = f2fs_xattr_generic_set, | |
152 | }; | |
153 | ||
573ea5fc | 154 | const struct xattr_handler f2fs_xattr_advise_handler = { |
98e9cb57 | 155 | .name = F2FS_SYSTEM_ADVISE_NAME, |
573ea5fc | 156 | .flags = F2FS_XATTR_INDEX_ADVISE, |
573ea5fc JK |
157 | .get = f2fs_xattr_advise_get, |
158 | .set = f2fs_xattr_advise_set, | |
159 | }; | |
160 | ||
8ae8f162 JK |
161 | const struct xattr_handler f2fs_xattr_security_handler = { |
162 | .prefix = XATTR_SECURITY_PREFIX, | |
163 | .flags = F2FS_XATTR_INDEX_SECURITY, | |
8ae8f162 JK |
164 | .get = f2fs_xattr_generic_get, |
165 | .set = f2fs_xattr_generic_set, | |
166 | }; | |
167 | ||
af48b85b JK |
168 | static const struct xattr_handler *f2fs_xattr_handler_map[] = { |
169 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, | |
170 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
a6dda0e6 CH |
171 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
172 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, | |
af48b85b JK |
173 | #endif |
174 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, | |
8ae8f162 JK |
175 | #ifdef CONFIG_F2FS_FS_SECURITY |
176 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, | |
177 | #endif | |
af48b85b JK |
178 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
179 | }; | |
180 | ||
181 | const struct xattr_handler *f2fs_xattr_handlers[] = { | |
182 | &f2fs_xattr_user_handler, | |
183 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
a6dda0e6 CH |
184 | &posix_acl_access_xattr_handler, |
185 | &posix_acl_default_xattr_handler, | |
af48b85b JK |
186 | #endif |
187 | &f2fs_xattr_trusted_handler, | |
8ae8f162 JK |
188 | #ifdef CONFIG_F2FS_FS_SECURITY |
189 | &f2fs_xattr_security_handler, | |
190 | #endif | |
af48b85b JK |
191 | &f2fs_xattr_advise_handler, |
192 | NULL, | |
193 | }; | |
194 | ||
e1123268 | 195 | static inline const struct xattr_handler *f2fs_xattr_handler(int index) |
af48b85b JK |
196 | { |
197 | const struct xattr_handler *handler = NULL; | |
198 | ||
e1123268 JK |
199 | if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map)) |
200 | handler = f2fs_xattr_handler_map[index]; | |
af48b85b JK |
201 | return handler; |
202 | } | |
203 | ||
e1123268 JK |
204 | static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index, |
205 | size_t len, const char *name) | |
dd9cfe23 JK |
206 | { |
207 | struct f2fs_xattr_entry *entry; | |
208 | ||
209 | list_for_each_xattr(entry, base_addr) { | |
e1123268 | 210 | if (entry->e_name_index != index) |
dd9cfe23 | 211 | continue; |
e1123268 | 212 | if (entry->e_name_len != len) |
dd9cfe23 | 213 | continue; |
e1123268 | 214 | if (!memcmp(entry->e_name, name, len)) |
dd9cfe23 JK |
215 | break; |
216 | } | |
217 | return entry; | |
218 | } | |
219 | ||
ba38c27e CY |
220 | static struct f2fs_xattr_entry *__find_inline_xattr(void *base_addr, |
221 | void **last_addr, int index, | |
222 | size_t len, const char *name) | |
223 | { | |
224 | struct f2fs_xattr_entry *entry; | |
225 | unsigned int inline_size = F2FS_INLINE_XATTR_ADDRS << 2; | |
226 | ||
227 | list_for_each_xattr(entry, base_addr) { | |
228 | if ((void *)entry + sizeof(__u32) > base_addr + inline_size || | |
229 | (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) > | |
230 | base_addr + inline_size) { | |
231 | *last_addr = entry; | |
232 | return NULL; | |
233 | } | |
234 | if (entry->e_name_index != index) | |
235 | continue; | |
236 | if (entry->e_name_len != len) | |
237 | continue; | |
238 | if (!memcmp(entry->e_name, name, len)) | |
239 | break; | |
240 | } | |
241 | return entry; | |
242 | } | |
243 | ||
244 | static int lookup_all_xattrs(struct inode *inode, struct page *ipage, | |
245 | unsigned int index, unsigned int len, | |
246 | const char *name, struct f2fs_xattr_entry **xe, | |
247 | void **base_addr) | |
248 | { | |
249 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
250 | void *cur_addr, *txattr_addr, *last_addr = NULL; | |
251 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
252 | unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0; | |
253 | unsigned int inline_size = 0; | |
254 | int err = 0; | |
255 | ||
256 | inline_size = inline_xattr_size(inode); | |
257 | ||
258 | if (!size && !inline_size) | |
259 | return -ENODATA; | |
260 | ||
261 | txattr_addr = kzalloc(inline_size + size + sizeof(__u32), | |
262 | GFP_F2FS_ZERO); | |
263 | if (!txattr_addr) | |
264 | return -ENOMEM; | |
265 | ||
266 | /* read from inline xattr */ | |
267 | if (inline_size) { | |
268 | struct page *page = NULL; | |
269 | void *inline_addr; | |
270 | ||
271 | if (ipage) { | |
272 | inline_addr = inline_xattr_addr(ipage); | |
273 | } else { | |
274 | page = get_node_page(sbi, inode->i_ino); | |
275 | if (IS_ERR(page)) { | |
276 | err = PTR_ERR(page); | |
277 | goto out; | |
278 | } | |
279 | inline_addr = inline_xattr_addr(page); | |
280 | } | |
281 | memcpy(txattr_addr, inline_addr, inline_size); | |
282 | f2fs_put_page(page, 1); | |
283 | ||
284 | *xe = __find_inline_xattr(txattr_addr, &last_addr, | |
285 | index, len, name); | |
286 | if (*xe) | |
287 | goto check; | |
288 | } | |
289 | ||
290 | /* read from xattr node block */ | |
291 | if (xnid) { | |
292 | struct page *xpage; | |
293 | void *xattr_addr; | |
294 | ||
295 | /* The inode already has an extended attribute block. */ | |
296 | xpage = get_node_page(sbi, xnid); | |
297 | if (IS_ERR(xpage)) { | |
298 | err = PTR_ERR(xpage); | |
299 | goto out; | |
300 | } | |
301 | ||
302 | xattr_addr = page_address(xpage); | |
303 | memcpy(txattr_addr + inline_size, xattr_addr, size); | |
304 | f2fs_put_page(xpage, 1); | |
305 | } | |
306 | ||
307 | if (last_addr) | |
308 | cur_addr = XATTR_HDR(last_addr) - 1; | |
309 | else | |
310 | cur_addr = txattr_addr; | |
311 | ||
312 | *xe = __find_xattr(cur_addr, index, len, name); | |
313 | check: | |
314 | if (IS_XATTR_LAST_ENTRY(*xe)) { | |
315 | err = -ENODATA; | |
316 | goto out; | |
317 | } | |
318 | ||
319 | *base_addr = txattr_addr; | |
320 | return 0; | |
321 | out: | |
322 | kzfree(txattr_addr); | |
323 | return err; | |
324 | } | |
325 | ||
86696966 CY |
326 | static int read_all_xattrs(struct inode *inode, struct page *ipage, |
327 | void **base_addr) | |
65985d93 | 328 | { |
4081363f | 329 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
65985d93 JK |
330 | struct f2fs_xattr_header *header; |
331 | size_t size = PAGE_SIZE, inline_size = 0; | |
332 | void *txattr_addr; | |
86696966 | 333 | int err; |
65985d93 JK |
334 | |
335 | inline_size = inline_xattr_size(inode); | |
336 | ||
808a1d74 | 337 | txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO); |
65985d93 | 338 | if (!txattr_addr) |
86696966 | 339 | return -ENOMEM; |
65985d93 JK |
340 | |
341 | /* read from inline xattr */ | |
342 | if (inline_size) { | |
343 | struct page *page = NULL; | |
344 | void *inline_addr; | |
345 | ||
346 | if (ipage) { | |
347 | inline_addr = inline_xattr_addr(ipage); | |
348 | } else { | |
349 | page = get_node_page(sbi, inode->i_ino); | |
86696966 CY |
350 | if (IS_ERR(page)) { |
351 | err = PTR_ERR(page); | |
65985d93 | 352 | goto fail; |
86696966 | 353 | } |
65985d93 JK |
354 | inline_addr = inline_xattr_addr(page); |
355 | } | |
356 | memcpy(txattr_addr, inline_addr, inline_size); | |
357 | f2fs_put_page(page, 1); | |
358 | } | |
359 | ||
360 | /* read from xattr node block */ | |
361 | if (F2FS_I(inode)->i_xattr_nid) { | |
362 | struct page *xpage; | |
363 | void *xattr_addr; | |
364 | ||
365 | /* The inode already has an extended attribute block. */ | |
366 | xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); | |
86696966 CY |
367 | if (IS_ERR(xpage)) { |
368 | err = PTR_ERR(xpage); | |
65985d93 | 369 | goto fail; |
86696966 | 370 | } |
65985d93 JK |
371 | |
372 | xattr_addr = page_address(xpage); | |
373 | memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE); | |
374 | f2fs_put_page(xpage, 1); | |
375 | } | |
376 | ||
377 | header = XATTR_HDR(txattr_addr); | |
378 | ||
379 | /* never been allocated xattrs */ | |
380 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { | |
381 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); | |
382 | header->h_refcount = cpu_to_le32(1); | |
383 | } | |
86696966 CY |
384 | *base_addr = txattr_addr; |
385 | return 0; | |
65985d93 JK |
386 | fail: |
387 | kzfree(txattr_addr); | |
86696966 | 388 | return err; |
65985d93 JK |
389 | } |
390 | ||
391 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, | |
392 | void *txattr_addr, struct page *ipage) | |
393 | { | |
4081363f | 394 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
65985d93 JK |
395 | size_t inline_size = 0; |
396 | void *xattr_addr; | |
397 | struct page *xpage; | |
398 | nid_t new_nid = 0; | |
399 | int err; | |
400 | ||
401 | inline_size = inline_xattr_size(inode); | |
402 | ||
403 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) | |
404 | if (!alloc_nid(sbi, &new_nid)) | |
405 | return -ENOSPC; | |
406 | ||
407 | /* write to inline xattr */ | |
408 | if (inline_size) { | |
409 | struct page *page = NULL; | |
410 | void *inline_addr; | |
411 | ||
412 | if (ipage) { | |
413 | inline_addr = inline_xattr_addr(ipage); | |
fec1d657 | 414 | f2fs_wait_on_page_writeback(ipage, NODE, true); |
ee6d182f | 415 | set_page_dirty(ipage); |
65985d93 JK |
416 | } else { |
417 | page = get_node_page(sbi, inode->i_ino); | |
418 | if (IS_ERR(page)) { | |
419 | alloc_nid_failed(sbi, new_nid); | |
420 | return PTR_ERR(page); | |
421 | } | |
422 | inline_addr = inline_xattr_addr(page); | |
fec1d657 | 423 | f2fs_wait_on_page_writeback(page, NODE, true); |
65985d93 JK |
424 | } |
425 | memcpy(inline_addr, txattr_addr, inline_size); | |
426 | f2fs_put_page(page, 1); | |
427 | ||
428 | /* no need to use xattr node block */ | |
429 | if (hsize <= inline_size) { | |
430 | err = truncate_xattr_node(inode, ipage); | |
431 | alloc_nid_failed(sbi, new_nid); | |
432 | return err; | |
433 | } | |
434 | } | |
435 | ||
436 | /* write to xattr node block */ | |
437 | if (F2FS_I(inode)->i_xattr_nid) { | |
438 | xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); | |
439 | if (IS_ERR(xpage)) { | |
440 | alloc_nid_failed(sbi, new_nid); | |
441 | return PTR_ERR(xpage); | |
442 | } | |
9850cf4a | 443 | f2fs_bug_on(sbi, new_nid); |
fec1d657 | 444 | f2fs_wait_on_page_writeback(xpage, NODE, true); |
65985d93 JK |
445 | } else { |
446 | struct dnode_of_data dn; | |
447 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); | |
448 | xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage); | |
449 | if (IS_ERR(xpage)) { | |
450 | alloc_nid_failed(sbi, new_nid); | |
451 | return PTR_ERR(xpage); | |
452 | } | |
453 | alloc_nid_done(sbi, new_nid); | |
454 | } | |
455 | ||
456 | xattr_addr = page_address(xpage); | |
ba38c27e | 457 | memcpy(xattr_addr, txattr_addr + inline_size, MAX_XATTR_BLOCK_SIZE); |
65985d93 JK |
458 | set_page_dirty(xpage); |
459 | f2fs_put_page(xpage, 1); | |
460 | ||
65985d93 JK |
461 | return 0; |
462 | } | |
463 | ||
e1123268 | 464 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
bce8d112 | 465 | void *buffer, size_t buffer_size, struct page *ipage) |
af48b85b | 466 | { |
ba38c27e | 467 | struct f2fs_xattr_entry *entry = NULL; |
dd9cfe23 | 468 | int error = 0; |
ba38c27e CY |
469 | unsigned int size, len; |
470 | char *pval; | |
471 | void *base_addr = NULL; | |
af48b85b JK |
472 | |
473 | if (name == NULL) | |
474 | return -EINVAL; | |
e1123268 JK |
475 | |
476 | len = strlen(name); | |
477 | if (len > F2FS_NAME_LEN) | |
6e452d69 | 478 | return -ERANGE; |
af48b85b | 479 | |
ba38c27e CY |
480 | error = lookup_all_xattrs(inode, ipage, index, len, name, |
481 | &entry, &base_addr); | |
86696966 CY |
482 | if (error) |
483 | return error; | |
af48b85b | 484 | |
e1123268 | 485 | size = le16_to_cpu(entry->e_value_size); |
af48b85b | 486 | |
e1123268 | 487 | if (buffer && size > buffer_size) { |
af48b85b | 488 | error = -ERANGE; |
ba38c27e | 489 | goto out; |
af48b85b JK |
490 | } |
491 | ||
ba38c27e CY |
492 | pval = entry->e_name + entry->e_name_len; |
493 | ||
af48b85b JK |
494 | if (buffer) { |
495 | char *pval = entry->e_name + entry->e_name_len; | |
e1123268 | 496 | memcpy(buffer, pval, size); |
af48b85b | 497 | } |
e1123268 | 498 | error = size; |
ba38c27e | 499 | out: |
65985d93 | 500 | kzfree(base_addr); |
af48b85b JK |
501 | return error; |
502 | } | |
503 | ||
504 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
505 | { | |
2b0143b5 | 506 | struct inode *inode = d_inode(dentry); |
af48b85b | 507 | struct f2fs_xattr_entry *entry; |
af48b85b JK |
508 | void *base_addr; |
509 | int error = 0; | |
510 | size_t rest = buffer_size; | |
511 | ||
86696966 CY |
512 | error = read_all_xattrs(inode, NULL, &base_addr); |
513 | if (error) | |
514 | return error; | |
af48b85b JK |
515 | |
516 | list_for_each_xattr(entry, base_addr) { | |
517 | const struct xattr_handler *handler = | |
518 | f2fs_xattr_handler(entry->e_name_index); | |
764a5c6b AG |
519 | const char *prefix; |
520 | size_t prefix_len; | |
af48b85b JK |
521 | size_t size; |
522 | ||
764a5c6b | 523 | if (!handler || (handler->list && !handler->list(dentry))) |
af48b85b JK |
524 | continue; |
525 | ||
764a5c6b AG |
526 | prefix = handler->prefix ?: handler->name; |
527 | prefix_len = strlen(prefix); | |
528 | size = prefix_len + entry->e_name_len + 1; | |
529 | if (buffer) { | |
530 | if (size > rest) { | |
531 | error = -ERANGE; | |
532 | goto cleanup; | |
533 | } | |
534 | memcpy(buffer, prefix, prefix_len); | |
535 | buffer += prefix_len; | |
536 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
537 | buffer += entry->e_name_len; | |
538 | *buffer++ = 0; | |
af48b85b | 539 | } |
af48b85b JK |
540 | rest -= size; |
541 | } | |
542 | error = buffer_size - rest; | |
543 | cleanup: | |
65985d93 | 544 | kzfree(base_addr); |
af48b85b JK |
545 | return error; |
546 | } | |
547 | ||
e1123268 JK |
548 | static int __f2fs_setxattr(struct inode *inode, int index, |
549 | const char *name, const void *value, size_t size, | |
c02745ef | 550 | struct page *ipage, int flags) |
af48b85b | 551 | { |
af48b85b | 552 | struct f2fs_xattr_entry *here, *last; |
af48b85b | 553 | void *base_addr; |
65985d93 | 554 | int found, newsize; |
e1123268 | 555 | size_t len; |
65985d93 | 556 | __u32 new_hsize; |
a0995af6 | 557 | int error = 0; |
af48b85b JK |
558 | |
559 | if (name == NULL) | |
560 | return -EINVAL; | |
af48b85b JK |
561 | |
562 | if (value == NULL) | |
e1123268 | 563 | size = 0; |
af48b85b | 564 | |
e1123268 | 565 | len = strlen(name); |
7c909772 | 566 | |
037fe70c | 567 | if (len > F2FS_NAME_LEN) |
af48b85b JK |
568 | return -ERANGE; |
569 | ||
037fe70c CY |
570 | if (size > MAX_VALUE_LEN(inode)) |
571 | return -E2BIG; | |
572 | ||
86696966 CY |
573 | error = read_all_xattrs(inode, ipage, &base_addr); |
574 | if (error) | |
575 | return error; | |
af48b85b JK |
576 | |
577 | /* find entry with wanted name. */ | |
e1123268 | 578 | here = __find_xattr(base_addr, index, len, name); |
af48b85b | 579 | |
dd9cfe23 | 580 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
af48b85b | 581 | |
916decbf JK |
582 | if ((flags & XATTR_REPLACE) && !found) { |
583 | error = -ENODATA; | |
584 | goto exit; | |
585 | } else if ((flags & XATTR_CREATE) && found) { | |
586 | error = -EEXIST; | |
587 | goto exit; | |
588 | } | |
589 | ||
590 | last = here; | |
af48b85b JK |
591 | while (!IS_XATTR_LAST_ENTRY(last)) |
592 | last = XATTR_NEXT_ENTRY(last); | |
593 | ||
e1123268 | 594 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
af48b85b JK |
595 | |
596 | /* 1. Check space */ | |
597 | if (value) { | |
65985d93 JK |
598 | int free; |
599 | /* | |
600 | * If value is NULL, it is remove operation. | |
e1c42045 | 601 | * In case of update operation, we calculate free. |
af48b85b | 602 | */ |
65985d93 | 603 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
af48b85b | 604 | if (found) |
cc3de6a3 | 605 | free = free + ENTRY_SIZE(here); |
af48b85b | 606 | |
6bacf52f | 607 | if (unlikely(free < newsize)) { |
58457f1c | 608 | error = -E2BIG; |
65985d93 | 609 | goto exit; |
af48b85b JK |
610 | } |
611 | } | |
612 | ||
613 | /* 2. Remove old entry */ | |
614 | if (found) { | |
65985d93 JK |
615 | /* |
616 | * If entry is found, remove old entry. | |
af48b85b JK |
617 | * If not found, remove operation is not needed. |
618 | */ | |
619 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); | |
620 | int oldsize = ENTRY_SIZE(here); | |
621 | ||
622 | memmove(here, next, (char *)last - (char *)next); | |
623 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); | |
624 | memset(last, 0, oldsize); | |
625 | } | |
626 | ||
65985d93 JK |
627 | new_hsize = (char *)last - (char *)base_addr; |
628 | ||
af48b85b JK |
629 | /* 3. Write new entry */ |
630 | if (value) { | |
65985d93 JK |
631 | char *pval; |
632 | /* | |
633 | * Before we come here, old entry is removed. | |
634 | * We just write new entry. | |
635 | */ | |
e1123268 JK |
636 | last->e_name_index = index; |
637 | last->e_name_len = len; | |
638 | memcpy(last->e_name, name, len); | |
639 | pval = last->e_name + len; | |
640 | memcpy(pval, value, size); | |
641 | last->e_value_size = cpu_to_le16(size); | |
65985d93 | 642 | new_hsize += newsize; |
af48b85b JK |
643 | } |
644 | ||
65985d93 JK |
645 | error = write_all_xattrs(inode, new_hsize, base_addr, ipage); |
646 | if (error) | |
647 | goto exit; | |
af48b85b | 648 | |
91942321 JK |
649 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { |
650 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
c2050a45 | 651 | inode->i_ctime = current_time(inode); |
91942321 | 652 | clear_inode_flag(inode, FI_ACL_MODE); |
af48b85b | 653 | } |
f424f664 JK |
654 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
655 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) | |
656 | f2fs_set_encrypted_inode(inode); | |
7c45729a | 657 | f2fs_mark_inode_dirty_sync(inode, true); |
bbf156f7 JK |
658 | if (!error && S_ISDIR(inode->i_mode)) |
659 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); | |
7c909772 | 660 | exit: |
65985d93 | 661 | kzfree(base_addr); |
af48b85b JK |
662 | return error; |
663 | } | |
52ab9560 | 664 | |
e1123268 JK |
665 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
666 | const void *value, size_t size, | |
c02745ef | 667 | struct page *ipage, int flags) |
52ab9560 | 668 | { |
4081363f | 669 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
52ab9560 RK |
670 | int err; |
671 | ||
d631abda JK |
672 | /* this case is only from init_inode_metadata */ |
673 | if (ipage) | |
674 | return __f2fs_setxattr(inode, index, name, value, | |
675 | size, ipage, flags); | |
2c4db1a6 | 676 | f2fs_balance_fs(sbi, true); |
52ab9560 | 677 | |
e479556b | 678 | f2fs_lock_op(sbi); |
d928bfbf JK |
679 | /* protect xattr_ver */ |
680 | down_write(&F2FS_I(inode)->i_sem); | |
c02745ef | 681 | err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); |
d928bfbf | 682 | up_write(&F2FS_I(inode)->i_sem); |
e479556b | 683 | f2fs_unlock_op(sbi); |
52ab9560 | 684 | |
d0239e1b | 685 | f2fs_update_time(sbi, REQ_TIME); |
52ab9560 RK |
686 | return err; |
687 | } |