Debian Codegraph @

inode.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/slab.h>
7 #include <linux/buffer_head.h>
8 #include <linux/mount.h>
9 #include <linux/mpage.h>
10 #include <linux/blk_types.h>
11 #include "apfs.h"
12
13 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
14 #include <linux/sched/mm.h>
15 #endif
16
17 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 13, 0)
18 #include <linux/fileattr.h>
19 #endif
20
21 #define MAX_PFK_LEN 512
22
23 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0)
24 static struct page *grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index)
25 {
26 return pagecache_get_page(mapping, index, FGP_WRITEBEGIN, mapping_gfp_mask(mapping));
27 }
28 #endif
29
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
31
32 static int apfs_read_folio(struct file *file, struct folio *folio)
33 {
34 return mpage_read_folio(folio, apfs_get_block);
35 }
36
37 #else
38
39 static int apfs_readpage(struct file *file, struct page *page)
40 {
41 return mpage_readpage(page, apfs_get_block);
42 }
43
44 #endif
45
46 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) /* Misses mpage_readpages() */
47
48 static void apfs_readahead(struct readahead_control *rac)
49 {
50 mpage_readahead(rac, apfs_get_block);
51 }
52
53 #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) */
54
55 static int apfs_readpages(struct file *file, struct address_space *mapping,
56 struct list_head *pages, unsigned int nr_pages)
57 {
58 return mpage_readpages(mapping, pages, nr_pages, apfs_get_block);
59 }
60
61 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) */
62
63 /**
64 * apfs_create_dstream_rec - Create a data stream record
65 * @dstream: data stream info
66 *
67 * Does nothing if the record already exists. TODO: support cloned files.
68 * Returns 0 on success or a negative error code in case of failure.
69 */
70 static int apfs_create_dstream_rec(struct apfs_dstream_info *dstream)
71 {
72 struct super_block *sb = dstream->ds_sb;
73 struct apfs_sb_info *sbi = APFS_SB(sb);
74 struct apfs_query *query;
75 struct apfs_dstream_id_key raw_key;
76 struct apfs_dstream_id_val raw_val;
77 int ret;
78
79 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
80 if (!query)
81 return -ENOMEM;
82 apfs_init_dstream_id_key(dstream->ds_id, &query->key);
83 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
84
85 ret = apfs_btree_query(sb, &query);
86 if (ret != -ENODATA) /* Either an error, or the record already exists */
87 goto out;
88
89 apfs_key_set_hdr(APFS_TYPE_DSTREAM_ID, dstream->ds_id, &raw_key);
90 raw_val.refcnt = cpu_to_le32(1);
91 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
92 if (ret) {
93 apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
94 goto out;
95 }
96 out:
97 apfs_free_query(query);
98 return ret;
99 }
100
101 static int apfs_check_dstream_refcnt(struct inode *inode);
102 static int apfs_put_dstream_rec(struct apfs_dstream_info *dstream);
103
104 /**
105 * apfs_inode_create_exclusive_dstream - Make an inode's dstream not shared
106 * @inode: the vfs inode
107 *
108 * Returns 0 on success, or a negative error code in case of failure.
109 */
110 int apfs_inode_create_exclusive_dstream(struct inode *inode)
111 {
112 struct super_block *sb = inode->i_sb;
113 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
114 struct apfs_inode_info *ai = APFS_I(inode);
115 struct apfs_dstream_info *dstream = &ai->i_dstream;
116 u64 new_id;
117 int err;
118
119 if (!ai->i_has_dstream || !dstream->ds_shared)
120 return 0;
121
122 /*
123 * The ds_shared field is not updated when the other user of the
124 * dstream puts it, so it could be a false positive. Check it again
125 * before actually putting the dstream. The double query is wasteful,
126 * but I don't know if it makes sense to optimize this (TODO).
127 */
128 err = apfs_check_dstream_refcnt(inode);
129 if (err) {
130 apfs_err(sb, "failed to check refcnt for ino 0x%llx", apfs_ino(inode));
131 return err;
132 }
133 if (!dstream->ds_shared)
134 return 0;
135 err = apfs_put_dstream_rec(dstream);
136 if (err) {
137 apfs_err(sb, "failed to put dstream for ino 0x%llx", apfs_ino(inode));
138 return err;
139 }
140
141 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
142 new_id = le64_to_cpu(vsb_raw->apfs_next_obj_id);
143 le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
144
145 err = apfs_clone_extents(dstream, new_id);
146 if (err) {
147 apfs_err(sb, "failed clone extents for ino 0x%llx", apfs_ino(inode));
148 return err;
149 }
150
151 dstream->ds_id = new_id;
152 err = apfs_create_dstream_rec(dstream);
153 if (err) {
154 apfs_err(sb, "failed to create dstream for ino 0x%llx", apfs_ino(inode));
155 return err;
156 }
157
158 dstream->ds_shared = false;
159 return 0;
160 }
161
162 /**
163 * apfs_inode_create_dstream_rec - Create the data stream record for an inode
164 * @inode: the vfs inode
165 *
166 * Does nothing if the record already exists. TODO: support cloned files.
167 * Returns 0 on success or a negative error code in case of failure.
168 */
169 static int apfs_inode_create_dstream_rec(struct inode *inode)
170 {
171 struct apfs_inode_info *ai = APFS_I(inode);
172 int err;
173
174 if (ai->i_has_dstream)
175 return apfs_inode_create_exclusive_dstream(inode);
176
177 err = apfs_create_dstream_rec(&ai->i_dstream);
178 if (err)
179 return err;
180
181 ai->i_has_dstream = true;
182 return 0;
183 }
184
185 /**
186 * apfs_dstream_adj_refcnt - Adjust dstream record refcount
187 * @dstream: data stream info
188 * @delta: desired change in reference count
189 *
190 * Deletes the record if the reference count goes to zero. Returns 0 on success
191 * or a negative error code in case of failure.
192 */
193 int apfs_dstream_adj_refcnt(struct apfs_dstream_info *dstream, u32 delta)
194 {
195 struct super_block *sb = dstream->ds_sb;
196 struct apfs_sb_info *sbi = APFS_SB(sb);
197 struct apfs_query *query;
198 struct apfs_dstream_id_val raw_val;
199 void *raw = NULL;
200 u32 refcnt;
201 int ret;
202
203 ASSERT(APFS_I(dstream->ds_inode)->i_has_dstream);
204
205 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
206 if (!query)
207 return -ENOMEM;
208 apfs_init_dstream_id_key(dstream->ds_id, &query->key);
209 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
210
211 ret = apfs_btree_query(sb, &query);
212 if (ret) {
213 apfs_err(sb, "query failed for id 0x%llx", dstream->ds_id);
214 if (ret == -ENODATA)
215 ret = -EFSCORRUPTED;
216 goto out;
217 }
218
219 if (query->len != sizeof(raw_val)) {
220 apfs_err(sb, "bad value length (%d)", query->len);
221 ret = -EFSCORRUPTED;
222 goto out;
223 }
224 raw = query->node->object.data;
225 raw_val = *(struct apfs_dstream_id_val *)(raw + query->off);
226 refcnt = le32_to_cpu(raw_val.refcnt);
227
228 refcnt += delta;
229 if (refcnt == 0) {
230 ret = apfs_btree_remove(query);
231 if (ret)
232 apfs_err(sb, "removal failed for id 0x%llx", dstream->ds_id);
233 goto out;
234 }
235
236 raw_val.refcnt = cpu_to_le32(refcnt);
237 ret = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, &raw_val, sizeof(raw_val));
238 if (ret)
239 apfs_err(sb, "update failed for id 0x%llx", dstream->ds_id);
240 out:
241 apfs_free_query(query);
242 return ret;
243 }
244
245 /**
246 * apfs_put_dstream_rec - Put a reference for a data stream record
247 * @dstream: data stream info
248 *
249 * Deletes the record if the reference count goes to zero. Returns 0 on success
250 * or a negative error code in case of failure.
251 */
252 static int apfs_put_dstream_rec(struct apfs_dstream_info *dstream)
253 {
254 struct apfs_inode_info *ai = APFS_I(dstream->ds_inode);
255
256 if (!ai->i_has_dstream)
257 return 0;
258 return apfs_dstream_adj_refcnt(dstream, -1);
259 }
260
261 /**
262 * apfs_create_crypto_rec - Create the crypto state record for an inode
263 * @inode: the vfs inode
264 *
265 * Does nothing if the record already exists. TODO: support cloned files.
266 * Returns 0 on success or a negative error code in case of failure.
267 */
268 static int apfs_create_crypto_rec(struct inode *inode)
269 {
270 struct super_block *sb = inode->i_sb;
271 struct apfs_sb_info *sbi = APFS_SB(sb);
272 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
273 struct apfs_query *query;
274 struct apfs_crypto_state_key raw_key;
275 int ret;
276
277 if (inode->i_size || inode->i_blocks) /* Already has a dstream */
278 return 0;
279
280 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
281 if (!query)
282 return -ENOMEM;
283 apfs_init_crypto_state_key(dstream->ds_id, &query->key);
284 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
285
286 ret = apfs_btree_query(sb, &query);
287 if (ret != -ENODATA) /* Either an error, or the record already exists */
288 goto out;
289
290 apfs_key_set_hdr(APFS_TYPE_CRYPTO_STATE, dstream->ds_id, &raw_key);
291 if (sbi->s_dflt_pfk) {
292 struct apfs_crypto_state_val *raw_val = sbi->s_dflt_pfk;
293 unsigned int key_len = le16_to_cpu(raw_val->state.key_len);
294
295 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, sizeof(*raw_val) + key_len);
296 if (ret)
297 apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
298 } else {
299 struct apfs_crypto_state_val raw_val;
300
301 raw_val.refcnt = cpu_to_le32(1);
302 raw_val.state.major_version = cpu_to_le16(APFS_WMCS_MAJOR_VERSION);
303 raw_val.state.minor_version = cpu_to_le16(APFS_WMCS_MINOR_VERSION);
304 raw_val.state.cpflags = 0;
305 raw_val.state.persistent_class = cpu_to_le32(APFS_PROTECTION_CLASS_F);
306 raw_val.state.key_os_version = 0;
307 raw_val.state.key_revision = cpu_to_le16(1);
308 raw_val.state.key_len = cpu_to_le16(0);
309 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
310 if (ret)
311 apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
312 }
313 out:
314 apfs_free_query(query);
315 return ret;
316 }
317
318 /**
319 * apfs_dflt_key_class - Returns default key class for files in volume
320 * @sb: volume superblock
321 */
322 static unsigned int apfs_dflt_key_class(struct super_block *sb)
323 {
324 struct apfs_sb_info *sbi = APFS_SB(sb);
325
326 if (!sbi->s_dflt_pfk)
327 return APFS_PROTECTION_CLASS_F;
328
329 return le32_to_cpu(sbi->s_dflt_pfk->state.persistent_class);
330 }
331
332 /**
333 * apfs_create_crypto_rec - Adjust crypto state record refcount
334 * @sb: volume superblock
335 * @crypto_id: crypto_id to adjust
336 * @delta: desired change in reference count
337 *
338 * This function is used when adding or removing extents, as each extent holds
339 * a reference to the crypto ID. It should also be used when removing inodes,
340 * and in that case it should also remove the crypto record (TODO).
341 */
342 int apfs_crypto_adj_refcnt(struct super_block *sb, u64 crypto_id, int delta)
343 {
344 struct apfs_sb_info *sbi = APFS_SB(sb);
345 struct apfs_query *query;
346 struct apfs_crypto_state_val *raw_val;
347 char *raw;
348 int ret;
349
350 if (!crypto_id)
351 return 0;
352
353 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
354 if (!query)
355 return -ENOMEM;
356 apfs_init_crypto_state_key(crypto_id, &query->key);
357 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
358
359 ret = apfs_btree_query(sb, &query);
360 if (ret) {
361 apfs_err(sb, "query failed for id 0x%llx", crypto_id);
362 goto out;
363 }
364
365 ret = apfs_query_join_transaction(query);
366 if (ret) {
367 apfs_err(sb, "query join failed");
368 return ret;
369 }
370 raw = query->node->object.data;
371 raw_val = (void *)raw + query->off;
372
373 le32_add_cpu(&raw_val->refcnt, delta);
374
375 out:
376 apfs_free_query(query);
377 return ret;
378 }
379
380 /**
381 * apfs_crypto_set_key - Modify content of crypto state record
382 * @sb: volume superblock
383 * @crypto_id: crypto_id to modify
384 * @new_val: new crypto state data; new_val->refcnt is overridden
385 *
386 * This function does not alter the inode's default protection class field.
387 * It needs to be done separately if the class changes.
388 */
389 static int apfs_crypto_set_key(struct super_block *sb, u64 crypto_id, struct apfs_crypto_state_val *new_val)
390 {
391 struct apfs_sb_info *sbi = APFS_SB(sb);
392 struct apfs_query *query;
393 struct apfs_crypto_state_val *raw_val;
394 char *raw;
395 int ret;
396 unsigned int pfk_len;
397
398 if (!crypto_id)
399 return 0;
400
401 pfk_len = le16_to_cpu(new_val->state.key_len);
402
403 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
404 if (!query)
405 return -ENOMEM;
406 apfs_init_crypto_state_key(crypto_id, &query->key);
407 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
408
409 ret = apfs_btree_query(sb, &query);
410 if (ret) {
411 apfs_err(sb, "query failed for id 0x%llx", crypto_id);
412 goto out;
413 }
414 raw = query->node->object.data;
415 raw_val = (void *)raw + query->off;
416
417 new_val->refcnt = raw_val->refcnt;
418
419 ret = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, new_val, sizeof(*new_val) + pfk_len);
420 if (ret)
421 apfs_err(sb, "update failed for id 0x%llx", crypto_id);
422
423 out:
424 apfs_free_query(query);
425 return ret;
426 }
427
428 /**
429 * apfs_crypto_get_key - Retrieve content of crypto state record
430 * @sb: volume superblock
431 * @crypto_id: crypto_id to modify
432 * @val: result crypto state data
433 * @max_len: maximum allowed value of val->state.key_len
434 */
435 static int apfs_crypto_get_key(struct super_block *sb, u64 crypto_id, struct apfs_crypto_state_val *val,
436 unsigned int max_len)
437 {
438 struct apfs_sb_info *sbi = APFS_SB(sb);
439 struct apfs_query *query;
440 struct apfs_crypto_state_val *raw_val;
441 char *raw;
442 int ret;
443 unsigned int pfk_len;
444
445 if (!crypto_id)
446 return -ENOENT;
447
448 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
449 if (!query)
450 return -ENOMEM;
451 apfs_init_crypto_state_key(crypto_id, &query->key);
452 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
453
454 ret = apfs_btree_query(sb, &query);
455 if (ret)
456 goto out;
457 raw = query->node->object.data;
458 raw_val = (void *)raw + query->off;
459
460 pfk_len = le16_to_cpu(raw_val->state.key_len);
461 if (pfk_len > max_len) {
462 ret = -ENOSPC;
463 goto out;
464 }
465
466 memcpy(val, raw_val, sizeof(*val) + pfk_len);
467
468 out:
469 apfs_free_query(query);
470 return ret;
471 }
472
473 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
474 int __apfs_write_begin(const struct kiocb *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int flags, struct page **pagep, void **fsdata)
475 #else
476 int __apfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int flags, struct page **pagep, void **fsdata)
477 #endif
478 {
479 struct inode *inode = mapping->host;
480 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
481 struct super_block *sb = inode->i_sb;
482 struct page *page;
483 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
484 struct folio *folio;
485 #endif
486 struct buffer_head *bh, *head;
487 unsigned int blocksize, block_start, block_end, from, to;
488 pgoff_t index = pos >> PAGE_SHIFT;
489 sector_t iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
490 loff_t i_blks_end;
491 int err;
492
493 apfs_inode_join_transaction(sb, inode);
494
495 err = apfs_inode_create_dstream_rec(inode);
496 if (err) {
497 apfs_err(sb, "failed to create dstream for ino 0x%llx", apfs_ino(inode));
498 return err;
499 }
500
501 if (apfs_vol_is_encrypted(sb)) {
502 err = apfs_create_crypto_rec(inode);
503 if (err) {
504 apfs_err(sb, "crypto creation failed for ino 0x%llx", apfs_ino(inode));
505 return err;
506 }
507 }
508
509 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
510 flags = memalloc_nofs_save();
511 page = grab_cache_page_write_begin(mapping, index);
512 memalloc_nofs_restore(flags);
513 #else
514 page = grab_cache_page_write_begin(mapping, index, flags | AOP_FLAG_NOFS);
515 #endif
516 if (!page)
517 return -ENOMEM;
518 if (!page_has_buffers(page)) {
519 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
520 create_empty_buffers(page, sb->s_blocksize, 0);
521 #else
522 folio = page_folio(page);
523 bh = folio_buffers(folio);
524 if (!bh)
525 bh = create_empty_buffers(folio, sb->s_blocksize, 0);
526 #endif
527 }
528
529 /* CoW moves existing blocks, so read them but mark them as unmapped */
530 head = page_buffers(page);
531 blocksize = head->b_size;
532 i_blks_end = (inode->i_size + sb->s_blocksize - 1) >> inode->i_blkbits;
533 i_blks_end <<= inode->i_blkbits;
534 if (i_blks_end >= pos) {
535 from = pos & (PAGE_SIZE - 1);
536 to = from + min(i_blks_end - pos, (loff_t)len);
537 } else {
538 /* TODO: deal with preallocated tail blocks */
539 from = UINT_MAX;
540 to = 0;
541 }
542 for (bh = head, block_start = 0; bh != head || !block_start;
543 block_start = block_end, bh = bh->b_this_page, ++iblock) {
544 block_end = block_start + blocksize;
545 if (to > block_start && from < block_end) {
546 if (buffer_trans(bh))
547 continue;
548 if (!buffer_mapped(bh)) {
549 err = __apfs_get_block(dstream, iblock, bh,
550 false /* create */);
551 if (err) {
552 apfs_err(sb, "failed to map block for ino 0x%llx", apfs_ino(inode));
553 goto out_put_page;
554 }
555 }
556 if (buffer_mapped(bh) && !buffer_uptodate(bh)) {
557 get_bh(bh);
558 lock_buffer(bh);
559 apfs_submit_bh(REQ_OP_READ, 0, bh);
560 wait_on_buffer(bh);
561 if (!buffer_uptodate(bh)) {
562 apfs_err(sb, "failed to read block for ino 0x%llx", apfs_ino(inode));
563 err = -EIO;
564 goto out_put_page;
565 }
566 }
567 clear_buffer_mapped(bh);
568 }
569 }
570
571 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
572 err = __block_write_begin(page_folio(page), pos, len, apfs_get_new_block);
573 #else
574 err = __block_write_begin(page, pos, len, apfs_get_new_block);
575 #endif
576 if (err) {
577 apfs_err(sb, "CoW failed in inode 0x%llx", apfs_ino(inode));
578 goto out_put_page;
579 }
580
581 *pagep = page;
582 return 0;
583
584 out_put_page:
585 unlock_page(page);
586 put_page(page);
587 return err;
588 }
589
590 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
591 static int apfs_write_begin(const struct kiocb *file, struct address_space *mapping,
592 loff_t pos, unsigned int len,
593 struct folio **foliop, void **fsdata)
594 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
595 static int apfs_write_begin(struct file *file, struct address_space *mapping,
596 loff_t pos, unsigned int len,
597 struct folio **foliop, void **fsdata)
598 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
599 static int apfs_write_begin(struct file *file, struct address_space *mapping,
600 loff_t pos, unsigned int len,
601 struct page **pagep, void **fsdata)
602 #else
603 static int apfs_write_begin(struct file *file, struct address_space *mapping,
604 loff_t pos, unsigned int len, unsigned int flags,
605 struct page **pagep, void **fsdata)
606 #endif
607 {
608 struct inode *inode = mapping->host;
609 struct super_block *sb = inode->i_sb;
610 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
611 struct page *page = NULL;
612 struct page **pagep = &page;
613 #endif
614 int err;
615 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
616 unsigned int flags = 0;
617 #endif
618
619 if (unlikely(pos >= APFS_MAX_FILE_SIZE))
620 return -EFBIG;
621
622 err = apfs_transaction_start(sb, APFS_TRANS_REG);
623 if (err)
624 return err;
625
626 err = __apfs_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
627 if (err)
628 goto fail;
629 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
630 *foliop = page_folio(page);
631 #endif
632 return 0;
633
634 fail:
635 apfs_transaction_abort(sb);
636 return err;
637 }
638
639 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
640 int __apfs_write_end(const struct kiocb *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int copied, struct page *page, void *fsdata)
641 #else
642 int __apfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int copied, struct page *page, void *fsdata)
643 #endif
644 {
645 struct inode *inode = mapping->host;
646 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
647 int ret, err;
648
649 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
650 ret = generic_write_end(file, mapping, pos, len, copied, page_folio(page), fsdata);
651 #else
652 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
653 #endif
654 dstream->ds_size = i_size_read(inode);
655 if (ret < len && pos + len > inode->i_size) {
656 truncate_pagecache(inode, inode->i_size);
657 err = apfs_truncate(dstream, inode->i_size);
658 if (err) {
659 apfs_err(inode->i_sb, "truncation failed for ino 0x%llx", apfs_ino(inode));
660 return err;
661 }
662 }
663 return ret;
664 }
665
666 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
667 static int apfs_write_end(const struct kiocb *file, struct address_space *mapping,
668 loff_t pos, unsigned int len, unsigned int copied,
669 struct folio *folio, void *fsdata)
670 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
671 static int apfs_write_end(struct file *file, struct address_space *mapping,
672 loff_t pos, unsigned int len, unsigned int copied,
673 struct folio *folio, void *fsdata)
674 #else
675 static int apfs_write_end(struct file *file, struct address_space *mapping,
676 loff_t pos, unsigned int len, unsigned int copied,
677 struct page *page, void *fsdata)
678 #endif
679 {
680 struct inode *inode = mapping->host;
681 struct super_block *sb = inode->i_sb;
682 struct apfs_nx_transaction *trans = &APFS_NXI(sb)->nx_transaction;
683 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
684 struct page *page = &folio->page;
685 #endif
686 int ret, err;
687
688 ret = __apfs_write_end(file, mapping, pos, len, copied, page, fsdata);
689 if (ret < 0) {
690 err = ret;
691 goto fail;
692 }
693
694 if ((pos + ret) & (sb->s_blocksize - 1))
695 trans->t_state |= APFS_NX_TRANS_INCOMPLETE_BLOCK;
696 else
697 trans->t_state &= ~APFS_NX_TRANS_INCOMPLETE_BLOCK;
698
699 err = apfs_transaction_commit(sb);
700 if (!err)
701 return ret;
702
703 fail:
704 apfs_transaction_abort(sb);
705 return err;
706 }
707
708 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0) || RHEL_VERSION_GE(9, 3)
709 static void apfs_noop_invalidate_folio(struct folio *folio, size_t offset, size_t length)
710 #else
711 static void apfs_noop_invalidatepage(struct page *page, unsigned int offset, unsigned int length)
712 #endif
713 {
714 }
715
716 /* bmap is not implemented to avoid issues with CoW on swapfiles */
717 static const struct address_space_operations apfs_aops = {
718 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0) || RHEL_VERSION_GE(9, 2)
719 .dirty_folio = block_dirty_folio,
720 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
721 .set_page_dirty = __set_page_dirty_buffers,
722 #endif
723
724 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0) || RHEL_VERSION_GE(9, 3)
725 .read_folio = apfs_read_folio,
726 #else
727 .readpage = apfs_readpage,
728 #endif
729
730 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
731 .readahead = apfs_readahead,
732 #else
733 .readpages = apfs_readpages,
734 #endif
735
736 .write_begin = apfs_write_begin,
737 .write_end = apfs_write_end,
738
739 /* The intention is to keep bhs around until the transaction is over */
740 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0) || RHEL_VERSION_GE(9, 3)
741 .invalidate_folio = apfs_noop_invalidate_folio,
742 #else
743 .invalidatepage = apfs_noop_invalidatepage,
744 #endif
745 };
746
747 /**
748 * apfs_inode_set_ops - Set up an inode's operations
749 * @inode: vfs inode to set up
750 * @rdev: device id (0 if not a device file)
751 * @compressed: is this a compressed inode?
752 *
753 * For device files, also sets the device id to @rdev.
754 */
755 static void apfs_inode_set_ops(struct inode *inode, dev_t rdev, bool compressed)
756 {
757 /* A lot of operations still missing, of course */
758 switch (inode->i_mode & S_IFMT) {
759 case S_IFREG:
760 inode->i_op = &apfs_file_inode_operations;
761 if (compressed) {
762 inode->i_fop = &apfs_compress_file_operations;
763 inode->i_mapping->a_ops = &apfs_compress_aops;
764 } else {
765 inode->i_fop = &apfs_file_operations;
766 inode->i_mapping->a_ops = &apfs_aops;
767 }
768 break;
769 case S_IFDIR:
770 inode->i_op = &apfs_dir_inode_operations;
771 inode->i_fop = &apfs_dir_operations;
772 break;
773 case S_IFLNK:
774 inode->i_op = &apfs_symlink_inode_operations;
775 break;
776 default:
777 inode->i_op = &apfs_special_inode_operations;
778 init_special_inode(inode, inode->i_mode, rdev);
779 break;
780 }
781 }
782
783 /**
784 * apfs_inode_from_query - Read the inode found by a successful query
785 * @query: the query that found the record
786 * @inode: vfs inode to be filled with the read data
787 *
788 * Reads the inode record into @inode and performs some basic sanity checks,
789 * mostly as a protection against crafted filesystems. Returns 0 on success
790 * or a negative error code otherwise.
791 */
792 static int apfs_inode_from_query(struct apfs_query *query, struct inode *inode)
793 {
794 struct apfs_inode_info *ai = APFS_I(inode);
795 struct apfs_dstream_info *dstream = &ai->i_dstream;
796 struct apfs_inode_val *inode_val;
797 char *raw = query->node->object.data;
798 char *xval = NULL;
799 int xlen;
800 u32 rdev = 0, bsd_flags;
801 bool compressed = false;
802
803 if (query->len < sizeof(*inode_val))
804 goto corrupted;
805
806 inode_val = (struct apfs_inode_val *)(raw + query->off);
807
808 ai->i_parent_id = le64_to_cpu(inode_val->parent_id);
809 dstream->ds_id = le64_to_cpu(inode_val->private_id);
810 inode->i_mode = le16_to_cpu(inode_val->mode);
811 ai->i_key_class = le32_to_cpu(inode_val->default_protection_class);
812 ai->i_int_flags = le64_to_cpu(inode_val->internal_flags);
813
814 ai->i_saved_uid = le32_to_cpu(inode_val->owner);
815 i_uid_write(inode, ai->i_saved_uid);
816 ai->i_saved_gid = le32_to_cpu(inode_val->group);
817 i_gid_write(inode, ai->i_saved_gid);
818
819 ai->i_bsd_flags = bsd_flags = le32_to_cpu(inode_val->bsd_flags);
820 if (bsd_flags & APFS_INOBSD_IMMUTABLE)
821 inode->i_flags |= S_IMMUTABLE;
822 if (bsd_flags & APFS_INOBSD_APPEND)
823 inode->i_flags |= S_APPEND;
824
825 if (!S_ISDIR(inode->i_mode)) {
826 /*
827 * Directory inodes don't store their link count, so to provide
828 * it we would have to actually count the subdirectories. The
829 * HFS/HFS+ modules just leave it at 1, and so do we, for now.
830 */
831 set_nlink(inode, le32_to_cpu(inode_val->nlink));
832 } else {
833 ai->i_nchildren = le32_to_cpu(inode_val->nchildren);
834 }
835
836 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
837 inode->i_ctime = ns_to_timespec64(le64_to_cpu(inode_val->change_time));
838 #else
839 inode_set_ctime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->change_time)));
840 #endif
841
842 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
843 inode->i_atime = ns_to_timespec64(le64_to_cpu(inode_val->access_time));
844 inode->i_mtime = ns_to_timespec64(le64_to_cpu(inode_val->mod_time));
845 #else
846 inode_set_atime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->access_time)));
847 inode_set_mtime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->mod_time)));
848 #endif
849 ai->i_crtime = ns_to_timespec64(le64_to_cpu(inode_val->create_time));
850
851 dstream->ds_size = inode->i_size = inode->i_blocks = 0;
852 ai->i_has_dstream = false;
853 if ((bsd_flags & APFS_INOBSD_COMPRESSED) && !S_ISDIR(inode->i_mode)) {
854 if (!apfs_compress_get_size(inode, &inode->i_size)) {
855 inode->i_blocks = (inode->i_size + 511) >> 9;
856 compressed = true;
857 }
858 } else {
859 xlen = apfs_find_xfield(inode_val->xfields,
860 query->len - sizeof(*inode_val),
861 APFS_INO_EXT_TYPE_DSTREAM, &xval);
862 if (xlen >= sizeof(struct apfs_dstream)) {
863 struct apfs_dstream *dstream_raw = (struct apfs_dstream *)xval;
864
865 dstream->ds_size = inode->i_size = le64_to_cpu(dstream_raw->size);
866 inode->i_blocks = le64_to_cpu(dstream_raw->alloced_size) >> 9;
867 ai->i_has_dstream = true;
868 }
869 }
870 xval = NULL;
871
872 /* TODO: move each xfield read to its own function */
873 dstream->ds_sparse_bytes = 0;
874 xlen = apfs_find_xfield(inode_val->xfields, query->len - sizeof(*inode_val), APFS_INO_EXT_TYPE_SPARSE_BYTES, &xval);
875 if (xlen >= sizeof(__le64)) {
876 __le64 *sparse_bytes_p = (__le64 *)xval;
877
878 dstream->ds_sparse_bytes = le64_to_cpup(sparse_bytes_p);
879 }
880 xval = NULL;
881
882 rdev = 0;
883 xlen = apfs_find_xfield(inode_val->xfields,
884 query->len - sizeof(*inode_val),
885 APFS_INO_EXT_TYPE_RDEV, &xval);
886 if (xlen >= sizeof(__le32)) {
887 __le32 *rdev_p = (__le32 *)xval;
888
889 rdev = le32_to_cpup(rdev_p);
890 }
891
892 apfs_inode_set_ops(inode, rdev, compressed);
893 return 0;
894
895 corrupted:
896 apfs_err(inode->i_sb, "bad inode record for inode 0x%llx", apfs_ino(inode));
897 return -EFSCORRUPTED;
898 }
899
900 /**
901 * apfs_inode_lookup - Lookup an inode record in the catalog b-tree
902 * @inode: vfs inode to lookup
903 *
904 * Runs a catalog query for the apfs_ino(@inode) inode record; returns a pointer
905 * to the query structure on success, or an error pointer in case of failure.
906 */
907 static struct apfs_query *apfs_inode_lookup(const struct inode *inode)
908 {
909 struct super_block *sb = inode->i_sb;
910 struct apfs_sb_info *sbi = APFS_SB(sb);
911 struct apfs_query *query;
912 int ret;
913
914 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
915 if (!query)
916 return ERR_PTR(-ENOMEM);
917 apfs_init_inode_key(apfs_ino(inode), &query->key);
918 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
919
920 ret = apfs_btree_query(sb, &query);
921 if (!ret)
922 return query;
923
924 /* Don't complain if an orphan is already gone */
925 if (!current_work() || ret != -ENODATA)
926 apfs_err(sb, "query failed for id 0x%llx", apfs_ino(inode));
927 apfs_free_query(query);
928 return ERR_PTR(ret);
929 }
930
931 /**
932 * apfs_test_inode - Check if the inode matches a 64-bit inode number
933 * @inode: inode to test
934 * @cnid: pointer to the inode number
935 */
936 static int apfs_test_inode(struct inode *inode, void *cnid)
937 {
938 u64 *ino = cnid;
939
940 return apfs_ino(inode) == *ino;
941 }
942
943 /**
944 * apfs_set_inode - Set a 64-bit inode number on the given inode
945 * @inode: inode to set
946 * @cnid: pointer to the inode number
947 */
948 static int apfs_set_inode(struct inode *inode, void *cnid)
949 {
950 apfs_set_ino(inode, *(u64 *)cnid);
951 return 0;
952 }
953
954 /**
955 * apfs_iget_locked - Wrapper for iget5_locked()
956 * @sb: filesystem superblock
957 * @cnid: 64-bit inode number
958 *
959 * Works the same as iget_locked(), but can handle 64-bit inode numbers on
960 * 32-bit architectures.
961 */
962 static struct inode *apfs_iget_locked(struct super_block *sb, u64 cnid)
963 {
964 return iget5_locked(sb, cnid, apfs_test_inode, apfs_set_inode, &cnid);
965 }
966
967 /**
968 * apfs_check_dstream_refcnt - Check if an inode's dstream is shared
969 * @inode: the inode to check
970 *
971 * Sets the value of ds_shared for the inode's dstream. Returns 0 on success,
972 * or a negative error code in case of failure.
973 */
974 static int apfs_check_dstream_refcnt(struct inode *inode)
975 {
976 struct apfs_inode_info *ai = APFS_I(inode);
977 struct apfs_dstream_info *dstream = &ai->i_dstream;
978 struct super_block *sb = inode->i_sb;
979 struct apfs_sb_info *sbi = APFS_SB(sb);
980 struct apfs_query *query = NULL;
981 struct apfs_dstream_id_val raw_val;
982 void *raw = NULL;
983 u32 refcnt;
984 int ret;
985
986 if (!ai->i_has_dstream) {
987 dstream->ds_shared = false;
988 return 0;
989 }
990
991 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
992 if (!query)
993 return -ENOMEM;
994 apfs_init_dstream_id_key(dstream->ds_id, &query->key);
995 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
996
997 ret = apfs_btree_query(sb, &query);
998 if (ret) {
999 apfs_err(sb, "query failed for id 0x%llx", dstream->ds_id);
1000 if (ret == -ENODATA)
1001 ret = -EFSCORRUPTED;
1002 goto fail;
1003 }
1004
1005 if (query->len != sizeof(raw_val)) {
1006 ret = -EFSCORRUPTED;
1007 goto fail;
1008 }
1009 raw = query->node->object.data;
1010 raw_val = *(struct apfs_dstream_id_val *)(raw + query->off);
1011 refcnt = le32_to_cpu(raw_val.refcnt);
1012
1013 dstream->ds_shared = refcnt > 1;
1014 fail:
1015 apfs_free_query(query);
1016 return ret;
1017 }
1018
1019 /**
1020 * apfs_iget - Populate inode structures with metadata from disk
1021 * @sb: filesystem superblock
1022 * @cnid: inode number
1023 *
1024 * Populates the vfs inode and the corresponding apfs_inode_info structure.
1025 * Returns a pointer to the vfs inode in case of success, or an appropriate
1026 * error pointer otherwise.
1027 */
1028 struct inode *apfs_iget(struct super_block *sb, u64 cnid)
1029 {
1030 struct apfs_sb_info *sbi = APFS_SB(sb);
1031 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
1032 struct inode *inode;
1033 struct apfs_query *query;
1034 int err;
1035
1036 inode = apfs_iget_locked(sb, cnid);
1037 if (!inode)
1038 return ERR_PTR(-ENOMEM);
1039 if (!(apfs_inode_state_read_once(inode) & I_NEW))
1040 return inode;
1041
1042 down_read(&nxi->nx_big_sem);
1043 query = apfs_inode_lookup(inode);
1044 if (IS_ERR(query)) {
1045 err = PTR_ERR(query);
1046 /* Don't complain if an orphan is already gone */
1047 if (!current_work() || err != -ENODATA)
1048 apfs_err(sb, "lookup failed for ino 0x%llx", cnid);
1049 goto fail;
1050 }
1051 err = apfs_inode_from_query(query, inode);
1052 apfs_free_query(query);
1053 if (err)
1054 goto fail;
1055 err = apfs_check_dstream_refcnt(inode);
1056 if (err) {
1057 apfs_err(sb, "refcnt check failed for ino 0x%llx", cnid);
1058 goto fail;
1059 }
1060 up_read(&nxi->nx_big_sem);
1061
1062 /* Allow the user to override the ownership */
1063 if (uid_valid(sbi->s_uid))
1064 inode->i_uid = sbi->s_uid;
1065 if (gid_valid(sbi->s_gid))
1066 inode->i_gid = sbi->s_gid;
1067
1068 /* Inode flags are not important for now, leave them at 0 */
1069 unlock_new_inode(inode);
1070 return inode;
1071
1072 fail:
1073 up_read(&nxi->nx_big_sem);
1074 iget_failed(inode);
1075 return ERR_PTR(err);
1076 }
1077
1078 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) /* No statx yet... */
1079
1080 int apfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1081 struct kstat *stat)
1082 {
1083 struct inode *inode = d_inode(dentry);
1084
1085 generic_fillattr(inode, stat);
1086 stat->dev = APFS_SB(inode->i_sb)->s_anon_dev;
1087 stat->ino = apfs_ino(inode);
1088 return 0;
1089 }
1090
1091 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
1092
1093 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1094 int apfs_getattr(const struct path *path, struct kstat *stat,
1095 u32 request_mask, unsigned int query_flags)
1096 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1097 int apfs_getattr(struct user_namespace *mnt_userns,
1098 const struct path *path, struct kstat *stat, u32 request_mask,
1099 unsigned int query_flags)
1100 #else
1101 int apfs_getattr(struct mnt_idmap *idmap,
1102 const struct path *path, struct kstat *stat, u32 request_mask,
1103 unsigned int query_flags)
1104 #endif
1105 {
1106 struct inode *inode = d_inode(path->dentry);
1107 struct apfs_inode_info *ai = APFS_I(inode);
1108
1109 stat->result_mask |= STATX_BTIME;
1110 stat->btime = ai->i_crtime;
1111
1112 if (ai->i_bsd_flags & APFS_INOBSD_APPEND)
1113 stat->attributes |= STATX_ATTR_APPEND;
1114 if (ai->i_bsd_flags & APFS_INOBSD_IMMUTABLE)
1115 stat->attributes |= STATX_ATTR_IMMUTABLE;
1116 if (ai->i_bsd_flags & APFS_INOBSD_NODUMP)
1117 stat->attributes |= STATX_ATTR_NODUMP;
1118 if (ai->i_bsd_flags & APFS_INOBSD_COMPRESSED)
1119 stat->attributes |= STATX_ATTR_COMPRESSED;
1120
1121 stat->attributes_mask |= STATX_ATTR_APPEND;
1122 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
1123 stat->attributes_mask |= STATX_ATTR_NODUMP;
1124 stat->attributes_mask |= STATX_ATTR_COMPRESSED;
1125
1126 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1127 generic_fillattr(inode, stat);
1128 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1129 generic_fillattr(mnt_userns, inode, stat);
1130 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1131 generic_fillattr(idmap, inode, stat);
1132 #else
1133 generic_fillattr(idmap, request_mask, inode, stat);
1134 #endif
1135
1136 stat->dev = APFS_SB(inode->i_sb)->s_anon_dev;
1137 stat->ino = apfs_ino(inode);
1138 return 0;
1139 }
1140
1141 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
1142
1143 /**
1144 * apfs_build_inode_val - Allocate and initialize the value for an inode record
1145 * @inode: vfs inode to record
1146 * @qname: filename for primary link
1147 * @val_p: on return, a pointer to the new on-disk value structure
1148 *
1149 * Returns the length of the value, or a negative error code in case of failure.
1150 */
1151 static int apfs_build_inode_val(struct inode *inode, const struct qstr *qname,
1152 struct apfs_inode_val **val_p)
1153 {
1154 struct apfs_inode_val *val;
1155 struct apfs_x_field xkey;
1156 #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
1157 struct timespec64 ts;
1158 #endif
1159 int total_xlen, val_len;
1160 bool is_device = S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode);
1161 __le32 rdev;
1162
1163 /* The only required xfield is the name, and the id if it's a device */
1164 total_xlen = sizeof(struct apfs_xf_blob);
1165 total_xlen += sizeof(xkey) + round_up(qname->len + 1, 8);
1166 if (is_device)
1167 total_xlen += sizeof(xkey) + round_up(sizeof(rdev), 8);
1168
1169 val_len = sizeof(*val) + total_xlen;
1170 val = kzalloc(val_len, GFP_KERNEL);
1171 if (!val)
1172 return -ENOMEM;
1173
1174 val->parent_id = cpu_to_le64(APFS_I(inode)->i_parent_id);
1175 val->private_id = cpu_to_le64(apfs_ino(inode));
1176
1177 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1178 val->mod_time = cpu_to_le64(timespec64_to_ns(&inode->i_mtime));
1179 #else
1180 ts = inode_get_mtime(inode);
1181 val->mod_time = cpu_to_le64(timespec64_to_ns(&ts));
1182 #endif
1183 val->create_time = val->change_time = val->access_time = val->mod_time;
1184
1185 if (S_ISDIR(inode->i_mode))
1186 val->nchildren = 0;
1187 else
1188 val->nlink = cpu_to_le32(1);
1189
1190 val->owner = cpu_to_le32(i_uid_read(inode));
1191 val->group = cpu_to_le32(i_gid_read(inode));
1192 val->mode = cpu_to_le16(inode->i_mode);
1193
1194 /* The buffer was just allocated: none of these functions should fail */
1195 apfs_init_xfields(val->xfields, total_xlen);
1196 xkey.x_type = APFS_INO_EXT_TYPE_NAME;
1197 xkey.x_flags = APFS_XF_DO_NOT_COPY;
1198 xkey.x_size = cpu_to_le16(qname->len + 1);
1199 apfs_insert_xfield(val->xfields, total_xlen, &xkey, qname->name);
1200 if (is_device) {
1201 rdev = cpu_to_le32(inode->i_rdev);
1202 xkey.x_type = APFS_INO_EXT_TYPE_RDEV;
1203 xkey.x_flags = 0; /* TODO: proper flags here? */
1204 xkey.x_size = cpu_to_le16(sizeof(rdev));
1205 apfs_insert_xfield(val->xfields, total_xlen, &xkey, &rdev);
1206 }
1207
1208 *val_p = val;
1209 return val_len;
1210 }
1211
1212 /*
1213 * apfs_inode_rename - Update the primary name reported in an inode record
1214 * @inode: the in-memory inode
1215 * @new_name: name of the new primary link (NULL if unchanged)
1216 * @query: the query that found the inode record
1217 *
1218 * Returns 0 on success, or a negative error code in case of failure.
1219 */
1220 static int apfs_inode_rename(struct inode *inode, char *new_name,
1221 struct apfs_query *query)
1222 {
1223 char *raw = query->node->object.data;
1224 struct apfs_inode_val *new_val = NULL;
1225 int buflen, namelen;
1226 struct apfs_x_field xkey;
1227 int xlen;
1228 int err;
1229
1230 if (!new_name)
1231 return 0;
1232
1233 namelen = strlen(new_name) + 1; /* Count the null-termination */
1234 buflen = query->len;
1235 buflen += sizeof(struct apfs_x_field) + round_up(namelen, 8);
1236 new_val = kzalloc(buflen, GFP_KERNEL);
1237 if (!new_val)
1238 return -ENOMEM;
1239 memcpy(new_val, raw + query->off, query->len);
1240
1241 /* TODO: can we assume that all inode records have an xfield blob? */
1242 xkey.x_type = APFS_INO_EXT_TYPE_NAME;
1243 xkey.x_flags = APFS_XF_DO_NOT_COPY;
1244 xkey.x_size = cpu_to_le16(namelen);
1245 xlen = apfs_insert_xfield(new_val->xfields, buflen - sizeof(*new_val),
1246 &xkey, new_name);
1247 if (!xlen) {
1248 /* Buffer has enough space, but the metadata claims otherwise */
1249 apfs_err(inode->i_sb, "bad xfields on inode 0x%llx", apfs_ino(inode));
1250 err = -EFSCORRUPTED;
1251 goto fail;
1252 }
1253
1254 /* Just remove the old record and create a new one */
1255 err = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, new_val, sizeof(*new_val) + xlen);
1256 if (err)
1257 apfs_err(inode->i_sb, "update failed for ino 0x%llx", apfs_ino(inode));
1258
1259 fail:
1260 kfree(new_val);
1261 return err;
1262 }
1263
1264 /**
1265 * apfs_create_dstream_xfield - Create the inode xfield for a new data stream
1266 * @inode: the in-memory inode
1267 * @query: the query that found the inode record
1268 *
1269 * Returns 0 on success, or a negative error code in case of failure.
1270 */
1271 static int apfs_create_dstream_xfield(struct inode *inode,
1272 struct apfs_query *query)
1273 {
1274 char *raw = query->node->object.data;
1275 struct apfs_inode_val *new_val;
1276 struct apfs_dstream dstream_raw = {0};
1277 struct apfs_x_field xkey;
1278 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
1279 int xlen;
1280 int buflen;
1281 int err;
1282
1283 buflen = query->len;
1284 buflen += sizeof(struct apfs_x_field) + sizeof(dstream_raw);
1285 new_val = kzalloc(buflen, GFP_KERNEL);
1286 if (!new_val)
1287 return -ENOMEM;
1288 memcpy(new_val, raw + query->off, query->len);
1289
1290 dstream_raw.size = cpu_to_le64(inode->i_size);
1291 dstream_raw.alloced_size = cpu_to_le64(apfs_alloced_size(dstream));
1292 if (apfs_vol_is_encrypted(inode->i_sb))
1293 dstream_raw.default_crypto_id = cpu_to_le64(dstream->ds_id);
1294
1295 /* TODO: can we assume that all inode records have an xfield blob? */
1296 xkey.x_type = APFS_INO_EXT_TYPE_DSTREAM;
1297 xkey.x_flags = APFS_XF_SYSTEM_FIELD;
1298 xkey.x_size = cpu_to_le16(sizeof(dstream_raw));
1299 xlen = apfs_insert_xfield(new_val->xfields, buflen - sizeof(*new_val),
1300 &xkey, &dstream_raw);
1301 if (!xlen) {
1302 /* Buffer has enough space, but the metadata claims otherwise */
1303 apfs_err(inode->i_sb, "bad xfields on inode 0x%llx", apfs_ino(inode));
1304 err = -EFSCORRUPTED;
1305 goto fail;
1306 }
1307
1308 /* Just remove the old record and create a new one */
1309 err = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, new_val, sizeof(*new_val) + xlen);
1310 if (err)
1311 apfs_err(inode->i_sb, "update failed for ino 0x%llx", apfs_ino(inode));
1312
1313 fail:
1314 kfree(new_val);
1315 return err;
1316 }
1317
1318 /**
1319 * apfs_inode_resize - Update the sizes reported in an inode record
1320 * @inode: the in-memory inode
1321 * @query: the query that found the inode record
1322 *
1323 * Returns 0 on success, or a negative error code in case of failure.
1324 */
1325 static int apfs_inode_resize(struct inode *inode, struct apfs_query *query)
1326 {
1327 struct apfs_inode_info *ai = APFS_I(inode);
1328 char *raw;
1329 struct apfs_inode_val *inode_raw;
1330 char *xval;
1331 int xlen;
1332 int err;
1333
1334 /* All dstream records must have a matching xfield, even if empty */
1335 if (!ai->i_has_dstream)
1336 return 0;
1337
1338 err = apfs_query_join_transaction(query);
1339 if (err) {
1340 apfs_err(inode->i_sb, "query join failed");
1341 return err;
1342 }
1343 raw = query->node->object.data;
1344 inode_raw = (void *)raw + query->off;
1345
1346 xlen = apfs_find_xfield(inode_raw->xfields,
1347 query->len - sizeof(*inode_raw),
1348 APFS_INO_EXT_TYPE_DSTREAM, &xval);
1349
1350 if (xlen) {
1351 struct apfs_dstream *dstream;
1352
1353 if (xlen != sizeof(*dstream)) {
1354 apfs_err(inode->i_sb, "bad xlen (%d) on inode 0x%llx", xlen, apfs_ino(inode));
1355 return -EFSCORRUPTED;
1356 }
1357 dstream = (struct apfs_dstream *)xval;
1358
1359 /* TODO: count bytes read and written */
1360 dstream->size = cpu_to_le64(inode->i_size);
1361 dstream->alloced_size = cpu_to_le64(apfs_alloced_size(&ai->i_dstream));
1362 return 0;
1363 }
1364 /* This inode has no dstream xfield, so we need to create it */
1365 return apfs_create_dstream_xfield(inode, query);
1366 }
1367
1368 /**
1369 * apfs_create_sparse_xfield - Create an inode xfield to count sparse bytes
1370 * @inode: the in-memory inode
1371 * @query: the query that found the inode record
1372 *
1373 * Returns 0 on success, or a negative error code in case of failure.
1374 */
1375 static int apfs_create_sparse_xfield(struct inode *inode, struct apfs_query *query)
1376 {
1377 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
1378 char *raw = query->node->object.data;
1379 struct apfs_inode_val *new_val;
1380 __le64 sparse_bytes;
1381 struct apfs_x_field xkey;
1382 int xlen;
1383 int buflen;
1384 int err;
1385
1386 buflen = query->len;
1387 buflen += sizeof(struct apfs_x_field) + sizeof(sparse_bytes);
1388 new_val = kzalloc(buflen, GFP_KERNEL);
1389 if (!new_val)
1390 return -ENOMEM;
1391 memcpy(new_val, raw + query->off, query->len);
1392
1393 sparse_bytes = cpu_to_le64(dstream->ds_sparse_bytes);
1394
1395 /* TODO: can we assume that all inode records have an xfield blob? */
1396 xkey.x_type = APFS_INO_EXT_TYPE_SPARSE_BYTES;
1397 xkey.x_flags = APFS_XF_SYSTEM_FIELD | APFS_XF_CHILDREN_INHERIT;
1398 xkey.x_size = cpu_to_le16(sizeof(sparse_bytes));
1399 xlen = apfs_insert_xfield(new_val->xfields, buflen - sizeof(*new_val), &xkey, &sparse_bytes);
1400 if (!xlen) {
1401 /* Buffer has enough space, but the metadata claims otherwise */
1402 apfs_err(inode->i_sb, "bad xfields on inode 0x%llx", apfs_ino(inode));
1403 err = -EFSCORRUPTED;
1404 goto fail;
1405 }
1406
1407 /* Just remove the old record and create a new one */
1408 err = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, new_val, sizeof(*new_val) + xlen);
1409 if (err)
1410 apfs_err(inode->i_sb, "update failed for ino 0x%llx", apfs_ino(inode));
1411
1412 fail:
1413 kfree(new_val);
1414 return err;
1415 }
1416
1417 /**
1418 * apfs_inode_resize_sparse - Update sparse byte count reported in inode record
1419 * @inode: the in-memory inode
1420 * @query: the query that found the inode record
1421 *
1422 * Returns 0 on success, or a negative error code in case of failure.
1423 *
1424 * TODO: should the xfield be removed if the count reaches 0? Should the inode
1425 * flag change?
1426 */
1427 static int apfs_inode_resize_sparse(struct inode *inode, struct apfs_query *query)
1428 {
1429 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
1430 char *raw;
1431 struct apfs_inode_val *inode_raw;
1432 char *xval;
1433 int xlen;
1434 int err;
1435
1436 err = apfs_query_join_transaction(query);
1437 if (err) {
1438 apfs_err(inode->i_sb, "query join failed");
1439 return err;
1440 }
1441 raw = query->node->object.data;
1442 inode_raw = (void *)raw + query->off;
1443
1444 xlen = apfs_find_xfield(inode_raw->xfields,
1445 query->len - sizeof(*inode_raw),
1446 APFS_INO_EXT_TYPE_SPARSE_BYTES, &xval);
1447 if (!xlen && !dstream->ds_sparse_bytes)
1448 return 0;
1449
1450 if (xlen) {
1451 __le64 *sparse_bytes_p;
1452
1453 if (xlen != sizeof(*sparse_bytes_p)) {
1454 apfs_err(inode->i_sb, "bad xlen (%d) on inode 0x%llx", xlen, apfs_ino(inode));
1455 return -EFSCORRUPTED;
1456 }
1457 sparse_bytes_p = (__le64 *)xval;
1458
1459 *sparse_bytes_p = cpu_to_le64(dstream->ds_sparse_bytes);
1460 return 0;
1461 }
1462 return apfs_create_sparse_xfield(inode, query);
1463 }
1464
1465 /**
1466 * apfs_update_inode - Update an existing inode record
1467 * @inode: the modified in-memory inode
1468 * @new_name: name of the new primary link (NULL if unchanged)
1469 *
1470 * Returns 0 on success, or a negative error code in case of failure.
1471 */
1472 int apfs_update_inode(struct inode *inode, char *new_name)
1473 {
1474 struct super_block *sb = inode->i_sb;
1475 struct apfs_sb_info *sbi = APFS_SB(sb);
1476 struct apfs_inode_info *ai = APFS_I(inode);
1477 struct apfs_dstream_info *dstream = &ai->i_dstream;
1478 struct apfs_query *query;
1479 struct apfs_btree_node_phys *node_raw;
1480 struct apfs_inode_val *inode_raw;
1481 int err;
1482
1483 err = apfs_flush_extent_cache(dstream);
1484 if (err) {
1485 apfs_err(sb, "extent cache flush failed for inode 0x%llx", apfs_ino(inode));
1486 return err;
1487 }
1488
1489 query = apfs_inode_lookup(inode);
1490 if (IS_ERR(query)) {
1491 apfs_err(sb, "lookup failed for ino 0x%llx", apfs_ino(inode));
1492 return PTR_ERR(query);
1493 }
1494
1495 /* TODO: copy the record to memory and make all xfield changes there */
1496 err = apfs_inode_rename(inode, new_name, query);
1497 if (err) {
1498 apfs_err(sb, "rename failed for ino 0x%llx", apfs_ino(inode));
1499 goto fail;
1500 }
1501
1502 err = apfs_inode_resize(inode, query);
1503 if (err) {
1504 apfs_err(sb, "resize failed for ino 0x%llx", apfs_ino(inode));
1505 goto fail;
1506 }
1507
1508 err = apfs_inode_resize_sparse(inode, query);
1509 if (err) {
1510 apfs_err(sb, "sparse resize failed for ino 0x%llx", apfs_ino(inode));
1511 goto fail;
1512 }
1513 if (dstream->ds_sparse_bytes)
1514 ai->i_int_flags |= APFS_INODE_IS_SPARSE;
1515
1516 /* TODO: just use apfs_btree_replace()? */
1517 err = apfs_query_join_transaction(query);
1518 if (err) {
1519 apfs_err(sb, "query join failed");
1520 goto fail;
1521 }
1522 node_raw = (void *)query->node->object.data;
1523 apfs_assert_in_transaction(sb, &node_raw->btn_o);
1524 inode_raw = (void *)node_raw + query->off;
1525
1526 inode_raw->parent_id = cpu_to_le64(ai->i_parent_id);
1527 inode_raw->private_id = cpu_to_le64(dstream->ds_id);
1528 inode_raw->mode = cpu_to_le16(inode->i_mode);
1529 inode_raw->owner = cpu_to_le32(i_uid_read(inode));
1530 inode_raw->group = cpu_to_le32(i_gid_read(inode));
1531 inode_raw->default_protection_class = cpu_to_le32(ai->i_key_class);
1532 inode_raw->internal_flags = cpu_to_le64(ai->i_int_flags);
1533 inode_raw->bsd_flags = cpu_to_le32(ai->i_bsd_flags);
1534
1535 /* Don't persist the uid/gid provided by the user on mount */
1536 if (uid_valid(sbi->s_uid))
1537 inode_raw->owner = cpu_to_le32(ai->i_saved_uid);
1538 if (gid_valid(sbi->s_gid))
1539 inode_raw->group = cpu_to_le32(ai->i_saved_gid);
1540
1541 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1542 inode_raw->change_time = cpu_to_le64(timespec64_to_ns(&inode->i_ctime));
1543 #else
1544 struct timespec64 ictime = inode_get_ctime(inode);
1545 inode_raw->change_time = cpu_to_le64(timespec64_to_ns(&ictime));
1546 #endif
1547
1548 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1549 inode_raw->access_time = cpu_to_le64(timespec64_to_ns(&inode->i_atime));
1550 inode_raw->mod_time = cpu_to_le64(timespec64_to_ns(&inode->i_mtime));
1551 #else
1552 struct timespec64 ts = inode_get_mtime(inode);
1553 inode_raw->mod_time = cpu_to_le64(timespec64_to_ns(&ts));
1554 ts = inode_get_atime(inode);
1555 inode_raw->access_time = cpu_to_le64(timespec64_to_ns(&ts));
1556 #endif
1557 inode_raw->create_time = cpu_to_le64(timespec64_to_ns(&ai->i_crtime));
1558
1559 if (S_ISDIR(inode->i_mode)) {
1560 inode_raw->nchildren = cpu_to_le32(ai->i_nchildren);
1561 } else {
1562 /* The remaining link for orphan inodes is not counted */
1563 inode_raw->nlink = cpu_to_le32(inode->i_nlink);
1564 }
1565
1566 fail:
1567 apfs_free_query(query);
1568 return err;
1569 }
1570
1571 /**
1572 * apfs_delete_inode - Delete an inode record
1573 * @inode: the vfs inode to delete
1574 *
1575 * Returns 0 on success or a negative error code in case of failure, which may
1576 * be -EAGAIN if the inode was not deleted in full.
1577 */
1578 static int apfs_delete_inode(struct inode *inode)
1579 {
1580 struct super_block *sb = inode->i_sb;
1581 struct apfs_inode_info *ai = APFS_I(inode);
1582 struct apfs_dstream_info *dstream = NULL;
1583 struct apfs_query *query;
1584 u64 old_dstream_id;
1585 int ret;
1586
1587 ret = apfs_delete_all_xattrs(inode);
1588 if (ret) {
1589 apfs_err(sb, "xattr deletion failed for ino 0x%llx", apfs_ino(inode));
1590 return ret;
1591 }
1592
1593 dstream = &ai->i_dstream;
1594 old_dstream_id = dstream->ds_id;
1595
1596 /*
1597 * This is very wasteful since all the new extents and references will
1598 * get deleted right away, but it only affects clones, so I don't see a
1599 * big reason to improve it (TODO)
1600 */
1601 ret = apfs_inode_create_exclusive_dstream(inode);
1602 if (ret) {
1603 apfs_err(sb, "dstream creation failed for ino 0x%llx", apfs_ino(inode));
1604 return ret;
1605 }
1606
1607 /* TODO: what about partial deletion of xattrs? Is that allowed? */
1608 ret = apfs_inode_delete_front(inode);
1609 if (ret) {
1610 /*
1611 * If the inode had too many extents, only the first few get
1612 * deleted and the inode remains in the orphan list for now.
1613 * I don't know why the deletion starts at the front, but it
1614 * seems to be what the official driver does.
1615 */
1616 if (ret != -EAGAIN) {
1617 apfs_err(sb, "head deletion failed for ino 0x%llx", apfs_ino(inode));
1618 return ret;
1619 }
1620 if (dstream->ds_id != old_dstream_id) {
1621 ret = apfs_update_inode(inode, NULL /* new_name */);
1622 if (ret) {
1623 apfs_err(sb, "dstream id update failed for orphan 0x%llx", apfs_ino(inode));
1624 return ret;
1625 }
1626 }
1627 return -EAGAIN;
1628 }
1629
1630 ret = apfs_put_dstream_rec(dstream);
1631 if (ret) {
1632 apfs_err(sb, "failed to put dstream for ino 0x%llx", apfs_ino(inode));
1633 return ret;
1634 }
1635 dstream = NULL;
1636 ai->i_has_dstream = false;
1637
1638 query = apfs_inode_lookup(inode);
1639 if (IS_ERR(query)) {
1640 apfs_err(sb, "lookup failed for ino 0x%llx", apfs_ino(inode));
1641 return PTR_ERR(query);
1642 }
1643 ret = apfs_btree_remove(query);
1644 apfs_free_query(query);
1645 if (ret) {
1646 apfs_err(sb, "removal failed for ino 0x%llx", apfs_ino(inode));
1647 return ret;
1648 }
1649
1650 ai->i_cleaned = true;
1651 return ret;
1652 }
1653
1654 /**
1655 * apfs_clean_single_orphan - Clean the given orphan file
1656 * @inode: inode for the file to clean
1657 *
1658 * Returns 0 on success or a negative error code in case of failure, which may
1659 * be -EAGAIN if the file could not be deleted in full.
1660 */
1661 static int apfs_clean_single_orphan(struct inode *inode)
1662 {
1663 struct super_block *sb = inode->i_sb;
1664 u64 ino = apfs_ino(inode);
1665 bool eagain = false;
1666 int err;
1667
1668 err = apfs_transaction_start(sb, APFS_TRANS_DEL);
1669 if (err)
1670 return err;
1671 err = apfs_delete_inode(inode);
1672 if (err) {
1673 if (err != -EAGAIN) {
1674 apfs_err(sb, "failed to delete orphan 0x%llx", ino);
1675 goto fail;
1676 }
1677 eagain = true;
1678 } else {
1679 err = apfs_delete_orphan_link(inode);
1680 if (err) {
1681 apfs_err(sb, "failed to unlink orphan 0x%llx", ino);
1682 goto fail;
1683 }
1684 }
1685 err = apfs_transaction_commit(sb);
1686 if (err)
1687 goto fail;
1688 return eagain ? -EAGAIN : 0;
1689
1690 fail:
1691 apfs_transaction_abort(sb);
1692 return err;
1693 }
1694
1695 /**
1696 * apfs_clean_any_orphan - Pick an orphan and delete as much as reasonable
1697 * @sb: filesystem superblock
1698 *
1699 * Returns 0 on success, or a negative error code in case of failure, which may
1700 * be -ENODATA if there are no more orphan files or -EAGAIN if a file could not
1701 * be deleted in full.
1702 */
1703 static int apfs_clean_any_orphan(struct super_block *sb)
1704 {
1705 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
1706 struct inode *inode = NULL;
1707 int err;
1708 u64 ino;
1709
1710 down_read(&nxi->nx_big_sem);
1711 err = apfs_any_orphan_ino(sb, &ino);
1712 up_read(&nxi->nx_big_sem);
1713 if (err) {
1714 if (err == -ENODATA)
1715 return -ENODATA;
1716 apfs_err(sb, "failed to find orphan inode numbers");
1717 return err;
1718 }
1719
1720 inode = apfs_iget(sb, ino);
1721 if (IS_ERR(inode)) {
1722 err = PTR_ERR(inode);
1723 if (err != -ENODATA) {
1724 apfs_err(sb, "iget failed for orphan 0x%llx", ino);
1725 return err;
1726 }
1727 /*
1728 * This happens rarely for files with no extents, if we hit a
1729 * race with ->evict_inode(). Not a problem: the file is gone.
1730 */
1731 apfs_notice(sb, "orphan 0x%llx not found", ino);
1732 return 0;
1733 }
1734
1735 if (atomic_read(&inode->i_count) > 1)
1736 goto out;
1737 err = apfs_clean_single_orphan(inode);
1738 if (err && err != -EAGAIN) {
1739 apfs_err(sb, "failed to clean orphan 0x%llx", ino);
1740 goto out;
1741 }
1742 out:
1743 iput(inode);
1744 return err;
1745 }
1746
1747 /**
1748 * apfs_schedule_orphan_cleanup - Schedule cleanup for orphan inodes
1749 * @sb: filesystem superblock
1750 */
1751 void apfs_schedule_orphan_cleanup(struct super_block *sb)
1752 {
1753 struct apfs_sb_info *sbi = APFS_SB(sb);
1754
1755 /*
1756 * Don't schedule cleanups during unmount: completing all of it could
1757 * take a while so just leave future mounts to handle the orphans.
1758 */
1759 if (atomic_read(&sb->s_active) == 0)
1760 return;
1761
1762 /*
1763 * Don't keep retrying orphan cleanups nonstop when they run into an
1764 * unexpected error: it won't do any good and it will flood dmesg. We
1765 * will retry eventually for ENOSPC, but that's handled elsewhere.
1766 */
1767 if (atomic_read(&sbi->s_orphan_cleanup_err))
1768 return;
1769
1770 schedule_work(&sbi->s_orphan_cleanup_work);
1771 }
1772
1773 /**
1774 * apfs_clean_orphans - Delete as many orphan files as is reasonable
1775 * @sb: filesystem superblock
1776 *
1777 * Returns 0 on success or a negative error code in case of failure.
1778 */
1779 static int apfs_clean_orphans(struct super_block *sb)
1780 {
1781 struct apfs_sb_info *sbi = APFS_SB(sb);
1782 int ret, i;
1783
1784 for (i = 0; i < 100; ++i) {
1785 ret = apfs_clean_any_orphan(sb);
1786 if (ret == 0)
1787 continue;
1788 if (ret == -ENODATA)
1789 return 0;
1790 if (ret == -EAGAIN)
1791 break;
1792 apfs_err(sb, "failed to delete an orphan file");
1793 atomic_set(&sbi->s_orphan_cleanup_err, ret);
1794 return ret;
1795 }
1796
1797 /*
1798 * If a file is too big, or if there are too many files, take a break
1799 * and continue later.
1800 */
1801 apfs_schedule_orphan_cleanup(sb);
1802 return 0;
1803 }
1804
1805 void apfs_evict_inode(struct inode *inode)
1806 {
1807 struct super_block *sb = inode->i_sb;
1808 struct apfs_inode_info *ai = APFS_I(inode);
1809 int err;
1810
1811 if (is_bad_inode(inode) || inode->i_nlink || ai->i_cleaned)
1812 goto out;
1813
1814 if (!ai->i_has_dstream || ai->i_dstream.ds_size == 0) {
1815 /* For files with no extents, scheduled cleanup wastes time */
1816 err = apfs_clean_single_orphan(inode);
1817 if (err) {
1818 apfs_err(sb, "failed to clean orphan 0x%llx (err:%d)", apfs_ino(inode), err);
1819 atomic_set(&APFS_SB(sb)->s_orphan_cleanup_err, err);
1820 }
1821 goto out;
1822 }
1823
1824 /* If the inode still has extents then schedule cleanup for the rest */
1825 apfs_schedule_orphan_cleanup(sb);
1826 out:
1827 truncate_inode_pages_final(&inode->i_data);
1828 clear_inode(inode);
1829 }
1830
1831 void apfs_orphan_cleanup_work(struct work_struct *work)
1832 {
1833 struct super_block *sb = NULL;
1834 struct apfs_sb_info *sbi = NULL;
1835 struct inode *priv = NULL;
1836 int err;
1837
1838 sbi = container_of(work, struct apfs_sb_info, s_orphan_cleanup_work);
1839 priv = sbi->s_private_dir;
1840 sb = priv->i_sb;
1841
1842 if (sb->s_flags & SB_RDONLY) {
1843 apfs_alert(sb, "attempt to flush orphans in read-only mount");
1844 return;
1845 }
1846
1847 err = apfs_clean_orphans(sb);
1848 if (err)
1849 apfs_err(sb, "orphan cleanup failed (err:%d)", err);
1850 }
1851
1852 /**
1853 * apfs_insert_inode_locked - Wrapper for insert_inode_locked4()
1854 * @inode: vfs inode to insert in cache
1855 *
1856 * Works the same as insert_inode_locked(), but can handle 64-bit inode numbers
1857 * on 32-bit architectures.
1858 */
1859 static int apfs_insert_inode_locked(struct inode *inode)
1860 {
1861 u64 cnid = apfs_ino(inode);
1862
1863 return insert_inode_locked4(inode, cnid, apfs_test_inode, &cnid);
1864 }
1865
1866 /**
1867 * apfs_new_inode - Create a new in-memory inode
1868 * @dir: parent inode
1869 * @mode: mode bits for the new inode
1870 * @rdev: device id (0 if not a device file)
1871 *
1872 * Returns a pointer to the new vfs inode on success, or an error pointer in
1873 * case of failure.
1874 */
1875 struct inode *apfs_new_inode(struct inode *dir, umode_t mode, dev_t rdev)
1876 {
1877 struct super_block *sb = dir->i_sb;
1878 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
1879 struct inode *inode;
1880 struct apfs_inode_info *ai;
1881 struct apfs_dstream_info *dstream;
1882 u64 cnid;
1883 struct timespec64 now;
1884
1885 /* Updating on-disk structures here is odd, but it works for now */
1886 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
1887
1888 inode = new_inode(sb);
1889 if (!inode)
1890 return ERR_PTR(-ENOMEM);
1891 ai = APFS_I(inode);
1892 dstream = &ai->i_dstream;
1893
1894 cnid = le64_to_cpu(vsb_raw->apfs_next_obj_id);
1895 le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
1896 apfs_set_ino(inode, cnid);
1897
1898 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1899 inode_init_owner(inode, dir, mode);
1900 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1901 inode_init_owner(&init_user_ns, inode, dir, mode);
1902 #else
1903 inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
1904 #endif
1905
1906 ai->i_saved_uid = i_uid_read(inode);
1907 ai->i_saved_gid = i_gid_read(inode);
1908 ai->i_parent_id = apfs_ino(dir);
1909 set_nlink(inode, 1);
1910 ai->i_nchildren = 0;
1911 if (apfs_vol_is_encrypted(sb) && S_ISREG(mode))
1912 ai->i_key_class = apfs_dflt_key_class(sb);
1913 else
1914 ai->i_key_class = 0;
1915 ai->i_int_flags = APFS_INODE_NO_RSRC_FORK;
1916 ai->i_bsd_flags = 0;
1917
1918 ai->i_has_dstream = false;
1919 dstream->ds_id = cnid;
1920 dstream->ds_size = 0;
1921 dstream->ds_sparse_bytes = 0;
1922 dstream->ds_shared = false;
1923
1924 now = current_time(inode);
1925 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1926 inode->i_atime = inode->i_mtime = inode->i_ctime = ai->i_crtime = now;
1927 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1928 inode_set_ctime_to_ts(inode, now);
1929 inode->i_atime = inode->i_mtime = ai->i_crtime = now;
1930 #else
1931 ai->i_crtime = simple_inode_init_ts(inode);
1932 #endif
1933 vsb_raw->apfs_last_mod_time = cpu_to_le64(timespec64_to_ns(&now));
1934
1935 if (S_ISREG(mode))
1936 le64_add_cpu(&vsb_raw->apfs_num_files, 1);
1937 else if (S_ISDIR(mode))
1938 le64_add_cpu(&vsb_raw->apfs_num_directories, 1);
1939 else if (S_ISLNK(mode))
1940 le64_add_cpu(&vsb_raw->apfs_num_symlinks, 1);
1941 else
1942 le64_add_cpu(&vsb_raw->apfs_num_other_fsobjects, 1);
1943
1944 if (apfs_insert_inode_locked(inode)) {
1945 /* The inode number should have been free, but wasn't */
1946 apfs_err(sb, "next obj_id (0x%llx) not free", cnid);
1947 make_bad_inode(inode);
1948 iput(inode);
1949 return ERR_PTR(-EFSCORRUPTED);
1950 }
1951
1952 /* No need to dirty the inode, we'll write it to disk right away */
1953 apfs_inode_set_ops(inode, rdev, false /* compressed */);
1954 return inode;
1955 }
1956
1957 /**
1958 * apfs_create_inode_rec - Create an inode record in the catalog b-tree
1959 * @sb: filesystem superblock
1960 * @inode: vfs inode to record
1961 * @dentry: dentry for primary link
1962 *
1963 * Returns 0 on success or a negative error code in case of failure.
1964 */
1965 int apfs_create_inode_rec(struct super_block *sb, struct inode *inode,
1966 struct dentry *dentry)
1967 {
1968 struct apfs_sb_info *sbi = APFS_SB(sb);
1969 struct apfs_query *query;
1970 struct apfs_inode_key raw_key;
1971 struct apfs_inode_val *raw_val;
1972 int val_len;
1973 int ret;
1974
1975 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
1976 if (!query)
1977 return -ENOMEM;
1978 apfs_init_inode_key(apfs_ino(inode), &query->key);
1979 query->flags |= APFS_QUERY_CAT;
1980
1981 ret = apfs_btree_query(sb, &query);
1982 if (ret && ret != -ENODATA) {
1983 apfs_err(sb, "query failed for ino 0x%llx", apfs_ino(inode));
1984 goto fail;
1985 }
1986
1987 apfs_key_set_hdr(APFS_TYPE_INODE, apfs_ino(inode), &raw_key);
1988
1989 val_len = apfs_build_inode_val(inode, &dentry->d_name, &raw_val);
1990 if (val_len < 0) {
1991 ret = val_len;
1992 goto fail;
1993 }
1994
1995 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, val_len);
1996 if (ret)
1997 apfs_err(sb, "insertion failed for ino 0x%llx", apfs_ino(inode));
1998 kfree(raw_val);
1999
2000 fail:
2001 apfs_free_query(query);
2002 return ret;
2003 }
2004
2005 /**
2006 * apfs_setsize - Change the size of a regular file
2007 * @inode: the vfs inode
2008 * @new_size: the new size
2009 *
2010 * Returns 0 on success or a negative error code in case of failure.
2011 */
2012 static int apfs_setsize(struct inode *inode, loff_t new_size)
2013 {
2014 struct super_block *sb = inode->i_sb;
2015 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
2016 int err;
2017
2018 if (new_size == inode->i_size)
2019 return 0;
2020 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
2021 inode->i_mtime = inode->i_ctime = current_time(inode);
2022 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
2023 inode->i_mtime = inode_set_ctime_current(inode);
2024 #else
2025 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2026 #endif
2027
2028 err = apfs_inode_create_dstream_rec(inode);
2029 if (err) {
2030 apfs_err(sb, "failed to create dstream for ino 0x%llx", apfs_ino(inode));
2031 return err;
2032 }
2033
2034 /* Must be called before i_size is changed */
2035 err = apfs_truncate(dstream, new_size);
2036 if (err) {
2037 apfs_err(sb, "truncation failed for ino 0x%llx", apfs_ino(inode));
2038 return err;
2039 }
2040
2041 truncate_setsize(inode, new_size);
2042 dstream->ds_size = i_size_read(inode);
2043 return 0;
2044 }
2045
2046 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
2047 int apfs_setattr(struct dentry *dentry, struct iattr *iattr)
2048 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
2049 int apfs_setattr(struct user_namespace *mnt_userns,
2050 struct dentry *dentry, struct iattr *iattr)
2051 #else
2052 int apfs_setattr(struct mnt_idmap *idmap,
2053 struct dentry *dentry, struct iattr *iattr)
2054 #endif
2055 {
2056 struct inode *inode = d_inode(dentry);
2057 struct super_block *sb = inode->i_sb;
2058 bool resizing = S_ISREG(inode->i_mode) && (iattr->ia_valid & ATTR_SIZE);
2059 bool shrinking = false;
2060 int err;
2061
2062 if (resizing && iattr->ia_size > APFS_MAX_FILE_SIZE)
2063 return -EFBIG;
2064 if (resizing && iattr->ia_size < inode->i_size)
2065 shrinking = true;
2066
2067 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
2068 err = setattr_prepare(dentry, iattr);
2069 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
2070 err = setattr_prepare(&init_user_ns, dentry, iattr);
2071 #else
2072 err = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
2073 #endif
2074 if (err)
2075 return err;
2076
2077 /* TODO: figure out why ->write_inode() isn't firing */
2078 err = apfs_transaction_start(sb, shrinking ? APFS_TRANS_DEL : APFS_TRANS_REG);
2079 if (err)
2080 return err;
2081 apfs_inode_join_transaction(sb, inode);
2082
2083 if (resizing) {
2084 err = apfs_setsize(inode, iattr->ia_size);
2085 if (err) {
2086 apfs_err(sb, "setsize failed for ino 0x%llx", apfs_ino(inode));
2087 goto fail;
2088 }
2089 }
2090
2091 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
2092 setattr_copy(inode, iattr);
2093 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
2094 setattr_copy(&init_user_ns, inode, iattr);
2095 #else
2096 setattr_copy(&nop_mnt_idmap, inode, iattr);
2097 #endif
2098
2099 mark_inode_dirty(inode);
2100 err = apfs_transaction_commit(sb);
2101 if (err)
2102 goto fail;
2103 return 0;
2104
2105 fail:
2106 apfs_transaction_abort(sb);
2107 return err;
2108 }
2109
2110 /* TODO: this only seems to be necessary because ->write_inode() isn't firing */
2111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
2112 int apfs_update_time(struct inode *inode, enum fs_update_time time, unsigned int flags)
2113 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
2114 int apfs_update_time(struct inode *inode, struct timespec64 *time, int flags)
2115 #else
2116 int apfs_update_time(struct inode *inode, int flags)
2117 #endif
2118 {
2119 struct super_block *sb = inode->i_sb;
2120 int err;
2121
2122 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2123 if (err)
2124 return err;
2125 apfs_inode_join_transaction(sb, inode);
2126
2127 #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
2128 generic_update_time(inode, time, flags);
2129 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0) && !RHEL_VERSION_GE(9, 6)
2130 generic_update_time(inode, time, flags);
2131 #else
2132 generic_update_time(inode, flags);
2133 #endif
2134
2135 err = apfs_transaction_commit(sb);
2136 if (err)
2137 goto fail;
2138 return 0;
2139
2140 fail:
2141 apfs_transaction_abort(sb);
2142 return err;
2143 }
2144
2145 static int apfs_ioc_set_dflt_pfk(struct file *file, void __user *user_pfk)
2146 {
2147 struct inode *inode = file_inode(file);
2148 struct super_block *sb = inode->i_sb;
2149 struct apfs_sb_info *sbi = APFS_SB(sb);
2150 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
2151 struct apfs_wrapped_crypto_state pfk_hdr;
2152 struct apfs_crypto_state_val *pfk;
2153 unsigned int key_len;
2154
2155 if (__copy_from_user(&pfk_hdr, user_pfk, sizeof(pfk_hdr)))
2156 return -EFAULT;
2157 key_len = le16_to_cpu(pfk_hdr.key_len);
2158 if (key_len > MAX_PFK_LEN)
2159 return -EFBIG;
2160 pfk = kmalloc(sizeof(*pfk) + key_len, GFP_KERNEL);
2161 if (!pfk)
2162 return -ENOMEM;
2163 if (__copy_from_user(&pfk->state, user_pfk, sizeof(pfk_hdr) + key_len)) {
2164 kfree(pfk);
2165 return -EFAULT;
2166 }
2167 pfk->refcnt = cpu_to_le32(1);
2168
2169 down_write(&nxi->nx_big_sem);
2170
2171 if (sbi->s_dflt_pfk)
2172 kfree(sbi->s_dflt_pfk);
2173 sbi->s_dflt_pfk = pfk;
2174
2175 up_write(&nxi->nx_big_sem);
2176
2177 return 0;
2178 }
2179
2180 static int apfs_ioc_set_dir_class(struct file *file, u32 __user *user_class)
2181 {
2182 struct inode *inode = file_inode(file);
2183 struct apfs_inode_info *ai = APFS_I(inode);
2184 struct super_block *sb = inode->i_sb;
2185 u32 class;
2186 int err;
2187
2188 if (get_user(class, user_class))
2189 return -EFAULT;
2190
2191 ai->i_key_class = class;
2192
2193 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2194 if (err)
2195 return err;
2196 apfs_inode_join_transaction(sb, inode);
2197 err = apfs_transaction_commit(sb);
2198 if (err)
2199 goto fail;
2200 return 0;
2201
2202 fail:
2203 apfs_transaction_abort(sb);
2204 return err;
2205 }
2206
2207 static int apfs_ioc_set_pfk(struct file *file, void __user *user_pfk)
2208 {
2209 struct inode *inode = file_inode(file);
2210 struct super_block *sb = inode->i_sb;
2211 struct apfs_wrapped_crypto_state pfk_hdr;
2212 struct apfs_crypto_state_val *pfk;
2213 struct apfs_inode_info *ai = APFS_I(inode);
2214 struct apfs_dstream_info *dstream = &ai->i_dstream;
2215 unsigned int key_len, key_class;
2216 int err;
2217
2218 if (__copy_from_user(&pfk_hdr, user_pfk, sizeof(pfk_hdr)))
2219 return -EFAULT;
2220 key_len = le16_to_cpu(pfk_hdr.key_len);
2221 if (key_len > MAX_PFK_LEN)
2222 return -EFBIG;
2223 pfk = kmalloc(sizeof(*pfk) + key_len, GFP_KERNEL);
2224 if (!pfk)
2225 return -ENOMEM;
2226 if (__copy_from_user(&pfk->state, user_pfk, sizeof(pfk_hdr) + key_len)) {
2227 kfree(pfk);
2228 return -EFAULT;
2229 }
2230 pfk->refcnt = cpu_to_le32(1);
2231
2232 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2233 if (err) {
2234 kfree(pfk);
2235 return err;
2236 }
2237
2238 err = apfs_crypto_set_key(sb, dstream->ds_id, pfk);
2239 if (err)
2240 goto fail;
2241
2242 key_class = le32_to_cpu(pfk_hdr.persistent_class);
2243 if (ai->i_key_class != key_class) {
2244 ai->i_key_class = key_class;
2245 apfs_inode_join_transaction(sb, inode);
2246 }
2247
2248 err = apfs_transaction_commit(sb);
2249 if (err)
2250 goto fail;
2251 kfree(pfk);
2252 return 0;
2253
2254 fail:
2255 apfs_transaction_abort(sb);
2256 kfree(pfk);
2257 return err;
2258 }
2259
2260 static int apfs_ioc_get_class(struct file *file, u32 __user *user_class)
2261 {
2262 struct inode *inode = file_inode(file);
2263 struct apfs_inode_info *ai = APFS_I(inode);
2264 u32 class;
2265
2266 class = ai->i_key_class;
2267 if (put_user(class, user_class))
2268 return -EFAULT;
2269 return 0;
2270 }
2271
2272 static int apfs_ioc_get_pfk(struct file *file, void __user *user_pfk)
2273 {
2274 struct inode *inode = file_inode(file);
2275 struct super_block *sb = inode->i_sb;
2276 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
2277 struct apfs_wrapped_crypto_state pfk_hdr;
2278 struct apfs_crypto_state_val *pfk;
2279 unsigned int max_len, key_len;
2280 struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
2281 int err;
2282
2283 if (__copy_from_user(&pfk_hdr, user_pfk, sizeof(pfk_hdr)))
2284 return -EFAULT;
2285 max_len = le16_to_cpu(pfk_hdr.key_len);
2286 if (max_len > MAX_PFK_LEN)
2287 return -EFBIG;
2288 pfk = kmalloc(sizeof(*pfk) + max_len, GFP_KERNEL);
2289 if (!pfk)
2290 return -ENOMEM;
2291
2292 down_read(&nxi->nx_big_sem);
2293
2294 err = apfs_crypto_get_key(sb, dstream->ds_id, pfk, max_len);
2295 if (err)
2296 goto fail;
2297
2298 up_read(&nxi->nx_big_sem);
2299
2300 key_len = le16_to_cpu(pfk->state.key_len);
2301 if (__copy_to_user(user_pfk, &pfk->state, sizeof(pfk_hdr) + key_len)) {
2302 kfree(pfk);
2303 return -EFAULT;
2304 }
2305
2306 kfree(pfk);
2307 return 0;
2308
2309 fail:
2310 up_read(&nxi->nx_big_sem);
2311 kfree(pfk);
2312 return err;
2313 }
2314
2315 /*
2316 * Older kernels have no vfs_ioc_setflags_prepare(), so don't implement the
2317 * SETFLAGS/GETFLAGS ioctls there. It should be easy to fix, but it's not
2318 * really needed at all. Be careful with this macro check, because it nests
2319 * over a few others.
2320 */
2321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
2322
2323 /**
2324 * apfs_getflags - Read an inode's bsd flags in FS_IOC_GETFLAGS format
2325 * @inode: the vfs inode
2326 */
2327 static unsigned int apfs_getflags(struct inode *inode)
2328 {
2329 struct apfs_inode_info *ai = APFS_I(inode);
2330 unsigned int flags = 0;
2331
2332 if (ai->i_bsd_flags & APFS_INOBSD_APPEND)
2333 flags |= FS_APPEND_FL;
2334 if (ai->i_bsd_flags & APFS_INOBSD_IMMUTABLE)
2335 flags |= FS_IMMUTABLE_FL;
2336 if (ai->i_bsd_flags & APFS_INOBSD_NODUMP)
2337 flags |= FS_NODUMP_FL;
2338 return flags;
2339 }
2340
2341 /**
2342 * apfs_setflags - Set an inode's bsd flags
2343 * @inode: the vfs inode
2344 * @flags: flags to set, in FS_IOC_SETFLAGS format
2345 */
2346 static void apfs_setflags(struct inode *inode, unsigned int flags)
2347 {
2348 struct apfs_inode_info *ai = APFS_I(inode);
2349 unsigned int i_flags = 0;
2350
2351 if (flags & FS_APPEND_FL) {
2352 ai->i_bsd_flags |= APFS_INOBSD_APPEND;
2353 i_flags |= S_APPEND;
2354 } else {
2355 ai->i_bsd_flags &= ~APFS_INOBSD_APPEND;
2356 }
2357
2358 if (flags & FS_IMMUTABLE_FL) {
2359 ai->i_bsd_flags |= APFS_INOBSD_IMMUTABLE;
2360 i_flags |= S_IMMUTABLE;
2361 } else {
2362 ai->i_bsd_flags &= ~APFS_INOBSD_IMMUTABLE;
2363 }
2364
2365 if (flags & FS_NODUMP_FL)
2366 ai->i_bsd_flags |= APFS_INOBSD_NODUMP;
2367 else
2368 ai->i_bsd_flags &= ~APFS_INOBSD_NODUMP;
2369
2370 inode_set_flags(inode, i_flags, S_IMMUTABLE | S_APPEND);
2371 }
2372
2373 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0)
2374
2375 /**
2376 * apfs_ioc_getflags - Ioctl handler for FS_IOC_GETFLAGS
2377 * @file: affected file
2378 * @arg: ioctl argument
2379 *
2380 * Returns 0 on success, or a negative error code in case of failure.
2381 */
2382 static int apfs_ioc_getflags(struct file *file, int __user *arg)
2383 {
2384 unsigned int flags = apfs_getflags(file_inode(file));
2385
2386 return put_user(flags, arg);
2387 }
2388
2389 /**
2390 * apfs_do_ioc_setflags - Actual work for apfs_ioc_setflags(), after preparation
2391 * @inode: affected vfs inode
2392 * @newflags: inode flags to set, in FS_IOC_SETFLAGS format
2393 *
2394 * Returns 0 on success, or a negative error code in case of failure.
2395 */
2396 static int apfs_do_ioc_setflags(struct inode *inode, unsigned int newflags)
2397 {
2398 struct super_block *sb = inode->i_sb;
2399 unsigned int oldflags;
2400 int err;
2401
2402 lockdep_assert_held_write(&inode->i_rwsem);
2403
2404 oldflags = apfs_getflags(inode);
2405 err = vfs_ioc_setflags_prepare(inode, oldflags, newflags);
2406 if (err)
2407 return err;
2408
2409 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2410 if (err)
2411 return err;
2412
2413 apfs_inode_join_transaction(sb, inode);
2414 apfs_setflags(inode, newflags);
2415 inode->i_ctime = current_time(inode);
2416
2417 err = apfs_transaction_commit(sb);
2418 if (err)
2419 apfs_transaction_abort(sb);
2420 return err;
2421 }
2422
2423 /**
2424 * apfs_ioc_setflags - Ioctl handler for FS_IOC_SETFLAGS
2425 * @file: affected file
2426 * @arg: ioctl argument
2427 *
2428 * Returns 0 on success, or a negative error code in case of failure.
2429 */
2430 static int apfs_ioc_setflags(struct file *file, int __user *arg)
2431 {
2432 struct inode *inode = file_inode(file);
2433 struct super_block *sb = inode->i_sb;
2434 unsigned int newflags;
2435 int err;
2436
2437 if (sb->s_flags & SB_RDONLY)
2438 return -EROFS;
2439
2440 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
2441 if (!inode_owner_or_capable(inode))
2442 #else
2443 if (!inode_owner_or_capable(&init_user_ns, inode))
2444 #endif
2445 return -EPERM;
2446
2447 if (get_user(newflags, arg))
2448 return -EFAULT;
2449
2450 if (newflags & ~(FS_APPEND_FL | FS_IMMUTABLE_FL | FS_NODUMP_FL))
2451 return -EOPNOTSUPP;
2452
2453 err = mnt_want_write_file(file);
2454 if (err)
2455 return err;
2456
2457 inode_lock(inode);
2458 err = apfs_do_ioc_setflags(inode, newflags);
2459 inode_unlock(inode);
2460
2461 mnt_drop_write_file(file);
2462 return err;
2463 }
2464
2465 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
2466
2467 int apfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
2468 {
2469 unsigned int flags = apfs_getflags(d_inode(dentry));
2470
2471 fileattr_fill_flags(fa, flags);
2472 return 0;
2473 }
2474
2475 int apfs_fileattr_set(struct user_namespace *mnt_userns, struct dentry *dentry, struct fileattr *fa)
2476 {
2477 struct inode *inode = d_inode(dentry);
2478 struct super_block *sb = inode->i_sb;
2479 int err;
2480
2481 if (sb->s_flags & SB_RDONLY)
2482 return -EROFS;
2483
2484 if (fa->flags & ~(FS_APPEND_FL | FS_IMMUTABLE_FL | FS_NODUMP_FL))
2485 return -EOPNOTSUPP;
2486 if (fileattr_has_fsx(fa))
2487 return -EOPNOTSUPP;
2488
2489 lockdep_assert_held_write(&inode->i_rwsem);
2490
2491 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2492 if (err)
2493 return err;
2494
2495 apfs_inode_join_transaction(sb, inode);
2496 apfs_setflags(inode, fa->flags);
2497 inode->i_ctime = current_time(inode);
2498
2499 err = apfs_transaction_commit(sb);
2500 if (err)
2501 apfs_transaction_abort(sb);
2502 return err;
2503 }
2504
2505 #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) */
2506
2507 int apfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
2508 {
2509 unsigned int flags = apfs_getflags(d_inode(dentry));
2510
2511 fileattr_fill_flags(fa, flags);
2512 return 0;
2513 }
2514
2515 int apfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry, struct fileattr *fa)
2516 {
2517 struct inode *inode = d_inode(dentry);
2518 struct super_block *sb = inode->i_sb;
2519 int err;
2520
2521 if (sb->s_flags & SB_RDONLY)
2522 return -EROFS;
2523
2524 if (fa->flags & ~(FS_APPEND_FL | FS_IMMUTABLE_FL | FS_NODUMP_FL))
2525 return -EOPNOTSUPP;
2526 if (fileattr_has_fsx(fa))
2527 return -EOPNOTSUPP;
2528
2529 lockdep_assert_held_write(&inode->i_rwsem);
2530
2531 err = apfs_transaction_start(sb, APFS_TRANS_REG);
2532 if (err)
2533 return err;
2534
2535 apfs_inode_join_transaction(sb, inode);
2536 apfs_setflags(inode, fa->flags);
2537 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
2538 inode->i_ctime = current_time(inode);
2539 #else
2540 inode_set_ctime_current(inode);
2541 #endif
2542
2543 err = apfs_transaction_commit(sb);
2544 if (err)
2545 apfs_transaction_abort(sb);
2546 return err;
2547 }
2548
2549 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0) */
2550
2551 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0) */
2552
2553 long apfs_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2554 {
2555 void __user *argp = (void __user *)arg;
2556
2557 switch (cmd) {
2558 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
2559 case FS_IOC_GETFLAGS:
2560 return apfs_ioc_getflags(file, argp);
2561 case FS_IOC_SETFLAGS:
2562 return apfs_ioc_setflags(file, argp);
2563 #endif
2564 case APFS_IOC_SET_DFLT_PFK:
2565 return apfs_ioc_set_dflt_pfk(file, argp);
2566 case APFS_IOC_SET_DIR_CLASS:
2567 return apfs_ioc_set_dir_class(file, argp);
2568 case APFS_IOC_GET_CLASS:
2569 return apfs_ioc_get_class(file, argp);
2570 case APFS_IOC_TAKE_SNAPSHOT:
2571 return apfs_ioc_take_snapshot(file, argp);
2572 default:
2573 return -ENOTTY;
2574 }
2575 }
2576
2577 long apfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2578 {
2579 void __user *argp = (void __user *)arg;
2580
2581 switch (cmd) {
2582 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 13, 0) && LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
2583 case FS_IOC_GETFLAGS:
2584 return apfs_ioc_getflags(file, argp);
2585 case FS_IOC_SETFLAGS:
2586 return apfs_ioc_setflags(file, argp);
2587 #endif
2588 case APFS_IOC_SET_PFK:
2589 return apfs_ioc_set_pfk(file, argp);
2590 case APFS_IOC_GET_CLASS:
2591 return apfs_ioc_get_class(file, argp);
2592 case APFS_IOC_GET_PFK:
2593 return apfs_ioc_get_pfk(file, argp);
2594 default:
2595 return -ENOTTY;
2596 }
2597 }