xref: /openbmc/linux/fs/ubifs/xattr.c (revision c78c7e35a4709b55d3126624662c8f6d7e3d1a5e)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
201e51764aSArtem Bityutskiy  *          Adrian Hunter
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy /*
241e51764aSArtem Bityutskiy  * This file implements UBIFS extended attributes support.
251e51764aSArtem Bityutskiy  *
261e51764aSArtem Bityutskiy  * Extended attributes are implemented as regular inodes with attached data,
271e51764aSArtem Bityutskiy  * which limits extended attribute size to UBIFS block size (4KiB). Names of
281e51764aSArtem Bityutskiy  * extended attributes are described by extended attribute entries (xentries),
291e51764aSArtem Bityutskiy  * which are almost identical to directory entries, but have different key type.
301e51764aSArtem Bityutskiy  *
311e51764aSArtem Bityutskiy  * In other words, the situation with extended attributes is very similar to
321e51764aSArtem Bityutskiy  * directories. Indeed, any inode (but of course not xattr inodes) may have a
331e51764aSArtem Bityutskiy  * number of associated xentries, just like directory inodes have associated
341e51764aSArtem Bityutskiy  * directory entries. Extended attribute entries store the name of the extended
351e51764aSArtem Bityutskiy  * attribute, the host inode number, and the extended attribute inode number.
361e51764aSArtem Bityutskiy  * Similarly, direntries store the name, the parent and the target inode
371e51764aSArtem Bityutskiy  * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
381e51764aSArtem Bityutskiy  * extended attributes.
391e51764aSArtem Bityutskiy  *
401e51764aSArtem Bityutskiy  * The number of extended attributes is not limited, but there is Linux
411e51764aSArtem Bityutskiy  * limitation on the maximum possible size of the list of all extended
421e51764aSArtem Bityutskiy  * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
431e51764aSArtem Bityutskiy  * the sum of all extended attribute names of the inode does not exceed that
441e51764aSArtem Bityutskiy  * limit.
451e51764aSArtem Bityutskiy  *
461e51764aSArtem Bityutskiy  * Extended attributes are synchronous, which means they are written to the
471e51764aSArtem Bityutskiy  * flash media synchronously and there is no write-back for extended attribute
481e51764aSArtem Bityutskiy  * inodes. The extended attribute values are not stored in compressed form on
491e51764aSArtem Bityutskiy  * the media.
501e51764aSArtem Bityutskiy  *
511e51764aSArtem Bityutskiy  * Since extended attributes are represented by regular inodes, they are cached
521e51764aSArtem Bityutskiy  * in the VFS inode cache. The xentries are cached in the LNC cache (see
531e51764aSArtem Bityutskiy  * tnc.c).
541e51764aSArtem Bityutskiy  *
551e51764aSArtem Bityutskiy  * ACL support is not implemented.
561e51764aSArtem Bityutskiy  */
571e51764aSArtem Bityutskiy 
581e51764aSArtem Bityutskiy #include <linux/xattr.h>
591e51764aSArtem Bityutskiy #include <linux/posix_acl_xattr.h>
601e51764aSArtem Bityutskiy #include "ubifs.h"
611e51764aSArtem Bityutskiy 
621e51764aSArtem Bityutskiy /*
631e51764aSArtem Bityutskiy  * Limit the number of extended attributes per inode so that the total size
64*c78c7e35SArtem Bityutskiy  * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
651e51764aSArtem Bityutskiy  */
661e51764aSArtem Bityutskiy #define MAX_XATTRS_PER_INODE 65535
671e51764aSArtem Bityutskiy 
681e51764aSArtem Bityutskiy /*
691e51764aSArtem Bityutskiy  * Extended attribute type constants.
701e51764aSArtem Bityutskiy  *
711e51764aSArtem Bityutskiy  * USER_XATTR: user extended attribute ("user.*")
721e51764aSArtem Bityutskiy  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
731e51764aSArtem Bityutskiy  * SECURITY_XATTR: security extended attribute ("security.*")
741e51764aSArtem Bityutskiy  */
751e51764aSArtem Bityutskiy enum {
761e51764aSArtem Bityutskiy 	USER_XATTR,
771e51764aSArtem Bityutskiy 	TRUSTED_XATTR,
781e51764aSArtem Bityutskiy 	SECURITY_XATTR,
791e51764aSArtem Bityutskiy };
801e51764aSArtem Bityutskiy 
811e51764aSArtem Bityutskiy static struct inode_operations none_inode_operations;
821e51764aSArtem Bityutskiy static struct address_space_operations none_address_operations;
831e51764aSArtem Bityutskiy static struct file_operations none_file_operations;
841e51764aSArtem Bityutskiy 
851e51764aSArtem Bityutskiy /**
861e51764aSArtem Bityutskiy  * create_xattr - create an extended attribute.
871e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
881e51764aSArtem Bityutskiy  * @host: host inode
891e51764aSArtem Bityutskiy  * @nm: extended attribute name
901e51764aSArtem Bityutskiy  * @value: extended attribute value
911e51764aSArtem Bityutskiy  * @size: size of extended attribute value
921e51764aSArtem Bityutskiy  *
931e51764aSArtem Bityutskiy  * This is a helper function which creates an extended attribute of name @nm
941e51764aSArtem Bityutskiy  * and value @value for inode @host. The host inode is also updated on flash
951e51764aSArtem Bityutskiy  * because the ctime and extended attribute accounting data changes. This
961e51764aSArtem Bityutskiy  * function returns zero in case of success and a negative error code in case
971e51764aSArtem Bityutskiy  * of failure.
981e51764aSArtem Bityutskiy  */
991e51764aSArtem Bityutskiy static int create_xattr(struct ubifs_info *c, struct inode *host,
1001e51764aSArtem Bityutskiy 			const struct qstr *nm, const void *value, int size)
1011e51764aSArtem Bityutskiy {
1021e51764aSArtem Bityutskiy 	int err;
1031e51764aSArtem Bityutskiy 	struct inode *inode;
1041e51764aSArtem Bityutskiy 	struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
1051e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1065acd6ff8SZoltan Sogor 				.new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
107dab4b4d2SArtem Bityutskiy 				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
1081e51764aSArtem Bityutskiy 
1091e51764aSArtem Bityutskiy 	if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
1101e51764aSArtem Bityutskiy 		return -ENOSPC;
1111e51764aSArtem Bityutskiy 	/*
1121e51764aSArtem Bityutskiy 	 * Linux limits the maximum size of the extended attribute names list
113*c78c7e35SArtem Bityutskiy 	 * to %XATTR_LIST_MAX. This means we should not allow creating more
1141e51764aSArtem Bityutskiy 	 * extended attributes if the name list becomes larger. This limitation
1151e51764aSArtem Bityutskiy 	 * is artificial for UBIFS, though.
1161e51764aSArtem Bityutskiy 	 */
1171e51764aSArtem Bityutskiy 	if (host_ui->xattr_names + host_ui->xattr_cnt +
1181e51764aSArtem Bityutskiy 					nm->len + 1 > XATTR_LIST_MAX)
1191e51764aSArtem Bityutskiy 		return -ENOSPC;
1201e51764aSArtem Bityutskiy 
1211e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
1221e51764aSArtem Bityutskiy 	if (err)
1231e51764aSArtem Bityutskiy 		return err;
1241e51764aSArtem Bityutskiy 
1251e51764aSArtem Bityutskiy 	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
1261e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
1271e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
1281e51764aSArtem Bityutskiy 		goto out_budg;
1291e51764aSArtem Bityutskiy 	}
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	/* Re-define all operations to be "nothing" */
1321e51764aSArtem Bityutskiy 	inode->i_mapping->a_ops = &none_address_operations;
1331e51764aSArtem Bityutskiy 	inode->i_op = &none_inode_operations;
1341e51764aSArtem Bityutskiy 	inode->i_fop = &none_file_operations;
1351e51764aSArtem Bityutskiy 
1361e51764aSArtem Bityutskiy 	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
1371e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
1381e51764aSArtem Bityutskiy 	ui->xattr = 1;
1391e51764aSArtem Bityutskiy 	ui->flags |= UBIFS_XATTR_FL;
1401e51764aSArtem Bityutskiy 	ui->data = kmalloc(size, GFP_NOFS);
1411e51764aSArtem Bityutskiy 	if (!ui->data) {
1421e51764aSArtem Bityutskiy 		err = -ENOMEM;
143*c78c7e35SArtem Bityutskiy 		goto out_free;
1441e51764aSArtem Bityutskiy 	}
1451e51764aSArtem Bityutskiy 	memcpy(ui->data, value, size);
146*c78c7e35SArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
147*c78c7e35SArtem Bityutskiy 	ui->data_len = size;
148*c78c7e35SArtem Bityutskiy 
149*c78c7e35SArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
1501e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
1511e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
1521e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
1531e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
1541e51764aSArtem Bityutskiy 	host_ui->xattr_names += nm->len;
1551e51764aSArtem Bityutskiy 
1561e51764aSArtem Bityutskiy 	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
1571e51764aSArtem Bityutskiy 	if (err)
1581e51764aSArtem Bityutskiy 		goto out_cancel;
1591e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
1601e51764aSArtem Bityutskiy 
1611e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
1621e51764aSArtem Bityutskiy 	insert_inode_hash(inode);
1631e51764aSArtem Bityutskiy 	iput(inode);
1641e51764aSArtem Bityutskiy 	return 0;
1651e51764aSArtem Bityutskiy 
1661e51764aSArtem Bityutskiy out_cancel:
1671e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
1681e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
1691e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
1701e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
171*c78c7e35SArtem Bityutskiy out_free:
1721e51764aSArtem Bityutskiy 	make_bad_inode(inode);
1731e51764aSArtem Bityutskiy 	iput(inode);
1741e51764aSArtem Bityutskiy out_budg:
1751e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
1761e51764aSArtem Bityutskiy 	return err;
1771e51764aSArtem Bityutskiy }
1781e51764aSArtem Bityutskiy 
1791e51764aSArtem Bityutskiy /**
1801e51764aSArtem Bityutskiy  * change_xattr - change an extended attribute.
1811e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1821e51764aSArtem Bityutskiy  * @host: host inode
1831e51764aSArtem Bityutskiy  * @inode: extended attribute inode
1841e51764aSArtem Bityutskiy  * @value: extended attribute value
1851e51764aSArtem Bityutskiy  * @size: size of extended attribute value
1861e51764aSArtem Bityutskiy  *
1871e51764aSArtem Bityutskiy  * This helper function changes the value of extended attribute @inode with new
1881e51764aSArtem Bityutskiy  * data from @value. Returns zero in case of success and a negative error code
1891e51764aSArtem Bityutskiy  * in case of failure.
1901e51764aSArtem Bityutskiy  */
1911e51764aSArtem Bityutskiy static int change_xattr(struct ubifs_info *c, struct inode *host,
1921e51764aSArtem Bityutskiy 			struct inode *inode, const void *value, int size)
1931e51764aSArtem Bityutskiy {
1941e51764aSArtem Bityutskiy 	int err;
1951e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
1961e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
1971e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .dirtied_ino = 2,
1985acd6ff8SZoltan Sogor 		.dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
1991e51764aSArtem Bityutskiy 
2001e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
2011e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
2021e51764aSArtem Bityutskiy 	if (err)
2031e51764aSArtem Bityutskiy 		return err;
2041e51764aSArtem Bityutskiy 
2051e51764aSArtem Bityutskiy 	kfree(ui->data);
2061e51764aSArtem Bityutskiy 	ui->data = kmalloc(size, GFP_NOFS);
2071e51764aSArtem Bityutskiy 	if (!ui->data) {
2081e51764aSArtem Bityutskiy 		err = -ENOMEM;
209*c78c7e35SArtem Bityutskiy 		goto out_free;
2101e51764aSArtem Bityutskiy 	}
2111e51764aSArtem Bityutskiy 	memcpy(ui->data, value, size);
2121e51764aSArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
2131e51764aSArtem Bityutskiy 	ui->data_len = size;
2141e51764aSArtem Bityutskiy 
215*c78c7e35SArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
216*c78c7e35SArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
217*c78c7e35SArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
218*c78c7e35SArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
219*c78c7e35SArtem Bityutskiy 
2201e51764aSArtem Bityutskiy 	/*
2211e51764aSArtem Bityutskiy 	 * It is important to write the host inode after the xattr inode
2221e51764aSArtem Bityutskiy 	 * because if the host inode gets synchronized (via 'fsync()'), then
2231e51764aSArtem Bityutskiy 	 * the extended attribute inode gets synchronized, because it goes
2241e51764aSArtem Bityutskiy 	 * before the host inode in the write-buffer.
2251e51764aSArtem Bityutskiy 	 */
2261e51764aSArtem Bityutskiy 	err = ubifs_jnl_change_xattr(c, inode, host);
2271e51764aSArtem Bityutskiy 	if (err)
2281e51764aSArtem Bityutskiy 		goto out_cancel;
2291e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
2301e51764aSArtem Bityutskiy 
2311e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
2321e51764aSArtem Bityutskiy 	return 0;
2331e51764aSArtem Bityutskiy 
2341e51764aSArtem Bityutskiy out_cancel:
2351e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
2361e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
2371e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
238*c78c7e35SArtem Bityutskiy 	make_bad_inode(inode);
239*c78c7e35SArtem Bityutskiy out_free:
2401e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
2411e51764aSArtem Bityutskiy 	return err;
2421e51764aSArtem Bityutskiy }
2431e51764aSArtem Bityutskiy 
2441e51764aSArtem Bityutskiy /**
2451e51764aSArtem Bityutskiy  * check_namespace - check extended attribute name-space.
2461e51764aSArtem Bityutskiy  * @nm: extended attribute name
2471e51764aSArtem Bityutskiy  *
2481e51764aSArtem Bityutskiy  * This function makes sure the extended attribute name belongs to one of the
2491e51764aSArtem Bityutskiy  * supported extended attribute name-spaces. Returns name-space index in case
2501e51764aSArtem Bityutskiy  * of success and a negative error code in case of failure.
2511e51764aSArtem Bityutskiy  */
2521e51764aSArtem Bityutskiy static int check_namespace(const struct qstr *nm)
2531e51764aSArtem Bityutskiy {
2541e51764aSArtem Bityutskiy 	int type;
2551e51764aSArtem Bityutskiy 
2561e51764aSArtem Bityutskiy 	if (nm->len > UBIFS_MAX_NLEN)
2571e51764aSArtem Bityutskiy 		return -ENAMETOOLONG;
2581e51764aSArtem Bityutskiy 
2591e51764aSArtem Bityutskiy 	if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
2601e51764aSArtem Bityutskiy 		     XATTR_TRUSTED_PREFIX_LEN)) {
2611e51764aSArtem Bityutskiy 		if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
2621e51764aSArtem Bityutskiy 			return -EINVAL;
2631e51764aSArtem Bityutskiy 		type = TRUSTED_XATTR;
2641e51764aSArtem Bityutskiy 	} else if (!strncmp(nm->name, XATTR_USER_PREFIX,
2651e51764aSArtem Bityutskiy 				      XATTR_USER_PREFIX_LEN)) {
2661e51764aSArtem Bityutskiy 		if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
2671e51764aSArtem Bityutskiy 			return -EINVAL;
2681e51764aSArtem Bityutskiy 		type = USER_XATTR;
2691e51764aSArtem Bityutskiy 	} else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
2701e51764aSArtem Bityutskiy 				     XATTR_SECURITY_PREFIX_LEN)) {
2711e51764aSArtem Bityutskiy 		if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
2721e51764aSArtem Bityutskiy 			return -EINVAL;
2731e51764aSArtem Bityutskiy 		type = SECURITY_XATTR;
2741e51764aSArtem Bityutskiy 	} else
2751e51764aSArtem Bityutskiy 		return -EOPNOTSUPP;
2761e51764aSArtem Bityutskiy 
2771e51764aSArtem Bityutskiy 	return type;
2781e51764aSArtem Bityutskiy }
2791e51764aSArtem Bityutskiy 
2801e51764aSArtem Bityutskiy static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
2811e51764aSArtem Bityutskiy {
2821e51764aSArtem Bityutskiy 	struct inode *inode;
2831e51764aSArtem Bityutskiy 
2841e51764aSArtem Bityutskiy 	inode = ubifs_iget(c->vfs_sb, inum);
2851e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
2861e51764aSArtem Bityutskiy 		ubifs_err("dead extended attribute entry, error %d",
2871e51764aSArtem Bityutskiy 			  (int)PTR_ERR(inode));
2881e51764aSArtem Bityutskiy 		return inode;
2891e51764aSArtem Bityutskiy 	}
2901e51764aSArtem Bityutskiy 	if (ubifs_inode(inode)->xattr)
2911e51764aSArtem Bityutskiy 		return inode;
2921e51764aSArtem Bityutskiy 	ubifs_err("corrupt extended attribute entry");
2931e51764aSArtem Bityutskiy 	iput(inode);
2941e51764aSArtem Bityutskiy 	return ERR_PTR(-EINVAL);
2951e51764aSArtem Bityutskiy }
2961e51764aSArtem Bityutskiy 
2971e51764aSArtem Bityutskiy int ubifs_setxattr(struct dentry *dentry, const char *name,
2981e51764aSArtem Bityutskiy 		   const void *value, size_t size, int flags)
2991e51764aSArtem Bityutskiy {
3001e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
3011e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
3021e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
3031e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
3041e51764aSArtem Bityutskiy 	union ubifs_key key;
3051e51764aSArtem Bityutskiy 	int err, type;
3061e51764aSArtem Bityutskiy 
3071e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
3081e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
309*c78c7e35SArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&host->i_mutex));
3101e51764aSArtem Bityutskiy 
3111e51764aSArtem Bityutskiy 	if (size > UBIFS_MAX_INO_DATA)
3121e51764aSArtem Bityutskiy 		return -ERANGE;
3131e51764aSArtem Bityutskiy 
3141e51764aSArtem Bityutskiy 	type = check_namespace(&nm);
3151e51764aSArtem Bityutskiy 	if (type < 0)
3161e51764aSArtem Bityutskiy 		return type;
3171e51764aSArtem Bityutskiy 
3181e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
3191e51764aSArtem Bityutskiy 	if (!xent)
3201e51764aSArtem Bityutskiy 		return -ENOMEM;
3211e51764aSArtem Bityutskiy 
3221e51764aSArtem Bityutskiy 	/*
3231e51764aSArtem Bityutskiy 	 * The extended attribute entries are stored in LNC, so multiple
3241e51764aSArtem Bityutskiy 	 * look-ups do not involve reading the flash.
3251e51764aSArtem Bityutskiy 	 */
3261e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
3271e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
3281e51764aSArtem Bityutskiy 	if (err) {
3291e51764aSArtem Bityutskiy 		if (err != -ENOENT)
3301e51764aSArtem Bityutskiy 			goto out_free;
3311e51764aSArtem Bityutskiy 
3321e51764aSArtem Bityutskiy 		if (flags & XATTR_REPLACE)
3331e51764aSArtem Bityutskiy 			/* We are asked not to create the xattr */
3341e51764aSArtem Bityutskiy 			err = -ENODATA;
3351e51764aSArtem Bityutskiy 		else
3361e51764aSArtem Bityutskiy 			err = create_xattr(c, host, &nm, value, size);
3371e51764aSArtem Bityutskiy 		goto out_free;
3381e51764aSArtem Bityutskiy 	}
3391e51764aSArtem Bityutskiy 
3401e51764aSArtem Bityutskiy 	if (flags & XATTR_CREATE) {
3411e51764aSArtem Bityutskiy 		/* We are asked not to replace the xattr */
3421e51764aSArtem Bityutskiy 		err = -EEXIST;
3431e51764aSArtem Bityutskiy 		goto out_free;
3441e51764aSArtem Bityutskiy 	}
3451e51764aSArtem Bityutskiy 
3461e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3471e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
3481e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
3491e51764aSArtem Bityutskiy 		goto out_free;
3501e51764aSArtem Bityutskiy 	}
3511e51764aSArtem Bityutskiy 
3521e51764aSArtem Bityutskiy 	err = change_xattr(c, host, inode, value, size);
3531e51764aSArtem Bityutskiy 	iput(inode);
3541e51764aSArtem Bityutskiy 
3551e51764aSArtem Bityutskiy out_free:
3561e51764aSArtem Bityutskiy 	kfree(xent);
3571e51764aSArtem Bityutskiy 	return err;
3581e51764aSArtem Bityutskiy }
3591e51764aSArtem Bityutskiy 
3601e51764aSArtem Bityutskiy ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
3611e51764aSArtem Bityutskiy 		       size_t size)
3621e51764aSArtem Bityutskiy {
3631e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
3641e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
3651e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
3661e51764aSArtem Bityutskiy 	struct ubifs_inode *ui;
3671e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
3681e51764aSArtem Bityutskiy 	union ubifs_key key;
3691e51764aSArtem Bityutskiy 	int err;
3701e51764aSArtem Bityutskiy 
3711e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
3721e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
3731e51764aSArtem Bityutskiy 
3741e51764aSArtem Bityutskiy 	err = check_namespace(&nm);
3751e51764aSArtem Bityutskiy 	if (err < 0)
3761e51764aSArtem Bityutskiy 		return err;
3771e51764aSArtem Bityutskiy 
3781e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
3791e51764aSArtem Bityutskiy 	if (!xent)
3801e51764aSArtem Bityutskiy 		return -ENOMEM;
3811e51764aSArtem Bityutskiy 
3821e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
3831e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
3841e51764aSArtem Bityutskiy 	if (err) {
3851e51764aSArtem Bityutskiy 		if (err == -ENOENT)
3861e51764aSArtem Bityutskiy 			err = -ENODATA;
3871e51764aSArtem Bityutskiy 		goto out_unlock;
3881e51764aSArtem Bityutskiy 	}
3891e51764aSArtem Bityutskiy 
3901e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3911e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
3921e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
3931e51764aSArtem Bityutskiy 		goto out_unlock;
3941e51764aSArtem Bityutskiy 	}
3951e51764aSArtem Bityutskiy 
3961e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
3971e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_size == ui->data_len);
3981e51764aSArtem Bityutskiy 	ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
3991e51764aSArtem Bityutskiy 
4001e51764aSArtem Bityutskiy 	if (buf) {
4011e51764aSArtem Bityutskiy 		/* If @buf is %NULL we are supposed to return the length */
4021e51764aSArtem Bityutskiy 		if (ui->data_len > size) {
4031e51764aSArtem Bityutskiy 			dbg_err("buffer size %zd, xattr len %d",
4041e51764aSArtem Bityutskiy 				size, ui->data_len);
4051e51764aSArtem Bityutskiy 			err = -ERANGE;
4061e51764aSArtem Bityutskiy 			goto out_iput;
4071e51764aSArtem Bityutskiy 		}
4081e51764aSArtem Bityutskiy 
4091e51764aSArtem Bityutskiy 		memcpy(buf, ui->data, ui->data_len);
4101e51764aSArtem Bityutskiy 	}
4111e51764aSArtem Bityutskiy 	err = ui->data_len;
4121e51764aSArtem Bityutskiy 
4131e51764aSArtem Bityutskiy out_iput:
4141e51764aSArtem Bityutskiy 	iput(inode);
4151e51764aSArtem Bityutskiy out_unlock:
4161e51764aSArtem Bityutskiy 	kfree(xent);
4171e51764aSArtem Bityutskiy 	return err;
4181e51764aSArtem Bityutskiy }
4191e51764aSArtem Bityutskiy 
4201e51764aSArtem Bityutskiy ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
4211e51764aSArtem Bityutskiy {
4221e51764aSArtem Bityutskiy 	union ubifs_key key;
4231e51764aSArtem Bityutskiy 	struct inode *host = dentry->d_inode;
4241e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
4251e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
4261e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent, *pxent = NULL;
4271e51764aSArtem Bityutskiy 	int err, len, written = 0;
4281e51764aSArtem Bityutskiy 	struct qstr nm = { .name = NULL };
4291e51764aSArtem Bityutskiy 
4301e51764aSArtem Bityutskiy 	dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
4311e51764aSArtem Bityutskiy 		dentry->d_name.len, dentry->d_name.name, size);
4321e51764aSArtem Bityutskiy 
4331e51764aSArtem Bityutskiy 	len = host_ui->xattr_names + host_ui->xattr_cnt;
4341e51764aSArtem Bityutskiy 	if (!buffer)
4351e51764aSArtem Bityutskiy 		/*
4361e51764aSArtem Bityutskiy 		 * We should return the minimum buffer size which will fit a
4371e51764aSArtem Bityutskiy 		 * null-terminated list of all the extended attribute names.
4381e51764aSArtem Bityutskiy 		 */
4391e51764aSArtem Bityutskiy 		return len;
4401e51764aSArtem Bityutskiy 
4411e51764aSArtem Bityutskiy 	if (len > size)
4421e51764aSArtem Bityutskiy 		return -ERANGE;
4431e51764aSArtem Bityutskiy 
4441e51764aSArtem Bityutskiy 	lowest_xent_key(c, &key, host->i_ino);
4451e51764aSArtem Bityutskiy 	while (1) {
4461e51764aSArtem Bityutskiy 		int type;
4471e51764aSArtem Bityutskiy 
4481e51764aSArtem Bityutskiy 		xent = ubifs_tnc_next_ent(c, &key, &nm);
4491e51764aSArtem Bityutskiy 		if (unlikely(IS_ERR(xent))) {
4501e51764aSArtem Bityutskiy 			err = PTR_ERR(xent);
4511e51764aSArtem Bityutskiy 			break;
4521e51764aSArtem Bityutskiy 		}
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy 		nm.name = xent->name;
4551e51764aSArtem Bityutskiy 		nm.len = le16_to_cpu(xent->nlen);
4561e51764aSArtem Bityutskiy 
4571e51764aSArtem Bityutskiy 		type = check_namespace(&nm);
4581e51764aSArtem Bityutskiy 		if (unlikely(type < 0)) {
4591e51764aSArtem Bityutskiy 			err = type;
4601e51764aSArtem Bityutskiy 			break;
4611e51764aSArtem Bityutskiy 		}
4621e51764aSArtem Bityutskiy 
4631e51764aSArtem Bityutskiy 		/* Show trusted namespace only for "power" users */
4641e51764aSArtem Bityutskiy 		if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
4651e51764aSArtem Bityutskiy 			memcpy(buffer + written, nm.name, nm.len + 1);
4661e51764aSArtem Bityutskiy 			written += nm.len + 1;
4671e51764aSArtem Bityutskiy 		}
4681e51764aSArtem Bityutskiy 
4691e51764aSArtem Bityutskiy 		kfree(pxent);
4701e51764aSArtem Bityutskiy 		pxent = xent;
4711e51764aSArtem Bityutskiy 		key_read(c, &xent->key, &key);
4721e51764aSArtem Bityutskiy 	}
4731e51764aSArtem Bityutskiy 
4741e51764aSArtem Bityutskiy 	kfree(pxent);
4751e51764aSArtem Bityutskiy 	if (err != -ENOENT) {
4761e51764aSArtem Bityutskiy 		ubifs_err("cannot find next direntry, error %d", err);
4771e51764aSArtem Bityutskiy 		return err;
4781e51764aSArtem Bityutskiy 	}
4791e51764aSArtem Bityutskiy 
4801e51764aSArtem Bityutskiy 	ubifs_assert(written <= size);
4811e51764aSArtem Bityutskiy 	return written;
4821e51764aSArtem Bityutskiy }
4831e51764aSArtem Bityutskiy 
4841e51764aSArtem Bityutskiy static int remove_xattr(struct ubifs_info *c, struct inode *host,
4851e51764aSArtem Bityutskiy 			struct inode *inode, const struct qstr *nm)
4861e51764aSArtem Bityutskiy {
4871e51764aSArtem Bityutskiy 	int err;
4881e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
4891e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
4905acd6ff8SZoltan Sogor 	struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
4915acd6ff8SZoltan Sogor 				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
4921e51764aSArtem Bityutskiy 
4931e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
4941e51764aSArtem Bityutskiy 
4951e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
4961e51764aSArtem Bityutskiy 	if (err)
4971e51764aSArtem Bityutskiy 		return err;
4981e51764aSArtem Bityutskiy 
4991e51764aSArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
5001e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
5011e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
5021e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
5031e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
5041e51764aSArtem Bityutskiy 	host_ui->xattr_names -= nm->len;
5051e51764aSArtem Bityutskiy 
5061e51764aSArtem Bityutskiy 	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
5071e51764aSArtem Bityutskiy 	if (err)
5081e51764aSArtem Bityutskiy 		goto out_cancel;
5091e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
5101e51764aSArtem Bityutskiy 
5111e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
5121e51764aSArtem Bityutskiy 	return 0;
5131e51764aSArtem Bityutskiy 
5141e51764aSArtem Bityutskiy out_cancel:
5151e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
5161e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
5171e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
5181e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
5191e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
5201e51764aSArtem Bityutskiy 	make_bad_inode(inode);
5211e51764aSArtem Bityutskiy 	return err;
5221e51764aSArtem Bityutskiy }
5231e51764aSArtem Bityutskiy 
5241e51764aSArtem Bityutskiy int ubifs_removexattr(struct dentry *dentry, const char *name)
5251e51764aSArtem Bityutskiy {
5261e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
5271e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
5281e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
5291e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
5301e51764aSArtem Bityutskiy 	union ubifs_key key;
5311e51764aSArtem Bityutskiy 	int err;
5321e51764aSArtem Bityutskiy 
5331e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
5341e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name);
5351e51764aSArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&host->i_mutex));
5361e51764aSArtem Bityutskiy 
5371e51764aSArtem Bityutskiy 	err = check_namespace(&nm);
5381e51764aSArtem Bityutskiy 	if (err < 0)
5391e51764aSArtem Bityutskiy 		return err;
5401e51764aSArtem Bityutskiy 
5411e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
5421e51764aSArtem Bityutskiy 	if (!xent)
5431e51764aSArtem Bityutskiy 		return -ENOMEM;
5441e51764aSArtem Bityutskiy 
5451e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
5461e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
5471e51764aSArtem Bityutskiy 	if (err) {
5481e51764aSArtem Bityutskiy 		if (err == -ENOENT)
5491e51764aSArtem Bityutskiy 			err = -ENODATA;
5501e51764aSArtem Bityutskiy 		goto out_free;
5511e51764aSArtem Bityutskiy 	}
5521e51764aSArtem Bityutskiy 
5531e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
5541e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
5551e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
5561e51764aSArtem Bityutskiy 		goto out_free;
5571e51764aSArtem Bityutskiy 	}
5581e51764aSArtem Bityutskiy 
5591e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_nlink == 1);
5601e51764aSArtem Bityutskiy 	inode->i_nlink = 0;
5611e51764aSArtem Bityutskiy 	err = remove_xattr(c, host, inode, &nm);
5621e51764aSArtem Bityutskiy 	if (err)
5631e51764aSArtem Bityutskiy 		inode->i_nlink = 1;
5641e51764aSArtem Bityutskiy 
5651e51764aSArtem Bityutskiy 	/* If @i_nlink is 0, 'iput()' will delete the inode */
5661e51764aSArtem Bityutskiy 	iput(inode);
5671e51764aSArtem Bityutskiy 
5681e51764aSArtem Bityutskiy out_free:
5691e51764aSArtem Bityutskiy 	kfree(xent);
5701e51764aSArtem Bityutskiy 	return err;
5711e51764aSArtem Bityutskiy }
572