Commit | Line | Data |
---|---|---|
7c1a000d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0a8165d7 | 2 | /* |
af48b85b JK |
3 | * fs/f2fs/xattr.c |
4 | * | |
5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com/ | |
7 | * | |
8 | * Portions of this code from linux/fs/ext2/xattr.c | |
9 | * | |
10 | * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de> | |
11 | * | |
12 | * Fix by Harrison Xing <harrison@mountainviewdata.com>. | |
13 | * Extended attributes for symlinks and special files added per | |
14 | * suggestion of Luka Renko <luka.renko@hermes.si>. | |
15 | * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, | |
16 | * Red Hat Inc. | |
af48b85b JK |
17 | */ |
18 | #include <linux/rwsem.h> | |
19 | #include <linux/f2fs_fs.h> | |
8ae8f162 | 20 | #include <linux/security.h> |
a6dda0e6 | 21 | #include <linux/posix_acl_xattr.h> |
af48b85b JK |
22 | #include "f2fs.h" |
23 | #include "xattr.h" | |
955ebcd3 | 24 | #include "segment.h" |
af48b85b | 25 | |
a999150f CY |
26 | static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline) |
27 | { | |
28 | if (likely(size == sbi->inline_xattr_slab_size)) { | |
29 | *is_inline = true; | |
32410577 CY |
30 | return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab, |
31 | GFP_F2FS_ZERO, false, sbi); | |
a999150f CY |
32 | } |
33 | *is_inline = false; | |
34 | return f2fs_kzalloc(sbi, size, GFP_NOFS); | |
35 | } | |
36 | ||
37 | static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr, | |
38 | bool is_inline) | |
39 | { | |
40 | if (is_inline) | |
41 | kmem_cache_free(sbi->inline_xattr_slab, xattr_addr); | |
42 | else | |
c8eb7024 | 43 | kfree(xattr_addr); |
a999150f CY |
44 | } |
45 | ||
d9a82a04 | 46 | static int f2fs_xattr_generic_get(const struct xattr_handler *handler, |
b296821a AV |
47 | struct dentry *unused, struct inode *inode, |
48 | const char *name, void *buffer, size_t size) | |
af48b85b | 49 | { |
b296821a | 50 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 51 | |
d9a82a04 | 52 | switch (handler->flags) { |
af48b85b JK |
53 | case F2FS_XATTR_INDEX_USER: |
54 | if (!test_opt(sbi, XATTR_USER)) | |
55 | return -EOPNOTSUPP; | |
56 | break; | |
57 | case F2FS_XATTR_INDEX_TRUSTED: | |
8ae8f162 JK |
58 | case F2FS_XATTR_INDEX_SECURITY: |
59 | break; | |
af48b85b JK |
60 | default: |
61 | return -EINVAL; | |
62 | } | |
b296821a | 63 | return f2fs_getxattr(inode, handler->flags, name, |
d9a82a04 | 64 | buffer, size, NULL); |
af48b85b JK |
65 | } |
66 | ||
d9a82a04 | 67 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
39f60c1c | 68 | struct mnt_idmap *idmap, |
59301226 AV |
69 | struct dentry *unused, struct inode *inode, |
70 | const char *name, const void *value, | |
d9a82a04 | 71 | size_t size, int flags) |
af48b85b | 72 | { |
59301226 | 73 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 74 | |
d9a82a04 | 75 | switch (handler->flags) { |
af48b85b JK |
76 | case F2FS_XATTR_INDEX_USER: |
77 | if (!test_opt(sbi, XATTR_USER)) | |
78 | return -EOPNOTSUPP; | |
79 | break; | |
80 | case F2FS_XATTR_INDEX_TRUSTED: | |
8ae8f162 JK |
81 | case F2FS_XATTR_INDEX_SECURITY: |
82 | break; | |
af48b85b JK |
83 | default: |
84 | return -EINVAL; | |
85 | } | |
59301226 | 86 | return f2fs_setxattr(inode, handler->flags, name, |
c02745ef | 87 | value, size, NULL, flags); |
af48b85b JK |
88 | } |
89 | ||
764a5c6b | 90 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
573ea5fc | 91 | { |
764a5c6b AG |
92 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
93 | ||
94 | return test_opt(sbi, XATTR_USER); | |
95 | } | |
573ea5fc | 96 | |
764a5c6b AG |
97 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
98 | { | |
99 | return capable(CAP_SYS_ADMIN); | |
573ea5fc JK |
100 | } |
101 | ||
d9a82a04 | 102 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
b296821a AV |
103 | struct dentry *unused, struct inode *inode, |
104 | const char *name, void *buffer, size_t size) | |
573ea5fc | 105 | { |
84e97c27 CY |
106 | if (buffer) |
107 | *((char *)buffer) = F2FS_I(inode)->i_advise; | |
573ea5fc JK |
108 | return sizeof(char); |
109 | } | |
110 | ||
d9a82a04 | 111 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
39f60c1c | 112 | struct mnt_idmap *idmap, |
59301226 AV |
113 | struct dentry *unused, struct inode *inode, |
114 | const char *name, const void *value, | |
d9a82a04 | 115 | size_t size, int flags) |
573ea5fc | 116 | { |
797c1cb5 CY |
117 | unsigned char old_advise = F2FS_I(inode)->i_advise; |
118 | unsigned char new_advise; | |
119 | ||
01beba79 | 120 | if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) |
573ea5fc JK |
121 | return -EPERM; |
122 | if (value == NULL) | |
123 | return -EINVAL; | |
124 | ||
797c1cb5 CY |
125 | new_advise = *(char *)value; |
126 | if (new_advise & ~FADVISE_MODIFIABLE_BITS) | |
127 | return -EINVAL; | |
128 | ||
129 | new_advise = new_advise & FADVISE_MODIFIABLE_BITS; | |
130 | new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS; | |
131 | ||
132 | F2FS_I(inode)->i_advise = new_advise; | |
7c45729a | 133 | f2fs_mark_inode_dirty_sync(inode, true); |
573ea5fc JK |
134 | return 0; |
135 | } | |
136 | ||
8ae8f162 JK |
137 | #ifdef CONFIG_F2FS_FS_SECURITY |
138 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, | |
953ab314 | 139 | void *folio) |
8ae8f162 JK |
140 | { |
141 | const struct xattr *xattr; | |
142 | int err = 0; | |
143 | ||
144 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
d631abda | 145 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
8ae8f162 | 146 | xattr->name, xattr->value, |
953ab314 | 147 | xattr->value_len, folio, 0); |
8ae8f162 JK |
148 | if (err < 0) |
149 | break; | |
150 | } | |
151 | return err; | |
152 | } | |
153 | ||
154 | int f2fs_init_security(struct inode *inode, struct inode *dir, | |
953ab314 | 155 | const struct qstr *qstr, struct folio *ifolio) |
8ae8f162 JK |
156 | { |
157 | return security_inode_init_security(inode, dir, qstr, | |
953ab314 | 158 | f2fs_initxattrs, ifolio); |
8ae8f162 JK |
159 | } |
160 | #endif | |
161 | ||
af48b85b JK |
162 | const struct xattr_handler f2fs_xattr_user_handler = { |
163 | .prefix = XATTR_USER_PREFIX, | |
164 | .flags = F2FS_XATTR_INDEX_USER, | |
764a5c6b | 165 | .list = f2fs_xattr_user_list, |
af48b85b JK |
166 | .get = f2fs_xattr_generic_get, |
167 | .set = f2fs_xattr_generic_set, | |
168 | }; | |
169 | ||
170 | const struct xattr_handler f2fs_xattr_trusted_handler = { | |
171 | .prefix = XATTR_TRUSTED_PREFIX, | |
172 | .flags = F2FS_XATTR_INDEX_TRUSTED, | |
764a5c6b | 173 | .list = f2fs_xattr_trusted_list, |
af48b85b JK |
174 | .get = f2fs_xattr_generic_get, |
175 | .set = f2fs_xattr_generic_set, | |
176 | }; | |
177 | ||
573ea5fc | 178 | const struct xattr_handler f2fs_xattr_advise_handler = { |
98e9cb57 | 179 | .name = F2FS_SYSTEM_ADVISE_NAME, |
573ea5fc | 180 | .flags = F2FS_XATTR_INDEX_ADVISE, |
a87aff1d JQ |
181 | .get = f2fs_xattr_advise_get, |
182 | .set = f2fs_xattr_advise_set, | |
573ea5fc JK |
183 | }; |
184 | ||
8ae8f162 JK |
185 | const struct xattr_handler f2fs_xattr_security_handler = { |
186 | .prefix = XATTR_SECURITY_PREFIX, | |
187 | .flags = F2FS_XATTR_INDEX_SECURITY, | |
8ae8f162 JK |
188 | .get = f2fs_xattr_generic_get, |
189 | .set = f2fs_xattr_generic_set, | |
190 | }; | |
191 | ||
a1c0752c | 192 | static const struct xattr_handler * const f2fs_xattr_handler_map[] = { |
af48b85b JK |
193 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, |
194 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
d549b741 CB |
195 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access, |
196 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default, | |
af48b85b JK |
197 | #endif |
198 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, | |
8ae8f162 JK |
199 | #ifdef CONFIG_F2FS_FS_SECURITY |
200 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, | |
201 | #endif | |
af48b85b JK |
202 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
203 | }; | |
204 | ||
a1c0752c | 205 | const struct xattr_handler * const f2fs_xattr_handlers[] = { |
af48b85b | 206 | &f2fs_xattr_user_handler, |
af48b85b | 207 | &f2fs_xattr_trusted_handler, |
8ae8f162 JK |
208 | #ifdef CONFIG_F2FS_FS_SECURITY |
209 | &f2fs_xattr_security_handler, | |
210 | #endif | |
af48b85b JK |
211 | &f2fs_xattr_advise_handler, |
212 | NULL, | |
213 | }; | |
214 | ||
a5488f29 CB |
215 | static inline const char *f2fs_xattr_prefix(int index, |
216 | struct dentry *dentry) | |
af48b85b JK |
217 | { |
218 | const struct xattr_handler *handler = NULL; | |
219 | ||
e1123268 JK |
220 | if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map)) |
221 | handler = f2fs_xattr_handler_map[index]; | |
a5488f29 CB |
222 | |
223 | if (!xattr_handler_can_list(handler, dentry)) | |
224 | return NULL; | |
225 | ||
226 | return xattr_prefix(handler); | |
af48b85b JK |
227 | } |
228 | ||
2777e654 | 229 | static struct f2fs_xattr_entry *__find_xattr(void *base_addr, |
dd9d4a3a CY |
230 | void *last_base_addr, void **last_addr, |
231 | int index, size_t len, const char *name) | |
dd9cfe23 JK |
232 | { |
233 | struct f2fs_xattr_entry *entry; | |
234 | ||
235 | list_for_each_xattr(entry, base_addr) { | |
2777e654 | 236 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
dd9d4a3a CY |
237 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { |
238 | if (last_addr) | |
239 | *last_addr = entry; | |
2777e654 | 240 | return NULL; |
dd9d4a3a | 241 | } |
2777e654 | 242 | |
e1123268 | 243 | if (entry->e_name_index != index) |
dd9cfe23 | 244 | continue; |
e1123268 | 245 | if (entry->e_name_len != len) |
dd9cfe23 | 246 | continue; |
e1123268 | 247 | if (!memcmp(entry->e_name, name, len)) |
dd9cfe23 JK |
248 | break; |
249 | } | |
250 | return entry; | |
251 | } | |
252 | ||
6afc662e CY |
253 | static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode, |
254 | void *base_addr, void **last_addr, int index, | |
255 | size_t len, const char *name) | |
ba38c27e CY |
256 | { |
257 | struct f2fs_xattr_entry *entry; | |
6afc662e | 258 | unsigned int inline_size = inline_xattr_size(inode); |
2c28aba8 | 259 | void *max_addr = base_addr + inline_size; |
ba38c27e | 260 | |
dd9d4a3a CY |
261 | entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name); |
262 | if (!entry) | |
263 | return NULL; | |
2c28aba8 CY |
264 | |
265 | /* inline xattr header or entry across max inline xattr size */ | |
266 | if (IS_XATTR_LAST_ENTRY(entry) && | |
267 | (void *)entry + sizeof(__u32) > max_addr) { | |
268 | *last_addr = entry; | |
269 | return NULL; | |
270 | } | |
ba38c27e CY |
271 | return entry; |
272 | } | |
273 | ||
c8b19874 | 274 | static int read_inline_xattr(struct inode *inode, struct folio *ifolio, |
a5f433f7 CY |
275 | void *txattr_addr) |
276 | { | |
277 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
278 | unsigned int inline_size = inline_xattr_size(inode); | |
1aa46701 | 279 | struct folio *folio = NULL; |
a5f433f7 CY |
280 | void *inline_addr; |
281 | ||
c8b19874 | 282 | if (ifolio) { |
e0691a05 | 283 | inline_addr = inline_xattr_addr(inode, ifolio); |
a5f433f7 | 284 | } else { |
1aa46701 MWO |
285 | folio = f2fs_get_inode_folio(sbi, inode->i_ino); |
286 | if (IS_ERR(folio)) | |
287 | return PTR_ERR(folio); | |
a5f433f7 | 288 | |
e0691a05 | 289 | inline_addr = inline_xattr_addr(inode, folio); |
a5f433f7 CY |
290 | } |
291 | memcpy(txattr_addr, inline_addr, inline_size); | |
1aa46701 | 292 | f2fs_folio_put(folio, true); |
a5f433f7 CY |
293 | |
294 | return 0; | |
295 | } | |
296 | ||
63840695 CY |
297 | static int read_xattr_block(struct inode *inode, void *txattr_addr) |
298 | { | |
299 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
300 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
301 | unsigned int inline_size = inline_xattr_size(inode); | |
c972c546 | 302 | struct folio *xfolio; |
63840695 CY |
303 | void *xattr_addr; |
304 | ||
305 | /* The inode already has an extended attribute block. */ | |
c972c546 MWO |
306 | xfolio = f2fs_get_xnode_folio(sbi, xnid); |
307 | if (IS_ERR(xfolio)) | |
308 | return PTR_ERR(xfolio); | |
63840695 | 309 | |
c972c546 | 310 | xattr_addr = folio_address(xfolio); |
63840695 | 311 | memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); |
c972c546 | 312 | f2fs_folio_put(xfolio, true); |
63840695 CY |
313 | |
314 | return 0; | |
315 | } | |
316 | ||
39d20727 | 317 | static int lookup_all_xattrs(struct inode *inode, struct folio *ifolio, |
ba38c27e CY |
318 | unsigned int index, unsigned int len, |
319 | const char *name, struct f2fs_xattr_entry **xe, | |
a999150f CY |
320 | void **base_addr, int *base_size, |
321 | bool *is_inline) | |
ba38c27e | 322 | { |
2777e654 RH |
323 | void *cur_addr, *txattr_addr, *last_txattr_addr; |
324 | void *last_addr = NULL; | |
ba38c27e | 325 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
89e9eabd | 326 | unsigned int inline_size = inline_xattr_size(inode); |
2e0cd472 | 327 | int err; |
ba38c27e | 328 | |
2777e654 | 329 | if (!xnid && !inline_size) |
ba38c27e CY |
330 | return -ENODATA; |
331 | ||
ba3b583c | 332 | *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE; |
a999150f | 333 | txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline); |
ba38c27e CY |
334 | if (!txattr_addr) |
335 | return -ENOMEM; | |
336 | ||
ba3b583c | 337 | last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode); |
2777e654 | 338 | |
ba38c27e CY |
339 | /* read from inline xattr */ |
340 | if (inline_size) { | |
c8b19874 | 341 | err = read_inline_xattr(inode, ifolio, txattr_addr); |
a5f433f7 CY |
342 | if (err) |
343 | goto out; | |
ba38c27e | 344 | |
6afc662e | 345 | *xe = __find_inline_xattr(inode, txattr_addr, &last_addr, |
ba38c27e | 346 | index, len, name); |
64beba05 JK |
347 | if (*xe) { |
348 | *base_size = inline_size; | |
ba38c27e | 349 | goto check; |
64beba05 | 350 | } |
ba38c27e CY |
351 | } |
352 | ||
353 | /* read from xattr node block */ | |
354 | if (xnid) { | |
63840695 CY |
355 | err = read_xattr_block(inode, txattr_addr); |
356 | if (err) | |
ba38c27e | 357 | goto out; |
ba38c27e CY |
358 | } |
359 | ||
360 | if (last_addr) | |
361 | cur_addr = XATTR_HDR(last_addr) - 1; | |
362 | else | |
363 | cur_addr = txattr_addr; | |
364 | ||
dd9d4a3a | 365 | *xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name); |
2777e654 | 366 | if (!*xe) { |
50a472bb | 367 | f2fs_err(F2FS_I_SB(inode), "lookup inode (%lu) has corrupted xattr", |
c83414ae CY |
368 | inode->i_ino); |
369 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
50a472bb | 370 | err = -ENODATA; |
95fa90c9 CY |
371 | f2fs_handle_error(F2FS_I_SB(inode), |
372 | ERROR_CORRUPTED_XATTR); | |
2777e654 RH |
373 | goto out; |
374 | } | |
ba38c27e CY |
375 | check: |
376 | if (IS_XATTR_LAST_ENTRY(*xe)) { | |
377 | err = -ENODATA; | |
378 | goto out; | |
379 | } | |
380 | ||
381 | *base_addr = txattr_addr; | |
382 | return 0; | |
383 | out: | |
a999150f | 384 | xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline); |
ba38c27e CY |
385 | return err; |
386 | } | |
387 | ||
b3955efb | 388 | static int read_all_xattrs(struct inode *inode, struct folio *ifolio, |
86696966 | 389 | void **base_addr) |
65985d93 | 390 | { |
65985d93 | 391 | struct f2fs_xattr_header *header; |
89e9eabd CY |
392 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
393 | unsigned int size = VALID_XATTR_BLOCK_SIZE; | |
394 | unsigned int inline_size = inline_xattr_size(inode); | |
65985d93 | 395 | void *txattr_addr; |
86696966 | 396 | int err; |
65985d93 | 397 | |
acbf054d CY |
398 | txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), |
399 | inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS); | |
65985d93 | 400 | if (!txattr_addr) |
86696966 | 401 | return -ENOMEM; |
65985d93 JK |
402 | |
403 | /* read from inline xattr */ | |
404 | if (inline_size) { | |
c8b19874 | 405 | err = read_inline_xattr(inode, ifolio, txattr_addr); |
a5f433f7 CY |
406 | if (err) |
407 | goto fail; | |
65985d93 JK |
408 | } |
409 | ||
410 | /* read from xattr node block */ | |
89e9eabd | 411 | if (xnid) { |
63840695 CY |
412 | err = read_xattr_block(inode, txattr_addr); |
413 | if (err) | |
65985d93 | 414 | goto fail; |
65985d93 JK |
415 | } |
416 | ||
417 | header = XATTR_HDR(txattr_addr); | |
418 | ||
419 | /* never been allocated xattrs */ | |
420 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { | |
421 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); | |
422 | header->h_refcount = cpu_to_le32(1); | |
423 | } | |
86696966 CY |
424 | *base_addr = txattr_addr; |
425 | return 0; | |
65985d93 | 426 | fail: |
c8eb7024 | 427 | kfree(txattr_addr); |
86696966 | 428 | return err; |
65985d93 JK |
429 | } |
430 | ||
431 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, | |
170c445a | 432 | void *txattr_addr, struct folio *ifolio) |
65985d93 | 433 | { |
4081363f | 434 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
89e9eabd | 435 | size_t inline_size = inline_xattr_size(inode); |
170c445a | 436 | struct folio *in_folio = NULL; |
65985d93 | 437 | void *xattr_addr; |
bf9c1427 | 438 | void *inline_addr = NULL; |
b3094519 | 439 | struct folio *xfolio; |
65985d93 | 440 | nid_t new_nid = 0; |
bf9c1427 | 441 | int err = 0; |
65985d93 | 442 | |
65985d93 | 443 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) |
4d57b86d | 444 | if (!f2fs_alloc_nid(sbi, &new_nid)) |
65985d93 JK |
445 | return -ENOSPC; |
446 | ||
447 | /* write to inline xattr */ | |
448 | if (inline_size) { | |
170c445a | 449 | if (ifolio) { |
e0691a05 | 450 | inline_addr = inline_xattr_addr(inode, ifolio); |
65985d93 | 451 | } else { |
170c445a MWO |
452 | in_folio = f2fs_get_inode_folio(sbi, inode->i_ino); |
453 | if (IS_ERR(in_folio)) { | |
4d57b86d | 454 | f2fs_alloc_nid_failed(sbi, new_nid); |
170c445a | 455 | return PTR_ERR(in_folio); |
65985d93 | 456 | } |
e0691a05 | 457 | inline_addr = inline_xattr_addr(inode, in_folio); |
65985d93 | 458 | } |
65985d93 | 459 | |
170c445a | 460 | f2fs_folio_wait_writeback(ifolio ? ifolio : in_folio, |
bae0ee7a | 461 | NODE, true, true); |
65985d93 JK |
462 | /* no need to use xattr node block */ |
463 | if (hsize <= inline_size) { | |
4d57b86d CY |
464 | err = f2fs_truncate_xattr_node(inode); |
465 | f2fs_alloc_nid_failed(sbi, new_nid); | |
bf9c1427 | 466 | if (err) { |
170c445a | 467 | f2fs_folio_put(in_folio, true); |
bf9c1427 JK |
468 | return err; |
469 | } | |
470 | memcpy(inline_addr, txattr_addr, inline_size); | |
170c445a | 471 | folio_mark_dirty(ifolio ? ifolio : in_folio); |
bf9c1427 | 472 | goto in_page_out; |
65985d93 JK |
473 | } |
474 | } | |
475 | ||
476 | /* write to xattr node block */ | |
477 | if (F2FS_I(inode)->i_xattr_nid) { | |
b3094519 MWO |
478 | xfolio = f2fs_get_xnode_folio(sbi, F2FS_I(inode)->i_xattr_nid); |
479 | if (IS_ERR(xfolio)) { | |
480 | err = PTR_ERR(xfolio); | |
4d57b86d | 481 | f2fs_alloc_nid_failed(sbi, new_nid); |
bf9c1427 | 482 | goto in_page_out; |
65985d93 | 483 | } |
9850cf4a | 484 | f2fs_bug_on(sbi, new_nid); |
b3094519 | 485 | f2fs_folio_wait_writeback(xfolio, NODE, true, true); |
65985d93 JK |
486 | } else { |
487 | struct dnode_of_data dn; | |
5f029c04 | 488 | |
65985d93 | 489 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); |
b3094519 MWO |
490 | xfolio = f2fs_new_node_folio(&dn, XATTR_NODE_OFFSET); |
491 | if (IS_ERR(xfolio)) { | |
492 | err = PTR_ERR(xfolio); | |
4d57b86d | 493 | f2fs_alloc_nid_failed(sbi, new_nid); |
bf9c1427 | 494 | goto in_page_out; |
65985d93 | 495 | } |
4d57b86d | 496 | f2fs_alloc_nid_done(sbi, new_nid); |
65985d93 | 497 | } |
b3094519 | 498 | xattr_addr = folio_address(xfolio); |
bf9c1427 JK |
499 | |
500 | if (inline_size) | |
501 | memcpy(inline_addr, txattr_addr, inline_size); | |
22588f87 | 502 | memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); |
bf9c1427 JK |
503 | |
504 | if (inline_size) | |
170c445a | 505 | folio_mark_dirty(ifolio ? ifolio : in_folio); |
b3094519 | 506 | folio_mark_dirty(xfolio); |
65985d93 | 507 | |
b3094519 | 508 | f2fs_folio_put(xfolio, true); |
bf9c1427 | 509 | in_page_out: |
170c445a | 510 | f2fs_folio_put(in_folio, true); |
bf9c1427 | 511 | return err; |
65985d93 JK |
512 | } |
513 | ||
e1123268 | 514 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
39d20727 | 515 | void *buffer, size_t buffer_size, struct folio *ifolio) |
af48b85b | 516 | { |
ba38c27e | 517 | struct f2fs_xattr_entry *entry = NULL; |
2e0cd472 | 518 | int error; |
ba38c27e | 519 | unsigned int size, len; |
ba38c27e | 520 | void *base_addr = NULL; |
64beba05 | 521 | int base_size; |
a999150f | 522 | bool is_inline; |
af48b85b JK |
523 | |
524 | if (name == NULL) | |
525 | return -EINVAL; | |
e1123268 JK |
526 | |
527 | len = strlen(name); | |
528 | if (len > F2FS_NAME_LEN) | |
6e452d69 | 529 | return -ERANGE; |
af48b85b | 530 | |
39d20727 | 531 | if (!ifolio) |
5eda1ad1 | 532 | f2fs_down_read(&F2FS_I(inode)->i_xattr_sem); |
39d20727 | 533 | error = lookup_all_xattrs(inode, ifolio, index, len, name, |
a999150f | 534 | &entry, &base_addr, &base_size, &is_inline); |
39d20727 | 535 | if (!ifolio) |
5eda1ad1 | 536 | f2fs_up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
537 | if (error) |
538 | return error; | |
af48b85b | 539 | |
e1123268 | 540 | size = le16_to_cpu(entry->e_value_size); |
af48b85b | 541 | |
e1123268 | 542 | if (buffer && size > buffer_size) { |
af48b85b | 543 | error = -ERANGE; |
ba38c27e | 544 | goto out; |
af48b85b JK |
545 | } |
546 | ||
547 | if (buffer) { | |
548 | char *pval = entry->e_name + entry->e_name_len; | |
64beba05 JK |
549 | |
550 | if (base_size - (pval - (char *)base_addr) < size) { | |
551 | error = -ERANGE; | |
552 | goto out; | |
553 | } | |
e1123268 | 554 | memcpy(buffer, pval, size); |
af48b85b | 555 | } |
e1123268 | 556 | error = size; |
ba38c27e | 557 | out: |
a999150f | 558 | xattr_free(F2FS_I_SB(inode), base_addr, is_inline); |
af48b85b JK |
559 | return error; |
560 | } | |
561 | ||
562 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
563 | { | |
2b0143b5 | 564 | struct inode *inode = d_inode(dentry); |
af48b85b | 565 | struct f2fs_xattr_entry *entry; |
688078e7 | 566 | void *base_addr, *last_base_addr; |
2e0cd472 | 567 | int error; |
af48b85b JK |
568 | size_t rest = buffer_size; |
569 | ||
e4544b63 | 570 | f2fs_down_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 | 571 | error = read_all_xattrs(inode, NULL, &base_addr); |
e4544b63 | 572 | f2fs_up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
573 | if (error) |
574 | return error; | |
af48b85b | 575 | |
ba3b583c | 576 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
688078e7 | 577 | |
af48b85b | 578 | list_for_each_xattr(entry, base_addr) { |
764a5c6b AG |
579 | const char *prefix; |
580 | size_t prefix_len; | |
af48b85b JK |
581 | size_t size; |
582 | ||
a5488f29 CB |
583 | prefix = f2fs_xattr_prefix(entry->e_name_index, dentry); |
584 | ||
688078e7 RH |
585 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
586 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { | |
50a472bb | 587 | f2fs_err(F2FS_I_SB(inode), "list inode (%lu) has corrupted xattr", |
688078e7 RH |
588 | inode->i_ino); |
589 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
95fa90c9 CY |
590 | f2fs_handle_error(F2FS_I_SB(inode), |
591 | ERROR_CORRUPTED_XATTR); | |
50a472bb | 592 | break; |
688078e7 RH |
593 | } |
594 | ||
a5488f29 | 595 | if (!prefix) |
af48b85b JK |
596 | continue; |
597 | ||
764a5c6b AG |
598 | prefix_len = strlen(prefix); |
599 | size = prefix_len + entry->e_name_len + 1; | |
600 | if (buffer) { | |
601 | if (size > rest) { | |
602 | error = -ERANGE; | |
603 | goto cleanup; | |
604 | } | |
605 | memcpy(buffer, prefix, prefix_len); | |
606 | buffer += prefix_len; | |
607 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
608 | buffer += entry->e_name_len; | |
609 | *buffer++ = 0; | |
af48b85b | 610 | } |
af48b85b JK |
611 | rest -= size; |
612 | } | |
613 | error = buffer_size - rest; | |
614 | cleanup: | |
c8eb7024 | 615 | kfree(base_addr); |
af48b85b JK |
616 | return error; |
617 | } | |
618 | ||
5f35a2cd KM |
619 | static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, |
620 | const void *value, size_t size) | |
621 | { | |
622 | void *pval = entry->e_name + entry->e_name_len; | |
b71deadb JK |
623 | |
624 | return (le16_to_cpu(entry->e_value_size) == size) && | |
625 | !memcmp(pval, value, size); | |
5f35a2cd KM |
626 | } |
627 | ||
e1123268 JK |
628 | static int __f2fs_setxattr(struct inode *inode, int index, |
629 | const char *name, const void *value, size_t size, | |
b3955efb | 630 | struct folio *ifolio, int flags) |
af48b85b | 631 | { |
aaf8c0b9 | 632 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
af48b85b | 633 | struct f2fs_xattr_entry *here, *last; |
2777e654 | 634 | void *base_addr, *last_base_addr; |
65985d93 | 635 | int found, newsize; |
e1123268 | 636 | size_t len; |
65985d93 | 637 | __u32 new_hsize; |
2e0cd472 | 638 | int error; |
af48b85b JK |
639 | |
640 | if (name == NULL) | |
641 | return -EINVAL; | |
af48b85b JK |
642 | |
643 | if (value == NULL) | |
e1123268 | 644 | size = 0; |
af48b85b | 645 | |
e1123268 | 646 | len = strlen(name); |
7c909772 | 647 | |
037fe70c | 648 | if (len > F2FS_NAME_LEN) |
af48b85b JK |
649 | return -ERANGE; |
650 | ||
037fe70c CY |
651 | if (size > MAX_VALUE_LEN(inode)) |
652 | return -E2BIG; | |
50a472bb | 653 | retry: |
b3955efb | 654 | error = read_all_xattrs(inode, ifolio, &base_addr); |
86696966 CY |
655 | if (error) |
656 | return error; | |
af48b85b | 657 | |
ba3b583c | 658 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
2777e654 | 659 | |
af48b85b | 660 | /* find entry with wanted name. */ |
dd9d4a3a | 661 | here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name); |
2777e654 | 662 | if (!here) { |
50a472bb | 663 | if (!F2FS_I(inode)->i_xattr_nid) { |
86d7d57a | 664 | error = f2fs_recover_xattr_data(inode, NULL); |
50a472bb | 665 | f2fs_notice(F2FS_I_SB(inode), |
86d7d57a ZN |
666 | "recover xattr in inode (%lu), error(%d)", |
667 | inode->i_ino, error); | |
668 | if (!error) { | |
669 | kfree(base_addr); | |
670 | goto retry; | |
671 | } | |
50a472bb JK |
672 | } |
673 | f2fs_err(F2FS_I_SB(inode), "set inode (%lu) has corrupted xattr", | |
c83414ae CY |
674 | inode->i_ino); |
675 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
10f966bb | 676 | error = -EFSCORRUPTED; |
95fa90c9 CY |
677 | f2fs_handle_error(F2FS_I_SB(inode), |
678 | ERROR_CORRUPTED_XATTR); | |
2777e654 RH |
679 | goto exit; |
680 | } | |
af48b85b | 681 | |
dd9cfe23 | 682 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
af48b85b | 683 | |
5f35a2cd KM |
684 | if (found) { |
685 | if ((flags & XATTR_CREATE)) { | |
686 | error = -EEXIST; | |
687 | goto exit; | |
688 | } | |
689 | ||
b2c4692b | 690 | if (value && f2fs_xattr_value_same(here, value, size)) |
17232e83 | 691 | goto same; |
5f35a2cd | 692 | } else if ((flags & XATTR_REPLACE)) { |
916decbf JK |
693 | error = -ENODATA; |
694 | goto exit; | |
916decbf JK |
695 | } |
696 | ||
697 | last = here; | |
645a3c40 CY |
698 | while (!IS_XATTR_LAST_ENTRY(last)) { |
699 | if ((void *)(last) + sizeof(__u32) > last_base_addr || | |
700 | (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) { | |
701 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu", | |
702 | inode->i_ino, ENTRY_SIZE(last)); | |
703 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
704 | error = -EFSCORRUPTED; | |
95fa90c9 CY |
705 | f2fs_handle_error(F2FS_I_SB(inode), |
706 | ERROR_CORRUPTED_XATTR); | |
645a3c40 CY |
707 | goto exit; |
708 | } | |
af48b85b | 709 | last = XATTR_NEXT_ENTRY(last); |
645a3c40 | 710 | } |
af48b85b | 711 | |
e1123268 | 712 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
af48b85b JK |
713 | |
714 | /* 1. Check space */ | |
715 | if (value) { | |
65985d93 JK |
716 | int free; |
717 | /* | |
718 | * If value is NULL, it is remove operation. | |
e1c42045 | 719 | * In case of update operation, we calculate free. |
af48b85b | 720 | */ |
65985d93 | 721 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
af48b85b | 722 | if (found) |
cc3de6a3 | 723 | free = free + ENTRY_SIZE(here); |
af48b85b | 724 | |
6bacf52f | 725 | if (unlikely(free < newsize)) { |
58457f1c | 726 | error = -E2BIG; |
65985d93 | 727 | goto exit; |
af48b85b JK |
728 | } |
729 | } | |
730 | ||
731 | /* 2. Remove old entry */ | |
732 | if (found) { | |
65985d93 JK |
733 | /* |
734 | * If entry is found, remove old entry. | |
af48b85b JK |
735 | * If not found, remove operation is not needed. |
736 | */ | |
737 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); | |
738 | int oldsize = ENTRY_SIZE(here); | |
739 | ||
740 | memmove(here, next, (char *)last - (char *)next); | |
741 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); | |
742 | memset(last, 0, oldsize); | |
743 | } | |
744 | ||
65985d93 JK |
745 | new_hsize = (char *)last - (char *)base_addr; |
746 | ||
af48b85b JK |
747 | /* 3. Write new entry */ |
748 | if (value) { | |
65985d93 JK |
749 | char *pval; |
750 | /* | |
751 | * Before we come here, old entry is removed. | |
752 | * We just write new entry. | |
753 | */ | |
e1123268 JK |
754 | last->e_name_index = index; |
755 | last->e_name_len = len; | |
756 | memcpy(last->e_name, name, len); | |
757 | pval = last->e_name + len; | |
758 | memcpy(pval, value, size); | |
759 | last->e_value_size = cpu_to_le16(size); | |
65985d93 | 760 | new_hsize += newsize; |
e26b6d39 EB |
761 | /* |
762 | * Explicitly add the null terminator. The unused xattr space | |
763 | * is supposed to always be zeroed, which would make this | |
764 | * unnecessary, but don't depend on that. | |
765 | */ | |
766 | *(u32 *)((u8 *)last + newsize) = 0; | |
af48b85b JK |
767 | } |
768 | ||
170c445a | 769 | error = write_all_xattrs(inode, new_hsize, base_addr, ifolio); |
65985d93 JK |
770 | if (error) |
771 | goto exit; | |
af48b85b | 772 | |
f424f664 JK |
773 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
774 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) | |
775 | f2fs_set_encrypted_inode(inode); | |
17232e83 | 776 | |
aaf8c0b9 CY |
777 | if (!S_ISDIR(inode->i_mode)) |
778 | goto same; | |
779 | /* | |
780 | * In restrict mode, fsync() always try to trigger checkpoint for all | |
781 | * metadata consistency, in other mode, it triggers checkpoint when | |
782 | * parent's xattr metadata was updated. | |
783 | */ | |
784 | if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) | |
785 | set_sbi_flag(sbi, SBI_NEED_CP); | |
786 | else | |
787 | f2fs_add_ino_entry(sbi, inode->i_ino, XATTR_DIR_INO); | |
17232e83 CY |
788 | same: |
789 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { | |
790 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
17232e83 CY |
791 | clear_inode_flag(inode, FI_ACL_MODE); |
792 | } | |
793 | ||
92901222 | 794 | inode_set_ctime_current(inode); |
8874ad7d | 795 | f2fs_mark_inode_dirty_sync(inode, true); |
7c909772 | 796 | exit: |
c8eb7024 | 797 | kfree(base_addr); |
af48b85b JK |
798 | return error; |
799 | } | |
52ab9560 | 800 | |
e1123268 JK |
801 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
802 | const void *value, size_t size, | |
953ab314 | 803 | struct folio *ifolio, int flags) |
52ab9560 | 804 | { |
4081363f | 805 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
52ab9560 RK |
806 | int err; |
807 | ||
a25c2cdc CY |
808 | if (unlikely(f2fs_cp_error(sbi))) |
809 | return -EIO; | |
00e09c0b CY |
810 | if (!f2fs_is_checkpoint_ready(sbi)) |
811 | return -ENOSPC; | |
955ebcd3 | 812 | |
10a26878 | 813 | err = f2fs_dquot_initialize(inode); |
d8d1389e JK |
814 | if (err) |
815 | return err; | |
816 | ||
4d57b86d | 817 | /* this case is only from f2fs_init_inode_metadata */ |
953ab314 | 818 | if (ifolio) |
d631abda | 819 | return __f2fs_setxattr(inode, index, name, value, |
b3955efb | 820 | size, ifolio, flags); |
2c4db1a6 | 821 | f2fs_balance_fs(sbi, true); |
52ab9560 | 822 | |
e479556b | 823 | f2fs_lock_op(sbi); |
e4544b63 | 824 | f2fs_down_write(&F2FS_I(inode)->i_xattr_sem); |
953ab314 | 825 | err = __f2fs_setxattr(inode, index, name, value, size, NULL, flags); |
e4544b63 | 826 | f2fs_up_write(&F2FS_I(inode)->i_xattr_sem); |
e479556b | 827 | f2fs_unlock_op(sbi); |
52ab9560 | 828 | |
d0239e1b | 829 | f2fs_update_time(sbi, REQ_TIME); |
52ab9560 RK |
830 | return err; |
831 | } | |
a999150f CY |
832 | |
833 | int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) | |
834 | { | |
835 | dev_t dev = sbi->sb->s_bdev->bd_dev; | |
836 | char slab_name[32]; | |
837 | ||
838 | sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev)); | |
839 | ||
840 | sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size * | |
841 | sizeof(__le32) + XATTR_PADDING_SIZE; | |
842 | ||
843 | sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name, | |
844 | sbi->inline_xattr_slab_size); | |
845 | if (!sbi->inline_xattr_slab) | |
846 | return -ENOMEM; | |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
851 | void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) | |
852 | { | |
853 | kmem_cache_destroy(sbi->inline_xattr_slab); | |
854 | } |