]>
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 <[email protected]> | |
11 | * | |
12 | * Fix by Harrison Xing <[email protected]>. | |
13 | * Extended attributes for symlinks and special files added per | |
14 | * suggestion of Luka Renko <[email protected]>. | |
15 | * xattr consolidation Copyright (c) 2004 James Morris <[email protected]>, | |
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; | |
30 | return kmem_cache_zalloc(sbi->inline_xattr_slab, GFP_NOFS); | |
31 | } | |
32 | *is_inline = false; | |
33 | return f2fs_kzalloc(sbi, size, GFP_NOFS); | |
34 | } | |
35 | ||
36 | static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr, | |
37 | bool is_inline) | |
38 | { | |
39 | if (is_inline) | |
40 | kmem_cache_free(sbi->inline_xattr_slab, xattr_addr); | |
41 | else | |
42 | kvfree(xattr_addr); | |
43 | } | |
44 | ||
d9a82a04 | 45 | static int f2fs_xattr_generic_get(const struct xattr_handler *handler, |
b296821a AV |
46 | struct dentry *unused, struct inode *inode, |
47 | const char *name, void *buffer, size_t size) | |
af48b85b | 48 | { |
b296821a | 49 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 50 | |
d9a82a04 | 51 | switch (handler->flags) { |
af48b85b JK |
52 | case F2FS_XATTR_INDEX_USER: |
53 | if (!test_opt(sbi, XATTR_USER)) | |
54 | return -EOPNOTSUPP; | |
55 | break; | |
56 | case F2FS_XATTR_INDEX_TRUSTED: | |
8ae8f162 JK |
57 | case F2FS_XATTR_INDEX_SECURITY: |
58 | break; | |
af48b85b JK |
59 | default: |
60 | return -EINVAL; | |
61 | } | |
b296821a | 62 | return f2fs_getxattr(inode, handler->flags, name, |
d9a82a04 | 63 | buffer, size, NULL); |
af48b85b JK |
64 | } |
65 | ||
d9a82a04 | 66 | static int f2fs_xattr_generic_set(const struct xattr_handler *handler, |
59301226 AV |
67 | struct dentry *unused, struct inode *inode, |
68 | const char *name, const void *value, | |
d9a82a04 | 69 | size_t size, int flags) |
af48b85b | 70 | { |
59301226 | 71 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
af48b85b | 72 | |
d9a82a04 | 73 | switch (handler->flags) { |
af48b85b JK |
74 | case F2FS_XATTR_INDEX_USER: |
75 | if (!test_opt(sbi, XATTR_USER)) | |
76 | return -EOPNOTSUPP; | |
77 | break; | |
78 | case F2FS_XATTR_INDEX_TRUSTED: | |
8ae8f162 JK |
79 | case F2FS_XATTR_INDEX_SECURITY: |
80 | break; | |
af48b85b JK |
81 | default: |
82 | return -EINVAL; | |
83 | } | |
59301226 | 84 | return f2fs_setxattr(inode, handler->flags, name, |
c02745ef | 85 | value, size, NULL, flags); |
af48b85b JK |
86 | } |
87 | ||
764a5c6b | 88 | static bool f2fs_xattr_user_list(struct dentry *dentry) |
573ea5fc | 89 | { |
764a5c6b AG |
90 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
91 | ||
92 | return test_opt(sbi, XATTR_USER); | |
93 | } | |
573ea5fc | 94 | |
764a5c6b AG |
95 | static bool f2fs_xattr_trusted_list(struct dentry *dentry) |
96 | { | |
97 | return capable(CAP_SYS_ADMIN); | |
573ea5fc JK |
98 | } |
99 | ||
d9a82a04 | 100 | static int f2fs_xattr_advise_get(const struct xattr_handler *handler, |
b296821a AV |
101 | struct dentry *unused, struct inode *inode, |
102 | const char *name, void *buffer, size_t size) | |
573ea5fc | 103 | { |
84e97c27 CY |
104 | if (buffer) |
105 | *((char *)buffer) = F2FS_I(inode)->i_advise; | |
573ea5fc JK |
106 | return sizeof(char); |
107 | } | |
108 | ||
d9a82a04 | 109 | static int f2fs_xattr_advise_set(const struct xattr_handler *handler, |
59301226 AV |
110 | struct dentry *unused, struct inode *inode, |
111 | const char *name, const void *value, | |
d9a82a04 | 112 | size_t size, int flags) |
573ea5fc | 113 | { |
797c1cb5 CY |
114 | unsigned char old_advise = F2FS_I(inode)->i_advise; |
115 | unsigned char new_advise; | |
116 | ||
573ea5fc JK |
117 | if (!inode_owner_or_capable(inode)) |
118 | return -EPERM; | |
119 | if (value == NULL) | |
120 | return -EINVAL; | |
121 | ||
797c1cb5 CY |
122 | new_advise = *(char *)value; |
123 | if (new_advise & ~FADVISE_MODIFIABLE_BITS) | |
124 | return -EINVAL; | |
125 | ||
126 | new_advise = new_advise & FADVISE_MODIFIABLE_BITS; | |
127 | new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS; | |
128 | ||
129 | F2FS_I(inode)->i_advise = new_advise; | |
7c45729a | 130 | f2fs_mark_inode_dirty_sync(inode, true); |
573ea5fc JK |
131 | return 0; |
132 | } | |
133 | ||
8ae8f162 JK |
134 | #ifdef CONFIG_F2FS_FS_SECURITY |
135 | static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array, | |
136 | void *page) | |
137 | { | |
138 | const struct xattr *xattr; | |
139 | int err = 0; | |
140 | ||
141 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
d631abda | 142 | err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY, |
8ae8f162 | 143 | xattr->name, xattr->value, |
c02745ef | 144 | xattr->value_len, (struct page *)page, 0); |
8ae8f162 JK |
145 | if (err < 0) |
146 | break; | |
147 | } | |
148 | return err; | |
149 | } | |
150 | ||
151 | int f2fs_init_security(struct inode *inode, struct inode *dir, | |
152 | const struct qstr *qstr, struct page *ipage) | |
153 | { | |
154 | return security_inode_init_security(inode, dir, qstr, | |
155 | &f2fs_initxattrs, ipage); | |
156 | } | |
157 | #endif | |
158 | ||
af48b85b JK |
159 | const struct xattr_handler f2fs_xattr_user_handler = { |
160 | .prefix = XATTR_USER_PREFIX, | |
161 | .flags = F2FS_XATTR_INDEX_USER, | |
764a5c6b | 162 | .list = f2fs_xattr_user_list, |
af48b85b JK |
163 | .get = f2fs_xattr_generic_get, |
164 | .set = f2fs_xattr_generic_set, | |
165 | }; | |
166 | ||
167 | const struct xattr_handler f2fs_xattr_trusted_handler = { | |
168 | .prefix = XATTR_TRUSTED_PREFIX, | |
169 | .flags = F2FS_XATTR_INDEX_TRUSTED, | |
764a5c6b | 170 | .list = f2fs_xattr_trusted_list, |
af48b85b JK |
171 | .get = f2fs_xattr_generic_get, |
172 | .set = f2fs_xattr_generic_set, | |
173 | }; | |
174 | ||
573ea5fc | 175 | const struct xattr_handler f2fs_xattr_advise_handler = { |
98e9cb57 | 176 | .name = F2FS_SYSTEM_ADVISE_NAME, |
573ea5fc | 177 | .flags = F2FS_XATTR_INDEX_ADVISE, |
a87aff1d JQ |
178 | .get = f2fs_xattr_advise_get, |
179 | .set = f2fs_xattr_advise_set, | |
573ea5fc JK |
180 | }; |
181 | ||
8ae8f162 JK |
182 | const struct xattr_handler f2fs_xattr_security_handler = { |
183 | .prefix = XATTR_SECURITY_PREFIX, | |
184 | .flags = F2FS_XATTR_INDEX_SECURITY, | |
8ae8f162 JK |
185 | .get = f2fs_xattr_generic_get, |
186 | .set = f2fs_xattr_generic_set, | |
187 | }; | |
188 | ||
af48b85b JK |
189 | static const struct xattr_handler *f2fs_xattr_handler_map[] = { |
190 | [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler, | |
191 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
a6dda0e6 CH |
192 | [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, |
193 | [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, | |
af48b85b JK |
194 | #endif |
195 | [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler, | |
8ae8f162 JK |
196 | #ifdef CONFIG_F2FS_FS_SECURITY |
197 | [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler, | |
198 | #endif | |
af48b85b JK |
199 | [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler, |
200 | }; | |
201 | ||
202 | const struct xattr_handler *f2fs_xattr_handlers[] = { | |
203 | &f2fs_xattr_user_handler, | |
204 | #ifdef CONFIG_F2FS_FS_POSIX_ACL | |
a6dda0e6 CH |
205 | &posix_acl_access_xattr_handler, |
206 | &posix_acl_default_xattr_handler, | |
af48b85b JK |
207 | #endif |
208 | &f2fs_xattr_trusted_handler, | |
8ae8f162 JK |
209 | #ifdef CONFIG_F2FS_FS_SECURITY |
210 | &f2fs_xattr_security_handler, | |
211 | #endif | |
af48b85b JK |
212 | &f2fs_xattr_advise_handler, |
213 | NULL, | |
214 | }; | |
215 | ||
e1123268 | 216 | static inline const struct xattr_handler *f2fs_xattr_handler(int index) |
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]; | |
af48b85b JK |
222 | return handler; |
223 | } | |
224 | ||
2777e654 RH |
225 | static struct f2fs_xattr_entry *__find_xattr(void *base_addr, |
226 | void *last_base_addr, int index, | |
227 | size_t len, const char *name) | |
dd9cfe23 JK |
228 | { |
229 | struct f2fs_xattr_entry *entry; | |
230 | ||
231 | list_for_each_xattr(entry, base_addr) { | |
2777e654 RH |
232 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
233 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) | |
234 | return NULL; | |
235 | ||
e1123268 | 236 | if (entry->e_name_index != index) |
dd9cfe23 | 237 | continue; |
e1123268 | 238 | if (entry->e_name_len != len) |
dd9cfe23 | 239 | continue; |
e1123268 | 240 | if (!memcmp(entry->e_name, name, len)) |
dd9cfe23 JK |
241 | break; |
242 | } | |
243 | return entry; | |
244 | } | |
245 | ||
6afc662e CY |
246 | static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode, |
247 | void *base_addr, void **last_addr, int index, | |
248 | size_t len, const char *name) | |
ba38c27e CY |
249 | { |
250 | struct f2fs_xattr_entry *entry; | |
6afc662e | 251 | unsigned int inline_size = inline_xattr_size(inode); |
2c28aba8 | 252 | void *max_addr = base_addr + inline_size; |
ba38c27e CY |
253 | |
254 | list_for_each_xattr(entry, base_addr) { | |
2c28aba8 CY |
255 | if ((void *)entry + sizeof(__u32) > max_addr || |
256 | (void *)XATTR_NEXT_ENTRY(entry) > max_addr) { | |
ba38c27e CY |
257 | *last_addr = entry; |
258 | return NULL; | |
259 | } | |
260 | if (entry->e_name_index != index) | |
261 | continue; | |
262 | if (entry->e_name_len != len) | |
263 | continue; | |
264 | if (!memcmp(entry->e_name, name, len)) | |
265 | break; | |
266 | } | |
2c28aba8 CY |
267 | |
268 | /* inline xattr header or entry across max inline xattr size */ | |
269 | if (IS_XATTR_LAST_ENTRY(entry) && | |
270 | (void *)entry + sizeof(__u32) > max_addr) { | |
271 | *last_addr = entry; | |
272 | return NULL; | |
273 | } | |
ba38c27e CY |
274 | return entry; |
275 | } | |
276 | ||
a5f433f7 CY |
277 | static int read_inline_xattr(struct inode *inode, struct page *ipage, |
278 | void *txattr_addr) | |
279 | { | |
280 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
281 | unsigned int inline_size = inline_xattr_size(inode); | |
282 | struct page *page = NULL; | |
283 | void *inline_addr; | |
284 | ||
285 | if (ipage) { | |
6afc662e | 286 | inline_addr = inline_xattr_addr(inode, ipage); |
a5f433f7 | 287 | } else { |
4d57b86d | 288 | page = f2fs_get_node_page(sbi, inode->i_ino); |
a5f433f7 CY |
289 | if (IS_ERR(page)) |
290 | return PTR_ERR(page); | |
291 | ||
6afc662e | 292 | inline_addr = inline_xattr_addr(inode, page); |
a5f433f7 CY |
293 | } |
294 | memcpy(txattr_addr, inline_addr, inline_size); | |
295 | f2fs_put_page(page, 1); | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
63840695 CY |
300 | static int read_xattr_block(struct inode *inode, void *txattr_addr) |
301 | { | |
302 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
303 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
304 | unsigned int inline_size = inline_xattr_size(inode); | |
305 | struct page *xpage; | |
306 | void *xattr_addr; | |
307 | ||
308 | /* The inode already has an extended attribute block. */ | |
4d57b86d | 309 | xpage = f2fs_get_node_page(sbi, xnid); |
63840695 CY |
310 | if (IS_ERR(xpage)) |
311 | return PTR_ERR(xpage); | |
312 | ||
313 | xattr_addr = page_address(xpage); | |
314 | memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); | |
315 | f2fs_put_page(xpage, 1); | |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
ba38c27e CY |
320 | static int lookup_all_xattrs(struct inode *inode, struct page *ipage, |
321 | unsigned int index, unsigned int len, | |
322 | const char *name, struct f2fs_xattr_entry **xe, | |
a999150f CY |
323 | void **base_addr, int *base_size, |
324 | bool *is_inline) | |
ba38c27e | 325 | { |
2777e654 RH |
326 | void *cur_addr, *txattr_addr, *last_txattr_addr; |
327 | void *last_addr = NULL; | |
ba38c27e | 328 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
89e9eabd | 329 | unsigned int inline_size = inline_xattr_size(inode); |
ba38c27e CY |
330 | int err = 0; |
331 | ||
2777e654 | 332 | if (!xnid && !inline_size) |
ba38c27e CY |
333 | return -ENODATA; |
334 | ||
ba3b583c | 335 | *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE; |
a999150f | 336 | txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline); |
ba38c27e CY |
337 | if (!txattr_addr) |
338 | return -ENOMEM; | |
339 | ||
ba3b583c | 340 | last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode); |
2777e654 | 341 | |
ba38c27e CY |
342 | /* read from inline xattr */ |
343 | if (inline_size) { | |
a5f433f7 CY |
344 | err = read_inline_xattr(inode, ipage, txattr_addr); |
345 | if (err) | |
346 | goto out; | |
ba38c27e | 347 | |
6afc662e | 348 | *xe = __find_inline_xattr(inode, txattr_addr, &last_addr, |
ba38c27e | 349 | index, len, name); |
64beba05 JK |
350 | if (*xe) { |
351 | *base_size = inline_size; | |
ba38c27e | 352 | goto check; |
64beba05 | 353 | } |
ba38c27e CY |
354 | } |
355 | ||
356 | /* read from xattr node block */ | |
357 | if (xnid) { | |
63840695 CY |
358 | err = read_xattr_block(inode, txattr_addr); |
359 | if (err) | |
ba38c27e | 360 | goto out; |
ba38c27e CY |
361 | } |
362 | ||
363 | if (last_addr) | |
364 | cur_addr = XATTR_HDR(last_addr) - 1; | |
365 | else | |
366 | cur_addr = txattr_addr; | |
367 | ||
2777e654 RH |
368 | *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name); |
369 | if (!*xe) { | |
c83414ae CY |
370 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", |
371 | inode->i_ino); | |
372 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
10f966bb | 373 | err = -EFSCORRUPTED; |
2777e654 RH |
374 | goto out; |
375 | } | |
ba38c27e CY |
376 | check: |
377 | if (IS_XATTR_LAST_ENTRY(*xe)) { | |
378 | err = -ENODATA; | |
379 | goto out; | |
380 | } | |
381 | ||
382 | *base_addr = txattr_addr; | |
383 | return 0; | |
384 | out: | |
a999150f | 385 | xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline); |
ba38c27e CY |
386 | return err; |
387 | } | |
388 | ||
86696966 CY |
389 | static int read_all_xattrs(struct inode *inode, struct page *ipage, |
390 | void **base_addr) | |
65985d93 | 391 | { |
65985d93 | 392 | struct f2fs_xattr_header *header; |
89e9eabd CY |
393 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
394 | unsigned int size = VALID_XATTR_BLOCK_SIZE; | |
395 | unsigned int inline_size = inline_xattr_size(inode); | |
65985d93 | 396 | void *txattr_addr; |
86696966 | 397 | int err; |
65985d93 | 398 | |
acbf054d CY |
399 | txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), |
400 | inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS); | |
65985d93 | 401 | if (!txattr_addr) |
86696966 | 402 | return -ENOMEM; |
65985d93 JK |
403 | |
404 | /* read from inline xattr */ | |
405 | if (inline_size) { | |
a5f433f7 CY |
406 | err = read_inline_xattr(inode, ipage, txattr_addr); |
407 | if (err) | |
408 | goto fail; | |
65985d93 JK |
409 | } |
410 | ||
411 | /* read from xattr node block */ | |
89e9eabd | 412 | if (xnid) { |
63840695 CY |
413 | err = read_xattr_block(inode, txattr_addr); |
414 | if (err) | |
65985d93 | 415 | goto fail; |
65985d93 JK |
416 | } |
417 | ||
418 | header = XATTR_HDR(txattr_addr); | |
419 | ||
420 | /* never been allocated xattrs */ | |
421 | if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { | |
422 | header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); | |
423 | header->h_refcount = cpu_to_le32(1); | |
424 | } | |
86696966 CY |
425 | *base_addr = txattr_addr; |
426 | return 0; | |
65985d93 | 427 | fail: |
2a6a7e72 | 428 | kvfree(txattr_addr); |
86696966 | 429 | return err; |
65985d93 JK |
430 | } |
431 | ||
432 | static inline int write_all_xattrs(struct inode *inode, __u32 hsize, | |
433 | void *txattr_addr, struct page *ipage) | |
434 | { | |
4081363f | 435 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
89e9eabd | 436 | size_t inline_size = inline_xattr_size(inode); |
bf9c1427 | 437 | struct page *in_page = NULL; |
65985d93 | 438 | void *xattr_addr; |
bf9c1427 | 439 | void *inline_addr = NULL; |
65985d93 JK |
440 | struct page *xpage; |
441 | nid_t new_nid = 0; | |
bf9c1427 | 442 | int err = 0; |
65985d93 | 443 | |
65985d93 | 444 | if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) |
4d57b86d | 445 | if (!f2fs_alloc_nid(sbi, &new_nid)) |
65985d93 JK |
446 | return -ENOSPC; |
447 | ||
448 | /* write to inline xattr */ | |
449 | if (inline_size) { | |
65985d93 | 450 | if (ipage) { |
6afc662e | 451 | inline_addr = inline_xattr_addr(inode, ipage); |
65985d93 | 452 | } else { |
4d57b86d | 453 | in_page = f2fs_get_node_page(sbi, inode->i_ino); |
bf9c1427 | 454 | if (IS_ERR(in_page)) { |
4d57b86d | 455 | f2fs_alloc_nid_failed(sbi, new_nid); |
bf9c1427 | 456 | return PTR_ERR(in_page); |
65985d93 | 457 | } |
bf9c1427 | 458 | inline_addr = inline_xattr_addr(inode, in_page); |
65985d93 | 459 | } |
65985d93 | 460 | |
bf9c1427 | 461 | f2fs_wait_on_page_writeback(ipage ? ipage : in_page, |
bae0ee7a | 462 | NODE, true, true); |
65985d93 JK |
463 | /* no need to use xattr node block */ |
464 | if (hsize <= inline_size) { | |
4d57b86d CY |
465 | err = f2fs_truncate_xattr_node(inode); |
466 | f2fs_alloc_nid_failed(sbi, new_nid); | |
bf9c1427 JK |
467 | if (err) { |
468 | f2fs_put_page(in_page, 1); | |
469 | return err; | |
470 | } | |
471 | memcpy(inline_addr, txattr_addr, inline_size); | |
472 | set_page_dirty(ipage ? ipage : in_page); | |
473 | goto in_page_out; | |
65985d93 JK |
474 | } |
475 | } | |
476 | ||
477 | /* write to xattr node block */ | |
478 | if (F2FS_I(inode)->i_xattr_nid) { | |
4d57b86d | 479 | xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); |
65985d93 | 480 | if (IS_ERR(xpage)) { |
d620439f | 481 | err = PTR_ERR(xpage); |
4d57b86d | 482 | f2fs_alloc_nid_failed(sbi, new_nid); |
bf9c1427 | 483 | goto in_page_out; |
65985d93 | 484 | } |
9850cf4a | 485 | f2fs_bug_on(sbi, new_nid); |
bae0ee7a | 486 | f2fs_wait_on_page_writeback(xpage, NODE, true, true); |
65985d93 JK |
487 | } else { |
488 | struct dnode_of_data dn; | |
489 | set_new_dnode(&dn, inode, NULL, NULL, new_nid); | |
4d57b86d | 490 | xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); |
65985d93 | 491 | if (IS_ERR(xpage)) { |
d620439f | 492 | err = PTR_ERR(xpage); |
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 | } |
65985d93 | 498 | xattr_addr = page_address(xpage); |
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) | |
505 | set_page_dirty(ipage ? ipage : in_page); | |
65985d93 | 506 | set_page_dirty(xpage); |
65985d93 | 507 | |
bf9c1427 JK |
508 | f2fs_put_page(xpage, 1); |
509 | in_page_out: | |
510 | f2fs_put_page(in_page, 1); | |
511 | return err; | |
65985d93 JK |
512 | } |
513 | ||
e1123268 | 514 | int f2fs_getxattr(struct inode *inode, int index, const char *name, |
bce8d112 | 515 | void *buffer, size_t buffer_size, struct page *ipage) |
af48b85b | 516 | { |
ba38c27e | 517 | struct f2fs_xattr_entry *entry = NULL; |
dd9cfe23 | 518 | int error = 0; |
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 | |
27161f13 | 531 | down_read(&F2FS_I(inode)->i_xattr_sem); |
ba38c27e | 532 | error = lookup_all_xattrs(inode, ipage, index, len, name, |
a999150f | 533 | &entry, &base_addr, &base_size, &is_inline); |
27161f13 | 534 | up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
535 | if (error) |
536 | return error; | |
af48b85b | 537 | |
e1123268 | 538 | size = le16_to_cpu(entry->e_value_size); |
af48b85b | 539 | |
e1123268 | 540 | if (buffer && size > buffer_size) { |
af48b85b | 541 | error = -ERANGE; |
ba38c27e | 542 | goto out; |
af48b85b JK |
543 | } |
544 | ||
545 | if (buffer) { | |
546 | char *pval = entry->e_name + entry->e_name_len; | |
64beba05 JK |
547 | |
548 | if (base_size - (pval - (char *)base_addr) < size) { | |
549 | error = -ERANGE; | |
550 | goto out; | |
551 | } | |
e1123268 | 552 | memcpy(buffer, pval, size); |
af48b85b | 553 | } |
e1123268 | 554 | error = size; |
ba38c27e | 555 | out: |
a999150f | 556 | xattr_free(F2FS_I_SB(inode), base_addr, is_inline); |
af48b85b JK |
557 | return error; |
558 | } | |
559 | ||
560 | ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) | |
561 | { | |
2b0143b5 | 562 | struct inode *inode = d_inode(dentry); |
af48b85b | 563 | struct f2fs_xattr_entry *entry; |
688078e7 | 564 | void *base_addr, *last_base_addr; |
af48b85b JK |
565 | int error = 0; |
566 | size_t rest = buffer_size; | |
567 | ||
27161f13 | 568 | down_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 | 569 | error = read_all_xattrs(inode, NULL, &base_addr); |
27161f13 | 570 | up_read(&F2FS_I(inode)->i_xattr_sem); |
86696966 CY |
571 | if (error) |
572 | return error; | |
af48b85b | 573 | |
ba3b583c | 574 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
688078e7 | 575 | |
af48b85b JK |
576 | list_for_each_xattr(entry, base_addr) { |
577 | const struct xattr_handler *handler = | |
578 | f2fs_xattr_handler(entry->e_name_index); | |
764a5c6b AG |
579 | const char *prefix; |
580 | size_t prefix_len; | |
af48b85b JK |
581 | size_t size; |
582 | ||
688078e7 RH |
583 | if ((void *)(entry) + sizeof(__u32) > last_base_addr || |
584 | (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { | |
585 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", | |
586 | inode->i_ino); | |
587 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
588 | error = -EFSCORRUPTED; | |
589 | goto cleanup; | |
590 | } | |
591 | ||
764a5c6b | 592 | if (!handler || (handler->list && !handler->list(dentry))) |
af48b85b JK |
593 | continue; |
594 | ||
eecfa427 | 595 | prefix = xattr_prefix(handler); |
764a5c6b AG |
596 | prefix_len = strlen(prefix); |
597 | size = prefix_len + entry->e_name_len + 1; | |
598 | if (buffer) { | |
599 | if (size > rest) { | |
600 | error = -ERANGE; | |
601 | goto cleanup; | |
602 | } | |
603 | memcpy(buffer, prefix, prefix_len); | |
604 | buffer += prefix_len; | |
605 | memcpy(buffer, entry->e_name, entry->e_name_len); | |
606 | buffer += entry->e_name_len; | |
607 | *buffer++ = 0; | |
af48b85b | 608 | } |
af48b85b JK |
609 | rest -= size; |
610 | } | |
611 | error = buffer_size - rest; | |
612 | cleanup: | |
2a6a7e72 | 613 | kvfree(base_addr); |
af48b85b JK |
614 | return error; |
615 | } | |
616 | ||
5f35a2cd KM |
617 | static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, |
618 | const void *value, size_t size) | |
619 | { | |
620 | void *pval = entry->e_name + entry->e_name_len; | |
b71deadb JK |
621 | |
622 | return (le16_to_cpu(entry->e_value_size) == size) && | |
623 | !memcmp(pval, value, size); | |
5f35a2cd KM |
624 | } |
625 | ||
e1123268 JK |
626 | static int __f2fs_setxattr(struct inode *inode, int index, |
627 | const char *name, const void *value, size_t size, | |
c02745ef | 628 | struct page *ipage, int flags) |
af48b85b | 629 | { |
af48b85b | 630 | struct f2fs_xattr_entry *here, *last; |
2777e654 | 631 | void *base_addr, *last_base_addr; |
65985d93 | 632 | int found, newsize; |
e1123268 | 633 | size_t len; |
65985d93 | 634 | __u32 new_hsize; |
a0995af6 | 635 | int error = 0; |
af48b85b JK |
636 | |
637 | if (name == NULL) | |
638 | return -EINVAL; | |
af48b85b JK |
639 | |
640 | if (value == NULL) | |
e1123268 | 641 | size = 0; |
af48b85b | 642 | |
e1123268 | 643 | len = strlen(name); |
7c909772 | 644 | |
037fe70c | 645 | if (len > F2FS_NAME_LEN) |
af48b85b JK |
646 | return -ERANGE; |
647 | ||
037fe70c CY |
648 | if (size > MAX_VALUE_LEN(inode)) |
649 | return -E2BIG; | |
650 | ||
86696966 CY |
651 | error = read_all_xattrs(inode, ipage, &base_addr); |
652 | if (error) | |
653 | return error; | |
af48b85b | 654 | |
ba3b583c | 655 | last_base_addr = (void *)base_addr + XATTR_SIZE(inode); |
2777e654 | 656 | |
af48b85b | 657 | /* find entry with wanted name. */ |
2777e654 RH |
658 | here = __find_xattr(base_addr, last_base_addr, index, len, name); |
659 | if (!here) { | |
c83414ae CY |
660 | f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", |
661 | inode->i_ino); | |
662 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); | |
10f966bb | 663 | error = -EFSCORRUPTED; |
2777e654 RH |
664 | goto exit; |
665 | } | |
af48b85b | 666 | |
dd9cfe23 | 667 | found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; |
af48b85b | 668 | |
5f35a2cd KM |
669 | if (found) { |
670 | if ((flags & XATTR_CREATE)) { | |
671 | error = -EEXIST; | |
672 | goto exit; | |
673 | } | |
674 | ||
b2c4692b | 675 | if (value && f2fs_xattr_value_same(here, value, size)) |
5f35a2cd KM |
676 | goto exit; |
677 | } else if ((flags & XATTR_REPLACE)) { | |
916decbf JK |
678 | error = -ENODATA; |
679 | goto exit; | |
916decbf JK |
680 | } |
681 | ||
682 | last = here; | |
af48b85b JK |
683 | while (!IS_XATTR_LAST_ENTRY(last)) |
684 | last = XATTR_NEXT_ENTRY(last); | |
685 | ||
e1123268 | 686 | newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); |
af48b85b JK |
687 | |
688 | /* 1. Check space */ | |
689 | if (value) { | |
65985d93 JK |
690 | int free; |
691 | /* | |
692 | * If value is NULL, it is remove operation. | |
e1c42045 | 693 | * In case of update operation, we calculate free. |
af48b85b | 694 | */ |
65985d93 | 695 | free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); |
af48b85b | 696 | if (found) |
cc3de6a3 | 697 | free = free + ENTRY_SIZE(here); |
af48b85b | 698 | |
6bacf52f | 699 | if (unlikely(free < newsize)) { |
58457f1c | 700 | error = -E2BIG; |
65985d93 | 701 | goto exit; |
af48b85b JK |
702 | } |
703 | } | |
704 | ||
705 | /* 2. Remove old entry */ | |
706 | if (found) { | |
65985d93 JK |
707 | /* |
708 | * If entry is found, remove old entry. | |
af48b85b JK |
709 | * If not found, remove operation is not needed. |
710 | */ | |
711 | struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); | |
712 | int oldsize = ENTRY_SIZE(here); | |
713 | ||
714 | memmove(here, next, (char *)last - (char *)next); | |
715 | last = (struct f2fs_xattr_entry *)((char *)last - oldsize); | |
716 | memset(last, 0, oldsize); | |
717 | } | |
718 | ||
65985d93 JK |
719 | new_hsize = (char *)last - (char *)base_addr; |
720 | ||
af48b85b JK |
721 | /* 3. Write new entry */ |
722 | if (value) { | |
65985d93 JK |
723 | char *pval; |
724 | /* | |
725 | * Before we come here, old entry is removed. | |
726 | * We just write new entry. | |
727 | */ | |
e1123268 JK |
728 | last->e_name_index = index; |
729 | last->e_name_len = len; | |
730 | memcpy(last->e_name, name, len); | |
731 | pval = last->e_name + len; | |
732 | memcpy(pval, value, size); | |
733 | last->e_value_size = cpu_to_le16(size); | |
65985d93 | 734 | new_hsize += newsize; |
af48b85b JK |
735 | } |
736 | ||
65985d93 JK |
737 | error = write_all_xattrs(inode, new_hsize, base_addr, ipage); |
738 | if (error) | |
739 | goto exit; | |
af48b85b | 740 | |
91942321 JK |
741 | if (is_inode_flag_set(inode, FI_ACL_MODE)) { |
742 | inode->i_mode = F2FS_I(inode)->i_acl_mode; | |
c2050a45 | 743 | inode->i_ctime = current_time(inode); |
91942321 | 744 | clear_inode_flag(inode, FI_ACL_MODE); |
af48b85b | 745 | } |
f424f664 JK |
746 | if (index == F2FS_XATTR_INDEX_ENCRYPTION && |
747 | !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) | |
748 | f2fs_set_encrypted_inode(inode); | |
7c45729a | 749 | f2fs_mark_inode_dirty_sync(inode, true); |
bbf156f7 JK |
750 | if (!error && S_ISDIR(inode->i_mode)) |
751 | set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); | |
7c909772 | 752 | exit: |
2a6a7e72 | 753 | kvfree(base_addr); |
af48b85b JK |
754 | return error; |
755 | } | |
52ab9560 | 756 | |
e1123268 JK |
757 | int f2fs_setxattr(struct inode *inode, int index, const char *name, |
758 | const void *value, size_t size, | |
c02745ef | 759 | struct page *ipage, int flags) |
52ab9560 | 760 | { |
4081363f | 761 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
52ab9560 RK |
762 | int err; |
763 | ||
a25c2cdc CY |
764 | if (unlikely(f2fs_cp_error(sbi))) |
765 | return -EIO; | |
00e09c0b CY |
766 | if (!f2fs_is_checkpoint_ready(sbi)) |
767 | return -ENOSPC; | |
955ebcd3 | 768 | |
d8d1389e JK |
769 | err = dquot_initialize(inode); |
770 | if (err) | |
771 | return err; | |
772 | ||
4d57b86d | 773 | /* this case is only from f2fs_init_inode_metadata */ |
d631abda JK |
774 | if (ipage) |
775 | return __f2fs_setxattr(inode, index, name, value, | |
776 | size, ipage, flags); | |
2c4db1a6 | 777 | f2fs_balance_fs(sbi, true); |
52ab9560 | 778 | |
e479556b | 779 | f2fs_lock_op(sbi); |
27161f13 | 780 | down_write(&F2FS_I(inode)->i_xattr_sem); |
c02745ef | 781 | err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); |
27161f13 | 782 | up_write(&F2FS_I(inode)->i_xattr_sem); |
e479556b | 783 | f2fs_unlock_op(sbi); |
52ab9560 | 784 | |
d0239e1b | 785 | f2fs_update_time(sbi, REQ_TIME); |
52ab9560 RK |
786 | return err; |
787 | } | |
a999150f CY |
788 | |
789 | int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) | |
790 | { | |
791 | dev_t dev = sbi->sb->s_bdev->bd_dev; | |
792 | char slab_name[32]; | |
793 | ||
794 | sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev)); | |
795 | ||
796 | sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size * | |
797 | sizeof(__le32) + XATTR_PADDING_SIZE; | |
798 | ||
799 | sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name, | |
800 | sbi->inline_xattr_slab_size); | |
801 | if (!sbi->inline_xattr_slab) | |
802 | return -ENOMEM; | |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
807 | void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) | |
808 | { | |
809 | kmem_cache_destroy(sbi->inline_xattr_slab); | |
810 | } |