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