smb-server 0.11.0

SMB2/3 file-sharing server library with pluggable storage backends.
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
//! The worker: it takes entries off the queue of the overlay when their time
//! comes and runs the jobs that bring the bucket to their state, a few at a
//! time.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

use tokio::sync::watch;
use tokio::task::{Id, JoinError, JoinHandle, JoinSet};
use tracing::error;

use super::backend::Shared;
use super::entry::{Entry, Progress};
use super::error::S3Error;
use super::job;
use super::overlay::{Claim, Held, Overlay};
use super::retry::Stop;
use super::tombs;

/// The two signals of a pause a test puts up: the job notifies the first when
/// it reaches the pause, and goes on when the second is notified.
#[cfg(test)]
pub(super) type Gates = (Arc<tokio::sync::Notify>, Arc<tokio::sync::Notify>);

/// Waits here if a test put up `gates`.
#[cfg(test)]
pub(super) async fn pause(gates: &std::sync::Mutex<Option<Gates>>) {
    if let Some((reached, resume)) = take_gates(gates) {
        reached.notify_one();
        resume.notified().await;
    }
}

/// [`pause`] for a thread that blocks.
#[cfg(test)]
pub(super) fn pause_blocking(gates: &std::sync::Mutex<Option<Gates>>) {
    if let Some((reached, resume)) = take_gates(gates) {
        reached.notify_one();
        tokio::runtime::Handle::current().block_on(resume.notified());
    }
}

#[cfg(test)]
fn take_gates(gates: &std::sync::Mutex<Option<Gates>>) -> Option<Gates> {
    gates
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .take()
}

/// A flag a test raises to make the code that checks it panic.
#[cfg(test)]
#[derive(Default)]
pub(super) struct PanicHook(std::sync::atomic::AtomicBool);

#[cfg(test)]
impl PanicHook {
    pub(super) fn raise(&self) {
        self.0.store(true, std::sync::atomic::Ordering::SeqCst);
    }

    /// Panics with `what` if a test raised the flag.
    pub(super) fn check(&self, what: &str) {
        assert!(!self.0.load(std::sync::atomic::Ordering::SeqCst), "{what}");
    }
}

/// The size of the largest object one CopyObject request takes.
const MAX_COPY: u64 = 5 << 30;

/// What the worker runs jobs with, and the signals it sends.
pub(super) struct Jobs {
    /// The size of the parts of a multipart upload; objects up to this size go
    /// up in one request.
    pub(super) part_size: u64,
    /// The size of the largest object one copy request takes; a larger one is
    /// copied in parts of this size.
    copy_limit: std::sync::atomic::AtomicU64,
    /// Jobs at once, and parts in flight for each.
    pub(super) concurrency: usize,
    /// Ends the worker, and the retries of the clean-up it does for jobs.
    pub(super) stop: Stop,
    /// Set when the worker has ended, however it ended.
    pub(super) exited: watch::Sender<bool>,
    /// How many tasks run on after the call that started them answered: those
    /// of a renamed directory and those that record a closed file. The worker
    /// does not end before those that began by then have. A task that begins
    /// later holds the backend state, so the cache directory stays locked until
    /// it ends.
    tasks: watch::Sender<usize>,
    /// How many of those tasks record closed files.
    #[cfg(test)]
    pub(super) saves: watch::Sender<usize>,
    /// Keeps the worker from starting jobs while set.
    #[cfg(test)]
    pub(super) held: std::sync::atomic::AtomicBool,
    /// Keeps the worker from deleting keys no entry owns while set.
    #[cfg(test)]
    pub(super) sweeps_held: std::sync::atomic::AtomicBool,
    /// Makes the worker panic.
    #[cfg(test)]
    pub(super) panic_worker: PanicHook,
    /// Makes the next job that has hashed a file wait.
    #[cfg(test)]
    pub(super) pause: std::sync::Mutex<Option<Gates>>,
    /// Makes the next job whose request has stored the file wait.
    #[cfg(test)]
    pub(super) pause_stored: std::sync::Mutex<Option<Gates>>,
    /// Makes the next job that has taken in what it stored wait, before it
    /// deletes keys.
    #[cfg(test)]
    pub(super) pause_landed: std::sync::Mutex<Option<Gates>>,
    /// Makes the next lookup that found an object wait before it takes a hold
    /// on the entry for it.
    #[cfg(test)]
    pub(super) pause_found: std::sync::Mutex<Option<Gates>>,
    /// Makes the next rename wait after it has reserved its name.
    #[cfg(test)]
    pub(super) pause_reserved: std::sync::Mutex<Option<Gates>>,
    /// Makes the next move of a directory wait before it records the entries
    /// it made.
    #[cfg(test)]
    pub(super) pause_batch: std::sync::Mutex<Option<Gates>>,
    /// Makes the next create wait once it has checked the directory it is in,
    /// before it enters the overlay.
    #[cfg(test)]
    pub(super) pause_create: std::sync::Mutex<Option<Gates>>,
    /// Makes the next move of a directory wait once it has read the keys below
    /// it.
    #[cfg(test)]
    pub(super) pause_listed: std::sync::Mutex<Option<Gates>>,
    /// Makes the next task that records a closed file wait before it takes the
    /// locks of the entry.
    #[cfg(test)]
    pub(super) pause_close: std::sync::Mutex<Option<Gates>>,
    /// Makes the task that records the entries of a moved directory panic.
    #[cfg(test)]
    pub(super) panic_dissolve: PanicHook,
    /// Makes the tasks that read the metadata of moved objects panic.
    #[cfg(test)]
    pub(super) panic_read_base: PanicHook,
    /// How many times a call waited for requests in flight or keys to clear.
    #[cfg(test)]
    pub(super) waits: std::sync::atomic::AtomicUsize,
}

