ax-fs-ng 0.7.0

ArceOS filesystem module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use alloc::{
    borrow::ToOwned,
    format,
    string::{String, ToString},
    sync::Arc,
};
use core::any::Any;

use axfs_ng_vfs::{
    DeviceId, DirEntry, DirEntrySink, DirNode, DirNodeOps, FileNode, FileNodeOps, FilesystemOps,
    FsIoEvents, FsPollable, Metadata, MetadataUpdate, NodeFlags, NodeOps, NodePermission, NodeType,
    Reference, VfsError, VfsResult, WeakDirEntry,
};
use rsext4::{BLOCK_SIZE, bmalloc::InodeNumber};

use super::{
    Ext4Filesystem,
    util::{dir_entry_type_to_vfs, inode_to_vfs_type, into_vfs_err, vfs_type_to_dir_entry},
};
use crate::highlevel::forget_cached_file_key;

pub struct Inode {
    fs: Arc<Ext4Filesystem>,
    ino: InodeNumber,
    this: Option<WeakDirEntry>,
    path: Option<String>,
}

impl Inode {
    pub(crate) fn new(
        fs: Arc<Ext4Filesystem>,
        ino: InodeNumber,
        this: Option<WeakDirEntry>,
        path: Option<String>,
    ) -> Arc<Self> {
        Arc::new(Self {
            fs,
            ino,
            this,
            path,
        })
    }

    fn create_entry(
        &self,
        ino: InodeNumber,
        inode: &rsext4::disknode::Ext4Inode,
        name: impl Into<String>,
    ) -> DirEntry {
        let name = name.into();
        let reference = Reference::new(
            self.this.as_ref().and_then(WeakDirEntry::upgrade),
            name.clone(),
        );
        let path = self.dir_path().map(|dir| join_child_path(&dir, &name)).ok();
        if inode.is_dir() {
            DirEntry::new_dir(
                |this| DirNode::new(Inode::new(self.fs.clone(), ino, Some(this), path.clone())),
                reference,
            )
        } else {
            DirEntry::new_file(
                FileNode::new(Inode::new(self.fs.clone(), ino, None, path)),
                inode_to_vfs_type(inode.i_mode),
                reference,
            )
        }
    }

    fn dir_path(&self) -> VfsResult<String> {
        if let Some(this) = self.this.as_ref().and_then(WeakDirEntry::upgrade) {
            return Ok(this.absolute_path()?.to_string());
        }
        self.path.clone().ok_or(VfsError::InvalidInput)
    }

    fn lookup_locked(&self, name: &str) -> VfsResult<DirEntry> {
        let path = join_child_path(&self.dir_path()?, name);
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        let (ino, inode) = rsext4::dir::get_inode_with_num(fs, dev, &path)
            .map_err(into_vfs_err)?
            .ok_or(VfsError::NotFound)?;
        Ok(self.create_entry(ino, &inode, name))
    }

    fn update_ctime_with(
        fs: &mut rsext4::Ext4FileSystem,
        dev: &mut rsext4::Jbd2Dev<super::Ext4Disk>,
        ino: InodeNumber,
    ) -> VfsResult<()> {
        fs.modify_inode(dev, ino, |inode| {
            if cfg!(feature = "times") {
                inode.i_ctime = crate::os::wall_time().as_secs() as u32;
            }
        })
        .map_err(into_vfs_err)
    }
}

impl NodeOps for Inode {
    fn inode(&self) -> u64 {
        self.ino.as_u64()
    }

    fn metadata(&self) -> VfsResult<Metadata> {
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        let inode = fs.get_inode_by_num(dev, self.ino).map_err(into_vfs_err)?;
        let node_type = inode_to_vfs_type(inode.i_mode);
        Ok(Metadata {
            inode: self.ino.as_u64(),
            device: 0,
            nlink: inode.i_links_count as _,
            mode: NodePermission::from_bits_truncate(inode.permissions()),
            node_type,
            uid: inode.uid(),
            gid: inode.gid(),
            size: inode.size(),
            block_size: fs.superblock.block_size(),
            blocks: inode.blocks_count(),
            rdev: if matches!(node_type, NodeType::CharacterDevice | NodeType::BlockDevice) {
                decode_ext4_rdev(&inode.i_block)
            } else {
                DeviceId::default()
            },
            atime: core::time::Duration::from_secs(inode.i_atime as u64),
            mtime: core::time::Duration::from_secs(inode.i_mtime as u64),
            ctime: core::time::Duration::from_secs(inode.i_ctime as u64),
        })
    }

