Debian Codegraph @

xattr.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
4 */
5
6 #include <linux/buffer_head.h>
7 #include <linux/xattr.h>
8 #include <linux/blk_types.h>
9 #include "apfs.h"
10
11 /**
12 * apfs_xattr_from_query - Read the xattr record found by a successful query
13 * @query: the query that found the record
14 * @xattr: Return parameter. The xattr record found.
15 *
16 * Reads the xattr record into @xattr and performs some basic sanity checks
17 * as a protection against crafted filesystems. Returns 0 on success or
18 * -EFSCORRUPTED otherwise.
19 *
20 * The caller must not free @query while @xattr is in use, because @xattr->name
21 * and @xattr->xdata point to data on disk.
22 */
23 static int apfs_xattr_from_query(struct apfs_query *query,
24 struct apfs_xattr *xattr)
25 {
26 struct super_block *sb = query->node->object.sb;
27 struct apfs_xattr_val *xattr_val;
28 struct apfs_xattr_key *xattr_key;
29 char *raw = query->node->object.data;
30 int datalen = query->len - sizeof(*xattr_val);
31 int namelen = query->key_len - sizeof(*xattr_key);
32
33 if (namelen < 1 || datalen < 0) {
34 apfs_err(sb, "bad length of name (%d) or data (%d)", namelen, datalen);
35 return -EFSCORRUPTED;
36 }
37
38 xattr_val = (struct apfs_xattr_val *)(raw + query->off);
39 xattr_key = (struct apfs_xattr_key *)(raw + query->key_off);
40
41 if (namelen != le16_to_cpu(xattr_key->name_len)) {
42 apfs_err(sb, "inconsistent name length (%d vs %d)", namelen, le16_to_cpu(xattr_key->name_len));
43 return -EFSCORRUPTED;
44 }
45
46 /* The xattr name must be NULL-terminated */
47 if (xattr_key->name[namelen - 1] != 0) {
48 apfs_err(sb, "null termination missing");
49 return -EFSCORRUPTED;
50 }
51
52 xattr->has_dstream = le16_to_cpu(xattr_val->flags) &
53 APFS_XATTR_DATA_STREAM;
54
55 if (xattr->has_dstream && datalen != sizeof(struct apfs_xattr_dstream)) {
56 apfs_err(sb, "bad data length (%d)", datalen);
57 return -EFSCORRUPTED;
58 }
59 if (!xattr->has_dstream && datalen != le16_to_cpu(xattr_val->xdata_len)) {
60 apfs_err(sb, "inconsistent data length (%d vs %d)", datalen, le16_to_cpu(xattr_val->xdata_len));
61 return -EFSCORRUPTED;
62 }
63
64 xattr->name = xattr_key->name;
65 xattr->name_len = namelen - 1; /* Don't count the NULL termination */
66 xattr->xdata = xattr_val->xdata;
67 xattr->xdata_len = datalen;
68 return 0;
69 }
70
71 /**
72 * apfs_dstream_from_xattr - Get the dstream info for a dstream xattr
73 * @sb: filesystem superblock
74 * @xattr: in-memory xattr record (already sanity-checked)
75 * @dstream: on return, the data stream info
76 */
77 static void apfs_dstream_from_xattr(struct super_block *sb, struct apfs_xattr *xattr, struct apfs_dstream_info *dstream)
78 {
79 struct apfs_xattr_dstream *xdata = (void *)xattr->xdata;
80
81 dstream->ds_sb = sb;
82 dstream->ds_inode = NULL;
83 dstream->ds_id = le64_to_cpu(xdata->xattr_obj_id);
84 dstream->ds_size = le64_to_cpu(xdata->dstream.size);
85 dstream->ds_sparse_bytes = 0; /* Irrelevant for xattrs */
86
87 dstream->ds_cached_ext.len = 0;
88 dstream->ds_ext_dirty = false;
89 spin_lock_init(&dstream->ds_ext_lock);
90
91 /* Xattrs can't be cloned */
92 dstream->ds_shared = false;
93 }
94
95 /**
96 * apfs_xattr_extents_read - Read the value of a xattr from its extents
97 * @parent: inode the attribute belongs to
98 * @xattr: the xattr structure
99 * @buffer: where to copy the attribute value
100 * @size: size of @buffer
101 * @only_whole: are partial reads banned?
102 *
103 * Copies the value of @xattr to @buffer, if provided. If @buffer is NULL, just
104 * computes the size of the buffer required.
105 *
106 * Returns the number of bytes used/required, or a negative error code in case
107 * of failure.
108 */
109 static int apfs_xattr_extents_read(struct inode *parent,
110 struct apfs_xattr *xattr,
111 void *buffer, size_t size, bool only_whole)
112 {
113 struct super_block *sb = parent->i_sb;
114 struct apfs_dstream_info *dstream;
115 int length, ret;
116
117 dstream = kzalloc(sizeof(*dstream), GFP_KERNEL);
118 if (!dstream)
119 return -ENOMEM;
120 apfs_dstream_from_xattr(sb, xattr, dstream);
121
122 length = dstream->ds_size;
123 if (length < 0 || length < dstream->ds_size) {
124 /* TODO: avoid overflow here for huge compressed files */
125 apfs_warn(sb, "xattr is too big to read on linux (0x%llx)", dstream->ds_size);
126 ret = -E2BIG;
127 goto out;
128 }
129
130 if (!buffer) {
131 /* All we want is the length */
132 ret = length;
133 goto out;
134 }
135
136 if (only_whole) {
137 if (length > size) {
138 /* xattr won't fit in the buffer */
139 ret = -ERANGE;
140 goto out;
141 }
142 } else {
143 if (length > size)
144 length = size;
145 }
146
147 ret = apfs_nonsparse_dstream_read(dstream, buffer, length, 0 /* offset */);
148 if (ret == 0)
149 ret = length;
150
151 out:
152 kfree(dstream);
153 return ret;
154 }
155
156 /**
157 * apfs_xattr_inline_read - Read the value of an inline xattr
158 * @xattr: the xattr structure
159 * @buffer: where to copy the attribute value
160 * @size: size of @buffer
161 * @only_whole: are partial reads banned?
162 *
163 * Copies the inline value of @xattr to @buffer, if provided. If @buffer is
164 * NULL, just computes the size of the buffer required.
165 *
166 * Returns the number of bytes used/required, or a negative error code in case
167 * of failure.
168 */
169 static int apfs_xattr_inline_read(struct apfs_xattr *xattr, void *buffer, size_t size, bool only_whole)
170 {
171 int length = xattr->xdata_len;
172
173 if (!buffer) /* All we want is the length */
174 return length;
175 if (only_whole) {
176 if (length > size) /* xattr won't fit in the buffer */
177 return -ERANGE;
178 } else {
179 if (length > size)
180 length = size;
181 }
182 memcpy(buffer, xattr->xdata, length);
183 return length;
184 }
185
186 /**
187 * apfs_xattr_get_compressed_data - Get the compressed data in a named attribute
188 * @inode: inode the attribute belongs to
189 * @name: name of the attribute
190 * @cdata: compressed data struct to set on return
191 *
192 * Returns 0 on success or a negative error code in case of failure.
193 */
194 int apfs_xattr_get_compressed_data(struct inode *inode, const char *name, struct apfs_compressed_data *cdata)
195 {
196 struct super_block *sb = inode->i_sb;
197 struct apfs_sb_info *sbi = APFS_SB(sb);
198 struct apfs_query *query;
199 struct apfs_xattr xattr;
200 u64 cnid = apfs_ino(inode);
201 int ret;
202
203
204 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
205 if (!query)
206 return -ENOMEM;
207 apfs_init_xattr_key(cnid, name, &query->key);
208 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
209
210 ret = apfs_btree_query(sb, &query);
211 if (ret) {
212 apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name);
213 goto done;
214 }
215
216 ret = apfs_xattr_from_query(query, &xattr);
217 if (ret) {
218 apfs_err(sb, "bad xattr record in inode 0x%llx", cnid);
219 goto done;
220 }
221
222 cdata->has_dstream = xattr.has_dstream;
223 if (cdata->has_dstream) {
224 struct apfs_dstream_info *dstream = NULL;
225
226 dstream = kzalloc(sizeof(*dstream), GFP_KERNEL);
227 if (!dstream) {
228 ret = -ENOMEM;
229 goto done;
230 }
231 apfs_dstream_from_xattr(sb, &xattr, dstream);
232
233 cdata->dstream = dstream;
234 cdata->size = dstream->ds_size;
235 } else {
236 void *data = NULL;
237 int len;
238
239 len = xattr.xdata_len;
240 if (len > APFS_XATTR_MAX_EMBEDDED_SIZE) {
241 apfs_err(sb, "inline xattr too big");
242 ret = -EFSCORRUPTED;
243 goto done;
244 }
245 data = kzalloc(len, GFP_KERNEL);
246 if (!data) {
247 ret = -ENOMEM;
248 goto done;
249 }
250 memcpy(data, xattr.xdata, len);
251
252 cdata->data = data;
253 cdata->size = len;
254 }
255 ret = 0;
256
257 done:
258 apfs_free_query(query);
259 return ret;
260 }
261
262 /**
263 * apfs_release_compressed_data - Clean up a compressed data struct
264 * @cdata: struct to clean up (but not free)
265 */
266 void apfs_release_compressed_data(struct apfs_compressed_data *cdata)
267 {
268 if (!cdata)
269 return;
270
271 if (cdata->has_dstream) {
272 kfree(cdata->dstream);
273 cdata->dstream = NULL;
274 } else {
275 kfree(cdata->data);
276 cdata->data = NULL;
277 }
278 }
279
280 /**
281 * apfs_compressed_data_read - Read from a compressed data struct
282 * @cdata: compressed data struct
283 * @buf: destination buffer
284 * @count: exact number of bytes to read
285 * @offset: dstream offset to read from
286 *
287 * Returns 0 on success or a negative error code in case of failure.
288 */
289 int apfs_compressed_data_read(struct apfs_compressed_data *cdata, void *buf, size_t count, u64 offset)
290 {
291 if (cdata->has_dstream)
292 return apfs_nonsparse_dstream_read(cdata->dstream, buf, count, offset);
293
294 if (offset > cdata->size || count > cdata->size - offset) {
295 apfs_err(NULL, "reading past the end (0x%llx-0x%llx)", offset, (unsigned long long)count);
296 /* No caller is expected to legitimately read out-of-bounds */
297 return -EFSCORRUPTED;
298 }
299 memcpy(buf, cdata->data + offset, count);
300 return 0;
301 }
302
303 /**
304 * __apfs_xattr_get - Find and read a named attribute
305 * @inode: inode the attribute belongs to
306 * @name: name of the attribute
307 * @buffer: where to copy the attribute value
308 * @size: size of @buffer
309 *
310 * This does the same as apfs_xattr_get(), but without taking any locks.
311 */
312 int __apfs_xattr_get(struct inode *inode, const char *name, void *buffer,
313 size_t size)
314 {
315 return ____apfs_xattr_get(inode, name, buffer, size, true /* only_whole */);
316 }
317
318 /**
319 * ____apfs_xattr_get - Find and read a named attribute, optionally header only
320 * @inode: inode the attribute belongs to
321 * @name: name of the attribute
322 * @buffer: where to copy the attribute value
323 * @size: size of @buffer
324 * @only_whole: must read complete (no partial header read allowed)
325 */
326 int ____apfs_xattr_get(struct inode *inode, const char *name, void *buffer,
327 size_t size, bool only_whole)
328 {
329 struct super_block *sb = inode->i_sb;
330 struct apfs_sb_info *sbi = APFS_SB(sb);
331 struct apfs_query *query;
332 struct apfs_xattr xattr;
333 u64 cnid = apfs_ino(inode);
334 int ret;
335
336 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
337 if (!query)
338 return -ENOMEM;
339 apfs_init_xattr_key(cnid, name, &query->key);
340 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
341
342 ret = apfs_btree_query(sb, &query);
343 if (ret) {
344 if (ret != -ENODATA)
345 apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name);
346 goto done;
347 }
348
349 ret = apfs_xattr_from_query(query, &xattr);
350 if (ret) {
351 apfs_err(sb, "bad xattr record in inode 0x%llx", cnid);
352 goto done;
353 }
354
355 if (xattr.has_dstream)
356 ret = apfs_xattr_extents_read(inode, &xattr, buffer, size, only_whole);
357 else
358 ret = apfs_xattr_inline_read(&xattr, buffer, size, only_whole);
359
360 done:
361 apfs_free_query(query);
362 return ret;
363 }
364
365 /**
366 * apfs_xattr_get - Find and read a named attribute
367 * @inode: inode the attribute belongs to
368 * @name: name of the attribute
369 * @buffer: where to copy the attribute value
370 * @size: size of @buffer
371 *
372 * Finds an extended attribute and copies its value to @buffer, if provided. If
373 * @buffer is NULL, just computes the size of the buffer required.
374 *
375 * Returns the number of bytes used/required, or a negative error code in case
376 * of failure.
377 */
378 static int apfs_xattr_get(struct inode *inode, const char *name, void *buffer, size_t size)
379 {
380 struct apfs_nxsb_info *nxi = APFS_NXI(inode->i_sb);
381 int ret;
382
383 down_read(&nxi->nx_big_sem);
384 ret = __apfs_xattr_get(inode, name, buffer, size);
385 up_read(&nxi->nx_big_sem);
386 if (ret > XATTR_SIZE_MAX) {
387 apfs_warn(inode->i_sb, "xattr is too big to read on linux (%d)", ret);
388 return -E2BIG;
389 }
390 return ret;
391 }
392
393 static int apfs_xattr_osx_get(const struct xattr_handler *handler,
394 struct dentry *unused, struct inode *inode,
395 const char *name, void *buffer, size_t size)
396 {
397 /* Ignore the fake 'osx' prefix */
398 return apfs_xattr_get(inode, name, buffer, size);
399 }
400
401 /**
402 * apfs_delete_xattr - Delete an extended attribute
403 * @query: successful query pointing to the xattr to delete
404 *
405 * Returns 0 on success or a negative error code in case of failure.
406 */
407 static int apfs_delete_xattr(struct apfs_query *query)
408 {
409 struct super_block *sb = query->node->object.sb;
410 struct apfs_xattr xattr;
411 struct apfs_dstream_info *dstream;
412 int err;
413
414 err = apfs_xattr_from_query(query, &xattr);
415 if (err) {
416 apfs_err(sb, "bad xattr record");
417 return err;
418 }
419
420 if (!xattr.has_dstream)
421 return apfs_btree_remove(query);
422
423 dstream = kzalloc(sizeof(*dstream), GFP_KERNEL);
424 if (!dstream)
425 return -ENOMEM;
426 apfs_dstream_from_xattr(sb, &xattr, dstream);
427
428 /*
429 * Remove the xattr record before truncation, because truncation creates
430 * new queries and makes ours invalid. This stuff is all too subtle, I
431 * really need to add some assertions (TODO).
432 */
433 err = apfs_btree_remove(query);
434 if (err) {
435 apfs_err(sb, "removal failed");
436 goto fail;
437 }
438 err = apfs_truncate(dstream, 0);
439 if (err)
440 apfs_err(sb, "truncation failed for dstream 0x%llx", dstream->ds_id);
441
442 fail:
443 kfree(dstream);
444 return err;
445 }
446
447 /**
448 * apfs_delete_any_xattr - Delete any single xattr for a given inode
449 * @inode: the vfs inode
450 *
451 * Intended to be called repeatedly, to delete all the xattrs one by one.
452 * Returns -EAGAIN on success until the process is complete, then it returns
453 * 0. Returns other negative error codes in case of failure.
454 */
455 static int apfs_delete_any_xattr(struct inode *inode)
456 {
457 struct super_block *sb = inode->i_sb;
458 struct apfs_sb_info *sbi = APFS_SB(sb);
459 struct apfs_query *query;
460 int ret;
461
462 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
463 if (!query)
464 return -ENOMEM;
465 apfs_init_xattr_key(apfs_ino(inode), NULL /* name */, &query->key);
466 query->flags = APFS_QUERY_CAT | APFS_QUERY_ANY_NAME | APFS_QUERY_EXACT;
467
468 ret = apfs_btree_query(sb, &query);
469 if (ret) {
470 if (ret == -ENODATA)
471 ret = 0; /* No more xattrs, we are done */
472 else
473 apfs_err(sb, "query failed for ino 0x%llx", apfs_ino(inode));
474 goto out;
475 }
476
477 ret = apfs_delete_xattr(query);
478 if (!ret)
479 ret = -EAGAIN;
480 else
481 apfs_err(sb, "xattr deletion failed");
482
483 out:
484 apfs_free_query(query);
485 return ret;
486 }
487
488 /**
489 * apfs_delete_all_xattrs - Delete all xattrs for a given inode
490 * @inode: the vfs inode
491 *
492 * Returns 0 on success or a negative error code in case of failure.
493 */
494 int apfs_delete_all_xattrs(struct inode *inode)
495 {
496 struct apfs_nxsb_info *nxi = APFS_NXI(inode->i_sb);
497 int ret;
498
499 lockdep_assert_held_write(&nxi->nx_big_sem);
500
501 do {
502 ret = apfs_delete_any_xattr(inode);
503 } while (ret == -EAGAIN);
504
505 return ret;
506 }
507
508 /**
509 * apfs_build_xattr_key - Allocate and initialize the key for a xattr record
510 * @name: xattr name
511 * @ino: inode number for xattr's owner
512 * @key_p: on return, a pointer to the new on-disk key structure
513 *
514 * Returns the length of the key, or a negative error code in case of failure.
515 */
516 static int apfs_build_xattr_key(const char *name, u64 ino, struct apfs_xattr_key **key_p)
517 {
518 struct apfs_xattr_key *key;
519 u16 namelen = strlen(name) + 1; /* We count the null-termination */
520 int key_len;
521
522 key_len = sizeof(*key) + namelen;
523 key = kmalloc(key_len, GFP_KERNEL);
524 if (!key)
525 return -ENOMEM;
526
527 apfs_key_set_hdr(APFS_TYPE_XATTR, ino, key);
528 key->name_len = cpu_to_le16(namelen);
529 strscpy(key->name, name, namelen);
530
531 *key_p = key;
532 return key_len;
533 }
534
535 /**
536 * apfs_build_dstream_xattr_val - Allocate and init value for a dstream xattr
537 * @dstream: data stream info
538 * @val_p: on return, a pointer to the new on-disk value structure
539 *
540 * Returns the length of the value, or a negative error code in case of failure.
541 */
542 static int apfs_build_dstream_xattr_val(struct apfs_dstream_info *dstream, struct apfs_xattr_val **val_p)
543 {
544 struct apfs_xattr_val *val;
545 struct apfs_xattr_dstream *dstream_raw;
546 int val_len;
547
548 val_len = sizeof(*val) + sizeof(*dstream_raw);
549 val = kzalloc(val_len, GFP_KERNEL);
550 if (!val)
551 return -ENOMEM;
552
553 val->flags = cpu_to_le16(APFS_XATTR_DATA_STREAM);
554 val->xdata_len = cpu_to_le16(sizeof(*dstream_raw));
555
556 dstream_raw = (void *)val->xdata;
557 dstream_raw->xattr_obj_id = cpu_to_le64(dstream->ds_id);
558 dstream_raw->dstream.size = cpu_to_le64(dstream->ds_size);
559 dstream_raw->dstream.alloced_size = cpu_to_le64(apfs_alloced_size(dstream));
560 if (apfs_vol_is_encrypted(dstream->ds_sb))
561 dstream_raw->dstream.default_crypto_id = cpu_to_le64(dstream->ds_id);
562
563 *val_p = val;
564 return val_len;
565 }
566
567 /**
568 * apfs_build_inline_xattr_val - Allocate and init value for an inline xattr
569 * @value: content of the xattr
570 * @size: size of @value
571 * @val_p: on return, a pointer to the new on-disk value structure
572 *
573 * Returns the length of the value, or a negative error code in case of failure.
574 */
575 static int apfs_build_inline_xattr_val(const void *value, size_t size, struct apfs_xattr_val **val_p)
576 {
577 struct apfs_xattr_val *val;
578 int val_len;
579
580 val_len = sizeof(*val) + size;
581 val = kmalloc(val_len, GFP_KERNEL);
582 if (!val)
583 return -ENOMEM;
584
585 val->flags = cpu_to_le16(APFS_XATTR_DATA_EMBEDDED);
586 val->xdata_len = cpu_to_le16(size);
587 memcpy(val->xdata, value, size);
588
589 *val_p = val;
590 return val_len;
591 }
592
593 /**
594 * apfs_create_xattr_dstream - Create the extents for a dstream xattr
595 * @sb: filesystem superblock
596 * @value: value for the attribute
597 * @size: sizeo of @value
598 *
599 * Returns the info for the created data stream, or an error pointer in case
600 * of failure.
601 */
602 static struct apfs_dstream_info *apfs_create_xattr_dstream(struct super_block *sb, const void *value, size_t size)
603 {
604 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
605 struct apfs_dstream_info *dstream;
606 int blkcnt, i;
607 int err;
608
609 dstream = kzalloc(sizeof(*dstream), GFP_KERNEL);
610 if (!dstream)
611 return ERR_PTR(-ENOMEM);
612 dstream->ds_sb = sb;
613 spin_lock_init(&dstream->ds_ext_lock);
614
615 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
616 dstream->ds_id = le64_to_cpu(vsb_raw->apfs_next_obj_id);
617 le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
618
619 blkcnt = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
620 for (i = 0; i < blkcnt; i++) {
621 struct buffer_head *bh;
622 int off, tocopy;
623 u64 bno;
624
625 err = apfs_dstream_get_new_bno(dstream, i, &bno);
626 if (err) {
627 apfs_err(sb, "failed to get new block in dstream 0x%llx", dstream->ds_id);
628 goto fail;
629 }
630 bh = apfs_sb_bread(sb, bno);
631 if (!bh) {
632 apfs_err(sb, "failed to read new block");
633 err = -EIO;
634 goto fail;
635 }
636
637 err = apfs_transaction_join(sb, bh);
638 if (err) {
639 brelse(bh);
640 goto fail;
641 }
642
643 off = i << sb->s_blocksize_bits;
644 tocopy = min(sb->s_blocksize, (unsigned long)(size - off));
645 memcpy(bh->b_data, value + off, tocopy);
646 if (tocopy < sb->s_blocksize)
647 memset(bh->b_data + tocopy, 0, sb->s_blocksize - tocopy);
648 brelse(bh);
649
650 dstream->ds_size += tocopy;
651 }
652
653 err = apfs_flush_extent_cache(dstream);
654 if (err) {
655 apfs_err(sb, "extent cache flush failed for dstream 0x%llx", dstream->ds_id);
656 goto fail;
657 }
658 return dstream;
659
660 fail:
661 kfree(dstream);
662 return ERR_PTR(err);
663 }
664
665 /**
666 * apfs_xattr_dstream_from_query - Extract the dstream from a xattr record
667 * @query: the query that found the record
668 * @dstream_p: on return, the newly allocated dstream info (or NULL if none)
669 *
670 * Returns 0 on success, or a negative error code in case of failure.
671 */
672 static int apfs_xattr_dstream_from_query(struct apfs_query *query, struct apfs_dstream_info **dstream_p)
673 {
674 struct super_block *sb = query->node->object.sb;
675 struct apfs_dstream_info *dstream = NULL;
676 struct apfs_xattr xattr;
677 int err;
678
679 err = apfs_xattr_from_query(query, &xattr);
680 if (err) {
681 apfs_err(sb, "bad xattr record");
682 return err;
683 }
684
685 if (xattr.has_dstream) {
686 dstream = kzalloc(sizeof(*dstream), GFP_KERNEL);
687 if (!dstream)
688 return -ENOMEM;
689 apfs_dstream_from_xattr(sb, &xattr, dstream);
690 }
691 *dstream_p = dstream;
692 return 0;
693 }
694
695 /**
696 * apfs_xattr_set - Write a named attribute
697 * @inode: inode the attribute will belong to
698 * @name: name for the attribute
699 * @value: value for the attribute
700 * @size: size of @value
701 * @flags: XATTR_REPLACE and XATTR_CREATE
702 *
703 * Returns 0 on success, or a negative error code in case of failure.
704 */
705 int apfs_xattr_set(struct inode *inode, const char *name, const void *value,
706 size_t size, int flags)
707 {
708 struct super_block *sb = inode->i_sb;
709 struct apfs_sb_info *sbi = APFS_SB(sb);
710 struct apfs_query *query = NULL;
711 u64 cnid = apfs_ino(inode);
712 int key_len, val_len;
713 struct apfs_xattr_key *raw_key = NULL;
714 struct apfs_xattr_val *raw_val = NULL;
715 struct apfs_dstream_info *dstream = NULL;
716 struct apfs_dstream_info *old_dstream = NULL;
717 int ret;
718
719 if (size > APFS_XATTR_MAX_EMBEDDED_SIZE) {
720 dstream = apfs_create_xattr_dstream(sb, value, size);
721 if (IS_ERR(dstream)) {
722 apfs_err(sb, "failed to set xattr dstream for ino 0x%llx", apfs_ino(inode));
723 return PTR_ERR(dstream);
724 }
725 }
726
727 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
728 if (!query) {
729 ret = -ENOMEM;
730 goto done;
731 }
732 apfs_init_xattr_key(cnid, name, &query->key);
733 query->flags = APFS_QUERY_CAT | APFS_QUERY_EXACT;
734
735 ret = apfs_btree_query(sb, &query);
736 if (ret) {
737 if (ret != -ENODATA) {
738 apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name);
739 goto done;
740 } else if (flags & XATTR_REPLACE) {
741 goto done;
742 }
743 } else if (flags & XATTR_CREATE) {
744 ret = -EEXIST;
745 goto done;
746 } else if (!value) {
747 ret = apfs_delete_xattr(query);
748 if (ret)
749 apfs_err(sb, "xattr deletion failed");
750 goto done;
751 } else {
752 /* Remember the old dstream to clean it up later */
753 ret = apfs_xattr_dstream_from_query(query, &old_dstream);
754 if (ret) {
755 apfs_err(sb, "failed to get the old dstream");
756 goto done;
757 }
758 }
759
760 key_len = apfs_build_xattr_key(name, cnid, &raw_key);
761 if (key_len < 0) {
762 ret = key_len;
763 goto done;
764 }
765
766 if (dstream)
767 val_len = apfs_build_dstream_xattr_val(dstream, &raw_val);
768 else
769 val_len = apfs_build_inline_xattr_val(value, size, &raw_val);
770 if (val_len < 0) {
771 ret = val_len;
772 goto done;
773 }
774
775 /* For now this is the only system xattr we support */
776 if (strcmp(name, APFS_XATTR_NAME_SYMLINK) == 0)
777 raw_val->flags |= cpu_to_le16(APFS_XATTR_FILE_SYSTEM_OWNED);
778
779 if (ret)
780 ret = apfs_btree_insert(query, raw_key, key_len, raw_val, val_len);
781 else
782 ret = apfs_btree_replace(query, raw_key, key_len, raw_val, val_len);
783 if (ret) {
784 apfs_err(sb, "insertion/update failed for id 0x%llx (%s)", cnid, name);
785 goto done;
786 }
787
788 if (old_dstream) {
789 ret = apfs_truncate(old_dstream, 0);
790 if (ret)
791 apfs_err(sb, "truncation failed for dstream 0x%llx", old_dstream->ds_id);
792 }
793
794 done:
795 kfree(dstream);
796 kfree(old_dstream);
797 kfree(raw_val);
798 kfree(raw_key);
799 apfs_free_query(query);
800 return ret;
801 }
802
803 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
804 static int apfs_xattr_osx_set(const struct xattr_handler *handler,
805 struct dentry *unused, struct inode *inode, const char *name,
806 const void *value, size_t size, int flags)
807 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
808 static int apfs_xattr_osx_set(const struct xattr_handler *handler,
809 struct user_namespace *mnt_userns, struct dentry *unused,
810 struct inode *inode, const char *name, const void *value,
811 size_t size, int flags)
812 #else
813 static int apfs_xattr_osx_set(const struct xattr_handler *handler,
814 struct mnt_idmap *idmap, struct dentry *unused,
815 struct inode *inode, const char *name, const void *value,
816 size_t size, int flags)
817 #endif
818 {
819 struct super_block *sb = inode->i_sb;
820 int err;
821
822 err = apfs_transaction_start(sb, value ? APFS_TRANS_REG : APFS_TRANS_DEL);
823 if (err)
824 return err;
825
826 /* Ignore the fake 'osx' prefix */
827 err = apfs_xattr_set(inode, name, value, size, flags);
828 if (err)
829 goto fail;
830
831 err = apfs_transaction_commit(sb);
832 if (!err)
833 return 0;
834
835 fail:
836 apfs_transaction_abort(sb);
837 return err;
838 }
839
840 static const struct xattr_handler apfs_xattr_osx_handler = {
841 .prefix = XATTR_MAC_OSX_PREFIX,
842 .get = apfs_xattr_osx_get,
843 .set = apfs_xattr_osx_set,
844 };
845
846 /* On-disk xattrs have no namespace; use a fake 'osx' prefix in the kernel */
847 const struct xattr_handler *apfs_xattr_handlers[] = {
848 &apfs_xattr_osx_handler,
849 NULL
850 };
851
852 ssize_t apfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
853 {
854 struct inode *inode = d_inode(dentry);
855 struct super_block *sb = inode->i_sb;
856 struct apfs_sb_info *sbi = APFS_SB(sb);
857 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
858 struct apfs_query *query;
859 u64 cnid = apfs_ino(inode);
860 size_t free = size;
861 ssize_t ret;
862
863 down_read(&nxi->nx_big_sem);
864
865 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
866 if (!query) {
867 ret = -ENOMEM;
868 goto fail;
869 }
870
871 /* We want all the xattrs for the cnid, regardless of the name */
872 apfs_init_xattr_key(cnid, NULL /* name */, &query->key);
873 query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
874
875 while (1) {
876 struct apfs_xattr xattr;
877
878 ret = apfs_btree_query(sb, &query);
879 if (ret == -ENODATA) { /* Got all the xattrs */
880 ret = size - free;
881 break;
882 }
883 if (ret) {
884 apfs_err(sb, "query failed for id 0x%llx", cnid);
885 break;
886 }
887
888 ret = apfs_xattr_from_query(query, &xattr);
889 if (ret) {
890 apfs_err(sb, "bad xattr key in inode %llx", cnid);
891 break;
892 }
893
894 if (buffer) {
895 /* Prepend the fake 'osx' prefix before listing */
896 if (xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1 >
897 free) {
898 ret = -ERANGE;
899 break;
900 }
901 memcpy(buffer, XATTR_MAC_OSX_PREFIX,
902 XATTR_MAC_OSX_PREFIX_LEN);
903 buffer += XATTR_MAC_OSX_PREFIX_LEN;
904 memcpy(buffer, xattr.name, xattr.name_len + 1);
905 buffer += xattr.name_len + 1;
906 }
907 free -= xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1;
908 }
909
910 fail:
911 apfs_free_query(query);
912 up_read(&nxi->nx_big_sem);
913 return ret;
914 }