ostrya 0.2.8

Pure-Rust async reimplementation of the ostree repository library
Documentation
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
//! The repository lock: cross-process exclusion plus in-process coordination.
//!
//! ostree guards a repository with an advisory lock on `<repo>/.lock`. This
//! module reproduces that with a classic `fcntl` record lock (`F_SETLK`, via
//! [`rustix::fs::fcntl_lock`]), which shares a lock space with the OFD locks the
//! `ostree` tool takes, so the library and the tool exclude each other on the
//! same repository.
//!
//! `F_SETLK` locks are process-associated: two descriptors in one process do
//! not conflict, and closing any one descriptor to the file drops every lock
//! the process holds on it. Both hazards are avoided by keeping exactly one
//! `.lock` descriptor per repository per process. A process-global registry
//! keyed by the lock file's `(device, inode)` hands every repository handle to
//! one underlying repository -- clones and independent opens alike -- the same
//! [`RepoLock`], so a single descriptor and a shared reference count mediate all
//! in-process holders. The reference count lets several shared holders share
//! one descriptor lock, touching the descriptor only at the transitions that
//! change the effective lock.
//!
//! A shared acquire and an exclusive acquire exclude each other inside the
//! process: an exclusive acquire waits while any shared holder stands, and a
//! shared acquire waits while an exclusive holder stands. An exclusive acquire
//! is not re-entrant, so a second one waits for the first to release. A
//! destructive run therefore excludes this process's own transactions for the
//! whole of the run.
//!
//! Cross-process contention is resolved by a non-blocking attempt followed by an
//! [`ostrya_rt::Timer`] retry loop bounded by `lock-timeout-secs`, matching the
//! tool's retry-until-timeout behavior.

use std::collections::HashMap;
use std::os::fd::{BorrowedFd, OwnedFd};
use std::sync::{Arc, Mutex, OnceLock, Weak};
use std::time::{Duration, Instant};

use ostrya_core::RepoMode;
use rustix::fs::{AtFlags, FlockOperation, Mode, OFlags};
use rustix::io::Errno;

use crate::error::{Error, Result};
use crate::perm;

/// The repository lock file, relative to the repository root.
const LOCK_FILE: &str = ".lock";

/// The mode a created `.lock` file is requested with, matching the tool. The
/// process umask reduces it. In a `bare-user-shared` repository an `fchmod`
/// after the create restores [`perm::SHARED_LOCK_MODE`].
const LOCK_MODE: u32 = 0o660;

/// The delay between lock-acquisition attempts while contended.
const POLL_INTERVAL: Duration = Duration::from_millis(100);

/// Whether a transaction takes the repository lock shared or exclusive.
///
/// A writing transaction takes it [`Shared`](LockKind::Shared): many commits
/// proceed at once, matching the read lock the tool holds during a commit.
/// Destructive maintenance takes it [`Exclusive`](LockKind::Exclusive).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockKind {
    /// A shared (read) lock.
    Shared,
    /// An exclusive (write) lock.
    Exclusive,
}

/// The effective lock currently applied to the `.lock` descriptor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OsLock {
    Unlocked,
    Shared,
    Exclusive,
}

/// In-process hold counts and the effective descriptor lock.
#[derive(Debug)]
struct LockState {
    shared: usize,
    exclusive: usize,
    os: OsLock,
}

/// The outcome of one non-blocking acquisition attempt.
enum TryOutcome {
    Acquired,
    WouldBlock,
}

/// The per-repository lock, shared by every handle to one repository in a
/// process.
#[derive(Debug)]
pub(crate) struct RepoLock {
    key: (u64, u64),
    fd: OwnedFd,
    state: Mutex<LockState>,
}

/// The process-global registry mapping a lock file's `(device, inode)` to its
/// live [`RepoLock`].
type LockRegistry = HashMap<(u64, u64), Weak<RepoLock>>;

fn registry() -> &'static Mutex<LockRegistry> {
    static REGISTRY: OnceLock<Mutex<LockRegistry>> = OnceLock::new();
    REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}

