blob: 2bdab8b11f3ffed41c5f20984121eb7fd81818ce [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS extended attributes support.
25 *
26 * Extended attributes are implemented as regular inodes with attached data,
27 * which limits extended attribute size to UBIFS block size (4KiB). Names of
28 * extended attributes are described by extended attribute entries (xentries),
29 * which are almost identical to directory entries, but have different key type.
30 *
31 * In other words, the situation with extended attributes is very similar to
32 * directories. Indeed, any inode (but of course not xattr inodes) may have a
33 * number of associated xentries, just like directory inodes have associated
34 * directory entries. Extended attribute entries store the name of the extended
35 * attribute, the host inode number, and the extended attribute inode number.
36 * Similarly, direntries store the name, the parent and the target inode
37 * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
38 * extended attributes.
39 *
40 * The number of extended attributes is not limited, but there is Linux
41 * limitation on the maximum possible size of the list of all extended
42 * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
43 * the sum of all extended attribute names of the inode does not exceed that
44 * limit.
45 *
46 * Extended attributes are synchronous, which means they are written to the
47 * flash media synchronously and there is no write-back for extended attribute
48 * inodes. The extended attribute values are not stored in compressed form on
49 * the media.
50 *
51 * Since extended attributes are represented by regular inodes, they are cached
52 * in the VFS inode cache. The xentries are cached in the LNC cache (see
53 * tnc.c).
54 *
55 * ACL support is not implemented.
56 */
57
Al Viro073aaa12009-06-09 12:11:54 -040058#include "ubifs.h"
Jens Axboe7dcda1c2011-04-05 23:51:48 +020059#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030061#include <linux/xattr.h>
62#include <linux/posix_acl_xattr.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030063
64/*
65 * Limit the number of extended attributes per inode so that the total size
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +030066 * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030067 */
68#define MAX_XATTRS_PER_INODE 65535
69
70/*
71 * Extended attribute type constants.
72 *
73 * USER_XATTR: user extended attribute ("user.*")
74 * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
75 * SECURITY_XATTR: security extended attribute ("security.*")
76 */
77enum {
78 USER_XATTR,
79 TRUSTED_XATTR,
80 SECURITY_XATTR,
81};
82
Sedat Dilek14ffd5d2011-04-04 01:55:39 +020083static const struct inode_operations empty_iops;
84static const struct file_operations empty_fops;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030085
86/**
87 * create_xattr - create an extended attribute.
88 * @c: UBIFS file-system description object
89 * @host: host inode
90 * @nm: extended attribute name
91 * @value: extended attribute value
92 * @size: size of extended attribute value
93 *
94 * This is a helper function which creates an extended attribute of name @nm
95 * and value @value for inode @host. The host inode is also updated on flash
96 * because the ctime and extended attribute accounting data changes. This
97 * function returns zero in case of success and a negative error code in case
98 * of failure.
99 */
100static int create_xattr(struct ubifs_info *c, struct inode *host,
101 const struct qstr *nm, const void *value, int size)
102{
103 int err;
104 struct inode *inode;
105 struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
106 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Zoltan Sogor5acd6ff2008-08-12 13:54:54 +0300107 .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
108 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300109
110 if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
111 return -ENOSPC;
112 /*
113 * Linux limits the maximum size of the extended attribute names list
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300114 * to %XATTR_LIST_MAX. This means we should not allow creating more
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300115 * extended attributes if the name list becomes larger. This limitation
116 * is artificial for UBIFS, though.
117 */
118 if (host_ui->xattr_names + host_ui->xattr_cnt +
119 nm->len + 1 > XATTR_LIST_MAX)
120 return -ENOSPC;
121
122 err = ubifs_budget_space(c, &req);
123 if (err)
124 return err;
125
126 inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
127 if (IS_ERR(inode)) {
128 err = PTR_ERR(inode);
129 goto out_budg;
130 }
131
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300132 /* Re-define all operations to be "nothing" */
Jens Axboe7dcda1c2011-04-05 23:51:48 +0200133 inode->i_mapping->a_ops = &empty_aops;
Sedat Dilek14ffd5d2011-04-04 01:55:39 +0200134 inode->i_op = &empty_iops;
135 inode->i_fop = &empty_fops;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300136
137 inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
138 ui = ubifs_inode(inode);
139 ui->xattr = 1;
140 ui->flags |= UBIFS_XATTR_FL;
Thomas Meyereaecf432011-11-18 00:00:52 +0100141 ui->data = kmemdup(value, size, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300142 if (!ui->data) {
143 err = -ENOMEM;
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300144 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300145 }
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300146 inode->i_size = ui->ui_size = size;
147 ui->data_len = size;
148
149 mutex_lock(&host_ui->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300150 host->i_ctime = ubifs_current_time(host);
151 host_ui->xattr_cnt += 1;
152 host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
153 host_ui->xattr_size += CALC_XATTR_BYTES(size);
154 host_ui->xattr_names += nm->len;
155
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300156 err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
157 if (err)
158 goto out_cancel;
159 mutex_unlock(&host_ui->ui_mutex);
160
161 ubifs_release_budget(c, &req);
162 insert_inode_hash(inode);
163 iput(inode);
164 return 0;
165
166out_cancel:
167 host_ui->xattr_cnt -= 1;
168 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
169 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300170 mutex_unlock(&host_ui->ui_mutex);
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300171out_free:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300172 make_bad_inode(inode);
173 iput(inode);
174out_budg:
175 ubifs_release_budget(c, &req);
176 return err;
177}
178
179/**
180 * change_xattr - change an extended attribute.
181 * @c: UBIFS file-system description object
182 * @host: host inode
183 * @inode: extended attribute inode
184 * @value: extended attribute value
185 * @size: size of extended attribute value
186 *
187 * This helper function changes the value of extended attribute @inode with new
188 * data from @value. Returns zero in case of success and a negative error code
189 * in case of failure.
190 */
191static int change_xattr(struct ubifs_info *c, struct inode *host,
192 struct inode *inode, const void *value, int size)
193{
194 int err;
195 struct ubifs_inode *host_ui = ubifs_inode(host);
196 struct ubifs_inode *ui = ubifs_inode(inode);
197 struct ubifs_budget_req req = { .dirtied_ino = 2,
Zoltan Sogor5acd6ff2008-08-12 13:54:54 +0300198 .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300199
200 ubifs_assert(ui->data_len == inode->i_size);
201 err = ubifs_budget_space(c, &req);
202 if (err)
203 return err;
204
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300205 kfree(ui->data);
Thomas Meyereaecf432011-11-18 00:00:52 +0100206 ui->data = kmemdup(value, size, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300207 if (!ui->data) {
208 err = -ENOMEM;
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300209 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300210 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300211 inode->i_size = ui->ui_size = size;
212 ui->data_len = size;
213
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300214 mutex_lock(&host_ui->ui_mutex);
215 host->i_ctime = ubifs_current_time(host);
216 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
217 host_ui->xattr_size += CALC_XATTR_BYTES(size);
218
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300219 /*
220 * It is important to write the host inode after the xattr inode
221 * because if the host inode gets synchronized (via 'fsync()'), then
222 * the extended attribute inode gets synchronized, because it goes
223 * before the host inode in the write-buffer.
224 */
225 err = ubifs_jnl_change_xattr(c, inode, host);
226 if (err)
227 goto out_cancel;
228 mutex_unlock(&host_ui->ui_mutex);
229
230 ubifs_release_budget(c, &req);
231 return 0;
232
233out_cancel:
234 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
235 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300236 mutex_unlock(&host_ui->ui_mutex);
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300237 make_bad_inode(inode);
238out_free:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300239 ubifs_release_budget(c, &req);
240 return err;
241}
242
243/**
244 * check_namespace - check extended attribute name-space.
245 * @nm: extended attribute name
246 *
247 * This function makes sure the extended attribute name belongs to one of the
248 * supported extended attribute name-spaces. Returns name-space index in case
249 * of success and a negative error code in case of failure.
250 */
251static int check_namespace(const struct qstr *nm)
252{
253 int type;
254
255 if (nm->len > UBIFS_MAX_NLEN)
256 return -ENAMETOOLONG;
257
258 if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
259 XATTR_TRUSTED_PREFIX_LEN)) {
260 if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
261 return -EINVAL;
262 type = TRUSTED_XATTR;
263 } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
264 XATTR_USER_PREFIX_LEN)) {
265 if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
266 return -EINVAL;
267 type = USER_XATTR;
268 } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
269 XATTR_SECURITY_PREFIX_LEN)) {
270 if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
271 return -EINVAL;
272 type = SECURITY_XATTR;
273 } else
274 return -EOPNOTSUPP;
275
276 return type;
277}
278
279static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
280{
281 struct inode *inode;
282
283 inode = ubifs_iget(c->vfs_sb, inum);
284 if (IS_ERR(inode)) {
285 ubifs_err("dead extended attribute entry, error %d",
286 (int)PTR_ERR(inode));
287 return inode;
288 }
289 if (ubifs_inode(inode)->xattr)
290 return inode;
291 ubifs_err("corrupt extended attribute entry");
292 iput(inode);
293 return ERR_PTR(-EINVAL);
294}
295
Subodh Nijsure895d9db2014-10-31 13:50:29 -0500296static int setxattr(struct inode *host, const char *name, const void *value,
297 size_t size, int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300298{
Subodh Nijsure895d9db2014-10-31 13:50:29 -0500299 struct inode *inode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300300 struct ubifs_info *c = host->i_sb->s_fs_info;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700301 struct qstr nm = QSTR_INIT(name, strlen(name));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300302 struct ubifs_dent_node *xent;
303 union ubifs_key key;
304 int err, type;
305
Artem Bityutskiyc78c7e32008-08-12 16:30:12 +0300306 ubifs_assert(mutex_is_locked(&host->i_mutex));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307
308 if (size > UBIFS_MAX_INO_DATA)
309 return -ERANGE;
310
311 type = check_namespace(&nm);
312 if (type < 0)
313 return type;
314
315 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
316 if (!xent)
317 return -ENOMEM;
318
319 /*
320 * The extended attribute entries are stored in LNC, so multiple
321 * look-ups do not involve reading the flash.
322 */
323 xent_key_init(c, &key, host->i_ino, &nm);
324 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
325 if (err) {
326 if (err != -ENOENT)
327 goto out_free;
328
329 if (flags & XATTR_REPLACE)
330 /* We are asked not to create the xattr */
331 err = -ENODATA;
332 else
333 err = create_xattr(c, host, &nm, value, size);
334 goto out_free;
335 }
336
337 if (flags & XATTR_CREATE) {
338 /* We are asked not to replace the xattr */
339 err = -EEXIST;
340 goto out_free;
341 }
342
343 inode = iget_xattr(c, le64_to_cpu(xent->inum));
344 if (IS_ERR(inode)) {
345 err = PTR_ERR(inode);
346 goto out_free;
347 }
348
349 err = change_xattr(c, host, inode, value, size);
350 iput(inode);
351
352out_free:
353 kfree(xent);
354 return err;
355}
356
Subodh Nijsure895d9db2014-10-31 13:50:29 -0500357int ubifs_setxattr(struct dentry *dentry, const char *name,
358 const void *value, size_t size, int flags)
359{
360 dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
361 name, dentry->d_inode->i_ino, dentry, size);
362
363 return setxattr(dentry->d_inode, name, value, size, flags);
364}
365
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300366ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
367 size_t size)
368{
369 struct inode *inode, *host = dentry->d_inode;
370 struct ubifs_info *c = host->i_sb->s_fs_info;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700371 struct qstr nm = QSTR_INIT(name, strlen(name));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300372 struct ubifs_inode *ui;
373 struct ubifs_dent_node *xent;
374 union ubifs_key key;
375 int err;
376
Al Viro4cb2a012013-09-16 10:58:53 -0400377 dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
378 host->i_ino, dentry, size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300379
380 err = check_namespace(&nm);
381 if (err < 0)
382 return err;
383
384 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
385 if (!xent)
386 return -ENOMEM;
387
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300388 xent_key_init(c, &key, host->i_ino, &nm);
389 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
390 if (err) {
391 if (err == -ENOENT)
392 err = -ENODATA;
393 goto out_unlock;
394 }
395
396 inode = iget_xattr(c, le64_to_cpu(xent->inum));
397 if (IS_ERR(inode)) {
398 err = PTR_ERR(inode);
399 goto out_unlock;
400 }
401
402 ui = ubifs_inode(inode);
403 ubifs_assert(inode->i_size == ui->data_len);
404 ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
405
406 if (buf) {
407 /* If @buf is %NULL we are supposed to return the length */
408 if (ui->data_len > size) {
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300409 ubifs_err("buffer size %zd, xattr len %d",
410 size, ui->data_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300411 err = -ERANGE;
412 goto out_iput;
413 }
414
415 memcpy(buf, ui->data, ui->data_len);
416 }
417 err = ui->data_len;
418
419out_iput:
420 iput(inode);
421out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300422 kfree(xent);
423 return err;
424}
425
426ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
427{
428 union ubifs_key key;
429 struct inode *host = dentry->d_inode;
430 struct ubifs_info *c = host->i_sb->s_fs_info;
431 struct ubifs_inode *host_ui = ubifs_inode(host);
432 struct ubifs_dent_node *xent, *pxent = NULL;
433 int err, len, written = 0;
434 struct qstr nm = { .name = NULL };
435
Al Viro4cb2a012013-09-16 10:58:53 -0400436 dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
437 dentry, size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300438
439 len = host_ui->xattr_names + host_ui->xattr_cnt;
440 if (!buffer)
441 /*
442 * We should return the minimum buffer size which will fit a
443 * null-terminated list of all the extended attribute names.
444 */
445 return len;
446
447 if (len > size)
448 return -ERANGE;
449
450 lowest_xent_key(c, &key, host->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300451 while (1) {
452 int type;
453
454 xent = ubifs_tnc_next_ent(c, &key, &nm);
Hirofumi Nakagawa8d47aef2008-08-21 17:16:40 +0300455 if (IS_ERR(xent)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300456 err = PTR_ERR(xent);
457 break;
458 }
459
460 nm.name = xent->name;
461 nm.len = le16_to_cpu(xent->nlen);
462
463 type = check_namespace(&nm);
464 if (unlikely(type < 0)) {
465 err = type;
466 break;
467 }
468
469 /* Show trusted namespace only for "power" users */
470 if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
471 memcpy(buffer + written, nm.name, nm.len + 1);
472 written += nm.len + 1;
473 }
474
475 kfree(pxent);
476 pxent = xent;
477 key_read(c, &xent->key, &key);
478 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300479
480 kfree(pxent);
481 if (err != -ENOENT) {
482 ubifs_err("cannot find next direntry, error %d", err);
483 return err;
484 }
485
486 ubifs_assert(written <= size);
487 return written;
488}
489
490static int remove_xattr(struct ubifs_info *c, struct inode *host,
491 struct inode *inode, const struct qstr *nm)
492{
493 int err;
494 struct ubifs_inode *host_ui = ubifs_inode(host);
495 struct ubifs_inode *ui = ubifs_inode(inode);
Zoltan Sogor5acd6ff2008-08-12 13:54:54 +0300496 struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
497 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300498
499 ubifs_assert(ui->data_len == inode->i_size);
500
501 err = ubifs_budget_space(c, &req);
502 if (err)
503 return err;
504
505 mutex_lock(&host_ui->ui_mutex);
506 host->i_ctime = ubifs_current_time(host);
507 host_ui->xattr_cnt -= 1;
508 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
509 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
510 host_ui->xattr_names -= nm->len;
511
512 err = ubifs_jnl_delete_xattr(c, host, inode, nm);
513 if (err)
514 goto out_cancel;
515 mutex_unlock(&host_ui->ui_mutex);
516
517 ubifs_release_budget(c, &req);
518 return 0;
519
520out_cancel:
521 host_ui->xattr_cnt += 1;
522 host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
523 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
524 mutex_unlock(&host_ui->ui_mutex);
525 ubifs_release_budget(c, &req);
526 make_bad_inode(inode);
527 return err;
528}
529
530int ubifs_removexattr(struct dentry *dentry, const char *name)
531{
532 struct inode *inode, *host = dentry->d_inode;
533 struct ubifs_info *c = host->i_sb->s_fs_info;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700534 struct qstr nm = QSTR_INIT(name, strlen(name));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300535 struct ubifs_dent_node *xent;
536 union ubifs_key key;
537 int err;
538
Al Viro4cb2a012013-09-16 10:58:53 -0400539 dbg_gen("xattr '%s', ino %lu ('%pd')", name,
540 host->i_ino, dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300541 ubifs_assert(mutex_is_locked(&host->i_mutex));
542
543 err = check_namespace(&nm);
544 if (err < 0)
545 return err;
546
547 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
548 if (!xent)
549 return -ENOMEM;
550
551 xent_key_init(c, &key, host->i_ino, &nm);
552 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
553 if (err) {
554 if (err == -ENOENT)
555 err = -ENODATA;
556 goto out_free;
557 }
558
559 inode = iget_xattr(c, le64_to_cpu(xent->inum));
560 if (IS_ERR(inode)) {
561 err = PTR_ERR(inode);
562 goto out_free;
563 }
564
565 ubifs_assert(inode->i_nlink == 1);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200566 clear_nlink(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300567 err = remove_xattr(c, host, inode, &nm);
568 if (err)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200569 set_nlink(inode, 1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300570
571 /* If @i_nlink is 0, 'iput()' will delete the inode */
572 iput(inode);
573
574out_free:
575 kfree(xent);
576 return err;
577}
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500578
579static size_t security_listxattr(struct dentry *d, char *list, size_t list_size,
580 const char *name, size_t name_len, int flags)
581{
582 const int prefix_len = XATTR_SECURITY_PREFIX_LEN;
583 const size_t total_len = prefix_len + name_len + 1;
584
585 if (list && total_len <= list_size) {
586 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
587 memcpy(list + prefix_len, name, name_len);
588 list[prefix_len + name_len] = '\0';
589 }
590
591 return total_len;
592}
593
594static int security_getxattr(struct dentry *d, const char *name, void *buffer,
595 size_t size, int flags)
596{
597 return ubifs_getxattr(d, name, buffer, size);
598}
599
600static int security_setxattr(struct dentry *d, const char *name,
601 const void *value, size_t size, int flags,
602 int handler_flags)
603{
604 return ubifs_setxattr(d, name, value, size, flags);
605}
606
607static const struct xattr_handler ubifs_xattr_security_handler = {
608 .prefix = XATTR_SECURITY_PREFIX,
609 .list = security_listxattr,
610 .get = security_getxattr,
611 .set = security_setxattr,
612};
613
614const struct xattr_handler *ubifs_xattr_handlers[] = {
615 &ubifs_xattr_security_handler,
616 NULL,
617};
618
619static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
620 void *fs_info)
621{
622 const struct xattr *xattr;
623 char *name;
624 int err = 0;
625
626 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
627 name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
628 strlen(xattr->name) + 1, GFP_NOFS);
629 if (!name) {
630 err = -ENOMEM;
631 break;
632 }
633 strcpy(name, XATTR_SECURITY_PREFIX);
634 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
635 err = setxattr(inode, name, xattr->value, xattr->value_len, 0);
636 kfree(name);
637 if (err < 0)
638 break;
639 }
640
641 return err;
642}
643
644int ubifs_init_security(struct inode *dentry, struct inode *inode,
645 const struct qstr *qstr)
646{
647 int err;
648
649 mutex_lock(&inode->i_mutex);
650 err = security_inode_init_security(inode, dentry, qstr,
651 &init_xattrs, 0);
652 mutex_unlock(&inode->i_mutex);
653
654 return err;
655}