redox-scheme 0.11.1

Library for writing Redox scheme daemons
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
// Missing: F_SETLKW, F_OFD_SETLKW, and deadlock-detection

use std::collections::LinkedList;

const OFFSET_MAX: u64 = i64::MAX as u64;

fn span_end(start: u64, len: u64) -> Result<u64, LockError> {
    if len == 0 {
        Ok(OFFSET_MAX)
    } else {
        if len - 1 > OFFSET_MAX - start {
            return Err(LockError::Overflow);
        }
        Ok(start + (len - 1))
    }
}

#[derive(Debug)]
pub struct LockSet {
    list: LinkedList<Lock>,
}

impl LockSet {
    pub fn new() -> Self {
        Self {
            list: LinkedList::new(),
        }
    }

    /// # Note
    ///
    /// According to POSIX Issue 8:
    /// * Request for a *shared* lock shall fail if the file descriptor is not
    /// open for *reading*.
    ///
    /// * Request for an *exclusive* lock shall fail if the file descriptor is
    /// not open for *writing*.
    ///
    /// The caller **must** ensure that the above conditions are met before
    /// calling this method.
    pub fn lock(
        &mut self,
        kind: LockKind,
        owner: LockOwner,
        start: u64,
        len: u64,
    ) -> Result<(), LockError> {
        let new = Lock::new(kind, owner, start, span_end(start, len)?);
        if let Some(_conflict) = self.list.iter().find(|lck| lck.conflicts_with(&new)) {
            return Err(LockError::Conflict);
        }

        // Locks are grouped by owner. Find the first lock in the lock set with
        // the same owner as the new lock.
        let mut cursor = self.list.cursor_front_mut();
        while let Some(curr) = cursor.current()
            && curr.owner != new.owner
        {
            cursor.move_next();
        }

        if cursor.current().is_none() {
            // No lock with the same owner, insert at the end.
            cursor.insert_before(new);
            return Ok(());
        }

        while let Some(curr) = cursor.current()
            && curr.owner == new.owner
        {
            if new.start > curr.end + 1 {
                // The `new` lock comes after `curr` lock.
                cursor.move_next();
                continue;
            } else if new.end + 1 < curr.start {
                // The `new` lock comes before `curr` lock. Break and insert it
                // *before* `curr` lock.
                break;
            }

            // Now `new` and `curr` overlap or adjacent
            if curr.kind == new.kind {
                // Merge
                curr.start = new.start.min(curr.start);
                curr.end = new.end.max(curr.end);
                return Ok(());
            } else {
                if new.start <= curr.start && new.end >= curr.end {
                    *curr = new;
                    return Ok(());
                } else if start > curr.start && new.end < curr.end {
                    let right = Lock::new(curr.kind, curr.owner, new.end + 1, curr.end);
                    curr.end = new.start - 1;
                    cursor.insert_after(new);
                    cursor.move_next();
                    cursor.insert_after(right);
                    return Ok(());
                } else if start > curr.start {
                    curr.end = start - 1;
                    cursor.insert_after(new);
                    return Ok(());
                } else if new.end < curr.end {
                    curr.start = new.end + 1;
                    break;
                }
            }
        }

        cursor.insert_before(new);
        Ok(())
    }

    // TODO: take out common logic from lock and unlock
    pub fn unlock(&mut self, owner: LockOwner, start: u64, len: u64) -> Result<(), LockError> {
        let end = span_end(start, len)?;
        let mut cursor = self.list.cursor_front_mut();
        while let Some(curr) = cursor.current()
            && curr.owner != owner
        {
            cursor.move_next();
        }

        if cursor.current().is_none() {
            return Err(LockError::NotPresent);
        }

        let mut success = false;
        while let Some(curr) = cursor.current()
            && curr.owner == owner
        {
            if start > curr.end + 1 {
                // The `new` lock comes after `curr` lock.
                cursor.move_next();
                continue;
            } else if end + 1 < curr.start {
                // The `new` lock comes before `curr` lock. Break and insert it
                // *before* `curr` lock.
                break;
            }

            success = true;
            if start <= curr.start && end >= curr.end {
                // Unlock range covers the entire lock. Cursor is moved to point
                // to the next element by `remove_cursor`
                cursor.remove_current();
                continue;
            } else if start > curr.start && end < curr.end {
                // Unlock range is strictly inside the lock.
                let right = Lock::new(curr.kind, curr.owner, end, curr.end);
                curr.end = start - 1;
                cursor.insert_after(right);
                return Ok(());
            } else if start > curr.start {
                // Unlock covers the tail of this lock. Shrink from right.
                curr.end = start - 1;
            } else if end < curr.end {
                // Unlock covers the head of this lock. Shrink from left.
                curr.start = end + 1;
            }

            cursor.move_next();
        }

        if success {
            Ok(())
        } else {
            Err(LockError::NotPresent)
        }
    }

    pub fn get_lock(
        &mut self,
        kind: LockKind,
        owner: LockOwner,
        start: u64,
        len: u64,
    ) -> Option<&Lock> {
        let query = Lock::new(kind, owner, start, start + (len - 1));
        self.list.iter().find(|lck| lck.overlaps_with(&query))
    }