impl RepoLock {
    /// Return the [`RepoLock`] for the repository rooted at `repo_fd`, creating
    /// `<repo>/.lock` and registering it on first use. Runs synchronous
    /// filesystem calls and is meant to be offloaded to the blocking pool.
    ///
    /// A `.lock` this call creates in a `bare-user-shared` repository is forced
    /// to [`perm::SHARED_LOCK_MODE`], so every member of the repository group
    /// opens it `O_RDWR` and takes the lock. The create attempt therefore
    /// carries `O_EXCL`, which separates the arm that made the file from the
    /// arm that found one: a `.lock` another member owns keeps the mode it has.
    pub(crate) fn get_or_create(
        repo_fd: BorrowedFd<'_>,
        repo_mode: RepoMode,
    ) -> std::io::Result<Arc<RepoLock>> {
        let mut reg = registry().lock().unwrap();

        // Probe an existing entry without opening a second descriptor: opening
        // and closing another descriptor to `.lock` would drop the live lock.
        if let Ok(stat) = rustix::fs::statat(repo_fd, LOCK_FILE, AtFlags::empty()) {
            let key = (stat.st_dev, stat.st_ino);
            if let Some(existing) = reg.get(&key).and_then(Weak::upgrade) {
                return Ok(existing);
            }
        }

        let fd = match rustix::fs::openat(
            repo_fd,
            LOCK_FILE,
            OFlags::RDWR | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC,
            Mode::from_raw_mode(LOCK_MODE),
        ) {
            Ok(fd) => {
                perm::force_created_mode(&fd, repo_mode, perm::SHARED_LOCK_MODE)?;
                fd
            }
            Err(Errno::EXIST) => rustix::fs::openat(
                repo_fd,
                LOCK_FILE,
                OFlags::RDWR | OFlags::CLOEXEC,
                Mode::empty(),
            )?,
            Err(e) => return Err(e.into()),
        };
        let stat = rustix::fs::fstat(&fd)?;
        let key = (stat.st_dev, stat.st_ino);

        let lock = Arc::new(RepoLock {
            key,
            fd,
            state: Mutex::new(LockState {
                shared: 0,
                exclusive: 0,
                os: OsLock::Unlocked,
            }),
        });
        reg.insert(key, Arc::downgrade(&lock));
        Ok(lock)
    }

    /// Add one holder of `kind` without blocking.
    ///
    /// A record lock is process-associated, so the descriptor alone cannot hold
    /// one in-process holder off another. The hold counts do that: an acquire
    /// that the rule excludes reports [`TryOutcome::WouldBlock`], which sends
    /// the caller into the retry loop, and it raises no count.
    fn try_acquire(&self, kind: LockKind) -> std::io::Result<TryOutcome> {
        let mut st = self.state.lock().unwrap();
        match kind {
            LockKind::Shared => {
                // An exclusive holder excludes every other holder in this
                // process.
                if st.exclusive > 0 {
                    return Ok(TryOutcome::WouldBlock);
                }
                if st.os == OsLock::Shared {
                    st.shared += 1;
                    return Ok(TryOutcome::Acquired);
                }
                match rustix::fs::fcntl_lock(&self.fd, FlockOperation::NonBlockingLockShared) {
                    Ok(()) => {
                        st.os = OsLock::Shared;
                        st.shared += 1;
                        Ok(TryOutcome::Acquired)
                    }
                    Err(e) if would_block(e) => Ok(TryOutcome::WouldBlock),
                    Err(e) => Err(e.into()),
                }
            }
            LockKind::Exclusive => {
                // An exclusive acquire waits for every other holder, an
                // exclusive one included: the hold is not re-entrant, so two
                // callers never share it.
                if st.shared > 0 || st.exclusive > 0 {
                    return Ok(TryOutcome::WouldBlock);
                }
                match rustix::fs::fcntl_lock(&self.fd, FlockOperation::NonBlockingLockExclusive) {
                    Ok(()) => {
                        st.os = OsLock::Exclusive;
                        st.exclusive += 1;
                        Ok(TryOutcome::Acquired)
                    }
                    Err(e) if would_block(e) => Ok(TryOutcome::WouldBlock),
                    Err(e) => Err(e.into()),
                }
            }
        }
    }

