blob: d39a7923de423bdf5f599851566f24e54302e011 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaf48b852012-11-02 17:12:17 +09002 * fs/f2fs/xattr.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Portions of this code from linux/fs/ext2/xattr.c
8 *
9 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
10 *
11 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
12 * Extended attributes for symlinks and special files added per
13 * suggestion of Luka Renko <luka.renko@hermes.si>.
14 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
15 * Red Hat Inc.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21#include <linux/rwsem.h>
22#include <linux/f2fs_fs.h>
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090023#include <linux/security.h>
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -080024#include <linux/posix_acl_xattr.h>
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090025#include "f2fs.h"
26#include "xattr.h"
27
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020028static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040029 struct dentry *unused, struct inode *inode,
30 const char *name, void *buffer, size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090031{
Al Virob2968212016-04-10 20:48:24 -040032 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090033
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020034 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090035 case F2FS_XATTR_INDEX_USER:
36 if (!test_opt(sbi, XATTR_USER))
37 return -EOPNOTSUPP;
38 break;
39 case F2FS_XATTR_INDEX_TRUSTED:
40 if (!capable(CAP_SYS_ADMIN))
41 return -EPERM;
42 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090043 case F2FS_XATTR_INDEX_SECURITY:
44 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090045 default:
46 return -EINVAL;
47 }
Al Virob2968212016-04-10 20:48:24 -040048 return f2fs_getxattr(inode, handler->flags, name,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020049 buffer, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090050}
51
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020052static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040053 struct dentry *unused, struct inode *inode,
54 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020055 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090056{
Al Viro59301222016-05-27 10:19:30 -040057 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090058
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020059 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090060 case F2FS_XATTR_INDEX_USER:
61 if (!test_opt(sbi, XATTR_USER))
62 return -EOPNOTSUPP;
63 break;
64 case F2FS_XATTR_INDEX_TRUSTED:
65 if (!capable(CAP_SYS_ADMIN))
66 return -EPERM;
67 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090068 case F2FS_XATTR_INDEX_SECURITY:
69 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090070 default:
71 return -EINVAL;
72 }
Al Viro59301222016-05-27 10:19:30 -040073 return f2fs_setxattr(inode, handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +090074 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090075}
76
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010077static bool f2fs_xattr_user_list(struct dentry *dentry)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090078{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010079 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090080
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010081 return test_opt(sbi, XATTR_USER);
82}
83
84static bool f2fs_xattr_trusted_list(struct dentry *dentry)
85{
86 return capable(CAP_SYS_ADMIN);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090087}
88
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020089static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -040090 struct dentry *unused, struct inode *inode,
91 const char *name, void *buffer, size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090092{
Chao Yu84e97c22015-03-23 10:36:15 +080093 if (buffer)
94 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090095 return sizeof(char);
96}
97
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020098static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -040099 struct dentry *unused, struct inode *inode,
100 const char *name, const void *value,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200101 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900102{
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900103 if (!inode_owner_or_capable(inode))
104 return -EPERM;
105 if (value == NULL)
106 return -EINVAL;
107
108 F2FS_I(inode)->i_advise |= *(char *)value;
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700109 f2fs_mark_inode_dirty_sync(inode);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900110 return 0;
111}
112
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900113#ifdef CONFIG_F2FS_FS_SECURITY
114static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
115 void *page)
116{
117 const struct xattr *xattr;
118 int err = 0;
119
120 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900121 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900122 xattr->name, xattr->value,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900123 xattr->value_len, (struct page *)page, 0);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900124 if (err < 0)
125 break;
126 }
127 return err;
128}
129
130int f2fs_init_security(struct inode *inode, struct inode *dir,
131 const struct qstr *qstr, struct page *ipage)
132{
133 return security_inode_init_security(inode, dir, qstr,
134 &f2fs_initxattrs, ipage);
135}
136#endif
137
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900138const struct xattr_handler f2fs_xattr_user_handler = {
139 .prefix = XATTR_USER_PREFIX,
140 .flags = F2FS_XATTR_INDEX_USER,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100141 .list = f2fs_xattr_user_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900142 .get = f2fs_xattr_generic_get,
143 .set = f2fs_xattr_generic_set,
144};
145
146const struct xattr_handler f2fs_xattr_trusted_handler = {
147 .prefix = XATTR_TRUSTED_PREFIX,
148 .flags = F2FS_XATTR_INDEX_TRUSTED,
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100149 .list = f2fs_xattr_trusted_list,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900150 .get = f2fs_xattr_generic_get,
151 .set = f2fs_xattr_generic_set,
152};
153
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900154const struct xattr_handler f2fs_xattr_advise_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100155 .name = F2FS_SYSTEM_ADVISE_NAME,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900156 .flags = F2FS_XATTR_INDEX_ADVISE,
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900157 .get = f2fs_xattr_advise_get,
158 .set = f2fs_xattr_advise_set,
159};
160
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900161const struct xattr_handler f2fs_xattr_security_handler = {
162 .prefix = XATTR_SECURITY_PREFIX,
163 .flags = F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900164 .get = f2fs_xattr_generic_get,
165 .set = f2fs_xattr_generic_set,
166};
167
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900168static const struct xattr_handler *f2fs_xattr_handler_map[] = {
169 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
170#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800171 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
172 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900173#endif
174 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900175#ifdef CONFIG_F2FS_FS_SECURITY
176 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
177#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900178 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
179};
180
181const struct xattr_handler *f2fs_xattr_handlers[] = {
182 &f2fs_xattr_user_handler,
183#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800184 &posix_acl_access_xattr_handler,
185 &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900186#endif
187 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900188#ifdef CONFIG_F2FS_FS_SECURITY
189 &f2fs_xattr_security_handler,
190#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900191 &f2fs_xattr_advise_handler,
192 NULL,
193};
194
Jaegeuk Kime1123262014-04-23 12:17:25 +0900195static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900196{
197 const struct xattr_handler *handler = NULL;
198
Jaegeuk Kime1123262014-04-23 12:17:25 +0900199 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
200 handler = f2fs_xattr_handler_map[index];
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900201 return handler;
202}
203
Jaegeuk Kime1123262014-04-23 12:17:25 +0900204static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
205 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900206{
207 struct f2fs_xattr_entry *entry;
208
209 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kime1123262014-04-23 12:17:25 +0900210 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900211 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900212 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900213 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900214 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900215 break;
216 }
217 return entry;
218}
219
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900220static void *read_all_xattrs(struct inode *inode, struct page *ipage)
221{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700222 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900223 struct f2fs_xattr_header *header;
224 size_t size = PAGE_SIZE, inline_size = 0;
225 void *txattr_addr;
226
227 inline_size = inline_xattr_size(inode);
228
Jaegeuk Kim808a1d72014-03-20 22:21:08 +0900229 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900230 if (!txattr_addr)
231 return NULL;
232
233 /* read from inline xattr */
234 if (inline_size) {
235 struct page *page = NULL;
236 void *inline_addr;
237
238 if (ipage) {
239 inline_addr = inline_xattr_addr(ipage);
240 } else {
241 page = get_node_page(sbi, inode->i_ino);
242 if (IS_ERR(page))
243 goto fail;
244 inline_addr = inline_xattr_addr(page);
245 }
246 memcpy(txattr_addr, inline_addr, inline_size);
247 f2fs_put_page(page, 1);
248 }
249
250 /* read from xattr node block */
251 if (F2FS_I(inode)->i_xattr_nid) {
252 struct page *xpage;
253 void *xattr_addr;
254
255 /* The inode already has an extended attribute block. */
256 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
257 if (IS_ERR(xpage))
258 goto fail;
259
260 xattr_addr = page_address(xpage);
261 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
262 f2fs_put_page(xpage, 1);
263 }
264
265 header = XATTR_HDR(txattr_addr);
266
267 /* never been allocated xattrs */
268 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
269 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
270 header->h_refcount = cpu_to_le32(1);
271 }
272 return txattr_addr;
273fail:
274 kzfree(txattr_addr);
275 return NULL;
276}
277
278static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
279 void *txattr_addr, struct page *ipage)
280{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900282 size_t inline_size = 0;
283 void *xattr_addr;
284 struct page *xpage;
285 nid_t new_nid = 0;
286 int err;
287
288 inline_size = inline_xattr_size(inode);
289
290 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
291 if (!alloc_nid(sbi, &new_nid))
292 return -ENOSPC;
293
294 /* write to inline xattr */
295 if (inline_size) {
296 struct page *page = NULL;
297 void *inline_addr;
298
299 if (ipage) {
300 inline_addr = inline_xattr_addr(ipage);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800301 f2fs_wait_on_page_writeback(ipage, NODE, true);
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700302 set_page_dirty(ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900303 } else {
304 page = get_node_page(sbi, inode->i_ino);
305 if (IS_ERR(page)) {
306 alloc_nid_failed(sbi, new_nid);
307 return PTR_ERR(page);
308 }
309 inline_addr = inline_xattr_addr(page);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800310 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900311 }
312 memcpy(inline_addr, txattr_addr, inline_size);
313 f2fs_put_page(page, 1);
314
315 /* no need to use xattr node block */
316 if (hsize <= inline_size) {
317 err = truncate_xattr_node(inode, ipage);
318 alloc_nid_failed(sbi, new_nid);
319 return err;
320 }
321 }
322
323 /* write to xattr node block */
324 if (F2FS_I(inode)->i_xattr_nid) {
325 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
326 if (IS_ERR(xpage)) {
327 alloc_nid_failed(sbi, new_nid);
328 return PTR_ERR(xpage);
329 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700330 f2fs_bug_on(sbi, new_nid);
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800331 f2fs_wait_on_page_writeback(xpage, NODE, true);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900332 } else {
333 struct dnode_of_data dn;
334 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
335 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
336 if (IS_ERR(xpage)) {
337 alloc_nid_failed(sbi, new_nid);
338 return PTR_ERR(xpage);
339 }
340 alloc_nid_done(sbi, new_nid);
341 }
342
343 xattr_addr = page_address(xpage);
344 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
345 sizeof(struct node_footer));
346 set_page_dirty(xpage);
347 f2fs_put_page(xpage, 1);
348
349 /* need to checkpoint during fsync */
350 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
351 return 0;
352}
353
Jaegeuk Kime1123262014-04-23 12:17:25 +0900354int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700355 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900356{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900357 struct f2fs_xattr_entry *entry;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900358 void *base_addr;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900359 int error = 0;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900360 size_t size, len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900361
362 if (name == NULL)
363 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900364
365 len = strlen(name);
366 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800367 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900368
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700369 base_addr = read_all_xattrs(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900370 if (!base_addr)
371 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900372
Jaegeuk Kime1123262014-04-23 12:17:25 +0900373 entry = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900374 if (IS_XATTR_LAST_ENTRY(entry)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900375 error = -ENODATA;
376 goto cleanup;
377 }
378
Jaegeuk Kime1123262014-04-23 12:17:25 +0900379 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900380
Jaegeuk Kime1123262014-04-23 12:17:25 +0900381 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900382 error = -ERANGE;
383 goto cleanup;
384 }
385
386 if (buffer) {
387 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900388 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900389 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900390 error = size;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900391
392cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900393 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900394 return error;
395}
396
397ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
398{
David Howells2b0143b2015-03-17 22:25:59 +0000399 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900400 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900401 void *base_addr;
402 int error = 0;
403 size_t rest = buffer_size;
404
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900405 base_addr = read_all_xattrs(inode, NULL);
406 if (!base_addr)
407 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900408
409 list_for_each_xattr(entry, base_addr) {
410 const struct xattr_handler *handler =
411 f2fs_xattr_handler(entry->e_name_index);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100412 const char *prefix;
413 size_t prefix_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900414 size_t size;
415
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100416 if (!handler || (handler->list && !handler->list(dentry)))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900417 continue;
418
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100419 prefix = handler->prefix ?: handler->name;
420 prefix_len = strlen(prefix);
421 size = prefix_len + entry->e_name_len + 1;
422 if (buffer) {
423 if (size > rest) {
424 error = -ERANGE;
425 goto cleanup;
426 }
427 memcpy(buffer, prefix, prefix_len);
428 buffer += prefix_len;
429 memcpy(buffer, entry->e_name, entry->e_name_len);
430 buffer += entry->e_name_len;
431 *buffer++ = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900432 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900433 rest -= size;
434 }
435 error = buffer_size - rest;
436cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900437 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900438 return error;
439}
440
Jaegeuk Kime1123262014-04-23 12:17:25 +0900441static int __f2fs_setxattr(struct inode *inode, int index,
442 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900443 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900444{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900445 struct f2fs_xattr_entry *here, *last;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900446 void *base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900447 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900448 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900449 __u32 new_hsize;
Tiezhu Yanga0995af2016-06-28 07:27:59 +0800450 int error = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900451
452 if (name == NULL)
453 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900454
455 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900456 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900457
Jaegeuk Kime1123262014-04-23 12:17:25 +0900458 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900459
Chao Yu037fe702015-07-13 17:45:19 +0800460 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900461 return -ERANGE;
462
Chao Yu037fe702015-07-13 17:45:19 +0800463 if (size > MAX_VALUE_LEN(inode))
464 return -E2BIG;
465
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900466 base_addr = read_all_xattrs(inode, ipage);
467 if (!base_addr)
Tiezhu Yanga0995af2016-06-28 07:27:59 +0800468 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900469
470 /* find entry with wanted name. */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900471 here = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900472
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900473 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900474
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900475 if ((flags & XATTR_REPLACE) && !found) {
476 error = -ENODATA;
477 goto exit;
478 } else if ((flags & XATTR_CREATE) && found) {
479 error = -EEXIST;
480 goto exit;
481 }
482
483 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900484 while (!IS_XATTR_LAST_ENTRY(last))
485 last = XATTR_NEXT_ENTRY(last);
486
Jaegeuk Kime1123262014-04-23 12:17:25 +0900487 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900488
489 /* 1. Check space */
490 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900491 int free;
492 /*
493 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900494 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900495 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900496 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900497 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800498 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900499
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900500 if (unlikely(free < newsize)) {
Jaegeuk Kim58457f12016-04-12 11:52:30 -0700501 error = -E2BIG;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900502 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900503 }
504 }
505
506 /* 2. Remove old entry */
507 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900508 /*
509 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900510 * If not found, remove operation is not needed.
511 */
512 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
513 int oldsize = ENTRY_SIZE(here);
514
515 memmove(here, next, (char *)last - (char *)next);
516 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
517 memset(last, 0, oldsize);
518 }
519
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900520 new_hsize = (char *)last - (char *)base_addr;
521
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900522 /* 3. Write new entry */
523 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900524 char *pval;
525 /*
526 * Before we come here, old entry is removed.
527 * We just write new entry.
528 */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900529 last->e_name_index = index;
530 last->e_name_len = len;
531 memcpy(last->e_name, name, len);
532 pval = last->e_name + len;
533 memcpy(pval, value, size);
534 last->e_value_size = cpu_to_le16(size);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900535 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900536 }
537
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900538 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
539 if (error)
540 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900541
Jaegeuk Kim91942322016-05-20 10:13:22 -0700542 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
543 inode->i_mode = F2FS_I(inode)->i_acl_mode;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900544 inode->i_ctime = CURRENT_TIME;
Jaegeuk Kim91942322016-05-20 10:13:22 -0700545 clear_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900546 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700547 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
548 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
549 f2fs_set_encrypted_inode(inode);
Jaegeuk Kimb56ab832016-06-30 19:09:37 -0700550 f2fs_mark_inode_dirty_sync(inode);
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700551 if (!error && S_ISDIR(inode->i_mode))
552 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
Namjae Jeon7c909772013-03-17 17:26:39 +0900553exit:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900554 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900555 return error;
556}
Russ Knize52ab9562013-09-24 15:49:23 -0500557
Jaegeuk Kime1123262014-04-23 12:17:25 +0900558int f2fs_setxattr(struct inode *inode, int index, const char *name,
559 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900560 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500561{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700562 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500563 int err;
564
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900565 /* this case is only from init_inode_metadata */
566 if (ipage)
567 return __f2fs_setxattr(inode, index, name, value,
568 size, ipage, flags);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800569 f2fs_balance_fs(sbi, true);
Russ Knize52ab9562013-09-24 15:49:23 -0500570
Gu Zhenge4795562013-09-27 18:08:30 +0800571 f2fs_lock_op(sbi);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900572 /* protect xattr_ver */
573 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900574 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900575 up_write(&F2FS_I(inode)->i_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800576 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500577
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800578 f2fs_update_time(sbi, REQ_TIME);
Russ Knize52ab9562013-09-24 15:49:23 -0500579 return err;
580}