12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21e51764aSArtem Bityutskiy /* 31e51764aSArtem Bityutskiy * This file is part of UBIFS. 41e51764aSArtem Bityutskiy * 51e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation. 61e51764aSArtem Bityutskiy * 71e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 81e51764aSArtem Bityutskiy * Adrian Hunter 91e51764aSArtem Bityutskiy */ 101e51764aSArtem Bityutskiy 111e51764aSArtem Bityutskiy /* 121e51764aSArtem Bityutskiy * This file implements UBIFS extended attributes support. 131e51764aSArtem Bityutskiy * 141e51764aSArtem Bityutskiy * Extended attributes are implemented as regular inodes with attached data, 151e51764aSArtem Bityutskiy * which limits extended attribute size to UBIFS block size (4KiB). Names of 161e51764aSArtem Bityutskiy * extended attributes are described by extended attribute entries (xentries), 171e51764aSArtem Bityutskiy * which are almost identical to directory entries, but have different key type. 181e51764aSArtem Bityutskiy * 191e51764aSArtem Bityutskiy * In other words, the situation with extended attributes is very similar to 201e51764aSArtem Bityutskiy * directories. Indeed, any inode (but of course not xattr inodes) may have a 211e51764aSArtem Bityutskiy * number of associated xentries, just like directory inodes have associated 221e51764aSArtem Bityutskiy * directory entries. Extended attribute entries store the name of the extended 231e51764aSArtem Bityutskiy * attribute, the host inode number, and the extended attribute inode number. 241e51764aSArtem Bityutskiy * Similarly, direntries store the name, the parent and the target inode 251e51764aSArtem Bityutskiy * numbers. Thus, most of the common UBIFS mechanisms may be re-used for 261e51764aSArtem Bityutskiy * extended attributes. 271e51764aSArtem Bityutskiy * 281e51764aSArtem Bityutskiy * The number of extended attributes is not limited, but there is Linux 291e51764aSArtem Bityutskiy * limitation on the maximum possible size of the list of all extended 301e51764aSArtem Bityutskiy * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure 311e51764aSArtem Bityutskiy * the sum of all extended attribute names of the inode does not exceed that 321e51764aSArtem Bityutskiy * limit. 331e51764aSArtem Bityutskiy * 341e51764aSArtem Bityutskiy * Extended attributes are synchronous, which means they are written to the 351e51764aSArtem Bityutskiy * flash media synchronously and there is no write-back for extended attribute 361e51764aSArtem Bityutskiy * inodes. The extended attribute values are not stored in compressed form on 371e51764aSArtem Bityutskiy * the media. 381e51764aSArtem Bityutskiy * 391e51764aSArtem Bityutskiy * Since extended attributes are represented by regular inodes, they are cached 401e51764aSArtem Bityutskiy * in the VFS inode cache. The xentries are cached in the LNC cache (see 411e51764aSArtem Bityutskiy * tnc.c). 421e51764aSArtem Bityutskiy * 431e51764aSArtem Bityutskiy * ACL support is not implemented. 441e51764aSArtem Bityutskiy */ 451e51764aSArtem Bityutskiy 46073aaa1bSAl Viro #include "ubifs.h" 477dcda1c9SJens Axboe #include <linux/fs.h> 485a0e3ad6STejun Heo #include <linux/slab.h> 491e51764aSArtem Bityutskiy #include <linux/xattr.h> 501e51764aSArtem Bityutskiy 511e51764aSArtem Bityutskiy /* 521e51764aSArtem Bityutskiy * Extended attribute type constants. 531e51764aSArtem Bityutskiy * 541e51764aSArtem Bityutskiy * USER_XATTR: user extended attribute ("user.*") 551e51764aSArtem Bityutskiy * TRUSTED_XATTR: trusted extended attribute ("trusted.*) 561e51764aSArtem Bityutskiy * SECURITY_XATTR: security extended attribute ("security.*") 571e51764aSArtem Bityutskiy */ 581e51764aSArtem Bityutskiy enum { 591e51764aSArtem Bityutskiy USER_XATTR, 601e51764aSArtem Bityutskiy TRUSTED_XATTR, 611e51764aSArtem Bityutskiy SECURITY_XATTR, 621e51764aSArtem Bityutskiy }; 631e51764aSArtem Bityutskiy 6414ffd5d0SSedat Dilek static const struct inode_operations empty_iops; 6514ffd5d0SSedat Dilek static const struct file_operations empty_fops; 661e51764aSArtem Bityutskiy 671e51764aSArtem Bityutskiy /** 681e51764aSArtem Bityutskiy * create_xattr - create an extended attribute. 691e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 701e51764aSArtem Bityutskiy * @host: host inode 711e51764aSArtem Bityutskiy * @nm: extended attribute name 721e51764aSArtem Bityutskiy * @value: extended attribute value 731e51764aSArtem Bityutskiy * @size: size of extended attribute value 741e51764aSArtem Bityutskiy * 751e51764aSArtem Bityutskiy * This is a helper function which creates an extended attribute of name @nm 761e51764aSArtem Bityutskiy * and value @value for inode @host. The host inode is also updated on flash 771e51764aSArtem Bityutskiy * because the ctime and extended attribute accounting data changes. This 781e51764aSArtem Bityutskiy * function returns zero in case of success and a negative error code in case 791e51764aSArtem Bityutskiy * of failure. 801e51764aSArtem Bityutskiy */ 811e51764aSArtem Bityutskiy static int create_xattr(struct ubifs_info *c, struct inode *host, 82f4f61d2cSRichard Weinberger const struct fscrypt_name *nm, const void *value, int size) 831e51764aSArtem Bityutskiy { 84fee1756dSSubodh Nijsure int err, names_len; 851e51764aSArtem Bityutskiy struct inode *inode; 861e51764aSArtem Bityutskiy struct ubifs_inode *ui, *host_ui = ubifs_inode(host); 871e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, 885acd6ff8SZoltan Sogor .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1, 89dab4b4d2SArtem Bityutskiy .dirtied_ino_d = ALIGN(host_ui->data_len, 8) }; 901e51764aSArtem Bityutskiy 919ca2d732SRichard Weinberger if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) { 92235c362bSSheng Yong ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more", 93fee1756dSSubodh Nijsure host->i_ino, host_ui->xattr_cnt); 941e51764aSArtem Bityutskiy return -ENOSPC; 95fee1756dSSubodh Nijsure } 961e51764aSArtem Bityutskiy /* 971e51764aSArtem Bityutskiy * Linux limits the maximum size of the extended attribute names list 98c78c7e35SArtem Bityutskiy * to %XATTR_LIST_MAX. This means we should not allow creating more 991e51764aSArtem Bityutskiy * extended attributes if the name list becomes larger. This limitation 1001e51764aSArtem Bityutskiy * is artificial for UBIFS, though. 1011e51764aSArtem Bityutskiy */ 102f4f61d2cSRichard Weinberger names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1; 103fee1756dSSubodh Nijsure if (names_len > XATTR_LIST_MAX) { 104235c362bSSheng Yong ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d", 105fee1756dSSubodh Nijsure host->i_ino, names_len, XATTR_LIST_MAX); 1061e51764aSArtem Bityutskiy return -ENOSPC; 107fee1756dSSubodh Nijsure } 1081e51764aSArtem Bityutskiy 1091e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 1101e51764aSArtem Bityutskiy if (err) 1111e51764aSArtem Bityutskiy return err; 1121e51764aSArtem Bityutskiy 1131e51764aSArtem Bityutskiy inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO); 1141e51764aSArtem Bityutskiy if (IS_ERR(inode)) { 1151e51764aSArtem Bityutskiy err = PTR_ERR(inode); 1161e51764aSArtem Bityutskiy goto out_budg; 1171e51764aSArtem Bityutskiy } 1181e51764aSArtem Bityutskiy 1191e51764aSArtem Bityutskiy /* Re-define all operations to be "nothing" */ 1207dcda1c9SJens Axboe inode->i_mapping->a_ops = &empty_aops; 12114ffd5d0SSedat Dilek inode->i_op = &empty_iops; 12214ffd5d0SSedat Dilek inode->i_fop = &empty_fops; 1231e51764aSArtem Bityutskiy 124422edaceSWang Shilong inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME; 1251e51764aSArtem Bityutskiy ui = ubifs_inode(inode); 1261e51764aSArtem Bityutskiy ui->xattr = 1; 1271e51764aSArtem Bityutskiy ui->flags |= UBIFS_XATTR_FL; 128eaecf43aSThomas Meyer ui->data = kmemdup(value, size, GFP_NOFS); 1291e51764aSArtem Bityutskiy if (!ui->data) { 1301e51764aSArtem Bityutskiy err = -ENOMEM; 131c78c7e35SArtem Bityutskiy goto out_free; 1321e51764aSArtem Bityutskiy } 133c78c7e35SArtem Bityutskiy inode->i_size = ui->ui_size = size; 134c78c7e35SArtem Bityutskiy ui->data_len = size; 135c78c7e35SArtem Bityutskiy 136c78c7e35SArtem Bityutskiy mutex_lock(&host_ui->ui_mutex); 137607a11adSDeepa Dinamani host->i_ctime = current_time(host); 1381e51764aSArtem Bityutskiy host_ui->xattr_cnt += 1; 139f4f61d2cSRichard Weinberger host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm)); 1401e51764aSArtem Bityutskiy host_ui->xattr_size += CALC_XATTR_BYTES(size); 141f4f61d2cSRichard Weinberger host_ui->xattr_names += fname_len(nm); 1421e51764aSArtem Bityutskiy 143d475a507SRichard Weinberger /* 144d475a507SRichard Weinberger * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we 145d475a507SRichard Weinberger * have to set the UBIFS_CRYPT_FL flag on the host inode. 146d475a507SRichard Weinberger * To avoid multiple updates of the same inode in the same operation, 147d475a507SRichard Weinberger * let's do it here. 148d475a507SRichard Weinberger */ 149f4f61d2cSRichard Weinberger if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) 150d475a507SRichard Weinberger host_ui->flags |= UBIFS_CRYPT_FL; 151d475a507SRichard Weinberger 1521e51764aSArtem Bityutskiy err = ubifs_jnl_update(c, host, nm, inode, 0, 1); 1531e51764aSArtem Bityutskiy if (err) 1541e51764aSArtem Bityutskiy goto out_cancel; 1552ee6a576SEric Biggers ubifs_set_inode_flags(host); 1561e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 1571e51764aSArtem Bityutskiy 1581e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 1591e51764aSArtem Bityutskiy insert_inode_hash(inode); 1601e51764aSArtem Bityutskiy iput(inode); 1611e51764aSArtem Bityutskiy return 0; 1621e51764aSArtem Bityutskiy 1631e51764aSArtem Bityutskiy out_cancel: 1641e51764aSArtem Bityutskiy host_ui->xattr_cnt -= 1; 165f4f61d2cSRichard Weinberger host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm)); 1661e51764aSArtem Bityutskiy host_ui->xattr_size -= CALC_XATTR_BYTES(size); 167f4f61d2cSRichard Weinberger host_ui->xattr_names -= fname_len(nm); 168d475a507SRichard Weinberger host_ui->flags &= ~UBIFS_CRYPT_FL; 1691e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 170c78c7e35SArtem Bityutskiy out_free: 1711e51764aSArtem Bityutskiy make_bad_inode(inode); 1721e51764aSArtem Bityutskiy iput(inode); 1731e51764aSArtem Bityutskiy out_budg: 1741e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 1751e51764aSArtem Bityutskiy return err; 1761e51764aSArtem Bityutskiy } 1771e51764aSArtem Bityutskiy 1781e51764aSArtem Bityutskiy /** 1791e51764aSArtem Bityutskiy * change_xattr - change an extended attribute. 1801e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1811e51764aSArtem Bityutskiy * @host: host inode 1821e51764aSArtem Bityutskiy * @inode: extended attribute inode 1831e51764aSArtem Bityutskiy * @value: extended attribute value 1841e51764aSArtem Bityutskiy * @size: size of extended attribute value 1851e51764aSArtem Bityutskiy * 1861e51764aSArtem Bityutskiy * This helper function changes the value of extended attribute @inode with new 1871e51764aSArtem Bityutskiy * data from @value. Returns zero in case of success and a negative error code 1881e51764aSArtem Bityutskiy * in case of failure. 1891e51764aSArtem Bityutskiy */ 1901e51764aSArtem Bityutskiy static int change_xattr(struct ubifs_info *c, struct inode *host, 1911e51764aSArtem Bityutskiy struct inode *inode, const void *value, int size) 1921e51764aSArtem Bityutskiy { 1931e51764aSArtem Bityutskiy int err; 1941e51764aSArtem Bityutskiy struct ubifs_inode *host_ui = ubifs_inode(host); 1951e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 196ab92a20bSDongsheng Yang void *buf = NULL; 19774e9c700SPascal Eberhard int old_size; 1981e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .dirtied_ino = 2, 1995acd6ff8SZoltan Sogor .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) }; 2001e51764aSArtem Bityutskiy 2016eb61d58SRichard Weinberger ubifs_assert(c, ui->data_len == inode->i_size); 2021e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 2031e51764aSArtem Bityutskiy if (err) 2041e51764aSArtem Bityutskiy return err; 2051e51764aSArtem Bityutskiy 206ab92a20bSDongsheng Yang buf = kmemdup(value, size, GFP_NOFS); 207ab92a20bSDongsheng Yang if (!buf) { 2081e51764aSArtem Bityutskiy err = -ENOMEM; 209c78c7e35SArtem Bityutskiy goto out_free; 2101e51764aSArtem Bityutskiy } 211ab92a20bSDongsheng Yang kfree(ui->data); 212ab92a20bSDongsheng Yang ui->data = buf; 2131e51764aSArtem Bityutskiy inode->i_size = ui->ui_size = size; 21474e9c700SPascal Eberhard old_size = ui->data_len; 2151e51764aSArtem Bityutskiy ui->data_len = size; 2161e51764aSArtem Bityutskiy 217c78c7e35SArtem Bityutskiy mutex_lock(&host_ui->ui_mutex); 218607a11adSDeepa Dinamani host->i_ctime = current_time(host); 21974e9c700SPascal Eberhard host_ui->xattr_size -= CALC_XATTR_BYTES(old_size); 220c78c7e35SArtem Bityutskiy host_ui->xattr_size += CALC_XATTR_BYTES(size); 221c78c7e35SArtem Bityutskiy 2221e51764aSArtem Bityutskiy /* 2231e51764aSArtem Bityutskiy * It is important to write the host inode after the xattr inode 2241e51764aSArtem Bityutskiy * because if the host inode gets synchronized (via 'fsync()'), then 2251e51764aSArtem Bityutskiy * the extended attribute inode gets synchronized, because it goes 2261e51764aSArtem Bityutskiy * before the host inode in the write-buffer. 2271e51764aSArtem Bityutskiy */ 2281e51764aSArtem Bityutskiy err = ubifs_jnl_change_xattr(c, inode, host); 2291e51764aSArtem Bityutskiy if (err) 2301e51764aSArtem Bityutskiy goto out_cancel; 2311e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 2321e51764aSArtem Bityutskiy 2331e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 2341e51764aSArtem Bityutskiy return 0; 2351e51764aSArtem Bityutskiy 2361e51764aSArtem Bityutskiy out_cancel: 2371e51764aSArtem Bityutskiy host_ui->xattr_size -= CALC_XATTR_BYTES(size); 23874e9c700SPascal Eberhard host_ui->xattr_size += CALC_XATTR_BYTES(old_size); 2391e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 240c78c7e35SArtem Bityutskiy make_bad_inode(inode); 241c78c7e35SArtem Bityutskiy out_free: 2421e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 2431e51764aSArtem Bityutskiy return err; 2441e51764aSArtem Bityutskiy } 2451e51764aSArtem Bityutskiy 2461e51764aSArtem Bityutskiy static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum) 2471e51764aSArtem Bityutskiy { 2481e51764aSArtem Bityutskiy struct inode *inode; 2491e51764aSArtem Bityutskiy 2501e51764aSArtem Bityutskiy inode = ubifs_iget(c->vfs_sb, inum); 2511e51764aSArtem Bityutskiy if (IS_ERR(inode)) { 252235c362bSSheng Yong ubifs_err(c, "dead extended attribute entry, error %d", 2531e51764aSArtem Bityutskiy (int)PTR_ERR(inode)); 2541e51764aSArtem Bityutskiy return inode; 2551e51764aSArtem Bityutskiy } 2561e51764aSArtem Bityutskiy if (ubifs_inode(inode)->xattr) 2571e51764aSArtem Bityutskiy return inode; 258235c362bSSheng Yong ubifs_err(c, "corrupt extended attribute entry"); 2591e51764aSArtem Bityutskiy iput(inode); 2601e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 2611e51764aSArtem Bityutskiy } 2621e51764aSArtem Bityutskiy 263ade46c3aSRichard Weinberger int ubifs_xattr_set(struct inode *host, const char *name, const void *value, 264d8db5b1cSXiaolei Li size_t size, int flags, bool check_lock) 2651e51764aSArtem Bityutskiy { 266895d9db2SSubodh Nijsure struct inode *inode; 2671e51764aSArtem Bityutskiy struct ubifs_info *c = host->i_sb->s_fs_info; 268f4f61d2cSRichard Weinberger struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))}; 2691e51764aSArtem Bityutskiy struct ubifs_dent_node *xent; 2701e51764aSArtem Bityutskiy union ubifs_key key; 2712b88fc21SAndreas Gruenbacher int err; 2721e51764aSArtem Bityutskiy 273d8db5b1cSXiaolei Li if (check_lock) 2746eb61d58SRichard Weinberger ubifs_assert(c, inode_is_locked(host)); 2751e51764aSArtem Bityutskiy 2761e51764aSArtem Bityutskiy if (size > UBIFS_MAX_INO_DATA) 2771e51764aSArtem Bityutskiy return -ERANGE; 2781e51764aSArtem Bityutskiy 279f4f61d2cSRichard Weinberger if (fname_len(&nm) > UBIFS_MAX_NLEN) 2802b88fc21SAndreas Gruenbacher return -ENAMETOOLONG; 2811e51764aSArtem Bityutskiy 2821e51764aSArtem Bityutskiy xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); 2831e51764aSArtem Bityutskiy if (!xent) 2841e51764aSArtem Bityutskiy return -ENOMEM; 2851e51764aSArtem Bityutskiy 286f4e3634aSZhihao Cheng down_write(&ubifs_inode(host)->xattr_sem); 2871e51764aSArtem Bityutskiy /* 2881e51764aSArtem Bityutskiy * The extended attribute entries are stored in LNC, so multiple 2891e51764aSArtem Bityutskiy * look-ups do not involve reading the flash. 2901e51764aSArtem Bityutskiy */ 2911e51764aSArtem Bityutskiy xent_key_init(c, &key, host->i_ino, &nm); 2921e51764aSArtem Bityutskiy err = ubifs_tnc_lookup_nm(c, &key, xent, &nm); 2931e51764aSArtem Bityutskiy if (err) { 2941e51764aSArtem Bityutskiy if (err != -ENOENT) 2951e51764aSArtem Bityutskiy goto out_free; 2961e51764aSArtem Bityutskiy 2971e51764aSArtem Bityutskiy if (flags & XATTR_REPLACE) 2981e51764aSArtem Bityutskiy /* We are asked not to create the xattr */ 2991e51764aSArtem Bityutskiy err = -ENODATA; 3001e51764aSArtem Bityutskiy else 3011e51764aSArtem Bityutskiy err = create_xattr(c, host, &nm, value, size); 3021e51764aSArtem Bityutskiy goto out_free; 3031e51764aSArtem Bityutskiy } 3041e51764aSArtem Bityutskiy 3051e51764aSArtem Bityutskiy if (flags & XATTR_CREATE) { 3061e51764aSArtem Bityutskiy /* We are asked not to replace the xattr */ 3071e51764aSArtem Bityutskiy err = -EEXIST; 3081e51764aSArtem Bityutskiy goto out_free; 3091e51764aSArtem Bityutskiy } 3101e51764aSArtem Bityutskiy 3111e51764aSArtem Bityutskiy inode = iget_xattr(c, le64_to_cpu(xent->inum)); 3121e51764aSArtem Bityutskiy if (IS_ERR(inode)) { 3131e51764aSArtem Bityutskiy err = PTR_ERR(inode); 3141e51764aSArtem Bityutskiy goto out_free; 3151e51764aSArtem Bityutskiy } 3161e51764aSArtem Bityutskiy 3171e51764aSArtem Bityutskiy err = change_xattr(c, host, inode, value, size); 3181e51764aSArtem Bityutskiy iput(inode); 3191e51764aSArtem Bityutskiy 3201e51764aSArtem Bityutskiy out_free: 321f4e3634aSZhihao Cheng up_write(&ubifs_inode(host)->xattr_sem); 3221e51764aSArtem Bityutskiy kfree(xent); 3231e51764aSArtem Bityutskiy return err; 3241e51764aSArtem Bityutskiy } 3251e51764aSArtem Bityutskiy 326ade46c3aSRichard Weinberger ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf, 327ade46c3aSRichard Weinberger size_t size) 3281e51764aSArtem Bityutskiy { 329ce23e640SAl Viro struct inode *inode; 3301e51764aSArtem Bityutskiy struct ubifs_info *c = host->i_sb->s_fs_info; 331f4f61d2cSRichard Weinberger struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))}; 3321e51764aSArtem Bityutskiy struct ubifs_inode *ui; 3331e51764aSArtem Bityutskiy struct ubifs_dent_node *xent; 3341e51764aSArtem Bityutskiy union ubifs_key key; 3351e51764aSArtem Bityutskiy int err; 3361e51764aSArtem Bityutskiy 337f4f61d2cSRichard Weinberger if (fname_len(&nm) > UBIFS_MAX_NLEN) 3382b88fc21SAndreas Gruenbacher return -ENAMETOOLONG; 3391e51764aSArtem Bityutskiy 3401e51764aSArtem Bityutskiy xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); 3411e51764aSArtem Bityutskiy if (!xent) 3421e51764aSArtem Bityutskiy return -ENOMEM; 3431e51764aSArtem Bityutskiy 344f4e3634aSZhihao Cheng down_read(&ubifs_inode(host)->xattr_sem); 3451e51764aSArtem Bityutskiy xent_key_init(c, &key, host->i_ino, &nm); 3461e51764aSArtem Bityutskiy err = ubifs_tnc_lookup_nm(c, &key, xent, &nm); 3471e51764aSArtem Bityutskiy if (err) { 3481e51764aSArtem Bityutskiy if (err == -ENOENT) 3491e51764aSArtem Bityutskiy err = -ENODATA; 350f4e3634aSZhihao Cheng goto out_cleanup; 3511e51764aSArtem Bityutskiy } 3521e51764aSArtem Bityutskiy 3531e51764aSArtem Bityutskiy inode = iget_xattr(c, le64_to_cpu(xent->inum)); 3541e51764aSArtem Bityutskiy if (IS_ERR(inode)) { 3551e51764aSArtem Bityutskiy err = PTR_ERR(inode); 356f4e3634aSZhihao Cheng goto out_cleanup; 3571e51764aSArtem Bityutskiy } 3581e51764aSArtem Bityutskiy 3591e51764aSArtem Bityutskiy ui = ubifs_inode(inode); 3606eb61d58SRichard Weinberger ubifs_assert(c, inode->i_size == ui->data_len); 3616eb61d58SRichard Weinberger ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len); 3621e51764aSArtem Bityutskiy 3631e51764aSArtem Bityutskiy if (buf) { 3641e51764aSArtem Bityutskiy /* If @buf is %NULL we are supposed to return the length */ 3651e51764aSArtem Bityutskiy if (ui->data_len > size) { 3661e51764aSArtem Bityutskiy err = -ERANGE; 3671e51764aSArtem Bityutskiy goto out_iput; 3681e51764aSArtem Bityutskiy } 3691e51764aSArtem Bityutskiy 3701e51764aSArtem Bityutskiy memcpy(buf, ui->data, ui->data_len); 3711e51764aSArtem Bityutskiy } 3721e51764aSArtem Bityutskiy err = ui->data_len; 3731e51764aSArtem Bityutskiy 3741e51764aSArtem Bityutskiy out_iput: 3751e51764aSArtem Bityutskiy iput(inode); 376f4e3634aSZhihao Cheng out_cleanup: 377f4e3634aSZhihao Cheng up_read(&ubifs_inode(host)->xattr_sem); 3781e51764aSArtem Bityutskiy kfree(xent); 3791e51764aSArtem Bityutskiy return err; 3801e51764aSArtem Bityutskiy } 3811e51764aSArtem Bityutskiy 38243b113feSRichard Weinberger static bool xattr_visible(const char *name) 38343b113feSRichard Weinberger { 38443b113feSRichard Weinberger /* File encryption related xattrs are for internal use only */ 38543b113feSRichard Weinberger if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0) 38643b113feSRichard Weinberger return false; 38743b113feSRichard Weinberger 38843b113feSRichard Weinberger /* Show trusted namespace only for "power" users */ 38943b113feSRichard Weinberger if (strncmp(name, XATTR_TRUSTED_PREFIX, 39043b113feSRichard Weinberger XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN)) 39143b113feSRichard Weinberger return false; 39243b113feSRichard Weinberger 39343b113feSRichard Weinberger return true; 39443b113feSRichard Weinberger } 39543b113feSRichard Weinberger 3961e51764aSArtem Bityutskiy ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size) 3971e51764aSArtem Bityutskiy { 3981e51764aSArtem Bityutskiy union ubifs_key key; 3992b0143b5SDavid Howells struct inode *host = d_inode(dentry); 4001e51764aSArtem Bityutskiy struct ubifs_info *c = host->i_sb->s_fs_info; 4011e51764aSArtem Bityutskiy struct ubifs_inode *host_ui = ubifs_inode(host); 4021e51764aSArtem Bityutskiy struct ubifs_dent_node *xent, *pxent = NULL; 4031e51764aSArtem Bityutskiy int err, len, written = 0; 404f4f61d2cSRichard Weinberger struct fscrypt_name nm = {0}; 4051e51764aSArtem Bityutskiy 4064cb2a01dSAl Viro dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino, 4074cb2a01dSAl Viro dentry, size); 4081e51764aSArtem Bityutskiy 409f4e3634aSZhihao Cheng down_read(&host_ui->xattr_sem); 4101e51764aSArtem Bityutskiy len = host_ui->xattr_names + host_ui->xattr_cnt; 411f4e3634aSZhihao Cheng if (!buffer) { 4121e51764aSArtem Bityutskiy /* 4131e51764aSArtem Bityutskiy * We should return the minimum buffer size which will fit a 4141e51764aSArtem Bityutskiy * null-terminated list of all the extended attribute names. 4151e51764aSArtem Bityutskiy */ 416f4e3634aSZhihao Cheng err = len; 417f4e3634aSZhihao Cheng goto out_err; 418f4e3634aSZhihao Cheng } 4191e51764aSArtem Bityutskiy 420f4e3634aSZhihao Cheng if (len > size) { 421f4e3634aSZhihao Cheng err = -ERANGE; 422f4e3634aSZhihao Cheng goto out_err; 423f4e3634aSZhihao Cheng } 4241e51764aSArtem Bityutskiy 4251e51764aSArtem Bityutskiy lowest_xent_key(c, &key, host->i_ino); 4261e51764aSArtem Bityutskiy while (1) { 4271e51764aSArtem Bityutskiy xent = ubifs_tnc_next_ent(c, &key, &nm); 4288d47aef4SHirofumi Nakagawa if (IS_ERR(xent)) { 4291e51764aSArtem Bityutskiy err = PTR_ERR(xent); 4301e51764aSArtem Bityutskiy break; 4311e51764aSArtem Bityutskiy } 4321e51764aSArtem Bityutskiy 433f4f61d2cSRichard Weinberger fname_name(&nm) = xent->name; 434f4f61d2cSRichard Weinberger fname_len(&nm) = le16_to_cpu(xent->nlen); 4351e51764aSArtem Bityutskiy 43643b113feSRichard Weinberger if (xattr_visible(xent->name)) { 437f4f61d2cSRichard Weinberger memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1); 438f4f61d2cSRichard Weinberger written += fname_len(&nm) + 1; 4391e51764aSArtem Bityutskiy } 4401e51764aSArtem Bityutskiy 4411e51764aSArtem Bityutskiy kfree(pxent); 4421e51764aSArtem Bityutskiy pxent = xent; 4431e51764aSArtem Bityutskiy key_read(c, &xent->key, &key); 4441e51764aSArtem Bityutskiy } 4451e51764aSArtem Bityutskiy kfree(pxent); 446f4e3634aSZhihao Cheng up_read(&host_ui->xattr_sem); 447f4e3634aSZhihao Cheng 4481e51764aSArtem Bityutskiy if (err != -ENOENT) { 449235c362bSSheng Yong ubifs_err(c, "cannot find next direntry, error %d", err); 4501e51764aSArtem Bityutskiy return err; 4511e51764aSArtem Bityutskiy } 4521e51764aSArtem Bityutskiy 4536eb61d58SRichard Weinberger ubifs_assert(c, written <= size); 4541e51764aSArtem Bityutskiy return written; 455f4e3634aSZhihao Cheng 456f4e3634aSZhihao Cheng out_err: 457f4e3634aSZhihao Cheng up_read(&host_ui->xattr_sem); 458f4e3634aSZhihao Cheng return err; 4591e51764aSArtem Bityutskiy } 4601e51764aSArtem Bityutskiy 4611e51764aSArtem Bityutskiy static int remove_xattr(struct ubifs_info *c, struct inode *host, 462f4f61d2cSRichard Weinberger struct inode *inode, const struct fscrypt_name *nm) 4631e51764aSArtem Bityutskiy { 4641e51764aSArtem Bityutskiy int err; 4651e51764aSArtem Bityutskiy struct ubifs_inode *host_ui = ubifs_inode(host); 4661e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 4675acd6ff8SZoltan Sogor struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1, 4685acd6ff8SZoltan Sogor .dirtied_ino_d = ALIGN(host_ui->data_len, 8) }; 4691e51764aSArtem Bityutskiy 4706eb61d58SRichard Weinberger ubifs_assert(c, ui->data_len == inode->i_size); 4711e51764aSArtem Bityutskiy 4721e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 4731e51764aSArtem Bityutskiy if (err) 4741e51764aSArtem Bityutskiy return err; 4751e51764aSArtem Bityutskiy 4761e51764aSArtem Bityutskiy mutex_lock(&host_ui->ui_mutex); 477607a11adSDeepa Dinamani host->i_ctime = current_time(host); 4781e51764aSArtem Bityutskiy host_ui->xattr_cnt -= 1; 479f4f61d2cSRichard Weinberger host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm)); 4801e51764aSArtem Bityutskiy host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len); 481f4f61d2cSRichard Weinberger host_ui->xattr_names -= fname_len(nm); 4821e51764aSArtem Bityutskiy 4831e51764aSArtem Bityutskiy err = ubifs_jnl_delete_xattr(c, host, inode, nm); 4841e51764aSArtem Bityutskiy if (err) 4851e51764aSArtem Bityutskiy goto out_cancel; 4861e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 4871e51764aSArtem Bityutskiy 4881e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 4891e51764aSArtem Bityutskiy return 0; 4901e51764aSArtem Bityutskiy 4911e51764aSArtem Bityutskiy out_cancel: 4921e51764aSArtem Bityutskiy host_ui->xattr_cnt += 1; 493f4f61d2cSRichard Weinberger host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm)); 4941e51764aSArtem Bityutskiy host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len); 495f4f61d2cSRichard Weinberger host_ui->xattr_names += fname_len(nm); 4961e51764aSArtem Bityutskiy mutex_unlock(&host_ui->ui_mutex); 4971e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 4981e51764aSArtem Bityutskiy make_bad_inode(inode); 4991e51764aSArtem Bityutskiy return err; 5001e51764aSArtem Bityutskiy } 5011e51764aSArtem Bityutskiy 5029ca2d732SRichard Weinberger int ubifs_purge_xattrs(struct inode *host) 5039ca2d732SRichard Weinberger { 5049ca2d732SRichard Weinberger union ubifs_key key; 5059ca2d732SRichard Weinberger struct ubifs_info *c = host->i_sb->s_fs_info; 5069ca2d732SRichard Weinberger struct ubifs_dent_node *xent, *pxent = NULL; 5079ca2d732SRichard Weinberger struct inode *xino; 5089ca2d732SRichard Weinberger struct fscrypt_name nm = {0}; 5099ca2d732SRichard Weinberger int err; 5109ca2d732SRichard Weinberger 511d984bcf5SSascha Hauer if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c)) 5129ca2d732SRichard Weinberger return 0; 5139ca2d732SRichard Weinberger 5149ca2d732SRichard Weinberger ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion", 5159ca2d732SRichard Weinberger host->i_ino); 5169ca2d732SRichard Weinberger 517f4e3634aSZhihao Cheng down_write(&ubifs_inode(host)->xattr_sem); 5189ca2d732SRichard Weinberger lowest_xent_key(c, &key, host->i_ino); 5199ca2d732SRichard Weinberger while (1) { 5209ca2d732SRichard Weinberger xent = ubifs_tnc_next_ent(c, &key, &nm); 5219ca2d732SRichard Weinberger if (IS_ERR(xent)) { 5229ca2d732SRichard Weinberger err = PTR_ERR(xent); 5239ca2d732SRichard Weinberger break; 5249ca2d732SRichard Weinberger } 5259ca2d732SRichard Weinberger 5269ca2d732SRichard Weinberger fname_name(&nm) = xent->name; 5279ca2d732SRichard Weinberger fname_len(&nm) = le16_to_cpu(xent->nlen); 5289ca2d732SRichard Weinberger 5294dd04815SRichard Weinberger xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum)); 5309ca2d732SRichard Weinberger if (IS_ERR(xino)) { 5319ca2d732SRichard Weinberger err = PTR_ERR(xino); 5329ca2d732SRichard Weinberger ubifs_err(c, "dead directory entry '%s', error %d", 5339ca2d732SRichard Weinberger xent->name, err); 5349ca2d732SRichard Weinberger ubifs_ro_mode(c, err); 5359ca2d732SRichard Weinberger kfree(pxent); 536f2aae745SZhihao Cheng kfree(xent); 537f4e3634aSZhihao Cheng goto out_err; 5389ca2d732SRichard Weinberger } 5399ca2d732SRichard Weinberger 5409ca2d732SRichard Weinberger ubifs_assert(c, ubifs_inode(xino)->xattr); 5419ca2d732SRichard Weinberger 5429ca2d732SRichard Weinberger clear_nlink(xino); 5439ca2d732SRichard Weinberger err = remove_xattr(c, host, xino, &nm); 5449ca2d732SRichard Weinberger if (err) { 5459ca2d732SRichard Weinberger kfree(pxent); 546f2aae745SZhihao Cheng kfree(xent); 5479ca2d732SRichard Weinberger iput(xino); 5489ca2d732SRichard Weinberger ubifs_err(c, "cannot remove xattr, error %d", err); 549f4e3634aSZhihao Cheng goto out_err; 5509ca2d732SRichard Weinberger } 5519ca2d732SRichard Weinberger 5529ca2d732SRichard Weinberger iput(xino); 5539ca2d732SRichard Weinberger 5549ca2d732SRichard Weinberger kfree(pxent); 5559ca2d732SRichard Weinberger pxent = xent; 5569ca2d732SRichard Weinberger key_read(c, &xent->key, &key); 5579ca2d732SRichard Weinberger } 5589ca2d732SRichard Weinberger kfree(pxent); 559f4e3634aSZhihao Cheng up_write(&ubifs_inode(host)->xattr_sem); 560f4e3634aSZhihao Cheng 5619ca2d732SRichard Weinberger if (err != -ENOENT) { 5629ca2d732SRichard Weinberger ubifs_err(c, "cannot find next direntry, error %d", err); 5639ca2d732SRichard Weinberger return err; 5649ca2d732SRichard Weinberger } 5659ca2d732SRichard Weinberger 5669ca2d732SRichard Weinberger return 0; 567f4e3634aSZhihao Cheng 568f4e3634aSZhihao Cheng out_err: 569f4e3634aSZhihao Cheng up_write(&ubifs_inode(host)->xattr_sem); 570f4e3634aSZhihao Cheng return err; 5719ca2d732SRichard Weinberger } 5729ca2d732SRichard Weinberger 573272eda82SRichard Weinberger /** 574272eda82SRichard Weinberger * ubifs_evict_xattr_inode - Evict an xattr inode. 575272eda82SRichard Weinberger * @c: UBIFS file-system description object 576272eda82SRichard Weinberger * @xattr_inum: xattr inode number 577272eda82SRichard Weinberger * 578272eda82SRichard Weinberger * When an inode that hosts xattrs is being removed we have to make sure 579272eda82SRichard Weinberger * that cached inodes of the xattrs also get removed from the inode cache 580272eda82SRichard Weinberger * otherwise we'd waste memory. This function looks up an inode from the 581272eda82SRichard Weinberger * inode cache and clears the link counter such that iput() will evict 582272eda82SRichard Weinberger * the inode. 583272eda82SRichard Weinberger */ 584272eda82SRichard Weinberger void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum) 585272eda82SRichard Weinberger { 586272eda82SRichard Weinberger struct inode *inode; 587272eda82SRichard Weinberger 588272eda82SRichard Weinberger inode = ilookup(c->vfs_sb, xattr_inum); 589272eda82SRichard Weinberger if (inode) { 590272eda82SRichard Weinberger clear_nlink(inode); 591272eda82SRichard Weinberger iput(inode); 592272eda82SRichard Weinberger } 593272eda82SRichard Weinberger } 594272eda82SRichard Weinberger 595ade46c3aSRichard Weinberger static int ubifs_xattr_remove(struct inode *host, const char *name) 5961e51764aSArtem Bityutskiy { 5972b88fc21SAndreas Gruenbacher struct inode *inode; 5981e51764aSArtem Bityutskiy struct ubifs_info *c = host->i_sb->s_fs_info; 599f4f61d2cSRichard Weinberger struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))}; 6001e51764aSArtem Bityutskiy struct ubifs_dent_node *xent; 6011e51764aSArtem Bityutskiy union ubifs_key key; 6021e51764aSArtem Bityutskiy int err; 6031e51764aSArtem Bityutskiy 6046eb61d58SRichard Weinberger ubifs_assert(c, inode_is_locked(host)); 6051e51764aSArtem Bityutskiy 606f4f61d2cSRichard Weinberger if (fname_len(&nm) > UBIFS_MAX_NLEN) 6072b88fc21SAndreas Gruenbacher return -ENAMETOOLONG; 6081e51764aSArtem Bityutskiy 6091e51764aSArtem Bityutskiy xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS); 6101e51764aSArtem Bityutskiy if (!xent) 6111e51764aSArtem Bityutskiy return -ENOMEM; 6121e51764aSArtem Bityutskiy 613f4e3634aSZhihao Cheng down_write(&ubifs_inode(host)->xattr_sem); 6141e51764aSArtem Bityutskiy xent_key_init(c, &key, host->i_ino, &nm); 6151e51764aSArtem Bityutskiy err = ubifs_tnc_lookup_nm(c, &key, xent, &nm); 6161e51764aSArtem Bityutskiy if (err) { 6171e51764aSArtem Bityutskiy if (err == -ENOENT) 6181e51764aSArtem Bityutskiy err = -ENODATA; 6191e51764aSArtem Bityutskiy goto out_free; 6201e51764aSArtem Bityutskiy } 6211e51764aSArtem Bityutskiy 6221e51764aSArtem Bityutskiy inode = iget_xattr(c, le64_to_cpu(xent->inum)); 6231e51764aSArtem Bityutskiy if (IS_ERR(inode)) { 6241e51764aSArtem Bityutskiy err = PTR_ERR(inode); 6251e51764aSArtem Bityutskiy goto out_free; 6261e51764aSArtem Bityutskiy } 6271e51764aSArtem Bityutskiy 6286eb61d58SRichard Weinberger ubifs_assert(c, inode->i_nlink == 1); 6296d6b77f1SMiklos Szeredi clear_nlink(inode); 6301e51764aSArtem Bityutskiy err = remove_xattr(c, host, inode, &nm); 6311e51764aSArtem Bityutskiy if (err) 632bfe86848SMiklos Szeredi set_nlink(inode, 1); 6331e51764aSArtem Bityutskiy 6341e51764aSArtem Bityutskiy /* If @i_nlink is 0, 'iput()' will delete the inode */ 6351e51764aSArtem Bityutskiy iput(inode); 6361e51764aSArtem Bityutskiy 6371e51764aSArtem Bityutskiy out_free: 638f4e3634aSZhihao Cheng up_write(&ubifs_inode(host)->xattr_sem); 6391e51764aSArtem Bityutskiy kfree(xent); 6401e51764aSArtem Bityutskiy return err; 6411e51764aSArtem Bityutskiy } 642d7f0b70dSSubodh Nijsure 6438326c1eeSHyunchul Lee #ifdef CONFIG_UBIFS_FS_SECURITY 644d7f0b70dSSubodh Nijsure static int init_xattrs(struct inode *inode, const struct xattr *xattr_array, 645d7f0b70dSSubodh Nijsure void *fs_info) 646d7f0b70dSSubodh Nijsure { 647d7f0b70dSSubodh Nijsure const struct xattr *xattr; 648d7f0b70dSSubodh Nijsure char *name; 649d7f0b70dSSubodh Nijsure int err = 0; 650d7f0b70dSSubodh Nijsure 651d7f0b70dSSubodh Nijsure for (xattr = xattr_array; xattr->name != NULL; xattr++) { 652d7f0b70dSSubodh Nijsure name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 653d7f0b70dSSubodh Nijsure strlen(xattr->name) + 1, GFP_NOFS); 654d7f0b70dSSubodh Nijsure if (!name) { 655d7f0b70dSSubodh Nijsure err = -ENOMEM; 656d7f0b70dSSubodh Nijsure break; 657d7f0b70dSSubodh Nijsure } 658d7f0b70dSSubodh Nijsure strcpy(name, XATTR_SECURITY_PREFIX); 659d7f0b70dSSubodh Nijsure strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name); 660d8db5b1cSXiaolei Li /* 661d8db5b1cSXiaolei Li * creating a new inode without holding the inode rwsem, 662d8db5b1cSXiaolei Li * no need to check whether inode is locked. 663d8db5b1cSXiaolei Li */ 664ade46c3aSRichard Weinberger err = ubifs_xattr_set(inode, name, xattr->value, 665d8db5b1cSXiaolei Li xattr->value_len, 0, false); 666d7f0b70dSSubodh Nijsure kfree(name); 667d7f0b70dSSubodh Nijsure if (err < 0) 668d7f0b70dSSubodh Nijsure break; 669d7f0b70dSSubodh Nijsure } 670d7f0b70dSSubodh Nijsure 671d7f0b70dSSubodh Nijsure return err; 672d7f0b70dSSubodh Nijsure } 673d7f0b70dSSubodh Nijsure 674d7f0b70dSSubodh Nijsure int ubifs_init_security(struct inode *dentry, struct inode *inode, 675d7f0b70dSSubodh Nijsure const struct qstr *qstr) 676d7f0b70dSSubodh Nijsure { 677d7f0b70dSSubodh Nijsure int err; 678d7f0b70dSSubodh Nijsure 679d7f0b70dSSubodh Nijsure err = security_inode_init_security(inode, dentry, qstr, 680*532aef59SHaowen Bai &init_xattrs, NULL); 681235c362bSSheng Yong if (err) { 682235c362bSSheng Yong struct ubifs_info *c = dentry->i_sb->s_fs_info; 683235c362bSSheng Yong ubifs_err(c, "cannot initialize security for inode %lu, error %d", 684fee1756dSSubodh Nijsure inode->i_ino, err); 685235c362bSSheng Yong } 686d7f0b70dSSubodh Nijsure return err; 687d7f0b70dSSubodh Nijsure } 6888326c1eeSHyunchul Lee #endif 6892b88fc21SAndreas Gruenbacher 690ade46c3aSRichard Weinberger static int xattr_get(const struct xattr_handler *handler, 6912b88fc21SAndreas Gruenbacher struct dentry *dentry, struct inode *inode, 6922b88fc21SAndreas Gruenbacher const char *name, void *buffer, size_t size) 6932b88fc21SAndreas Gruenbacher { 6942b88fc21SAndreas Gruenbacher dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name, 6952b88fc21SAndreas Gruenbacher inode->i_ino, dentry, size); 6962b88fc21SAndreas Gruenbacher 69717ce1eb0SRichard Weinberger name = xattr_full_name(handler, name); 698ade46c3aSRichard Weinberger return ubifs_xattr_get(inode, name, buffer, size); 6992b88fc21SAndreas Gruenbacher } 7002b88fc21SAndreas Gruenbacher 701ade46c3aSRichard Weinberger static int xattr_set(const struct xattr_handler *handler, 702e65ce2a5SChristian Brauner struct user_namespace *mnt_userns, 70359301226SAl Viro struct dentry *dentry, struct inode *inode, 70459301226SAl Viro const char *name, const void *value, 70559301226SAl Viro size_t size, int flags) 7062b88fc21SAndreas Gruenbacher { 7072b88fc21SAndreas Gruenbacher dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd", 7082b88fc21SAndreas Gruenbacher name, inode->i_ino, dentry, size); 7092b88fc21SAndreas Gruenbacher 71017ce1eb0SRichard Weinberger name = xattr_full_name(handler, name); 71117ce1eb0SRichard Weinberger 7122b88fc21SAndreas Gruenbacher if (value) 713d8db5b1cSXiaolei Li return ubifs_xattr_set(inode, name, value, size, flags, true); 7142b88fc21SAndreas Gruenbacher else 715ade46c3aSRichard Weinberger return ubifs_xattr_remove(inode, name); 7162b88fc21SAndreas Gruenbacher } 7172b88fc21SAndreas Gruenbacher 718dfaf8d2aSBen Dooks static const struct xattr_handler ubifs_user_xattr_handler = { 7192b88fc21SAndreas Gruenbacher .prefix = XATTR_USER_PREFIX, 720ade46c3aSRichard Weinberger .get = xattr_get, 721ade46c3aSRichard Weinberger .set = xattr_set, 7222b88fc21SAndreas Gruenbacher }; 7232b88fc21SAndreas Gruenbacher 724dfaf8d2aSBen Dooks static const struct xattr_handler ubifs_trusted_xattr_handler = { 7252b88fc21SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 726ade46c3aSRichard Weinberger .get = xattr_get, 727ade46c3aSRichard Weinberger .set = xattr_set, 7282b88fc21SAndreas Gruenbacher }; 7292b88fc21SAndreas Gruenbacher 7308326c1eeSHyunchul Lee #ifdef CONFIG_UBIFS_FS_SECURITY 731dfaf8d2aSBen Dooks static const struct xattr_handler ubifs_security_xattr_handler = { 7322b88fc21SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 733ade46c3aSRichard Weinberger .get = xattr_get, 734ade46c3aSRichard Weinberger .set = xattr_set, 7352b88fc21SAndreas Gruenbacher }; 7368326c1eeSHyunchul Lee #endif 7372b88fc21SAndreas Gruenbacher 7382b88fc21SAndreas Gruenbacher const struct xattr_handler *ubifs_xattr_handlers[] = { 7392b88fc21SAndreas Gruenbacher &ubifs_user_xattr_handler, 7402b88fc21SAndreas Gruenbacher &ubifs_trusted_xattr_handler, 7418326c1eeSHyunchul Lee #ifdef CONFIG_UBIFS_FS_SECURITY 7422b88fc21SAndreas Gruenbacher &ubifs_security_xattr_handler, 7438326c1eeSHyunchul Lee #endif 7442b88fc21SAndreas Gruenbacher NULL 7452b88fc21SAndreas Gruenbacher }; 746