    /// Drop one holder of `kind`, releasing the descriptor lock when the last
    /// holder goes away.
    fn release(&self, kind: LockKind) {
        let mut st = self.state.lock().unwrap();
        match kind {
            LockKind::Shared => st.shared = st.shared.saturating_sub(1),
            LockKind::Exclusive => st.exclusive = st.exclusive.saturating_sub(1),
        }
        let target = if st.exclusive > 0 {
            OsLock::Exclusive
        } else if st.shared > 0 {
            OsLock::Shared
        } else {
            OsLock::Unlocked
        };
        if target == st.os {
            return;
        }
        // A shared holder and an exclusive holder exclude each other, so one of
        // the two counts is zero and the target reached here is `Unlocked`.
        // Errors are ignored so a release (including Drop) never fails.
        let _ = rustix::fs::fcntl_lock(&self.fd, FlockOperation::Unlock);
        st.os = target;
    }
}

impl Drop for RepoLock {
    fn drop(&mut self) {
        // Remove our now-dead registry entry, unless it was already replaced by
        // a newer lock over the same inode.
        if let Ok(mut reg) = registry().lock()
            && let Some(weak) = reg.get(&self.key)
            && weak.strong_count() == 0
        {
            reg.remove(&self.key);
        }
        // Closing `self.fd` releases any residual record lock.
    }
}

/// Whether a lock error means the lock is held elsewhere.
fn would_block(e: Errno) -> bool {
    e == Errno::AGAIN || e == Errno::ACCESS
}

/// An acquired lock hold. Releasing happens on drop.
#[derive(Debug)]
pub(crate) struct LockGuard {
    hold: Option<(Arc<RepoLock>, LockKind)>,
}

impl LockGuard {
    /// A guard that holds no lock, for a repository with locking disabled.
    pub(crate) fn disabled() -> LockGuard {
        LockGuard { hold: None }
    }
}

impl Drop for LockGuard {
    fn drop(&mut self) {
        if let Some((lock, kind)) = &self.hold {
            lock.release(*kind);
        }
    }
}

