aboutsummaryrefslogtreecommitdiffstats
path: root/fs/f2fs
diff options
context:
space:
mode:
authorJaegeuk Kim <jaegeuk.kim@samsung.com>2012-11-02 04:12:17 -0400
committerJaegeuk Kim <jaegeuk.kim@samsung.com>2012-12-10 23:43:41 -0500
commitaf48b85b8cd3fbb12c9b6759c16db6d69c0b03da (patch)
tree90d490a0fdc6b75ff92dcf96449b405b8b618c9f /fs/f2fs
parent6b4ea0160ae236a6561defa28e19f973aedda9ff (diff)
f2fs: add xattr and acl functionalities
This implements xattr and acl functionalities. - F2FS uses a node page to contain use extended attributes. Signed-off-by: Changman Lee <cm224.lee@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
Diffstat (limited to 'fs/f2fs')
-rw-r--r--fs/f2fs/acl.c465
-rw-r--r--fs/f2fs/acl.h57
-rw-r--r--fs/f2fs/xattr.c389
-rw-r--r--fs/f2fs/xattr.h145
4 files changed, 1056 insertions, 0 deletions
diff --git a/fs/f2fs/acl.c b/fs/f2fs/acl.c
new file mode 100644
index 000000000000..dff2a2bfa755
--- /dev/null
+++ b/fs/f2fs/acl.c
@@ -0,0 +1,465 @@
1/**
2 * fs/f2fs/acl.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/acl.c
8 *
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15#include <linux/f2fs_fs.h>
16#include "f2fs.h"
17#include "xattr.h"
18#include "acl.h"
19
20#define get_inode_mode(i) ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
21 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
22
23static inline size_t f2fs_acl_size(int count)
24{
25 if (count <= 4) {
26 return sizeof(struct f2fs_acl_header) +
27 count * sizeof(struct f2fs_acl_entry_short);
28 } else {
29 return sizeof(struct f2fs_acl_header) +
30 4 * sizeof(struct f2fs_acl_entry_short) +
31 (count - 4) * sizeof(struct f2fs_acl_entry);
32 }
33}
34
35static inline int f2fs_acl_count(size_t size)
36{
37 ssize_t s;
38 size -= sizeof(struct f2fs_acl_header);
39 s = size - 4 * sizeof(struct f2fs_acl_entry_short);
40 if (s < 0) {
41 if (size % sizeof(struct f2fs_acl_entry_short))
42 return -1;
43 return size / sizeof(struct f2fs_acl_entry_short);
44 } else {
45 if (s % sizeof(struct f2fs_acl_entry))
46 return -1;
47 return s / sizeof(struct f2fs_acl_entry) + 4;
48 }
49}
50
51static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
52{
53 int i, count;
54 struct posix_acl *acl;
55 struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
56 struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
57 const char *end = value + size;
58
59 if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
60 return ERR_PTR(-EINVAL);
61
62 count = f2fs_acl_count(size);
63 if (count < 0)
64 return ERR_PTR(-EINVAL);
65 if (count == 0)
66 return NULL;
67
68 acl = posix_acl_alloc(count, GFP_KERNEL);
69 if (!acl)
70 return ERR_PTR(-ENOMEM);
71
72 for (i = 0; i < count; i++) {
73
74 if ((char *)entry > end)
75 goto fail;
76
77 acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
78 acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
79
80 switch (acl->a_entries[i].e_tag) {
81 case ACL_USER_OBJ:
82 case ACL_GROUP_OBJ:
83 case ACL_MASK:
84 case ACL_OTHER:
85 acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
86 entry = (struct f2fs_acl_entry *)((char *)entry +
87 sizeof(struct f2fs_acl_entry_short));
88 break;
89
90 case ACL_USER:
91 acl->a_entries[i].e_uid =
92 make_kuid(&init_user_ns,
93 le32_to_cpu(entry->e_id));
94 entry = (struct f2fs_acl_entry *)((char *)entry +
95 sizeof(struct f2fs_acl_entry));
96 break;
97 case ACL_GROUP:
98 acl->a_entries[i].e_gid =
99 make_kgid(&init_user_ns,
100 le32_to_cpu(entry->e_id));
101 entry = (struct f2fs_acl_entry *)((char *)entry +
102 sizeof(struct f2fs_acl_entry));
103 break;
104 default:
105 goto fail;
106 }
107 }
108 if ((char *)entry != end)
109 goto fail;
110 return acl;
111fail:
112 posix_acl_release(acl);
113 return ERR_PTR(-EINVAL);
114}
115
116static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
117{
118 struct f2fs_acl_header *f2fs_acl;
119 struct f2fs_acl_entry *entry;
120 int i;
121
122 f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
123 sizeof(struct f2fs_acl_entry), GFP_KERNEL);
124 if (!f2fs_acl)
125 return ERR_PTR(-ENOMEM);
126
127 f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
128 entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
129
130 for (i = 0; i < acl->a_count; i++) {
131
132 entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
133 entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
134
135 switch (acl->a_entries[i].e_tag) {
136 case ACL_USER:
137 entry->e_id = cpu_to_le32(
138 from_kuid(&init_user_ns,
139 acl->a_entries[i].e_uid));
140 entry = (struct f2fs_acl_entry *)((char *)entry +
141 sizeof(struct f2fs_acl_entry));
142 break;
143 case ACL_GROUP:
144 entry->e_id = cpu_to_le32(
145 from_kgid(&init_user_ns,
146 acl->a_entries[i].e_gid));
147 entry = (struct f2fs_acl_entry *)((char *)entry +
148 sizeof(struct f2fs_acl_entry));
149 break;
150 case ACL_USER_OBJ:
151 case ACL_GROUP_OBJ:
152 case ACL_MASK:
153 case ACL_OTHER:
154 entry = (struct f2fs_acl_entry *)((char *)entry +
155 sizeof(struct f2fs_acl_entry_short));
156 break;
157 default:
158 goto fail;
159 }
160 }
161 *size = f2fs_acl_size(acl->a_count);
162 return (void *)f2fs_acl;
163
164fail:
165 kfree(f2fs_acl);
166 return ERR_PTR(-EINVAL);
167}
168
169struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
170{
171 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
172 int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
173 void *value = NULL;
174 struct posix_acl *acl;
175 int retval;
176
177 if (!test_opt(sbi, POSIX_ACL))
178 return NULL;
179
180 acl = get_cached_acl(inode, type);
181 if (acl != ACL_NOT_CACHED)
182 return acl;
183
184 if (type == ACL_TYPE_ACCESS)
185 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
186
187 retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
188 if (retval > 0) {
189 value = kmalloc(retval, GFP_KERNEL);
190 if (!value)
191 return ERR_PTR(-ENOMEM);
192 retval = f2fs_getxattr(inode, name_index, "", value, retval);
193 }
194
195 if (retval < 0) {
196 if (retval == -ENODATA)
197 acl = NULL;
198 else
199 acl = ERR_PTR(retval);
200 } else {
201 acl = f2fs_acl_from_disk(value, retval);
202 }
203 kfree(value);
204 if (!IS_ERR(acl))
205 set_cached_acl(inode, type, acl);
206
207 return acl;
208}
209
210static int f2fs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
211{
212 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
213 struct f2fs_inode_info *fi = F2FS_I(inode);
214 int name_index;
215 void *value = NULL;
216 size_t size = 0;
217 int error;
218
219 if (!test_opt(sbi, POSIX_ACL))
220 return 0;
221 if (S_ISLNK(inode->i_mode))
222 return -EOPNOTSUPP;
223
224 switch (type) {
225 case ACL_TYPE_ACCESS:
226 name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
227 if (acl) {
228 error = posix_acl_equiv_mode(acl, &inode->i_mode);
229 if (error < 0)
230 return error;
231 set_acl_inode(fi, inode->i_mode);
232 if (error == 0)
233 acl = NULL;
234 }
235 break;
236
237 case ACL_TYPE_DEFAULT:
238 name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
239 if (!S_ISDIR(inode->i_mode))
240 return acl ? -EACCES : 0;
241 break;
242
243 default:
244 return -EINVAL;
245 }
246
247 if (acl) {
248 value = f2fs_acl_to_disk(acl, &size);
249 if (IS_ERR(value)) {
250 cond_clear_inode_flag(fi, FI_ACL_MODE);
251 return (int)PTR_ERR(value);
252 }
253 }
254
255 error = f2fs_setxattr(inode, name_index, "", value, size);
256
257 kfree(value);
258 if (!error)
259 set_cached_acl(inode, type, acl);
260
261 cond_clear_inode_flag(fi, FI_ACL_MODE);
262 return error;
263}
264
265int f2fs_init_acl(struct inode *inode, struct inode *dir)
266{
267 struct posix_acl *acl = NULL;
268 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
269 int error = 0;
270
271 if (!S_ISLNK(inode->i_mode)) {
272 if (test_opt(sbi, POSIX_ACL)) {
273 acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
274 if (IS_ERR(acl))
275 return PTR_ERR(acl);
276 }
277 if (!acl)
278 inode->i_mode &= ~current_umask();
279 }
280
281 if (test_opt(sbi, POSIX_ACL) && acl) {
282
283 if (S_ISDIR(inode->i_mode)) {
284 error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
285 if (error)
286 goto cleanup;
287 }
288 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
289 if (error < 0)
290 return error;
291 if (error > 0)
292 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
293 }
294cleanup:
295 posix_acl_release(acl);
296 return error;
297}
298
299int f2fs_acl_chmod(struct inode *inode)
300{
301 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
302 struct posix_acl *acl;
303 int error;
304 mode_t mode = get_inode_mode(inode);
305
306 if (!test_opt(sbi, POSIX_ACL))
307 return 0;
308 if (S_ISLNK(mode))
309 return -EOPNOTSUPP;
310
311 acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
312 if (IS_ERR(acl) || !acl)
313 return PTR_ERR(acl);
314
315 error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
316 if (error)
317 return error;
318 error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
319 posix_acl_release(acl);
320 return error;
321}
322
323static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
324 size_t list_size, const char *name, size_t name_len, int type)
325{
326 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
327 const char *xname = POSIX_ACL_XATTR_DEFAULT;
328 size_t size;
329
330 if (!test_opt(sbi, POSIX_ACL))
331 return 0;
332
333 if (type == ACL_TYPE_ACCESS)
334 xname = POSIX_ACL_XATTR_ACCESS;
335
336 size = strlen(xname) + 1;
337 if (list && size <= list_size)
338 memcpy(list, xname, size);
339 return size;
340}
341
342static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
343 void *buffer, size_t size, int type)
344{
345 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
346 struct posix_acl *acl;
347 int error;
348
349 if (strcmp(name, "") != 0)
350 return -EINVAL;
351 if (!test_opt(sbi, POSIX_ACL))
352 return -EOPNOTSUPP;
353
354 acl = f2fs_get_acl(dentry->d_inode, type);
355 if (IS_ERR(acl))
356 return PTR_ERR(acl);
357 if (!acl)
358 return -ENODATA;
359 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
360 posix_acl_release(acl);
361
362 return error;
363}
364
365static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
366 const void *value, size_t size, int flags, int type)
367{
368 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
369 struct inode *inode = dentry->d_inode;
370 struct posix_acl *acl = NULL;
371 int error;
372
373 if (strcmp(name, "") != 0)
374 return -EINVAL;
375 if (!test_opt(sbi, POSIX_ACL))
376 return -EOPNOTSUPP;
377 if (!inode_owner_or_capable(inode))
378 return -EPERM;
379
380 if (value) {
381 acl = posix_acl_from_xattr(&init_user_ns, value, size);
382 if (IS_ERR(acl))
383 return PTR_ERR(acl);
384 if (acl) {
385 error = posix_acl_valid(acl);
386 if (error)
387 goto release_and_out;
388 }
389 } else {
390 acl = NULL;
391 }
392
393 error = f2fs_set_acl(inode, type, acl);
394
395release_and_out:
396 posix_acl_release(acl);
397 return error;
398}
399
400const struct xattr_handler f2fs_xattr_acl_default_handler = {
401 .prefix = POSIX_ACL_XATTR_DEFAULT,
402 .flags = ACL_TYPE_DEFAULT,
403 .list = f2fs_xattr_list_acl,
404 .get = f2fs_xattr_get_acl,
405 .set = f2fs_xattr_set_acl,
406};
407
408const struct xattr_handler f2fs_xattr_acl_access_handler = {
409 .prefix = POSIX_ACL_XATTR_ACCESS,
410 .flags = ACL_TYPE_ACCESS,
411 .list = f2fs_xattr_list_acl,
412 .get = f2fs_xattr_get_acl,
413 .set = f2fs_xattr_set_acl,
414};
415
416static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
417 size_t list_size, const char *name, size_t name_len, int type)
418{
419 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
420 size_t size;
421
422 if (type != F2FS_XATTR_INDEX_ADVISE)
423 return 0;
424
425 size = strlen(xname) + 1;
426 if (list && size <= list_size)
427 memcpy(list, xname, size);
428 return size;
429}
430
431static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
432 void *buffer, size_t size, int type)
433{
434 struct inode *inode = dentry->d_inode;
435
436 if (strcmp(name, "") != 0)
437 return -EINVAL;
438
439 *((char *)buffer) = F2FS_I(inode)->i_advise;
440 return sizeof(char);
441}
442
443static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
444 const void *value, size_t size, int flags, int type)
445{
446 struct inode *inode = dentry->d_inode;
447
448 if (strcmp(name, "") != 0)
449 return -EINVAL;
450 if (!inode_owner_or_capable(inode))
451 return -EPERM;
452 if (value == NULL)
453 return -EINVAL;
454
455 F2FS_I(inode)->i_advise |= *(char *)value;
456 return 0;
457}
458
459const struct xattr_handler f2fs_xattr_advise_handler = {
460 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
461 .flags = F2FS_XATTR_INDEX_ADVISE,
462 .list = f2fs_xattr_advise_list,
463 .get = f2fs_xattr_advise_get,
464 .set = f2fs_xattr_advise_set,
465};
diff --git a/fs/f2fs/acl.h b/fs/f2fs/acl.h
new file mode 100644
index 000000000000..c97675e18fe2
--- /dev/null
+++ b/fs/f2fs/acl.h
@@ -0,0 +1,57 @@
1/**
2 * fs/f2fs/acl.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Portions of this code from linux/fs/ext2/acl.h
8 *
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15#ifndef __F2FS_ACL_H__
16#define __F2FS_ACL_H__
17
18#include <linux/posix_acl_xattr.h>
19
20#define F2FS_ACL_VERSION 0x0001
21
22struct f2fs_acl_entry {
23 __le16 e_tag;
24 __le16 e_perm;
25 __le32 e_id;
26};
27
28struct f2fs_acl_entry_short {
29 __le16 e_tag;
30 __le16 e_perm;
31};
32
33struct f2fs_acl_header {
34 __le32 a_version;
35};
36
37#ifdef CONFIG_F2FS_FS_POSIX_ACL
38
39extern struct posix_acl *f2fs_get_acl(struct inode *inode, int type);
40extern int f2fs_acl_chmod(struct inode *inode);
41extern int f2fs_init_acl(struct inode *inode, struct inode *dir);
42#else
43#define f2fs_check_acl NULL
44#define f2fs_get_acl NULL
45#define f2fs_set_acl NULL
46
47static inline int f2fs_acl_chmod(struct inode *inode)
48{
49 return 0;
50}
51
52static inline int f2fs_init_acl(struct inode *inode, struct inode *dir)
53{
54 return 0;
55}
56#endif
57#endif /* __F2FS_ACL_H__ */
diff --git a/fs/f2fs/xattr.c b/fs/f2fs/xattr.c
new file mode 100644
index 000000000000..aca50fe163f6
--- /dev/null
+++ b/fs/f2fs/xattr.c
@@ -0,0 +1,389 @@
1/**
2 * 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>
23#include "f2fs.h"
24#include "xattr.h"
25
26static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
27 size_t list_size, const char *name, size_t name_len, int type)
28{
29 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
30 int total_len, prefix_len = 0;
31 const char *prefix = NULL;
32
33 switch (type) {
34 case F2FS_XATTR_INDEX_USER:
35 if (!test_opt(sbi, XATTR_USER))
36 return -EOPNOTSUPP;
37 prefix = XATTR_USER_PREFIX;
38 prefix_len = XATTR_USER_PREFIX_LEN;
39 break;
40 case F2FS_XATTR_INDEX_TRUSTED:
41 if (!capable(CAP_SYS_ADMIN))
42 return -EPERM;
43 prefix = XATTR_TRUSTED_PREFIX;
44 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
45 break;
46 default:
47 return -EINVAL;
48 }
49
50 total_len = prefix_len + name_len + 1;
51 if (list && total_len <= list_size) {
52 memcpy(list, prefix, prefix_len);
53 memcpy(list+prefix_len, name, name_len);
54 list[prefix_len + name_len] = '\0';
55 }
56 return total_len;
57}
58
59static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
60 void *buffer, size_t size, int type)
61{
62 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
63
64 switch (type) {
65 case F2FS_XATTR_INDEX_USER:
66 if (!test_opt(sbi, XATTR_USER))
67 return -EOPNOTSUPP;
68 break;
69 case F2FS_XATTR_INDEX_TRUSTED:
70 if (!capable(CAP_SYS_ADMIN))
71 return -EPERM;
72 break;
73 default:
74 return -EINVAL;
75 }
76 if (strcmp(name, "") == 0)
77 return -EINVAL;
78 return f2fs_getxattr(dentry->d_inode, type, name,
79 buffer, size);
80}
81
82static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
83 const void *value, size_t size, int flags, int type)
84{
85 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
86
87 switch (type) {
88 case F2FS_XATTR_INDEX_USER:
89 if (!test_opt(sbi, XATTR_USER))
90 return -EOPNOTSUPP;
91 break;
92 case F2FS_XATTR_INDEX_TRUSTED:
93 if (!capable(CAP_SYS_ADMIN))
94 return -EPERM;
95 break;
96 default:
97 return -EINVAL;
98 }
99 if (strcmp(name, "") == 0)
100 return -EINVAL;
101
102 return f2fs_setxattr(dentry->d_inode, type, name, value, size);
103}
104
105const struct xattr_handler f2fs_xattr_user_handler = {
106 .prefix = XATTR_USER_PREFIX,
107 .flags = F2FS_XATTR_INDEX_USER,
108 .list = f2fs_xattr_generic_list,
109 .get = f2fs_xattr_generic_get,
110 .set = f2fs_xattr_generic_set,
111};
112
113const struct xattr_handler f2fs_xattr_trusted_handler = {
114 .prefix = XATTR_TRUSTED_PREFIX,
115 .flags = F2FS_XATTR_INDEX_TRUSTED,
116 .list = f2fs_xattr_generic_list,
117 .get = f2fs_xattr_generic_get,
118 .set = f2fs_xattr_generic_set,
119};
120
121static const struct xattr_handler *f2fs_xattr_handler_map[] = {
122 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
123#ifdef CONFIG_F2FS_FS_POSIX_ACL
124 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
125 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
126#endif
127 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
128 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
129};
130
131const struct xattr_handler *f2fs_xattr_handlers[] = {
132 &f2fs_xattr_user_handler,
133#ifdef CONFIG_F2FS_FS_POSIX_ACL
134 &f2fs_xattr_acl_access_handler,
135 &f2fs_xattr_acl_default_handler,
136#endif
137 &f2fs_xattr_trusted_handler,
138 &f2fs_xattr_advise_handler,
139 NULL,
140};
141
142static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
143{
144 const struct xattr_handler *handler = NULL;
145
146 if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
147 handler = f2fs_xattr_handler_map[name_index];
148 return handler;
149}
150
151int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
152 void *buffer, size_t buffer_size)
153{
154 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
155 struct f2fs_inode_info *fi = F2FS_I(inode);
156 struct f2fs_xattr_entry *entry;
157 struct page *page;
158 void *base_addr;
159 int error = 0, found = 0;
160 int value_len, name_len;
161
162 if (name == NULL)
163 return -EINVAL;
164 name_len = strlen(name);
165
166 if (!fi->i_xattr_nid)
167 return -ENODATA;
168
169 page = get_node_page(sbi, fi->i_xattr_nid);
170 base_addr = page_address(page);
171
172 list_for_each_xattr(entry, base_addr) {
173 if (entry->e_name_index != name_index)
174 continue;
175 if (entry->e_name_len != name_len)
176 continue;
177 if (!memcmp(entry->e_name, name, name_len)) {
178 found = 1;
179 break;
180 }
181 }
182 if (!found) {
183 error = -ENODATA;
184 goto cleanup;
185 }
186
187 value_len = le16_to_cpu(entry->e_value_size);
188
189 if (buffer && value_len > buffer_size) {
190 error = -ERANGE;
191 goto cleanup;
192 }
193
194 if (buffer) {
195 char *pval = entry->e_name + entry->e_name_len;
196 memcpy(buffer, pval, value_len);
197 }
198 error = value_len;
199
200cleanup:
201 f2fs_put_page(page, 1);
202 return error;
203}
204
205ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
206{
207 struct inode *inode = dentry->d_inode;
208 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
209 struct f2fs_inode_info *fi = F2FS_I(inode);
210 struct f2fs_xattr_entry *entry;
211 struct page *page;
212 void *base_addr;
213 int error = 0;
214 size_t rest = buffer_size;
215
216 if (!fi->i_xattr_nid)
217 return 0;
218
219 page = get_node_page(sbi, fi->i_xattr_nid);
220 base_addr = page_address(page);
221
222 list_for_each_xattr(entry, base_addr) {
223 const struct xattr_handler *handler =
224 f2fs_xattr_handler(entry->e_name_index);
225 size_t size;
226
227 if (!handler)
228 continue;
229
230 size = handler->list(dentry, buffer, rest, entry->e_name,
231 entry->e_name_len, handler->flags);
232 if (buffer && size > rest) {
233 error = -ERANGE;
234 goto cleanup;
235 }
236
237 if (buffer)
238 buffer += size;
239 rest -= size;
240 }
241 error = buffer_size - rest;
242cleanup:
243 f2fs_put_page(page, 1);
244 return error;
245}
246
247int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
248 const void *value, size_t value_len)
249{
250 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
251 struct f2fs_inode_info *fi = F2FS_I(inode);
252 struct f2fs_xattr_header *header = NULL;
253 struct f2fs_xattr_entry *here, *last;
254 struct page *page;
255 void *base_addr;
256 int error, found, free, name_len, newsize;
257 char *pval;
258
259 if (name == NULL)
260 return -EINVAL;
261 name_len = strlen(name);
262
263 if (value == NULL)
264 value_len = 0;
265
266 if (name_len > 255 || value_len > MAX_VALUE_LEN)
267 return -ERANGE;
268
269 mutex_lock_op(sbi, NODE_NEW);
270 if (!fi->i_xattr_nid) {
271 /* Allocate new attribute block */
272 struct dnode_of_data dn;
273
274 if (!alloc_nid(sbi, &fi->i_xattr_nid)) {
275 mutex_unlock_op(sbi, NODE_NEW);
276 return -ENOSPC;
277 }
278 set_new_dnode(&dn, inode, NULL, NULL, fi->i_xattr_nid);
279 mark_inode_dirty(inode);
280
281 page = new_node_page(&dn, XATTR_NODE_OFFSET);
282 if (IS_ERR(page)) {
283 alloc_nid_failed(sbi, fi->i_xattr_nid);
284 fi->i_xattr_nid = 0;
285 mutex_unlock_op(sbi, NODE_NEW);
286 return PTR_ERR(page);
287 }
288
289 alloc_nid_done(sbi, fi->i_xattr_nid);
290 base_addr = page_address(page);
291 header = XATTR_HDR(base_addr);
292 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
293 header->h_refcount = cpu_to_le32(1);
294 } else {
295 /* The inode already has an extended attribute block. */
296 page = get_node_page(sbi, fi->i_xattr_nid);
297 if (IS_ERR(page)) {
298 mutex_unlock_op(sbi, NODE_NEW);
299 return PTR_ERR(page);
300 }
301
302 base_addr = page_address(page);
303 header = XATTR_HDR(base_addr);
304 }
305
306 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
307 error = -EIO;
308 goto cleanup;
309 }
310
311 /* find entry with wanted name. */
312 found = 0;
313 list_for_each_xattr(here, base_addr) {
314 if (here->e_name_index != name_index)
315 continue;
316 if (here->e_name_len != name_len)
317 continue;
318 if (!memcmp(here->e_name, name, name_len)) {
319 found = 1;
320 break;
321 }
322 }
323
324 last = here;
325
326 while (!IS_XATTR_LAST_ENTRY(last))
327 last = XATTR_NEXT_ENTRY(last);
328
329 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
330 name_len + value_len);
331
332 /* 1. Check space */
333 if (value) {
334 /* If value is NULL, it is remove operation.
335 * In case of update operation, we caculate free.
336 */
337 free = MIN_OFFSET - ((char *)last - (char *)header);
338 if (found)
339 free = free - ENTRY_SIZE(here);
340
341 if (free < newsize) {
342 error = -ENOSPC;
343 goto cleanup;
344 }
345 }
346
347 /* 2. Remove old entry */
348 if (found) {
349 /* If entry is found, remove old entry.
350 * If not found, remove operation is not needed.
351 */
352 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
353 int oldsize = ENTRY_SIZE(here);
354
355 memmove(here, next, (char *)last - (char *)next);
356 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
357 memset(last, 0, oldsize);
358 }
359
360 /* 3. Write new entry */
361 if (value) {
362 /* Before we come here, old entry is removed.
363 * We just write new entry. */
364 memset(last, 0, newsize);
365 last->e_name_index = name_index;
366 last->e_name_len = name_len;
367 memcpy(last->e_name, name, name_len);
368 pval = last->e_name + name_len;
369 memcpy(pval, value, value_len);
370 last->e_value_size = cpu_to_le16(value_len);
371 }
372
373 set_page_dirty(page);
374 f2fs_put_page(page, 1);
375
376 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
377 inode->i_mode = fi->i_acl_mode;
378 inode->i_ctime = CURRENT_TIME;
379 clear_inode_flag(fi, FI_ACL_MODE);
380 }
381 f2fs_write_inode(inode, NULL);
382 mutex_unlock_op(sbi, NODE_NEW);
383
384 return 0;
385cleanup:
386 f2fs_put_page(page, 1);
387 mutex_unlock_op(sbi, NODE_NEW);
388 return error;
389}
diff --git a/fs/f2fs/xattr.h b/fs/f2fs/xattr.h
new file mode 100644
index 000000000000..29b0a08e1e14
--- /dev/null
+++ b/fs/f2fs/xattr.h
@@ -0,0 +1,145 @@
1/**
2 * fs/f2fs/xattr.h
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.h
8 *
9 * On-disk format of extended attributes for the ext2 filesystem.
10 *
11 * (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17#ifndef __F2FS_XATTR_H__
18#define __F2FS_XATTR_H__
19
20#include <linux/init.h>
21#include <linux/xattr.h>
22
23/* Magic value in attribute blocks */
24#define F2FS_XATTR_MAGIC 0xF2F52011
25
26/* Maximum number of references to one attribute block */
27#define F2FS_XATTR_REFCOUNT_MAX 1024
28
29/* Name indexes */
30#define F2FS_SYSTEM_ADVISE_PREFIX "system.advise"
31#define F2FS_XATTR_INDEX_USER 1
32#define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS 2
33#define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
34#define F2FS_XATTR_INDEX_TRUSTED 4
35#define F2FS_XATTR_INDEX_LUSTRE 5
36#define F2FS_XATTR_INDEX_SECURITY 6
37#define F2FS_XATTR_INDEX_ADVISE 7
38
39struct f2fs_xattr_header {
40 __le32 h_magic; /* magic number for identification */
41 __le32 h_refcount; /* reference count */
42 __u32 h_reserved[4]; /* zero right now */
43};
44
45struct f2fs_xattr_entry {
46 __u8 e_name_index;
47 __u8 e_name_len;
48 __le16 e_value_size; /* size of attribute value */
49 char e_name[0]; /* attribute name */
50};
51
52#define XATTR_HDR(ptr) ((struct f2fs_xattr_header *)(ptr))
53#define XATTR_ENTRY(ptr) ((struct f2fs_xattr_entry *)(ptr))
54#define XATTR_FIRST_ENTRY(ptr) (XATTR_ENTRY(XATTR_HDR(ptr)+1))
55#define XATTR_ROUND (3)
56
57#define XATTR_ALIGN(size) ((size + XATTR_ROUND) & ~XATTR_ROUND)
58
59#define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
60 entry->e_name_len + le16_to_cpu(entry->e_value_size)))
61
62#define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *)((char *)(entry) +\
63 ENTRY_SIZE(entry)))
64
65#define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
66
67#define list_for_each_xattr(entry, addr) \
68 for (entry = XATTR_FIRST_ENTRY(addr);\
69 !IS_XATTR_LAST_ENTRY(entry);\
70 entry = XATTR_NEXT_ENTRY(entry))
71
72
73#define MIN_OFFSET XATTR_ALIGN(PAGE_SIZE - \
74 sizeof(struct node_footer) - \
75 sizeof(__u32))
76
77#define MAX_VALUE_LEN (MIN_OFFSET - sizeof(struct f2fs_xattr_header) - \
78 sizeof(struct f2fs_xattr_entry))
79
80/**
81 * On-disk structure of f2fs_xattr
82 * We use only 1 block for xattr.
83 *
84 * +--------------------+
85 * | f2fs_xattr_header |
86 * | |
87 * +--------------------+
88 * | f2fs_xattr_entry |
89 * | .e_name_index = 1 |
90 * | .e_name_len = 3 |
91 * | .e_value_size = 14 |
92 * | .e_name = "foo" |
93 * | "value_of_xattr" |<- value_offs = e_name + e_name_len
94 * +--------------------+
95 * | f2fs_xattr_entry |
96 * | .e_name_index = 4 |
97 * | .e_name = "bar" |
98 * +--------------------+
99 * | |
100 * | Free |
101 * | |
102 * +--------------------+<- MIN_OFFSET
103 * | node_footer |
104 * | (nid, ino, offset) |
105 * +--------------------+
106 *
107 **/
108
109#ifdef CONFIG_F2FS_FS_XATTR
110extern const struct xattr_handler f2fs_xattr_user_handler;
111extern const struct xattr_handler f2fs_xattr_trusted_handler;
112extern const struct xattr_handler f2fs_xattr_acl_access_handler;
113extern const struct xattr_handler f2fs_xattr_acl_default_handler;
114extern const struct xattr_handler f2fs_xattr_advise_handler;
115
116extern const struct xattr_handler *f2fs_xattr_handlers[];
117
118extern int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
119 const void *value, size_t value_len);
120extern int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
121 void *buffer, size_t buffer_size);
122extern ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer,
123 size_t buffer_size);
124
125#else
126
127#define f2fs_xattr_handlers NULL
128static inline int f2fs_setxattr(struct inode *inode, int name_index,
129 const char *name, const void *value, size_t value_len)
130{
131 return -EOPNOTSUPP;
132}
133static inline int f2fs_getxattr(struct inode *inode, int name_index,
134 const char *name, void *buffer, size_t buffer_size)
135{
136 return -EOPNOTSUPP;
137}
138static inline ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer,
139 size_t buffer_size)
140{
141 return -EOPNOTSUPP;
142}
143#endif
144
145#endif /* __F2FS_XATTR_H__ */