    fn update_metadata(&self, update: MetadataUpdate) -> VfsResult<()> {
        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            fs.modify_inode(dev, self.ino, |inode| {
                if let Some(mode) = update.mode {
                    inode.i_mode =
                        (inode.i_mode & rsext4::disknode::Ext4Inode::S_IFMT) | mode.bits();
                }
                if let Some((uid, gid)) = update.owner {
                    inode.i_uid = (uid & 0xffff) as u16;
                    inode.l_i_uid_high = ((uid >> 16) & 0xffff) as u16;
                    inode.i_gid = (gid & 0xffff) as u16;
                    inode.l_i_gid_high = ((gid >> 16) & 0xffff) as u16;
                }
                if let Some(rdev) = update.rdev {
                    let ty = inode_to_vfs_type(inode.i_mode);
                    if matches!(ty, NodeType::CharacterDevice | NodeType::BlockDevice) {
                        let (b0, b1) = encode_ext4_rdev(rdev);
                        inode.i_block[0] = b0;
                        inode.i_block[1] = b1;
                    }
                }
                if let Some(atime) = update.atime {
                    inode.i_atime = atime.as_secs() as u32;
                }
                if let Some(mtime) = update.mtime {
                    inode.i_mtime = mtime.as_secs() as u32;
                }
                if cfg!(feature = "times") {
                    inode.i_ctime = crate::os::wall_time().as_secs() as u32;
                }
            })
            .map_err(into_vfs_err)?;
        }
        self.fs.sync_to_disk()
    }

    fn len(&self) -> VfsResult<u64> {
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        fs.get_inode_by_num(dev, self.ino)
            .map(|inode| inode.size())
            .map_err(into_vfs_err)
    }

    fn filesystem(&self) -> &dyn FilesystemOps {
        &*self.fs
    }

    fn sync(&self, _data_only: bool) -> VfsResult<()> {
        self.fs.sync_to_disk()
    }

    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::BLOCKING
    }
}

impl FileNodeOps for Inode {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> VfsResult<usize> {
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        rsext4::read_inode_data_into(dev, fs, self.ino, offset, buf).map_err(into_vfs_err)
    }

