xattr.c (2612e3bbc0386368a850140a6c9b990cd496a5ec) xattr.c (5de75970c9fd7220e394b76e6d20fbafa1369b5a)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 File: fs/xattr.c
4
5 Extended attribute handling.
6
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>

--- 1026 unchanged lines hidden (view full) ---

1035{
1036 size_t prefix_len = strlen(xattr_prefix(handler));
1037
1038 return name - prefix_len;
1039}
1040EXPORT_SYMBOL(xattr_full_name);
1041
1042/**
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 File: fs/xattr.c
4
5 Extended attribute handling.
6
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>

--- 1026 unchanged lines hidden (view full) ---

1035{
1036 size_t prefix_len = strlen(xattr_prefix(handler));
1037
1038 return name - prefix_len;
1039}
1040EXPORT_SYMBOL(xattr_full_name);
1041
1042/**
1043 * free_simple_xattr - free an xattr object
1043 * simple_xattr_free - free an xattr object
1044 * @xattr: the xattr object
1045 *
1046 * Free the xattr object. Can handle @xattr being NULL.
1047 */
1044 * @xattr: the xattr object
1045 *
1046 * Free the xattr object. Can handle @xattr being NULL.
1047 */
1048static inline void free_simple_xattr(struct simple_xattr *xattr)
1048void simple_xattr_free(struct simple_xattr *xattr)
1049{
1050 if (xattr)
1051 kfree(xattr->name);
1052 kvfree(xattr);
1053}
1054
1055/**
1056 * simple_xattr_alloc - allocate new xattr object

--- 102 unchanged lines hidden (view full) ---

1159
1160/**
1161 * simple_xattr_set - set an xattr object
1162 * @xattrs: the header of the xattr object
1163 * @name: the name of the xattr to retrieve
1164 * @value: the value to store along the xattr
1165 * @size: the size of @value
1166 * @flags: the flags determining how to set the xattr
1049{
1050 if (xattr)
1051 kfree(xattr->name);
1052 kvfree(xattr);
1053}
1054
1055/**
1056 * simple_xattr_alloc - allocate new xattr object

--- 102 unchanged lines hidden (view full) ---

1159
1160/**
1161 * simple_xattr_set - set an xattr object
1162 * @xattrs: the header of the xattr object
1163 * @name: the name of the xattr to retrieve
1164 * @value: the value to store along the xattr
1165 * @size: the size of @value
1166 * @flags: the flags determining how to set the xattr
1167 * @removed_size: the size of the removed xattr
1168 *
1169 * Set a new xattr object.
1170 * If @value is passed a new xattr object will be allocated. If XATTR_REPLACE
1171 * is specified in @flags a matching xattr object for @name must already exist.
1172 * If it does it will be replaced with the new xattr object. If it doesn't we
1173 * fail. If XATTR_CREATE is specified and a matching xattr does already exist
1174 * we fail. If it doesn't we create a new xattr. If @flags is zero we simply
1175 * insert the new xattr replacing any existing one.
1176 *
1177 * If @value is empty and a matching xattr object is found we delete it if
1178 * XATTR_REPLACE is specified in @flags or @flags is zero.
1179 *
1180 * If @value is empty and no matching xattr object for @name is found we do
1181 * nothing if XATTR_CREATE is specified in @flags or @flags is zero. For
1182 * XATTR_REPLACE we fail as mentioned above.
1183 *
1167 *
1168 * Set a new xattr object.
1169 * If @value is passed a new xattr object will be allocated. If XATTR_REPLACE
1170 * is specified in @flags a matching xattr object for @name must already exist.
1171 * If it does it will be replaced with the new xattr object. If it doesn't we
1172 * fail. If XATTR_CREATE is specified and a matching xattr does already exist
1173 * we fail. If it doesn't we create a new xattr. If @flags is zero we simply
1174 * insert the new xattr replacing any existing one.
1175 *
1176 * If @value is empty and a matching xattr object is found we delete it if
1177 * XATTR_REPLACE is specified in @flags or @flags is zero.
1178 *
1179 * If @value is empty and no matching xattr object for @name is found we do
1180 * nothing if XATTR_CREATE is specified in @flags or @flags is zero. For
1181 * XATTR_REPLACE we fail as mentioned above.
1182 *
1184 * Return: On success zero and on error a negative error code is returned.
1183 * Return: On success, the removed or replaced xattr is returned, to be freed
1184 * by the caller; or NULL if none. On failure a negative error code is returned.
1185 */
1185 */
1186int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
1187 const void *value, size_t size, int flags,
1188 ssize_t *removed_size)
1186struct simple_xattr *simple_xattr_set(struct simple_xattrs *xattrs,
1187 const char *name, const void *value,
1188 size_t size, int flags)
1189{
1189{
1190 struct simple_xattr *xattr = NULL, *new_xattr = NULL;
1190 struct simple_xattr *old_xattr = NULL, *new_xattr = NULL;
1191 struct rb_node *parent = NULL, **rbp;
1192 int err = 0, ret;
1193
1191 struct rb_node *parent = NULL, **rbp;
1192 int err = 0, ret;
1193
1194 if (removed_size)
1195 *removed_size = -1;
1196
1197 /* value == NULL means remove */
1198 if (value) {
1199 new_xattr = simple_xattr_alloc(value, size);
1200 if (!new_xattr)
1194 /* value == NULL means remove */
1195 if (value) {
1196 new_xattr = simple_xattr_alloc(value, size);
1197 if (!new_xattr)
1201 return -ENOMEM;
1198 return ERR_PTR(-ENOMEM);
1202
1203 new_xattr->name = kstrdup(name, GFP_KERNEL);
1204 if (!new_xattr->name) {
1199
1200 new_xattr->name = kstrdup(name, GFP_KERNEL);
1201 if (!new_xattr->name) {
1205 free_simple_xattr(new_xattr);
1206 return -ENOMEM;
1202 simple_xattr_free(new_xattr);
1203 return ERR_PTR(-ENOMEM);
1207 }
1208 }
1209
1210 write_lock(&xattrs->lock);
1211 rbp = &xattrs->rb_root.rb_node;
1212 while (*rbp) {
1213 parent = *rbp;
1214 ret = rbtree_simple_xattr_cmp(name, *rbp);
1215 if (ret < 0)
1216 rbp = &(*rbp)->rb_left;
1217 else if (ret > 0)
1218 rbp = &(*rbp)->rb_right;
1219 else
1204 }
1205 }
1206
1207 write_lock(&xattrs->lock);
1208 rbp = &xattrs->rb_root.rb_node;
1209 while (*rbp) {
1210 parent = *rbp;
1211 ret = rbtree_simple_xattr_cmp(name, *rbp);
1212 if (ret < 0)
1213 rbp = &(*rbp)->rb_left;
1214 else if (ret > 0)
1215 rbp = &(*rbp)->rb_right;
1216 else
1220 xattr = rb_entry(*rbp, struct simple_xattr, rb_node);
1221 if (xattr)
1217 old_xattr = rb_entry(*rbp, struct simple_xattr, rb_node);
1218 if (old_xattr)
1222 break;
1223 }
1224
1219 break;
1220 }
1221
1225 if (xattr) {
1222 if (old_xattr) {
1226 /* Fail if XATTR_CREATE is requested and the xattr exists. */
1227 if (flags & XATTR_CREATE) {
1228 err = -EEXIST;
1229 goto out_unlock;
1230 }
1231
1232 if (new_xattr)
1223 /* Fail if XATTR_CREATE is requested and the xattr exists. */
1224 if (flags & XATTR_CREATE) {
1225 err = -EEXIST;
1226 goto out_unlock;
1227 }
1228
1229 if (new_xattr)
1233 rb_replace_node(&xattr->rb_node, &new_xattr->rb_node,
1234 &xattrs->rb_root);
1230 rb_replace_node(&old_xattr->rb_node,
1231 &new_xattr->rb_node, &xattrs->rb_root);
1235 else
1232 else
1236 rb_erase(&xattr->rb_node, &xattrs->rb_root);
1237 if (!err && removed_size)
1238 *removed_size = xattr->size;
1233 rb_erase(&old_xattr->rb_node, &xattrs->rb_root);
1239 } else {
1240 /* Fail if XATTR_REPLACE is requested but no xattr is found. */
1241 if (flags & XATTR_REPLACE) {
1242 err = -ENODATA;
1243 goto out_unlock;
1244 }
1245
1246 /*

--- 8 unchanged lines hidden (view full) ---

1255 /*
1256 * If XATTR_CREATE or no flags are specified and neither an
1257 * old or new xattr exist then we don't need to do anything.
1258 */
1259 }
1260
1261out_unlock:
1262 write_unlock(&xattrs->lock);
1234 } else {
1235 /* Fail if XATTR_REPLACE is requested but no xattr is found. */
1236 if (flags & XATTR_REPLACE) {
1237 err = -ENODATA;
1238 goto out_unlock;
1239 }
1240
1241 /*

--- 8 unchanged lines hidden (view full) ---

1250 /*
1251 * If XATTR_CREATE or no flags are specified and neither an
1252 * old or new xattr exist then we don't need to do anything.
1253 */
1254 }
1255
1256out_unlock:
1257 write_unlock(&xattrs->lock);
1263 if (err)
1264 free_simple_xattr(new_xattr);
1265 else
1266 free_simple_xattr(xattr);
1267 return err;
1268
1258 if (!err)
1259 return old_xattr;
1260 simple_xattr_free(new_xattr);
1261 return ERR_PTR(err);
1269}
1270
1271static bool xattr_is_trusted(const char *name)
1272{
1273 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1274}
1275
1276/**

--- 104 unchanged lines hidden (view full) ---

1381 rbp = rb_first(&xattrs->rb_root);
1382 while (rbp) {
1383 struct simple_xattr *xattr;
1384 struct rb_node *rbp_next;
1385
1386 rbp_next = rb_next(rbp);
1387 xattr = rb_entry(rbp, struct simple_xattr, rb_node);
1388 rb_erase(&xattr->rb_node, &xattrs->rb_root);
1262}
1263
1264static bool xattr_is_trusted(const char *name)
1265{
1266 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1267}
1268
1269/**

--- 104 unchanged lines hidden (view full) ---

1374 rbp = rb_first(&xattrs->rb_root);
1375 while (rbp) {
1376 struct simple_xattr *xattr;
1377 struct rb_node *rbp_next;
1378
1379 rbp_next = rb_next(rbp);
1380 xattr = rb_entry(rbp, struct simple_xattr, rb_node);
1381 rb_erase(&xattr->rb_node, &xattrs->rb_root);
1389 free_simple_xattr(xattr);
1382 simple_xattr_free(xattr);
1390 rbp = rbp_next;
1391 }
1392}
1383 rbp = rbp_next;
1384 }
1385}