impl Jobs {
    pub(super) fn new(part_size: u64, concurrency: usize) -> Arc<Jobs> {
        Arc::new(Jobs {
            part_size,
            copy_limit: std::sync::atomic::AtomicU64::new(MAX_COPY),
            concurrency,
            stop: Stop::new(),
            exited: watch::Sender::new(false),
            tasks: watch::Sender::new(0),
            #[cfg(test)]
            saves: watch::Sender::new(0),
            #[cfg(test)]
            held: std::sync::atomic::AtomicBool::new(false),
            #[cfg(test)]
            sweeps_held: std::sync::atomic::AtomicBool::new(false),
            #[cfg(test)]
            panic_worker: PanicHook::default(),
            #[cfg(test)]
            pause: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_stored: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_landed: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_found: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_reserved: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_batch: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_create: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_listed: std::sync::Mutex::new(None),
            #[cfg(test)]
            pause_close: std::sync::Mutex::new(None),
            #[cfg(test)]
            panic_dissolve: PanicHook::default(),
            #[cfg(test)]
            panic_read_base: PanicHook::default(),
            #[cfg(test)]
            waits: std::sync::atomic::AtomicUsize::new(0),
        })
    }

    /// Notes a task that runs on after the call that started it answered,
    /// until the value returned is dropped.
    pub(super) fn begin_task(self: &Arc<Self>) -> Task {
        self.tasks.send_modify(|count| *count += 1);
        Task(Arc::clone(self))
    }

    /// Notes a task that records a closed file, until the value returned is
    /// dropped.
    pub(super) fn begin_save(self: &Arc<Self>) -> Save {
        #[cfg(test)]
        self.saves.send_modify(|count| *count += 1);
        Save {
            _task: self.begin_task(),
            #[cfg(test)]
            jobs: Arc::clone(self),
        }
    }

