Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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 <linux/security.h>
 24#include <linux/posix_acl_xattr.h>
 25#include "f2fs.h"
 26#include "xattr.h"
 27
 28static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
 29		struct dentry *unused, struct inode *inode,
 30		const char *name, void *buffer, size_t size)
 31{
 32	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
 33
 34	switch (handler->flags) {
 35	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;
 43	case F2FS_XATTR_INDEX_SECURITY:
 44		break;
 45	default:
 46		return -EINVAL;
 47	}
 48	return f2fs_getxattr(inode, handler->flags, name,
 49			     buffer, size, NULL);
 50}
 51
 52static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
 53		struct dentry *unused, struct inode *inode,
 54		const char *name, const void *value,
 55		size_t size, int flags)
 56{
 57	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
 58
 59	switch (handler->flags) {
 60	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;
 68	case F2FS_XATTR_INDEX_SECURITY:
 69		break;
 70	default:
 71		return -EINVAL;
 72	}
 73	return f2fs_setxattr(inode, handler->flags, name,
 74					value, size, NULL, flags);
 75}
 76
 77static bool f2fs_xattr_user_list(struct dentry *dentry)
 78{
 79	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
 80
 81	return test_opt(sbi, XATTR_USER);
 82}
 83
 84static bool f2fs_xattr_trusted_list(struct dentry *dentry)
 85{
 86	return capable(CAP_SYS_ADMIN);
 87}
 88
 89static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
 90		struct dentry *unused, struct inode *inode,
 91		const char *name, void *buffer, size_t size)
 92{
 93	if (buffer)
 94		*((char *)buffer) = F2FS_I(inode)->i_advise;
 95	return sizeof(char);
 96}
 97
 98static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
 99		struct dentry *unused, struct inode *inode,