    fn write_at(&self, buf: &[u8], offset: u64) -> VfsResult<usize> {
        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            // Use inode-number-based write to avoid path re-resolution.
            // Path-based write_file() fails with NotFound after rename/unlink,
            // which causes dirty page loss when jcode atomically replaces files.
            rsext4::write_inode_data(dev, fs, self.ino, offset, buf).map_err(into_vfs_err)?;
        }
        Ok(buf.len())
    }

    fn append(&self, buf: &[u8]) -> VfsResult<(usize, u64)> {
        let length = {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            let inode = fs.get_inode_by_num(dev, self.ino).map_err(into_vfs_err)?;
            let length = inode.size();
            rsext4::write_inode_data(dev, fs, self.ino, length, buf).map_err(into_vfs_err)?;
            length
        };
        Ok((buf.len(), length + buf.len() as u64))
    }

    fn set_len(&self, len: u64) -> VfsResult<()> {
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        rsext4::truncate(
            dev,
            fs,
            &self.path.clone().ok_or(VfsError::InvalidInput)?,
            len,
        )
        .map_err(into_vfs_err)
    }

    fn set_symlink(&self, target: &str) -> VfsResult<()> {
        let Some(_path) = self.path.clone() else {
            return Err(VfsError::InvalidInput);
        };

        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            let mut inode = fs.get_inode_by_num(dev, self.ino).map_err(into_vfs_err)?;

            if !inode.is_symlink() {
                return Err(VfsError::InvalidInput);
            }

            if let Ok(blocks) = rsext4::loopfile::resolve_inode_block_allextend(fs, dev, &mut inode)
            {
                for blk in blocks.values() {
                    let _ = fs.free_block(dev, *blk);
                }
            }

            let target_bytes = target.as_bytes();
            let target_len = target_bytes.len();
            inode.i_size_lo = (target_len as u64 & 0xffffffff) as u32;
            inode.i_size_high = ((target_len as u64) >> 32) as u32;
            inode.i_blocks_lo = 0;
            inode.l_i_blocks_high = 0;
            inode.i_block = [0; 15];

            if target_len == 0 {
                inode.i_flags &= !rsext4::disknode::Ext4Inode::EXT4_EXTENTS_FL;
            } else if target_len <= 60 {
                inode.i_flags &= !rsext4::disknode::Ext4Inode::EXT4_EXTENTS_FL;
                let mut raw = [0u8; 60];
                raw[..target_len].copy_from_slice(target_bytes);
                for i in 0..15 {
                    inode.i_block[i] = u32::from_le_bytes([
                        raw[i * 4],
                        raw[i * 4 + 1],
                        raw[i * 4 + 2],
                        raw[i * 4 + 3],
                    ]);
                }
            } else {
                if !fs.superblock.has_extents() {
                    return Err(VfsError::Unsupported);
                }

                let mut data_blocks = alloc::vec::Vec::new();
                let mut remaining = target_len;
                let mut src_off = 0usize;
                while remaining > 0 {
                    let blk = fs.alloc_block(dev).map_err(into_vfs_err)?;
                    let write_len = core::cmp::min(remaining, BLOCK_SIZE);
                    fs.datablock_cache
                        .modify_new(dev, blk, |data| {
                            for b in data.iter_mut() {
                                *b = 0;
                            }
                            let end = src_off + write_len;
                            data[..write_len].copy_from_slice(&target_bytes[src_off..end]);
                        })
                        .map_err(into_vfs_err)?;
                    data_blocks.push(blk);
                    remaining -= write_len;
                    src_off += write_len;
                }

                let used_datablocks = data_blocks.len() as u64;
                let iblocks_used = used_datablocks.saturating_mul(BLOCK_SIZE as u64 / 512) as u32;
                inode.i_blocks_lo = iblocks_used;
                inode.l_i_blocks_high = 0;
                rsext4::file::build_file_block_mapping_with_inode_num(
                    fs,
                    &mut inode,
                    self.ino,
                    &data_blocks,
                    dev,
                );
            }

            fs.modify_inode(dev, self.ino, |on_disk| {
                *on_disk = inode;
            })
            .map_err(into_vfs_err)?;
        }

        self.fs.sync_to_disk()
    }
}

impl FsPollable for Inode {
    fn poll(&self) -> FsIoEvents {
        FsIoEvents::IN | FsIoEvents::OUT
    }

    fn register(&self, _context: &mut core::task::Context<'_>, _events: FsIoEvents) {}
}

impl DirNodeOps for Inode {
    fn read_dir(&self, offset: u64, sink: &mut dyn DirEntrySink) -> VfsResult<usize> {
        let mut state = self.fs.lock();
        let (fs, dev) = state.split();
        let mut inode = fs.get_inode_by_num(dev, self.ino).map_err(into_vfs_err)?;

        let blocks = rsext4::loopfile::resolve_inode_block_allextend(fs, dev, &mut inode)
            .map_err(into_vfs_err)?;

        let mut byte_offset: u64 = 0;
        let mut count = 0usize;
        for &phys in blocks.values() {
            let cached = fs
                .datablock_cache
                .get_or_load(dev, phys)
                .map_err(into_vfs_err)?;
            let data = &cached.data[..BLOCK_SIZE];

            // Manually iterate entries, tracking byte_offset for ALL entries
            // (including inode==0 deleted ones) so offset stays physical.
            let mut pos = 0usize;
            while pos + 8 <= data.len() {
                let entry_inode =
                    u32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
                let rec_len = u16::from_le_bytes([data[pos + 4], data[pos + 5]]);
                if rec_len < 8 {
                    break;
                }
                let rec_usize = rec_len as usize;
                if pos + rec_usize > data.len() {
                    break;
                }

                let entry_offset = byte_offset;
                byte_offset += rec_len as u64;
                pos += rec_usize;

                if entry_inode == 0 {
                    continue;
                }
                if entry_offset < offset {
                    continue;
                }

                let name_len = data[pos - rec_usize + 6] as usize;
                let file_type = data[pos - rec_usize + 7];
                let name_start = pos - rec_usize + 8;
                if name_len > rec_usize - 8 {
                    continue;
                }
                let name = core::str::from_utf8(&data[name_start..name_start + name_len])
                    .map_err(|_| VfsError::InvalidData)?
                    .to_owned();
                let node_type = dir_entry_type_to_vfs(file_type);
                if !sink.accept(&name, entry_inode as u64, node_type, byte_offset) {
                    return Ok(count);
                }
                count += 1;
            }
        }

        Ok(count)
    }

