dentry.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. pub mod dcache;
  2. mod walk;
  3. use core::{
  4. cell::UnsafeCell,
  5. fmt,
  6. hash::{BuildHasher, BuildHasherDefault, Hasher},
  7. sync::atomic::{AtomicPtr, AtomicU64, AtomicU8, Ordering},
  8. };
  9. use alloc::sync::Arc;
  10. use arcref::AsArcRef;
  11. use eonix_sync::LazyLock;
  12. use pointers::BorrowedArc;
  13. use posix_types::{namei::RenameFlags, open::OpenFlags, result::PosixError, stat::StatX};
  14. use crate::{
  15. hash::KernelHasher,
  16. io::Buffer,
  17. io::Stream,
  18. kernel::constants::{EEXIST, EINVAL, EISDIR, ELOOP, ENOENT, EPERM, ERANGE},
  19. kernel::{block::BlockDevice, CharDevice},
  20. path::Path,
  21. prelude::*,
  22. rcu::{rcu_read_lock, RCUNode, RCUPointer, RCUReadGuard},
  23. };
  24. use super::{
  25. inode::{Ino, Inode, InodeUse, RenameData, WriteOffset},
  26. types::{DeviceId, Format, Mode, Permission},
  27. FsContext,
  28. };
  29. const D_INVALID: u8 = 0;
  30. const D_REGULAR: u8 = 1;
  31. const D_DIRECTORY: u8 = 2;
  32. const D_SYMLINK: u8 = 3;
  33. #[derive(Debug, PartialEq, Eq)]
  34. enum DentryKind {
  35. Regular = D_REGULAR as isize,
  36. Directory = D_DIRECTORY as isize,
  37. Symlink = D_SYMLINK as isize,
  38. }
  39. /// The [`Inode`] associated with a [`Dentry`].
  40. ///
  41. /// We could assign an inode to a negative dentry exactly once when the dentry
  42. /// is invalid and we create a file or directory on it, or the dentry is brought
  43. /// to the dcache by [lookup()].
  44. ///
  45. /// This guarantees that as long as we acquire a non-invalid from [`Self::kind`],
  46. /// we are synced with the writer and can safely read the [`Self::inode`] field
  47. /// without reading torn data.
  48. ///
  49. /// [lookup()]: crate::kernel::vfs::inode::InodeDirOps::lookup
  50. struct AssociatedInode {
  51. kind: UnsafeCell<Option<DentryKind>>,
  52. inode: UnsafeCell<Option<InodeUse<dyn Inode>>>,
  53. }
  54. /// # Safety
  55. ///
  56. /// We wrap `Dentry` in `Arc` to ensure that the `Dentry` is not dropped while it is still in use.
  57. ///
  58. /// Since a `Dentry` is created and marked as live(some data is saved to it), it keeps alive until
  59. /// the last reference is dropped.
  60. pub struct Dentry {
  61. // Const after insertion into dcache
  62. parent: RCUPointer<Dentry>,
  63. name: RCUPointer<Arc<[u8]>>,
  64. hash: AtomicU64,
  65. // Used by the dentry cache
  66. prev: AtomicPtr<Dentry>,
  67. next: AtomicPtr<Dentry>,
  68. inode: AssociatedInode,
  69. }
  70. pub(super) static DROOT: LazyLock<Arc<Dentry>> = LazyLock::new(|| {
  71. let root = Arc::new(Dentry {
  72. parent: RCUPointer::empty(),
  73. name: RCUPointer::new(Arc::new(Arc::from(&b"[root]"[..]))),
  74. hash: AtomicU64::new(0),
  75. prev: AtomicPtr::default(),
  76. next: AtomicPtr::default(),
  77. inode: AssociatedInode::new(),
  78. });
  79. unsafe {
  80. root.parent.swap(Some(root.clone()));
  81. }
  82. root.rehash();
  83. root
  84. });
  85. impl fmt::Debug for Dentry {
  86. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  87. f.debug_struct("Dentry")
  88. .field("name", &String::from_utf8_lossy(&self.name()))
  89. .finish()
  90. }
  91. }
  92. impl RCUNode<Dentry> for Dentry {
  93. fn rcu_prev(&self) -> &AtomicPtr<Self> {
  94. &self.prev
  95. }
  96. fn rcu_next(&self) -> &AtomicPtr<Self> {
  97. &self.next
  98. }
  99. }
  100. impl Dentry {
  101. fn is_hashed(&self) -> bool {
  102. self.prev.load(Ordering::Relaxed) != core::ptr::null_mut()
  103. }
  104. fn rehash(&self) {
  105. assert!(
  106. !self.is_hashed(),
  107. "`rehash()` called on some already hashed dentry"
  108. );
  109. let builder: BuildHasherDefault<KernelHasher> = Default::default();
  110. let mut hasher = builder.build_hasher();
  111. hasher.write_usize(self.parent_addr() as usize);
  112. hasher.write(&self.name());
  113. let hash = hasher.finish();
  114. self.hash.store(hash, Ordering::Relaxed);
  115. }
  116. }
  117. impl Dentry {
  118. pub fn create(parent: Arc<Dentry>, name: &[u8]) -> Arc<Self> {
  119. // TODO!!!: don't acquire our parent's refcount here...
  120. let val = Arc::new(Self {
  121. parent: RCUPointer::new(parent),
  122. name: RCUPointer::new(Arc::new(Arc::from(name))),
  123. hash: AtomicU64::new(0),
  124. prev: AtomicPtr::default(),
  125. next: AtomicPtr::default(),
  126. inode: AssociatedInode::new(),
  127. });
  128. val.rehash();
  129. val
  130. }
  131. /// Check the equality of two denties inside the same dentry cache hash group
  132. /// where `other` is identified by `hash`, `parent` and `name`
  133. ///
  134. fn hash_eq(&self, other: &Self) -> bool {
  135. self.hash.load(Ordering::Relaxed) == other.hash.load(Ordering::Relaxed)
  136. && self.parent_addr() == other.parent_addr()
  137. && &***self.name() == &***other.name()
  138. }
  139. pub fn name(&self) -> RCUReadGuard<BorrowedArc<Arc<[u8]>>> {
  140. self.name.load().expect("Dentry has no name")
  141. }
  142. pub fn get_name(&self) -> Arc<[u8]> {
  143. (***self.name()).clone()
  144. }
  145. pub fn parent<'a>(&self) -> RCUReadGuard<'a, BorrowedArc<Dentry>> {
  146. self.parent.load().expect("Dentry has no parent")
  147. }
  148. pub fn parent_addr(&self) -> *const Self {
  149. self.parent
  150. .load()
  151. .map_or(core::ptr::null(), |parent| Arc::as_ptr(&parent))
  152. }
  153. pub fn fill(&self, file: InodeUse<dyn Inode>) {
  154. self.inode.store(file);
  155. }
  156. pub fn inode(&self) -> Option<InodeUse<dyn Inode>> {
  157. self.inode.load().map(|(_, inode)| inode.clone())
  158. }
  159. pub fn get_inode(&self) -> KResult<InodeUse<dyn Inode>> {
  160. self.inode().ok_or(ENOENT)
  161. }
  162. pub fn is_directory(&self) -> bool {
  163. self.inode
  164. .load()
  165. .map_or(false, |(kind, _)| kind == DentryKind::Directory)
  166. }
  167. pub fn is_valid(&self) -> bool {
  168. self.inode.load().is_some()
  169. }
  170. pub async fn open_check(self: &Arc<Self>, flags: OpenFlags, perm: Permission) -> KResult<()> {
  171. match self.inode.load() {
  172. Some(_) => {
  173. if flags.contains(OpenFlags::O_CREAT | OpenFlags::O_EXCL) {
  174. Err(EEXIST)
  175. } else {
  176. Ok(())
  177. }
  178. }
  179. None => {
  180. if !flags.contains(OpenFlags::O_CREAT) {
  181. return Err(ENOENT);
  182. }
  183. let parent = self.parent().get_inode()?;
  184. parent.create(self, perm).await
  185. }
  186. }
  187. }
  188. }
  189. impl Dentry {
  190. pub async fn open(
  191. context: &FsContext,
  192. path: &Path,
  193. follow_symlinks: bool,
  194. ) -> KResult<Arc<Self>> {
  195. let cwd = context.cwd.lock().clone();
  196. Self::open_at(context, &cwd, path, follow_symlinks).await
  197. }
  198. pub async fn open_at(
  199. context: &FsContext,
  200. at: &Arc<Self>,
  201. path: &Path,
  202. follow_symlinks: bool,
  203. ) -> KResult<Arc<Self>> {
  204. let mut found = context.start_recursive_walk(at, path).await?;
  205. if !follow_symlinks {
  206. return Ok(found);
  207. }
  208. loop {
  209. match found.inode.load() {
  210. Some((DentryKind::Symlink, inode)) => {
  211. found = context.follow_symlink(found.aref(), inode, 0).await?;
  212. }
  213. _ => return Ok(found),
  214. }
  215. }
  216. }
  217. pub fn get_path(self: &Arc<Self>, context: &FsContext, buffer: &mut dyn Buffer) -> KResult<()> {
  218. let rcu_read = rcu_read_lock();
  219. let mut path = vec![];
  220. let mut current = self.aref();
  221. let mut parent = self.parent.dereference(&rcu_read).unwrap();
  222. while !current.ptr_eq_arc(&context.fsroot) {
  223. if path.len() > 32 {
  224. return Err(ELOOP);
  225. }
  226. path.push(current.name.dereference(&rcu_read).unwrap());
  227. current = parent;
  228. parent = current.parent.dereference(&rcu_read).unwrap();
  229. }
  230. buffer.fill(b"/")?.ok_or(ERANGE)?;
  231. for item in path.iter().rev().map(|name| name.as_ref()) {
  232. buffer.fill(item)?.ok_or(ERANGE)?;
  233. buffer.fill(b"/")?.ok_or(ERANGE)?;
  234. }
  235. buffer.fill(&[0])?.ok_or(ERANGE)?;
  236. Ok(())
  237. }
  238. }
  239. impl Dentry {
  240. pub async fn read(&self, buffer: &mut dyn Buffer, offset: usize) -> KResult<usize> {
  241. let inode = self.get_inode()?;
  242. // Safety: Changing mode alone will have no effect on the file's contents
  243. match inode.format() {
  244. Format::DIR => Err(EISDIR),
  245. Format::REG => inode.read(buffer, offset).await,
  246. Format::BLK => {
  247. let device = BlockDevice::get(inode.devid()?)?;
  248. Ok(device.read_some(offset, buffer).await?.allow_partial())
  249. }
  250. Format::CHR => {
  251. let device = CharDevice::get(inode.devid()?).ok_or(EPERM)?;
  252. device.read(buffer)
  253. }
  254. _ => Err(EINVAL),
  255. }
  256. }
  257. pub async fn write(&self, stream: &mut dyn Stream, offset: WriteOffset<'_>) -> KResult<usize> {
  258. let inode = self.get_inode()?;
  259. // Safety: Changing mode alone will have no effect on the file's contents
  260. match inode.format() {
  261. Format::DIR => Err(EISDIR),
  262. Format::REG => inode.write(stream, offset).await,
  263. Format::BLK => Err(EINVAL), // TODO
  264. Format::CHR => CharDevice::get(inode.devid()?).ok_or(EPERM)?.write(stream),
  265. _ => Err(EINVAL),
  266. }
  267. }
  268. pub async fn readdir<F>(&self, offset: usize, mut for_each_entry: F) -> KResult<KResult<usize>>
  269. where
  270. F: FnMut(&[u8], Ino) -> KResult<bool> + Send,
  271. {
  272. let dir = self.get_inode()?;
  273. dir.readdir(offset, &mut for_each_entry).await
  274. }
  275. pub async fn mkdir(&self, perm: Permission) -> KResult<()> {
  276. if self.get_inode().is_ok() {
  277. Err(EEXIST)
  278. } else {
  279. let dir = self.parent().get_inode()?;
  280. dir.mkdir(self, perm).await
  281. }
  282. }
  283. pub fn statx(&self, stat: &mut StatX, mask: u32) -> KResult<()> {
  284. self.get_inode()?.statx(stat, mask)
  285. }
  286. pub async fn truncate(&self, size: usize) -> KResult<()> {
  287. self.get_inode()?.truncate(size).await
  288. }
  289. pub async fn unlink(self: &Arc<Self>) -> KResult<()> {
  290. if self.get_inode().is_err() {
  291. Err(ENOENT)
  292. } else {
  293. let dir = self.parent().get_inode()?;
  294. dir.unlink(self).await
  295. }
  296. }
  297. pub async fn symlink(self: &Arc<Self>, link: &[u8]) -> KResult<()> {
  298. if self.get_inode().is_ok() {
  299. Err(EEXIST)
  300. } else {
  301. let dir = self.parent().get_inode()?;
  302. dir.symlink(self, link).await
  303. }
  304. }
  305. pub async fn readlink(&self, buffer: &mut dyn Buffer) -> KResult<usize> {
  306. self.get_inode()?.readlink(buffer).await
  307. }
  308. pub async fn mknod(&self, mode: Mode, devid: DeviceId) -> KResult<()> {
  309. if self.get_inode().is_ok() {
  310. Err(EEXIST)
  311. } else {
  312. let dir = self.parent().get_inode()?;
  313. dir.mknod(self, mode, devid).await
  314. }
  315. }
  316. pub async fn chmod(&self, mode: Mode) -> KResult<()> {
  317. self.get_inode()?.chmod(mode).await
  318. }
  319. pub async fn chown(&self, uid: u32, gid: u32) -> KResult<()> {
  320. self.get_inode()?.chown(uid, gid).await
  321. }
  322. pub async fn rename(self: &Arc<Self>, new: &Arc<Self>, flags: RenameFlags) -> KResult<()> {
  323. if Arc::ptr_eq(self, new) {
  324. return Ok(());
  325. }
  326. let old_parent = self.parent().get_inode()?;
  327. let new_parent = new.parent().get_inode()?;
  328. // If the two dentries are not in the same filesystem, return EXDEV.
  329. if old_parent.sbref().eq(&new_parent.sbref()) {
  330. Err(PosixError::EXDEV)?;
  331. }
  332. let rename_data = RenameData {
  333. old_dentry: self,
  334. new_dentry: new,
  335. new_parent,
  336. is_exchange: flags.contains(RenameFlags::RENAME_EXCHANGE),
  337. no_replace: flags.contains(RenameFlags::RENAME_NOREPLACE),
  338. };
  339. // Delegate to the parent directory's rename implementation
  340. old_parent.rename(rename_data).await
  341. }
  342. }
  343. impl DentryKind {
  344. fn into_raw(self) -> u8 {
  345. unsafe { core::mem::transmute(self) }
  346. }
  347. fn from_raw(raw: u8) -> Option<Self> {
  348. unsafe { core::mem::transmute(raw) }
  349. }
  350. fn as_atomic(me: &UnsafeCell<Option<Self>>) -> &AtomicU8 {
  351. unsafe { AtomicU8::from_ptr(me.get().cast()) }
  352. }
  353. fn atomic_acq(me: &UnsafeCell<Option<Self>>) -> Option<Self> {
  354. Self::from_raw(Self::as_atomic(me).load(Ordering::Acquire))
  355. }
  356. fn atomic_swap_acqrel(me: &UnsafeCell<Option<Self>>, kind: Option<Self>) -> Option<Self> {
  357. Self::from_raw(Self::as_atomic(me).swap(kind.map_or(0, Self::into_raw), Ordering::AcqRel))
  358. }
  359. }
  360. impl AssociatedInode {
  361. fn new() -> Self {
  362. Self {
  363. inode: UnsafeCell::new(None),
  364. kind: UnsafeCell::new(None),
  365. }
  366. }
  367. fn store(&self, inode: InodeUse<dyn Inode>) {
  368. let kind = match inode.format() {
  369. Format::REG | Format::BLK | Format::CHR => DentryKind::Regular,
  370. Format::DIR => DentryKind::Directory,
  371. Format::LNK => DentryKind::Symlink,
  372. };
  373. unsafe {
  374. // SAFETY: We should be the first and only one to store the inode as
  375. // is checked below. All other readers reading non-invalid
  376. // kind will see the fully written inode.
  377. self.inode.get().write(Some(inode));
  378. }
  379. assert_eq!(
  380. DentryKind::atomic_swap_acqrel(&self.kind, Some(kind)),
  381. None,
  382. "Dentry can only be stored once."
  383. );
  384. }
  385. fn kind(&self) -> Option<DentryKind> {
  386. DentryKind::atomic_acq(&self.kind)
  387. }
  388. fn load(&self) -> Option<(DentryKind, &InodeUse<dyn Inode>)> {
  389. self.kind().map(|kind| unsafe {
  390. let inode = (&*self.inode.get())
  391. .as_ref()
  392. .expect("Dentry with non-invalid kind has no inode");
  393. (kind, inode)
  394. })
  395. }
  396. }
  397. unsafe impl Send for AssociatedInode {}
  398. unsafe impl Sync for AssociatedInode {}