Loading...
Note: File does not exist in v5.4.
1// SPDX-License-Identifier: GPL-2.0
2
3// Copyright (C) 2024 Google LLC.
4
5//! Files and file descriptors.
6//!
7//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h) and
8//! [`include/linux/file.h`](srctree/include/linux/file.h)
9
10use crate::{
11 bindings,
12 cred::Credential,
13 error::{code::*, Error, Result},
14 types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
15};
16use core::ptr;
17
18/// Flags associated with a [`File`].
19pub mod flags {
20 /// File is opened in append mode.
21 pub const O_APPEND: u32 = bindings::O_APPEND;
22
23 /// Signal-driven I/O is enabled.
24 pub const O_ASYNC: u32 = bindings::FASYNC;
25
26 /// Close-on-exec flag is set.
27 pub const O_CLOEXEC: u32 = bindings::O_CLOEXEC;
28
29 /// File was created if it didn't already exist.
30 pub const O_CREAT: u32 = bindings::O_CREAT;
31
32 /// Direct I/O is enabled for this file.
33 pub const O_DIRECT: u32 = bindings::O_DIRECT;
34
35 /// File must be a directory.
36 pub const O_DIRECTORY: u32 = bindings::O_DIRECTORY;
37
38 /// Like [`O_SYNC`] except metadata is not synced.
39 pub const O_DSYNC: u32 = bindings::O_DSYNC;
40
41 /// Ensure that this file is created with the `open(2)` call.
42 pub const O_EXCL: u32 = bindings::O_EXCL;
43
44 /// Large file size enabled (`off64_t` over `off_t`).
45 pub const O_LARGEFILE: u32 = bindings::O_LARGEFILE;
46
47 /// Do not update the file last access time.
48 pub const O_NOATIME: u32 = bindings::O_NOATIME;
49
50 /// File should not be used as process's controlling terminal.
51 pub const O_NOCTTY: u32 = bindings::O_NOCTTY;
52
53 /// If basename of path is a symbolic link, fail open.
54 pub const O_NOFOLLOW: u32 = bindings::O_NOFOLLOW;
55
56 /// File is using nonblocking I/O.
57 pub const O_NONBLOCK: u32 = bindings::O_NONBLOCK;
58
59 /// File is using nonblocking I/O.
60 ///
61 /// This is effectively the same flag as [`O_NONBLOCK`] on all architectures
62 /// except SPARC64.
63 pub const O_NDELAY: u32 = bindings::O_NDELAY;
64
65 /// Used to obtain a path file descriptor.
66 pub const O_PATH: u32 = bindings::O_PATH;
67
68 /// Write operations on this file will flush data and metadata.
69 pub const O_SYNC: u32 = bindings::O_SYNC;
70
71 /// This file is an unnamed temporary regular file.
72 pub const O_TMPFILE: u32 = bindings::O_TMPFILE;
73
74 /// File should be truncated to length 0.
75 pub const O_TRUNC: u32 = bindings::O_TRUNC;
76
77 /// Bitmask for access mode flags.
78 ///
79 /// # Examples
80 ///
81 /// ```
82 /// use kernel::fs::file;
83 /// # fn do_something() {}
84 /// # let flags = 0;
85 /// if (flags & file::flags::O_ACCMODE) == file::flags::O_RDONLY {
86 /// do_something();
87 /// }
88 /// ```
89 pub const O_ACCMODE: u32 = bindings::O_ACCMODE;
90
91 /// File is read only.
92 pub const O_RDONLY: u32 = bindings::O_RDONLY;
93
94 /// File is write only.
95 pub const O_WRONLY: u32 = bindings::O_WRONLY;
96
97 /// File can be both read and written.
98 pub const O_RDWR: u32 = bindings::O_RDWR;
99}
100
101/// Wraps the kernel's `struct file`. Thread safe.
102///
103/// This represents an open file rather than a file on a filesystem. Processes generally reference
104/// open files using file descriptors. However, file descriptors are not the same as files. A file
105/// descriptor is just an integer that corresponds to a file, and a single file may be referenced
106/// by multiple file descriptors.
107///
108/// # Refcounting
109///
110/// Instances of this type are reference-counted. The reference count is incremented by the
111/// `fget`/`get_file` functions and decremented by `fput`. The Rust type `ARef<File>` represents a
112/// pointer that owns a reference count on the file.
113///
114/// Whenever a process opens a file descriptor (fd), it stores a pointer to the file in its fd
115/// table (`struct files_struct`). This pointer owns a reference count to the file, ensuring the
116/// file isn't prematurely deleted while the file descriptor is open. In Rust terminology, the
117/// pointers in `struct files_struct` are `ARef<File>` pointers.
118///
119/// ## Light refcounts
120///
121/// Whenever a process has an fd to a file, it may use something called a "light refcount" as a
122/// performance optimization. Light refcounts are acquired by calling `fdget` and released with
123/// `fdput`. The idea behind light refcounts is that if the fd is not closed between the calls to
124/// `fdget` and `fdput`, then the refcount cannot hit zero during that time, as the `struct
125/// files_struct` holds a reference until the fd is closed. This means that it's safe to access the
126/// file even if `fdget` does not increment the refcount.
127///
128/// The requirement that the fd is not closed during a light refcount applies globally across all
129/// threads - not just on the thread using the light refcount. For this reason, light refcounts are
130/// only used when the `struct files_struct` is not shared with other threads, since this ensures
131/// that other unrelated threads cannot suddenly start using the fd and close it. Therefore,
132/// calling `fdget` on a shared `struct files_struct` creates a normal refcount instead of a light
133/// refcount.
134///
135/// Light reference counts must be released with `fdput` before the system call returns to
136/// userspace. This means that if you wait until the current system call returns to userspace, then
137/// all light refcounts that existed at the time have gone away.
138///
139/// ### The file position
140///
141/// Each `struct file` has a position integer, which is protected by the `f_pos_lock` mutex.
142/// However, if the `struct file` is not shared, then the kernel may avoid taking the lock as a
143/// performance optimization.
144///
145/// The condition for avoiding the `f_pos_lock` mutex is different from the condition for using
146/// `fdget`. With `fdget`, you may avoid incrementing the refcount as long as the current fd table
147/// is not shared; it is okay if there are other fd tables that also reference the same `struct
148/// file`. However, `fdget_pos` can only avoid taking the `f_pos_lock` if the entire `struct file`
149/// is not shared, as different processes with an fd to the same `struct file` share the same
150/// position.
151///
152/// To represent files that are not thread safe due to this optimization, the [`LocalFile`] type is
153/// used.
154///
155/// ## Rust references
156///
157/// The reference type `&File` is similar to light refcounts:
158///
159/// * `&File` references don't own a reference count. They can only exist as long as the reference
160/// count stays positive, and can only be created when there is some mechanism in place to ensure
161/// this.
162///
163/// * The Rust borrow-checker normally ensures this by enforcing that the `ARef<File>` from which
164/// a `&File` is created outlives the `&File`.
165///
166/// * Using the unsafe [`File::from_raw_file`] means that it is up to the caller to ensure that the
167/// `&File` only exists while the reference count is positive.
168///
169/// * You can think of `fdget` as using an fd to look up an `ARef<File>` in the `struct
170/// files_struct` and create an `&File` from it. The "fd cannot be closed" rule is like the Rust
171/// rule "the `ARef<File>` must outlive the `&File`".
172///
173/// # Invariants
174///
175/// * All instances of this type are refcounted using the `f_count` field.
176/// * There must not be any active calls to `fdget_pos` on this file that did not take the
177/// `f_pos_lock` mutex.
178#[repr(transparent)]
179pub struct File {
180 inner: Opaque<bindings::file>,
181}
182
183// SAFETY: This file is known to not have any active `fdget_pos` calls that did not take the
184// `f_pos_lock` mutex, so it is safe to transfer it between threads.
185unsafe impl Send for File {}
186
187// SAFETY: This file is known to not have any active `fdget_pos` calls that did not take the
188// `f_pos_lock` mutex, so it is safe to access its methods from several threads in parallel.
189unsafe impl Sync for File {}
190
191// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation
192// makes `ARef<File>` own a normal refcount.
193unsafe impl AlwaysRefCounted for File {
194 #[inline]
195 fn inc_ref(&self) {
196 // SAFETY: The existence of a shared reference means that the refcount is nonzero.
197 unsafe { bindings::get_file(self.as_ptr()) };
198 }
199
200 #[inline]
201 unsafe fn dec_ref(obj: ptr::NonNull<File>) {
202 // SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we
203 // may drop it. The cast is okay since `File` has the same representation as `struct file`.
204 unsafe { bindings::fput(obj.cast().as_ptr()) }
205 }
206}
207
208/// Wraps the kernel's `struct file`. Not thread safe.
209///
210/// This type represents a file that is not known to be safe to transfer across thread boundaries.
211/// To obtain a thread-safe [`File`], use the [`assume_no_fdget_pos`] conversion.
212///
213/// See the documentation for [`File`] for more information.
214///
215/// # Invariants
216///
217/// * All instances of this type are refcounted using the `f_count` field.
218/// * If there is an active call to `fdget_pos` that did not take the `f_pos_lock` mutex, then it
219/// must be on the same thread as this file.
220///
221/// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos
222pub struct LocalFile {
223 inner: Opaque<bindings::file>,
224}
225
226// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation
227// makes `ARef<File>` own a normal refcount.
228unsafe impl AlwaysRefCounted for LocalFile {
229 #[inline]
230 fn inc_ref(&self) {
231 // SAFETY: The existence of a shared reference means that the refcount is nonzero.
232 unsafe { bindings::get_file(self.as_ptr()) };
233 }
234
235 #[inline]
236 unsafe fn dec_ref(obj: ptr::NonNull<LocalFile>) {
237 // SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we
238 // may drop it. The cast is okay since `File` has the same representation as `struct file`.
239 unsafe { bindings::fput(obj.cast().as_ptr()) }
240 }
241}
242
243impl LocalFile {
244 /// Constructs a new `struct file` wrapper from a file descriptor.
245 ///
246 /// The file descriptor belongs to the current process, and there might be active local calls
247 /// to `fdget_pos` on the same file.
248 ///
249 /// To obtain an `ARef<File>`, use the [`assume_no_fdget_pos`] function to convert.
250 ///
251 /// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos
252 #[inline]
253 pub fn fget(fd: u32) -> Result<ARef<LocalFile>, BadFdError> {
254 // SAFETY: FFI call, there are no requirements on `fd`.
255 let ptr = ptr::NonNull::new(unsafe { bindings::fget(fd) }).ok_or(BadFdError)?;
256
257 // SAFETY: `bindings::fget` created a refcount, and we pass ownership of it to the `ARef`.
258 //
259 // INVARIANT: This file is in the fd table on this thread, so either all `fdget_pos` calls
260 // are on this thread, or the file is shared, in which case `fdget_pos` calls took the
261 // `f_pos_lock` mutex.
262 Ok(unsafe { ARef::from_raw(ptr.cast()) })
263 }
264
265 /// Creates a reference to a [`LocalFile`] from a valid pointer.
266 ///
267 /// # Safety
268 ///
269 /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is
270 /// positive for the duration of 'a.
271 /// * The caller must ensure that if there is an active call to `fdget_pos` that did not take
272 /// the `f_pos_lock` mutex, then that call is on the current thread.
273 #[inline]
274 pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a LocalFile {
275 // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
276 // duration of 'a. The cast is okay because `File` is `repr(transparent)`.
277 //
278 // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
279 unsafe { &*ptr.cast() }
280 }
281
282 /// Assume that there are no active `fdget_pos` calls that prevent us from sharing this file.
283 ///
284 /// This makes it safe to transfer this file to other threads. No checks are performed, and
285 /// using it incorrectly may lead to a data race on the file position if the file is shared
286 /// with another thread.
287 ///
288 /// This method is intended to be used together with [`LocalFile::fget`] when the caller knows
289 /// statically that there are no `fdget_pos` calls on the current thread. For example, you
290 /// might use it when calling `fget` from an ioctl, since ioctls usually do not touch the file
291 /// position.
292 ///
293 /// # Safety
294 ///
295 /// There must not be any active `fdget_pos` calls on the current thread.
296 #[inline]
297 pub unsafe fn assume_no_fdget_pos(me: ARef<LocalFile>) -> ARef<File> {
298 // INVARIANT: There are no `fdget_pos` calls on the current thread, and by the type
299 // invariants, if there is a `fdget_pos` call on another thread, then it took the
300 // `f_pos_lock` mutex.
301 //
302 // SAFETY: `LocalFile` and `File` have the same layout.
303 unsafe { ARef::from_raw(ARef::into_raw(me).cast()) }
304 }
305
306 /// Returns a raw pointer to the inner C struct.
307 #[inline]
308 pub fn as_ptr(&self) -> *mut bindings::file {
309 self.inner.get()
310 }
311
312 /// Returns the credentials of the task that originally opened the file.
313 pub fn cred(&self) -> &Credential {
314 // SAFETY: It's okay to read the `f_cred` field without synchronization because `f_cred` is
315 // never changed after initialization of the file.
316 let ptr = unsafe { (*self.as_ptr()).f_cred };
317
318 // SAFETY: The signature of this function ensures that the caller will only access the
319 // returned credential while the file is still valid, and the C side ensures that the
320 // credential stays valid at least as long as the file.
321 unsafe { Credential::from_ptr(ptr) }
322 }
323
324 /// Returns the flags associated with the file.
325 ///
326 /// The flags are a combination of the constants in [`flags`].
327 #[inline]
328 pub fn flags(&self) -> u32 {
329 // This `read_volatile` is intended to correspond to a READ_ONCE call.
330 //
331 // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount.
332 //
333 // FIXME(read_once): Replace with `read_once` when available on the Rust side.
334 unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() }
335 }
336}
337
338impl File {
339 /// Creates a reference to a [`File`] from a valid pointer.
340 ///
341 /// # Safety
342 ///
343 /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is
344 /// positive for the duration of 'a.
345 /// * The caller must ensure that if there are active `fdget_pos` calls on this file, then they
346 /// took the `f_pos_lock` mutex.
347 #[inline]
348 pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a File {
349 // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
350 // duration of 'a. The cast is okay because `File` is `repr(transparent)`.
351 //
352 // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
353 unsafe { &*ptr.cast() }
354 }
355}
356
357// Make LocalFile methods available on File.
358impl core::ops::Deref for File {
359 type Target = LocalFile;
360 #[inline]
361 fn deref(&self) -> &LocalFile {
362 // SAFETY: The caller provides a `&File`, and since it is a reference, it must point at a
363 // valid file for the desired duration.
364 //
365 // By the type invariants, there are no `fdget_pos` calls that did not take the
366 // `f_pos_lock` mutex.
367 unsafe { LocalFile::from_raw_file(self as *const File as *const bindings::file) }
368 }
369}
370
371/// A file descriptor reservation.
372///
373/// This allows the creation of a file descriptor in two steps: first, we reserve a slot for it,
374/// then we commit or drop the reservation. The first step may fail (e.g., the current process ran
375/// out of available slots), but commit and drop never fail (and are mutually exclusive).
376///
377/// Dropping the reservation happens in the destructor of this type.
378///
379/// # Invariants
380///
381/// The fd stored in this struct must correspond to a reserved file descriptor of the current task.
382pub struct FileDescriptorReservation {
383 fd: u32,
384 /// Prevent values of this type from being moved to a different task.
385 ///
386 /// The `fd_install` and `put_unused_fd` functions assume that the value of `current` is
387 /// unchanged since the call to `get_unused_fd_flags`. By adding this marker to this type, we
388 /// prevent it from being moved across task boundaries, which ensures that `current` does not
389 /// change while this value exists.
390 _not_send: NotThreadSafe,
391}
392
393impl FileDescriptorReservation {
394 /// Creates a new file descriptor reservation.
395 pub fn get_unused_fd_flags(flags: u32) -> Result<Self> {
396 // SAFETY: FFI call, there are no safety requirements on `flags`.
397 let fd: i32 = unsafe { bindings::get_unused_fd_flags(flags) };
398 if fd < 0 {
399 return Err(Error::from_errno(fd));
400 }
401 Ok(Self {
402 fd: fd as u32,
403 _not_send: NotThreadSafe,
404 })
405 }
406
407 /// Returns the file descriptor number that was reserved.
408 pub fn reserved_fd(&self) -> u32 {
409 self.fd
410 }
411
412 /// Commits the reservation.
413 ///
414 /// The previously reserved file descriptor is bound to `file`. This method consumes the
415 /// [`FileDescriptorReservation`], so it will not be usable after this call.
416 pub fn fd_install(self, file: ARef<File>) {
417 // SAFETY: `self.fd` was previously returned by `get_unused_fd_flags`. We have not yet used
418 // the fd, so it is still valid, and `current` still refers to the same task, as this type
419 // cannot be moved across task boundaries.
420 //
421 // Furthermore, the file pointer is guaranteed to own a refcount by its type invariants,
422 // and we take ownership of that refcount by not running the destructor below.
423 // Additionally, the file is known to not have any non-shared `fdget_pos` calls, so even if
424 // this process starts using the file position, this will not result in a data race on the
425 // file position.
426 unsafe { bindings::fd_install(self.fd, file.as_ptr()) };
427
428 // `fd_install` consumes both the file descriptor and the file reference, so we cannot run
429 // the destructors.
430 core::mem::forget(self);
431 core::mem::forget(file);
432 }
433}
434
435impl Drop for FileDescriptorReservation {
436 fn drop(&mut self) {
437 // SAFETY: By the type invariants of this type, `self.fd` was previously returned by
438 // `get_unused_fd_flags`. We have not yet used the fd, so it is still valid, and `current`
439 // still refers to the same task, as this type cannot be moved across task boundaries.
440 unsafe { bindings::put_unused_fd(self.fd) };
441 }
442}
443
444/// Represents the `EBADF` error code.
445///
446/// Used for methods that can only fail with `EBADF`.
447#[derive(Copy, Clone, Eq, PartialEq)]
448pub struct BadFdError;
449
450impl From<BadFdError> for Error {
451 #[inline]
452 fn from(_: BadFdError) -> Error {
453 EBADF
454 }
455}
456
457impl core::fmt::Debug for BadFdError {
458 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
459 f.pad("EBADF")
460 }
461}