    fn lookup(&self, name: &str) -> VfsResult<DirEntry> {
        if name == "." {
            return self
                .this
                .as_ref()
                .and_then(WeakDirEntry::upgrade)
                .ok_or(VfsError::NotFound);
        }
        if name == ".." {
            return self
                .this
                .as_ref()
                .and_then(WeakDirEntry::upgrade)
                .and_then(|entry| entry.parent())
                .ok_or(VfsError::NotFound);
        }
        self.lookup_locked(name)
    }

    fn create(
        &self,
        name: &str,
        node_type: NodeType,
        permission: NodePermission,
        uid: u32,
        gid: u32,
    ) -> VfsResult<DirEntry> {
        let Some(dir_path) = self.dir_path().ok() else {
            return Err(VfsError::InvalidInput);
        };
        let path = join_child_path(&dir_path, name);
        let ino = {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            if rsext4::dir::get_inode_with_num(fs, dev, &path)
                .map_err(into_vfs_err)?
                .is_some()
            {
                return Err(VfsError::AlreadyExists);
            }

            if node_type == NodeType::Directory {
                rsext4::mkdir_with_owner(dev, fs, &path, uid, gid).map_err(into_vfs_err)?;
            } else {
                let file_type = vfs_type_to_dir_entry(node_type).ok_or(VfsError::InvalidData)?;
                rsext4::mkfile_with_owner(dev, fs, &path, None, Some(file_type), uid, gid)
                    .map_err(into_vfs_err)?;
            };

            let (ino, _inode) = rsext4::dir::get_inode_with_num(fs, dev, &path)
                .map_err(into_vfs_err)?
                .ok_or(VfsError::NotFound)?;

            let mode_bits = permission.bits();
            fs.modify_inode(dev, ino, |node| {
                node.i_mode = (node.i_mode & rsext4::disknode::Ext4Inode::S_IFMT) | mode_bits;
            })
            .map_err(into_vfs_err)?;
            Self::update_ctime_with(fs, dev, ino)?;
            ino
        };

        self.fs.sync_to_disk()?;

        let reference = Reference::new(
            self.this.as_ref().and_then(WeakDirEntry::upgrade),
            name.to_owned(),
        );
        Ok(if node_type == NodeType::Directory {
            DirEntry::new_dir(
                |this| DirNode::new(Inode::new(self.fs.clone(), ino, Some(this), Some(path))),
                reference,
            )
        } else {
            DirEntry::new_file(
                FileNode::new(Inode::new(self.fs.clone(), ino, None, Some(path))),
                node_type,
                reference,
            )
        })
    }

    fn link(&self, name: &str, node: &DirEntry) -> VfsResult<DirEntry> {
        let dir_path = self.dir_path()?;
        let link_path = join_child_path(&dir_path, name);
        let target_path = node.absolute_path()?.to_string();
        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();

            if rsext4::dir::get_inode_with_num(fs, dev, &target_path)
                .map_err(into_vfs_err)?
                .is_none()
            {
                return Err(VfsError::NotFound);
            }
            if rsext4::dir::get_inode_with_num(fs, dev, &link_path)
                .map_err(into_vfs_err)?
                .is_some()
            {
                return Err(VfsError::AlreadyExists);
            }

            rsext4::link(fs, dev, &link_path, &target_path).map_err(into_vfs_err)?;
            let target_ino = InodeNumber::new(node.inode() as u32).map_err(into_vfs_err)?;
            Self::update_ctime_with(fs, dev, target_ino)?;
        }
        self.fs.sync_to_disk()?;
        self.lookup_locked(name)
    }

