blob: 4f6582ef7ee331b7b62c3c0a1c77e4c732f0d1a7 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimaf48b852012-11-02 17:12:17 +09003 * 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.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090017 */
18#include <linux/rwsem.h>
19#include <linux/f2fs_fs.h>
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090020#include <linux/security.h>
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -080021#include <linux/posix_acl_xattr.h>
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090022#include "f2fs.h"
23#include "xattr.h"
Chao Yu955ebcd2019-07-22 17:57:05 +080024#include "segment.h"
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090025
Chao Yua9991502020-02-25 18:17:10 +080026static 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
36static 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
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020045static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040046 struct dentry *unused, struct inode *inode,
47 const char *name, void *buffer, size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090048{
Al Virob2968212016-04-10 20:48:24 -040049 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090050
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020051 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090052 case F2FS_XATTR_INDEX_USER:
53 if (!test_opt(sbi, XATTR_USER))
54 return -EOPNOTSUPP;
55 break;
56 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090057 case F2FS_XATTR_INDEX_SECURITY:
58 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090059 default:
60 return -EINVAL;
61 }
Al Virob2968212016-04-10 20:48:24 -040062 return f2fs_getxattr(inode, handler->flags, name,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020063 buffer, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090064}
65
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020066static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040067 struct dentry *unused, struct inode *inode,
68 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020069 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090070{
Al Viro59301222016-05-27 10:19:30 -040071 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090072
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020073 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090074 case F2FS_XATTR_INDEX_USER:
75 if (!test_opt(sbi, XATTR_USER))
76 return -EOPNOTSUPP;
77 break;
78 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090079 case F2FS_XATTR_INDEX_SECURITY:
80 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090081 default:
82 return -EINVAL;
83 }
Al Viro59301222016-05-27 10:19:30 -040084 return f2fs_setxattr(inode, handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +090085 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090086}
87
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010088static bool f2fs_xattr_user_list(struct dentry *dentry)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090089{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010090 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090091
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010092 return test_opt(sbi, XATTR_USER);
93}
94
95static bool f2fs_xattr_trusted_list(struct dentry *dentry)
96{
97 return capable(CAP_SYS_ADMIN);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090098}
99
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200100static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400101 struct dentry *unused, struct inode *inode,
102 const char *name, void *buffer, size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900103{
Chao Yu84e97c22015-03-23 10:36:15 +0800104 if (buffer)
105 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900106 return sizeof(char);
107}
108
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200109static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -0400110 struct dentry *unused, struct inode *inode,
111 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200112 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900113{
Chao Yu797c1cb2018-07-19 23:57:54 +0800114 unsigned char old_advise = F2FS_I(inode)->i_advise;
115 unsigned char new_advise;
116
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900117 if (!inode_owner_or_capable(inode))
118 return -EPERM;
119 if (value == NULL)
120 return -EINVAL;
121
Chao Yu797c1cb2018-07-19 23:57:54 +0800122 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;
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700130 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900131 return 0;
132}
133
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900134#ifdef CONFIG_F2FS_FS_SECURITY
135static 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++) {
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900142 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900143 xattr->name, xattr->value,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900144 xattr->value_len, (struct page *)page, 0);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900145 if (err < 0)
146 break;
147 }
148 return err;
149}
150
151int 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
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900159const struct xattr_handler f2fs_xattr_user_handler = {
160 .prefix = XATTR_USER_PREFIX,
161 .flags = F2FS_XATTR_INDEX_USER,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100162 .list = f2fs_xattr_user_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900163 .get = f2fs_xattr_generic_get,
164 .set = f2fs_xattr_generic_set,
165};
166
167const struct xattr_handler f2fs_xattr_trusted_handler = {
168 .prefix = XATTR_TRUSTED_PREFIX,
169 .flags = F2FS_XATTR_INDEX_TRUSTED,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100170 .list = f2fs_xattr_trusted_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900171 .get = f2fs_xattr_generic_get,
172 .set = f2fs_xattr_generic_set,
173};
174
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900175const struct xattr_handler f2fs_xattr_advise_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100176 .name = F2FS_SYSTEM_ADVISE_NAME,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900177 .flags = F2FS_XATTR_INDEX_ADVISE,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900178 .get = f2fs_xattr_advise_get,
179 .set = f2fs_xattr_advise_set,
180};
181
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900182const struct xattr_handler f2fs_xattr_security_handler = {
183 .prefix = XATTR_SECURITY_PREFIX,
184 .flags = F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900185 .get = f2fs_xattr_generic_get,
186 .set = f2fs_xattr_generic_set,
187};
188
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900189static const struct xattr_handler *f2fs_xattr_handler_map[] = {
190 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
191#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800192 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
193 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900194#endif
195 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900196#ifdef CONFIG_F2FS_FS_SECURITY
197 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
198#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900199 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
200};
201
202const struct xattr_handler *f2fs_xattr_handlers[] = {
203 &f2fs_xattr_user_handler,
204#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800205 &posix_acl_access_xattr_handler,
206 &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900207#endif
208 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900209#ifdef CONFIG_F2FS_FS_SECURITY
210 &f2fs_xattr_security_handler,
211#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900212 &f2fs_xattr_advise_handler,
213 NULL,
214};
215
Jaegeuk Kime1123262014-04-23 12:17:25 +0900216static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900217{
218 const struct xattr_handler *handler = NULL;
219
Jaegeuk Kime1123262014-04-23 12:17:25 +0900220 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
221 handler = f2fs_xattr_handler_map[index];
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900222 return handler;
223}
224
Randall Huang2777e652019-04-11 16:26:46 +0800225static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
226 void *last_base_addr, int index,
227 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900228{
229 struct f2fs_xattr_entry *entry;
230
231 list_for_each_xattr(entry, base_addr) {
Randall Huang2777e652019-04-11 16:26:46 +0800232 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
233 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
234 return NULL;
235
Jaegeuk Kime1123262014-04-23 12:17:25 +0900236 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900237 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900238 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900239 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900240 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900241 break;
242 }
243 return entry;
244}
245
Chao Yu6afc6622017-09-06 21:59:50 +0800246static 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)
Chao Yuba38c272017-01-24 20:39:51 +0800249{
250 struct f2fs_xattr_entry *entry;
Chao Yu6afc6622017-09-06 21:59:50 +0800251 unsigned int inline_size = inline_xattr_size(inode);
Chao Yu2c28aba2019-03-05 19:32:26 +0800252 void *max_addr = base_addr + inline_size;
Chao Yuba38c272017-01-24 20:39:51 +0800253
254 list_for_each_xattr(entry, base_addr) {
Chao Yu2c28aba2019-03-05 19:32:26 +0800255 if ((void *)entry + sizeof(__u32) > max_addr ||
256 (void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
Chao Yuba38c272017-01-24 20:39:51 +0800257 *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 }
Chao Yu2c28aba2019-03-05 19:32:26 +0800267
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 }
Chao Yuba38c272017-01-24 20:39:51 +0800274 return entry;
275}
276
Chao Yua5f433f2017-09-04 18:58:02 +0800277static 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) {
Chao Yu6afc6622017-09-06 21:59:50 +0800286 inline_addr = inline_xattr_addr(inode, ipage);
Chao Yua5f433f2017-09-04 18:58:02 +0800287 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800288 page = f2fs_get_node_page(sbi, inode->i_ino);
Chao Yua5f433f2017-09-04 18:58:02 +0800289 if (IS_ERR(page))
290 return PTR_ERR(page);
291
Chao Yu6afc6622017-09-06 21:59:50 +0800292 inline_addr = inline_xattr_addr(inode, page);
Chao Yua5f433f2017-09-04 18:58:02 +0800293 }
294 memcpy(txattr_addr, inline_addr, inline_size);
295 f2fs_put_page(page, 1);
296
297 return 0;
298}
299
Chao Yu63840692017-09-04 18:58:03 +0800300static 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. */
Chao Yu4d57b862018-05-30 00:20:41 +0800309 xpage = f2fs_get_node_page(sbi, xnid);
Chao Yu63840692017-09-04 18:58:03 +0800310 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
Chao Yuba38c272017-01-24 20:39:51 +0800320static 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,
Chao Yua9991502020-02-25 18:17:10 +0800323 void **base_addr, int *base_size,
324 bool *is_inline)
Chao Yuba38c272017-01-24 20:39:51 +0800325{
Randall Huang2777e652019-04-11 16:26:46 +0800326 void *cur_addr, *txattr_addr, *last_txattr_addr;
327 void *last_addr = NULL;
Chao Yuba38c272017-01-24 20:39:51 +0800328 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu89e9eab2017-03-23 13:38:25 +0800329 unsigned int inline_size = inline_xattr_size(inode);
Chao Yuba38c272017-01-24 20:39:51 +0800330 int err = 0;
331
Randall Huang2777e652019-04-11 16:26:46 +0800332 if (!xnid && !inline_size)
Chao Yuba38c272017-01-24 20:39:51 +0800333 return -ENODATA;
334
Chao Yuba3b5832020-02-14 17:44:11 +0800335 *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
Chao Yua9991502020-02-25 18:17:10 +0800336 txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
Chao Yuba38c272017-01-24 20:39:51 +0800337 if (!txattr_addr)
338 return -ENOMEM;
339
Chao Yuba3b5832020-02-14 17:44:11 +0800340 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
Randall Huang2777e652019-04-11 16:26:46 +0800341
Chao Yuba38c272017-01-24 20:39:51 +0800342 /* read from inline xattr */
343 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800344 err = read_inline_xattr(inode, ipage, txattr_addr);
345 if (err)
346 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800347
Chao Yu6afc6622017-09-06 21:59:50 +0800348 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
Chao Yuba38c272017-01-24 20:39:51 +0800349 index, len, name);
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800350 if (*xe) {
351 *base_size = inline_size;
Chao Yuba38c272017-01-24 20:39:51 +0800352 goto check;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800353 }
Chao Yuba38c272017-01-24 20:39:51 +0800354 }
355
356 /* read from xattr node block */
357 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800358 err = read_xattr_block(inode, txattr_addr);
359 if (err)
Chao Yuba38c272017-01-24 20:39:51 +0800360 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800361 }
362
363 if (last_addr)
364 cur_addr = XATTR_HDR(last_addr) - 1;
365 else
366 cur_addr = txattr_addr;
367
Randall Huang2777e652019-04-11 16:26:46 +0800368 *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
369 if (!*xe) {
Chao Yuc83414a2019-06-20 11:36:15 +0800370 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);
Chao Yu10f966b2019-06-20 11:36:14 +0800373 err = -EFSCORRUPTED;
Randall Huang2777e652019-04-11 16:26:46 +0800374 goto out;
375 }
Chao Yuba38c272017-01-24 20:39:51 +0800376check:
377 if (IS_XATTR_LAST_ENTRY(*xe)) {
378 err = -ENODATA;
379 goto out;
380 }
381
382 *base_addr = txattr_addr;
383 return 0;
384out:
Chao Yua9991502020-02-25 18:17:10 +0800385 xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
Chao Yuba38c272017-01-24 20:39:51 +0800386 return err;
387}
388
Chao Yu86696962016-09-18 23:30:04 +0800389static int read_all_xattrs(struct inode *inode, struct page *ipage,
390 void **base_addr)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900391{
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900392 struct f2fs_xattr_header *header;
Chao Yu89e9eab2017-03-23 13:38:25 +0800393 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);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900396 void *txattr_addr;
Chao Yu86696962016-09-18 23:30:04 +0800397 int err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900398
Chao Yuacbf0542017-11-30 19:28:17 +0800399 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
400 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900401 if (!txattr_addr)
Chao Yu86696962016-09-18 23:30:04 +0800402 return -ENOMEM;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900403
404 /* read from inline xattr */
405 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800406 err = read_inline_xattr(inode, ipage, txattr_addr);
407 if (err)
408 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900409 }
410
411 /* read from xattr node block */
Chao Yu89e9eab2017-03-23 13:38:25 +0800412 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800413 err = read_xattr_block(inode, txattr_addr);
414 if (err)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900415 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900416 }
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 }
Chao Yu86696962016-09-18 23:30:04 +0800425 *base_addr = txattr_addr;
426 return 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900427fail:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800428 kvfree(txattr_addr);
Chao Yu86696962016-09-18 23:30:04 +0800429 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900430}
431
432static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
433 void *txattr_addr, struct page *ipage)
434{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700435 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu89e9eab2017-03-23 13:38:25 +0800436 size_t inline_size = inline_xattr_size(inode);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700437 struct page *in_page = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900438 void *xattr_addr;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700439 void *inline_addr = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900440 struct page *xpage;
441 nid_t new_nid = 0;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700442 int err = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900443
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900444 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
Chao Yu4d57b862018-05-30 00:20:41 +0800445 if (!f2fs_alloc_nid(sbi, &new_nid))
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900446 return -ENOSPC;
447
448 /* write to inline xattr */
449 if (inline_size) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900450 if (ipage) {
Chao Yu6afc6622017-09-06 21:59:50 +0800451 inline_addr = inline_xattr_addr(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900452 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800453 in_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700454 if (IS_ERR(in_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800455 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700456 return PTR_ERR(in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900457 }
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700458 inline_addr = inline_xattr_addr(inode, in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900459 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900460
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700461 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
Chao Yubae0ee72018-12-25 17:43:42 +0800462 NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900463 /* no need to use xattr node block */
464 if (hsize <= inline_size) {
Chao Yu4d57b862018-05-30 00:20:41 +0800465 err = f2fs_truncate_xattr_node(inode);
466 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700467 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;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900474 }
475 }
476
477 /* write to xattr node block */
478 if (F2FS_I(inode)->i_xattr_nid) {
Chao Yu4d57b862018-05-30 00:20:41 +0800479 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900480 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800481 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800482 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700483 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900484 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700485 f2fs_bug_on(sbi, new_nid);
Chao Yubae0ee72018-12-25 17:43:42 +0800486 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900487 } else {
488 struct dnode_of_data dn;
489 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
Chao Yu4d57b862018-05-30 00:20:41 +0800490 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900491 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800492 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800493 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700494 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900495 }
Chao Yu4d57b862018-05-30 00:20:41 +0800496 f2fs_alloc_nid_done(sbi, new_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900497 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900498 xattr_addr = page_address(xpage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900499
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700500 if (inline_size)
501 memcpy(inline_addr, txattr_addr, inline_size);
502 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
503
504 if (inline_size)
505 set_page_dirty(ipage ? ipage : in_page);
506 set_page_dirty(xpage);
507
508 f2fs_put_page(xpage, 1);
509in_page_out:
510 f2fs_put_page(in_page, 1);
511 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900512}
513
Jaegeuk Kime1123262014-04-23 12:17:25 +0900514int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700515 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900516{
Chao Yuba38c272017-01-24 20:39:51 +0800517 struct f2fs_xattr_entry *entry = NULL;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900518 int error = 0;
Chao Yuba38c272017-01-24 20:39:51 +0800519 unsigned int size, len;
Chao Yuba38c272017-01-24 20:39:51 +0800520 void *base_addr = NULL;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800521 int base_size;
Chao Yua9991502020-02-25 18:17:10 +0800522 bool is_inline;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900523
524 if (name == NULL)
525 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900526
527 len = strlen(name);
528 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800529 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900530
Yunlei He27161f12017-09-07 10:40:54 +0800531 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yuba38c272017-01-24 20:39:51 +0800532 error = lookup_all_xattrs(inode, ipage, index, len, name,
Chao Yua9991502020-02-25 18:17:10 +0800533 &entry, &base_addr, &base_size, &is_inline);
Yunlei He27161f12017-09-07 10:40:54 +0800534 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800535 if (error)
536 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900537
Jaegeuk Kime1123262014-04-23 12:17:25 +0900538 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900539
Jaegeuk Kime1123262014-04-23 12:17:25 +0900540 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900541 error = -ERANGE;
Chao Yuba38c272017-01-24 20:39:51 +0800542 goto out;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900543 }
544
545 if (buffer) {
546 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800547
548 if (base_size - (pval - (char *)base_addr) < size) {
549 error = -ERANGE;
550 goto out;
551 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900552 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900553 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900554 error = size;
Chao Yuba38c272017-01-24 20:39:51 +0800555out:
Chao Yua9991502020-02-25 18:17:10 +0800556 xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900557 return error;
558}
559
560ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
561{
David Howells2b0143b2015-03-17 22:25:59 +0000562 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900563 struct f2fs_xattr_entry *entry;
Randall Huang688078e2019-10-18 14:56:22 +0800564 void *base_addr, *last_base_addr;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900565 int error = 0;
566 size_t rest = buffer_size;
567
Yunlei He27161f12017-09-07 10:40:54 +0800568 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800569 error = read_all_xattrs(inode, NULL, &base_addr);
Yunlei He27161f12017-09-07 10:40:54 +0800570 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800571 if (error)
572 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900573
Chao Yuba3b5832020-02-14 17:44:11 +0800574 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
Randall Huang688078e2019-10-18 14:56:22 +0800575
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900576 list_for_each_xattr(entry, base_addr) {
577 const struct xattr_handler *handler =
578 f2fs_xattr_handler(entry->e_name_index);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100579 const char *prefix;
580 size_t prefix_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900581 size_t size;
582
Randall Huang688078e2019-10-18 14:56:22 +0800583 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
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100592 if (!handler || (handler->list && !handler->list(dentry)))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900593 continue;
594
Gao Xiangeecfa422019-01-25 20:11:39 +0800595 prefix = xattr_prefix(handler);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100596 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;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900608 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900609 rest -= size;
610 }
611 error = buffer_size - rest;
612cleanup:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800613 kvfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900614 return error;
615}
616
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800617static 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;
Jaegeuk Kimb71dead2017-03-10 09:36:10 -0800621
622 return (le16_to_cpu(entry->e_value_size) == size) &&
623 !memcmp(pval, value, size);
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800624}
625
Jaegeuk Kime1123262014-04-23 12:17:25 +0900626static int __f2fs_setxattr(struct inode *inode, int index,
627 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900628 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900629{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900630 struct f2fs_xattr_entry *here, *last;
Randall Huang2777e652019-04-11 16:26:46 +0800631 void *base_addr, *last_base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900632 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900633 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900634 __u32 new_hsize;
Tiezhu Yanga0995af2016-06-28 07:27:59 +0800635 int error = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900636
637 if (name == NULL)
638 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900639
640 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900641 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900642
Jaegeuk Kime1123262014-04-23 12:17:25 +0900643 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900644
Chao Yu037fe702015-07-13 17:45:19 +0800645 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900646 return -ERANGE;
647
Chao Yu037fe702015-07-13 17:45:19 +0800648 if (size > MAX_VALUE_LEN(inode))
649 return -E2BIG;
650
Chao Yu86696962016-09-18 23:30:04 +0800651 error = read_all_xattrs(inode, ipage, &base_addr);
652 if (error)
653 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900654
Chao Yuba3b5832020-02-14 17:44:11 +0800655 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
Randall Huang2777e652019-04-11 16:26:46 +0800656
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900657 /* find entry with wanted name. */
Randall Huang2777e652019-04-11 16:26:46 +0800658 here = __find_xattr(base_addr, last_base_addr, index, len, name);
659 if (!here) {
Chao Yuc83414a2019-06-20 11:36:15 +0800660 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);
Chao Yu10f966b2019-06-20 11:36:14 +0800663 error = -EFSCORRUPTED;
Randall Huang2777e652019-04-11 16:26:46 +0800664 goto exit;
665 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900666
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900667 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900668
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800669 if (found) {
670 if ((flags & XATTR_CREATE)) {
671 error = -EEXIST;
672 goto exit;
673 }
674
Daeho Jeongb2c46922018-01-20 15:46:33 +0800675 if (value && f2fs_xattr_value_same(here, value, size))
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800676 goto exit;
677 } else if ((flags & XATTR_REPLACE)) {
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900678 error = -ENODATA;
679 goto exit;
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900680 }
681
682 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900683 while (!IS_XATTR_LAST_ENTRY(last))
684 last = XATTR_NEXT_ENTRY(last);
685
Jaegeuk Kime1123262014-04-23 12:17:25 +0900686 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900687
688 /* 1. Check space */
689 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900690 int free;
691 /*
692 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900693 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900694 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900695 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900696 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800697 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900698
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900699 if (unlikely(free < newsize)) {
Jaegeuk Kim58457f12016-04-12 11:52:30 -0700700 error = -E2BIG;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900701 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900702 }
703 }
704
705 /* 2. Remove old entry */
706 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900707 /*
708 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900709 * 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
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900719 new_hsize = (char *)last - (char *)base_addr;
720
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900721 /* 3. Write new entry */
722 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900723 char *pval;
724 /*
725 * Before we come here, old entry is removed.
726 * We just write new entry.
727 */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900728 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);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900734 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900735 }
736
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900737 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
738 if (error)
739 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900740
Jaegeuk Kim91942322016-05-20 10:13:22 -0700741 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
742 inode->i_mode = F2FS_I(inode)->i_acl_mode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700743 inode->i_ctime = current_time(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700744 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900745 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700746 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
747 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
748 f2fs_set_encrypted_inode(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700749 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700750 if (!error && S_ISDIR(inode->i_mode))
751 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
Namjae Jeon7c909772013-03-17 17:26:39 +0900752exit:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800753 kvfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900754 return error;
755}
Russ Knize52ab9562013-09-24 15:49:23 -0500756
Jaegeuk Kime1123262014-04-23 12:17:25 +0900757int f2fs_setxattr(struct inode *inode, int index, const char *name,
758 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900759 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500760{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700761 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500762 int err;
763
Chao Yua25c2cd2019-07-22 17:57:06 +0800764 if (unlikely(f2fs_cp_error(sbi)))
765 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800766 if (!f2fs_is_checkpoint_ready(sbi))
767 return -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +0800768
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200769 err = dquot_initialize(inode);
770 if (err)
771 return err;
772
Chao Yu4d57b862018-05-30 00:20:41 +0800773 /* this case is only from f2fs_init_inode_metadata */
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900774 if (ipage)
775 return __f2fs_setxattr(inode, index, name, value,
776 size, ipage, flags);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800777 f2fs_balance_fs(sbi, true);
Russ Knize52ab9562013-09-24 15:49:23 -0500778
Gu Zhenge4795562013-09-27 18:08:30 +0800779 f2fs_lock_op(sbi);
Yunlei He27161f12017-09-07 10:40:54 +0800780 down_write(&F2FS_I(inode)->i_xattr_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900781 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Yunlei He27161f12017-09-07 10:40:54 +0800782 up_write(&F2FS_I(inode)->i_xattr_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800783 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500784
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800785 f2fs_update_time(sbi, REQ_TIME);
Russ Knize52ab9562013-09-24 15:49:23 -0500786 return err;
787}
Chao Yua9991502020-02-25 18:17:10 +0800788
789int 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
807void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
808{
809 kmem_cache_destroy(sbi->inline_xattr_slab);
810}