blob: 10f1e784fa2390148aaa37f527526162be2d724d [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,
29 struct dentry *dentry, const char *name, void *buffer,
30 size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090031{
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33
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 }
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020048 return f2fs_getxattr(d_inode(dentry), handler->flags, name,
49 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,
53 struct dentry *dentry, const char *name, const void *value,
54 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090055{
56 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
57
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020058 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090059 case F2FS_XATTR_INDEX_USER:
60 if (!test_opt(sbi, XATTR_USER))
61 return -EOPNOTSUPP;
62 break;
63 case F2FS_XATTR_INDEX_TRUSTED:
64 if (!capable(CAP_SYS_ADMIN))
65 return -EPERM;
66 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090067 case F2FS_XATTR_INDEX_SECURITY:
68 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090069 default:
70 return -EINVAL;
71 }
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020072 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +090073 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090074}
75
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010076static bool f2fs_xattr_user_list(struct dentry *dentry)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090077{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010078 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090079
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010080 return test_opt(sbi, XATTR_USER);
81}
82
83static bool f2fs_xattr_trusted_list(struct dentry *dentry)
84{
85 return capable(CAP_SYS_ADMIN);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090086}
87
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020088static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
89 struct dentry *dentry, const char *name, void *buffer,
90 size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090091{
David Howells2b0143b2015-03-17 22:25:59 +000092 struct inode *inode = d_inode(dentry);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090093
Chao Yu84e97c22015-03-23 10:36:15 +080094 if (buffer)
95 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +090096 return sizeof(char);
97}
98
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020099static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
100 struct dentry *dentry, const char *name, const void *value,
101 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900102{
David Howells2b0143b2015-03-17 22:25:59 +0000103 struct inode *inode = d_inode(dentry);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900104
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900105 if (!inode_owner_or_capable(inode))
106 return -EPERM;
107 if (value == NULL)
108 return -EINVAL;
109
110 F2FS_I(inode)->i_advise |= *(char *)value;
Chao Yu30c62fd2015-03-23 10:37:39 +0800111 mark_inode_dirty(inode);
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
Jaegeuk Kime1123262014-04-23 12:17:25 +0900206static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
207 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900208{
209 struct f2fs_xattr_entry *entry;
210
211 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kime1123262014-04-23 12:17:25 +0900212 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900213 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900214 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900215 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900216 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900217 break;
218 }
219 return entry;
220}
221
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900222static void *read_all_xattrs(struct inode *inode, struct page *ipage)
223{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700224 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900225 struct f2fs_xattr_header *header;
226 size_t size = PAGE_SIZE, inline_size = 0;
227 void *txattr_addr;
228
229 inline_size = inline_xattr_size(inode);
230
Jaegeuk Kim808a1d72014-03-20 22:21:08 +0900231 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900232 if (!txattr_addr)
233 return NULL;
234
235 /* read from inline xattr */
236 if (inline_size) {
237 struct page *page = NULL;
238 void *inline_addr;
239
240 if (ipage) {
241 inline_addr = inline_xattr_addr(ipage);
242 } else {
243 page = get_node_page(sbi, inode->i_ino);
244 if (IS_ERR(page))
245 goto fail;
246 inline_addr = inline_xattr_addr(page);
247 }
248 memcpy(txattr_addr, inline_addr, inline_size);
249 f2fs_put_page(page, 1);
250 }
251
252 /* read from xattr node block */
253 if (F2FS_I(inode)->i_xattr_nid) {
254 struct page *xpage;
255 void *xattr_addr;
256
257 /* The inode already has an extended attribute block. */
258 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
259 if (IS_ERR(xpage))
260 goto fail;
261
262 xattr_addr = page_address(xpage);
263 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
264 f2fs_put_page(xpage, 1);
265 }
266
267 header = XATTR_HDR(txattr_addr);
268
269 /* never been allocated xattrs */
270 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
271 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
272 header->h_refcount = cpu_to_le32(1);
273 }
274 return txattr_addr;
275fail:
276 kzfree(txattr_addr);
277 return NULL;
278}
279
280static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
281 void *txattr_addr, struct page *ipage)
282{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900284 size_t inline_size = 0;
285 void *xattr_addr;
286 struct page *xpage;
287 nid_t new_nid = 0;
288 int err;
289
290 inline_size = inline_xattr_size(inode);
291
292 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
293 if (!alloc_nid(sbi, &new_nid))
294 return -ENOSPC;
295
296 /* write to inline xattr */
297 if (inline_size) {
298 struct page *page = NULL;
299 void *inline_addr;
300
301 if (ipage) {
302 inline_addr = inline_xattr_addr(ipage);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900303 f2fs_wait_on_page_writeback(ipage, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900304 } else {
305 page = get_node_page(sbi, inode->i_ino);
306 if (IS_ERR(page)) {
307 alloc_nid_failed(sbi, new_nid);
308 return PTR_ERR(page);
309 }
310 inline_addr = inline_xattr_addr(page);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900311 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900312 }
313 memcpy(inline_addr, txattr_addr, inline_size);
314 f2fs_put_page(page, 1);
315
316 /* no need to use xattr node block */
317 if (hsize <= inline_size) {
318 err = truncate_xattr_node(inode, ipage);
319 alloc_nid_failed(sbi, new_nid);
320 return err;
321 }
322 }
323
324 /* write to xattr node block */
325 if (F2FS_I(inode)->i_xattr_nid) {
326 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
327 if (IS_ERR(xpage)) {
328 alloc_nid_failed(sbi, new_nid);
329 return PTR_ERR(xpage);
330 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700331 f2fs_bug_on(sbi, new_nid);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900332 f2fs_wait_on_page_writeback(xpage, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900333 } else {
334 struct dnode_of_data dn;
335 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
336 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
337 if (IS_ERR(xpage)) {
338 alloc_nid_failed(sbi, new_nid);
339 return PTR_ERR(xpage);
340 }
341 alloc_nid_done(sbi, new_nid);
342 }
343
344 xattr_addr = page_address(xpage);
345 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
346 sizeof(struct node_footer));
347 set_page_dirty(xpage);
348 f2fs_put_page(xpage, 1);
349
350 /* need to checkpoint during fsync */
351 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
352 return 0;
353}
354
Jaegeuk Kime1123262014-04-23 12:17:25 +0900355int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700356 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900357{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900358 struct f2fs_xattr_entry *entry;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900359 void *base_addr;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900360 int error = 0;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900361 size_t size, len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900362
363 if (name == NULL)
364 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900365
366 len = strlen(name);
367 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800368 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900369
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700370 base_addr = read_all_xattrs(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900371 if (!base_addr)
372 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900373
Jaegeuk Kime1123262014-04-23 12:17:25 +0900374 entry = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900375 if (IS_XATTR_LAST_ENTRY(entry)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900376 error = -ENODATA;
377 goto cleanup;
378 }
379
Jaegeuk Kime1123262014-04-23 12:17:25 +0900380 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900381
Jaegeuk Kime1123262014-04-23 12:17:25 +0900382 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900383 error = -ERANGE;
384 goto cleanup;
385 }
386
387 if (buffer) {
388 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900389 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900390 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900391 error = size;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900392
393cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900394 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900395 return error;
396}
397
398ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
399{
David Howells2b0143b2015-03-17 22:25:59 +0000400 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900401 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900402 void *base_addr;
403 int error = 0;
404 size_t rest = buffer_size;
405
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900406 base_addr = read_all_xattrs(inode, NULL);
407 if (!base_addr)
408 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900409
410 list_for_each_xattr(entry, base_addr) {
411 const struct xattr_handler *handler =
412 f2fs_xattr_handler(entry->e_name_index);
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100413 const char *prefix;
414 size_t prefix_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900415 size_t size;
416
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100417 if (!handler || (handler->list && !handler->list(dentry)))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900418 continue;
419
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100420 prefix = handler->prefix ?: handler->name;
421 prefix_len = strlen(prefix);
422 size = prefix_len + entry->e_name_len + 1;
423 if (buffer) {
424 if (size > rest) {
425 error = -ERANGE;
426 goto cleanup;
427 }
428 memcpy(buffer, prefix, prefix_len);
429 buffer += prefix_len;
430 memcpy(buffer, entry->e_name, entry->e_name_len);
431 buffer += entry->e_name_len;
432 *buffer++ = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900433 }
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900434 rest -= size;
435 }
436 error = buffer_size - rest;
437cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900438 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900439 return error;
440}
441
Jaegeuk Kime1123262014-04-23 12:17:25 +0900442static int __f2fs_setxattr(struct inode *inode, int index,
443 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900444 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900445{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900446 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900447 struct f2fs_xattr_entry *here, *last;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900448 void *base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900449 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900450 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900451 __u32 new_hsize;
452 int error = -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900453
454 if (name == NULL)
455 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900456
457 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900458 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900459
Jaegeuk Kime1123262014-04-23 12:17:25 +0900460 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900461
Chao Yu037fe702015-07-13 17:45:19 +0800462 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900463 return -ERANGE;
464
Chao Yu037fe702015-07-13 17:45:19 +0800465 if (size > MAX_VALUE_LEN(inode))
466 return -E2BIG;
467
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900468 base_addr = read_all_xattrs(inode, ipage);
469 if (!base_addr)
470 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900471
472 /* find entry with wanted name. */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900473 here = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900474
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900475 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900476
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900477 if ((flags & XATTR_REPLACE) && !found) {
478 error = -ENODATA;
479 goto exit;
480 } else if ((flags & XATTR_CREATE) && found) {
481 error = -EEXIST;
482 goto exit;
483 }
484
485 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900486 while (!IS_XATTR_LAST_ENTRY(last))
487 last = XATTR_NEXT_ENTRY(last);
488
Jaegeuk Kime1123262014-04-23 12:17:25 +0900489 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900490
491 /* 1. Check space */
492 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900493 int free;
494 /*
495 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900496 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900497 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900498 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900499 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800500 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900501
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900502 if (unlikely(free < newsize)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900503 error = -ENOSPC;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900504 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900505 }
506 }
507
508 /* 2. Remove old entry */
509 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900510 /*
511 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900512 * If not found, remove operation is not needed.
513 */
514 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
515 int oldsize = ENTRY_SIZE(here);
516
517 memmove(here, next, (char *)last - (char *)next);
518 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
519 memset(last, 0, oldsize);
520 }
521
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900522 new_hsize = (char *)last - (char *)base_addr;
523
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900524 /* 3. Write new entry */
525 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900526 char *pval;
527 /*
528 * Before we come here, old entry is removed.
529 * We just write new entry.
530 */
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900531 memset(last, 0, newsize);
Jaegeuk Kime1123262014-04-23 12:17:25 +0900532 last->e_name_index = index;
533 last->e_name_len = len;
534 memcpy(last->e_name, name, len);
535 pval = last->e_name + len;
536 memcpy(pval, value, size);
537 last->e_value_size = cpu_to_le16(size);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900538 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900539 }
540
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900541 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
542 if (error)
543 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900544
545 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
546 inode->i_mode = fi->i_acl_mode;
547 inode->i_ctime = CURRENT_TIME;
548 clear_inode_flag(fi, FI_ACL_MODE);
549 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700550 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
551 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
552 f2fs_set_encrypted_inode(inode);
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900553
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900554 if (ipage)
555 update_inode(inode, ipage);
556 else
557 update_inode_page(inode);
Namjae Jeon7c909772013-03-17 17:26:39 +0900558exit:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900559 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900560 return error;
561}
Russ Knize52ab9562013-09-24 15:49:23 -0500562
Jaegeuk Kime1123262014-04-23 12:17:25 +0900563int f2fs_setxattr(struct inode *inode, int index, const char *name,
564 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900565 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500566{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700567 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500568 int err;
569
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900570 /* this case is only from init_inode_metadata */
571 if (ipage)
572 return __f2fs_setxattr(inode, index, name, value,
573 size, ipage, flags);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -0800574 f2fs_balance_fs(sbi, true);
Russ Knize52ab9562013-09-24 15:49:23 -0500575
Gu Zhenge4795562013-09-27 18:08:30 +0800576 f2fs_lock_op(sbi);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900577 /* protect xattr_ver */
578 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900579 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900580 up_write(&F2FS_I(inode)->i_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800581 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500582
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800583 f2fs_update_time(sbi, REQ_TIME);
Russ Knize52ab9562013-09-24 15:49:23 -0500584 return err;
585}