xref: /openbmc/linux/fs/ubifs/xattr.c (revision 1e51764a3c2ac05a23a22b2a95ddee4d9bffb16d)
1*1e51764aSArtem Bityutskiy /*
2*1e51764aSArtem Bityutskiy  * This file is part of UBIFS.
3*1e51764aSArtem Bityutskiy  *
4*1e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
5*1e51764aSArtem Bityutskiy  *
6*1e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
7*1e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
8*1e51764aSArtem Bityutskiy  * the Free Software Foundation.
9*1e51764aSArtem Bityutskiy  *
10*1e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
11*1e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12*1e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13*1e51764aSArtem Bityutskiy  * more details.
14*1e51764aSArtem Bityutskiy  *
15*1e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
16*1e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
17*1e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*1e51764aSArtem Bityutskiy  *
19*1e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
20*1e51764aSArtem Bityutskiy  *          Adrian Hunter
21*1e51764aSArtem Bityutskiy  */
22*1e51764aSArtem Bityutskiy 
23*1e51764aSArtem Bityutskiy /*
24*1e51764aSArtem Bityutskiy  * This file implements UBIFS extended attributes support.
25*1e51764aSArtem Bityutskiy  *
26*1e51764aSArtem Bityutskiy  * Extended attributes are implemented as regular inodes with attached data,
27*1e51764aSArtem Bityutskiy  * which limits extended attribute size to UBIFS block size (4KiB). Names of
28*1e51764aSArtem Bityutskiy  * extended attributes are described by extended attribute entries (xentries),
29*1e51764aSArtem Bityutskiy  * which are almost identical to directory entries, but have different key type.
30*1e51764aSArtem Bityutskiy  *
31*1e51764aSArtem Bityutskiy  * In other words, the situation with extended attributes is very similar to
32*1e51764aSArtem Bityutskiy  * directories. Indeed, any inode (but of course not xattr inodes) may have a
33*1e51764aSArtem Bityutskiy  * number of associated xentries, just like directory inodes have associated
34*1e51764aSArtem Bityutskiy  * directory entries. Extended attribute entries store the name of the extended
35*1e51764aSArtem Bityutskiy  * attribute, the host inode number, and the extended attribute inode number.
36*1e51764aSArtem Bityutskiy  * Similarly, direntries store the name, the parent and the target inode
37*1e51764aSArtem Bityutskiy  * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
38*1e51764aSArtem Bityutskiy  * extended attributes.
39*1e51764aSArtem Bityutskiy  *
40*1e51764aSArtem Bityutskiy  * The number of extended attributes is not limited, but there is Linux
41*1e51764aSArtem Bityutskiy  * limitation on the maximum possible size of the list of all extended
42*1e51764aSArtem Bityutskiy  * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
43*1e51764aSArtem Bityutskiy  * the sum of all extended attribute names of the inode does not exceed that
44*1e51764aSArtem Bityutskiy  * limit.
45*1e51764aSArtem Bityutskiy  *
46*1e51764aSArtem Bityutskiy  * Extended attributes are synchronous, which means they are written to the
47*1e51764aSArtem Bityutskiy  * flash media synchronously and there is no write-back for extended attribute
48*1e51764aSArtem Bityutskiy  * inodes. The extended attribute values are not stored in compressed form on
49*1e51764aSArtem Bityutskiy  * the media.
50*1e51764aSArtem Bityutskiy  *
51*1e51764aSArtem Bityutskiy  * Since extended attributes are represented by regular inodes, they are cached
52*1e51764aSArtem Bityutskiy  * in the VFS inode cache. The xentries are cached in the LNC cache (see
53*1e51764aSArtem Bityutskiy  * tnc.c).
54*1e51764aSArtem Bityutskiy  *
55*1e51764aSArtem Bityutskiy  * ACL support is not implemented.
56*1e51764aSArtem Bityutskiy  */
57*1e51764aSArtem Bityutskiy 
58*1e51764aSArtem Bityutskiy #include <linux/xattr.h>
59*1e51764aSArtem Bityutskiy #include <linux/posix_acl_xattr.h>
60*1e51764aSArtem Bityutskiy #include "ubifs.h"
61*1e51764aSArtem Bityutskiy 
62*1e51764aSArtem Bityutskiy /*
63*1e51764aSArtem Bityutskiy  * Limit the number of extended attributes per inode so that the total size
64*1e51764aSArtem Bityutskiy  * (xattr_size) is guaranteeded to fit in an 'unsigned int'.
65*1e51764aSArtem Bityutskiy  */
66*1e51764aSArtem Bityutskiy #define MAX_XATTRS_PER_INODE 65535
67*1e51764aSArtem Bityutskiy 
68*1e51764aSArtem Bityutskiy /*
69*1e51764aSArtem Bityutskiy  * Extended attribute type constants.
70*1e51764aSArtem Bityutskiy  *
71*1e51764aSArtem Bityutskiy  * USER_XATTR: user extended attribute ("user.*")
72*1e51764aSArtem Bityutskiy  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
73*1e51764aSArtem Bityutskiy  * SECURITY_XATTR: security extended attribute ("security.*")
74*1e51764aSArtem Bityutskiy  */
75*1e51764aSArtem Bityutskiy enum {
76*1e51764aSArtem Bityutskiy 	USER_XATTR,
77*1e51764aSArtem Bityutskiy 	TRUSTED_XATTR,
78*1e51764aSArtem Bityutskiy 	SECURITY_XATTR,
79*1e51764aSArtem Bityutskiy };
80*1e51764aSArtem Bityutskiy 
81*1e51764aSArtem Bityutskiy static struct inode_operations none_inode_operations;
82*1e51764aSArtem Bityutskiy static struct address_space_operations none_address_operations;
83*1e51764aSArtem Bityutskiy static struct file_operations none_file_operations;
84*1e51764aSArtem Bityutskiy 
85*1e51764aSArtem Bityutskiy /**
86*1e51764aSArtem Bityutskiy  * create_xattr - create an extended attribute.
87*1e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
88*1e51764aSArtem Bityutskiy  * @host: host inode
89*1e51764aSArtem Bityutskiy  * @nm: extended attribute name
90*1e51764aSArtem Bityutskiy  * @value: extended attribute value
91*1e51764aSArtem Bityutskiy  * @size: size of extended attribute value
92*1e51764aSArtem Bityutskiy  *
93*1e51764aSArtem Bityutskiy  * This is a helper function which creates an extended attribute of name @nm
94*1e51764aSArtem Bityutskiy  * and value @value for inode @host. The host inode is also updated on flash
95*1e51764aSArtem Bityutskiy  * because the ctime and extended attribute accounting data changes. This
96*1e51764aSArtem Bityutskiy  * function returns zero in case of success and a negative error code in case
97*1e51764aSArtem Bityutskiy  * of failure.
98*1e51764aSArtem Bityutskiy  */
99*1e51764aSArtem Bityutskiy static int create_xattr(struct ubifs_info *c, struct inode *host,
100*1e51764aSArtem Bityutskiy 			const struct qstr *nm, const void *value, int size)
101*1e51764aSArtem Bityutskiy {
102*1e51764aSArtem Bityutskiy 	int err;
103*1e51764aSArtem Bityutskiy 	struct inode *inode;
104*1e51764aSArtem Bityutskiy 	struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
105*1e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
106*1e51764aSArtem Bityutskiy 					.new_ino_d = size, .dirtied_ino = 1,
107*1e51764aSArtem Bityutskiy 					.dirtied_ino_d = host_ui->data_len};
108*1e51764aSArtem Bityutskiy 
109*1e51764aSArtem Bityutskiy 	if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
110*1e51764aSArtem Bityutskiy 		return -ENOSPC;
111*1e51764aSArtem Bityutskiy 	/*
112*1e51764aSArtem Bityutskiy 	 * Linux limits the maximum size of the extended attribute names list
113*1e51764aSArtem Bityutskiy 	 * to %XATTR_LIST_MAX. This means we should not allow creating more*
114*1e51764aSArtem Bityutskiy 	 * extended attributes if the name list becomes larger. This limitation
115*1e51764aSArtem Bityutskiy 	 * is artificial for UBIFS, though.
116*1e51764aSArtem Bityutskiy 	 */
117*1e51764aSArtem Bityutskiy 	if (host_ui->xattr_names + host_ui->xattr_cnt +
118*1e51764aSArtem Bityutskiy 					nm->len + 1 > XATTR_LIST_MAX)
119*1e51764aSArtem Bityutskiy 		return -ENOSPC;
120*1e51764aSArtem Bityutskiy 
121*1e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
122*1e51764aSArtem Bityutskiy 	if (err)
123*1e51764aSArtem Bityutskiy 		return err;
124*1e51764aSArtem Bityutskiy 
125*1e51764aSArtem Bityutskiy 	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
126*1e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
127*1e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
128*1e51764aSArtem Bityutskiy 		goto out_budg;
129*1e51764aSArtem Bityutskiy 	}
130*1e51764aSArtem Bityutskiy 
131*1e51764aSArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
132*1e51764aSArtem Bityutskiy 	/* Re-define all operations to be "nothing" */
133*1e51764aSArtem Bityutskiy 	inode->i_mapping->a_ops = &none_address_operations;
134*1e51764aSArtem Bityutskiy 	inode->i_op = &none_inode_operations;
135*1e51764aSArtem Bityutskiy 	inode->i_fop = &none_file_operations;
136*1e51764aSArtem Bityutskiy 
137*1e51764aSArtem Bityutskiy 	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
138*1e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
139*1e51764aSArtem Bityutskiy 	ui->xattr = 1;
140*1e51764aSArtem Bityutskiy 	ui->flags |= UBIFS_XATTR_FL;
141*1e51764aSArtem Bityutskiy 	ui->data = kmalloc(size, GFP_NOFS);
142*1e51764aSArtem Bityutskiy 	if (!ui->data) {
143*1e51764aSArtem Bityutskiy 		err = -ENOMEM;
144*1e51764aSArtem Bityutskiy 		goto out_unlock;
145*1e51764aSArtem Bityutskiy 	}
146*1e51764aSArtem Bityutskiy 
147*1e51764aSArtem Bityutskiy 	memcpy(ui->data, value, size);
148*1e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
149*1e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
150*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
151*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
152*1e51764aSArtem Bityutskiy 	host_ui->xattr_names += nm->len;
153*1e51764aSArtem Bityutskiy 
154*1e51764aSArtem Bityutskiy 	/*
155*1e51764aSArtem Bityutskiy 	 * We do not use i_size_write() because nobody can race with us as we
156*1e51764aSArtem Bityutskiy 	 * are holding host @host->i_mutex - every xattr operation for this
157*1e51764aSArtem Bityutskiy 	 * inode is serialized by it.
158*1e51764aSArtem Bityutskiy 	 */
159*1e51764aSArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
160*1e51764aSArtem Bityutskiy 	ui->data_len = size;
161*1e51764aSArtem Bityutskiy 	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
162*1e51764aSArtem Bityutskiy 	if (err)
163*1e51764aSArtem Bityutskiy 		goto out_cancel;
164*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
165*1e51764aSArtem Bityutskiy 
166*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
167*1e51764aSArtem Bityutskiy 	insert_inode_hash(inode);
168*1e51764aSArtem Bityutskiy 	iput(inode);
169*1e51764aSArtem Bityutskiy 	return 0;
170*1e51764aSArtem Bityutskiy 
171*1e51764aSArtem Bityutskiy out_cancel:
172*1e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
173*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
174*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
175*1e51764aSArtem Bityutskiy out_unlock:
176*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
177*1e51764aSArtem Bityutskiy 	make_bad_inode(inode);
178*1e51764aSArtem Bityutskiy 	iput(inode);
179*1e51764aSArtem Bityutskiy out_budg:
180*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
181*1e51764aSArtem Bityutskiy 	return err;
182*1e51764aSArtem Bityutskiy }
183*1e51764aSArtem Bityutskiy 
184*1e51764aSArtem Bityutskiy /**
185*1e51764aSArtem Bityutskiy  * change_xattr - change an extended attribute.
186*1e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
187*1e51764aSArtem Bityutskiy  * @host: host inode
188*1e51764aSArtem Bityutskiy  * @inode: extended attribute inode
189*1e51764aSArtem Bityutskiy  * @value: extended attribute value
190*1e51764aSArtem Bityutskiy  * @size: size of extended attribute value
191*1e51764aSArtem Bityutskiy  *
192*1e51764aSArtem Bityutskiy  * This helper function changes the value of extended attribute @inode with new
193*1e51764aSArtem Bityutskiy  * data from @value. Returns zero in case of success and a negative error code
194*1e51764aSArtem Bityutskiy  * in case of failure.
195*1e51764aSArtem Bityutskiy  */
196*1e51764aSArtem Bityutskiy static int change_xattr(struct ubifs_info *c, struct inode *host,
197*1e51764aSArtem Bityutskiy 			struct inode *inode, const void *value, int size)
198*1e51764aSArtem Bityutskiy {
199*1e51764aSArtem Bityutskiy 	int err;
200*1e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
201*1e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
202*1e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .dirtied_ino = 2,
203*1e51764aSArtem Bityutskiy 				.dirtied_ino_d = size + host_ui->data_len };
204*1e51764aSArtem Bityutskiy 
205*1e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
206*1e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
207*1e51764aSArtem Bityutskiy 	if (err)
208*1e51764aSArtem Bityutskiy 		return err;
209*1e51764aSArtem Bityutskiy 
210*1e51764aSArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
211*1e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
212*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
213*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
214*1e51764aSArtem Bityutskiy 
215*1e51764aSArtem Bityutskiy 	kfree(ui->data);
216*1e51764aSArtem Bityutskiy 	ui->data = kmalloc(size, GFP_NOFS);
217*1e51764aSArtem Bityutskiy 	if (!ui->data) {
218*1e51764aSArtem Bityutskiy 		err = -ENOMEM;
219*1e51764aSArtem Bityutskiy 		goto out_unlock;
220*1e51764aSArtem Bityutskiy 	}
221*1e51764aSArtem Bityutskiy 
222*1e51764aSArtem Bityutskiy 	memcpy(ui->data, value, size);
223*1e51764aSArtem Bityutskiy 	inode->i_size = ui->ui_size = size;
224*1e51764aSArtem Bityutskiy 	ui->data_len = size;
225*1e51764aSArtem Bityutskiy 
226*1e51764aSArtem Bityutskiy 	/*
227*1e51764aSArtem Bityutskiy 	 * It is important to write the host inode after the xattr inode
228*1e51764aSArtem Bityutskiy 	 * because if the host inode gets synchronized (via 'fsync()'), then
229*1e51764aSArtem Bityutskiy 	 * the extended attribute inode gets synchronized, because it goes
230*1e51764aSArtem Bityutskiy 	 * before the host inode in the write-buffer.
231*1e51764aSArtem Bityutskiy 	 */
232*1e51764aSArtem Bityutskiy 	err = ubifs_jnl_change_xattr(c, inode, host);
233*1e51764aSArtem Bityutskiy 	if (err)
234*1e51764aSArtem Bityutskiy 		goto out_cancel;
235*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
236*1e51764aSArtem Bityutskiy 
237*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
238*1e51764aSArtem Bityutskiy 	return 0;
239*1e51764aSArtem Bityutskiy 
240*1e51764aSArtem Bityutskiy out_cancel:
241*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
242*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
243*1e51764aSArtem Bityutskiy 	make_bad_inode(inode);
244*1e51764aSArtem Bityutskiy out_unlock:
245*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
246*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
247*1e51764aSArtem Bityutskiy 	return err;
248*1e51764aSArtem Bityutskiy }
249*1e51764aSArtem Bityutskiy 
250*1e51764aSArtem Bityutskiy /**
251*1e51764aSArtem Bityutskiy  * check_namespace - check extended attribute name-space.
252*1e51764aSArtem Bityutskiy  * @nm: extended attribute name
253*1e51764aSArtem Bityutskiy  *
254*1e51764aSArtem Bityutskiy  * This function makes sure the extended attribute name belongs to one of the
255*1e51764aSArtem Bityutskiy  * supported extended attribute name-spaces. Returns name-space index in case
256*1e51764aSArtem Bityutskiy  * of success and a negative error code in case of failure.
257*1e51764aSArtem Bityutskiy  */
258*1e51764aSArtem Bityutskiy static int check_namespace(const struct qstr *nm)
259*1e51764aSArtem Bityutskiy {
260*1e51764aSArtem Bityutskiy 	int type;
261*1e51764aSArtem Bityutskiy 
262*1e51764aSArtem Bityutskiy 	if (nm->len > UBIFS_MAX_NLEN)
263*1e51764aSArtem Bityutskiy 		return -ENAMETOOLONG;
264*1e51764aSArtem Bityutskiy 
265*1e51764aSArtem Bityutskiy 	if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
266*1e51764aSArtem Bityutskiy 		     XATTR_TRUSTED_PREFIX_LEN)) {
267*1e51764aSArtem Bityutskiy 		if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
268*1e51764aSArtem Bityutskiy 			return -EINVAL;
269*1e51764aSArtem Bityutskiy 		type = TRUSTED_XATTR;
270*1e51764aSArtem Bityutskiy 	} else if (!strncmp(nm->name, XATTR_USER_PREFIX,
271*1e51764aSArtem Bityutskiy 				      XATTR_USER_PREFIX_LEN)) {
272*1e51764aSArtem Bityutskiy 		if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
273*1e51764aSArtem Bityutskiy 			return -EINVAL;
274*1e51764aSArtem Bityutskiy 		type = USER_XATTR;
275*1e51764aSArtem Bityutskiy 	} else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
276*1e51764aSArtem Bityutskiy 				     XATTR_SECURITY_PREFIX_LEN)) {
277*1e51764aSArtem Bityutskiy 		if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
278*1e51764aSArtem Bityutskiy 			return -EINVAL;
279*1e51764aSArtem Bityutskiy 		type = SECURITY_XATTR;
280*1e51764aSArtem Bityutskiy 	} else
281*1e51764aSArtem Bityutskiy 		return -EOPNOTSUPP;
282*1e51764aSArtem Bityutskiy 
283*1e51764aSArtem Bityutskiy 	return type;
284*1e51764aSArtem Bityutskiy }
285*1e51764aSArtem Bityutskiy 
286*1e51764aSArtem Bityutskiy static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
287*1e51764aSArtem Bityutskiy {
288*1e51764aSArtem Bityutskiy 	struct inode *inode;
289*1e51764aSArtem Bityutskiy 
290*1e51764aSArtem Bityutskiy 	inode = ubifs_iget(c->vfs_sb, inum);
291*1e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
292*1e51764aSArtem Bityutskiy 		ubifs_err("dead extended attribute entry, error %d",
293*1e51764aSArtem Bityutskiy 			  (int)PTR_ERR(inode));
294*1e51764aSArtem Bityutskiy 		return inode;
295*1e51764aSArtem Bityutskiy 	}
296*1e51764aSArtem Bityutskiy 	if (ubifs_inode(inode)->xattr)
297*1e51764aSArtem Bityutskiy 		return inode;
298*1e51764aSArtem Bityutskiy 	ubifs_err("corrupt extended attribute entry");
299*1e51764aSArtem Bityutskiy 	iput(inode);
300*1e51764aSArtem Bityutskiy 	return ERR_PTR(-EINVAL);
301*1e51764aSArtem Bityutskiy }
302*1e51764aSArtem Bityutskiy 
303*1e51764aSArtem Bityutskiy int ubifs_setxattr(struct dentry *dentry, const char *name,
304*1e51764aSArtem Bityutskiy 		   const void *value, size_t size, int flags)
305*1e51764aSArtem Bityutskiy {
306*1e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
307*1e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
308*1e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
309*1e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
310*1e51764aSArtem Bityutskiy 	union ubifs_key key;
311*1e51764aSArtem Bityutskiy 	int err, type;
312*1e51764aSArtem Bityutskiy 
313*1e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
314*1e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
315*1e51764aSArtem Bityutskiy 
316*1e51764aSArtem Bityutskiy 	if (size > UBIFS_MAX_INO_DATA)
317*1e51764aSArtem Bityutskiy 		return -ERANGE;
318*1e51764aSArtem Bityutskiy 
319*1e51764aSArtem Bityutskiy 	type = check_namespace(&nm);
320*1e51764aSArtem Bityutskiy 	if (type < 0)
321*1e51764aSArtem Bityutskiy 		return type;
322*1e51764aSArtem Bityutskiy 
323*1e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
324*1e51764aSArtem Bityutskiy 	if (!xent)
325*1e51764aSArtem Bityutskiy 		return -ENOMEM;
326*1e51764aSArtem Bityutskiy 
327*1e51764aSArtem Bityutskiy 	/*
328*1e51764aSArtem Bityutskiy 	 * The extended attribute entries are stored in LNC, so multiple
329*1e51764aSArtem Bityutskiy 	 * look-ups do not involve reading the flash.
330*1e51764aSArtem Bityutskiy 	 */
331*1e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
332*1e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
333*1e51764aSArtem Bityutskiy 	if (err) {
334*1e51764aSArtem Bityutskiy 		if (err != -ENOENT)
335*1e51764aSArtem Bityutskiy 			goto out_free;
336*1e51764aSArtem Bityutskiy 
337*1e51764aSArtem Bityutskiy 		if (flags & XATTR_REPLACE)
338*1e51764aSArtem Bityutskiy 			/* We are asked not to create the xattr */
339*1e51764aSArtem Bityutskiy 			err = -ENODATA;
340*1e51764aSArtem Bityutskiy 		else
341*1e51764aSArtem Bityutskiy 			err = create_xattr(c, host, &nm, value, size);
342*1e51764aSArtem Bityutskiy 		goto out_free;
343*1e51764aSArtem Bityutskiy 	}
344*1e51764aSArtem Bityutskiy 
345*1e51764aSArtem Bityutskiy 	if (flags & XATTR_CREATE) {
346*1e51764aSArtem Bityutskiy 		/* We are asked not to replace the xattr */
347*1e51764aSArtem Bityutskiy 		err = -EEXIST;
348*1e51764aSArtem Bityutskiy 		goto out_free;
349*1e51764aSArtem Bityutskiy 	}
350*1e51764aSArtem Bityutskiy 
351*1e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
352*1e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
353*1e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
354*1e51764aSArtem Bityutskiy 		goto out_free;
355*1e51764aSArtem Bityutskiy 	}
356*1e51764aSArtem Bityutskiy 
357*1e51764aSArtem Bityutskiy 	err = change_xattr(c, host, inode, value, size);
358*1e51764aSArtem Bityutskiy 	iput(inode);
359*1e51764aSArtem Bityutskiy 
360*1e51764aSArtem Bityutskiy out_free:
361*1e51764aSArtem Bityutskiy 	kfree(xent);
362*1e51764aSArtem Bityutskiy 	return err;
363*1e51764aSArtem Bityutskiy }
364*1e51764aSArtem Bityutskiy 
365*1e51764aSArtem Bityutskiy ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
366*1e51764aSArtem Bityutskiy 		       size_t size)
367*1e51764aSArtem Bityutskiy {
368*1e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
369*1e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
370*1e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
371*1e51764aSArtem Bityutskiy 	struct ubifs_inode *ui;
372*1e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
373*1e51764aSArtem Bityutskiy 	union ubifs_key key;
374*1e51764aSArtem Bityutskiy 	int err;
375*1e51764aSArtem Bityutskiy 
376*1e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
377*1e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
378*1e51764aSArtem Bityutskiy 
379*1e51764aSArtem Bityutskiy 	err = check_namespace(&nm);
380*1e51764aSArtem Bityutskiy 	if (err < 0)
381*1e51764aSArtem Bityutskiy 		return err;
382*1e51764aSArtem Bityutskiy 
383*1e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
384*1e51764aSArtem Bityutskiy 	if (!xent)
385*1e51764aSArtem Bityutskiy 		return -ENOMEM;
386*1e51764aSArtem Bityutskiy 
387*1e51764aSArtem Bityutskiy 	mutex_lock(&host->i_mutex);
388*1e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
389*1e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
390*1e51764aSArtem Bityutskiy 	if (err) {
391*1e51764aSArtem Bityutskiy 		if (err == -ENOENT)
392*1e51764aSArtem Bityutskiy 			err = -ENODATA;
393*1e51764aSArtem Bityutskiy 		goto out_unlock;
394*1e51764aSArtem Bityutskiy 	}
395*1e51764aSArtem Bityutskiy 
396*1e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
397*1e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
398*1e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
399*1e51764aSArtem Bityutskiy 		goto out_unlock;
400*1e51764aSArtem Bityutskiy 	}
401*1e51764aSArtem Bityutskiy 
402*1e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
403*1e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_size == ui->data_len);
404*1e51764aSArtem Bityutskiy 	ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
405*1e51764aSArtem Bityutskiy 
406*1e51764aSArtem Bityutskiy 	if (buf) {
407*1e51764aSArtem Bityutskiy 		/* If @buf is %NULL we are supposed to return the length */
408*1e51764aSArtem Bityutskiy 		if (ui->data_len > size) {
409*1e51764aSArtem Bityutskiy 			dbg_err("buffer size %zd, xattr len %d",
410*1e51764aSArtem Bityutskiy 				size, ui->data_len);
411*1e51764aSArtem Bityutskiy 			err = -ERANGE;
412*1e51764aSArtem Bityutskiy 			goto out_iput;
413*1e51764aSArtem Bityutskiy 		}
414*1e51764aSArtem Bityutskiy 
415*1e51764aSArtem Bityutskiy 		memcpy(buf, ui->data, ui->data_len);
416*1e51764aSArtem Bityutskiy 	}
417*1e51764aSArtem Bityutskiy 	err = ui->data_len;
418*1e51764aSArtem Bityutskiy 
419*1e51764aSArtem Bityutskiy out_iput:
420*1e51764aSArtem Bityutskiy 	iput(inode);
421*1e51764aSArtem Bityutskiy out_unlock:
422*1e51764aSArtem Bityutskiy 	mutex_unlock(&host->i_mutex);
423*1e51764aSArtem Bityutskiy 	kfree(xent);
424*1e51764aSArtem Bityutskiy 	return err;
425*1e51764aSArtem Bityutskiy }
426*1e51764aSArtem Bityutskiy 
427*1e51764aSArtem Bityutskiy ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
428*1e51764aSArtem Bityutskiy {
429*1e51764aSArtem Bityutskiy 	union ubifs_key key;
430*1e51764aSArtem Bityutskiy 	struct inode *host = dentry->d_inode;
431*1e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
432*1e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
433*1e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent, *pxent = NULL;
434*1e51764aSArtem Bityutskiy 	int err, len, written = 0;
435*1e51764aSArtem Bityutskiy 	struct qstr nm = { .name = NULL };
436*1e51764aSArtem Bityutskiy 
437*1e51764aSArtem Bityutskiy 	dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
438*1e51764aSArtem Bityutskiy 		dentry->d_name.len, dentry->d_name.name, size);
439*1e51764aSArtem Bityutskiy 
440*1e51764aSArtem Bityutskiy 	len = host_ui->xattr_names + host_ui->xattr_cnt;
441*1e51764aSArtem Bityutskiy 	if (!buffer)
442*1e51764aSArtem Bityutskiy 		/*
443*1e51764aSArtem Bityutskiy 		 * We should return the minimum buffer size which will fit a
444*1e51764aSArtem Bityutskiy 		 * null-terminated list of all the extended attribute names.
445*1e51764aSArtem Bityutskiy 		 */
446*1e51764aSArtem Bityutskiy 		return len;
447*1e51764aSArtem Bityutskiy 
448*1e51764aSArtem Bityutskiy 	if (len > size)
449*1e51764aSArtem Bityutskiy 		return -ERANGE;
450*1e51764aSArtem Bityutskiy 
451*1e51764aSArtem Bityutskiy 	lowest_xent_key(c, &key, host->i_ino);
452*1e51764aSArtem Bityutskiy 
453*1e51764aSArtem Bityutskiy 	mutex_lock(&host->i_mutex);
454*1e51764aSArtem Bityutskiy 	while (1) {
455*1e51764aSArtem Bityutskiy 		int type;
456*1e51764aSArtem Bityutskiy 
457*1e51764aSArtem Bityutskiy 		xent = ubifs_tnc_next_ent(c, &key, &nm);
458*1e51764aSArtem Bityutskiy 		if (unlikely(IS_ERR(xent))) {
459*1e51764aSArtem Bityutskiy 			err = PTR_ERR(xent);
460*1e51764aSArtem Bityutskiy 			break;
461*1e51764aSArtem Bityutskiy 		}
462*1e51764aSArtem Bityutskiy 
463*1e51764aSArtem Bityutskiy 		nm.name = xent->name;
464*1e51764aSArtem Bityutskiy 		nm.len = le16_to_cpu(xent->nlen);
465*1e51764aSArtem Bityutskiy 
466*1e51764aSArtem Bityutskiy 		type = check_namespace(&nm);
467*1e51764aSArtem Bityutskiy 		if (unlikely(type < 0)) {
468*1e51764aSArtem Bityutskiy 			err = type;
469*1e51764aSArtem Bityutskiy 			break;
470*1e51764aSArtem Bityutskiy 		}
471*1e51764aSArtem Bityutskiy 
472*1e51764aSArtem Bityutskiy 		/* Show trusted namespace only for "power" users */
473*1e51764aSArtem Bityutskiy 		if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
474*1e51764aSArtem Bityutskiy 			memcpy(buffer + written, nm.name, nm.len + 1);
475*1e51764aSArtem Bityutskiy 			written += nm.len + 1;
476*1e51764aSArtem Bityutskiy 		}
477*1e51764aSArtem Bityutskiy 
478*1e51764aSArtem Bityutskiy 		kfree(pxent);
479*1e51764aSArtem Bityutskiy 		pxent = xent;
480*1e51764aSArtem Bityutskiy 		key_read(c, &xent->key, &key);
481*1e51764aSArtem Bityutskiy 	}
482*1e51764aSArtem Bityutskiy 	mutex_unlock(&host->i_mutex);
483*1e51764aSArtem Bityutskiy 
484*1e51764aSArtem Bityutskiy 	kfree(pxent);
485*1e51764aSArtem Bityutskiy 	if (err != -ENOENT) {
486*1e51764aSArtem Bityutskiy 		ubifs_err("cannot find next direntry, error %d", err);
487*1e51764aSArtem Bityutskiy 		return err;
488*1e51764aSArtem Bityutskiy 	}
489*1e51764aSArtem Bityutskiy 
490*1e51764aSArtem Bityutskiy 	ubifs_assert(written <= size);
491*1e51764aSArtem Bityutskiy 	return written;
492*1e51764aSArtem Bityutskiy }
493*1e51764aSArtem Bityutskiy 
494*1e51764aSArtem Bityutskiy static int remove_xattr(struct ubifs_info *c, struct inode *host,
495*1e51764aSArtem Bityutskiy 			struct inode *inode, const struct qstr *nm)
496*1e51764aSArtem Bityutskiy {
497*1e51764aSArtem Bityutskiy 	int err;
498*1e51764aSArtem Bityutskiy 	struct ubifs_inode *host_ui = ubifs_inode(host);
499*1e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
500*1e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .dirtied_ino = 1, .mod_dent = 1,
501*1e51764aSArtem Bityutskiy 					.dirtied_ino_d = host_ui->data_len };
502*1e51764aSArtem Bityutskiy 
503*1e51764aSArtem Bityutskiy 	ubifs_assert(ui->data_len == inode->i_size);
504*1e51764aSArtem Bityutskiy 
505*1e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
506*1e51764aSArtem Bityutskiy 	if (err)
507*1e51764aSArtem Bityutskiy 		return err;
508*1e51764aSArtem Bityutskiy 
509*1e51764aSArtem Bityutskiy 	mutex_lock(&host_ui->ui_mutex);
510*1e51764aSArtem Bityutskiy 	host->i_ctime = ubifs_current_time(host);
511*1e51764aSArtem Bityutskiy 	host_ui->xattr_cnt -= 1;
512*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
513*1e51764aSArtem Bityutskiy 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
514*1e51764aSArtem Bityutskiy 	host_ui->xattr_names -= nm->len;
515*1e51764aSArtem Bityutskiy 
516*1e51764aSArtem Bityutskiy 	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
517*1e51764aSArtem Bityutskiy 	if (err)
518*1e51764aSArtem Bityutskiy 		goto out_cancel;
519*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
520*1e51764aSArtem Bityutskiy 
521*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
522*1e51764aSArtem Bityutskiy 	return 0;
523*1e51764aSArtem Bityutskiy 
524*1e51764aSArtem Bityutskiy out_cancel:
525*1e51764aSArtem Bityutskiy 	host_ui->xattr_cnt += 1;
526*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
527*1e51764aSArtem Bityutskiy 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
528*1e51764aSArtem Bityutskiy 	mutex_unlock(&host_ui->ui_mutex);
529*1e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
530*1e51764aSArtem Bityutskiy 	make_bad_inode(inode);
531*1e51764aSArtem Bityutskiy 	return err;
532*1e51764aSArtem Bityutskiy }
533*1e51764aSArtem Bityutskiy 
534*1e51764aSArtem Bityutskiy int ubifs_removexattr(struct dentry *dentry, const char *name)
535*1e51764aSArtem Bityutskiy {
536*1e51764aSArtem Bityutskiy 	struct inode *inode, *host = dentry->d_inode;
537*1e51764aSArtem Bityutskiy 	struct ubifs_info *c = host->i_sb->s_fs_info;
538*1e51764aSArtem Bityutskiy 	struct qstr nm = { .name = name, .len = strlen(name) };
539*1e51764aSArtem Bityutskiy 	struct ubifs_dent_node *xent;
540*1e51764aSArtem Bityutskiy 	union ubifs_key key;
541*1e51764aSArtem Bityutskiy 	int err;
542*1e51764aSArtem Bityutskiy 
543*1e51764aSArtem Bityutskiy 	dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
544*1e51764aSArtem Bityutskiy 		host->i_ino, dentry->d_name.len, dentry->d_name.name);
545*1e51764aSArtem Bityutskiy 	ubifs_assert(mutex_is_locked(&host->i_mutex));
546*1e51764aSArtem Bityutskiy 
547*1e51764aSArtem Bityutskiy 	err = check_namespace(&nm);
548*1e51764aSArtem Bityutskiy 	if (err < 0)
549*1e51764aSArtem Bityutskiy 		return err;
550*1e51764aSArtem Bityutskiy 
551*1e51764aSArtem Bityutskiy 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
552*1e51764aSArtem Bityutskiy 	if (!xent)
553*1e51764aSArtem Bityutskiy 		return -ENOMEM;
554*1e51764aSArtem Bityutskiy 
555*1e51764aSArtem Bityutskiy 	xent_key_init(c, &key, host->i_ino, &nm);
556*1e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
557*1e51764aSArtem Bityutskiy 	if (err) {
558*1e51764aSArtem Bityutskiy 		if (err == -ENOENT)
559*1e51764aSArtem Bityutskiy 			err = -ENODATA;
560*1e51764aSArtem Bityutskiy 		goto out_free;
561*1e51764aSArtem Bityutskiy 	}
562*1e51764aSArtem Bityutskiy 
563*1e51764aSArtem Bityutskiy 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
564*1e51764aSArtem Bityutskiy 	if (IS_ERR(inode)) {
565*1e51764aSArtem Bityutskiy 		err = PTR_ERR(inode);
566*1e51764aSArtem Bityutskiy 		goto out_free;
567*1e51764aSArtem Bityutskiy 	}
568*1e51764aSArtem Bityutskiy 
569*1e51764aSArtem Bityutskiy 	ubifs_assert(inode->i_nlink == 1);
570*1e51764aSArtem Bityutskiy 	inode->i_nlink = 0;
571*1e51764aSArtem Bityutskiy 	err = remove_xattr(c, host, inode, &nm);
572*1e51764aSArtem Bityutskiy 	if (err)
573*1e51764aSArtem Bityutskiy 		inode->i_nlink = 1;
574*1e51764aSArtem Bityutskiy 
575*1e51764aSArtem Bityutskiy 	/* If @i_nlink is 0, 'iput()' will delete the inode */
576*1e51764aSArtem Bityutskiy 	iput(inode);
577*1e51764aSArtem Bityutskiy 
578*1e51764aSArtem Bityutskiy out_free:
579*1e51764aSArtem Bityutskiy 	kfree(xent);
580*1e51764aSArtem Bityutskiy 	return err;
581*1e51764aSArtem Bityutskiy }
582