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