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 <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> | |
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 AG |
52 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
53 | struct dentry *dentry, const char *name, const void *value, | |
54 | size_t size, int flags) | |
af48b85b JK |
55 | { |
56 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); | |
57 | ||
d9a82a04 | 58 | switch (handler->flags) { |
af48b85b JK |
59 | case F2FS_XATTR_INDEX_USER: |
60 | if (!test_opt(sbi, XATTR_USER)) | |
61 | return -EOPNOTSUPP; | |
62 | break; | |
63 | case F2FS_XATTR_INDEX_TRUSTED: | |
64 | if (!capable(CAP_SYS_ADMIN)) | |
65 | return -EPERM; | |
66 | break; | |
8ae8f162 JK |
67 | case F2FS_XATTR_INDEX_SECURITY: |
68 | break; | |
af48b85b JK |
69 | default: |
70 | return -EINVAL; | |
71 | } | |
d9a82a04 | 72 | return f2fs_setxattr(d_inode(dentry), handler->flags, name, |
c02745ef | 73 | value, size, NULL, flags); |
af48b85b JK |
74 | } |
75 | ||
764a5c6b | 76 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
573ea5fc | 77 | { |
764a5c6b AG |
78 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
79 | ||
80 | return test_opt(sbi, XATTR_USER); | |
81 | } | |
573ea5fc | 82 | |
764a5c6b AG |
83 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
84 | { | |
85 | return capable(CAP_SYS_ADMIN); | |
573ea5fc JK |
86 | } |
87 | ||
d9a82a04 | 88 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
b296821a AV |
89 | struct dentry *unused, struct inode *inode, |
90 | const char *name, void *buffer, size_t size) | |
573ea5fc | 91 | { |
84e97c27 CY |
92 | if (buffer) |
93 | *((char *)buffer) = F2FS_I(inode)->i_advise; | |
573ea5fc JK |
94 | return sizeof(char); |
95 | } | |
96 | ||
d9a82a04 AG |
97 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
98 | struct dentry *dentry, const char *name, const void *value, | |
99 | size_t size, int flags) | |
573ea5fc | 100 | { |
2b0143b5 | 101 | struct inode *inode = d_inode(dentry); |
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; | |
30c62fdb | 109 | mark_inode_dirty(inode); |
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 | ||
65985d93 JK |
220 | static void *read_all_xattrs(struct inode *inode, struct page *ipage) |
221 | { | |
4081363f | 222 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
65985d93 JK |
223 | struct f2fs_xattr_header *header; |
224 | size_t size = PAGE_SIZE, inline_size = 0; | |
225 | void *txattr_addr; | |
226 | ||
227 | inline_size = inline_xattr_size(inode); | |
228 | ||
808a1d74 | 229 | txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO); |
65985d93 JK |
230 | if (!txattr_addr) |
231 | return NULL; | |
232 | ||
233 | /* read from inline xattr */ | |
234 | if (inline_size) { | |
235 | struct page *page = NULL; | |
236 | void *inline_addr; | |
237 | ||
238 | if (ipage) { | |
239 | inline_addr = inline_xattr_addr(ipage); | |
240 | } else { | |
241 | page = get_node_page(sbi, inode->i_ino); | |
242 | if (IS_ERR(page)) | |
243 | goto fail; | |
244 | inline_addr = inline_xattr_addr(page); | |
245 | } | |
246 | memcpy(txattr_addr, inline_addr, inline_size); | |
247 | f2fs_put_page(page, 1); | |
248 | } | |
249 | ||
250 | /* read from xattr node block */ | |
251 | if (F2FS_I(inode)->i_xattr_nid) { | |
252 | struct page *xpage; | |
253 | void *xattr_addr; | |
254 | ||
255 | /* The inode already has an extended attribute block. */ | |
256 | xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); | |
257 | if (IS_ERR(xpage)) | |
258 | goto fail; | |
259 | ||
260 | xattr_addr = page_address(xpage); | |
261 | memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE); | |
262 | f2fs_put_page(xpage, 1); | |
263 | } | |
264 | ||
265 | header = XATTR_HDR(txattr_addr); | |
266 | ||
267 | /* never been allocated xattrs */ | |
268 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { | |
269 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); | |
270 | header->h_refcount = cpu_to_le32(1); | |
271 | } | |
272 | return txattr_addr; | |
273 | fail: | |
274 | kzfree(txattr_addr); | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, | |
279 | void *txattr_addr, struct page *ipage) | |
280 | { | |
4081363f | 281 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
65985d93 JK |
282 | size_t inline_size = 0; |
283 | void *xattr_addr; | |
284 | struct page *xpage; | |
285 | nid_t new_nid = 0; | |
286 | int err; | |
287 | ||
288 | inline_size = inline_xattr_size(inode); | |
289 | ||
290 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) | |
291 | if (!alloc_nid(sbi, &new_nid)) | |
292 | return -ENOSPC; | |
293 | ||
294 | /* write to inline xattr */ | |
295 | if (inline_size) { | |
296 | struct page *page = NULL; | |
297 | void *inline_addr; | |
298 | ||
299 | if (ipage) { | |
300 | inline_addr = inline_xattr_addr(ipage); | |
fec1d657 | 301 | f2fs_wait_on_page_writeback(ipage, NODE, true); |
65985d93 JK |
302 | } else { |
303 | page = get_node_page(sbi, inode->i_ino); | |
304 | if (IS_ERR(page)) { | |
305 | alloc_nid_failed(sbi, new_nid); | |
306 | return PTR_ERR(page); | |
307 | } | |
308 | inline_addr = inline_xattr_addr(page); | |
fec1d657 | 309 | f2fs_wait_on_page_writeback(page, NODE, true); |
65985d93 JK |
310 | } |
311 | memcpy(inline_addr, txattr_addr, inline_size); | |
312 | f2fs_put_page(page, 1); | |
313 | ||
314 | /* no need to use xattr node block */ | |
315 | if (hsize <= inline_size) { | |
316 | err = truncate_xattr_node(inode, ipage); | |
317 | alloc_nid_failed(sbi, new_nid); | |
318 | return err; | |
319 | } | |
320 | } | |
321 | ||
322 | /* write to xattr node block */ | |
323 | if (F2FS_I(inode)->i_xattr_nid) { | |
324 | xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); | |
325 | if (IS_ERR(xpage)) { | |
326 | alloc_nid_failed(sbi, new_nid); | |
327 | return PTR_ERR(xpage); | |
328 | } | |
9850cf4a | 329 | f2fs_bug_on(sbi, new_nid); |
fec1d657 | 330 | f2fs_wait_on_page_writeback(xpage, NODE, true); |
65985d93 JK |
331 | } else { |
332 | struct dnode_of_data dn; | |
333 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); | |
334 | xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage); | |
335 | if (IS_ERR(xpage)) { | |
336 | alloc_nid_failed(sbi, new_nid); | |
337 | return PTR_ERR(xpage); | |
338 | } | |
339 | alloc_nid_done(sbi, new_nid); | |
340 | } | |
341 | ||
342 | xattr_addr = page_address(xpage); | |
343 | memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE - | |
344 | sizeof(struct node_footer)); | |
345 | set_page_dirty(xpage); | |
346 | f2fs_put_page(xpage, 1); | |
347 | ||
348 | /* need to checkpoint during fsync */ | |
349 | F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi)); | |
350 | return 0; | |
351 | } | |
352 | ||
e1123268 | 353 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
bce8d112 | 354 | void *buffer, size_t buffer_size, struct page *ipage) |
af48b85b | 355 | { |
af48b85b | 356 | struct f2fs_xattr_entry *entry; |
65985d93 | 357 | void *base_addr; |
dd9cfe23 | 358 | int error = 0; |
e1123268 | 359 | size_t size, len; |
af48b85b JK |
360 | |
361 | if (name == NULL) | |
362 | return -EINVAL; | |
e1123268 JK |
363 | |
364 | len = strlen(name); | |
365 | if (len > F2FS_NAME_LEN) | |
6e452d69 | 366 | return -ERANGE; |
af48b85b | 367 | |
bce8d112 | 368 | base_addr = read_all_xattrs(inode, ipage); |
65985d93 JK |
369 | if (!base_addr) |
370 | return -ENOMEM; | |
af48b85b | 371 | |
e1123268 | 372 | entry = __find_xattr(base_addr, index, len, name); |
dd9cfe23 | 373 | if (IS_XATTR_LAST_ENTRY(entry)) { |
af48b85b JK |
374 | error = -ENODATA; |
375 | goto cleanup; | |
376 | } | |
377 | ||
e1123268 | 378 | size = le16_to_cpu(entry->e_value_size); |
af48b85b | 379 | |
e1123268 | 380 | if (buffer && size > buffer_size) { |
af48b85b JK |
381 | error = -ERANGE; |
382 | goto cleanup; | |
383 | } | |
384 | ||
385 | if (buffer) { | |
386 | char *pval = entry->e_name + entry->e_name_len; | |
e1123268 | 387 | memcpy(buffer, pval, size); |
af48b85b | 388 | } |
e1123268 | 389 | error = size; |
af48b85b JK |
390 | |
391 | cleanup: | |
65985d93 | 392 | kzfree(base_addr); |
af48b85b JK |
393 | return error; |
394 | } | |
395 | ||
396 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
397 | { | |
2b0143b5 | 398 | struct inode *inode = d_inode(dentry); |
af48b85b | 399 | struct f2fs_xattr_entry *entry; |
af48b85b JK |
400 | void *base_addr; |
401 | int error = 0; | |
402 | size_t rest = buffer_size; | |
403 | ||
65985d93 JK |
404 | base_addr = read_all_xattrs(inode, NULL); |
405 | if (!base_addr) | |
406 | return -ENOMEM; | |
af48b85b JK |
407 | |
408 | list_for_each_xattr(entry, base_addr) { | |
409 | const struct xattr_handler *handler = | |
410 | f2fs_xattr_handler(entry->e_name_index); | |
764a5c6b AG |
411 | const char *prefix; |
412 | size_t prefix_len; | |
af48b85b JK |
413 | size_t size; |
414 | ||
764a5c6b | 415 | if (!handler || (handler->list && !handler->list(dentry))) |
af48b85b JK |
416 | continue; |
417 | ||
764a5c6b AG |
418 | prefix = handler->prefix ?: handler->name; |
419 | prefix_len = strlen(prefix); | |
420 | size = prefix_len + entry->e_name_len + 1; | |
421 | if (buffer) { | |
422 | if (size > rest) { | |
423 | error = -ERANGE; | |
424 | goto cleanup; | |
425 | } | |
426 | memcpy(buffer, prefix, prefix_len); | |
427 | buffer += prefix_len; | |
428 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
429 | buffer += entry->e_name_len; | |
430 | *buffer++ = 0; | |
af48b85b | 431 | } |
af48b85b JK |
432 | rest -= size; |
433 | } | |
434 | error = buffer_size - rest; | |
435 | cleanup: | |
65985d93 | 436 | kzfree(base_addr); |
af48b85b JK |
437 | return error; |
438 | } | |
439 | ||
e1123268 JK |
440 | static int __f2fs_setxattr(struct inode *inode, int index, |
441 | const char *name, const void *value, size_t size, | |
c02745ef | 442 | struct page *ipage, int flags) |
af48b85b | 443 | { |
af48b85b | 444 | struct f2fs_inode_info *fi = F2FS_I(inode); |
af48b85b | 445 | struct f2fs_xattr_entry *here, *last; |
af48b85b | 446 | void *base_addr; |
65985d93 | 447 | int found, newsize; |
e1123268 | 448 | size_t len; |
65985d93 JK |
449 | __u32 new_hsize; |
450 | int error = -ENOMEM; | |
af48b85b JK |
451 | |
452 | if (name == NULL) | |
453 | return -EINVAL; | |
af48b85b JK |
454 | |
455 | if (value == NULL) | |
e1123268 | 456 | size = 0; |
af48b85b | 457 | |
e1123268 | 458 | len = strlen(name); |
7c909772 | 459 | |
037fe70c | 460 | if (len > F2FS_NAME_LEN) |
af48b85b JK |
461 | return -ERANGE; |
462 | ||
037fe70c CY |
463 | if (size > MAX_VALUE_LEN(inode)) |
464 | return -E2BIG; | |
465 | ||
65985d93 JK |
466 | base_addr = read_all_xattrs(inode, ipage); |
467 | if (!base_addr) | |
468 | goto exit; | |
af48b85b JK |
469 | |
470 | /* find entry with wanted name. */ | |
e1123268 | 471 | here = __find_xattr(base_addr, index, len, name); |
af48b85b | 472 | |
dd9cfe23 | 473 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
af48b85b | 474 | |
916decbf JK |
475 | if ((flags & XATTR_REPLACE) && !found) { |
476 | error = -ENODATA; | |
477 | goto exit; | |
478 | } else if ((flags & XATTR_CREATE) && found) { | |
479 | error = -EEXIST; | |
480 | goto exit; | |
481 | } | |
482 | ||
483 | last = here; | |
af48b85b JK |
484 | while (!IS_XATTR_LAST_ENTRY(last)) |
485 | last = XATTR_NEXT_ENTRY(last); | |
486 | ||
e1123268 | 487 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
af48b85b JK |
488 | |
489 | /* 1. Check space */ | |
490 | if (value) { | |
65985d93 JK |
491 | int free; |
492 | /* | |
493 | * If value is NULL, it is remove operation. | |
e1c42045 | 494 | * In case of update operation, we calculate free. |
af48b85b | 495 | */ |
65985d93 | 496 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
af48b85b | 497 | if (found) |
cc3de6a3 | 498 | free = free + ENTRY_SIZE(here); |
af48b85b | 499 | |
6bacf52f | 500 | if (unlikely(free < newsize)) { |
58457f1c | 501 | error = -E2BIG; |
65985d93 | 502 | goto exit; |
af48b85b JK |
503 | } |
504 | } | |
505 | ||
506 | /* 2. Remove old entry */ | |
507 | if (found) { | |
65985d93 JK |
508 | /* |
509 | * If entry is found, remove old entry. | |
af48b85b JK |
510 | * If not found, remove operation is not needed. |
511 | */ | |
512 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); | |
513 | int oldsize = ENTRY_SIZE(here); | |
514 | ||
515 | memmove(here, next, (char *)last - (char *)next); | |
516 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); | |
517 | memset(last, 0, oldsize); | |
518 | } | |
519 | ||
65985d93 JK |
520 | new_hsize = (char *)last - (char *)base_addr; |
521 | ||
af48b85b JK |
522 | /* 3. Write new entry */ |
523 | if (value) { | |
65985d93 JK |
524 | char *pval; |
525 | /* | |
526 | * Before we come here, old entry is removed. | |
527 | * We just write new entry. | |
528 | */ | |
e1123268 JK |
529 | last->e_name_index = index; |
530 | last->e_name_len = len; | |
531 | memcpy(last->e_name, name, len); | |
532 | pval = last->e_name + len; | |
533 | memcpy(pval, value, size); | |
534 | last->e_value_size = cpu_to_le16(size); | |
65985d93 | 535 | new_hsize += newsize; |
af48b85b JK |
536 | } |
537 | ||
65985d93 JK |
538 | error = write_all_xattrs(inode, new_hsize, base_addr, ipage); |
539 | if (error) | |
540 | goto exit; | |
af48b85b JK |
541 | |
542 | if (is_inode_flag_set(fi, FI_ACL_MODE)) { | |
543 | inode->i_mode = fi->i_acl_mode; | |
544 | inode->i_ctime = CURRENT_TIME; | |
545 | clear_inode_flag(fi, FI_ACL_MODE); | |
546 | } | |
f424f664 JK |
547 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
548 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) | |
549 | f2fs_set_encrypted_inode(inode); | |
e518ff81 | 550 | |
8ae8f162 JK |
551 | if (ipage) |
552 | update_inode(inode, ipage); | |
553 | else | |
554 | update_inode_page(inode); | |
7c909772 | 555 | exit: |
65985d93 | 556 | kzfree(base_addr); |
af48b85b JK |
557 | return error; |
558 | } | |
52ab9560 | 559 | |
e1123268 JK |
560 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
561 | const void *value, size_t size, | |
c02745ef | 562 | struct page *ipage, int flags) |
52ab9560 | 563 | { |
4081363f | 564 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
52ab9560 RK |
565 | int err; |
566 | ||
d631abda JK |
567 | /* this case is only from init_inode_metadata */ |
568 | if (ipage) | |
569 | return __f2fs_setxattr(inode, index, name, value, | |
570 | size, ipage, flags); | |
2c4db1a6 | 571 | f2fs_balance_fs(sbi, true); |
52ab9560 | 572 | |
e479556b | 573 | f2fs_lock_op(sbi); |
d928bfbf JK |
574 | /* protect xattr_ver */ |
575 | down_write(&F2FS_I(inode)->i_sem); | |
c02745ef | 576 | err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); |
d928bfbf | 577 | up_write(&F2FS_I(inode)->i_sem); |
e479556b | 578 | f2fs_unlock_op(sbi); |
52ab9560 | 579 | |
d0239e1b | 580 | f2fs_update_time(sbi, REQ_TIME); |
52ab9560 RK |
581 | return err; |
582 | } |