    /// `info` should be `None` for OFD-owned locks.
    pub fn on_close(&mut self, owner: LockOwner) -> Result<(), LockError> {
        let mut cursor = self.list.cursor_front_mut();
        while let Some(curr) = cursor.current() {
            // Closing any file descriptor drops all POSIX locks that the process holds on that
            // file, even if the lock was originally acquired using a different file
            // descriptor.
            let should_remove = owner == curr.owner;

            if should_remove {
                cursor.remove_current();
                return Ok(());
            } else {
                cursor.move_next();
            }
        }

        Err(LockError::NotPresent)
    }
}

#[derive(Debug, PartialEq)]
pub enum LockError {
    Conflict,
    NotPresent,
    Overflow,
}

#[derive(Debug)]
pub struct Lock {
    pub kind: LockKind,
    pub owner: LockOwner,
    // [start, end)
    pub start: u64,
    pub end: u64,
}

impl Lock {
    pub fn new(kind: LockKind, owner: LockOwner, start: u64, end: u64) -> Self {
        Self {
            kind,
            owner,
            start,
            end,
        }
    }

    #[inline]
    pub fn conflicts_with(&self, other: &Lock) -> bool {
        self.owner != other.owner
            && self.overlaps_with(other)
            // Each byte in the file can be either locked with one or more
            // shared locks or with a single exclusive lock.
            && (self.kind.is_exclusive() || other.kind.is_exclusive())
    }

    #[inline]
    pub fn overlaps_with(&self, other: &Lock) -> bool {
        self.start < other.end && self.end > other.start
    }

    #[inline]
    // FIXME: would this work for whole file locks with start=0 and end=0
    pub fn len(&self) -> u64 {
        self.end - self.start
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LockOwner {
    Process { pid: usize, inode: usize },
    Resource { fd: usize },
}

impl LockOwner {
    pub fn is_process(&self) -> bool {
        matches!(self, Self::Process { .. })
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LockKind {
    Shared,
    Exclusive,
}

impl LockKind {
    #[inline]
    pub fn is_exclusive(&self) -> bool {
        matches!(self, Self::Exclusive)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn mixed_owners() {
        let mut set = LockSet::new();
        assert!(
            set.lock(
                LockKind::Exclusive,
                LockOwner::Process { pid: 1, inode: 1 },
                0,
                2
            )
            .is_ok()
        );
        // According to POSIX Issue 8, an **exclusive** process or OFD-owned
        // lock prevents any **other** process or OFD-owned lock from being
        // placed on any overlapping byte range.
        assert!(
            !set.lock(LockKind::Shared, LockOwner::Resource { fd: 1 }, 0, 2)
                .is_ok()
        );
        assert!(
            !set.lock(LockKind::Exclusive, LockOwner::Resource { fd: 1 }, 0, 2)
                .is_ok()
        );

        assert!(
            !set.lock(
                LockKind::Shared,
                LockOwner::Process { pid: 2, inode: 2 },
                0,
                2
            )
            .is_ok()
        );
        assert!(
            !set.lock(
                LockKind::Exclusive,
                LockOwner::Process { pid: 2, inode: 2 },
                0,
                2
            )
            .is_ok()
        );
        // Similarly, a **shared** process or OFD-owned lock prevents any
        // **other exclusive** process or OFD-owned lock from being placed on
        // any overlapping byte range.
        assert!(
            set.lock(
                LockKind::Shared,
                LockOwner::Process { pid: 1, inode: 1 },
                2,
                2
            )
            .is_ok()
        );
        assert!(
            !set.lock(
                LockKind::Exclusive,
                LockOwner::Process { pid: 2, inode: 2 },
                2,
                2
            )
            .is_ok()
        );
        assert!(
            !set.lock(LockKind::Exclusive, LockOwner::Resource { fd: 1 }, 2, 2)
                .is_ok()
        );
        // A **shared** process or OFD-owned lock does **not** prevent any
        // **other shared** process or OFD-owned lock from being placed on any
        // overlapping byte range.
        assert!(
            set.lock(
                LockKind::Shared,
                LockOwner::Process { pid: 2, inode: 2 },
                2,
                2
            )
            .is_ok()
        );
        assert!(
            set.lock(LockKind::Shared, LockOwner::Resource { fd: 1 }, 2, 2)
                .is_ok()
        );
    }

    #[test]
    fn same_owner() {
        let mut set = LockSet::new();
        assert!(
            set.lock(
                LockKind::Exclusive,
                LockOwner::Process { pid: 1, inode: 1 },
                0,
                8
            )
            .is_ok()
        );
        // Requests from the **same** owner as the existing lock are allowed.
        assert!(
            set.lock(
                LockKind::Shared,
                LockOwner::Process { pid: 1, inode: 1 },
                4,
                4
            )
            .is_ok()
        );
        assert!(
            set.unlock(LockOwner::Process { pid: 1, inode: 1 }, 4, 8)
                .is_ok()
        );
        assert!(
            set.unlock(LockOwner::Process { pid: 1, inode: 1 }, 0, 8)
                .is_ok()
        );
    }

    #[test]
    fn test_on_close() {
        let mut set = LockSet::new();
        assert!(
            set.lock(
                LockKind::Exclusive,
                LockOwner::Process { pid: 1, inode: 1 },
                0,
                10
            )
            .is_ok()
        );
        assert!(
            set.on_close(LockOwner::Process { pid: 1, inode: 1 })
                .is_ok()
        );
        assert!(
            set.on_close(LockOwner::Process { pid: 1, inode: 1 }) == Err(LockError::NotPresent)
        );
    }
}