/// Acquire `kind` on `lock`, retrying until `timeout` elapses.
pub(crate) async fn acquire(
    lock: Arc<RepoLock>,
    kind: LockKind,
    timeout: Duration,
) -> Result<LockGuard> {
    let deadline = Instant::now() + timeout;
    loop {
        let probe = lock.clone();
        match ostrya_rt::unblock(move || probe.try_acquire(kind)).await? {
            TryOutcome::Acquired => {
                return Ok(LockGuard {
                    hold: Some((lock, kind)),
                });
            }
            TryOutcome::WouldBlock => {
                let now = Instant::now();
                if now >= deadline {
                    return Err(Error::LockTimeout {
                        secs: timeout.as_secs() as i64,
                    });
                }
                let wait = POLL_INTERVAL.min(deadline - now);
                ostrya_rt::Timer::after(wait).await;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::fd::AsFd;

    /// A repository root fd over a throwaway directory, for lock unit tests.
    struct Scratch {
        _dir: std::path::PathBuf,
        fd: OwnedFd,
    }

    impl Scratch {
        fn new(tag: &str) -> Scratch {
            use std::sync::atomic::{AtomicU64, Ordering};
            static COUNTER: AtomicU64 = AtomicU64::new(0);
            let n = COUNTER.fetch_add(1, Ordering::Relaxed);
            let dir =
                std::env::temp_dir().join(format!("ostrya-lock-{}-{tag}-{n}", std::process::id()));
            let _ = std::fs::remove_dir_all(&dir);
            std::fs::create_dir_all(&dir).unwrap();
            let fd = rustix::fs::openat(
                rustix::fs::CWD,
                &dir,
                OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
                Mode::empty(),
            )
            .unwrap();
            Scratch { _dir: dir, fd }
        }

        fn repo_fd(&self) -> BorrowedFd<'_> {
            self.fd.as_fd()
        }
    }

    impl Drop for Scratch {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self._dir);
        }
    }

    fn os_lock(lock: &RepoLock) -> OsLock {
        lock.state.lock().unwrap().os
    }

    /// The in-process hold counts, as `(shared, exclusive)`.
    fn counts(lock: &RepoLock) -> (usize, usize) {
        let st = lock.state.lock().unwrap();
        (st.shared, st.exclusive)
    }

    #[test]
    fn shared_holders_share_one_descriptor_lock() {
        let scratch = Scratch::new("shared");
        let lock = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();

        assert!(matches!(
            lock.try_acquire(LockKind::Shared).unwrap(),
            TryOutcome::Acquired
        ));
        assert!(matches!(
            lock.try_acquire(LockKind::Shared).unwrap(),
            TryOutcome::Acquired
        ));
        assert_eq!(os_lock(&lock), OsLock::Shared);

        lock.release(LockKind::Shared);
        assert_eq!(os_lock(&lock), OsLock::Shared); // one holder remains
        lock.release(LockKind::Shared);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
    }

    #[test]
    fn exclusive_waits_for_a_shared_holder() {
        let scratch = Scratch::new("wait");
        let lock = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();

        lock.try_acquire(LockKind::Shared).unwrap();
        assert_eq!(os_lock(&lock), OsLock::Shared);

        // The exclusive attempt reports `WouldBlock` while the shared holder
        // stands, and the effective lock stays shared.
        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::WouldBlock
        ));
        assert_eq!(os_lock(&lock), OsLock::Shared);
        assert_eq!(counts(&lock), (1, 0));

        // Once the shared holder releases, the attempt succeeds.
        lock.release(LockKind::Shared);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::Acquired
        ));
        assert_eq!(os_lock(&lock), OsLock::Exclusive);

        lock.release(LockKind::Exclusive);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
    }

    #[test]
    fn shared_waits_for_an_exclusive_holder() {
        let scratch = Scratch::new("shared-waits");
        let lock = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();

        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::Acquired
        ));
        assert_eq!(os_lock(&lock), OsLock::Exclusive);

        // The shared attempt reports `WouldBlock` while the exclusive holder
        // stands, the effective lock stays exclusive, and no count rises.
        assert!(matches!(
            lock.try_acquire(LockKind::Shared).unwrap(),
            TryOutcome::WouldBlock
        ));
        assert_eq!(os_lock(&lock), OsLock::Exclusive);
        assert_eq!(counts(&lock), (0, 1));

        // Once the exclusive holder releases, the attempt succeeds.
        lock.release(LockKind::Exclusive);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
        assert_eq!(counts(&lock), (0, 0));
        assert!(matches!(
            lock.try_acquire(LockKind::Shared).unwrap(),
            TryOutcome::Acquired
        ));
        assert_eq!(os_lock(&lock), OsLock::Shared);
        assert_eq!(counts(&lock), (1, 0));

        lock.release(LockKind::Shared);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
    }

    #[test]
    fn a_second_exclusive_waits_for_the_first() {
        let scratch = Scratch::new("exclusive-waits");
        let lock = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();

        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::Acquired
        ));

        // The hold is not re-entrant: a second exclusive attempt waits, and the
        // count stays at the one holder.
        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::WouldBlock
        ));
        assert_eq!(os_lock(&lock), OsLock::Exclusive);
        assert_eq!(counts(&lock), (0, 1));

        // Once the first holder releases, the second attempt succeeds.
        lock.release(LockKind::Exclusive);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
        assert_eq!(counts(&lock), (0, 0));
        assert!(matches!(
            lock.try_acquire(LockKind::Exclusive).unwrap(),
            TryOutcome::Acquired
        ));
        assert_eq!(os_lock(&lock), OsLock::Exclusive);
        assert_eq!(counts(&lock), (0, 1));

        lock.release(LockKind::Exclusive);
        assert_eq!(os_lock(&lock), OsLock::Unlocked);
    }

    #[test]
    fn one_repo_lock_is_shared_across_handles_and_reclaimed() {
        let scratch = Scratch::new("registry");
        let a = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();
        let b = RepoLock::get_or_create(scratch.repo_fd(), RepoMode::Bare).unwrap();
        assert!(Arc::ptr_eq(&a, &b), "same repo yields one shared lock");
        let key = a.key;

        drop(a);
        drop(b);
        assert!(
            !registry().lock().unwrap().contains_key(&key),
            "registry entry is reclaimed once the last handle drops"
        );
    }
}