rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
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 | //! POSIX record locks: fcntl(F_GETLK / F_SETLK / F_SETLKW), stage 16.
//!
//! A program can lock a byte range of a file: a *read* (shared) lock, which
//! many processes may hold at once, or a *write* (exclusive) lock, which
//! nobody else may overlap. They're "advisory": read() and write() don't
//! look at them, only other fcntl calls do. Databases (SQLite) and mail
//! programs use them so two processes don't change one file at once.
//!
//! The rules, as in Linux:
//! - locks belong to a *process* (not to an fd): locking a range you
//! already hold replaces your old lock there (splitting it if needed);
//! your own locks never conflict with each other;
//! - closing *any* fd of the file drops all your locks on it, and so does
//! exiting;
//! - F_SETLK fails with EAGAIN on a conflict, F_SETLKW waits (a signal
//! interrupts it), unless waiting would deadlock: EDEADLK.
//!
//! Files are told apart by a key: the path hash stat calls st_ino, or for
//! files without a path (pipes, sockets) the open file's address -- the
//! same number fstat reports as st_ino (`key_of`).
//! Ranges are [start, end) with end = u64::MAX for "to the end of the file
//! and beyond" (l_len = 0).
use crateErrno;
use crateWaitQueue;
use ;
use Mutex;
use without_interrupts;
/// More than this many locks in total: ENOLCK (keeps a runaway program
/// from eating the kernel heap).
const MAX_LOCKS: usize = 1024;
/// Locked with interrupts off (it's looked at in a wait condition).
static TABLE: = new;
/// F_SETLKW sleepers: woken whenever a lock goes away.
static UNLOCKED: WaitQueue = new;
/// The key of an open file (see the top).
/// F_GETLK: the first lock that would stop `want`, if any.
/// Replace `pid`'s locks on [start, end) of file `key` with `new` (None =
/// unlock that range). Doesn't check for conflicts.
/// F_SETLK: take (Some(write)) or drop (None) a lock. EAGAIN on a conflict.
/// Would `pid` waiting for `want` close a circle of waiters (A waits for
/// B who waits for ... A)?
/// F_SETLKW: like `set`, but wait while somebody else holds a conflicting
/// lock. A signal ends the wait with ERESTARTSYS (EINTR for the program).
/// `pid` closed an fd of file `key`: all its locks on that file go.
/// Process `pid` exited: drop all its locks.
/// How many locks exist, record locks and flocks (tests: nothing left behind).
// --- flock (stage 17) ------------------------------------------------------
//
// flock(fd, LOCK_SH / LOCK_EX / LOCK_UN, maybe | LOCK_NB) locks a *whole*
// file, BSD style. Unlike the record locks above, a flock belongs to the
// *open file* (what open() made; dup() and fork() share it), not to a
// process: two open()s of the same file in one process do conflict, and the
// lock goes away when the last fd sharing that open file is closed
// (`Drop for InodeFile` calls `flock_release`).
//
// Many holders may share LOCK_SH; LOCK_EX allows nobody else. Asking again
// on an open file that already holds one *converts* it (SH <-> EX).
/// file key -> the open files holding a flock on it: (owner, exclusive).
static FLOCKS: = new;
/// Remove `owner`'s flock (any file). True if it had one.
/// Take (or convert to) a shared / exclusive flock on file `key` for open
/// file `owner`. `wait`: sleep until it's free (a signal interrupts:
/// ERESTARTSYS); else EAGAIN (= EWOULDBLOCK) at once.
/// LOCK_UN, or the open file went away: drop its flock, if any.
/// How many flocks are held (tests: nothing left behind).
|