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