    fn unlink(&self, name: &str, is_dir: bool) -> VfsResult<()> {
        let dir_path = self.dir_path()?;
        let path = join_child_path(&dir_path, name);
        let mut forget_file_ino = None;
        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            let inode_info =
                rsext4::dir::get_inode_with_num(fs, dev, &path).map_err(into_vfs_err)?;
            if inode_info.is_none() {
                return Err(VfsError::NotFound);
            }
            let (ino, inode) = inode_info.unwrap();
            match (inode.is_dir(), is_dir) {
                (true, false) => return Err(VfsError::IsADirectory),
                (false, true) => return Err(VfsError::NotADirectory),
                _ => {}
            }
            if inode.is_dir() {
                let mut dir_inode = inode; // Ext4Inode is Copy
                if !rsext4::is_dir_empty(fs, dev, &mut dir_inode).map_err(into_vfs_err)? {
                    return Err(VfsError::DirectoryNotEmpty);
                }
                rsext4::delete_dir(fs, dev, &path).map_err(into_vfs_err)?;
            } else {
                if inode.i_links_count <= 1 {
                    forget_file_ino = Some(ino);
                }
                rsext4::unlink(fs, dev, &path).map_err(into_vfs_err)?;
            }
        }
        if let Some(ino) = forget_file_ino {
            forget_cached_file_key(&*self.fs, ino.as_u64());
        }
        self.fs.sync_to_disk()
    }

    fn rename(&self, src_name: &str, dst_dir: &DirNode, dst_name: &str) -> VfsResult<()> {
        let dst_dir: Arc<Self> = dst_dir.downcast().map_err(|_| VfsError::InvalidInput)?;
        let src_path = join_child_path(&self.dir_path()?, src_name);
        let dst_path = join_child_path(&dst_dir.dir_path()?, dst_name);
        let replaced_file_ino = {
            let mut state = dst_dir.fs.lock();
            let (fs, dev) = state.split();
            rsext4::dir::get_inode_with_num(fs, dev, &dst_path)
                .map_err(into_vfs_err)?
                .and_then(|(ino, inode)| {
                    (!inode.is_dir() && inode.i_links_count <= 1).then_some(ino)
                })
        };
        {
            let mut state = self.fs.lock();
            let (fs, dev) = state.split();
            rsext4::rename(dev, fs, &src_path, &dst_path).map_err(into_vfs_err)?;
        }
        if let Some(ino) = replaced_file_ino {
            forget_cached_file_key(&*self.fs, ino.as_u64());
        }
        self.fs.sync_to_disk()
    }
}

fn join_child_path(parent: &str, name: &str) -> String {
    if parent == "/" {
        format!("/{name}")
    } else {
        format!("{parent}/{name}")
    }
}

/// Bit widths for the ext4 *old* device-number format (16-bit, u16).
const EXT4_OLD_MAJOR_BITS: u32 = 8;
const EXT4_OLD_MINOR_BITS: u32 = 8;
const EXT4_OLD_MAJOR_MAX: u32 = (1 << EXT4_OLD_MAJOR_BITS) - 1; // 255
const EXT4_OLD_MINOR_MAX: u32 = (1 << EXT4_OLD_MINOR_BITS) - 1; // 255

/// Bit widths for the ext4 *new* device-number format (32-bit, u32).
/// Matches the Linux `new_encode_dev` / `new_decode_dev` layout.
const EXT4_NEW_MAJOR_BITS: u32 = 12;
const EXT4_NEW_MINOR_BITS: u32 = 20;
const EXT4_NEW_MAJOR_MASK: u32 = (1 << EXT4_NEW_MAJOR_BITS) - 1; // 0xFFF
const EXT4_NEW_MINOR_LOW_MASK: u32 = (1 << EXT4_OLD_MINOR_BITS) - 1; // 0xFF
const EXT4_NEW_MINOR_HIGH_MASK: u32 =
    ((1 << EXT4_NEW_MINOR_BITS) - 1) & !((1 << EXT4_OLD_MINOR_BITS) - 1); // 0xFFF00

