18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of UBIFS.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2006-2008 Nokia Corporation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём)
88c2ecf20Sopenharmony_ci *          Adrian Hunter
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file implements UBIFS extended attributes support.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Extended attributes are implemented as regular inodes with attached data,
158c2ecf20Sopenharmony_ci * which limits extended attribute size to UBIFS block size (4KiB). Names of
168c2ecf20Sopenharmony_ci * extended attributes are described by extended attribute entries (xentries),
178c2ecf20Sopenharmony_ci * which are almost identical to directory entries, but have different key type.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * In other words, the situation with extended attributes is very similar to
208c2ecf20Sopenharmony_ci * directories. Indeed, any inode (but of course not xattr inodes) may have a
218c2ecf20Sopenharmony_ci * number of associated xentries, just like directory inodes have associated
228c2ecf20Sopenharmony_ci * directory entries. Extended attribute entries store the name of the extended
238c2ecf20Sopenharmony_ci * attribute, the host inode number, and the extended attribute inode number.
248c2ecf20Sopenharmony_ci * Similarly, direntries store the name, the parent and the target inode
258c2ecf20Sopenharmony_ci * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
268c2ecf20Sopenharmony_ci * extended attributes.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * The number of extended attributes is not limited, but there is Linux
298c2ecf20Sopenharmony_ci * limitation on the maximum possible size of the list of all extended
308c2ecf20Sopenharmony_ci * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
318c2ecf20Sopenharmony_ci * the sum of all extended attribute names of the inode does not exceed that
328c2ecf20Sopenharmony_ci * limit.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * Extended attributes are synchronous, which means they are written to the
358c2ecf20Sopenharmony_ci * flash media synchronously and there is no write-back for extended attribute
368c2ecf20Sopenharmony_ci * inodes. The extended attribute values are not stored in compressed form on
378c2ecf20Sopenharmony_ci * the media.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Since extended attributes are represented by regular inodes, they are cached
408c2ecf20Sopenharmony_ci * in the VFS inode cache. The xentries are cached in the LNC cache (see
418c2ecf20Sopenharmony_ci * tnc.c).
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * ACL support is not implemented.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#include "ubifs.h"
478c2ecf20Sopenharmony_ci#include <linux/fs.h>
488c2ecf20Sopenharmony_ci#include <linux/slab.h>
498c2ecf20Sopenharmony_ci#include <linux/xattr.h>
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * Extended attribute type constants.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * USER_XATTR: user extended attribute ("user.*")
558c2ecf20Sopenharmony_ci * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
568c2ecf20Sopenharmony_ci * SECURITY_XATTR: security extended attribute ("security.*")
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_cienum {
598c2ecf20Sopenharmony_ci	USER_XATTR,
608c2ecf20Sopenharmony_ci	TRUSTED_XATTR,
618c2ecf20Sopenharmony_ci	SECURITY_XATTR,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const struct inode_operations empty_iops;
658c2ecf20Sopenharmony_cistatic const struct file_operations empty_fops;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * create_xattr - create an extended attribute.
698c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
708c2ecf20Sopenharmony_ci * @host: host inode
718c2ecf20Sopenharmony_ci * @nm: extended attribute name
728c2ecf20Sopenharmony_ci * @value: extended attribute value
738c2ecf20Sopenharmony_ci * @size: size of extended attribute value
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * This is a helper function which creates an extended attribute of name @nm
768c2ecf20Sopenharmony_ci * and value @value for inode @host. The host inode is also updated on flash
778c2ecf20Sopenharmony_ci * because the ctime and extended attribute accounting data changes. This
788c2ecf20Sopenharmony_ci * function returns zero in case of success and a negative error code in case
798c2ecf20Sopenharmony_ci * of failure.
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_cistatic int create_xattr(struct ubifs_info *c, struct inode *host,
828c2ecf20Sopenharmony_ci			const struct fscrypt_name *nm, const void *value, int size)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	int err, names_len;
858c2ecf20Sopenharmony_ci	struct inode *inode;
868c2ecf20Sopenharmony_ci	struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
878c2ecf20Sopenharmony_ci	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
888c2ecf20Sopenharmony_ci				.new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
898c2ecf20Sopenharmony_ci				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) {
928c2ecf20Sopenharmony_ci		ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
938c2ecf20Sopenharmony_ci			  host->i_ino, host_ui->xattr_cnt);
948c2ecf20Sopenharmony_ci		return -ENOSPC;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Linux limits the maximum size of the extended attribute names list
988c2ecf20Sopenharmony_ci	 * to %XATTR_LIST_MAX. This means we should not allow creating more
998c2ecf20Sopenharmony_ci	 * extended attributes if the name list becomes larger. This limitation
1008c2ecf20Sopenharmony_ci	 * is artificial for UBIFS, though.
1018c2ecf20Sopenharmony_ci	 */
1028c2ecf20Sopenharmony_ci	names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
1038c2ecf20Sopenharmony_ci	if (names_len > XATTR_LIST_MAX) {
1048c2ecf20Sopenharmony_ci		ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
1058c2ecf20Sopenharmony_ci			  host->i_ino, names_len, XATTR_LIST_MAX);
1068c2ecf20Sopenharmony_ci		return -ENOSPC;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	err = ubifs_budget_space(c, &req);
1108c2ecf20Sopenharmony_ci	if (err)
1118c2ecf20Sopenharmony_ci		return err;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
1148c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
1158c2ecf20Sopenharmony_ci		err = PTR_ERR(inode);
1168c2ecf20Sopenharmony_ci		goto out_budg;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* Re-define all operations to be "nothing" */
1208c2ecf20Sopenharmony_ci	inode->i_mapping->a_ops = &empty_aops;
1218c2ecf20Sopenharmony_ci	inode->i_op = &empty_iops;
1228c2ecf20Sopenharmony_ci	inode->i_fop = &empty_fops;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
1258c2ecf20Sopenharmony_ci	ui = ubifs_inode(inode);
1268c2ecf20Sopenharmony_ci	ui->xattr = 1;
1278c2ecf20Sopenharmony_ci	ui->flags |= UBIFS_XATTR_FL;
1288c2ecf20Sopenharmony_ci	ui->data = kmemdup(value, size, GFP_NOFS);
1298c2ecf20Sopenharmony_ci	if (!ui->data) {
1308c2ecf20Sopenharmony_ci		err = -ENOMEM;
1318c2ecf20Sopenharmony_ci		goto out_free;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci	inode->i_size = ui->ui_size = size;
1348c2ecf20Sopenharmony_ci	ui->data_len = size;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	mutex_lock(&host_ui->ui_mutex);
1378c2ecf20Sopenharmony_ci	host->i_ctime = current_time(host);
1388c2ecf20Sopenharmony_ci	host_ui->xattr_cnt += 1;
1398c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
1408c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_XATTR_BYTES(size);
1418c2ecf20Sopenharmony_ci	host_ui->xattr_names += fname_len(nm);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/*
1448c2ecf20Sopenharmony_ci	 * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
1458c2ecf20Sopenharmony_ci	 * have to set the UBIFS_CRYPT_FL flag on the host inode.
1468c2ecf20Sopenharmony_ci	 * To avoid multiple updates of the same inode in the same operation,
1478c2ecf20Sopenharmony_ci	 * let's do it here.
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
1508c2ecf20Sopenharmony_ci		host_ui->flags |= UBIFS_CRYPT_FL;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
1538c2ecf20Sopenharmony_ci	if (err)
1548c2ecf20Sopenharmony_ci		goto out_cancel;
1558c2ecf20Sopenharmony_ci	ubifs_set_inode_flags(host);
1568c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
1598c2ecf20Sopenharmony_ci	insert_inode_hash(inode);
1608c2ecf20Sopenharmony_ci	iput(inode);
1618c2ecf20Sopenharmony_ci	return 0;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciout_cancel:
1648c2ecf20Sopenharmony_ci	host_ui->xattr_cnt -= 1;
1658c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
1668c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
1678c2ecf20Sopenharmony_ci	host_ui->xattr_names -= fname_len(nm);
1688c2ecf20Sopenharmony_ci	host_ui->flags &= ~UBIFS_CRYPT_FL;
1698c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
1708c2ecf20Sopenharmony_ciout_free:
1718c2ecf20Sopenharmony_ci	make_bad_inode(inode);
1728c2ecf20Sopenharmony_ci	iput(inode);
1738c2ecf20Sopenharmony_ciout_budg:
1748c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
1758c2ecf20Sopenharmony_ci	return err;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/**
1798c2ecf20Sopenharmony_ci * change_xattr - change an extended attribute.
1808c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
1818c2ecf20Sopenharmony_ci * @host: host inode
1828c2ecf20Sopenharmony_ci * @inode: extended attribute inode
1838c2ecf20Sopenharmony_ci * @value: extended attribute value
1848c2ecf20Sopenharmony_ci * @size: size of extended attribute value
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci * This helper function changes the value of extended attribute @inode with new
1878c2ecf20Sopenharmony_ci * data from @value. Returns zero in case of success and a negative error code
1888c2ecf20Sopenharmony_ci * in case of failure.
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_cistatic int change_xattr(struct ubifs_info *c, struct inode *host,
1918c2ecf20Sopenharmony_ci			struct inode *inode, const void *value, int size)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	int err;
1948c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(host);
1958c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
1968c2ecf20Sopenharmony_ci	void *buf = NULL;
1978c2ecf20Sopenharmony_ci	int old_size;
1988c2ecf20Sopenharmony_ci	struct ubifs_budget_req req = { .dirtied_ino = 2,
1998c2ecf20Sopenharmony_ci		.dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	ubifs_assert(c, ui->data_len == inode->i_size);
2028c2ecf20Sopenharmony_ci	err = ubifs_budget_space(c, &req);
2038c2ecf20Sopenharmony_ci	if (err)
2048c2ecf20Sopenharmony_ci		return err;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	buf = kmemdup(value, size, GFP_NOFS);
2078c2ecf20Sopenharmony_ci	if (!buf) {
2088c2ecf20Sopenharmony_ci		err = -ENOMEM;
2098c2ecf20Sopenharmony_ci		goto out_free;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	kfree(ui->data);
2128c2ecf20Sopenharmony_ci	ui->data = buf;
2138c2ecf20Sopenharmony_ci	inode->i_size = ui->ui_size = size;
2148c2ecf20Sopenharmony_ci	old_size = ui->data_len;
2158c2ecf20Sopenharmony_ci	ui->data_len = size;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	mutex_lock(&host_ui->ui_mutex);
2188c2ecf20Sopenharmony_ci	host->i_ctime = current_time(host);
2198c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
2208c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_XATTR_BYTES(size);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/*
2238c2ecf20Sopenharmony_ci	 * It is important to write the host inode after the xattr inode
2248c2ecf20Sopenharmony_ci	 * because if the host inode gets synchronized (via 'fsync()'), then
2258c2ecf20Sopenharmony_ci	 * the extended attribute inode gets synchronized, because it goes
2268c2ecf20Sopenharmony_ci	 * before the host inode in the write-buffer.
2278c2ecf20Sopenharmony_ci	 */
2288c2ecf20Sopenharmony_ci	err = ubifs_jnl_change_xattr(c, inode, host);
2298c2ecf20Sopenharmony_ci	if (err)
2308c2ecf20Sopenharmony_ci		goto out_cancel;
2318c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
2348c2ecf20Sopenharmony_ci	return 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciout_cancel:
2378c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
2388c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
2398c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
2408c2ecf20Sopenharmony_ci	make_bad_inode(inode);
2418c2ecf20Sopenharmony_ciout_free:
2428c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
2438c2ecf20Sopenharmony_ci	return err;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct inode *inode;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	inode = ubifs_iget(c->vfs_sb, inum);
2518c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
2528c2ecf20Sopenharmony_ci		ubifs_err(c, "dead extended attribute entry, error %d",
2538c2ecf20Sopenharmony_ci			  (int)PTR_ERR(inode));
2548c2ecf20Sopenharmony_ci		return inode;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	if (ubifs_inode(inode)->xattr)
2578c2ecf20Sopenharmony_ci		return inode;
2588c2ecf20Sopenharmony_ci	ubifs_err(c, "corrupt extended attribute entry");
2598c2ecf20Sopenharmony_ci	iput(inode);
2608c2ecf20Sopenharmony_ci	return ERR_PTR(-EINVAL);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ciint ubifs_xattr_set(struct inode *host, const char *name, const void *value,
2648c2ecf20Sopenharmony_ci		    size_t size, int flags, bool check_lock)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	struct inode *inode;
2678c2ecf20Sopenharmony_ci	struct ubifs_info *c = host->i_sb->s_fs_info;
2688c2ecf20Sopenharmony_ci	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
2698c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent;
2708c2ecf20Sopenharmony_ci	union ubifs_key key;
2718c2ecf20Sopenharmony_ci	int err;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	if (check_lock)
2748c2ecf20Sopenharmony_ci		ubifs_assert(c, inode_is_locked(host));
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (size > UBIFS_MAX_INO_DATA)
2778c2ecf20Sopenharmony_ci		return -ERANGE;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (fname_len(&nm) > UBIFS_MAX_NLEN)
2808c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
2838c2ecf20Sopenharmony_ci	if (!xent)
2848c2ecf20Sopenharmony_ci		return -ENOMEM;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	down_write(&ubifs_inode(host)->xattr_sem);
2878c2ecf20Sopenharmony_ci	/*
2888c2ecf20Sopenharmony_ci	 * The extended attribute entries are stored in LNC, so multiple
2898c2ecf20Sopenharmony_ci	 * look-ups do not involve reading the flash.
2908c2ecf20Sopenharmony_ci	 */
2918c2ecf20Sopenharmony_ci	xent_key_init(c, &key, host->i_ino, &nm);
2928c2ecf20Sopenharmony_ci	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
2938c2ecf20Sopenharmony_ci	if (err) {
2948c2ecf20Sopenharmony_ci		if (err != -ENOENT)
2958c2ecf20Sopenharmony_ci			goto out_free;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		if (flags & XATTR_REPLACE)
2988c2ecf20Sopenharmony_ci			/* We are asked not to create the xattr */
2998c2ecf20Sopenharmony_ci			err = -ENODATA;
3008c2ecf20Sopenharmony_ci		else
3018c2ecf20Sopenharmony_ci			err = create_xattr(c, host, &nm, value, size);
3028c2ecf20Sopenharmony_ci		goto out_free;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (flags & XATTR_CREATE) {
3068c2ecf20Sopenharmony_ci		/* We are asked not to replace the xattr */
3078c2ecf20Sopenharmony_ci		err = -EEXIST;
3088c2ecf20Sopenharmony_ci		goto out_free;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3128c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
3138c2ecf20Sopenharmony_ci		err = PTR_ERR(inode);
3148c2ecf20Sopenharmony_ci		goto out_free;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	err = change_xattr(c, host, inode, value, size);
3188c2ecf20Sopenharmony_ci	iput(inode);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ciout_free:
3218c2ecf20Sopenharmony_ci	up_write(&ubifs_inode(host)->xattr_sem);
3228c2ecf20Sopenharmony_ci	kfree(xent);
3238c2ecf20Sopenharmony_ci	return err;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cissize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
3278c2ecf20Sopenharmony_ci			size_t size)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct inode *inode;
3308c2ecf20Sopenharmony_ci	struct ubifs_info *c = host->i_sb->s_fs_info;
3318c2ecf20Sopenharmony_ci	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
3328c2ecf20Sopenharmony_ci	struct ubifs_inode *ui;
3338c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent;
3348c2ecf20Sopenharmony_ci	union ubifs_key key;
3358c2ecf20Sopenharmony_ci	int err;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (fname_len(&nm) > UBIFS_MAX_NLEN)
3388c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
3418c2ecf20Sopenharmony_ci	if (!xent)
3428c2ecf20Sopenharmony_ci		return -ENOMEM;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	down_read(&ubifs_inode(host)->xattr_sem);
3458c2ecf20Sopenharmony_ci	xent_key_init(c, &key, host->i_ino, &nm);
3468c2ecf20Sopenharmony_ci	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
3478c2ecf20Sopenharmony_ci	if (err) {
3488c2ecf20Sopenharmony_ci		if (err == -ENOENT)
3498c2ecf20Sopenharmony_ci			err = -ENODATA;
3508c2ecf20Sopenharmony_ci		goto out_cleanup;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3548c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
3558c2ecf20Sopenharmony_ci		err = PTR_ERR(inode);
3568c2ecf20Sopenharmony_ci		goto out_cleanup;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	ui = ubifs_inode(inode);
3608c2ecf20Sopenharmony_ci	ubifs_assert(c, inode->i_size == ui->data_len);
3618c2ecf20Sopenharmony_ci	ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (buf) {
3648c2ecf20Sopenharmony_ci		/* If @buf is %NULL we are supposed to return the length */
3658c2ecf20Sopenharmony_ci		if (ui->data_len > size) {
3668c2ecf20Sopenharmony_ci			err = -ERANGE;
3678c2ecf20Sopenharmony_ci			goto out_iput;
3688c2ecf20Sopenharmony_ci		}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci		memcpy(buf, ui->data, ui->data_len);
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci	err = ui->data_len;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ciout_iput:
3758c2ecf20Sopenharmony_ci	iput(inode);
3768c2ecf20Sopenharmony_ciout_cleanup:
3778c2ecf20Sopenharmony_ci	up_read(&ubifs_inode(host)->xattr_sem);
3788c2ecf20Sopenharmony_ci	kfree(xent);
3798c2ecf20Sopenharmony_ci	return err;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic bool xattr_visible(const char *name)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	/* File encryption related xattrs are for internal use only */
3858c2ecf20Sopenharmony_ci	if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
3868c2ecf20Sopenharmony_ci		return false;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* Show trusted namespace only for "power" users */
3898c2ecf20Sopenharmony_ci	if (strncmp(name, XATTR_TRUSTED_PREFIX,
3908c2ecf20Sopenharmony_ci		    XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
3918c2ecf20Sopenharmony_ci		return false;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	return true;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cissize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	union ubifs_key key;
3998c2ecf20Sopenharmony_ci	struct inode *host = d_inode(dentry);
4008c2ecf20Sopenharmony_ci	struct ubifs_info *c = host->i_sb->s_fs_info;
4018c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(host);
4028c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent, *pxent = NULL;
4038c2ecf20Sopenharmony_ci	int err, len, written = 0;
4048c2ecf20Sopenharmony_ci	struct fscrypt_name nm = {0};
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
4078c2ecf20Sopenharmony_ci		dentry, size);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	down_read(&host_ui->xattr_sem);
4108c2ecf20Sopenharmony_ci	len = host_ui->xattr_names + host_ui->xattr_cnt;
4118c2ecf20Sopenharmony_ci	if (!buffer) {
4128c2ecf20Sopenharmony_ci		/*
4138c2ecf20Sopenharmony_ci		 * We should return the minimum buffer size which will fit a
4148c2ecf20Sopenharmony_ci		 * null-terminated list of all the extended attribute names.
4158c2ecf20Sopenharmony_ci		 */
4168c2ecf20Sopenharmony_ci		err = len;
4178c2ecf20Sopenharmony_ci		goto out_err;
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (len > size) {
4218c2ecf20Sopenharmony_ci		err = -ERANGE;
4228c2ecf20Sopenharmony_ci		goto out_err;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	lowest_xent_key(c, &key, host->i_ino);
4268c2ecf20Sopenharmony_ci	while (1) {
4278c2ecf20Sopenharmony_ci		xent = ubifs_tnc_next_ent(c, &key, &nm);
4288c2ecf20Sopenharmony_ci		if (IS_ERR(xent)) {
4298c2ecf20Sopenharmony_ci			err = PTR_ERR(xent);
4308c2ecf20Sopenharmony_ci			break;
4318c2ecf20Sopenharmony_ci		}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci		fname_name(&nm) = xent->name;
4348c2ecf20Sopenharmony_ci		fname_len(&nm) = le16_to_cpu(xent->nlen);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		if (xattr_visible(xent->name)) {
4378c2ecf20Sopenharmony_ci			memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
4388c2ecf20Sopenharmony_ci			written += fname_len(&nm) + 1;
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		kfree(pxent);
4428c2ecf20Sopenharmony_ci		pxent = xent;
4438c2ecf20Sopenharmony_ci		key_read(c, &xent->key, &key);
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci	kfree(pxent);
4468c2ecf20Sopenharmony_ci	up_read(&host_ui->xattr_sem);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (err != -ENOENT) {
4498c2ecf20Sopenharmony_ci		ubifs_err(c, "cannot find next direntry, error %d", err);
4508c2ecf20Sopenharmony_ci		return err;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	ubifs_assert(c, written <= size);
4548c2ecf20Sopenharmony_ci	return written;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ciout_err:
4578c2ecf20Sopenharmony_ci	up_read(&host_ui->xattr_sem);
4588c2ecf20Sopenharmony_ci	return err;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic int remove_xattr(struct ubifs_info *c, struct inode *host,
4628c2ecf20Sopenharmony_ci			struct inode *inode, const struct fscrypt_name *nm)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	int err;
4658c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(host);
4668c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
4678c2ecf20Sopenharmony_ci	struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
4688c2ecf20Sopenharmony_ci				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ubifs_assert(c, ui->data_len == inode->i_size);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	err = ubifs_budget_space(c, &req);
4738c2ecf20Sopenharmony_ci	if (err)
4748c2ecf20Sopenharmony_ci		return err;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	mutex_lock(&host_ui->ui_mutex);
4778c2ecf20Sopenharmony_ci	host->i_ctime = current_time(host);
4788c2ecf20Sopenharmony_ci	host_ui->xattr_cnt -= 1;
4798c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
4808c2ecf20Sopenharmony_ci	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
4818c2ecf20Sopenharmony_ci	host_ui->xattr_names -= fname_len(nm);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
4848c2ecf20Sopenharmony_ci	if (err)
4858c2ecf20Sopenharmony_ci		goto out_cancel;
4868c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
4898c2ecf20Sopenharmony_ci	return 0;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ciout_cancel:
4928c2ecf20Sopenharmony_ci	host_ui->xattr_cnt += 1;
4938c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
4948c2ecf20Sopenharmony_ci	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
4958c2ecf20Sopenharmony_ci	host_ui->xattr_names += fname_len(nm);
4968c2ecf20Sopenharmony_ci	mutex_unlock(&host_ui->ui_mutex);
4978c2ecf20Sopenharmony_ci	ubifs_release_budget(c, &req);
4988c2ecf20Sopenharmony_ci	make_bad_inode(inode);
4998c2ecf20Sopenharmony_ci	return err;
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciint ubifs_purge_xattrs(struct inode *host)
5038c2ecf20Sopenharmony_ci{
5048c2ecf20Sopenharmony_ci	union ubifs_key key;
5058c2ecf20Sopenharmony_ci	struct ubifs_info *c = host->i_sb->s_fs_info;
5068c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent, *pxent = NULL;
5078c2ecf20Sopenharmony_ci	struct inode *xino;
5088c2ecf20Sopenharmony_ci	struct fscrypt_name nm = {0};
5098c2ecf20Sopenharmony_ci	int err;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c))
5128c2ecf20Sopenharmony_ci		return 0;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion",
5158c2ecf20Sopenharmony_ci		   host->i_ino);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	down_write(&ubifs_inode(host)->xattr_sem);
5188c2ecf20Sopenharmony_ci	lowest_xent_key(c, &key, host->i_ino);
5198c2ecf20Sopenharmony_ci	while (1) {
5208c2ecf20Sopenharmony_ci		xent = ubifs_tnc_next_ent(c, &key, &nm);
5218c2ecf20Sopenharmony_ci		if (IS_ERR(xent)) {
5228c2ecf20Sopenharmony_ci			err = PTR_ERR(xent);
5238c2ecf20Sopenharmony_ci			break;
5248c2ecf20Sopenharmony_ci		}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		fname_name(&nm) = xent->name;
5278c2ecf20Sopenharmony_ci		fname_len(&nm) = le16_to_cpu(xent->nlen);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
5308c2ecf20Sopenharmony_ci		if (IS_ERR(xino)) {
5318c2ecf20Sopenharmony_ci			err = PTR_ERR(xino);
5328c2ecf20Sopenharmony_ci			ubifs_err(c, "dead directory entry '%s', error %d",
5338c2ecf20Sopenharmony_ci				  xent->name, err);
5348c2ecf20Sopenharmony_ci			ubifs_ro_mode(c, err);
5358c2ecf20Sopenharmony_ci			kfree(pxent);
5368c2ecf20Sopenharmony_ci			kfree(xent);
5378c2ecf20Sopenharmony_ci			goto out_err;
5388c2ecf20Sopenharmony_ci		}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		ubifs_assert(c, ubifs_inode(xino)->xattr);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci		clear_nlink(xino);
5438c2ecf20Sopenharmony_ci		err = remove_xattr(c, host, xino, &nm);
5448c2ecf20Sopenharmony_ci		if (err) {
5458c2ecf20Sopenharmony_ci			kfree(pxent);
5468c2ecf20Sopenharmony_ci			kfree(xent);
5478c2ecf20Sopenharmony_ci			iput(xino);
5488c2ecf20Sopenharmony_ci			ubifs_err(c, "cannot remove xattr, error %d", err);
5498c2ecf20Sopenharmony_ci			goto out_err;
5508c2ecf20Sopenharmony_ci		}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		iput(xino);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		kfree(pxent);
5558c2ecf20Sopenharmony_ci		pxent = xent;
5568c2ecf20Sopenharmony_ci		key_read(c, &xent->key, &key);
5578c2ecf20Sopenharmony_ci	}
5588c2ecf20Sopenharmony_ci	kfree(pxent);
5598c2ecf20Sopenharmony_ci	up_write(&ubifs_inode(host)->xattr_sem);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (err != -ENOENT) {
5628c2ecf20Sopenharmony_ci		ubifs_err(c, "cannot find next direntry, error %d", err);
5638c2ecf20Sopenharmony_ci		return err;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ciout_err:
5698c2ecf20Sopenharmony_ci	up_write(&ubifs_inode(host)->xattr_sem);
5708c2ecf20Sopenharmony_ci	return err;
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/**
5748c2ecf20Sopenharmony_ci * ubifs_evict_xattr_inode - Evict an xattr inode.
5758c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
5768c2ecf20Sopenharmony_ci * @xattr_inum: xattr inode number
5778c2ecf20Sopenharmony_ci *
5788c2ecf20Sopenharmony_ci * When an inode that hosts xattrs is being removed we have to make sure
5798c2ecf20Sopenharmony_ci * that cached inodes of the xattrs also get removed from the inode cache
5808c2ecf20Sopenharmony_ci * otherwise we'd waste memory. This function looks up an inode from the
5818c2ecf20Sopenharmony_ci * inode cache and clears the link counter such that iput() will evict
5828c2ecf20Sopenharmony_ci * the inode.
5838c2ecf20Sopenharmony_ci */
5848c2ecf20Sopenharmony_civoid ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct inode *inode;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	inode = ilookup(c->vfs_sb, xattr_inum);
5898c2ecf20Sopenharmony_ci	if (inode) {
5908c2ecf20Sopenharmony_ci		clear_nlink(inode);
5918c2ecf20Sopenharmony_ci		iput(inode);
5928c2ecf20Sopenharmony_ci	}
5938c2ecf20Sopenharmony_ci}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic int ubifs_xattr_remove(struct inode *host, const char *name)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	struct inode *inode;
5988c2ecf20Sopenharmony_ci	struct ubifs_info *c = host->i_sb->s_fs_info;
5998c2ecf20Sopenharmony_ci	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
6008c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent;
6018c2ecf20Sopenharmony_ci	union ubifs_key key;
6028c2ecf20Sopenharmony_ci	int err;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	ubifs_assert(c, inode_is_locked(host));
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (fname_len(&nm) > UBIFS_MAX_NLEN)
6078c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
6108c2ecf20Sopenharmony_ci	if (!xent)
6118c2ecf20Sopenharmony_ci		return -ENOMEM;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	down_write(&ubifs_inode(host)->xattr_sem);
6148c2ecf20Sopenharmony_ci	xent_key_init(c, &key, host->i_ino, &nm);
6158c2ecf20Sopenharmony_ci	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
6168c2ecf20Sopenharmony_ci	if (err) {
6178c2ecf20Sopenharmony_ci		if (err == -ENOENT)
6188c2ecf20Sopenharmony_ci			err = -ENODATA;
6198c2ecf20Sopenharmony_ci		goto out_free;
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	inode = iget_xattr(c, le64_to_cpu(xent->inum));
6238c2ecf20Sopenharmony_ci	if (IS_ERR(inode)) {
6248c2ecf20Sopenharmony_ci		err = PTR_ERR(inode);
6258c2ecf20Sopenharmony_ci		goto out_free;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	ubifs_assert(c, inode->i_nlink == 1);
6298c2ecf20Sopenharmony_ci	clear_nlink(inode);
6308c2ecf20Sopenharmony_ci	err = remove_xattr(c, host, inode, &nm);
6318c2ecf20Sopenharmony_ci	if (err)
6328c2ecf20Sopenharmony_ci		set_nlink(inode, 1);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	/* If @i_nlink is 0, 'iput()' will delete the inode */
6358c2ecf20Sopenharmony_ci	iput(inode);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ciout_free:
6388c2ecf20Sopenharmony_ci	up_write(&ubifs_inode(host)->xattr_sem);
6398c2ecf20Sopenharmony_ci	kfree(xent);
6408c2ecf20Sopenharmony_ci	return err;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci#ifdef CONFIG_UBIFS_FS_SECURITY
6448c2ecf20Sopenharmony_cistatic int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
6458c2ecf20Sopenharmony_ci		      void *fs_info)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	const struct xattr *xattr;
6488c2ecf20Sopenharmony_ci	char *name;
6498c2ecf20Sopenharmony_ci	int err = 0;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
6528c2ecf20Sopenharmony_ci		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
6538c2ecf20Sopenharmony_ci			       strlen(xattr->name) + 1, GFP_NOFS);
6548c2ecf20Sopenharmony_ci		if (!name) {
6558c2ecf20Sopenharmony_ci			err = -ENOMEM;
6568c2ecf20Sopenharmony_ci			break;
6578c2ecf20Sopenharmony_ci		}
6588c2ecf20Sopenharmony_ci		strcpy(name, XATTR_SECURITY_PREFIX);
6598c2ecf20Sopenharmony_ci		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
6608c2ecf20Sopenharmony_ci		/*
6618c2ecf20Sopenharmony_ci		 * creating a new inode without holding the inode rwsem,
6628c2ecf20Sopenharmony_ci		 * no need to check whether inode is locked.
6638c2ecf20Sopenharmony_ci		 */
6648c2ecf20Sopenharmony_ci		err = ubifs_xattr_set(inode, name, xattr->value,
6658c2ecf20Sopenharmony_ci				      xattr->value_len, 0, false);
6668c2ecf20Sopenharmony_ci		kfree(name);
6678c2ecf20Sopenharmony_ci		if (err < 0)
6688c2ecf20Sopenharmony_ci			break;
6698c2ecf20Sopenharmony_ci	}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	return err;
6728c2ecf20Sopenharmony_ci}
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ciint ubifs_init_security(struct inode *dentry, struct inode *inode,
6758c2ecf20Sopenharmony_ci			const struct qstr *qstr)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	int err;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	err = security_inode_init_security(inode, dentry, qstr,
6808c2ecf20Sopenharmony_ci					   &init_xattrs, 0);
6818c2ecf20Sopenharmony_ci	if (err) {
6828c2ecf20Sopenharmony_ci		struct ubifs_info *c = dentry->i_sb->s_fs_info;
6838c2ecf20Sopenharmony_ci		ubifs_err(c, "cannot initialize security for inode %lu, error %d",
6848c2ecf20Sopenharmony_ci			  inode->i_ino, err);
6858c2ecf20Sopenharmony_ci	}
6868c2ecf20Sopenharmony_ci	return err;
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci#endif
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic int xattr_get(const struct xattr_handler *handler,
6918c2ecf20Sopenharmony_ci			   struct dentry *dentry, struct inode *inode,
6928c2ecf20Sopenharmony_ci			   const char *name, void *buffer, size_t size)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
6958c2ecf20Sopenharmony_ci		inode->i_ino, dentry, size);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	name = xattr_full_name(handler, name);
6988c2ecf20Sopenharmony_ci	return ubifs_xattr_get(inode, name, buffer, size);
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic int xattr_set(const struct xattr_handler *handler,
7028c2ecf20Sopenharmony_ci			   struct dentry *dentry, struct inode *inode,
7038c2ecf20Sopenharmony_ci			   const char *name, const void *value,
7048c2ecf20Sopenharmony_ci			   size_t size, int flags)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
7078c2ecf20Sopenharmony_ci		name, inode->i_ino, dentry, size);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	name = xattr_full_name(handler, name);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	if (value)
7128c2ecf20Sopenharmony_ci		return ubifs_xattr_set(inode, name, value, size, flags, true);
7138c2ecf20Sopenharmony_ci	else
7148c2ecf20Sopenharmony_ci		return ubifs_xattr_remove(inode, name);
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic const struct xattr_handler ubifs_user_xattr_handler = {
7188c2ecf20Sopenharmony_ci	.prefix = XATTR_USER_PREFIX,
7198c2ecf20Sopenharmony_ci	.get = xattr_get,
7208c2ecf20Sopenharmony_ci	.set = xattr_set,
7218c2ecf20Sopenharmony_ci};
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic const struct xattr_handler ubifs_trusted_xattr_handler = {
7248c2ecf20Sopenharmony_ci	.prefix = XATTR_TRUSTED_PREFIX,
7258c2ecf20Sopenharmony_ci	.get = xattr_get,
7268c2ecf20Sopenharmony_ci	.set = xattr_set,
7278c2ecf20Sopenharmony_ci};
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci#ifdef CONFIG_UBIFS_FS_SECURITY
7308c2ecf20Sopenharmony_cistatic const struct xattr_handler ubifs_security_xattr_handler = {
7318c2ecf20Sopenharmony_ci	.prefix = XATTR_SECURITY_PREFIX,
7328c2ecf20Sopenharmony_ci	.get = xattr_get,
7338c2ecf20Sopenharmony_ci	.set = xattr_set,
7348c2ecf20Sopenharmony_ci};
7358c2ecf20Sopenharmony_ci#endif
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ciconst struct xattr_handler *ubifs_xattr_handlers[] = {
7388c2ecf20Sopenharmony_ci	&ubifs_user_xattr_handler,
7398c2ecf20Sopenharmony_ci	&ubifs_trusted_xattr_handler,
7408c2ecf20Sopenharmony_ci#ifdef CONFIG_UBIFS_FS_SECURITY
7418c2ecf20Sopenharmony_ci	&ubifs_security_xattr_handler,
7428c2ecf20Sopenharmony_ci#endif
7438c2ecf20Sopenharmony_ci	NULL
7448c2ecf20Sopenharmony_ci};
745