blob: f44b0c38398bc1cc9929fb951b760fc4c609beff [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"
24
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020025static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040026 struct dentry *unused, struct inode *inode,
27 const char *name, void *buffer, size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090028{
Al Virob2968212016-04-10 20:48:24 -040029 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090030
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020031 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090032 case F2FS_XATTR_INDEX_USER:
33 if (!test_opt(sbi, XATTR_USER))
34 return -EOPNOTSUPP;
35 break;
36 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090037 case F2FS_XATTR_INDEX_SECURITY:
38 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090039 default:
40 return -EINVAL;
41 }
Al Virob2968212016-04-10 20:48:24 -040042 return f2fs_getxattr(inode, handler->flags, name,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020043 buffer, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090044}
45
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020046static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040047 struct dentry *unused, struct inode *inode,
48 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020049 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090050{
Al Viro59301222016-05-27 10:19:30 -040051 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090052
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020053 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090054 case F2FS_XATTR_INDEX_USER:
55 if (!test_opt(sbi, XATTR_USER))
56 return -EOPNOTSUPP;
57 break;
58 case F2FS_XATTR_INDEX_TRUSTED:
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090059 case F2FS_XATTR_INDEX_SECURITY:
60 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090061 default:
62 return -EINVAL;
63 }
Al Viro59301222016-05-27 10:19:30 -040064 return f2fs_setxattr(inode, handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +090065 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090066}
67
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010068static bool f2fs_xattr_user_list(struct dentry *dentry)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090069{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010070 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090071
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010072 return test_opt(sbi, XATTR_USER);
73}
74
75static bool f2fs_xattr_trusted_list(struct dentry *dentry)
76{
77 return capable(CAP_SYS_ADMIN);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090078}
79
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020080static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040081 struct dentry *unused, struct inode *inode,
82 const char *name, void *buffer, size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090083{
Chao Yu84e97c22015-03-23 10:36:15 +080084 if (buffer)
85 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090086 return sizeof(char);
87}
88
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020089static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040090 struct dentry *unused, struct inode *inode,
91 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020092 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090093{
Chao Yu797c1cb2018-07-19 23:57:54 +080094 unsigned char old_advise = F2FS_I(inode)->i_advise;
95 unsigned char new_advise;
96
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090097 if (!inode_owner_or_capable(inode))
98 return -EPERM;
99 if (value == NULL)
100 return -EINVAL;
101
Chao Yu797c1cb2018-07-19 23:57:54 +0800102 new_advise = *(char *)value;
103 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
104 return -EINVAL;
105
106 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
107 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
108
109 F2FS_I(inode)->i_advise = new_advise;
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700110 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900111 return 0;
112}
113
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900114#ifdef CONFIG_F2FS_FS_SECURITY
115static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
116 void *page)
117{
118 const struct xattr *xattr;
119 int err = 0;
120
121 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900122 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900123 xattr->name, xattr->value,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900124 xattr->value_len, (struct page *)page, 0);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900125 if (err < 0)
126 break;
127 }
128 return err;
129}
130
131int f2fs_init_security(struct inode *inode, struct inode *dir,
132 const struct qstr *qstr, struct page *ipage)
133{
134 return security_inode_init_security(inode, dir, qstr,
135 &f2fs_initxattrs, ipage);
136}
137#endif
138
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900139const struct xattr_handler f2fs_xattr_user_handler = {
140 .prefix = XATTR_USER_PREFIX,
141 .flags = F2FS_XATTR_INDEX_USER,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100142 .list = f2fs_xattr_user_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900143 .get = f2fs_xattr_generic_get,
144 .set = f2fs_xattr_generic_set,
145};
146
147const struct xattr_handler f2fs_xattr_trusted_handler = {
148 .prefix = XATTR_TRUSTED_PREFIX,
149 .flags = F2FS_XATTR_INDEX_TRUSTED,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100150 .list = f2fs_xattr_trusted_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900151 .get = f2fs_xattr_generic_get,
152 .set = f2fs_xattr_generic_set,
153};
154
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900155const struct xattr_handler f2fs_xattr_advise_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100156 .name = F2FS_SYSTEM_ADVISE_NAME,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900157 .flags = F2FS_XATTR_INDEX_ADVISE,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900158 .get = f2fs_xattr_advise_get,
159 .set = f2fs_xattr_advise_set,
160};
161
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900162const struct xattr_handler f2fs_xattr_security_handler = {
163 .prefix = XATTR_SECURITY_PREFIX,
164 .flags = F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900165 .get = f2fs_xattr_generic_get,
166 .set = f2fs_xattr_generic_set,
167};
168
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900169static const struct xattr_handler *f2fs_xattr_handler_map[] = {
170 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
171#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800172 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
173 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900174#endif
175 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900176#ifdef CONFIG_F2FS_FS_SECURITY
177 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
178#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900179 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
180};
181
182const struct xattr_handler *f2fs_xattr_handlers[] = {
183 &f2fs_xattr_user_handler,
184#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800185 &posix_acl_access_xattr_handler,
186 &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900187#endif
188 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900189#ifdef CONFIG_F2FS_FS_SECURITY
190 &f2fs_xattr_security_handler,
191#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900192 &f2fs_xattr_advise_handler,
193 NULL,
194};
195
Jaegeuk Kime1123262014-04-23 12:17:25 +0900196static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900197{
198 const struct xattr_handler *handler = NULL;
199
Jaegeuk Kime1123262014-04-23 12:17:25 +0900200 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
201 handler = f2fs_xattr_handler_map[index];
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900202 return handler;
203}
204
Jaegeuk Kime1123262014-04-23 12:17:25 +0900205static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
206 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900207{
208 struct f2fs_xattr_entry *entry;
209
210 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kime1123262014-04-23 12:17:25 +0900211 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900212 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900213 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900214 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900215 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900216 break;
217 }
218 return entry;
219}
220
Chao Yu6afc6622017-09-06 21:59:50 +0800221static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
222 void *base_addr, void **last_addr, int index,
223 size_t len, const char *name)
Chao Yuba38c272017-01-24 20:39:51 +0800224{
225 struct f2fs_xattr_entry *entry;
Chao Yu6afc6622017-09-06 21:59:50 +0800226 unsigned int inline_size = inline_xattr_size(inode);
Chao Yuba38c272017-01-24 20:39:51 +0800227
228 list_for_each_xattr(entry, base_addr) {
229 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
230 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
231 base_addr + inline_size) {
232 *last_addr = entry;
233 return NULL;
234 }
235 if (entry->e_name_index != index)
236 continue;
237 if (entry->e_name_len != len)
238 continue;
239 if (!memcmp(entry->e_name, name, len))
240 break;
241 }
242 return entry;
243}
244
Chao Yua5f433f2017-09-04 18:58:02 +0800245static int read_inline_xattr(struct inode *inode, struct page *ipage,
246 void *txattr_addr)
247{
248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 unsigned int inline_size = inline_xattr_size(inode);
250 struct page *page = NULL;
251 void *inline_addr;
252
253 if (ipage) {
Chao Yu6afc6622017-09-06 21:59:50 +0800254 inline_addr = inline_xattr_addr(inode, ipage);
Chao Yua5f433f2017-09-04 18:58:02 +0800255 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800256 page = f2fs_get_node_page(sbi, inode->i_ino);
Chao Yua5f433f2017-09-04 18:58:02 +0800257 if (IS_ERR(page))
258 return PTR_ERR(page);
259
Chao Yu6afc6622017-09-06 21:59:50 +0800260 inline_addr = inline_xattr_addr(inode, page);
Chao Yua5f433f2017-09-04 18:58:02 +0800261 }
262 memcpy(txattr_addr, inline_addr, inline_size);
263 f2fs_put_page(page, 1);
264
265 return 0;
266}
267
Chao Yu63840692017-09-04 18:58:03 +0800268static int read_xattr_block(struct inode *inode, void *txattr_addr)
269{
270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
272 unsigned int inline_size = inline_xattr_size(inode);
273 struct page *xpage;
274 void *xattr_addr;
275
276 /* The inode already has an extended attribute block. */
Chao Yu4d57b862018-05-30 00:20:41 +0800277 xpage = f2fs_get_node_page(sbi, xnid);
Chao Yu63840692017-09-04 18:58:03 +0800278 if (IS_ERR(xpage))
279 return PTR_ERR(xpage);
280
281 xattr_addr = page_address(xpage);
282 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
283 f2fs_put_page(xpage, 1);
284
285 return 0;
286}
287
Chao Yuba38c272017-01-24 20:39:51 +0800288static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
289 unsigned int index, unsigned int len,
290 const char *name, struct f2fs_xattr_entry **xe,
291 void **base_addr)
292{
Chao Yuba38c272017-01-24 20:39:51 +0800293 void *cur_addr, *txattr_addr, *last_addr = NULL;
294 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
295 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
Chao Yu89e9eab2017-03-23 13:38:25 +0800296 unsigned int inline_size = inline_xattr_size(inode);
Chao Yuba38c272017-01-24 20:39:51 +0800297 int err = 0;
298
Chao Yuba38c272017-01-24 20:39:51 +0800299 if (!size && !inline_size)
300 return -ENODATA;
301
Chao Yuacbf0542017-11-30 19:28:17 +0800302 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
303 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
Chao Yuba38c272017-01-24 20:39:51 +0800304 if (!txattr_addr)
305 return -ENOMEM;
306
307 /* read from inline xattr */
308 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800309 err = read_inline_xattr(inode, ipage, txattr_addr);
310 if (err)
311 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800312
Chao Yu6afc6622017-09-06 21:59:50 +0800313 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
Chao Yuba38c272017-01-24 20:39:51 +0800314 index, len, name);
315 if (*xe)
316 goto check;
317 }
318
319 /* read from xattr node block */
320 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800321 err = read_xattr_block(inode, txattr_addr);
322 if (err)
Chao Yuba38c272017-01-24 20:39:51 +0800323 goto out;
Chao Yuba38c272017-01-24 20:39:51 +0800324 }
325
326 if (last_addr)
327 cur_addr = XATTR_HDR(last_addr) - 1;
328 else
329 cur_addr = txattr_addr;
330
331 *xe = __find_xattr(cur_addr, index, len, name);
332check:
333 if (IS_XATTR_LAST_ENTRY(*xe)) {
334 err = -ENODATA;
335 goto out;
336 }
337
338 *base_addr = txattr_addr;
339 return 0;
340out:
341 kzfree(txattr_addr);
342 return err;
343}
344
Chao Yu86696962016-09-18 23:30:04 +0800345static int read_all_xattrs(struct inode *inode, struct page *ipage,
346 void **base_addr)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900347{
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900348 struct f2fs_xattr_header *header;
Chao Yu89e9eab2017-03-23 13:38:25 +0800349 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
350 unsigned int size = VALID_XATTR_BLOCK_SIZE;
351 unsigned int inline_size = inline_xattr_size(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900352 void *txattr_addr;
Chao Yu86696962016-09-18 23:30:04 +0800353 int err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900354
Chao Yuacbf0542017-11-30 19:28:17 +0800355 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
356 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900357 if (!txattr_addr)
Chao Yu86696962016-09-18 23:30:04 +0800358 return -ENOMEM;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900359
360 /* read from inline xattr */
361 if (inline_size) {
Chao Yua5f433f2017-09-04 18:58:02 +0800362 err = read_inline_xattr(inode, ipage, txattr_addr);
363 if (err)
364 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900365 }
366
367 /* read from xattr node block */
Chao Yu89e9eab2017-03-23 13:38:25 +0800368 if (xnid) {
Chao Yu63840692017-09-04 18:58:03 +0800369 err = read_xattr_block(inode, txattr_addr);
370 if (err)
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900371 goto fail;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900372 }
373
374 header = XATTR_HDR(txattr_addr);
375
376 /* never been allocated xattrs */
377 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
378 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
379 header->h_refcount = cpu_to_le32(1);
380 }
Chao Yu86696962016-09-18 23:30:04 +0800381 *base_addr = txattr_addr;
382 return 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900383fail:
384 kzfree(txattr_addr);
Chao Yu86696962016-09-18 23:30:04 +0800385 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900386}
387
388static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
389 void *txattr_addr, struct page *ipage)
390{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700391 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu89e9eab2017-03-23 13:38:25 +0800392 size_t inline_size = inline_xattr_size(inode);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700393 struct page *in_page = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900394 void *xattr_addr;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700395 void *inline_addr = NULL;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900396 struct page *xpage;
397 nid_t new_nid = 0;
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700398 int err = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900399
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900400 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
Chao Yu4d57b862018-05-30 00:20:41 +0800401 if (!f2fs_alloc_nid(sbi, &new_nid))
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900402 return -ENOSPC;
403
404 /* write to inline xattr */
405 if (inline_size) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900406 if (ipage) {
Chao Yu6afc6622017-09-06 21:59:50 +0800407 inline_addr = inline_xattr_addr(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900408 } else {
Chao Yu4d57b862018-05-30 00:20:41 +0800409 in_page = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700410 if (IS_ERR(in_page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800411 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700412 return PTR_ERR(in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900413 }
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700414 inline_addr = inline_xattr_addr(inode, in_page);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900415 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900416
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700417 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
Chao Yubae0ee72018-12-25 17:43:42 +0800418 NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900419 /* no need to use xattr node block */
420 if (hsize <= inline_size) {
Chao Yu4d57b862018-05-30 00:20:41 +0800421 err = f2fs_truncate_xattr_node(inode);
422 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700423 if (err) {
424 f2fs_put_page(in_page, 1);
425 return err;
426 }
427 memcpy(inline_addr, txattr_addr, inline_size);
428 set_page_dirty(ipage ? ipage : in_page);
429 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900430 }
431 }
432
433 /* write to xattr node block */
434 if (F2FS_I(inode)->i_xattr_nid) {
Chao Yu4d57b862018-05-30 00:20:41 +0800435 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900436 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800437 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800438 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700439 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900440 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700441 f2fs_bug_on(sbi, new_nid);
Chao Yubae0ee72018-12-25 17:43:42 +0800442 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900443 } else {
444 struct dnode_of_data dn;
445 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
Chao Yu4d57b862018-05-30 00:20:41 +0800446 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900447 if (IS_ERR(xpage)) {
Jaegeuk Kimd6204392017-12-28 17:47:19 -0800448 err = PTR_ERR(xpage);
Chao Yu4d57b862018-05-30 00:20:41 +0800449 f2fs_alloc_nid_failed(sbi, new_nid);
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700450 goto in_page_out;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900451 }
Chao Yu4d57b862018-05-30 00:20:41 +0800452 f2fs_alloc_nid_done(sbi, new_nid);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900453 }
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900454 xattr_addr = page_address(xpage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900455
Jaegeuk Kimbf9c1422017-10-16 15:05:16 -0700456 if (inline_size)
457 memcpy(inline_addr, txattr_addr, inline_size);
458 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
459
460 if (inline_size)
461 set_page_dirty(ipage ? ipage : in_page);
462 set_page_dirty(xpage);
463
464 f2fs_put_page(xpage, 1);
465in_page_out:
466 f2fs_put_page(in_page, 1);
467 return err;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900468}
469
Jaegeuk Kime1123262014-04-23 12:17:25 +0900470int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700471 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900472{
Chao Yuba38c272017-01-24 20:39:51 +0800473 struct f2fs_xattr_entry *entry = NULL;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900474 int error = 0;
Chao Yuba38c272017-01-24 20:39:51 +0800475 unsigned int size, len;
Chao Yuba38c272017-01-24 20:39:51 +0800476 void *base_addr = NULL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900477
478 if (name == NULL)
479 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900480
481 len = strlen(name);
482 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800483 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900484
Yunlei He27161f12017-09-07 10:40:54 +0800485 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yuba38c272017-01-24 20:39:51 +0800486 error = lookup_all_xattrs(inode, ipage, index, len, name,
487 &entry, &base_addr);
Yunlei He27161f12017-09-07 10:40:54 +0800488 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800489 if (error)
490 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900491
Jaegeuk Kime1123262014-04-23 12:17:25 +0900492 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900493
Jaegeuk Kime1123262014-04-23 12:17:25 +0900494 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900495 error = -ERANGE;
Chao Yuba38c272017-01-24 20:39:51 +0800496 goto out;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900497 }
498
499 if (buffer) {
500 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900501 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900502 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900503 error = size;
Chao Yuba38c272017-01-24 20:39:51 +0800504out:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900505 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900506 return error;
507}
508
509ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
510{
David Howells2b0143b2015-03-17 22:25:59 +0000511 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900512 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900513 void *base_addr;
514 int error = 0;
515 size_t rest = buffer_size;
516
Yunlei He27161f12017-09-07 10:40:54 +0800517 down_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800518 error = read_all_xattrs(inode, NULL, &base_addr);
Yunlei He27161f12017-09-07 10:40:54 +0800519 up_read(&F2FS_I(inode)->i_xattr_sem);
Chao Yu86696962016-09-18 23:30:04 +0800520 if (error)
521 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900522
523 list_for_each_xattr(entry, base_addr) {
524 const struct xattr_handler *handler =
525 f2fs_xattr_handler(entry->e_name_index);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100526 const char *prefix;
527 size_t prefix_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900528 size_t size;
529
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100530 if (!handler || (handler->list && !handler->list(dentry)))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900531 continue;
532
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100533 prefix = handler->prefix ?: handler->name;
534 prefix_len = strlen(prefix);
535 size = prefix_len + entry->e_name_len + 1;
536 if (buffer) {
537 if (size > rest) {
538 error = -ERANGE;
539 goto cleanup;
540 }
541 memcpy(buffer, prefix, prefix_len);
542 buffer += prefix_len;
543 memcpy(buffer, entry->e_name, entry->e_name_len);
544 buffer += entry->e_name_len;
545 *buffer++ = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900546 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900547 rest -= size;
548 }
549 error = buffer_size - rest;
550cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900551 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900552 return error;
553}
554
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800555static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
556 const void *value, size_t size)
557{
558 void *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kimb71dead2017-03-10 09:36:10 -0800559
560 return (le16_to_cpu(entry->e_value_size) == size) &&
561 !memcmp(pval, value, size);
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800562}
563
Jaegeuk Kime1123262014-04-23 12:17:25 +0900564static int __f2fs_setxattr(struct inode *inode, int index,
565 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900566 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900567{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900568 struct f2fs_xattr_entry *here, *last;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900569 void *base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900570 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900571 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900572 __u32 new_hsize;
Tiezhu Yanga0995af2016-06-28 07:27:59 +0800573 int error = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900574
575 if (name == NULL)
576 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900577
578 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900579 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900580
Jaegeuk Kime1123262014-04-23 12:17:25 +0900581 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900582
Chao Yu037fe702015-07-13 17:45:19 +0800583 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900584 return -ERANGE;
585
Chao Yu037fe702015-07-13 17:45:19 +0800586 if (size > MAX_VALUE_LEN(inode))
587 return -E2BIG;
588
Chao Yu86696962016-09-18 23:30:04 +0800589 error = read_all_xattrs(inode, ipage, &base_addr);
590 if (error)
591 return error;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900592
593 /* find entry with wanted name. */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900594 here = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900595
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900596 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900597
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800598 if (found) {
599 if ((flags & XATTR_CREATE)) {
600 error = -EEXIST;
601 goto exit;
602 }
603
Daeho Jeongb2c46922018-01-20 15:46:33 +0800604 if (value && f2fs_xattr_value_same(here, value, size))
Kinglong Mee5f35a2c2017-02-25 19:23:27 +0800605 goto exit;
606 } else if ((flags & XATTR_REPLACE)) {
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900607 error = -ENODATA;
608 goto exit;
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900609 }
610
611 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900612 while (!IS_XATTR_LAST_ENTRY(last))
613 last = XATTR_NEXT_ENTRY(last);
614
Jaegeuk Kime1123262014-04-23 12:17:25 +0900615 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900616
617 /* 1. Check space */
618 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900619 int free;
620 /*
621 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900622 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900623 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900624 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900625 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800626 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900627
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900628 if (unlikely(free < newsize)) {
Jaegeuk Kim58457f12016-04-12 11:52:30 -0700629 error = -E2BIG;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900630 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900631 }
632 }
633
634 /* 2. Remove old entry */
635 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900636 /*
637 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900638 * If not found, remove operation is not needed.
639 */
640 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
641 int oldsize = ENTRY_SIZE(here);
642
643 memmove(here, next, (char *)last - (char *)next);
644 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
645 memset(last, 0, oldsize);
646 }
647
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900648 new_hsize = (char *)last - (char *)base_addr;
649
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900650 /* 3. Write new entry */
651 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900652 char *pval;
653 /*
654 * Before we come here, old entry is removed.
655 * We just write new entry.
656 */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900657 last->e_name_index = index;
658 last->e_name_len = len;
659 memcpy(last->e_name, name, len);
660 pval = last->e_name + len;
661 memcpy(pval, value, size);
662 last->e_value_size = cpu_to_le16(size);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900663 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900664 }
665
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900666 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
667 if (error)
668 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900669
Jaegeuk Kim91942322016-05-20 10:13:22 -0700670 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
671 inode->i_mode = F2FS_I(inode)->i_acl_mode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700672 inode->i_ctime = current_time(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -0700673 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900674 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700675 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
676 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
677 f2fs_set_encrypted_inode(inode);
Jaegeuk Kim7c457292016-10-14 11:51:23 -0700678 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700679 if (!error && S_ISDIR(inode->i_mode))
680 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
Namjae Jeon7c909772013-03-17 17:26:39 +0900681exit:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900682 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900683 return error;
684}
Russ Knize52ab9562013-09-24 15:49:23 -0500685
Jaegeuk Kime1123262014-04-23 12:17:25 +0900686int f2fs_setxattr(struct inode *inode, int index, const char *name,
687 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900688 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500689{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700690 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500691 int err;
692
Jaegeuk Kimd8d13892017-10-23 23:50:15 +0200693 err = dquot_initialize(inode);
694 if (err)
695 return err;
696
Chao Yu4d57b862018-05-30 00:20:41 +0800697 /* this case is only from f2fs_init_inode_metadata */
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900698 if (ipage)
699 return __f2fs_setxattr(inode, index, name, value,
700 size, ipage, flags);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800701 f2fs_balance_fs(sbi, true);
Russ Knize52ab9562013-09-24 15:49:23 -0500702
Gu Zhenge4795562013-09-27 18:08:30 +0800703 f2fs_lock_op(sbi);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900704 /* protect xattr_ver */
705 down_write(&F2FS_I(inode)->i_sem);
Yunlei He27161f12017-09-07 10:40:54 +0800706 down_write(&F2FS_I(inode)->i_xattr_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900707 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Yunlei He27161f12017-09-07 10:40:54 +0800708 up_write(&F2FS_I(inode)->i_xattr_sem);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900709 up_write(&F2FS_I(inode)->i_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800710 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500711
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800712 f2fs_update_time(sbi, REQ_TIME);
Russ Knize52ab9562013-09-24 15:49:23 -0500713 return err;
714}