/// Decodes an ext4 on-disk device number from `i_block[0..1]`.
///
/// ext4 uses two encoding formats:
/// - **Old** (u16): when major ≤ 255 && minor ≤ 255, stored in `i_block[0]` low 16 bits.
/// - **New** (u32): otherwise, `i_block[0] = 0` and `i_block[1] = new_encode_dev`.
fn decode_ext4_rdev(i_block: &[u32; 15]) -> DeviceId {
    if i_block[0] & 0xFFFF != 0 {
        // Old format: i_block[0] low 16 bits = old_encode_dev(dev)
        let v = i_block[0] as u16;
        let major = (v >> EXT4_OLD_MINOR_BITS) & (EXT4_OLD_MAJOR_MAX as u16);
        let minor = v & (EXT4_OLD_MINOR_MAX as u16);
        DeviceId::new(major as u32, minor as u32)
    } else {
        // New format: i_block[1] = new_encode_dev(dev)
        let v = i_block[1];
        let major = (v >> EXT4_OLD_MINOR_BITS) & EXT4_NEW_MAJOR_MASK;
        let minor =
            (v & EXT4_NEW_MINOR_LOW_MASK) | ((v >> EXT4_NEW_MAJOR_BITS) & EXT4_NEW_MINOR_HIGH_MASK);
        DeviceId::new(major, minor)
    }
}

/// Encodes a `DeviceId` into ext4 on-disk `i_block[0..1]` format.
///
/// Returns `(i_block[0], i_block[1])`.
fn encode_ext4_rdev(rdev: DeviceId) -> (u32, u32) {
    let major = rdev.major();
    let minor = rdev.minor();
    if major <= EXT4_OLD_MAJOR_MAX && minor <= EXT4_OLD_MINOR_MAX {
        // Old format: old_encode_dev
        ((major << EXT4_OLD_MINOR_BITS) | minor, 0)
    } else {
        // New format: new_encode_dev
        let encoded = (minor & EXT4_NEW_MINOR_LOW_MASK)
            | ((major & EXT4_NEW_MAJOR_MASK) << EXT4_OLD_MINOR_BITS)
            | ((minor & EXT4_NEW_MINOR_HIGH_MASK) << EXT4_NEW_MAJOR_BITS);
        (0, encoded)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Round-trip: encode a DeviceId to i_block, then decode it back.
    fn roundtrip(major: u32, minor: u32) -> (u32, u32) {
        let dev = DeviceId::new(major, minor);
        let (b0, b1) = encode_ext4_rdev(dev);
        let mut iblock = [0u32; 15];
        iblock[0] = b0;
        iblock[1] = b1;
        let back = decode_ext4_rdev(&iblock);
        (back.major(), back.minor())
    }

    #[test]
    fn rdev_old_format_small() {
        // (1, 3) — console, old format
        assert_eq!(roundtrip(1, 3), (1, 3));
    }

    #[test]
    fn rdev_old_format_boundary() {
        // Maximum old-format values
        assert_eq!(roundtrip(255, 255), (255, 255));
        assert_eq!(roundtrip(0, 0), (0, 0));
    }

    #[test]
    fn rdev_new_format_minor_exceeds_old() {
        // minor = 256 triggers new format
        assert_eq!(roundtrip(1, 256), (1, 256));
    }

    #[test]
    fn rdev_new_format_large_minor() {
        assert_eq!(roundtrip(1, 1040), (1, 1040));
        assert_eq!(roundtrip(8, 511), (8, 511));
    }

    #[test]
    fn rdev_old_on_disk_decodes_correctly() {
        // Simulate what Linux writes for (1,3) in old format:
        // i_block[0] = (1 << 8) | 3 = 259
        let mut iblock = [0u32; 15];
        iblock[0] = (1 << EXT4_OLD_MINOR_BITS) | 3;
        let dev = decode_ext4_rdev(&iblock);
        assert_eq!(dev.major(), 1);
        assert_eq!(dev.minor(), 3);
    }

    #[test]
    fn rdev_new_on_disk_decodes_correctly() {
        // Simulate what Linux writes for (1, 256) in new format:
        // new_encode_dev: (256 & 0xFF) | (1 << 8) | ((256 & 0xFFF00) << 12)
        // = 0 | 0x100 | (0x100 << 12) = 0x100 | 0x100000 = 0x100100
        let encoded = (256u32 & EXT4_NEW_MINOR_LOW_MASK)
            | ((1u32 & EXT4_NEW_MAJOR_MASK) << EXT4_OLD_MINOR_BITS)
            | ((256u32 & EXT4_NEW_MINOR_HIGH_MASK) << EXT4_NEW_MAJOR_BITS);
        let mut iblock = [0u32; 15];
        iblock[0] = 0;
        iblock[1] = encoded;
        let dev = decode_ext4_rdev(&iblock);
        assert_eq!(dev.major(), 1);
        assert_eq!(dev.minor(), 256);
    }
}