blob: 181900af2576ba535042642856b8485a9b667ad6 [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
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020026static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040027 struct dentry *unused, struct inode *inode,
28 const char *name, void *buffer, size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090029{
Al Virob2968212016-04-10 20:48:24 -040030 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090031
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020032 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090033 case F2FS_XATTR_INDEX_USER:
34 if (!test_opt(sbi, XATTR_USER))
35 return -EOPNOTSUPP;
36 break;
37 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090038 case F2FS_XATTR_INDEX_SECURITY:
39 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090040 default:
41 return -EINVAL;
42 }
Al Virob2968212016-04-10 20:48:24 -040043 return f2fs_getxattr(inode, handler->flags, name,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020044 buffer, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090045}
46
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020047static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040048 struct dentry *unused, struct inode *inode,
49 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020050 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090051{
Al Viro59301222016-05-27 10:19:30 -040052 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090053
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020054 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090055 case F2FS_XATTR_INDEX_USER:
56 if (!test_opt(sbi, XATTR_USER))
57 return -EOPNOTSUPP;
58 break;
59 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090060 case F2FS_XATTR_INDEX_SECURITY:
61 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090062 default:
63 return -EINVAL;
64 }
Al Viro59301222016-05-27 10:19:30 -040065 return f2fs_setxattr(inode, handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +090066 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090067}
68
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010069static bool f2fs_xattr_user_list(struct dentry *dentry)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090070{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010071 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090072
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010073 return test_opt(sbi, XATTR_USER);
74}
75
76static bool f2fs_xattr_trusted_list(struct dentry *dentry)
77{
78 return capable(CAP_SYS_ADMIN);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090079}
80
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020081static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040082 struct dentry *unused, struct inode *inode,
83 const char *name, void *buffer, size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090084{
Chao Yu84e97c22015-03-23 10:36:15 +080085 if (buffer)
86 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090087 return sizeof(char);
88}
89
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020090static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040091 struct dentry *unused, struct inode *inode,
92 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020093 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090094{
Chao Yu797c1cb2018-07-19 23:57:54 +080095 unsigned char old_advise = F2FS_I(inode)->i_advise;
96 unsigned char new_advise;
97
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090098 if (!inode_owner_or_capable(inode))
99 return -EPERM;
100 if (value == NULL)
101 return -EINVAL;
102
Chao Yu797c1cb2018-07-19 23:57:54 +0800103 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;
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700111 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900112 return 0;
113}
114
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900115#ifdef CONFIG_F2FS_FS_SECURITY
116static 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++) {
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900123 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900124 xattr->name, xattr->value,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900125 xattr->value_len, (struct page *)page, 0);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900126 if (err < 0)
127 break;
128 }
129 return err;
130}
131
132int 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
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900140const struct xattr_handler f2fs_xattr_user_handler = {
141 .prefix = XATTR_USER_PREFIX,
142 .flags = F2FS_XATTR_INDEX_USER,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100143 .list = f2fs_xattr_user_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900144 .get = f2fs_xattr_generic_get,
145 .set = f2fs_xattr_generic_set,
146};
147
148const struct xattr_handler f2fs_xattr_trusted_handler = {
149 .prefix = XATTR_TRUSTED_PREFIX,
150 .flags = F2FS_XATTR_INDEX_TRUSTED,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100151 .list = f2fs_xattr_trusted_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900152 .get = f2fs_xattr_generic_get,
153 .set = f2fs_xattr_generic_set,
154};
155
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900156const struct xattr_handler f2fs_xattr_advise_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100157 .name = F2FS_SYSTEM_ADVISE_NAME,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900158 .flags = F2FS_XATTR_INDEX_ADVISE,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900159 .get = f2fs_xattr_advise_get,
160 .set = f2fs_xattr_advise_set,
161};
162
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900163const struct xattr_handler f2fs_xattr_security_handler = {
164 .prefix = XATTR_SECURITY_PREFIX,
165 .flags = F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900166 .get = f2fs_xattr_generic_get,
167 .set = f2fs_xattr_generic_set,
168};
169
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900170static const struct xattr_handler *f2fs_xattr_handler_map[] = {
171 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
172#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800173 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
174 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900175#endif
176 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900177#ifdef CONFIG_F2FS_FS_SECURITY
178 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
179#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900180 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
181};
182
183const struct xattr_handler *f2fs_xattr_handlers[] = {
184 &f2fs_xattr_user_handler,
185#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800186 &posix_acl_access_xattr_handler,
187 &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900188#endif
189 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900190#ifdef CONFIG_F2FS_FS_SECURITY
191 &f2fs_xattr_security_handler,
192#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900193 &f2fs_xattr_advise_handler,
194 NULL,
195};
196
Jaegeuk Kime1123262014-04-23 12:17:25 +0900197static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900198{
199 const struct xattr_handler *handler = NULL;
200
Jaegeuk Kime1123262014-04-23 12:17:25 +0900201 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
202 handler = f2fs_xattr_handler_map[index];
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900203 return handler;
204}
205
Randall Huang2777e652019-04-11 16:26:46 +0800206static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
207 void *last_base_addr, int index,
208 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900209{
210 struct f2fs_xattr_entry *entry;
211
212 list_for_each_xattr(entry, base_addr) {
Randall Huang2777e652019-04-11 16:26:46 +0800213 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
214 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
215 return NULL;
216
Jaegeuk Kime1123262014-04-23 12:17:25 +0900217 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900218 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900219 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900220 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900221 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900222 break;
223 }
224 return entry;
225}
226
Chao Yu6afc6622017-09-06 21:59:50 +0800227static 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)
Chao Yuba38c272017-01-24 20:39:51 +0800230{
231 struct f2fs_xattr_entry *entry;
Chao Yu6afc6622017-09-06 21:59:50 +0800232 unsigned int inline_size = inline_xattr_size(inode);
Chao Yu2c28aba2019-03-05 19:32:26 +0800233 void *max_addr = base_addr + inline_size;
Chao Yuba38c272017-01-24 20:39:51 +0800234
235 list_for_each_xattr(entry, base_addr) {
Chao Yu2c28aba2019-03-05 19:32:26 +0800236 if ((void *)entry + sizeof(__u32) > max_addr ||
237 (void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
Chao Yuba38c272017-01-24 20:39:51 +0800238 *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 }
Chao Yu2c28aba2019-03-05 19:32:26 +0800248
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 }
Chao Yuba38c272017-01-24 20:39:51 +0800255 return entry;
256}
257
Chao Yua5f433f2017-09-04 18:58:02 +0800258static 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) {
Chao Yu6afc6622017-09-06 21:59:50 +0800267 inline_addr = inline_xattr_addr(inode, ipage);
Chao Yua5f433f2017-09-04 18:58:02 +0800268 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800269 page = f2fs_get_node_page(sbi, inode->i_ino);
Chao Yua5f433f2017-09-04 18:58:02 +0800270 if (IS_ERR(page))
271 return PTR_ERR(page);
272
Chao Yu6afc6622017-09-06 21:59:50 +0800273 inline_addr = inline_xattr_addr(inode, page);
Chao Yua5f433f2017-09-04 18:58:02 +0800274 }
275 memcpy(txattr_addr, inline_addr, inline_size);
276 f2fs_put_page(page, 1);
277
278 return 0;
279}
280
Chao Yu63840692017-09-04 18:58:03 +0800281static 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. */
Chao Yu4d57b862018-05-30 00:20:41 +0800290 xpage = f2fs_get_node_page(sbi, xnid);
Chao Yu63840692017-09-04 18:58:03 +0800291 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
Chao Yuba38c272017-01-24 20:39:51 +0800301static 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,
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800304 void **base_addr, int *base_size)
Chao Yuba38c272017-01-24 20:39:51 +0800305{
Randall Huang2777e652019-04-11 16:26:46 +0800306 void *cur_addr, *txattr_addr, *last_txattr_addr;
307 void *last_addr = NULL;
Chao Yuba38c272017-01-24 20:39:51 +0800308 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Chao Yu89e9eab2017-03-23 13:38:25 +0800309 unsigned int inline_size = inline_xattr_size(inode);
Chao Yuba38c272017-01-24 20:39:51 +0800310 int err = 0;
311
Randall Huang2777e652019-04-11 16:26:46 +0800312 if (!xnid && !inline_size)
Chao Yuba38c272017-01-24 20:39:51 +0800313 return -ENODATA;
314
Randall Huang2777e652019-04-11 16:26:46 +0800315 *base_size = XATTR_SIZE(xnid, inode) + XATTR_PADDING_SIZE;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800316 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
Chao Yuba38c272017-01-24 20:39:51 +0800317 if (!txattr_addr)
318 return -ENOMEM;
319
Randall Huang2777e652019-04-11 16:26:46 +0800320 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(xnid, inode);
321
Chao Yuba38c272017-01-24 20:39:51 +0800322 /* read from inline xattr */
323 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800324 err = read_inline_xattr(inode, ipage, txattr_addr);
325 if (err)
326 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800327
Chao Yu6afc6622017-09-06 21:59:50 +0800328 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
Chao Yuba38c272017-01-24 20:39:51 +0800329 index, len, name);
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800330 if (*xe) {
331 *base_size = inline_size;
Chao Yuba38c272017-01-24 20:39:51 +0800332 goto check;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800333 }
Chao Yuba38c272017-01-24 20:39:51 +0800334 }
335
336 /* read from xattr node block */
337 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800338 err = read_xattr_block(inode, txattr_addr);
339 if (err)
Chao Yuba38c272017-01-24 20:39:51 +0800340 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800341 }
342
343 if (last_addr)
344 cur_addr = XATTR_HDR(last_addr) - 1;
345 else
346 cur_addr = txattr_addr;
347
Randall Huang2777e652019-04-11 16:26:46 +0800348 *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
349 if (!*xe) {
Chao Yuc83414a2019-06-20 11:36:15 +0800350 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);
Chao Yu10f966b2019-06-20 11:36:14 +0800353 err = -EFSCORRUPTED;
Randall Huang2777e652019-04-11 16:26:46 +0800354 goto out;
355 }
Chao Yuba38c272017-01-24 20:39:51 +0800356check:
357 if (IS_XATTR_LAST_ENTRY(*xe)) {
358 err = -ENODATA;
359 goto out;
360 }
361
362 *base_addr = txattr_addr;
363 return 0;
364out:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800365 kvfree(txattr_addr);
Chao Yuba38c272017-01-24 20:39:51 +0800366 return err;
367}
368
Chao Yu86696962016-09-18 23:30:04 +0800369static int read_all_xattrs(struct inode *inode, struct page *ipage,
370 void **base_addr)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900371{
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900372 struct f2fs_xattr_header *header;
Chao Yu89e9eab2017-03-23 13:38:25 +0800373 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);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900376 void *txattr_addr;
Chao Yu86696962016-09-18 23:30:04 +0800377 int err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900378
Chao Yuacbf0542017-11-30 19:28:17 +0800379 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
380 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900381 if (!txattr_addr)
Chao Yu86696962016-09-18 23:30:04 +0800382 return -ENOMEM;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900383
384 /* read from inline xattr */
385 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800386 err = read_inline_xattr(inode, ipage, txattr_addr);
387 if (err)
388 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900389 }
390
391 /* read from xattr node block */
Chao Yu89e9eab2017-03-23 13:38:25 +0800392 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800393 err = read_xattr_block(inode, txattr_addr);
394 if (err)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900395 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900396 }
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 }
Chao Yu86696962016-09-18 23:30:04 +0800405 *base_addr = txattr_addr;
406 return 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900407fail:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800408 kvfree(txattr_addr);
Chao Yu86696962016-09-18 23:30:04 +0800409 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900410}
411
412static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
413 void *txattr_addr, struct page *ipage)
414{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700415 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu89e9eab2017-03-23 13:38:25 +0800416 size_t inline_size = inline_xattr_size(inode);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700417 struct page *in_page = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900418 void *xattr_addr;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700419 void *inline_addr = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900420 struct page *xpage;
421 nid_t new_nid = 0;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700422 int err = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900423
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900424 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
Chao Yu4d57b862018-05-30 00:20:41 +0800425 if (!f2fs_alloc_nid(sbi, &new_nid))
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900426 return -ENOSPC;
427
428 /* write to inline xattr */
429 if (inline_size) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900430 if (ipage) {
Chao Yu6afc6622017-09-06 21:59:50 +0800431 inline_addr = inline_xattr_addr(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900432 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800433 in_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700434 if (IS_ERR(in_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800435 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700436 return PTR_ERR(in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900437 }
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700438 inline_addr = inline_xattr_addr(inode, in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900439 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900440
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700441 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
Chao Yubae0ee72018-12-25 17:43:42 +0800442 NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900443 /* no need to use xattr node block */
444 if (hsize <= inline_size) {
Chao Yu4d57b862018-05-30 00:20:41 +0800445 err = f2fs_truncate_xattr_node(inode);
446 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700447 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;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900454 }
455 }
456
457 /* write to xattr node block */
458 if (F2FS_I(inode)->i_xattr_nid) {
Chao Yu4d57b862018-05-30 00:20:41 +0800459 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900460 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800461 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800462 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700463 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900464 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700465 f2fs_bug_on(sbi, new_nid);
Chao Yubae0ee72018-12-25 17:43:42 +0800466 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900467 } else {
468 struct dnode_of_data dn;
469 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
Chao Yu4d57b862018-05-30 00:20:41 +0800470 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900471 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800472 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800473 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700474 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900475 }
Chao Yu4d57b862018-05-30 00:20:41 +0800476 f2fs_alloc_nid_done(sbi, new_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900477 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900478 xattr_addr = page_address(xpage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900479
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700480 if (inline_size)
481 memcpy(inline_addr, txattr_addr, inline_size);
482 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
483
484 if (inline_size)
485 set_page_dirty(ipage ? ipage : in_page);
486 set_page_dirty(xpage);
487
488 f2fs_put_page(xpage, 1);
489in_page_out:
490 f2fs_put_page(in_page, 1);
491 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900492}
493
Jaegeuk Kime1123262014-04-23 12:17:25 +0900494int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700495 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900496{
Chao Yuba38c272017-01-24 20:39:51 +0800497 struct f2fs_xattr_entry *entry = NULL;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900498 int error = 0;
Chao Yuba38c272017-01-24 20:39:51 +0800499 unsigned int size, len;
Chao Yuba38c272017-01-24 20:39:51 +0800500 void *base_addr = NULL;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800501 int base_size;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900502
503 if (name == NULL)
504 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900505
506 len = strlen(name);
507 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800508 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900509
Yunlei He27161f12017-09-07 10:40:54 +0800510 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yuba38c272017-01-24 20:39:51 +0800511 error = lookup_all_xattrs(inode, ipage, index, len, name,
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800512 &entry, &base_addr, &base_size);
Yunlei He27161f12017-09-07 10:40:54 +0800513 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800514 if (error)
515 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900516
Jaegeuk Kime1123262014-04-23 12:17:25 +0900517 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900518
Jaegeuk Kime1123262014-04-23 12:17:25 +0900519 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900520 error = -ERANGE;
Chao Yuba38c272017-01-24 20:39:51 +0800521 goto out;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900522 }
523
524 if (buffer) {
525 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kim64beba02018-12-26 19:54:07 -0800526
527 if (base_size - (pval - (char *)base_addr) < size) {
528 error = -ERANGE;
529 goto out;
530 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900531 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900532 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900533 error = size;
Chao Yuba38c272017-01-24 20:39:51 +0800534out:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800535 kvfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900536 return error;
537}
538
539ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
540{
David Howells2b0143b2015-03-17 22:25:59 +0000541 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900542 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900543 void *base_addr;
544 int error = 0;
545 size_t rest = buffer_size;
546
Yunlei He27161f12017-09-07 10:40:54 +0800547 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800548 error = read_all_xattrs(inode, NULL, &base_addr);
Yunlei He27161f12017-09-07 10:40:54 +0800549 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800550 if (error)
551 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900552
553 list_for_each_xattr(entry, base_addr) {
554 const struct xattr_handler *handler =
555 f2fs_xattr_handler(entry->e_name_index);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100556 const char *prefix;
557 size_t prefix_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900558 size_t size;
559
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100560 if (!handler || (handler->list && !handler->list(dentry)))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900561 continue;
562
Gao Xiangeecfa422019-01-25 20:11:39 +0800563 prefix = xattr_prefix(handler);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100564 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;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900576 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900577 rest -= size;
578 }
579 error = buffer_size - rest;
580cleanup:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800581 kvfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900582 return error;
583}
584
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800585static 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;
Jaegeuk Kimb71dead2017-03-10 09:36:10 -0800589
590 return (le16_to_cpu(entry->e_value_size) == size) &&
591 !memcmp(pval, value, size);
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800592}
593
Jaegeuk Kime1123262014-04-23 12:17:25 +0900594static int __f2fs_setxattr(struct inode *inode, int index,
595 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900596 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900597{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900598 struct f2fs_xattr_entry *here, *last;
Randall Huang2777e652019-04-11 16:26:46 +0800599 void *base_addr, *last_base_addr;
600 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900601 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900602 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900603 __u32 new_hsize;
Tiezhu Yanga0995af2016-06-28 07:27:59 +0800604 int error = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900605
606 if (name == NULL)
607 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900608
609 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900610 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900611
Jaegeuk Kime1123262014-04-23 12:17:25 +0900612 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900613
Chao Yu037fe702015-07-13 17:45:19 +0800614 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900615 return -ERANGE;
616
Chao Yu037fe702015-07-13 17:45:19 +0800617 if (size > MAX_VALUE_LEN(inode))
618 return -E2BIG;
619
Chao Yu86696962016-09-18 23:30:04 +0800620 error = read_all_xattrs(inode, ipage, &base_addr);
621 if (error)
622 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900623
Randall Huang2777e652019-04-11 16:26:46 +0800624 last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
625
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900626 /* find entry with wanted name. */
Randall Huang2777e652019-04-11 16:26:46 +0800627 here = __find_xattr(base_addr, last_base_addr, index, len, name);
628 if (!here) {
Chao Yuc83414a2019-06-20 11:36:15 +0800629 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);
Chao Yu10f966b2019-06-20 11:36:14 +0800632 error = -EFSCORRUPTED;
Randall Huang2777e652019-04-11 16:26:46 +0800633 goto exit;
634 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900635
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900636 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900637
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800638 if (found) {
639 if ((flags & XATTR_CREATE)) {
640 error = -EEXIST;
641 goto exit;
642 }
643
Daeho Jeongb2c46922018-01-20 15:46:33 +0800644 if (value && f2fs_xattr_value_same(here, value, size))
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800645 goto exit;
646 } else if ((flags & XATTR_REPLACE)) {
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900647 error = -ENODATA;
648 goto exit;
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900649 }
650
651 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900652 while (!IS_XATTR_LAST_ENTRY(last))
653 last = XATTR_NEXT_ENTRY(last);
654
Jaegeuk Kime1123262014-04-23 12:17:25 +0900655 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900656
657 /* 1. Check space */
658 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900659 int free;
660 /*
661 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900662 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900663 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900664 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900665 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800666 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900667
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900668 if (unlikely(free < newsize)) {
Jaegeuk Kim58457f12016-04-12 11:52:30 -0700669 error = -E2BIG;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900670 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900671 }
672 }
673
674 /* 2. Remove old entry */
675 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900676 /*
677 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900678 * 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
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900688 new_hsize = (char *)last - (char *)base_addr;
689
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900690 /* 3. Write new entry */
691 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900692 char *pval;
693 /*
694 * Before we come here, old entry is removed.
695 * We just write new entry.
696 */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900697 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);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900703 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900704 }
705
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900706 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
707 if (error)
708 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900709
Jaegeuk Kim91942322016-05-20 10:13:22 -0700710 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
711 inode->i_mode = F2FS_I(inode)->i_acl_mode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700712 inode->i_ctime = current_time(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700713 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900714 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700715 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
716 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
717 f2fs_set_encrypted_inode(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700718 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700719 if (!error && S_ISDIR(inode->i_mode))
720 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
Namjae Jeon7c909772013-03-17 17:26:39 +0900721exit:
Chao Yu2a6a7e72019-03-05 17:52:33 +0800722 kvfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900723 return error;
724}
Russ Knize52ab9562013-09-24 15:49:23 -0500725
Jaegeuk Kime1123262014-04-23 12:17:25 +0900726int f2fs_setxattr(struct inode *inode, int index, const char *name,
727 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900728 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500729{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700730 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500731 int err;
732
Chao Yua25c2cd2019-07-22 17:57:06 +0800733 if (unlikely(f2fs_cp_error(sbi)))
734 return -EIO;
Chao Yu00e09c02019-08-23 17:58:36 +0800735 if (!f2fs_is_checkpoint_ready(sbi))
736 return -ENOSPC;
Chao Yu955ebcd2019-07-22 17:57:05 +0800737
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200738 err = dquot_initialize(inode);
739 if (err)
740 return err;
741
Chao Yu4d57b862018-05-30 00:20:41 +0800742 /* this case is only from f2fs_init_inode_metadata */
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900743 if (ipage)
744 return __f2fs_setxattr(inode, index, name, value,
745 size, ipage, flags);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800746 f2fs_balance_fs(sbi, true);
Russ Knize52ab9562013-09-24 15:49:23 -0500747
Gu Zhenge4795562013-09-27 18:08:30 +0800748 f2fs_lock_op(sbi);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900749 /* protect xattr_ver */
750 down_write(&F2FS_I(inode)->i_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800751 down_write(&F2FS_I(inode)->i_xattr_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900752 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Yunlei He27161f12017-09-07 10:40:54 +0800753 up_write(&F2FS_I(inode)->i_xattr_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900754 up_write(&F2FS_I(inode)->i_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800755 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500756
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800757 f2fs_update_time(sbi, REQ_TIME);
Russ Knize52ab9562013-09-24 15:49:23 -0500758 return err;
759}