Debian Codegraph @

snapshot.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2022 Ernesto A. Fernández <[email protected]>
4 */
5
6 #include <linux/mount.h>
7 #include <linux/slab.h>
8 #include "apfs.h"
9
10 /**
11 * apfs_create_superblock_snapshot - Take a snapshot of the volume superblock
12 * @sb: superblock structure
13 * @bno: on return, the block number for the new superblock copy
14 *
15 * Returns 0 on success or a negative error code in case of failure.
16 */
17 static int apfs_create_superblock_snapshot(struct super_block *sb, u64 *bno)
18 {
19 struct apfs_sb_info *sbi = APFS_SB(sb);
20 struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
21 struct buffer_head *curr_bh = NULL;
22 struct buffer_head *snap_bh = NULL;
23 struct apfs_superblock *snap_raw = NULL;
24 int err;
25
26 err = apfs_spaceman_allocate_block(sb, bno, true /* backwards */);
27 if (err) {
28 apfs_err(sb, "block allocation failed");
29 goto fail;
30 }
31
32 snap_bh = apfs_getblk(sb, *bno);
33 if (!snap_bh) {
34 apfs_err(sb, "failed to map block for volume snap (0x%llx)", *bno);
35 err = -EIO;
36 goto fail;
37 }
38 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
39
40 curr_bh = sbi->s_vobject.o_bh;
41 memcpy(snap_bh->b_data, curr_bh->b_data, sb->s_blocksize);
42 curr_bh = NULL;
43
44 err = apfs_transaction_join(sb, snap_bh);
45 if (err)
46 goto fail;
47 set_buffer_csum(snap_bh);
48
49 snap_raw = (struct apfs_superblock *)snap_bh->b_data;
50 /* Volume superblocks in snapshots are physical objects */
51 snap_raw->apfs_o.o_oid = cpu_to_le64p(bno);
52 snap_raw->apfs_o.o_type = cpu_to_le32(APFS_OBJ_PHYSICAL | APFS_OBJECT_TYPE_FS);
53 /* The omap is shared with the current volume */
54 snap_raw->apfs_omap_oid = 0;
55 /* The extent reference tree is given by the snapshot metadata */
56 snap_raw->apfs_extentref_tree_oid = 0;
57 /* No snapshots inside snapshots */
58 snap_raw->apfs_snap_meta_tree_oid = 0;
59
60 err = 0;
61 fail:
62 snap_raw = NULL;
63 brelse(snap_bh);
64 snap_bh = NULL;
65 return err;
66 }
67
68 static int apfs_create_snap_metadata_rec(struct inode *mntpoint, struct apfs_node *snap_root, const char *name, int name_len, u64 sblock_oid)
69 {
70 struct super_block *sb = mntpoint->i_sb;
71 struct apfs_sb_info *sbi = APFS_SB(sb);
72 struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
73 struct apfs_query *query = NULL;
74 struct apfs_snap_metadata_key raw_key;
75 struct apfs_snap_metadata_val *raw_val = NULL;
76 int val_len;
77 struct timespec64 now;
78 u64 xid = APFS_NXI(sb)->nx_xid;
79 int err;
80
81 query = apfs_alloc_query(snap_root, NULL /* parent */);
82 if (!query) {
83 err = -ENOMEM;
84 goto fail;
85 }
86 apfs_init_snap_metadata_key(xid, &query->key);
87 query->flags |= APFS_QUERY_SNAP_META | APFS_QUERY_EXACT;
88
89 err = apfs_btree_query(sb, &query);
90 if (err == 0) {
91 apfs_err(sb, "record exists for xid 0x%llx", xid);
92 err = -EFSCORRUPTED;
93 goto fail;
94 }
95 if (err != -ENODATA) {
96 apfs_err(sb, "query failed for xid 0x%llx", xid);
97 goto fail;
98 }
99
100 apfs_key_set_hdr(APFS_TYPE_SNAP_METADATA, xid, &raw_key);
101
102 val_len = sizeof(*raw_val) + name_len + 1;
103 raw_val = kzalloc(val_len, GFP_KERNEL);
104 if (!raw_val) {
105 err = -ENOMEM;
106 goto fail;
107 }
108 raw_val->extentref_tree_oid = vsb_raw->apfs_extentref_tree_oid;
109 raw_val->sblock_oid = cpu_to_le64(sblock_oid);
110 now = current_time(mntpoint);
111 raw_val->create_time = cpu_to_le64(timespec64_to_ns(&now));
112 raw_val->change_time = raw_val->create_time;
113 raw_val->extentref_tree_type = vsb_raw->apfs_extentref_tree_type;
114 raw_val->flags = 0;
115 raw_val->name_len = cpu_to_le16(name_len + 1); /* Count the null byte */
116 strscpy(raw_val->name, name, name_len + 1);
117
118 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
119 raw_val->inum = vsb_raw->apfs_next_obj_id;
120 le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
121
122 err = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, val_len);
123 if (err)
124 apfs_err(sb, "insertion failed for xid 0x%llx", xid);
125 fail:
126 kfree(raw_val);
127 raw_val = NULL;
128 apfs_free_query(query);
129 query = NULL;
130 return err;
131 }
132
133 static int apfs_create_snap_name_rec(struct apfs_node *snap_root, const char *name, int name_len)
134 {
135 struct super_block *sb = snap_root->object.sb;
136 struct apfs_query *query = NULL;
137 struct apfs_snap_name_key *raw_key = NULL;
138 struct apfs_snap_name_val raw_val;
139 int key_len;
140 int err;
141
142 query = apfs_alloc_query(snap_root, NULL /* parent */);
143 if (!query) {
144 err = -ENOMEM;
145 goto fail;
146 }
147 apfs_init_snap_name_key(name, &query->key);
148 query->flags |= APFS_QUERY_SNAP_META | APFS_QUERY_EXACT;
149
150 err = apfs_btree_query(sb, &query);
151 if (err == 0) {
152 /* This should never happen here, the caller has checked */
153 apfs_alert(sb, "snapshot name record already exists");
154 err = -EFSCORRUPTED;
155 goto fail;
156 }
157 if (err != -ENODATA) {
158 apfs_err(sb, "query failed (%s)", name);
159 goto fail;
160 }
161
162 key_len = sizeof(*raw_key) + name_len + 1;
163 raw_key = kzalloc(key_len, GFP_KERNEL);
164 if (!raw_key) {
165 err = -ENOMEM;
166 goto fail;
167 }
168 apfs_key_set_hdr(APFS_TYPE_SNAP_NAME, APFS_SNAP_NAME_OBJ_ID, raw_key);
169 raw_key->name_len = cpu_to_le16(name_len + 1); /* Count the null byte */
170 strscpy(raw_key->name, name, name_len + 1);
171
172 raw_val.snap_xid = cpu_to_le64(APFS_NXI(sb)->nx_xid);
173
174 err = apfs_btree_insert(query, raw_key, key_len, &raw_val, sizeof(raw_val));
175 if (err)
176 apfs_err(sb, "insertion failed (%s)", name);
177 fail:
178 kfree(raw_key);
179 raw_key = NULL;
180 apfs_free_query(query);
181 query = NULL;
182 return err;
183 }
184
185 static int apfs_create_snap_meta_records(struct inode *mntpoint, const char *name, int name_len, u64 sblock_oid)
186 {
187 struct super_block *sb = mntpoint->i_sb;
188 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
189 struct apfs_node *snap_root = NULL;
190 int err;
191
192 snap_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid), APFS_OBJ_PHYSICAL, true /* write */);
193 if (IS_ERR(snap_root)) {
194 apfs_err(sb, "failed to read snap meta root 0x%llx", le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid));
195 return PTR_ERR(snap_root);
196 }
197 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
198 vsb_raw->apfs_snap_meta_tree_oid = cpu_to_le64(snap_root->object.oid);
199
200 err = apfs_create_snap_metadata_rec(mntpoint, snap_root, name, name_len, sblock_oid);
201 if (err) {
202 apfs_err(sb, "meta rec creation failed");
203 goto fail;
204 }
205 err = apfs_create_snap_name_rec(snap_root, name, name_len);
206 if (err)
207 apfs_err(sb, "name rec creation failed");
208
209 fail:
210 apfs_node_free(snap_root);
211 return err;
212 }
213
214 static int apfs_create_new_extentref_tree(struct super_block *sb)
215 {
216 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
217 u64 oid;
218 int err;
219
220 err = apfs_make_empty_btree_root(sb, APFS_OBJECT_TYPE_BLOCKREFTREE, &oid);
221 if (err) {
222 apfs_err(sb, "failed to make empty root");
223 return err;
224 }
225
226 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
227 vsb_raw->apfs_extentref_tree_oid = cpu_to_le64(oid);
228 return 0;
229 }
230
231 /**
232 * apfs_update_omap_snap_tree - Add the current xid to the omap's snapshot tree
233 * @sb: filesystem superblock
234 * @oid_p: pointer to the on-disk block number for the root node
235 *
236 * Returns 0 on success, or a negative error code in case of failure.
237 */
238 static int apfs_update_omap_snap_tree(struct super_block *sb, __le64 *oid_p)
239 {
240 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
241 struct apfs_node *root = NULL;
242 u64 oid = le64_to_cpup(oid_p);
243 struct apfs_query *query = NULL;
244 __le64 raw_key;
245 struct apfs_omap_snapshot raw_val = {0};
246 int err;
247
248 /* An empty snapshots tree may not even have a root yet */
249 if (!oid) {
250 err = apfs_make_empty_btree_root(sb, APFS_OBJECT_TYPE_OMAP_SNAPSHOT, &oid);
251 if (err) {
252 apfs_err(sb, "failed to make empty root");
253 return err;
254 }
255 }
256
257 root = apfs_read_node(sb, oid, APFS_OBJ_PHYSICAL, true /* write */);
258 if (IS_ERR(root)) {
259 apfs_err(sb, "failed to read omap snap root 0x%llx", oid);
260 return PTR_ERR(root);
261 }
262 oid = 0;
263
264 query = apfs_alloc_query(root, NULL /* parent */);
265 if (!query) {
266 err = -ENOMEM;
267 goto fail;
268 }
269 apfs_init_omap_snap_key(nxi->nx_xid, &query->key);
270 query->flags = APFS_QUERY_OMAP_SNAP | APFS_QUERY_EXACT;
271
272 err = apfs_btree_query(sb, &query);
273 if (err == 0) {
274 apfs_err(sb, "record exists for xid 0x%llx", nxi->nx_xid);
275 err = -EFSCORRUPTED;
276 goto fail;
277 }
278 if (err != -ENODATA) {
279 apfs_err(sb, "query failed for xid 0x%llx", nxi->nx_xid);
280 goto fail;
281 }
282
283 raw_key = cpu_to_le64(nxi->nx_xid);
284 err = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
285 if (err)
286 apfs_err(sb, "insertion failed for xid 0x%llx", nxi->nx_xid);
287 *oid_p = cpu_to_le64(root->object.block_nr);
288
289 fail:
290 apfs_free_query(query);
291 query = NULL;
292 apfs_node_free(root);
293 root = NULL;
294 return err;
295 }
296
297 /**
298 * apfs_update_omap_snapshots - Add the current xid to the omap's snapshots
299 * @sb: filesystem superblock
300 *
301 * Returns 0 on success, or a negative error code in case of failure.
302 */
303 static int apfs_update_omap_snapshots(struct super_block *sb)
304 {
305 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
306 struct buffer_head *bh = NULL;
307 struct apfs_omap_phys *omap = NULL;
308 u64 omap_blk;
309 u64 xid;
310 int err;
311
312 xid = APFS_NXI(sb)->nx_xid;
313
314 omap_blk = le64_to_cpu(vsb_raw->apfs_omap_oid);
315 bh = apfs_read_object_block(sb, omap_blk, true /* write */, false /* preserve */);
316 if (IS_ERR(bh)) {
317 apfs_err(sb, "CoW failed for bno 0x%llx", omap_blk);
318 return PTR_ERR(bh);
319 }
320 omap = (struct apfs_omap_phys *)bh->b_data;
321
322 apfs_assert_in_transaction(sb, &omap->om_o);
323 le32_add_cpu(&omap->om_snap_count, 1);
324 omap->om_most_recent_snap = cpu_to_le64(xid);
325 err = apfs_update_omap_snap_tree(sb, &omap->om_snapshot_tree_oid);
326 if (err)
327 apfs_err(sb, "omap snap tree update failed");
328
329 omap = NULL;
330 brelse(bh);
331 bh = NULL;
332 return err;
333 }
334
335 /**
336 * apfs_snapshot_name_check - Check if a snapshot with the given name exists
337 * @sb: filesystem superblock
338 * @name: name to check
339 * @name_len: length of @name
340 * @eexist: on return, whether the name exists or not
341 *
342 * Returns 0 on success, or a negative error code in case of failure.
343 */
344 static int apfs_snapshot_name_check(struct super_block *sb, const char *name, int name_len, bool *eexist)
345 {
346 struct apfs_sb_info *sbi = APFS_SB(sb);
347 struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
348 struct apfs_node *snap_root = NULL;
349 struct apfs_query *query = NULL;
350 int err;
351
352 snap_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid), APFS_OBJ_PHYSICAL, false /* write */);
353 if (IS_ERR(snap_root)) {
354 apfs_err(sb, "failed to read snap meta root 0x%llx", le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid));
355 return PTR_ERR(snap_root);
356 }
357 vsb_raw = NULL;
358
359 query = apfs_alloc_query(snap_root, NULL /* parent */);
360 if (!query) {
361 err = -ENOMEM;
362 goto out;
363 }
364 apfs_init_snap_name_key(name, &query->key);
365 query->flags |= APFS_QUERY_SNAP_META | APFS_QUERY_EXACT;
366
367 err = apfs_btree_query(sb, &query);
368 if (err == 0) {
369 *eexist = true;
370 } else if (err == -ENODATA) {
371 *eexist = false;
372 err = 0;
373 } else {
374 apfs_err(sb, "query failed (%s)", name);
375 }
376
377 out:
378 apfs_free_query(query);
379 query = NULL;
380 apfs_node_free(snap_root);
381 snap_root = NULL;
382 return err;
383 }
384
385 /**
386 * apfs_do_ioc_takesnapshot - Actual work for apfs_ioc_take_snapshot()
387 * @mntpoint: inode of the mount point to snapshot
388 * @name: label for the snapshot
389 *
390 * Returns 0 on success, or a negative error code in case of failure.
391 */
392 static int apfs_do_ioc_take_snapshot(struct inode *mntpoint, const char *name, int name_len)
393 {
394 struct super_block *sb = mntpoint->i_sb;
395 struct apfs_sb_info *sbi = APFS_SB(sb);
396 struct apfs_superblock *vsb_raw = NULL;
397 struct apfs_omap *omap = sbi->s_omap;
398 u64 sblock_oid;
399 bool eexist = false;
400 int err;
401
402 err = apfs_transaction_start(sb, APFS_TRANS_REG);
403 if (err)
404 return err;
405
406 /*
407 * This check can fail in normal operation, so run it before making any
408 * changes and exit the transaction cleanly if necessary. The name
409 * lookup will have to be repeated later, but it's ok because I don't
410 * expect snapshot creation to be a bottleneck for anyone.
411 */
412 err = apfs_snapshot_name_check(sb, name, name_len, &eexist);
413 if (err) {
414 apfs_err(sb, "snapshot name existence check failed");
415 goto fail;
416 }
417 if (eexist) {
418 err = apfs_transaction_commit(sb);
419 if (err)
420 goto fail;
421 apfs_warn(sb, "snapshot name already in use (%s)", name);
422 return -EEXIST;
423 }
424
425 /*
426 * Flush the extent caches to the extenref tree before it gets moved to
427 * the snapshot. It seems safer in general to avoid big unpredictable
428 * changes to the layout after the snapshot is set up.
429 */
430 err = apfs_transaction_flush_all_inodes(sb);
431 if (err) {
432 apfs_err(sb, "failed to flush all inodes");
433 goto fail;
434 }
435
436 err = apfs_create_superblock_snapshot(sb, &sblock_oid);
437 if (err) {
438 apfs_err(sb, "failed to snapshot superblock");
439 goto fail;
440 }
441
442 err = apfs_create_snap_meta_records(mntpoint, name, name_len, sblock_oid);
443 if (err) {
444 apfs_err(sb, "failed to create snap meta records");
445 goto fail;
446 }
447
448 err = apfs_create_new_extentref_tree(sb);
449 if (err) {
450 apfs_err(sb, "failed to create new extref tree");
451 goto fail;
452 }
453
454 err = apfs_update_omap_snapshots(sb);
455 if (err) {
456 apfs_err(sb, "failed to update omap snapshots");
457 goto fail;
458 }
459
460 /*
461 * The official reference allows old implementations to ignore extended
462 * snapshot metadata, so I don't see any reason why we can't do the
463 * same for now.
464 */
465
466 vsb_raw = sbi->s_vsb_raw;
467 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
468 le64_add_cpu(&vsb_raw->apfs_num_snapshots, 1);
469
470 omap->omap_latest_snap = APFS_NXI(sb)->nx_xid;
471
472 sbi->s_nxi->nx_transaction.t_state |= APFS_NX_TRANS_FORCE_COMMIT;
473 err = apfs_transaction_commit(sb);
474 if (err)
475 goto fail;
476 return 0;
477
478 fail:
479 apfs_transaction_abort(sb);
480 return err;
481 }
482
483 /**
484 * apfs_ioc_take_snapshot - Ioctl handler for APFS_IOC_CREATE_SNAPSHOT
485 * @file: affected file
486 * @arg: ioctl argument
487 *
488 * Returns 0 on success, or a negative error code in case of failure.
489 */
490 int apfs_ioc_take_snapshot(struct file *file, void __user *user_arg)
491 {
492 struct inode *inode = file_inode(file);
493 struct super_block *sb = inode->i_sb;
494 struct apfs_ioctl_snap_name *arg = NULL;
495 size_t name_len;
496 int err;
497
498 if (apfs_ino(inode) != APFS_ROOT_DIR_INO_NUM) {
499 apfs_info(sb, "snapshot must be requested on mountpoint");
500 return -ENOTTY;
501 }
502
503 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
504 if (!inode_owner_or_capable(inode))
505 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
506 if (!inode_owner_or_capable(&init_user_ns, inode))
507 #else
508 if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
509 #endif
510 return -EPERM;
511
512 err = mnt_want_write_file(file);
513 if (err)
514 return err;
515
516 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
517 if (!arg) {
518 err = -ENOMEM;
519 goto fail;
520 }
521
522 if (copy_from_user(arg, user_arg, sizeof(*arg))) {
523 err = -EFAULT;
524 goto fail;
525 }
526
527 name_len = strnlen(arg->name, sizeof(arg->name));
528 if (name_len == sizeof(arg->name)) {
529 apfs_warn(sb, "snapshot name is too long (%d)", (int)name_len);
530 err = -EINVAL;
531 goto fail;
532 }
533
534 err = apfs_do_ioc_take_snapshot(inode, arg->name, name_len);
535 fail:
536 kfree(arg);
537 arg = NULL;
538 mnt_drop_write_file(file);
539 return err;
540 }
541
542 static int apfs_snap_xid_from_query(struct apfs_query *query, u64 *xid)
543 {
544 char *raw = query->node->object.data;
545 __le64 *val = NULL;
546
547 if (query->len != sizeof(*val)) {
548 apfs_err(query->node->object.sb, "bad value length (%d)", query->len);
549 return -EFSCORRUPTED;
550 }
551 val = (__le64 *)(raw + query->off);
552
553 *xid = le64_to_cpup(val);
554 return 0;
555 }
556
557 static int apfs_snapshot_name_to_xid(struct apfs_node *snap_root, const char *name, u64 *xid)
558 {
559 struct super_block *sb = snap_root->object.sb;
560 struct apfs_query *query = NULL;
561 int err;
562
563 query = apfs_alloc_query(snap_root, NULL /* parent */);
564 if (!query)
565 return -ENOMEM;
566 apfs_init_snap_name_key(name, &query->key);
567 query->flags |= APFS_QUERY_SNAP_META | APFS_QUERY_EXACT;
568
569 err = apfs_btree_query(sb, &query);
570 if (err) {
571 if (err != -ENODATA)
572 apfs_err(sb, "query failed (%s)", name);
573 goto fail;
574 }
575
576 err = apfs_snap_xid_from_query(query, xid);
577 if (err)
578 apfs_err(sb, "bad snap name record (%s)", name);
579 fail:
580 apfs_free_query(query);
581 query = NULL;
582 return err;
583 }
584
585 static int apfs_snap_sblock_from_query(struct apfs_query *query, u64 *sblock_oid)
586 {
587 struct super_block *sb = query->node->object.sb;
588 char *raw = query->node->object.data;
589 struct apfs_snap_metadata_val *val = NULL;
590
591 if (query->len < sizeof(*val)) {
592 apfs_err(sb, "bad value length (%d)", query->len);
593 return -EFSCORRUPTED;
594 }
595 val = (struct apfs_snap_metadata_val *)(raw + query->off);
596
597 /* I don't know anything about these snapshots, can they be mounted? */
598 if (le32_to_cpu(val->flags) & APFS_SNAP_META_PENDING_DATALESS) {
599 apfs_warn(sb, "the requested snapshot is dataless");
600 return -EOPNOTSUPP;
601 }
602
603 *sblock_oid = le64_to_cpu(val->sblock_oid);
604 return 0;
605 }
606
607 static int apfs_snapshot_xid_to_sblock(struct apfs_node *snap_root, u64 xid, u64 *sblock_oid)
608 {
609 struct super_block *sb = snap_root->object.sb;
610 struct apfs_query *query = NULL;
611 int err;
612
613 query = apfs_alloc_query(snap_root, NULL /* parent */);
614 if (!query)
615 return -ENOMEM;
616 apfs_init_snap_metadata_key(xid, &query->key);
617 query->flags |= APFS_QUERY_SNAP_META | APFS_QUERY_EXACT;
618
619 err = apfs_btree_query(sb, &query);
620 if (err) {
621 apfs_err(sb, "query failed for xid 0x%llx", xid);
622 goto fail;
623 }
624
625 err = apfs_snap_sblock_from_query(query, sblock_oid);
626 if (err)
627 apfs_err(sb, "bad snap meta record for xid 0x%llx", xid);
628 fail:
629 apfs_free_query(query);
630 query = NULL;
631 return err;
632 }
633
634 /**
635 * apfs_switch_to_snapshot - Start working with the snapshot volume superblock
636 * @sb: superblock structure
637 *
638 * Maps the volume superblock from the snapshot specified in the mount options.
639 * Returns 0 on success or a negative error code in case of failure.
640 */
641 int apfs_switch_to_snapshot(struct super_block *sb)
642 {
643 struct apfs_sb_info *sbi = APFS_SB(sb);
644 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
645 struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
646 struct apfs_node *snap_root = NULL;
647 const char *name = NULL;
648 u64 sblock_oid = 0;
649 u64 xid = 0;
650 int err;
651
652 ASSERT(sb->s_flags & SB_RDONLY);
653
654 name = sbi->s_snap_name;
655 if (strlen(name) > APFS_SNAP_MAX_NAMELEN) {
656 apfs_warn(sb, "snapshot name is too long");
657 return -EINVAL;
658 }
659
660 snap_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid), APFS_OBJ_PHYSICAL, false /* write */);
661 if (IS_ERR(snap_root)) {
662 apfs_err(sb, "failed to read snap meta root 0x%llx", le64_to_cpu(vsb_raw->apfs_snap_meta_tree_oid));
663 return PTR_ERR(snap_root);
664 }
665 vsb_raw = NULL;
666
667 err = apfs_snapshot_name_to_xid(snap_root, name, &xid);
668 if (err) {
669 if (err == -ENODATA)
670 apfs_info(sb, "no snapshot under that name (%s)", name);
671 goto fail;
672 }
673 sbi->s_snap_xid = xid;
674
675 err = apfs_snapshot_xid_to_sblock(snap_root, xid, &sblock_oid);
676 if (err)
677 goto fail;
678
679 apfs_unmap_volume_super(sb);
680 err = apfs_map_volume_super_bno(sb, sblock_oid, nxi->nx_flags & APFS_CHECK_NODES);
681 if (err)
682 apfs_err(sb, "failed to map volume block 0x%llx", sblock_oid);
683
684 fail:
685 apfs_node_free(snap_root);
686 return err;
687 }