Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Authors: Artem Bityutskiy (Битюцкий Артём)
0008  *          Adrian Hunter
0009  */
0010 
0011 /*
0012  * This file implements UBIFS extended attributes support.
0013  *
0014  * Extended attributes are implemented as regular inodes with attached data,
0015  * which limits extended attribute size to UBIFS block size (4KiB). Names of
0016  * extended attributes are described by extended attribute entries (xentries),
0017  * which are almost identical to directory entries, but have different key type.
0018  *
0019  * In other words, the situation with extended attributes is very similar to
0020  * directories. Indeed, any inode (but of course not xattr inodes) may have a
0021  * number of associated xentries, just like directory inodes have associated
0022  * directory entries. Extended attribute entries store the name of the extended
0023  * attribute, the host inode number, and the extended attribute inode number.
0024  * Similarly, direntries store the name, the parent and the target inode
0025  * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
0026  * extended attributes.
0027  *
0028  * The number of extended attributes is not limited, but there is Linux
0029  * limitation on the maximum possible size of the list of all extended
0030  * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
0031  * the sum of all extended attribute names of the inode does not exceed that
0032  * limit.
0033  *
0034  * Extended attributes are synchronous, which means they are written to the
0035  * flash media synchronously and there is no write-back for extended attribute
0036  * inodes. The extended attribute values are not stored in compressed form on
0037  * the media.
0038  *
0039  * Since extended attributes are represented by regular inodes, they are cached
0040  * in the VFS inode cache. The xentries are cached in the LNC cache (see
0041  * tnc.c).
0042  *
0043  * ACL support is not implemented.
0044  */
0045 
0046 #include "ubifs.h"
0047 #include <linux/fs.h>
0048 #include <linux/slab.h>
0049 #include <linux/xattr.h>
0050 
0051 /*
0052  * Extended attribute type constants.
0053  *
0054  * USER_XATTR: user extended attribute ("user.*")
0055  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
0056  * SECURITY_XATTR: security extended attribute ("security.*")
0057  */
0058 enum {
0059     USER_XATTR,
0060     TRUSTED_XATTR,
0061     SECURITY_XATTR,
0062 };
0063 
0064 static const struct inode_operations empty_iops;
0065 static const struct file_operations empty_fops;
0066 
0067 /**
0068  * create_xattr - create an extended attribute.
0069  * @c: UBIFS file-system description object
0070  * @host: host inode
0071  * @nm: extended attribute name
0072  * @value: extended attribute value
0073  * @size: size of extended attribute value
0074  *
0075  * This is a helper function which creates an extended attribute of name @nm
0076  * and value @value for inode @host. The host inode is also updated on flash
0077  * because the ctime and extended attribute accounting data changes. This
0078  * function returns zero in case of success and a negative error code in case
0079  * of failure.
0080  */
0081 static int create_xattr(struct ubifs_info *c, struct inode *host,
0082             const struct fscrypt_name *nm, const void *value, int size)
0083 {
0084     int err, names_len;
0085     struct inode *inode;
0086     struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
0087     struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
0088                 .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
0089                 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
0090 
0091     if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) {
0092         ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
0093               host->i_ino, host_ui->xattr_cnt);
0094         return -ENOSPC;
0095     }
0096     /*
0097      * Linux limits the maximum size of the extended attribute names list
0098      * to %XATTR_LIST_MAX. This means we should not allow creating more
0099      * extended attributes if the name list becomes larger. This limitation
0100      * is artificial for UBIFS, though.
0101      */
0102     names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
0103     if (names_len > XATTR_LIST_MAX) {
0104         ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
0105               host->i_ino, names_len, XATTR_LIST_MAX);
0106         return -ENOSPC;
0107     }
0108 
0109     err = ubifs_budget_space(c, &req);
0110     if (err)
0111         return err;
0112 
0113     inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
0114     if (IS_ERR(inode)) {
0115         err = PTR_ERR(inode);
0116         goto out_budg;
0117     }
0118 
0119     /* Re-define all operations to be "nothing" */
0120     inode->i_mapping->a_ops = &empty_aops;
0121     inode->i_op = &empty_iops;
0122     inode->i_fop = &empty_fops;
0123 
0124     inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
0125     ui = ubifs_inode(inode);
0126     ui->xattr = 1;
0127     ui->flags |= UBIFS_XATTR_FL;
0128     ui->data = kmemdup(value, size, GFP_NOFS);
0129     if (!ui->data) {
0130         err = -ENOMEM;
0131         goto out_free;
0132     }
0133     inode->i_size = ui->ui_size = size;
0134     ui->data_len = size;
0135 
0136     mutex_lock(&host_ui->ui_mutex);
0137     host->i_ctime = current_time(host);
0138     host_ui->xattr_cnt += 1;
0139     host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
0140     host_ui->xattr_size += CALC_XATTR_BYTES(size);
0141     host_ui->xattr_names += fname_len(nm);
0142 
0143     /*
0144      * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
0145      * have to set the UBIFS_CRYPT_FL flag on the host inode.
0146      * To avoid multiple updates of the same inode in the same operation,
0147      * let's do it here.
0148      */
0149     if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
0150         host_ui->flags |= UBIFS_CRYPT_FL;
0151 
0152     err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
0153     if (err)
0154         goto out_cancel;
0155     ubifs_set_inode_flags(host);
0156     mutex_unlock(&host_ui->ui_mutex);
0157 
0158     ubifs_release_budget(c, &req);
0159     insert_inode_hash(inode);
0160     iput(inode);
0161     return 0;
0162 
0163 out_cancel:
0164     host_ui->xattr_cnt -= 1;
0165     host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
0166     host_ui->xattr_size -= CALC_XATTR_BYTES(size);
0167     host_ui->xattr_names -= fname_len(nm);
0168     host_ui->flags &= ~UBIFS_CRYPT_FL;
0169     mutex_unlock(&host_ui->ui_mutex);
0170 out_free:
0171     make_bad_inode(inode);
0172     iput(inode);
0173 out_budg:
0174     ubifs_release_budget(c, &req);
0175     return err;
0176 }
0177 
0178 /**
0179  * change_xattr - change an extended attribute.
0180  * @c: UBIFS file-system description object
0181  * @host: host inode
0182  * @inode: extended attribute inode
0183  * @value: extended attribute value
0184  * @size: size of extended attribute value
0185  *
0186  * This helper function changes the value of extended attribute @inode with new
0187  * data from @value. Returns zero in case of success and a negative error code
0188  * in case of failure.
0189  */
0190 static int change_xattr(struct ubifs_info *c, struct inode *host,
0191             struct inode *inode, const void *value, int size)
0192 {
0193     int err;
0194     struct ubifs_inode *host_ui = ubifs_inode(host);
0195     struct ubifs_inode *ui = ubifs_inode(inode);
0196     void *buf = NULL;
0197     int old_size;
0198     struct ubifs_budget_req req = { .dirtied_ino = 2,
0199         .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
0200 
0201     ubifs_assert(c, ui->data_len == inode->i_size);
0202     err = ubifs_budget_space(c, &req);
0203     if (err)
0204         return err;
0205 
0206     buf = kmemdup(value, size, GFP_NOFS);
0207     if (!buf) {
0208         err = -ENOMEM;
0209         goto out_free;
0210     }
0211     kfree(ui->data);
0212     ui->data = buf;
0213     inode->i_size = ui->ui_size = size;
0214     old_size = ui->data_len;
0215     ui->data_len = size;
0216 
0217     mutex_lock(&host_ui->ui_mutex);
0218     host->i_ctime = current_time(host);
0219     host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
0220     host_ui->xattr_size += CALC_XATTR_BYTES(size);
0221 
0222     /*
0223      * It is important to write the host inode after the xattr inode
0224      * because if the host inode gets synchronized (via 'fsync()'), then
0225      * the extended attribute inode gets synchronized, because it goes
0226      * before the host inode in the write-buffer.
0227      */
0228     err = ubifs_jnl_change_xattr(c, inode, host);
0229     if (err)
0230         goto out_cancel;
0231     mutex_unlock(&host_ui->ui_mutex);
0232 
0233     ubifs_release_budget(c, &req);
0234     return 0;
0235 
0236 out_cancel:
0237     host_ui->xattr_size -= CALC_XATTR_BYTES(size);
0238     host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
0239     mutex_unlock(&host_ui->ui_mutex);
0240     make_bad_inode(inode);
0241 out_free:
0242     ubifs_release_budget(c, &req);
0243     return err;
0244 }
0245 
0246 static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
0247 {
0248     struct inode *inode;
0249 
0250     inode = ubifs_iget(c->vfs_sb, inum);
0251     if (IS_ERR(inode)) {
0252         ubifs_err(c, "dead extended attribute entry, error %d",
0253               (int)PTR_ERR(inode));
0254         return inode;
0255     }
0256     if (ubifs_inode(inode)->xattr)
0257         return inode;
0258     ubifs_err(c, "corrupt extended attribute entry");
0259     iput(inode);
0260     return ERR_PTR(-EINVAL);
0261 }
0262 
0263 int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
0264             size_t size, int flags, bool check_lock)
0265 {
0266     struct inode *inode;
0267     struct ubifs_info *c = host->i_sb->s_fs_info;
0268     struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
0269     struct ubifs_dent_node *xent;
0270     union ubifs_key key;
0271     int err;
0272 
0273     if (check_lock)
0274         ubifs_assert(c, inode_is_locked(host));
0275 
0276     if (size > UBIFS_MAX_INO_DATA)
0277         return -ERANGE;
0278 
0279     if (fname_len(&nm) > UBIFS_MAX_NLEN)
0280         return -ENAMETOOLONG;
0281 
0282     xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
0283     if (!xent)
0284         return -ENOMEM;
0285 
0286     down_write(&ubifs_inode(host)->xattr_sem);
0287     /*
0288      * The extended attribute entries are stored in LNC, so multiple
0289      * look-ups do not involve reading the flash.
0290      */
0291     xent_key_init(c, &key, host->i_ino, &nm);
0292     err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
0293     if (err) {
0294         if (err != -ENOENT)
0295             goto out_free;
0296 
0297         if (flags & XATTR_REPLACE)
0298             /* We are asked not to create the xattr */
0299             err = -ENODATA;
0300         else
0301             err = create_xattr(c, host, &nm, value, size);
0302         goto out_free;
0303     }
0304 
0305     if (flags & XATTR_CREATE) {
0306         /* We are asked not to replace the xattr */
0307         err = -EEXIST;
0308         goto out_free;
0309     }
0310 
0311     inode = iget_xattr(c, le64_to_cpu(xent->inum));
0312     if (IS_ERR(inode)) {
0313         err = PTR_ERR(inode);
0314         goto out_free;
0315     }
0316 
0317     err = change_xattr(c, host, inode, value, size);
0318     iput(inode);
0319 
0320 out_free:
0321     up_write(&ubifs_inode(host)->xattr_sem);
0322     kfree(xent);
0323     return err;
0324 }
0325 
0326 ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
0327             size_t size)
0328 {
0329     struct inode *inode;
0330     struct ubifs_info *c = host->i_sb->s_fs_info;
0331     struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
0332     struct ubifs_inode *ui;
0333     struct ubifs_dent_node *xent;
0334     union ubifs_key key;
0335     int err;
0336 
0337     if (fname_len(&nm) > UBIFS_MAX_NLEN)
0338         return -ENAMETOOLONG;
0339 
0340     xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
0341     if (!xent)
0342         return -ENOMEM;
0343 
0344     down_read(&ubifs_inode(host)->xattr_sem);
0345     xent_key_init(c, &key, host->i_ino, &nm);
0346     err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
0347     if (err) {
0348         if (err == -ENOENT)
0349             err = -ENODATA;
0350         goto out_cleanup;
0351     }
0352 
0353     inode = iget_xattr(c, le64_to_cpu(xent->inum));
0354     if (IS_ERR(inode)) {
0355         err = PTR_ERR(inode);
0356         goto out_cleanup;
0357     }
0358 
0359     ui = ubifs_inode(inode);
0360     ubifs_assert(c, inode->i_size == ui->data_len);
0361     ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
0362 
0363     if (buf) {
0364         /* If @buf is %NULL we are supposed to return the length */
0365         if (ui->data_len > size) {
0366             err = -ERANGE;
0367             goto out_iput;
0368         }
0369 
0370         memcpy(buf, ui->data, ui->data_len);
0371     }
0372     err = ui->data_len;
0373 
0374 out_iput:
0375     iput(inode);
0376 out_cleanup:
0377     up_read(&ubifs_inode(host)->xattr_sem);
0378     kfree(xent);
0379     return err;
0380 }
0381 
0382 static bool xattr_visible(const char *name)
0383 {
0384     /* File encryption related xattrs are for internal use only */
0385     if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
0386         return false;
0387 
0388     /* Show trusted namespace only for "power" users */
0389     if (strncmp(name, XATTR_TRUSTED_PREFIX,
0390             XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
0391         return false;
0392 
0393     return true;
0394 }
0395 
0396 ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
0397 {
0398     union ubifs_key key;
0399     struct inode *host = d_inode(dentry);
0400     struct ubifs_info *c = host->i_sb->s_fs_info;
0401     struct ubifs_inode *host_ui = ubifs_inode(host);
0402     struct ubifs_dent_node *xent, *pxent = NULL;
0403     int err, len, written = 0;
0404     struct fscrypt_name nm = {0};
0405 
0406     dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
0407         dentry, size);
0408 
0409     down_read(&host_ui->xattr_sem);
0410     len = host_ui->xattr_names + host_ui->xattr_cnt;
0411     if (!buffer) {
0412         /*
0413          * We should return the minimum buffer size which will fit a
0414          * null-terminated list of all the extended attribute names.
0415          */
0416         err = len;
0417         goto out_err;
0418     }
0419 
0420     if (len > size) {
0421         err = -ERANGE;
0422         goto out_err;
0423     }
0424 
0425     lowest_xent_key(c, &key, host->i_ino);
0426     while (1) {
0427         xent = ubifs_tnc_next_ent(c, &key, &nm);
0428         if (IS_ERR(xent)) {
0429             err = PTR_ERR(xent);
0430             break;
0431         }
0432 
0433         fname_name(&nm) = xent->name;
0434         fname_len(&nm) = le16_to_cpu(xent->nlen);
0435 
0436         if (xattr_visible(xent->name)) {
0437             memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
0438             written += fname_len(&nm) + 1;
0439         }
0440 
0441         kfree(pxent);
0442         pxent = xent;
0443         key_read(c, &xent->key, &key);
0444     }
0445     kfree(pxent);
0446     up_read(&host_ui->xattr_sem);
0447 
0448     if (err != -ENOENT) {
0449         ubifs_err(c, "cannot find next direntry, error %d", err);
0450         return err;
0451     }
0452 
0453     ubifs_assert(c, written <= size);
0454     return written;
0455 
0456 out_err:
0457     up_read(&host_ui->xattr_sem);
0458     return err;
0459 }
0460 
0461 static int remove_xattr(struct ubifs_info *c, struct inode *host,
0462             struct inode *inode, const struct fscrypt_name *nm)
0463 {
0464     int err;
0465     struct ubifs_inode *host_ui = ubifs_inode(host);
0466     struct ubifs_inode *ui = ubifs_inode(inode);
0467     struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
0468                 .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
0469 
0470     ubifs_assert(c, ui->data_len == inode->i_size);
0471 
0472     err = ubifs_budget_space(c, &req);
0473     if (err)
0474         return err;
0475 
0476     mutex_lock(&host_ui->ui_mutex);
0477     host->i_ctime = current_time(host);
0478     host_ui->xattr_cnt -= 1;
0479     host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
0480     host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
0481     host_ui->xattr_names -= fname_len(nm);
0482 
0483     err = ubifs_jnl_delete_xattr(c, host, inode, nm);
0484     if (err)
0485         goto out_cancel;
0486     mutex_unlock(&host_ui->ui_mutex);
0487 
0488     ubifs_release_budget(c, &req);
0489     return 0;
0490 
0491 out_cancel:
0492     host_ui->xattr_cnt += 1;
0493     host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
0494     host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
0495     host_ui->xattr_names += fname_len(nm);
0496     mutex_unlock(&host_ui->ui_mutex);
0497     ubifs_release_budget(c, &req);
0498     make_bad_inode(inode);
0499     return err;
0500 }
0501 
0502 int ubifs_purge_xattrs(struct inode *host)
0503 {
0504     union ubifs_key key;
0505     struct ubifs_info *c = host->i_sb->s_fs_info;
0506     struct ubifs_dent_node *xent, *pxent = NULL;
0507     struct inode *xino;
0508     struct fscrypt_name nm = {0};
0509     int err;
0510 
0511     if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c))
0512         return 0;
0513 
0514     ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion",
0515            host->i_ino);
0516 
0517     down_write(&ubifs_inode(host)->xattr_sem);
0518     lowest_xent_key(c, &key, host->i_ino);
0519     while (1) {
0520         xent = ubifs_tnc_next_ent(c, &key, &nm);
0521         if (IS_ERR(xent)) {
0522             err = PTR_ERR(xent);
0523             break;
0524         }
0525 
0526         fname_name(&nm) = xent->name;
0527         fname_len(&nm) = le16_to_cpu(xent->nlen);
0528 
0529         xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
0530         if (IS_ERR(xino)) {
0531             err = PTR_ERR(xino);
0532             ubifs_err(c, "dead directory entry '%s', error %d",
0533                   xent->name, err);
0534             ubifs_ro_mode(c, err);
0535             kfree(pxent);
0536             kfree(xent);
0537             goto out_err;
0538         }
0539 
0540         ubifs_assert(c, ubifs_inode(xino)->xattr);
0541 
0542         clear_nlink(xino);
0543         err = remove_xattr(c, host, xino, &nm);
0544         if (err) {
0545             kfree(pxent);
0546             kfree(xent);
0547             iput(xino);
0548             ubifs_err(c, "cannot remove xattr, error %d", err);
0549             goto out_err;
0550         }
0551 
0552         iput(xino);
0553 
0554         kfree(pxent);
0555         pxent = xent;
0556         key_read(c, &xent->key, &key);
0557     }
0558     kfree(pxent);
0559     up_write(&ubifs_inode(host)->xattr_sem);
0560 
0561     if (err != -ENOENT) {
0562         ubifs_err(c, "cannot find next direntry, error %d", err);
0563         return err;
0564     }
0565 
0566     return 0;
0567 
0568 out_err:
0569     up_write(&ubifs_inode(host)->xattr_sem);
0570     return err;
0571 }
0572 
0573 /**
0574  * ubifs_evict_xattr_inode - Evict an xattr inode.
0575  * @c: UBIFS file-system description object
0576  * @xattr_inum: xattr inode number
0577  *
0578  * When an inode that hosts xattrs is being removed we have to make sure
0579  * that cached inodes of the xattrs also get removed from the inode cache
0580  * otherwise we'd waste memory. This function looks up an inode from the
0581  * inode cache and clears the link counter such that iput() will evict
0582  * the inode.
0583  */
0584 void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
0585 {
0586     struct inode *inode;
0587 
0588     inode = ilookup(c->vfs_sb, xattr_inum);
0589     if (inode) {
0590         clear_nlink(inode);
0591         iput(inode);
0592     }
0593 }
0594 
0595 static int ubifs_xattr_remove(struct inode *host, const char *name)
0596 {
0597     struct inode *inode;
0598     struct ubifs_info *c = host->i_sb->s_fs_info;
0599     struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
0600     struct ubifs_dent_node *xent;
0601     union ubifs_key key;
0602     int err;
0603 
0604     ubifs_assert(c, inode_is_locked(host));
0605 
0606     if (fname_len(&nm) > UBIFS_MAX_NLEN)
0607         return -ENAMETOOLONG;
0608 
0609     xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
0610     if (!xent)
0611         return -ENOMEM;
0612 
0613     down_write(&ubifs_inode(host)->xattr_sem);
0614     xent_key_init(c, &key, host->i_ino, &nm);
0615     err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
0616     if (err) {
0617         if (err == -ENOENT)
0618             err = -ENODATA;
0619         goto out_free;
0620     }
0621 
0622     inode = iget_xattr(c, le64_to_cpu(xent->inum));
0623     if (IS_ERR(inode)) {
0624         err = PTR_ERR(inode);
0625         goto out_free;
0626     }
0627 
0628     ubifs_assert(c, inode->i_nlink == 1);
0629     clear_nlink(inode);
0630     err = remove_xattr(c, host, inode, &nm);
0631     if (err)
0632         set_nlink(inode, 1);
0633 
0634     /* If @i_nlink is 0, 'iput()' will delete the inode */
0635     iput(inode);
0636 
0637 out_free:
0638     up_write(&ubifs_inode(host)->xattr_sem);
0639     kfree(xent);
0640     return err;
0641 }
0642 
0643 #ifdef CONFIG_UBIFS_FS_SECURITY
0644 static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
0645               void *fs_info)
0646 {
0647     const struct xattr *xattr;
0648     char *name;
0649     int err = 0;
0650 
0651     for (xattr = xattr_array; xattr->name != NULL; xattr++) {
0652         name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
0653                    strlen(xattr->name) + 1, GFP_NOFS);
0654         if (!name) {
0655             err = -ENOMEM;
0656             break;
0657         }
0658         strcpy(name, XATTR_SECURITY_PREFIX);
0659         strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
0660         /*
0661          * creating a new inode without holding the inode rwsem,
0662          * no need to check whether inode is locked.
0663          */
0664         err = ubifs_xattr_set(inode, name, xattr->value,
0665                       xattr->value_len, 0, false);
0666         kfree(name);
0667         if (err < 0)
0668             break;
0669     }
0670 
0671     return err;
0672 }
0673 
0674 int ubifs_init_security(struct inode *dentry, struct inode *inode,
0675             const struct qstr *qstr)
0676 {
0677     int err;
0678 
0679     err = security_inode_init_security(inode, dentry, qstr,
0680                        &init_xattrs, NULL);
0681     if (err) {
0682         struct ubifs_info *c = dentry->i_sb->s_fs_info;
0683         ubifs_err(c, "cannot initialize security for inode %lu, error %d",
0684               inode->i_ino, err);
0685     }
0686     return err;
0687 }
0688 #endif
0689 
0690 static int xattr_get(const struct xattr_handler *handler,
0691                struct dentry *dentry, struct inode *inode,
0692                const char *name, void *buffer, size_t size)
0693 {
0694     dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
0695         inode->i_ino, dentry, size);
0696 
0697     name = xattr_full_name(handler, name);
0698     return ubifs_xattr_get(inode, name, buffer, size);
0699 }
0700 
0701 static int xattr_set(const struct xattr_handler *handler,
0702                struct user_namespace *mnt_userns,
0703                struct dentry *dentry, struct inode *inode,
0704                const char *name, const void *value,
0705                size_t size, int flags)
0706 {
0707     dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
0708         name, inode->i_ino, dentry, size);
0709 
0710     name = xattr_full_name(handler, name);
0711 
0712     if (value)
0713         return ubifs_xattr_set(inode, name, value, size, flags, true);
0714     else
0715         return ubifs_xattr_remove(inode, name);
0716 }
0717 
0718 static const struct xattr_handler ubifs_user_xattr_handler = {
0719     .prefix = XATTR_USER_PREFIX,
0720     .get = xattr_get,
0721     .set = xattr_set,
0722 };
0723 
0724 static const struct xattr_handler ubifs_trusted_xattr_handler = {
0725     .prefix = XATTR_TRUSTED_PREFIX,
0726     .get = xattr_get,
0727     .set = xattr_set,
0728 };
0729 
0730 #ifdef CONFIG_UBIFS_FS_SECURITY
0731 static const struct xattr_handler ubifs_security_xattr_handler = {
0732     .prefix = XATTR_SECURITY_PREFIX,
0733     .get = xattr_get,
0734     .set = xattr_set,
0735 };
0736 #endif
0737 
0738 const struct xattr_handler *ubifs_xattr_handlers[] = {
0739     &ubifs_user_xattr_handler,
0740     &ubifs_trusted_xattr_handler,
0741 #ifdef CONFIG_UBIFS_FS_SECURITY
0742     &ubifs_security_xattr_handler,
0743 #endif
0744     NULL
0745 };