    /// The size of the largest object one copy request takes.
    pub(super) fn copy_limit(&self) -> u64 {
        self.copy_limit.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Lowers the copy limit, for tests.
    #[cfg(test)]
    pub(super) fn limit_copy(&self, bytes: u64) {
        self.copy_limit
            .store(bytes, std::sync::atomic::Ordering::SeqCst);
    }

    #[cfg(test)]
    fn is_held(&self) -> bool {
        self.held.load(std::sync::atomic::Ordering::SeqCst)
    }

    #[cfg(not(test))]
    fn is_held(&self) -> bool {
        false
    }

    #[cfg(test)]
    fn are_sweeps_held(&self) -> bool {
        self.sweeps_held.load(std::sync::atomic::Ordering::SeqCst)
    }

    #[cfg(not(test))]
    fn are_sweeps_held(&self) -> bool {
        false
    }
}

/// A task that runs on after the call that started it answered.
pub(super) struct Task(Arc<Jobs>);

impl Drop for Task {
    fn drop(&mut self) {
        self.0.tasks.send_modify(|count| *count -= 1);
    }
}

/// A task that records a closed file.
pub(super) struct Save {
    _task: Task,
    #[cfg(test)]
    jobs: Arc<Jobs>,
}

#[cfg(test)]
impl Drop for Save {
    fn drop(&mut self) {
        self.jobs.saves.send_modify(|count| *count -= 1);
    }
}

/// The job running for an entry. When it ends the entry is free for the next
/// job, and the hold on it ends.
pub(super) struct Running {
    overlay: Arc<Overlay>,
    held: Option<Held>,
    pub(super) entry: Arc<Entry>,
    /// Stops the job when the entry changes.
    pub(super) stop: Stop,
    /// The job resolves a cycle of entries that each wait for the next.
    pub(super) cycle: bool,
}

impl Running {
    pub(super) fn held(&self) -> &Held {
        self.held.as_ref().expect("held until dropped")
    }
}

impl Drop for Running {
    fn drop(&mut self) {
        self.entry.state().job = None;
        self.overlay.end_writes(&self.entry);
        // The release puts the entry back in the queue if it has more to
        // store.
        drop(self.held.take());
        self.overlay.touch();
    }
}

/// Owns the worker of a backend. The worker ends when the last clone of the
/// backend, and with it this, is dropped.
pub(super) struct Guard {
    jobs: Arc<Jobs>,
    _worker: Option<JoinHandle<()>>,
}

impl Guard {
    /// Starts the worker of `shared`, if it is writable.
    pub(super) fn start(shared: &Arc<Shared>) -> Guard {
        let worker = shared
            .local
            .is_some()
            .then(|| tokio::spawn(work(Arc::clone(shared))));
        Guard {
            jobs: Arc::clone(&shared.jobs),
            _worker: worker,
        }
    }
}

impl Drop for Guard {
    fn drop(&mut self) {
        self.jobs.stop.stop();
    }
}

/// Tells that the worker has ended when it is dropped, whether the worker
/// returned, panicked or was cancelled.
struct Exit(Arc<Jobs>);

impl Drop for Exit {
    fn drop(&mut self) {
        self.0.exited.send_replace(true);
    }
}

/// Runs jobs until the backend is dropped, then lets go of the backend's
/// state.
async fn work(shared: Arc<Shared>) {
    let _exit = Exit(Arc::clone(&shared.jobs));
    run_jobs(shared).await;
}

/// Runs jobs until the backend is dropped. Jobs that are running then are
/// stopped, and the worker waits for the requests they have in flight. A
/// multipart upload left that way stays in the record of its entry, and the
/// next backend aborts it.
async fn run_jobs(shared: Arc<Shared>) {
    let jobs = Arc::clone(&shared.jobs);
    let queue = shared.overlay.queue();
    let mut running: JoinSet<()> = JoinSet::new();
    let mut stops: HashMap<Id, Stop> = HashMap::new();
    // Listens from before the queue is read, so a push after the read ends
    // the wait below.
    let woken = queue.woken();
    tokio::pin!(woken);
    loop {
        woken.as_mut().enable();
        #[cfg(test)]
        jobs.panic_worker.check("a test made the worker panic");
        let may_start = !jobs.is_held() && running.len() < jobs.concurrency;
        // One clock reading serves the claims and the wait, so a time that
        // passes between them is not missed.
        let now = Instant::now();
        if may_start {
            start(&shared, &mut running, &mut stops, now);
        }
        let due = if may_start {
            [queue.next_due(), shared.overlay.tomb_next_due(now)]
                .into_iter()
                .flatten()
                .min()
        } else {
            None
        };
        tokio::select! {
            () = jobs.stop.stopped() => break,
            () = woken.as_mut() => woken.set(queue.woken()),
            ended = running.join_next_with_id(), if !running.is_empty() => {
                if let Some(ended) = ended {
                    ended_job(ended, &mut stops);
                }
            }
            () = wait_until(due) => {}
        }
    }
    for stop in stops.values() {
        stop.stop();
    }
    while let Some(ended) = running.join_next_with_id().await {
        ended_job(ended, &mut stops);
    }
    let _ = jobs.tasks.subscribe().wait_for(|count| *count == 0).await;
}

/// Waits until `at`, or forever.
async fn wait_until(at: Option<Instant>) {
    match at {
        Some(at) => tokio::time::sleep_until(tokio::time::Instant::from_std(at)).await,
        None => std::future::pending().await,
    }
}

fn ended_job(ended: Result<(Id, ()), JoinError>, stops: &mut HashMap<Id, Stop>) {
    match ended {
        Ok((id, ())) => {
            stops.remove(&id);
        }
        Err(e) => {
            stops.remove(&e.id());
            error!(error = %e, "a background job ended without finishing");
        }
    }
}

/// Starts jobs for the entries of the queue whose time has come, while there
/// is room for them.
fn start(
    shared: &Arc<Shared>,
    running: &mut JoinSet<()>,
    stops: &mut HashMap<Id, Stop>,
    now: Instant,
) {
    let queue = shared.overlay.queue();
    while running.len() < shared.jobs.concurrency {
        let Some(entry) = queue.pop_due(now) else {
            break;
        };
        match shared.overlay.claim(entry, now) {
            Claim::Run(held, stop, cycle) => {
                let job = Running {
                    overlay: Arc::clone(&shared.overlay),
                    entry: Arc::clone(&held.entry),
                    held: Some(held),
                    stop: stop.clone(),
                    cycle,
                };
                let handle = running.spawn(job::run(Arc::clone(shared), job));
                stops.insert(handle.id(), stop);
            }
            Claim::Later(entry, due) => queue.push(entry, due),
            Claim::Skip => {}
        }
    }
    if running.len() < shared.jobs.concurrency
        && !shared.jobs.are_sweeps_held()
        && let Some(claim) = shared.overlay.claim_tombs(now)
    {
        let handle = running.spawn(tombs::delete(Arc::clone(shared), claim));
        stops.insert(handle.id(), shared.jobs.stop.clone());
    }
}

impl Shared {
    /// Waits until the files the backend holds bytes of, the changes of
    /// times, attributes, names and the directory markers it has not put, and
    /// the keys it is to delete, as they stand when the call begins, are in
    /// the bucket, or have failed for good.
    pub(super) async fn sync(&self) -> Result<(), S3Error> {
        if self.local.is_none() {
            return Ok(());
        }
        let mut ended = self.overlay.subscribe();
        let mut exited = self.jobs.exited.subscribe();
        let waits = self.overlay.work();
        let tombs = self.overlay.tomb_waits();
        loop {
            let mut waiting = false;
            for wait in &waits {
                match wait.entry.progress(wait.generation, &wait.gone) {
                    Progress::Done => {}
                    Progress::Waiting => {
                        if let Some(failure) = self.overlay.failure_behind(&wait.entry) {
                            return Err(failure);
                        }
                        waiting = true;
                    }
                    Progress::Failed(failure) => return Err(failure),
                }
            }
            for (key, seq) in &tombs {
                match self.overlay.tomb_progress(key, *seq) {
                    Progress::Done => {}
                    Progress::Waiting => waiting = true,
                    Progress::Failed(failure) => return Err(failure),
                }
            }
            if !waiting {
                return Ok(());
            }
            tokio::select! {
                _ = ended.changed() => {}
                _ = exited.wait_for(|exited| *exited) => return Err(S3Error::Stopped),
            }
        }
    }
}