]>
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 | ||
6afc662e CY |
220 | static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode, |
221 | void *base_addr, void **last_addr, int index, | |
222 | size_t len, const char *name) | |
ba38c27e CY |
223 | { |
224 | struct f2fs_xattr_entry *entry; | |
6afc662e | 225 | unsigned int inline_size = inline_xattr_size(inode); |
ba38c27e CY |
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 | ||
a5f433f7 CY |
244 | static int read_inline_xattr(struct inode *inode, struct page *ipage, |
245 | void *txattr_addr) | |
246 | { | |
247 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
248 | unsigned int inline_size = inline_xattr_size(inode); | |
249 | struct page *page = NULL; | |
250 | void *inline_addr; | |
251 | ||
252 | if (ipage) { | |
6afc662e | 253 | inline_addr = inline_xattr_addr(inode, ipage); |
a5f433f7 CY |
254 | } else { |
255 | page = get_node_page(sbi, inode->i_ino); | |
256 | if (IS_ERR(page)) | |
257 | return PTR_ERR(page); | |
258 | ||
6afc662e | 259 | inline_addr = inline_xattr_addr(inode, page); |
a5f433f7 CY |
260 | } |
261 | memcpy(txattr_addr, inline_addr, inline_size); | |
262 | f2fs_put_page(page, 1); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
63840695 CY |
267 | static int read_xattr_block(struct inode *inode, void *txattr_addr) |
268 | { | |
269 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
270 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
271 | unsigned int inline_size = inline_xattr_size(inode); | |
272 | struct page *xpage; | |
273 | void *xattr_addr; | |
274 | ||
275 | /* The inode already has an extended attribute block. */ | |
276 | xpage = get_node_page(sbi, xnid); | |
277 | if (IS_ERR(xpage)) | |
278 | return PTR_ERR(xpage); | |
279 | ||
280 | xattr_addr = page_address(xpage); | |
281 | memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); | |
282 | f2fs_put_page(xpage, 1); | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
ba38c27e CY |
287 | static int lookup_all_xattrs(struct inode *inode, struct page *ipage, |
288 | unsigned int index, unsigned int len, | |
289 | const char *name, struct f2fs_xattr_entry **xe, | |
290 | void **base_addr) | |
291 | { | |
ba38c27e CY |
292 | void *cur_addr, *txattr_addr, *last_addr = NULL; |
293 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
294 | unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0; | |
89e9eabd | 295 | unsigned int inline_size = inline_xattr_size(inode); |
ba38c27e CY |
296 | int err = 0; |
297 | ||
ba38c27e CY |
298 | if (!size && !inline_size) |
299 | return -ENODATA; | |
300 | ||
22588f87 | 301 | txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE, |
ba38c27e CY |
302 | GFP_F2FS_ZERO); |
303 | if (!txattr_addr) | |
304 | return -ENOMEM; | |
305 | ||
306 | /* read from inline xattr */ | |
307 | if (inline_size) { | |
a5f433f7 CY |
308 | err = read_inline_xattr(inode, ipage, txattr_addr); |
309 | if (err) | |
310 | goto out; | |
ba38c27e | 311 | |
6afc662e | 312 | *xe = __find_inline_xattr(inode, txattr_addr, &last_addr, |
ba38c27e CY |
313 | index, len, name); |
314 | if (*xe) | |
315 | goto check; | |
316 | } | |
317 | ||
318 | /* read from xattr node block */ | |
319 | if (xnid) { | |
63840695 CY |
320 | err = read_xattr_block(inode, txattr_addr); |
321 | if (err) | |
ba38c27e | 322 | goto out; |
ba38c27e CY |
323 | } |
324 | ||
325 | if (last_addr) | |
326 | cur_addr = XATTR_HDR(last_addr) - 1; | |
327 | else | |
328 | cur_addr = txattr_addr; | |
329 | ||
330 | *xe = __find_xattr(cur_addr, index, len, name); | |
331 | check: | |
332 | if (IS_XATTR_LAST_ENTRY(*xe)) { | |
333 | err = -ENODATA; | |
334 | goto out; | |
335 | } | |
336 | ||
337 | *base_addr = txattr_addr; | |
338 | return 0; | |
339 | out: | |
340 | kzfree(txattr_addr); | |
341 | return err; | |
342 | } | |
343 | ||
86696966 CY |
344 | static int read_all_xattrs(struct inode *inode, struct page *ipage, |
345 | void **base_addr) | |
65985d93 | 346 | { |
65985d93 | 347 | struct f2fs_xattr_header *header; |
89e9eabd CY |
348 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
349 | unsigned int size = VALID_XATTR_BLOCK_SIZE; | |
350 | unsigned int inline_size = inline_xattr_size(inode); | |
65985d93 | 351 | void *txattr_addr; |
86696966 | 352 | int err; |
65985d93 | 353 | |
22588f87 | 354 | txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE, |
89e9eabd | 355 | GFP_F2FS_ZERO); |
65985d93 | 356 | if (!txattr_addr) |
86696966 | 357 | return -ENOMEM; |
65985d93 JK |
358 | |
359 | /* read from inline xattr */ | |
360 | if (inline_size) { | |
a5f433f7 CY |
361 | err = read_inline_xattr(inode, ipage, txattr_addr); |
362 | if (err) | |
363 | goto fail; | |
65985d93 JK |
364 | } |
365 | ||
366 | /* read from xattr node block */ | |
89e9eabd | 367 | if (xnid) { |
63840695 CY |
368 | err = read_xattr_block(inode, txattr_addr); |
369 | if (err) | |
65985d93 | 370 | goto fail; |
65985d93 JK |
371 | } |
372 | ||
373 | header = XATTR_HDR(txattr_addr); | |
374 | ||
375 | /* never been allocated xattrs */ | |
376 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { | |
377 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); | |
378 | header->h_refcount = cpu_to_le32(1); | |
379 | } | |
86696966 CY |
380 | *base_addr = txattr_addr; |
381 | return 0; | |
65985d93 JK |
382 | fail: |
383 | kzfree(txattr_addr); | |
86696966 | 384 | return err; |
65985d93 JK |
385 | } |
386 | ||
387 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, | |
388 | void *txattr_addr, struct page *ipage) | |
389 | { | |
4081363f | 390 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
89e9eabd | 391 | size_t inline_size = inline_xattr_size(inode); |
bf9c1427 | 392 | struct page *in_page = NULL; |
65985d93 | 393 | void *xattr_addr; |
bf9c1427 | 394 | void *inline_addr = NULL; |
65985d93 JK |
395 | struct page *xpage; |
396 | nid_t new_nid = 0; | |
bf9c1427 | 397 | int err = 0; |
65985d93 | 398 | |
65985d93 JK |
399 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) |
400 | if (!alloc_nid(sbi, &new_nid)) | |
401 | return -ENOSPC; | |
402 | ||
403 | /* write to inline xattr */ | |
404 | if (inline_size) { | |
65985d93 | 405 | if (ipage) { |
6afc662e | 406 | inline_addr = inline_xattr_addr(inode, ipage); |
65985d93 | 407 | } else { |
bf9c1427 JK |
408 | in_page = get_node_page(sbi, inode->i_ino); |
409 | if (IS_ERR(in_page)) { | |
65985d93 | 410 | alloc_nid_failed(sbi, new_nid); |
bf9c1427 | 411 | return PTR_ERR(in_page); |
65985d93 | 412 | } |
bf9c1427 | 413 | inline_addr = inline_xattr_addr(inode, in_page); |
65985d93 | 414 | } |
65985d93 | 415 | |
bf9c1427 JK |
416 | f2fs_wait_on_page_writeback(ipage ? ipage : in_page, |
417 | NODE, true); | |
65985d93 JK |
418 | /* no need to use xattr node block */ |
419 | if (hsize <= inline_size) { | |
9c77f754 | 420 | err = truncate_xattr_node(inode); |
65985d93 | 421 | alloc_nid_failed(sbi, new_nid); |
bf9c1427 JK |
422 | if (err) { |
423 | f2fs_put_page(in_page, 1); | |
424 | return err; | |
425 | } | |
426 | memcpy(inline_addr, txattr_addr, inline_size); | |
427 | set_page_dirty(ipage ? ipage : in_page); | |
428 | goto in_page_out; | |
65985d93 JK |
429 | } |
430 | } | |
431 | ||
432 | /* write to xattr node block */ | |
433 | if (F2FS_I(inode)->i_xattr_nid) { | |
434 | xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); | |
435 | if (IS_ERR(xpage)) { | |
436 | alloc_nid_failed(sbi, new_nid); | |
bf9c1427 | 437 | goto in_page_out; |
65985d93 | 438 | } |
9850cf4a | 439 | f2fs_bug_on(sbi, new_nid); |
fec1d657 | 440 | f2fs_wait_on_page_writeback(xpage, NODE, true); |
65985d93 JK |
441 | } else { |
442 | struct dnode_of_data dn; | |
443 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); | |
5f4ce6ab | 444 | xpage = new_node_page(&dn, XATTR_NODE_OFFSET); |
65985d93 JK |
445 | if (IS_ERR(xpage)) { |
446 | alloc_nid_failed(sbi, new_nid); | |
bf9c1427 | 447 | goto in_page_out; |
65985d93 JK |
448 | } |
449 | alloc_nid_done(sbi, new_nid); | |
450 | } | |
65985d93 | 451 | xattr_addr = page_address(xpage); |
bf9c1427 JK |
452 | |
453 | if (inline_size) | |
454 | memcpy(inline_addr, txattr_addr, inline_size); | |
22588f87 | 455 | memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); |
bf9c1427 JK |
456 | |
457 | if (inline_size) | |
458 | set_page_dirty(ipage ? ipage : in_page); | |
65985d93 | 459 | set_page_dirty(xpage); |
65985d93 | 460 | |
bf9c1427 JK |
461 | f2fs_put_page(xpage, 1); |
462 | in_page_out: | |
463 | f2fs_put_page(in_page, 1); | |
464 | return err; | |
65985d93 JK |
465 | } |
466 | ||
e1123268 | 467 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
bce8d112 | 468 | void *buffer, size_t buffer_size, struct page *ipage) |
af48b85b | 469 | { |
ba38c27e | 470 | struct f2fs_xattr_entry *entry = NULL; |
dd9cfe23 | 471 | int error = 0; |
ba38c27e | 472 | unsigned int size, len; |
ba38c27e | 473 | void *base_addr = NULL; |
af48b85b JK |
474 | |
475 | if (name == NULL) | |
476 | return -EINVAL; | |
e1123268 JK |
477 | |
478 | len = strlen(name); | |
479 | if (len > F2FS_NAME_LEN) | |
6e452d69 | 480 | return -ERANGE; |
af48b85b | 481 | |
27161f13 | 482 | down_read(&F2FS_I(inode)->i_xattr_sem); |
ba38c27e CY |
483 | error = lookup_all_xattrs(inode, ipage, index, len, name, |
484 | &entry, &base_addr); | |
27161f13 | 485 | up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
486 | if (error) |
487 | return error; | |
af48b85b | 488 | |
e1123268 | 489 | size = le16_to_cpu(entry->e_value_size); |
af48b85b | 490 | |
e1123268 | 491 | if (buffer && size > buffer_size) { |
af48b85b | 492 | error = -ERANGE; |
ba38c27e | 493 | goto out; |
af48b85b JK |
494 | } |
495 | ||
496 | if (buffer) { | |
497 | char *pval = entry->e_name + entry->e_name_len; | |
e1123268 | 498 | memcpy(buffer, pval, size); |
af48b85b | 499 | } |
e1123268 | 500 | error = size; |
ba38c27e | 501 | out: |
65985d93 | 502 | kzfree(base_addr); |
af48b85b JK |
503 | return error; |
504 | } | |
505 | ||
506 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
507 | { | |
2b0143b5 | 508 | struct inode *inode = d_inode(dentry); |
af48b85b | 509 | struct f2fs_xattr_entry *entry; |
af48b85b JK |
510 | void *base_addr; |
511 | int error = 0; | |
512 | size_t rest = buffer_size; | |
513 | ||
27161f13 | 514 | down_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 | 515 | error = read_all_xattrs(inode, NULL, &base_addr); |
27161f13 | 516 | up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
517 | if (error) |
518 | return error; | |
af48b85b JK |
519 | |
520 | list_for_each_xattr(entry, base_addr) { | |
521 | const struct xattr_handler *handler = | |
522 | f2fs_xattr_handler(entry->e_name_index); | |
764a5c6b AG |
523 | const char *prefix; |
524 | size_t prefix_len; | |
af48b85b JK |
525 | size_t size; |
526 | ||
764a5c6b | 527 | if (!handler || (handler->list && !handler->list(dentry))) |
af48b85b JK |
528 | continue; |
529 | ||
764a5c6b AG |
530 | prefix = handler->prefix ?: handler->name; |
531 | prefix_len = strlen(prefix); | |
532 | size = prefix_len + entry->e_name_len + 1; | |
533 | if (buffer) { | |
534 | if (size > rest) { | |
535 | error = -ERANGE; | |
536 | goto cleanup; | |
537 | } | |
538 | memcpy(buffer, prefix, prefix_len); | |
539 | buffer += prefix_len; | |
540 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
541 | buffer += entry->e_name_len; | |
542 | *buffer++ = 0; | |
af48b85b | 543 | } |
af48b85b JK |
544 | rest -= size; |
545 | } | |
546 | error = buffer_size - rest; | |
547 | cleanup: | |
65985d93 | 548 | kzfree(base_addr); |
af48b85b JK |
549 | return error; |
550 | } | |
551 | ||
5f35a2cd KM |
552 | static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, |
553 | const void *value, size_t size) | |
554 | { | |
555 | void *pval = entry->e_name + entry->e_name_len; | |
b71deadb JK |
556 | |
557 | return (le16_to_cpu(entry->e_value_size) == size) && | |
558 | !memcmp(pval, value, size); | |
5f35a2cd KM |
559 | } |
560 | ||
e1123268 JK |
561 | static int __f2fs_setxattr(struct inode *inode, int index, |
562 | const char *name, const void *value, size_t size, | |
c02745ef | 563 | struct page *ipage, int flags) |
af48b85b | 564 | { |
af48b85b | 565 | struct f2fs_xattr_entry *here, *last; |
af48b85b | 566 | void *base_addr; |
65985d93 | 567 | int found, newsize; |
e1123268 | 568 | size_t len; |
65985d93 | 569 | __u32 new_hsize; |
a0995af6 | 570 | int error = 0; |
af48b85b JK |
571 | |
572 | if (name == NULL) | |
573 | return -EINVAL; | |
af48b85b JK |
574 | |
575 | if (value == NULL) | |
e1123268 | 576 | size = 0; |
af48b85b | 577 | |
e1123268 | 578 | len = strlen(name); |
7c909772 | 579 | |
037fe70c | 580 | if (len > F2FS_NAME_LEN) |
af48b85b JK |
581 | return -ERANGE; |
582 | ||
037fe70c CY |
583 | if (size > MAX_VALUE_LEN(inode)) |
584 | return -E2BIG; | |
585 | ||
86696966 CY |
586 | error = read_all_xattrs(inode, ipage, &base_addr); |
587 | if (error) | |
588 | return error; | |
af48b85b JK |
589 | |
590 | /* find entry with wanted name. */ | |
e1123268 | 591 | here = __find_xattr(base_addr, index, len, name); |
af48b85b | 592 | |
dd9cfe23 | 593 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
af48b85b | 594 | |
5f35a2cd KM |
595 | if (found) { |
596 | if ((flags & XATTR_CREATE)) { | |
597 | error = -EEXIST; | |
598 | goto exit; | |
599 | } | |
600 | ||
601 | if (f2fs_xattr_value_same(here, value, size)) | |
602 | goto exit; | |
603 | } else if ((flags & XATTR_REPLACE)) { | |
916decbf JK |
604 | error = -ENODATA; |
605 | goto exit; | |
916decbf JK |
606 | } |
607 | ||
608 | last = here; | |
af48b85b JK |
609 | while (!IS_XATTR_LAST_ENTRY(last)) |
610 | last = XATTR_NEXT_ENTRY(last); | |
611 | ||
e1123268 | 612 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
af48b85b JK |
613 | |
614 | /* 1. Check space */ | |
615 | if (value) { | |
65985d93 JK |
616 | int free; |
617 | /* | |
618 | * If value is NULL, it is remove operation. | |
e1c42045 | 619 | * In case of update operation, we calculate free. |
af48b85b | 620 | */ |
65985d93 | 621 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
af48b85b | 622 | if (found) |
cc3de6a3 | 623 | free = free + ENTRY_SIZE(here); |
af48b85b | 624 | |
6bacf52f | 625 | if (unlikely(free < newsize)) { |
58457f1c | 626 | error = -E2BIG; |
65985d93 | 627 | goto exit; |
af48b85b JK |
628 | } |
629 | } | |
630 | ||
631 | /* 2. Remove old entry */ | |
632 | if (found) { | |
65985d93 JK |
633 | /* |
634 | * If entry is found, remove old entry. | |
af48b85b JK |
635 | * If not found, remove operation is not needed. |
636 | */ | |
637 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); | |
638 | int oldsize = ENTRY_SIZE(here); | |
639 | ||
640 | memmove(here, next, (char *)last - (char *)next); | |
641 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); | |
642 | memset(last, 0, oldsize); | |
643 | } | |
644 | ||
65985d93 JK |
645 | new_hsize = (char *)last - (char *)base_addr; |
646 | ||
af48b85b JK |
647 | /* 3. Write new entry */ |
648 | if (value) { | |
65985d93 JK |
649 | char *pval; |
650 | /* | |
651 | * Before we come here, old entry is removed. | |
652 | * We just write new entry. | |
653 | */ | |
e1123268 JK |
654 | last->e_name_index = index; |
655 | last->e_name_len = len; | |
656 | memcpy(last->e_name, name, len); | |
657 | pval = last->e_name + len; | |
658 | memcpy(pval, value, size); | |
659 | last->e_value_size = cpu_to_le16(size); | |
65985d93 | 660 | new_hsize += newsize; |
af48b85b JK |
661 | } |
662 | ||
65985d93 JK |
663 | error = write_all_xattrs(inode, new_hsize, base_addr, ipage); |
664 | if (error) | |
665 | goto exit; | |
af48b85b | 666 | |
91942321 JK |
667 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { |
668 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
c2050a45 | 669 | inode->i_ctime = current_time(inode); |
91942321 | 670 | clear_inode_flag(inode, FI_ACL_MODE); |
af48b85b | 671 | } |
f424f664 JK |
672 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
673 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) | |
674 | f2fs_set_encrypted_inode(inode); | |
7c45729a | 675 | f2fs_mark_inode_dirty_sync(inode, true); |
bbf156f7 JK |
676 | if (!error && S_ISDIR(inode->i_mode)) |
677 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); | |
7c909772 | 678 | exit: |
65985d93 | 679 | kzfree(base_addr); |
af48b85b JK |
680 | return error; |
681 | } | |
52ab9560 | 682 | |
e1123268 JK |
683 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
684 | const void *value, size_t size, | |
c02745ef | 685 | struct page *ipage, int flags) |
52ab9560 | 686 | { |
4081363f | 687 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
52ab9560 RK |
688 | int err; |
689 | ||
d8d1389e JK |
690 | err = dquot_initialize(inode); |
691 | if (err) | |
692 | return err; | |
693 | ||
d631abda JK |
694 | /* this case is only from init_inode_metadata */ |
695 | if (ipage) | |
696 | return __f2fs_setxattr(inode, index, name, value, | |
697 | size, ipage, flags); | |
2c4db1a6 | 698 | f2fs_balance_fs(sbi, true); |
52ab9560 | 699 | |
e479556b | 700 | f2fs_lock_op(sbi); |
d928bfbf JK |
701 | /* protect xattr_ver */ |
702 | down_write(&F2FS_I(inode)->i_sem); | |
27161f13 | 703 | down_write(&F2FS_I(inode)->i_xattr_sem); |
c02745ef | 704 | err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); |
27161f13 | 705 | up_write(&F2FS_I(inode)->i_xattr_sem); |
d928bfbf | 706 | up_write(&F2FS_I(inode)->i_sem); |
e479556b | 707 | f2fs_unlock_op(sbi); |
52ab9560 | 708 | |
d0239e1b | 709 | f2fs_update_time(sbi, REQ_TIME); |
52ab9560 RK |
710 | return err; |
711 | } |