hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/btrfs/xattr.c
....@@ -76,9 +76,8 @@
7676 return ret;
7777 }
7878
79
-static int do_setxattr(struct btrfs_trans_handle *trans,
80
- struct inode *inode, const char *name,
81
- const void *value, size_t size, int flags)
79
+int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
80
+ const char *name, const void *value, size_t size, int flags)
8281 {
8382 struct btrfs_dir_item *di = NULL;
8483 struct btrfs_root *root = BTRFS_I(inode)->root;
....@@ -86,6 +85,8 @@
8685 struct btrfs_path *path;
8786 size_t name_len = strlen(name);
8887 int ret = 0;
88
+
89
+ ASSERT(trans);
8990
9091 if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
9192 return -ENOSPC;
....@@ -174,7 +175,7 @@
174175 char *ptr;
175176
176177 if (size > old_data_len) {
177
- if (btrfs_leaf_free_space(fs_info, leaf) <
178
+ if (btrfs_leaf_free_space(leaf) <
178179 (size - old_data_len)) {
179180 ret = -ENOSPC;
180181 goto out;
....@@ -184,17 +185,15 @@
184185 if (old_data_len + name_len + sizeof(*di) == item_size) {
185186 /* No other xattrs packed in the same leaf item. */
186187 if (size > old_data_len)
187
- btrfs_extend_item(fs_info, path,
188
- size - old_data_len);
188
+ btrfs_extend_item(path, size - old_data_len);
189189 else if (size < old_data_len)
190
- btrfs_truncate_item(fs_info, path,
191
- data_size, 1);
190
+ btrfs_truncate_item(path, data_size, 1);
192191 } else {
193192 /* There are other xattrs packed in the same item. */
194193 ret = btrfs_delete_one_dir_name(trans, root, path, di);
195194 if (ret)
196195 goto out;
197
- btrfs_extend_item(fs_info, path, data_size);
196
+ btrfs_extend_item(path, data_size);
198197 }
199198
200199 item = btrfs_item_nr(slot);
....@@ -214,40 +213,61 @@
214213 }
215214 out:
216215 btrfs_free_path(path);
216
+ if (!ret) {
217
+ set_bit(BTRFS_INODE_COPY_EVERYTHING,
218
+ &BTRFS_I(inode)->runtime_flags);
219
+ clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
220
+ }
217221 return ret;
218222 }
219223
220224 /*
221225 * @value: "" makes the attribute to empty, NULL removes it
222226 */
223
-int btrfs_setxattr(struct btrfs_trans_handle *trans,
224
- struct inode *inode, const char *name,
225
- const void *value, size_t size, int flags)
227
+int btrfs_setxattr_trans(struct inode *inode, const char *name,
228
+ const void *value, size_t size, int flags)
226229 {
227230 struct btrfs_root *root = BTRFS_I(inode)->root;
231
+ struct btrfs_trans_handle *trans;
232
+ const bool start_trans = (current->journal_info == NULL);
228233 int ret;
229234
230
- if (btrfs_root_readonly(root))
231
- return -EROFS;
235
+ if (start_trans) {
236
+ /*
237
+ * 1 unit for inserting/updating/deleting the xattr
238
+ * 1 unit for the inode item update
239
+ */
240
+ trans = btrfs_start_transaction(root, 2);
241
+ if (IS_ERR(trans))
242
+ return PTR_ERR(trans);
243
+ } else {
244
+ /*
245
+ * This can happen when smack is enabled and a directory is being
246
+ * created. It happens through d_instantiate_new(), which calls
247
+ * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
248
+ * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
249
+ * inode. We have already reserved space for the xattr and inode
250
+ * update at btrfs_mkdir(), so just use the transaction handle.
251
+ * We don't join or start a transaction, as that will reset the
252
+ * block_rsv of the handle and trigger a warning for the start
253
+ * case.
254
+ */
255
+ ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
256
+ XATTR_SECURITY_PREFIX_LEN) == 0);
257
+ trans = current->journal_info;
258
+ }
232259
233
- if (trans)
234
- return do_setxattr(trans, inode, name, value, size, flags);
235
-
236
- trans = btrfs_start_transaction(root, 2);
237
- if (IS_ERR(trans))
238
- return PTR_ERR(trans);
239
-
240
- ret = do_setxattr(trans, inode, name, value, size, flags);
260
+ ret = btrfs_setxattr(trans, inode, name, value, size, flags);
241261 if (ret)
242262 goto out;
243263
244264 inode_inc_iversion(inode);
245265 inode->i_ctime = current_time(inode);
246
- set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
247266 ret = btrfs_update_inode(trans, root, inode);
248267 BUG_ON(ret);
249268 out:
250
- btrfs_end_transaction(trans);
269
+ if (start_trans)
270
+ btrfs_end_transaction(trans);
251271 return ret;
252272 }
253273
....@@ -358,7 +378,8 @@
358378
359379 static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
360380 struct dentry *unused, struct inode *inode,
361
- const char *name, void *buffer, size_t size)
381
+ const char *name, void *buffer, size_t size,
382
+ int flags)
362383 {
363384 name = xattr_full_name(handler, name);
364385 return btrfs_getxattr(inode, name, buffer, size);
....@@ -369,8 +390,11 @@
369390 const char *name, const void *buffer,
370391 size_t size, int flags)
371392 {
393
+ if (btrfs_root_readonly(BTRFS_I(inode)->root))
394
+ return -EROFS;
395
+
372396 name = xattr_full_name(handler, name);
373
- return btrfs_setxattr(NULL, inode, name, buffer, size, flags);
397
+ return btrfs_setxattr_trans(inode, name, buffer, size, flags);
374398 }
375399
376400 static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
....@@ -378,8 +402,30 @@
378402 const char *name, const void *value,
379403 size_t size, int flags)
380404 {
405
+ int ret;
406
+ struct btrfs_trans_handle *trans;
407
+ struct btrfs_root *root = BTRFS_I(inode)->root;
408
+
381409 name = xattr_full_name(handler, name);
382
- return btrfs_set_prop(inode, name, value, size, flags);
410
+ ret = btrfs_validate_prop(name, value, size);
411
+ if (ret)
412
+ return ret;
413
+
414
+ trans = btrfs_start_transaction(root, 2);
415
+ if (IS_ERR(trans))
416
+ return PTR_ERR(trans);
417
+
418
+ ret = btrfs_set_prop(trans, inode, name, value, size, flags);
419
+ if (!ret) {
420
+ inode_inc_iversion(inode);
421
+ inode->i_ctime = current_time(inode);
422
+ ret = btrfs_update_inode(trans, root, inode);
423
+ BUG_ON(ret);
424
+ }
425
+
426
+ btrfs_end_transaction(trans);
427
+
428
+ return ret;
383429 }
384430
385431 static const struct xattr_handler btrfs_security_xattr_handler = {
....@@ -419,10 +465,10 @@
419465 };
420466
421467 static int btrfs_initxattrs(struct inode *inode,
422
- const struct xattr *xattr_array, void *fs_info)
468
+ const struct xattr *xattr_array, void *fs_private)
423469 {
470
+ struct btrfs_trans_handle *trans = fs_private;
424471 const struct xattr *xattr;
425
- struct btrfs_trans_handle *trans = fs_info;
426472 unsigned int nofs_flag;
427473 char *name;
428474 int err = 0;
....@@ -442,7 +488,7 @@
442488 strcpy(name, XATTR_SECURITY_PREFIX);
443489 strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
444490 err = btrfs_setxattr(trans, inode, name, xattr->value,
445
- xattr->value_len, 0);
491
+ xattr->value_len, 0);
446492 kfree(name);
447493 if (err < 0)
448494 break;