Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Authors: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *          Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * This file implements UBIFS extended attributes support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Extended attributes are implemented as regular inodes with attached data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * which limits extended attribute size to UBIFS block size (4KiB). Names of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * extended attributes are described by extended attribute entries (xentries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * which are almost identical to directory entries, but have different key type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * In other words, the situation with extended attributes is very similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * directories. Indeed, any inode (but of course not xattr inodes) may have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * number of associated xentries, just like directory inodes have associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * directory entries. Extended attribute entries store the name of the extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * attribute, the host inode number, and the extended attribute inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Similarly, direntries store the name, the parent and the target inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * extended attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * The number of extended attributes is not limited, but there is Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * limitation on the maximum possible size of the list of all extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * the sum of all extended attribute names of the inode does not exceed that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Extended attributes are synchronous, which means they are written to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * flash media synchronously and there is no write-back for extended attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * inodes. The extended attribute values are not stored in compressed form on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * the media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * Since extended attributes are represented by regular inodes, they are cached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * in the VFS inode cache. The xentries are cached in the LNC cache (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * tnc.c).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * ACL support is not implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Extended attribute type constants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * USER_XATTR: user extended attribute ("user.*")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * SECURITY_XATTR: security extended attribute ("security.*")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	USER_XATTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	TRUSTED_XATTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	SECURITY_XATTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static const struct inode_operations empty_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static const struct file_operations empty_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * create_xattr - create an extended attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * @host: host inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * @nm: extended attribute name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @value: extended attribute value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * @size: size of extended attribute value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * This is a helper function which creates an extended attribute of name @nm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * and value @value for inode @host. The host inode is also updated on flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * because the ctime and extended attribute accounting data changes. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * function returns zero in case of success and a negative error code in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int create_xattr(struct ubifs_info *c, struct inode *host,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			const struct fscrypt_name *nm, const void *value, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int err, names_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				.new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (host_ui->xattr_cnt >= ubifs_xattr_max_cnt(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			  host->i_ino, host_ui->xattr_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * Linux limits the maximum size of the extended attribute names list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * to %XATTR_LIST_MAX. This means we should not allow creating more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * extended attributes if the name list becomes larger. This limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * is artificial for UBIFS, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (names_len > XATTR_LIST_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			  host->i_ino, names_len, XATTR_LIST_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Re-define all operations to be "nothing" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	inode->i_mapping->a_ops = &empty_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	inode->i_op = &empty_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	inode->i_fop = &empty_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	ui->xattr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ui->flags |= UBIFS_XATTR_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ui->data = kmemdup(value, size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (!ui->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	inode->i_size = ui->ui_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	ui->data_len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	mutex_lock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	host->i_ctime = current_time(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	host_ui->xattr_cnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	host_ui->xattr_names += fname_len(nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	 * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * have to set the UBIFS_CRYPT_FL flag on the host inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 * To avoid multiple updates of the same inode in the same operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 * let's do it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		host_ui->flags |= UBIFS_CRYPT_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ubifs_set_inode_flags(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	host_ui->xattr_cnt -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	host_ui->xattr_names -= fname_len(nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	host_ui->flags &= ~UBIFS_CRYPT_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * change_xattr - change an extended attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * @host: host inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * @inode: extended attribute inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * @value: extended attribute value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * @size: size of extended attribute value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * This helper function changes the value of extended attribute @inode with new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * data from @value. Returns zero in case of success and a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int change_xattr(struct ubifs_info *c, struct inode *host,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			struct inode *inode, const void *value, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct ubifs_inode *host_ui = ubifs_inode(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	void *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct ubifs_budget_req req = { .dirtied_ino = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		.dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ubifs_assert(c, ui->data_len == inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	buf = kmemdup(value, size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	kfree(ui->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	ui->data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	inode->i_size = ui->ui_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	old_size = ui->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	ui->data_len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	mutex_lock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	host->i_ctime = current_time(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	host_ui->xattr_size += CALC_XATTR_BYTES(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	 * It is important to write the host inode after the xattr inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	 * because if the host inode gets synchronized (via 'fsync()'), then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	 * the extended attribute inode gets synchronized, because it goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * before the host inode in the write-buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	err = ubifs_jnl_change_xattr(c, inode, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	host_ui->xattr_size -= CALC_XATTR_BYTES(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	inode = ubifs_iget(c->vfs_sb, inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		ubifs_err(c, "dead extended attribute entry, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			  (int)PTR_ERR(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (ubifs_inode(inode)->xattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	ubifs_err(c, "corrupt extended attribute entry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		    size_t size, int flags, bool check_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct ubifs_info *c = host->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct ubifs_dent_node *xent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (check_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ubifs_assert(c, inode_is_locked(host));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (size > UBIFS_MAX_INO_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (fname_len(&nm) > UBIFS_MAX_NLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!xent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	down_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * The extended attribute entries are stored in LNC, so multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * look-ups do not involve reading the flash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	xent_key_init(c, &key, host->i_ino, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (flags & XATTR_REPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			/* We are asked not to create the xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			err = create_xattr(c, host, &nm, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (flags & XATTR_CREATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		/* We are asked not to replace the xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	err = change_xattr(c, host, inode, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	up_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	kfree(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	struct ubifs_info *c = host->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct ubifs_inode *ui;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct ubifs_dent_node *xent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (fname_len(&nm) > UBIFS_MAX_NLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (!xent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	down_read(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	xent_key_init(c, &key, host->i_ino, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		goto out_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		goto out_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	ubifs_assert(c, inode->i_size == ui->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		/* If @buf is %NULL we are supposed to return the length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (ui->data_len > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			err = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		memcpy(buf, ui->data, ui->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	err = ui->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) out_iput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) out_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	up_read(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	kfree(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static bool xattr_visible(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	/* File encryption related xattrs are for internal use only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* Show trusted namespace only for "power" users */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (strncmp(name, XATTR_TRUSTED_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		    XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct inode *host = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct ubifs_info *c = host->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct ubifs_inode *host_ui = ubifs_inode(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct ubifs_dent_node *xent, *pxent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	int err, len, written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	struct fscrypt_name nm = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		dentry, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	down_read(&host_ui->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	len = host_ui->xattr_names + host_ui->xattr_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		 * We should return the minimum buffer size which will fit a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		 * null-terminated list of all the extended attribute names.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		err = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (len > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		err = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	lowest_xent_key(c, &key, host->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		xent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		if (IS_ERR(xent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			err = PTR_ERR(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		fname_name(&nm) = xent->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		fname_len(&nm) = le16_to_cpu(xent->nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (xattr_visible(xent->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			written += fname_len(&nm) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		pxent = xent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		key_read(c, &xent->key, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	up_read(&host_ui->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		ubifs_err(c, "cannot find next direntry, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	ubifs_assert(c, written <= size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	up_read(&host_ui->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int remove_xattr(struct ubifs_info *c, struct inode *host,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			struct inode *inode, const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct ubifs_inode *host_ui = ubifs_inode(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 				.dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	ubifs_assert(c, ui->data_len == inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	mutex_lock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	host->i_ctime = current_time(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	host_ui->xattr_cnt -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	host_ui->xattr_names -= fname_len(nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	err = ubifs_jnl_delete_xattr(c, host, inode, nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	host_ui->xattr_cnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	host_ui->xattr_names += fname_len(nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	mutex_unlock(&host_ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int ubifs_purge_xattrs(struct inode *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct ubifs_info *c = host->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	struct ubifs_dent_node *xent, *pxent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct inode *xino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	struct fscrypt_name nm = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if (ubifs_inode(host)->xattr_cnt <= ubifs_xattr_max_cnt(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	ubifs_warn(c, "inode %lu has too many xattrs, doing a non-atomic deletion",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		   host->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	down_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	lowest_xent_key(c, &key, host->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		xent = ubifs_tnc_next_ent(c, &key, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		if (IS_ERR(xent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			err = PTR_ERR(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		fname_name(&nm) = xent->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		fname_len(&nm) = le16_to_cpu(xent->nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		if (IS_ERR(xino)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			err = PTR_ERR(xino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			ubifs_err(c, "dead directory entry '%s', error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 				  xent->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			ubifs_ro_mode(c, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			kfree(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		ubifs_assert(c, ubifs_inode(xino)->xattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		clear_nlink(xino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		err = remove_xattr(c, host, xino, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			kfree(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			iput(xino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 			ubifs_err(c, "cannot remove xattr, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		iput(xino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		pxent = xent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		key_read(c, &xent->key, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	kfree(pxent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	up_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (err != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		ubifs_err(c, "cannot find next direntry, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	up_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  * ubifs_evict_xattr_inode - Evict an xattr inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  * @xattr_inum: xattr inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  * When an inode that hosts xattrs is being removed we have to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  * that cached inodes of the xattrs also get removed from the inode cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  * otherwise we'd waste memory. This function looks up an inode from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * inode cache and clears the link counter such that iput() will evict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  * the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	inode = ilookup(c->vfs_sb, xattr_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int ubifs_xattr_remove(struct inode *host, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct ubifs_info *c = host->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	struct ubifs_dent_node *xent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	ubifs_assert(c, inode_is_locked(host));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (fname_len(&nm) > UBIFS_MAX_NLEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (!xent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	down_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	xent_key_init(c, &key, host->i_ino, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 			err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	inode = iget_xattr(c, le64_to_cpu(xent->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	ubifs_assert(c, inode->i_nlink == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	err = remove_xattr(c, host, inode, &nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	/* If @i_nlink is 0, 'iput()' will delete the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	up_write(&ubifs_inode(host)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	kfree(xent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) #ifdef CONFIG_UBIFS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		      void *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	const struct xattr *xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			       strlen(xattr->name) + 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		strcpy(name, XATTR_SECURITY_PREFIX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		 * creating a new inode without holding the inode rwsem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		 * no need to check whether inode is locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		err = ubifs_xattr_set(inode, name, xattr->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 				      xattr->value_len, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int ubifs_init_security(struct inode *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 			const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	err = security_inode_init_security(inode, dentry, qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 					   &init_xattrs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		struct ubifs_info *c = dentry->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		ubifs_err(c, "cannot initialize security for inode %lu, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 			  inode->i_ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static int xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			   struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			   const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			   int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		inode->i_ino, dentry, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	name = xattr_full_name(handler, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	return ubifs_xattr_get(inode, name, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) static int xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			   struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			   const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			   size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		name, inode->i_ino, dentry, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	name = xattr_full_name(handler, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		return ubifs_xattr_set(inode, name, value, size, flags, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		return ubifs_xattr_remove(inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static const struct xattr_handler ubifs_user_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	.prefix = XATTR_USER_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	.get = xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	.set = xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static const struct xattr_handler ubifs_trusted_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	.prefix = XATTR_TRUSTED_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	.get = xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	.set = xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) #ifdef CONFIG_UBIFS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) static const struct xattr_handler ubifs_security_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	.prefix = XATTR_SECURITY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	.get = xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	.set = xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) const struct xattr_handler *ubifs_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	&ubifs_user_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	&ubifs_trusted_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) #ifdef CONFIG_UBIFS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	&ubifs_security_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) };