xref: /openbmc/linux/fs/ubifs/xattr.c (revision 5930122683dff58f0846b0f0405b4bd598a3ba6a)
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 
58073aaa1bSAl Viro #include "ubifs.h"
597dcda1c9SJens Axboe #include <linux/fs.h>
605a0e3ad6STejun Heo #include <linux/slab.h>
611e51764aSArtem Bityutskiy #include <linux/xattr.h>
621e51764aSArtem Bityutskiy 
631e51764aSArtem Bityutskiy /*
641e51764aSArtem Bityutskiy  * Limit the number of extended attributes per inode so that the total size
65c78c7e35SArtem Bityutskiy  * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
661e51764aSArtem Bityutskiy  */
671e51764aSArtem Bityutskiy #define MAX_XATTRS_PER_INODE 65535
681e51764aSArtem Bityutskiy 
691e51764aSArtem Bityutskiy /*
701e51764aSArtem Bityutskiy  * Extended attribute type constants.
711e51764aSArtem Bityutskiy  *
721e51764aSArtem Bityutskiy  * USER_XATTR: user extended attribute ("user.*")
731e51764aSArtem Bityutskiy  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
741e51764aSArtem Bityutskiy  * SECURITY_XATTR: security extended attribute ("security.*")
751e51764aSArtem Bityutskiy  */
761e51764aSArtem Bityutskiy enum {
771e51764aSArtem Bityutskiy 	USER_XATTR,
781e51764aSArtem Bityutskiy 	TRUSTED_XATTR,
791e51764aSArtem Bityutskiy 	SECURITY_XATTR,
801e51764aSArtem Bityutskiy };
811e51764aSArtem Bityutskiy 
8214ffd5d0SSedat Dilek static const struct inode_operations empty_iops;
8314ffd5d0SSedat Dilek static const struct file_operations empty_fops;
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 {
102fee1756dSSubodh Nijsure 	int err, names_len;
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 
109fee1756dSSubodh Nijsure 	if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
110235c362bSSheng Yong 		ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
111fee1756dSSubodh Nijsure 			  host->i_ino, host_ui->xattr_cnt);
1121e51764aSArtem Bityutskiy 		return -ENOSPC;
113fee1756dSSubodh Nijsure 	}
1141e51764aSArtem Bityutskiy 	/*
1151e51764aSArtem Bityutskiy 	 * Linux limits the maximum size of the extended attribute names list
116c78c7e35SArtem Bityutskiy 	 * to %XATTR_LIST_MAX. This means we should not allow creating more
1171e51764aSArtem Bityutskiy 	 * extended attributes if the name list becomes larger. This limitation
1181e51764aSArtem Bityutskiy 	 * is artificial for UBIFS, though.
1191e51764aSArtem Bityutskiy 	 */
120fee1756dSSubodh Nijsure 	names_len = host_ui->xattr_names + host_ui->xattr_cnt + nm->len + 1;
121fee1756dSSubodh Nijsure 	if (names_len > XATTR_LIST_MAX) {
122235c362bSSheng Yong 		ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
123fee1756dSSubodh Nijsure 			  host->i_ino, names_len, XATTR_LIST_MAX);
1241e51764aSArtem Bityutskiy 		return -ENOSPC;
125fee1756dSSubodh Nijsure 	}
1261e51764aSArtem Bityutskiy 
1271e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
1281e51764aSArtem Bityutskiy 	if (err)
1291e51764aSArtem Bityutskiy 		return err;
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
1321e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
1331e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
1341e51764aSArtem Bityutskiy 		goto out_budg;
1351e51764aSArtem Bityutskiy 	}
1361e51764aSArtem Bityutskiy 
1371e51764aSArtem Bityutskiy 	/* Re-define all operations to be "nothing" */
1387dcda1c9SJens Axboe 	inode->i_mapping->a_ops = &empty_aops;
13914ffd5d0SSedat Dilek 	inode->i_op = &empty_iops;
14014ffd5d0SSedat Dilek 	inode->i_fop = &empty_fops;
1411e51764aSArtem Bityutskiy 
1421e51764aSArtem Bityutskiy 	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
1431e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
1441e51764aSArtem Bityutskiy 	ui->xattr = 1;
1451e51764aSArtem Bityutskiy 	ui->flags |= UBIFS_XATTR_FL;
146eaecf43aSThomas Meyer 	ui->data = kmemdup(value, size, GFP_NOFS);
1471e51764aSArtem Bityutskiy 	if (!ui->data) {
1481e51764aSArtem Bityutskiy 		err = -ENOMEM;
149c78c7e35SArtem Bityutskiy 		goto out_free;
1501e51764aSArtem Bityutskiy 	}
151c78c7e35SArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
152c78c7e35SArtem Bityutskiy 	ui->data_len = size;
153c78c7e35SArtem Bityutskiy 
154c78c7e35SArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
1551e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
1561e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
1571e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
1581e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
1591e51764aSArtem Bityutskiy 	host_ui->xattr_names += nm->len;
1601e51764aSArtem Bityutskiy 
1611e51764aSArtem Bityutskiy 	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
1621e51764aSArtem Bityutskiy 	if (err)
1631e51764aSArtem Bityutskiy 		goto out_cancel;
1641e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
1651e51764aSArtem Bityutskiy 
1661e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
1671e51764aSArtem Bityutskiy 	insert_inode_hash(inode);
1681e51764aSArtem Bityutskiy 	iput(inode);
1691e51764aSArtem Bityutskiy 	return 0;
1701e51764aSArtem Bityutskiy 
1711e51764aSArtem Bityutskiy out_cancel:
1721e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
1731e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
1741e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
1751e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
176c78c7e35SArtem Bityutskiy out_free:
1771e51764aSArtem Bityutskiy 	make_bad_inode(inode);
1781e51764aSArtem Bityutskiy 	iput(inode);
1791e51764aSArtem Bityutskiy out_budg:
1801e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
1811e51764aSArtem Bityutskiy 	return err;
1821e51764aSArtem Bityutskiy }
1831e51764aSArtem Bityutskiy 
1841e51764aSArtem Bityutskiy /**
1851e51764aSArtem Bityutskiy  * change_xattr - change an extended attribute.
1861e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1871e51764aSArtem Bityutskiy  * @host: host inode
1881e51764aSArtem Bityutskiy  * @inode: extended attribute inode
1891e51764aSArtem Bityutskiy  * @value: extended attribute value
1901e51764aSArtem Bityutskiy  * @size: size of extended attribute value
1911e51764aSArtem Bityutskiy  *
1921e51764aSArtem Bityutskiy  * This helper function changes the value of extended attribute @inode with new
1931e51764aSArtem Bityutskiy  * data from @value. Returns zero in case of success and a negative error code
1941e51764aSArtem Bityutskiy  * in case of failure.
1951e51764aSArtem Bityutskiy  */
1961e51764aSArtem Bityutskiy static int change_xattr(struct ubifs_info *c, struct inode *host,
1971e51764aSArtem Bityutskiy 			struct inode *inode, const void *value, int size)
1981e51764aSArtem Bityutskiy {
1991e51764aSArtem Bityutskiy 	int err;
2001e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
2011e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
202ab92a20bSDongsheng Yang 	void *buf = NULL;
2031e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .dirtied_ino = 2,
2045acd6ff8SZoltan Sogor 		.dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
2051e51764aSArtem Bityutskiy 
2061e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
2071e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
2081e51764aSArtem Bityutskiy 	if (err)
2091e51764aSArtem Bityutskiy 		return err;
2101e51764aSArtem Bityutskiy 
211ab92a20bSDongsheng Yang 	buf = kmemdup(value, size, GFP_NOFS);
212ab92a20bSDongsheng Yang 	if (!buf) {
2131e51764aSArtem Bityutskiy 		err = -ENOMEM;
214c78c7e35SArtem Bityutskiy 		goto out_free;
2151e51764aSArtem Bityutskiy 	}
216ab92a20bSDongsheng Yang 	mutex_lock(&ui->ui_mutex);
217ab92a20bSDongsheng Yang 	kfree(ui->data);
218ab92a20bSDongsheng Yang 	ui->data = buf;
2191e51764aSArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
2201e51764aSArtem Bityutskiy 	ui->data_len = size;
221ab92a20bSDongsheng Yang 	mutex_unlock(&ui->ui_mutex);
2221e51764aSArtem Bityutskiy 
223c78c7e35SArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
224c78c7e35SArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
225c78c7e35SArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
226c78c7e35SArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
227c78c7e35SArtem Bityutskiy 
2281e51764aSArtem Bityutskiy 	/*
2291e51764aSArtem Bityutskiy 	 * It is important to write the host inode after the xattr inode
2301e51764aSArtem Bityutskiy 	 * because if the host inode gets synchronized (via 'fsync()'), then
2311e51764aSArtem Bityutskiy 	 * the extended attribute inode gets synchronized, because it goes
2321e51764aSArtem Bityutskiy 	 * before the host inode in the write-buffer.
2331e51764aSArtem Bityutskiy 	 */
2341e51764aSArtem Bityutskiy 	err = ubifs_jnl_change_xattr(c, inode, host);
2351e51764aSArtem Bityutskiy 	if (err)
2361e51764aSArtem Bityutskiy 		goto out_cancel;
2371e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
2381e51764aSArtem Bityutskiy 
2391e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
2401e51764aSArtem Bityutskiy 	return 0;
2411e51764aSArtem Bityutskiy 
2421e51764aSArtem Bityutskiy out_cancel:
2431e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
2441e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
2451e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
246c78c7e35SArtem Bityutskiy 	make_bad_inode(inode);
247c78c7e35SArtem Bityutskiy out_free:
2481e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
2491e51764aSArtem Bityutskiy 	return err;
2501e51764aSArtem Bityutskiy }
2511e51764aSArtem Bityutskiy 
2521e51764aSArtem Bityutskiy static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
2531e51764aSArtem Bityutskiy {
2541e51764aSArtem Bityutskiy 	struct inode *inode;
2551e51764aSArtem Bityutskiy 
2561e51764aSArtem Bityutskiy 	inode = ubifs_iget(c->vfs_sb, inum);
2571e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
258235c362bSSheng Yong 		ubifs_err(c, "dead extended attribute entry, error %d",
2591e51764aSArtem Bityutskiy 			  (int)PTR_ERR(inode));
2601e51764aSArtem Bityutskiy 		return inode;
2611e51764aSArtem Bityutskiy 	}
2621e51764aSArtem Bityutskiy 	if (ubifs_inode(inode)->xattr)
2631e51764aSArtem Bityutskiy 		return inode;
264235c362bSSheng Yong 	ubifs_err(c, "corrupt extended attribute entry");
2651e51764aSArtem Bityutskiy 	iput(inode);
2661e51764aSArtem Bityutskiy 	return ERR_PTR(-EINVAL);
2671e51764aSArtem Bityutskiy }
2681e51764aSArtem Bityutskiy 
2692b88fc21SAndreas Gruenbacher static int __ubifs_setxattr(struct inode *host, const char *name,
2702b88fc21SAndreas Gruenbacher 			    const void *value, size_t size, int flags)
2711e51764aSArtem Bityutskiy {
272895d9db2SSubodh Nijsure 	struct inode *inode;
2731e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
27426fe5750SLinus Torvalds 	struct qstr nm = QSTR_INIT(name, strlen(name));
2751e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
2761e51764aSArtem Bityutskiy 	union ubifs_key key;
2772b88fc21SAndreas Gruenbacher 	int err;
2781e51764aSArtem Bityutskiy 
2795955102cSAl Viro 	ubifs_assert(inode_is_locked(host));
2801e51764aSArtem Bityutskiy 
2811e51764aSArtem Bityutskiy 	if (size > UBIFS_MAX_INO_DATA)
2821e51764aSArtem Bityutskiy 		return -ERANGE;
2831e51764aSArtem Bityutskiy 
2842b88fc21SAndreas Gruenbacher 	if (nm.len > UBIFS_MAX_NLEN)
2852b88fc21SAndreas Gruenbacher 		return -ENAMETOOLONG;
2861e51764aSArtem Bityutskiy 
2871e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
2881e51764aSArtem Bityutskiy 	if (!xent)
2891e51764aSArtem Bityutskiy 		return -ENOMEM;
2901e51764aSArtem Bityutskiy 
2911e51764aSArtem Bityutskiy 	/*
2921e51764aSArtem Bityutskiy 	 * The extended attribute entries are stored in LNC, so multiple
2931e51764aSArtem Bityutskiy 	 * look-ups do not involve reading the flash.
2941e51764aSArtem Bityutskiy 	 */
2951e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
2961e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
2971e51764aSArtem Bityutskiy 	if (err) {
2981e51764aSArtem Bityutskiy 		if (err != -ENOENT)
2991e51764aSArtem Bityutskiy 			goto out_free;
3001e51764aSArtem Bityutskiy 
3011e51764aSArtem Bityutskiy 		if (flags & XATTR_REPLACE)
3021e51764aSArtem Bityutskiy 			/* We are asked not to create the xattr */
3031e51764aSArtem Bityutskiy 			err = -ENODATA;
3041e51764aSArtem Bityutskiy 		else
3051e51764aSArtem Bityutskiy 			err = create_xattr(c, host, &nm, value, size);
3061e51764aSArtem Bityutskiy 		goto out_free;
3071e51764aSArtem Bityutskiy 	}
3081e51764aSArtem Bityutskiy 
3091e51764aSArtem Bityutskiy 	if (flags & XATTR_CREATE) {
3101e51764aSArtem Bityutskiy 		/* We are asked not to replace the xattr */
3111e51764aSArtem Bityutskiy 		err = -EEXIST;
3121e51764aSArtem Bityutskiy 		goto out_free;
3131e51764aSArtem Bityutskiy 	}
3141e51764aSArtem Bityutskiy 
3151e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3161e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
3171e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
3181e51764aSArtem Bityutskiy 		goto out_free;
3191e51764aSArtem Bityutskiy 	}
3201e51764aSArtem Bityutskiy 
3211e51764aSArtem Bityutskiy 	err = change_xattr(c, host, inode, value, size);
3221e51764aSArtem Bityutskiy 	iput(inode);
3231e51764aSArtem Bityutskiy 
3241e51764aSArtem Bityutskiy out_free:
3251e51764aSArtem Bityutskiy 	kfree(xent);
3261e51764aSArtem Bityutskiy 	return err;
3271e51764aSArtem Bityutskiy }
3281e51764aSArtem Bityutskiy 
3292b88fc21SAndreas Gruenbacher static ssize_t __ubifs_getxattr(struct inode *host, const char *name,
3302b88fc21SAndreas Gruenbacher 				void *buf, size_t size)
3311e51764aSArtem Bityutskiy {
332ce23e640SAl Viro 	struct inode *inode;
3331e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
33426fe5750SLinus Torvalds 	struct qstr nm = QSTR_INIT(name, strlen(name));
3351e51764aSArtem Bityutskiy 	struct ubifs_inode *ui;
3361e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
3371e51764aSArtem Bityutskiy 	union ubifs_key key;
3381e51764aSArtem Bityutskiy 	int err;
3391e51764aSArtem Bityutskiy 
3402b88fc21SAndreas Gruenbacher 	if (nm.len > UBIFS_MAX_NLEN)
3412b88fc21SAndreas Gruenbacher 		return -ENAMETOOLONG;
3421e51764aSArtem Bityutskiy 
3431e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
3441e51764aSArtem Bityutskiy 	if (!xent)
3451e51764aSArtem Bityutskiy 		return -ENOMEM;
3461e51764aSArtem Bityutskiy 
3471e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
3481e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
3491e51764aSArtem Bityutskiy 	if (err) {
3501e51764aSArtem Bityutskiy 		if (err == -ENOENT)
3511e51764aSArtem Bityutskiy 			err = -ENODATA;
3521e51764aSArtem Bityutskiy 		goto out_unlock;
3531e51764aSArtem Bityutskiy 	}
3541e51764aSArtem Bityutskiy 
3551e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
3561e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
3571e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
3581e51764aSArtem Bityutskiy 		goto out_unlock;
3591e51764aSArtem Bityutskiy 	}
3601e51764aSArtem Bityutskiy 
3611e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
3621e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_size == ui->data_len);
3631e51764aSArtem Bityutskiy 	ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
3641e51764aSArtem Bityutskiy 
365ab92a20bSDongsheng Yang 	mutex_lock(&ui->ui_mutex);
3661e51764aSArtem Bityutskiy 	if (buf) {
3671e51764aSArtem Bityutskiy 		/* If @buf is %NULL we are supposed to return the length */
3681e51764aSArtem Bityutskiy 		if (ui->data_len > size) {
369235c362bSSheng Yong 			ubifs_err(c, "buffer size %zd, xattr len %d",
3701e51764aSArtem Bityutskiy 				  size, ui->data_len);
3711e51764aSArtem Bityutskiy 			err = -ERANGE;
3721e51764aSArtem Bityutskiy 			goto out_iput;
3731e51764aSArtem Bityutskiy 		}
3741e51764aSArtem Bityutskiy 
3751e51764aSArtem Bityutskiy 		memcpy(buf, ui->data, ui->data_len);
3761e51764aSArtem Bityutskiy 	}
3771e51764aSArtem Bityutskiy 	err = ui->data_len;
3781e51764aSArtem Bityutskiy 
3791e51764aSArtem Bityutskiy out_iput:
380ab92a20bSDongsheng Yang 	mutex_unlock(&ui->ui_mutex);
3811e51764aSArtem Bityutskiy 	iput(inode);
3821e51764aSArtem Bityutskiy out_unlock:
3831e51764aSArtem Bityutskiy 	kfree(xent);
3841e51764aSArtem Bityutskiy 	return err;
3851e51764aSArtem Bityutskiy }
3861e51764aSArtem Bityutskiy 
3871e51764aSArtem Bityutskiy ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
3881e51764aSArtem Bityutskiy {
3891e51764aSArtem Bityutskiy 	union ubifs_key key;
3902b0143b5SDavid Howells 	struct inode *host = d_inode(dentry);
3911e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
3921e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
3931e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent, *pxent = NULL;
3941e51764aSArtem Bityutskiy 	int err, len, written = 0;
3951e51764aSArtem Bityutskiy 	struct qstr nm = { .name = NULL };
3961e51764aSArtem Bityutskiy 
3974cb2a01dSAl Viro 	dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
3984cb2a01dSAl Viro 		dentry, size);
3991e51764aSArtem Bityutskiy 
4001e51764aSArtem Bityutskiy 	len = host_ui->xattr_names + host_ui->xattr_cnt;
4011e51764aSArtem Bityutskiy 	if (!buffer)
4021e51764aSArtem Bityutskiy 		/*
4031e51764aSArtem Bityutskiy 		 * We should return the minimum buffer size which will fit a
4041e51764aSArtem Bityutskiy 		 * null-terminated list of all the extended attribute names.
4051e51764aSArtem Bityutskiy 		 */
4061e51764aSArtem Bityutskiy 		return len;
4071e51764aSArtem Bityutskiy 
4081e51764aSArtem Bityutskiy 	if (len > size)
4091e51764aSArtem Bityutskiy 		return -ERANGE;
4101e51764aSArtem Bityutskiy 
4111e51764aSArtem Bityutskiy 	lowest_xent_key(c, &key, host->i_ino);
4121e51764aSArtem Bityutskiy 	while (1) {
4131e51764aSArtem Bityutskiy 		xent = ubifs_tnc_next_ent(c, &key, &nm);
4148d47aef4SHirofumi Nakagawa 		if (IS_ERR(xent)) {
4151e51764aSArtem Bityutskiy 			err = PTR_ERR(xent);
4161e51764aSArtem Bityutskiy 			break;
4171e51764aSArtem Bityutskiy 		}
4181e51764aSArtem Bityutskiy 
4191e51764aSArtem Bityutskiy 		nm.name = xent->name;
4201e51764aSArtem Bityutskiy 		nm.len = le16_to_cpu(xent->nlen);
4211e51764aSArtem Bityutskiy 
4221e51764aSArtem Bityutskiy 		/* Show trusted namespace only for "power" users */
4232b88fc21SAndreas Gruenbacher 		if (strncmp(xent->name, XATTR_TRUSTED_PREFIX,
4242b88fc21SAndreas Gruenbacher 			    XATTR_TRUSTED_PREFIX_LEN) ||
4252b88fc21SAndreas Gruenbacher 		    capable(CAP_SYS_ADMIN)) {
4261e51764aSArtem Bityutskiy 			memcpy(buffer + written, nm.name, nm.len + 1);
4271e51764aSArtem Bityutskiy 			written += nm.len + 1;
4281e51764aSArtem Bityutskiy 		}
4291e51764aSArtem Bityutskiy 
4301e51764aSArtem Bityutskiy 		kfree(pxent);
4311e51764aSArtem Bityutskiy 		pxent = xent;
4321e51764aSArtem Bityutskiy 		key_read(c, &xent->key, &key);
4331e51764aSArtem Bityutskiy 	}
4341e51764aSArtem Bityutskiy 
4351e51764aSArtem Bityutskiy 	kfree(pxent);
4361e51764aSArtem Bityutskiy 	if (err != -ENOENT) {
437235c362bSSheng Yong 		ubifs_err(c, "cannot find next direntry, error %d", err);
4381e51764aSArtem Bityutskiy 		return err;
4391e51764aSArtem Bityutskiy 	}
4401e51764aSArtem Bityutskiy 
4411e51764aSArtem Bityutskiy 	ubifs_assert(written <= size);
4421e51764aSArtem Bityutskiy 	return written;
4431e51764aSArtem Bityutskiy }
4441e51764aSArtem Bityutskiy 
4451e51764aSArtem Bityutskiy static int remove_xattr(struct ubifs_info *c, struct inode *host,
4461e51764aSArtem Bityutskiy 			struct inode *inode, const struct qstr *nm)
4471e51764aSArtem Bityutskiy {
4481e51764aSArtem Bityutskiy 	int err;
4491e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
4501e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
4515acd6ff8SZoltan Sogor 	struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
4525acd6ff8SZoltan Sogor 				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
4551e51764aSArtem Bityutskiy 
4561e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
4571e51764aSArtem Bityutskiy 	if (err)
4581e51764aSArtem Bityutskiy 		return err;
4591e51764aSArtem Bityutskiy 
4601e51764aSArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
4611e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
4621e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
4631e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
4641e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
4651e51764aSArtem Bityutskiy 	host_ui->xattr_names -= nm->len;
4661e51764aSArtem Bityutskiy 
4671e51764aSArtem Bityutskiy 	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
4681e51764aSArtem Bityutskiy 	if (err)
4691e51764aSArtem Bityutskiy 		goto out_cancel;
4701e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
4711e51764aSArtem Bityutskiy 
4721e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
4731e51764aSArtem Bityutskiy 	return 0;
4741e51764aSArtem Bityutskiy 
4751e51764aSArtem Bityutskiy out_cancel:
4761e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
4771e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
4781e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
4791e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
4801e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
4811e51764aSArtem Bityutskiy 	make_bad_inode(inode);
4821e51764aSArtem Bityutskiy 	return err;
4831e51764aSArtem Bityutskiy }
4841e51764aSArtem Bityutskiy 
4852b88fc21SAndreas Gruenbacher static int __ubifs_removexattr(struct inode *host, const char *name)
4861e51764aSArtem Bityutskiy {
4872b88fc21SAndreas Gruenbacher 	struct inode *inode;
4881e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
48926fe5750SLinus Torvalds 	struct qstr nm = QSTR_INIT(name, strlen(name));
4901e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
4911e51764aSArtem Bityutskiy 	union ubifs_key key;
4921e51764aSArtem Bityutskiy 	int err;
4931e51764aSArtem Bityutskiy 
4945955102cSAl Viro 	ubifs_assert(inode_is_locked(host));
4951e51764aSArtem Bityutskiy 
4962b88fc21SAndreas Gruenbacher 	if (nm.len > UBIFS_MAX_NLEN)
4972b88fc21SAndreas Gruenbacher 		return -ENAMETOOLONG;
4981e51764aSArtem Bityutskiy 
4991e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
5001e51764aSArtem Bityutskiy 	if (!xent)
5011e51764aSArtem Bityutskiy 		return -ENOMEM;
5021e51764aSArtem Bityutskiy 
5031e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
5041e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
5051e51764aSArtem Bityutskiy 	if (err) {
5061e51764aSArtem Bityutskiy 		if (err == -ENOENT)
5071e51764aSArtem Bityutskiy 			err = -ENODATA;
5081e51764aSArtem Bityutskiy 		goto out_free;
5091e51764aSArtem Bityutskiy 	}
5101e51764aSArtem Bityutskiy 
5111e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
5121e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
5131e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
5141e51764aSArtem Bityutskiy 		goto out_free;
5151e51764aSArtem Bityutskiy 	}
5161e51764aSArtem Bityutskiy 
5171e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_nlink == 1);
5186d6b77f1SMiklos Szeredi 	clear_nlink(inode);
5191e51764aSArtem Bityutskiy 	err = remove_xattr(c, host, inode, &nm);
5201e51764aSArtem Bityutskiy 	if (err)
521bfe86848SMiklos Szeredi 		set_nlink(inode, 1);
5221e51764aSArtem Bityutskiy 
5231e51764aSArtem Bityutskiy 	/* If @i_nlink is 0, 'iput()' will delete the inode */
5241e51764aSArtem Bityutskiy 	iput(inode);
5251e51764aSArtem Bityutskiy 
5261e51764aSArtem Bityutskiy out_free:
5271e51764aSArtem Bityutskiy 	kfree(xent);
5281e51764aSArtem Bityutskiy 	return err;
5291e51764aSArtem Bityutskiy }
530d7f0b70dSSubodh Nijsure 
531d7f0b70dSSubodh Nijsure static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
532d7f0b70dSSubodh Nijsure 		      void *fs_info)
533d7f0b70dSSubodh Nijsure {
534d7f0b70dSSubodh Nijsure 	const struct xattr *xattr;
535d7f0b70dSSubodh Nijsure 	char *name;
536d7f0b70dSSubodh Nijsure 	int err = 0;
537d7f0b70dSSubodh Nijsure 
538d7f0b70dSSubodh Nijsure 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
539d7f0b70dSSubodh Nijsure 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
540d7f0b70dSSubodh Nijsure 			       strlen(xattr->name) + 1, GFP_NOFS);
541d7f0b70dSSubodh Nijsure 		if (!name) {
542d7f0b70dSSubodh Nijsure 			err = -ENOMEM;
543d7f0b70dSSubodh Nijsure 			break;
544d7f0b70dSSubodh Nijsure 		}
545d7f0b70dSSubodh Nijsure 		strcpy(name, XATTR_SECURITY_PREFIX);
546d7f0b70dSSubodh Nijsure 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
5472b88fc21SAndreas Gruenbacher 		err = __ubifs_setxattr(inode, name, xattr->value, xattr->value_len, 0);
548d7f0b70dSSubodh Nijsure 		kfree(name);
549d7f0b70dSSubodh Nijsure 		if (err < 0)
550d7f0b70dSSubodh Nijsure 			break;
551d7f0b70dSSubodh Nijsure 	}
552d7f0b70dSSubodh Nijsure 
553d7f0b70dSSubodh Nijsure 	return err;
554d7f0b70dSSubodh Nijsure }
555d7f0b70dSSubodh Nijsure 
556d7f0b70dSSubodh Nijsure int ubifs_init_security(struct inode *dentry, struct inode *inode,
557d7f0b70dSSubodh Nijsure 			const struct qstr *qstr)
558d7f0b70dSSubodh Nijsure {
559d7f0b70dSSubodh Nijsure 	int err;
560d7f0b70dSSubodh Nijsure 
561d7f0b70dSSubodh Nijsure 	err = security_inode_init_security(inode, dentry, qstr,
562d7f0b70dSSubodh Nijsure 					   &init_xattrs, 0);
563235c362bSSheng Yong 	if (err) {
564235c362bSSheng Yong 		struct ubifs_info *c = dentry->i_sb->s_fs_info;
565235c362bSSheng Yong 		ubifs_err(c, "cannot initialize security for inode %lu, error %d",
566fee1756dSSubodh Nijsure 			  inode->i_ino, err);
567235c362bSSheng Yong 	}
568d7f0b70dSSubodh Nijsure 	return err;
569d7f0b70dSSubodh Nijsure }
5702b88fc21SAndreas Gruenbacher 
5712b88fc21SAndreas Gruenbacher static int ubifs_xattr_get(const struct xattr_handler *handler,
5722b88fc21SAndreas Gruenbacher 			   struct dentry *dentry, struct inode *inode,
5732b88fc21SAndreas Gruenbacher 			   const char *name, void *buffer, size_t size)
5742b88fc21SAndreas Gruenbacher {
5752b88fc21SAndreas Gruenbacher 	dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
5762b88fc21SAndreas Gruenbacher 		inode->i_ino, dentry, size);
5772b88fc21SAndreas Gruenbacher 
5782b88fc21SAndreas Gruenbacher 	return  __ubifs_getxattr(inode, name, buffer, size);
5792b88fc21SAndreas Gruenbacher }
5802b88fc21SAndreas Gruenbacher 
5812b88fc21SAndreas Gruenbacher static int ubifs_xattr_set(const struct xattr_handler *handler,
582*59301226SAl Viro 			   struct dentry *dentry, struct inode *inode,
583*59301226SAl Viro 			   const char *name, const void *value,
584*59301226SAl Viro 			   size_t size, int flags)
5852b88fc21SAndreas Gruenbacher {
5862b88fc21SAndreas Gruenbacher 	dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
5872b88fc21SAndreas Gruenbacher 		name, inode->i_ino, dentry, size);
5882b88fc21SAndreas Gruenbacher 
5892b88fc21SAndreas Gruenbacher 	if (value)
5902b88fc21SAndreas Gruenbacher 		return __ubifs_setxattr(inode, name, value, size, flags);
5912b88fc21SAndreas Gruenbacher 	else
5922b88fc21SAndreas Gruenbacher 		return __ubifs_removexattr(inode, name);
5932b88fc21SAndreas Gruenbacher }
5942b88fc21SAndreas Gruenbacher 
5952b88fc21SAndreas Gruenbacher const struct xattr_handler ubifs_user_xattr_handler = {
5962b88fc21SAndreas Gruenbacher 	.prefix = XATTR_USER_PREFIX,
5972b88fc21SAndreas Gruenbacher 	.get = ubifs_xattr_get,
5982b88fc21SAndreas Gruenbacher 	.set = ubifs_xattr_set,
5992b88fc21SAndreas Gruenbacher };
6002b88fc21SAndreas Gruenbacher 
6012b88fc21SAndreas Gruenbacher const struct xattr_handler ubifs_trusted_xattr_handler = {
6022b88fc21SAndreas Gruenbacher 	.prefix = XATTR_TRUSTED_PREFIX,
6032b88fc21SAndreas Gruenbacher 	.get = ubifs_xattr_get,
6042b88fc21SAndreas Gruenbacher 	.set = ubifs_xattr_set,
6052b88fc21SAndreas Gruenbacher };
6062b88fc21SAndreas Gruenbacher 
6072b88fc21SAndreas Gruenbacher const struct xattr_handler ubifs_security_xattr_handler = {
6082b88fc21SAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
6092b88fc21SAndreas Gruenbacher 	.get = ubifs_xattr_get,
6102b88fc21SAndreas Gruenbacher 	.set = ubifs_xattr_set,
6112b88fc21SAndreas Gruenbacher };
6122b88fc21SAndreas Gruenbacher 
6132b88fc21SAndreas Gruenbacher const struct xattr_handler *ubifs_xattr_handlers[] = {
6142b88fc21SAndreas Gruenbacher 	&ubifs_user_xattr_handler,
6152b88fc21SAndreas Gruenbacher 	&ubifs_trusted_xattr_handler,
6162b88fc21SAndreas Gruenbacher 	&ubifs_security_xattr_handler,
6172b88fc21SAndreas Gruenbacher 	NULL
6182b88fc21SAndreas Gruenbacher };
619