100		const char *name, const void *value,
101		size_t size, int flags)
102{
103	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;
109	f2fs_mark_inode_dirty_sync(inode, true);
110	return 0;
111}
112
113#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++) {
121		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
122				xattr->name, xattr->value,
123				xattr->value_len, (struct page *)page, 0);
124		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
138const struct xattr_handler f2fs_xattr_user_handler = {
139	.prefix	= XATTR_USER_PREFIX,
140	.flags	= F2FS_XATTR_INDEX_USER,
141	.list	= f2fs_xattr_user_list,
142	.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,
149	.list	= f2fs_xattr_trusted_list,
150	.get	= f2fs_xattr_generic_get,
151	.set	= f2fs_xattr_generic_set,
152};
153
154const struct xattr_handler f2fs_xattr_advise_handler = {
155	.name	= F2FS_SYSTEM_ADVISE_NAME,
156	.flags	= F2FS_XATTR_INDEX_ADVISE,
157	.get    = f2fs_xattr_advise_get,
158	.set    = f2fs_xattr_advise_set,
159};
160
161const struct xattr_handler f2fs_xattr_security_handler = {
162	.prefix	= XATTR_SECURITY_PREFIX,
163	.flags	= F2FS_XATTR_INDEX_SECURITY,
164	.get	= f2fs_xattr_generic_get,
165	.set	= f2fs_xattr_generic_set,
166};
167
168static const struct xattr_handler *f2fs_xattr_handler_map[] = {
169	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
170#ifdef CONFIG_F2FS_FS_POSIX_ACL
171	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
172	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
173#endif
174	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
175#ifdef CONFIG_F2FS_FS_SECURITY
176	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
177#endif
178	[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
184	&posix_acl_access_xattr_handler,
185	&posix_acl_default_xattr_handler,
186#endif
187	&f2fs_xattr_trusted_handler,
188#ifdef CONFIG_F2FS_FS_SECURITY
189	&f2fs_xattr_security_handler,
190#endif
191	&f2fs_xattr_advise_handler,
192	NULL,
193};
194
195static inline const struct xattr_handler *f2fs_xattr_handler(int index)
196{
197	const struct xattr_handler *handler = NULL;
198
199	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
200		handler = f2fs_xattr_handler_map[index];
201	return handler;
202}
203
204static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
205					size_t len, const char *name)
206{
207	struct f2fs_xattr_entry *entry;
208
209	list_for_each_xattr(entry, base_addr) {
210		if (entry->e_name_index != index)
211			continue;
212		if (entry->e_name_len != len)
213			continue;
214		if (!memcmp(entry->e_name, name, len))
215			break;
216	}
217	return entry;
218}
219
220static int read_all_xattrs(struct inode *inode, struct page *ipage,
221							void **base_addr)
222{
223	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
224	struct f2fs_xattr_header *header;
225	size_t size = PAGE_SIZE, inline_size = 0;
226	void *txattr_addr;
227	int err;
228
229	inline_size = inline_xattr_size(inode);
230
231	txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
232	if (!txattr_addr)
233		return -ENOMEM;
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				err = PTR_ERR(page);
246				goto fail;
247			}
248			inline_addr = inline_xattr_addr(page);
249		}
250		memcpy(txattr_addr, inline_addr, inline_size);
251		f2fs_put_page(page, 1);
252	}
253
254	/* read from xattr node block */
255	if (F2FS_I(inode)->i_xattr_nid) {
256		struct page *xpage;
257		void *xattr_addr;
258
259		/* The inode already has an extended attribute block. */
260		xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
261		if (IS_ERR(xpage)) {
262			err = PTR_ERR(xpage);
263			goto fail;
264		}
265
266		xattr_addr = page_address(xpage);
267		memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
268		f2fs_put_page(xpage, 1);
269	}
270
271	header = XATTR_HDR(txattr_addr);
272
273	/* never been allocated xattrs */
274	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
275		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
276		header->h_refcount = cpu_to_le32(1);
277	}
278	*base_addr = txattr_addr;
279	return 0;
280fail:
281	kzfree(txattr_addr);
282	return err;
283}
284
285static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
286				void *txattr_addr, struct page *ipage)
287{
288	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
289	size_t inline_size = 0;
290	void *xattr_addr;
291	struct page *xpage;
292	nid_t new_nid = 0;
293	int err;
294
295	inline_size = inline_xattr_size(inode);
296
297	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
298		if (!alloc_nid(sbi, &new_nid))
299			return -ENOSPC;
300
301	/* write to inline xattr */
302	if (inline_size) {
303		struct page *page = NULL;
304		void *inline_addr;
305
306		if (ipage) {
307			inline_addr = inline_xattr_addr(ipage);
308			f2fs_wait_on_page_writeback(ipage, NODE, true);
309			set_page_dirty(ipage);
310		} else {
311			page = get_node_page(sbi, inode->i_ino);
312			if (IS_ERR(page)) {
313				alloc_nid_failed(sbi, new_nid);
314				return PTR_ERR(page);
315			}
316			inline_addr = inline_xattr_addr(page);
317			f2fs_wait_on_page_writeback(page, NODE, true);
318		}
319		memcpy(inline_addr, txattr_addr, inline_size);
320		f2fs_put_page(page, 1);
321
322		/* no need to use xattr node block */
323		if (hsize <= inline_size) {
324			err = truncate_xattr_node(inode, ipage);
325			alloc_nid_failed(sbi, new_nid);
326			return err;
327		}
328	}
329
330	/* write to xattr node block */
331	if (F2FS_I(inode)->i_xattr_nid) {
332		xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
333		if (IS_ERR(xpage)) {
334			alloc_nid_failed(sbi, new_nid);
335			return PTR_ERR(xpage);
336		}
337		f2fs_bug_on(sbi, new_nid);
338		f2fs_wait_on_page_writeback(xpage, NODE, true);
339	} else {
340		struct dnode_of_data dn;
341		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
342		xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
343		if (IS_ERR(xpage)) {
344			alloc_nid_failed(sbi, new_nid);
345			return PTR_ERR(xpage);
346		}
347		alloc_nid_done(sbi, new_nid);
348	}
349
350	xattr_addr = page_address(xpage);
351	memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
352						sizeof(struct node_footer));
353	set_page_dirty(xpage);
354	f2fs_put_page(xpage, 1);
355
356	/* need to checkpoint during fsync */
357	F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
358	return 0;
359}
360
361int f2fs_getxattr(struct inode *inode, int index, const char *name,
362		void *buffer, size_t buffer_size, struct page *ipage)
363{
364	struct f2fs_xattr_entry *entry;
365	void *base_addr;
366	int error = 0;
367	size_t size, len;
368
369	if (name == NULL)
370		return -EINVAL;
371
372	len = strlen(name);
373	if (len > F2FS_NAME_LEN)
374		return -ERANGE;
375
376	error = read_all_xattrs(inode, ipage, &base_addr);
377	if (error)
378		return error;
379
380	entry = __find_xattr(base_addr, index, len, name);
381	if (IS_XATTR_LAST_ENTRY(entry)) {
382		error = -ENODATA;
383		goto cleanup;
384	}
385
386	size = le16_to_cpu(entry->e_value_size);
387
388	if (buffer && size > buffer_size) {
389		error = -ERANGE;
390		goto cleanup;
391	}
392
393	if (buffer) {
394		char *pval = entry->e_name + entry->e_name_len;
395		memcpy(buffer, pval, size);
396	}
397	error = size;
398
399cleanup:
400	kzfree(base_addr);
401	return error;
402}
403
404ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
405{
406	struct inode *inode = d_inode(dentry);
407	struct f2fs_xattr_entry *entry;
408	void *base_addr;
409	int error = 0;
410	size_t rest = buffer_size;
411
412	error = read_all_xattrs(inode, NULL, &base_addr);
413	if (error)
414		return error;
415
416	list_for_each_xattr(entry, base_addr) {
417		const struct xattr_handler *handler =
418			f2fs_xattr_handler(entry->e_name_index);
419		const char *prefix;
420		size_t prefix_len;
421		size_t size;
422
423		if (!handler || (handler->list && !handler->list(dentry)))
424			continue;
425
426		prefix = handler->prefix ?: handler->name;
427		prefix_len = strlen(prefix);
428		size = prefix_len + entry->e_name_len + 1;
429		if (buffer) {
430			if (size > rest) {
431				error = -ERANGE;
432				goto cleanup;
433			}
434			memcpy(buffer, prefix, prefix_len);
435			buffer += prefix_len;
436			memcpy(buffer, entry->e_name, entry->e_name_len);
437			buffer += entry->e_name_len;
438			*buffer++ = 0;
439		}
440		rest -= size;
441	}
442	error = buffer_size - rest;
443cleanup:
444	kzfree(base_addr);
445	return error;
446}
447
448static int __f2fs_setxattr(struct inode *inode, int index,
449			const char *name, const void *value, size_t size,
450			struct page *ipage, int flags)
451{
452	struct f2fs_xattr_entry *here, *last;
453	void *base_addr;
454	int found, newsize;
455	size_t len;
456	__u32 new_hsize;
457	int error = 0;
458
459	if (name == NULL)
460		return -EINVAL;
461
462	if (value == NULL)
463		size = 0;
464
465	len = strlen(name);
466
467	if (len > F2FS_NAME_LEN)
468		return -ERANGE;
469
470	if (size > MAX_VALUE_LEN(inode))
471		return -E2BIG;
472
473	error = read_all_xattrs(inode, ipage, &base_addr);
474	if (error)
475		return error;
476
477	/* find entry with wanted name. */
478	here = __find_xattr(base_addr, index, len, name);
479
480	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
481
482	if ((flags & XATTR_REPLACE) && !found) {
483		error = -ENODATA;
484		goto exit;
485	} else if ((flags & XATTR_CREATE) && found) {
486		error = -EEXIST;
487		goto exit;
488	}
489
490	last = here;
491	while (!IS_XATTR_LAST_ENTRY(last))
492		last = XATTR_NEXT_ENTRY(last);
493
494	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
495
496	/* 1. Check space */
497	if (value) {
498		int free;
499		/*
500		 * If value is NULL, it is remove operation.
501		 * In case of update operation, we calculate free.
502		 */
503		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
504		if (found)
505			free = free + ENTRY_SIZE(here);
506
507		if (unlikely(free < newsize)) {
508			error = -E2BIG;
509			goto exit;
510		}
511	}
512
513	/* 2. Remove old entry */
514	if (found) {
515		/*
516		 * If entry is found, remove old entry.
517		 * If not found, remove operation is not needed.
518		 */
519		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
520		int oldsize = ENTRY_SIZE(here);
521
522		memmove(here, next, (char *)last - (char *)next);
523		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
524		memset(last, 0, oldsize);
525	}
526
527	new_hsize = (char *)last - (char *)base_addr;
528
529	/* 3. Write new entry */
530	if (value) {
531		char *pval;
532		/*
533		 * Before we come here, old entry is removed.
534		 * We just write new entry.
535		 */
536		last->e_name_index = index;
537		last->e_name_len = len;
538		memcpy(last->e_name, name, len);
539		pval = last->e_name + len;
540		memcpy(pval, value, size);
541		last->e_value_size = cpu_to_le16(size);
542		new_hsize += newsize;
543	}
544
545	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
546	if (error)
547		goto exit;
548
549	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
550		inode->i_mode = F2FS_I(inode)->i_acl_mode;
551		inode->i_ctime = current_time(inode);
552		clear_inode_flag(inode, FI_ACL_MODE);
553	}
554	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
555			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
556		f2fs_set_encrypted_inode(inode);
557	f2fs_mark_inode_dirty_sync(inode, true);
558	if (!error && S_ISDIR(inode->i_mode))
559		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
560exit:
561	kzfree(base_addr);
562	return error;
563}
564
565int f2fs_setxattr(struct inode *inode, int index, const char *name,
566				const void *value, size_t size,
567				struct page *ipage, int flags)
568{
569	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
570	int err;
571
572	/* this case is only from init_inode_metadata */
573	if (ipage)
574		return __f2fs_setxattr(inode, index, name, value,
575						size, ipage, flags);
576	f2fs_balance_fs(sbi, true);
577
578	f2fs_lock_op(sbi);
579	/* protect xattr_ver */
580	down_write(&F2FS_I(inode)->i_sem);
581	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
582	up_write(&F2FS_I(inode)->i_sem);
583	f2fs_unlock_op(sbi);
584
585	f2fs_update_time(sbi, REQ_TIME);
586	return err;
587}