blocktree.rs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. pub use private::{Blocktree, ModeAuthorizer};
  2. mod private {
  3. use btserde::{read_from, write_to};
  4. use fuse_backend_rs::{
  5. abi::fuse_abi::{stat64, statvfs64, CreateIn},
  6. api::filesystem::{
  7. Context, DirEntry as FuseDirEntry, Entry, FileSystem, FsOptions, OpenOptions,
  8. SetattrValid,
  9. },
  10. };
  11. use log::{debug, error, warn};
  12. use serde::{Deserialize, Serialize};
  13. use std::{
  14. collections::hash_map::{self, HashMap},
  15. ffi::CStr,
  16. fmt::{Display, Formatter},
  17. fs::File,
  18. io::{self, SeekFrom, Write},
  19. path::{Path, PathBuf},
  20. sync::{
  21. atomic::{AtomicU64, Ordering},
  22. RwLock,
  23. },
  24. time::Duration,
  25. };
  26. use crate::{
  27. bterr,
  28. crypto::Creds,
  29. error::{DisplayErr, IoErr},
  30. Block, BlockError, BlockMeta, BlockOpenOptions, BlockPath, BlockRecord, BoxInIoErr,
  31. DirEntry, DirEntryKind, Directory, Epoch, Result,
  32. };
  33. type Inode = u64;
  34. type Handle = u64;
  35. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
  36. pub enum Error {
  37. NotOpen(Inode),
  38. InvalidHandle { handle: u64, inode: u64 },
  39. NoHandlesAvailable(Inode),
  40. InodeNotFound(Inode),
  41. }
  42. impl Display for Error {
  43. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  44. match self {
  45. Error::NotOpen(inode) => write!(f, "inode {inode} is not open"),
  46. Error::InvalidHandle { handle, inode } => {
  47. write!(f, "invalid handle {handle} for inode {inode}")
  48. }
  49. Error::NoHandlesAvailable(inode) => {
  50. write!(f, "no handles are available for inode {inode}")
  51. }
  52. Error::InodeNotFound(inode) => write!(f, "inode {inode} could not be found"),
  53. }
  54. }
  55. }
  56. impl std::error::Error for Error {}
  57. #[repr(u64)]
  58. pub enum SpecInodes {
  59. RootDir = 1,
  60. Sb = 2,
  61. FirstFree = 11,
  62. }
  63. impl From<SpecInodes> for Inode {
  64. fn from(special: SpecInodes) -> Self {
  65. special as Inode
  66. }
  67. }
  68. #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
  69. #[repr(u32)] // This type needs to match `libc::mode_t`.
  70. /// The type of a file (regular, directory, etc).
  71. enum FileType {
  72. /// Directory.
  73. Dir = libc::S_IFDIR,
  74. /// Regular file.
  75. Reg = libc::S_IFREG,
  76. }
  77. impl FileType {
  78. /// Returns the underlying mode bits for this file type.
  79. fn value(self) -> libc::mode_t {
  80. self as libc::mode_t
  81. }
  82. /// Attempts to convert the given mode bits into a `FileType` enum value.
  83. fn from_value(value: libc::mode_t) -> Result<Self> {
  84. if (value & libc::S_IFDIR) != 0 {
  85. return Ok(FileType::Dir);
  86. }
  87. if (value & libc::S_IFREG) != 0 {
  88. return Ok(FileType::Reg);
  89. }
  90. Err(bterr!("unknown file type: 0o{value:0o}"))
  91. }
  92. fn dir_entry_kind(self) -> DirEntryKind {
  93. match self {
  94. Self::Dir => DirEntryKind::Directory,
  95. Self::Reg => DirEntryKind::File,
  96. }
  97. }
  98. }
  99. impl From<FileType> for libc::mode_t {
  100. fn from(file_type: FileType) -> Self {
  101. file_type.value()
  102. }
  103. }
  104. impl TryFrom<libc::mode_t> for FileType {
  105. type Error = crate::Error;
  106. fn try_from(value: libc::mode_t) -> Result<Self> {
  107. Self::from_value(value)
  108. }
  109. }
  110. impl From<FileType> for DirEntryKind {
  111. fn from(value: FileType) -> Self {
  112. value.dir_entry_kind()
  113. }
  114. }
  115. trait SeekFromExt {
  116. /// Converts a C-style `(whence, offset)` pair into a [SeekFrom] enum value.
  117. /// See the POSIX man page of `lseek` for more details.
  118. fn whence_offset(whence: u32, offset: u64) -> io::Result<SeekFrom> {
  119. let whence = whence as i32;
  120. match whence {
  121. libc::SEEK_SET => Ok(SeekFrom::Start(offset)),
  122. libc::SEEK_CUR => Ok(SeekFrom::Current(offset as i64)),
  123. libc::SEEK_END => Ok(SeekFrom::End(offset as i64)),
  124. _ => Err(io::Error::new(
  125. io::ErrorKind::InvalidInput,
  126. "`whence` was not one of `libc::{SEEK_SET, SEEK_CUR, SEEK_END}`",
  127. )),
  128. }
  129. }
  130. }
  131. impl SeekFromExt for SeekFrom {}
  132. /// This type provides context for an authorization decision as to whether a given process will
  133. /// be allowed to access a block.
  134. pub struct AuthzContext<'a> {
  135. /// The user ID of the process being authorized.
  136. pub uid: u32,
  137. /// The group ID of the process being authorized.
  138. pub gid: u32,
  139. /// The process ID of the process being authorized.
  140. pub pid: libc::pid_t,
  141. /// A reference to the metadata of a block, the access to which is being authorized.
  142. pub meta: &'a BlockMeta,
  143. }
  144. impl<'a> AuthzContext<'a> {
  145. pub fn new(ctx: &Context, meta: &'a BlockMeta) -> AuthzContext<'a> {
  146. AuthzContext {
  147. uid: ctx.uid,
  148. gid: ctx.gid,
  149. pid: ctx.pid,
  150. meta,
  151. }
  152. }
  153. }
  154. /// A trait for types which can render authorization decisions.
  155. pub trait Authorizer {
  156. /// Returns [Ok] if read authorization is granted, and [Err] otherwise.
  157. fn can_read<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()>;
  158. /// Returns [Ok] if write authorization is granted, and [Err] otherwise.
  159. fn can_write<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()>;
  160. /// Returns [Ok] if execute authorization is granted, and [Err] otherwise.
  161. fn can_exec<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()>;
  162. }
  163. trait AuthorizerExt: Authorizer {
  164. fn read_allowed(&self, ctx: &Context, meta: &BlockMeta) -> io::Result<()> {
  165. Authorizer::can_read(self, &AuthzContext::new(ctx, meta))
  166. }
  167. fn write_allowed(&self, ctx: &Context, meta: &BlockMeta) -> io::Result<()> {
  168. Authorizer::can_write(self, &AuthzContext::new(ctx, meta))
  169. }
  170. fn exec_allowed(&self, ctx: &Context, meta: &BlockMeta) -> io::Result<()> {
  171. Authorizer::can_exec(self, &AuthzContext::new(ctx, meta))
  172. }
  173. }
  174. impl<T: Authorizer> AuthorizerExt for T {}
  175. /// A particularly simple authorizer that just looks at the mode bits in the block metadata
  176. /// to make authorization decisions.
  177. pub struct ModeAuthorizer {}
  178. impl ModeAuthorizer {
  179. fn authorize(mode: u32, mask: u32, denied_msg: &str) -> io::Result<()> {
  180. if (mode & mask) != 0 {
  181. Ok(())
  182. } else {
  183. Err(io::Error::new(io::ErrorKind::PermissionDenied, denied_msg))
  184. }
  185. }
  186. fn user_is_root(ctx: &AuthzContext<'_>) -> bool {
  187. ctx.uid == 0
  188. }
  189. }
  190. impl Authorizer for ModeAuthorizer {
  191. fn can_read<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()> {
  192. if Self::user_is_root(ctx) {
  193. return Ok(());
  194. }
  195. let secrets = ctx.meta.body.secrets()?;
  196. let mask = (libc::S_IRUSR * (secrets.uid == ctx.uid) as u32)
  197. | (libc::S_IRGRP * (secrets.gid == ctx.gid) as u32)
  198. | libc::S_IROTH;
  199. Self::authorize(secrets.mode, mask, "read access denied")
  200. }
  201. fn can_write<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()> {
  202. if Self::user_is_root(ctx) {
  203. return Ok(());
  204. }
  205. let secrets = ctx.meta.body.secrets()?;
  206. let mask = (libc::S_IWUSR * (secrets.uid == ctx.uid) as u32)
  207. | (libc::S_IWGRP * (secrets.gid == ctx.gid) as u32)
  208. | libc::S_IWOTH;
  209. Self::authorize(secrets.mode, mask, "write access denied")
  210. }
  211. fn can_exec<'a>(&self, ctx: &AuthzContext<'a>) -> io::Result<()> {
  212. if Self::user_is_root(ctx) {
  213. return Ok(());
  214. }
  215. let secrets = ctx.meta.body.secrets()?;
  216. let mask = (libc::S_IXUSR * (secrets.uid == ctx.uid) as u32)
  217. | (libc::S_IXGRP * (secrets.gid == ctx.gid) as u32)
  218. | libc::S_IXOTH;
  219. Self::authorize(secrets.mode, mask, "exec access denied")
  220. }
  221. }
  222. enum HandleValue {
  223. File {
  224. block: RwLock<Box<dyn Block>>,
  225. },
  226. Directory {
  227. dir: Directory,
  228. block: RwLock<Box<dyn Block>>,
  229. },
  230. }
  231. impl HandleValue {
  232. fn new(block: Box<dyn Block>) -> HandleValue {
  233. HandleValue::File {
  234. block: RwLock::new(block),
  235. }
  236. }
  237. fn convert_to_dir(self) -> io::Result<HandleValue> {
  238. let lock = self.take_block();
  239. let dir = {
  240. let mut guard = lock.write().display_err()?;
  241. guard.seek(SeekFrom::Start(0))?;
  242. read_from(&mut *guard)?
  243. };
  244. Ok(HandleValue::Directory { dir, block: lock })
  245. }
  246. fn take_block(self) -> RwLock<Box<dyn Block>> {
  247. match self {
  248. Self::File { block, .. } => block,
  249. Self::Directory { block, .. } => block,
  250. }
  251. }
  252. fn block(&self) -> &RwLock<Box<dyn Block>> {
  253. match self {
  254. Self::File { block, .. } => block,
  255. Self::Directory { block, .. } => block,
  256. }
  257. }
  258. fn block_mut(&mut self) -> io::Result<&mut Box<dyn Block>> {
  259. let lock = match self {
  260. Self::File { block, .. } => block,
  261. Self::Directory { block, .. } => block,
  262. };
  263. lock.get_mut().display_err().io_err()
  264. }
  265. fn access_block<T, F: FnOnce(&Box<dyn Block>) -> io::Result<T>>(
  266. &self,
  267. cb: F,
  268. ) -> io::Result<T> {
  269. let guard = self.block().read().display_err()?;
  270. cb(&guard)
  271. }
  272. fn access_block_mut<T, F: FnOnce(&mut Box<dyn Block>) -> io::Result<T>>(
  273. &self,
  274. cb: F,
  275. ) -> io::Result<T> {
  276. let mut guard = self.block().write().display_err()?;
  277. cb(&mut guard)
  278. }
  279. fn directory(&self) -> io::Result<&Directory> {
  280. match self {
  281. Self::Directory { dir, .. } => Ok(dir),
  282. _ => Err(io::Error::new(
  283. io::ErrorKind::Other,
  284. "handle is not for a directory",
  285. )),
  286. }
  287. }
  288. }
  289. struct InodeTableValue {
  290. handle_values: HashMap<Handle, HandleValue>,
  291. next_handle: Handle,
  292. unclaimed_handles: Vec<Handle>,
  293. lookup_count: u64,
  294. delete: bool,
  295. }
  296. impl InodeTableValue {
  297. /// If more than this number of unclaimed blocks are open, then blocks are closed until
  298. /// only this number remain open.
  299. const UNCLAIMED_HANDLE_LIMIT: usize = 3;
  300. fn new(block: Box<dyn Block>) -> InodeTableValue {
  301. const FIRST_HANDLE: Handle = 1;
  302. let mut handles = HashMap::with_capacity(1);
  303. handles.insert(FIRST_HANDLE, HandleValue::new(block));
  304. Self {
  305. handle_values: handles,
  306. next_handle: FIRST_HANDLE + 1,
  307. lookup_count: 1,
  308. unclaimed_handles: vec![FIRST_HANDLE],
  309. delete: false,
  310. }
  311. }
  312. fn invalid_handle_err(handle: Handle) -> io::Error {
  313. io::Error::new(io::ErrorKind::Other, format!("invalid handle {handle}"))
  314. }
  315. fn value(&self, handle: Handle) -> io::Result<&HandleValue> {
  316. self.handle_values
  317. .get(&handle)
  318. .ok_or_else(|| Self::invalid_handle_err(handle))
  319. }
  320. fn value_mut(&mut self, handle: Handle) -> io::Result<&mut HandleValue> {
  321. self.handle_values
  322. .get_mut(&handle)
  323. .ok_or_else(|| Self::invalid_handle_err(handle))
  324. }
  325. fn block_mut(&mut self, handle: Handle) -> io::Result<&mut Box<dyn Block>> {
  326. self.value_mut(handle)?.block_mut()
  327. }
  328. fn convert_to_dir(&mut self, handle: Handle) -> io::Result<()> {
  329. let value = self
  330. .handle_values
  331. .remove(&handle)
  332. .ok_or_else(|| Self::invalid_handle_err(handle))?;
  333. let value = value.convert_to_dir()?;
  334. self.handle_values.insert(handle, value);
  335. Ok(())
  336. }
  337. fn access_block_mut<T, F: FnOnce(&mut Box<dyn Block>) -> io::Result<T>>(
  338. &self,
  339. handle: Handle,
  340. cb: F,
  341. ) -> io::Result<T> {
  342. self.value(handle)?.access_block_mut(cb)
  343. }
  344. fn borrow_block<T, F: FnOnce(&mut Box<dyn Block>) -> io::Result<T>>(
  345. &mut self,
  346. cb: F,
  347. ) -> io::Result<T> {
  348. let handle = self
  349. .unclaimed_handles
  350. .last()
  351. .ok_or_else(|| bterr!("no handles available"))?;
  352. let value = self.value_mut(*handle)?;
  353. let block = value.block_mut()?;
  354. cb(block)
  355. }
  356. fn insert(&mut self, block: Box<dyn Block>) {
  357. let handle = self.next_handle;
  358. self.next_handle += 1;
  359. self.handle_values.insert(handle, HandleValue::new(block));
  360. self.unclaimed_handles.push(handle);
  361. }
  362. fn insert_then_get_mut(&mut self, block: Box<dyn Block>) -> &mut Box<dyn Block> {
  363. self.insert(block);
  364. let handle = self.unclaimed_handles.last().unwrap();
  365. self.block_mut(*handle).unwrap()
  366. }
  367. fn take_handle(&mut self) -> io::Result<Handle> {
  368. self.unclaimed_handles
  369. .pop()
  370. .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "no unclaimed handles"))
  371. }
  372. fn give_handle(&mut self, handle: Handle) {
  373. self.unclaimed_handles.push(handle);
  374. while self.unclaimed_handles.len() > Self::UNCLAIMED_HANDLE_LIMIT {
  375. let handle = self.unclaimed_handles.pop().unwrap();
  376. self.handle_values.remove(&handle);
  377. }
  378. }
  379. /// Increments `lookup_count` by 1 and returns its current value.
  380. fn incr_lookup_count(&mut self) -> u64 {
  381. self.lookup_count += 1;
  382. self.lookup_count
  383. }
  384. /// Decrements `lookup_count` by `count` and returns its current value.
  385. fn decr_lookup_count(&mut self, count: u64) -> u64 {
  386. self.lookup_count -= count;
  387. self.lookup_count
  388. }
  389. }
  390. type InodeTable = HashMap<Inode, RwLock<InodeTableValue>>;
  391. type InodeTableEntry<'a> = hash_map::Entry<'a, Inode, RwLock<InodeTableValue>>;
  392. /// Structure for metadata about a blocktree.
  393. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
  394. struct Superblock {
  395. /// The generation number of the cluster this part of the blocktree is stored on.
  396. generation: u64,
  397. /// The next free inode available to the cluster.
  398. next_inode: u64,
  399. }
  400. /// Structure for managing the part of a blocktree which is stored in the local filesystem.
  401. pub struct Blocktree<C, A> {
  402. /// The path to the directory in the local filesystem where this blocktree is located.
  403. path: PathBuf,
  404. /// A map from inode numbers to their reference counts.
  405. inodes: RwLock<InodeTable>,
  406. /// The next inode that will be assigned to a new block.
  407. next_inode: AtomicU64,
  408. /// The generation number of this filesystem. This is the same for every other server in
  409. /// the same cluster.
  410. generation: u64,
  411. /// The credentials this blocktree instance will use for all cryptographic operations.
  412. creds: C,
  413. authorizer: A,
  414. }
  415. impl<C: Creds + 'static, A> Blocktree<C, A> {
  416. /// Creates a new empty blocktree at the given path.
  417. pub fn new_empty(
  418. btdir: PathBuf,
  419. generation: u64,
  420. creds: C,
  421. authorizer: A,
  422. ) -> Result<Blocktree<C, A>> {
  423. let root_block_path = creds
  424. .writecap()
  425. .ok_or(BlockError::MissingWritecap)?
  426. .root_block_path();
  427. // Initialize the superblock.
  428. let mut sb_block = Self::open_block(
  429. &btdir,
  430. SpecInodes::Sb.into(),
  431. creds.clone(),
  432. root_block_path.to_owned(),
  433. )?;
  434. let sb = Superblock {
  435. generation,
  436. next_inode: SpecInodes::FirstFree.into(),
  437. };
  438. write_to(&sb, &mut sb_block)?;
  439. sb_block.mut_meta_body().access_secrets(|secrets| {
  440. secrets.block_id.generation = generation;
  441. secrets.block_id.inode = SpecInodes::Sb.into();
  442. secrets.mode = FileType::Reg.value() | 0o666;
  443. secrets.uid = 0;
  444. secrets.gid = 0;
  445. secrets.nlink = 1;
  446. Ok(())
  447. })?;
  448. sb_block.flush()?;
  449. // Initialize the root directory.
  450. let mut root_block = Self::open_block(
  451. &btdir,
  452. SpecInodes::RootDir.into(),
  453. creds.clone(),
  454. root_block_path,
  455. )?;
  456. write_to(&Directory::new(), &mut root_block)?;
  457. root_block.mut_meta_body().access_secrets(|secrets| {
  458. secrets.block_id.generation = generation;
  459. secrets.block_id.inode = SpecInodes::RootDir.into();
  460. secrets.mode = FileType::Dir.value() | 0o777;
  461. secrets.uid = 0;
  462. secrets.gid = 0;
  463. secrets.nlink = 1;
  464. Ok(())
  465. })?;
  466. root_block.flush()?;
  467. Self::new(btdir, sb, sb_block, root_block, creds, authorizer)
  468. }
  469. /// Opens an existing blocktree stored at the given path.
  470. pub fn new_existing(btdir: PathBuf, creds: C, authorizer: A) -> Result<Blocktree<C, A>> {
  471. let root_block_path = creds
  472. .writecap()
  473. .ok_or(BlockError::MissingWritecap)?
  474. .root_block_path();
  475. let mut sb_block = Self::open_block(
  476. &btdir,
  477. SpecInodes::Sb.into(),
  478. creds.clone(),
  479. root_block_path.to_owned(),
  480. )?;
  481. let sb = read_from(&mut sb_block)?;
  482. let root_block = Self::open_block(
  483. &btdir,
  484. SpecInodes::RootDir.into(),
  485. creds.clone(),
  486. root_block_path,
  487. )?;
  488. Self::new(btdir, sb, sb_block, root_block, creds, authorizer)
  489. }
  490. fn new(
  491. btdir: PathBuf,
  492. sb: Superblock,
  493. sb_block: Box<dyn Block>,
  494. root_block: Box<dyn Block>,
  495. creds: C,
  496. authorizer: A,
  497. ) -> Result<Blocktree<C, A>> {
  498. let mut inodes = HashMap::with_capacity(1);
  499. inodes.insert(
  500. SpecInodes::Sb.into(),
  501. RwLock::new(InodeTableValue::new(sb_block)),
  502. );
  503. inodes.insert(
  504. SpecInodes::RootDir.into(),
  505. RwLock::new(InodeTableValue::new(root_block)),
  506. );
  507. Ok(Blocktree {
  508. path: btdir,
  509. inodes: RwLock::new(inodes),
  510. next_inode: AtomicU64::new(sb.next_inode),
  511. generation: sb.generation,
  512. creds,
  513. authorizer,
  514. })
  515. }
  516. /// Returns the path to the file storing the given inode's data.
  517. fn block_path<P: AsRef<Path>>(parent: P, inode: Inode) -> PathBuf {
  518. let group = inode / 0xFF;
  519. let mut path = PathBuf::new();
  520. path.push(parent);
  521. path.push(format!("{group:02x}"));
  522. path.push(format!("{inode:x}.blk"));
  523. path
  524. }
  525. fn open_block<P: AsRef<Path>>(
  526. btdir: P,
  527. inode: Inode,
  528. creds: C,
  529. block_path: BlockPath,
  530. ) -> io::Result<Box<dyn Block>> {
  531. let path = Self::block_path(&btdir, inode);
  532. let dir = path.ancestors().nth(1).unwrap();
  533. if let Err(err) = std::fs::create_dir(dir) {
  534. match err.kind() {
  535. io::ErrorKind::AlreadyExists => (),
  536. _ => return Err(err),
  537. }
  538. }
  539. let file = std::fs::OpenOptions::new()
  540. .read(true)
  541. .write(true)
  542. .create(true)
  543. .open(path)?;
  544. Self::open_block_file(file, creds, block_path)
  545. }
  546. fn open_block_file(
  547. file: File,
  548. creds: C,
  549. block_path: BlockPath,
  550. ) -> io::Result<Box<dyn Block>> {
  551. BlockOpenOptions::new()
  552. .with_creds(creds)
  553. .with_compress(false)
  554. .with_encrypt(true)
  555. .with_inner(file)
  556. .with_block_path(block_path)
  557. .open()
  558. .io_err()
  559. }
  560. /// Returns the [Err] variant containing the [io::Error] corresponding to [libc::ENOSYS].
  561. fn not_supported<T>() -> io::Result<T> {
  562. let err = io::Error::from_raw_os_error(libc::ENOSYS);
  563. debug!("{err}");
  564. Err(err)
  565. }
  566. fn access_entry<T, F: FnOnce(InodeTableEntry) -> io::Result<T>>(
  567. &self,
  568. inode: Inode,
  569. cb: F,
  570. ) -> io::Result<T> {
  571. let mut inodes = self.inodes.write().display_err()?;
  572. let entry = inodes.entry(inode);
  573. cb(entry)
  574. }
  575. fn access_value<T, F: FnOnce(&InodeTableValue) -> io::Result<T>>(
  576. &self,
  577. inode: Inode,
  578. cb: F,
  579. ) -> io::Result<T> {
  580. let inodes = self.inodes.read().display_err()?;
  581. let guard = inodes
  582. .get(&inode)
  583. .ok_or_else(|| bterr!(Error::NotOpen(inode)))?
  584. .read()
  585. .display_err()?;
  586. cb(&guard)
  587. }
  588. fn access_value_mut<T, F: FnOnce(&mut InodeTableValue) -> io::Result<T>>(
  589. &self,
  590. inode: Inode,
  591. cb: F,
  592. ) -> io::Result<T> {
  593. let inodes = self.inodes.read().display_err()?;
  594. let mut guard = inodes
  595. .get(&inode)
  596. .ok_or_else(|| bterr!(Error::NotOpen(inode)))?
  597. .write()
  598. .display_err()?;
  599. cb(&mut guard)
  600. }
  601. fn access_block_mut<T, F: FnOnce(&mut Box<dyn Block>) -> io::Result<T>>(
  602. &self,
  603. inode: Inode,
  604. handle: Handle,
  605. cb: F,
  606. ) -> io::Result<T> {
  607. self.access_value(inode, |value| value.access_block_mut(handle, cb))
  608. }
  609. fn access_meta<T, F: FnOnce(&BlockMeta) -> io::Result<T>>(
  610. &self,
  611. inode: Inode,
  612. cb: F,
  613. ) -> io::Result<T> {
  614. self.access_value(inode, |value| {
  615. let handle_value = value
  616. .handle_values
  617. .values()
  618. .next()
  619. .ok_or_else(|| bterr!(Error::NotOpen(inode)))?;
  620. // Because we're using any of the meta data structs we need to ensure that any
  621. // modification of meta data is performed on all open blocks.
  622. handle_value.access_block(|block| cb(block.meta()))
  623. })
  624. }
  625. fn borrow_block<T, F: FnOnce(&mut Box<dyn Block>) -> io::Result<T>>(
  626. &self,
  627. inode: Inode,
  628. cb: F,
  629. ) -> io::Result<T> {
  630. self.access_value_mut(inode, |value| {
  631. let block = match value.unclaimed_handles.last() {
  632. Some(handle) => value.handle_values.get_mut(handle).unwrap().block_mut()?,
  633. None => {
  634. let block_path = value
  635. .handle_values
  636. .values()
  637. .next()
  638. .ok_or_else(|| bterr!(Error::NotOpen(inode)))?
  639. .access_block(|block| Ok(block.meta_body().path.clone()))?;
  640. let block =
  641. Self::open_block(&self.path, inode, self.creds.clone(), block_path)?;
  642. value.insert_then_get_mut(block)
  643. }
  644. };
  645. cb(block)
  646. })
  647. }
  648. fn take_handle_if_ok<T, F: FnOnce(Handle, &mut Box<dyn Block>) -> io::Result<T>>(
  649. &self,
  650. inode: Inode,
  651. cb: F,
  652. ) -> io::Result<T> {
  653. self.access_value_mut(inode, |value| {
  654. let handle = value
  655. .take_handle()
  656. .map_err(|_| bterr!(Error::NoHandlesAvailable(inode)))?;
  657. let block = value.block_mut(handle)?;
  658. let result = cb(handle, block);
  659. if result.is_err() {
  660. value.give_handle(handle);
  661. }
  662. result
  663. })
  664. }
  665. fn open_value<T, F: FnOnce(&mut InodeTableValue) -> io::Result<T>>(
  666. &self,
  667. inode: Inode,
  668. block_path: BlockPath,
  669. cb: F,
  670. ) -> io::Result<T> {
  671. self.access_entry(inode, |entry| match entry {
  672. InodeTableEntry::Vacant(entry) => {
  673. let block =
  674. Self::open_block(&self.path, inode, self.creds.clone(), block_path)?;
  675. let mut value = InodeTableValue::new(block);
  676. let result = cb(&mut value);
  677. entry.insert(RwLock::new(value));
  678. result
  679. }
  680. InodeTableEntry::Occupied(mut entry) => {
  681. let value = entry.get_mut().get_mut().display_err()?;
  682. if value.unclaimed_handles.is_empty() {
  683. let block =
  684. Self::open_block(&self.path, inode, self.creds.clone(), block_path)?;
  685. value.insert(block);
  686. }
  687. cb(value)
  688. }
  689. })
  690. }
  691. fn open_then_take_handle<T, F: FnOnce(Handle, &mut InodeTableValue) -> io::Result<T>>(
  692. &self,
  693. inode: Inode,
  694. block_path: BlockPath,
  695. cb: F,
  696. ) -> io::Result<T> {
  697. self.open_value(inode, block_path, |value| {
  698. let handle = value.take_handle().unwrap();
  699. cb(handle, value)
  700. })
  701. }
  702. fn inode_forget(&self, inode: Inode, count: u64) -> io::Result<()> {
  703. let mut inodes = self.inodes.write().display_err()?;
  704. let lookup_count = {
  705. let inode_lock = match inodes.get_mut(&inode) {
  706. Some(inode_lock) => inode_lock,
  707. None => {
  708. warn!("an attempt was made to forget non-existent inode {inode}");
  709. return Ok(());
  710. }
  711. };
  712. let mut value = inode_lock.write().display_err()?;
  713. value.decr_lookup_count(count)
  714. };
  715. if 0 == lookup_count {
  716. let delete = inodes
  717. .remove(&inode)
  718. .unwrap()
  719. .into_inner()
  720. .display_err()?
  721. .delete;
  722. if delete {
  723. let path = Self::block_path(&self.path, inode);
  724. std::fs::remove_file(path)?;
  725. }
  726. }
  727. Ok(())
  728. }
  729. /// Returns the next available inode and updates the superblock in one atomic operation.
  730. /// TODO: Obviously this strategy won't work when there are multiple servers in this
  731. /// generation.
  732. fn next_inode(&self) -> io::Result<Inode> {
  733. self.borrow_block(SpecInodes::Sb.into(), |block| {
  734. // We don't need strict ordering because the lock the inode table is already
  735. // serializing access.
  736. let inode = self.next_inode.fetch_add(1, Ordering::Relaxed);
  737. let sb = Superblock {
  738. generation: self.generation,
  739. next_inode: inode + 1,
  740. };
  741. block.seek(SeekFrom::Start(0))?;
  742. write_to(&sb, block)?;
  743. Ok(inode)
  744. })
  745. }
  746. fn attr_timeout(&self) -> Duration {
  747. Duration::from_secs(5)
  748. }
  749. fn entry_timeout(&self) -> Duration {
  750. Duration::from_secs(5)
  751. }
  752. fn unsupported_flag<T>(flag: &str) -> io::Result<T> {
  753. Err(io::Error::new(
  754. io::ErrorKind::Unsupported,
  755. format!("unsupported flag: {flag}"),
  756. ))
  757. }
  758. fn fuse_entry(&self, inode: Inode, stat: stat64) -> Entry {
  759. Entry {
  760. generation: self.generation,
  761. inode,
  762. attr: stat,
  763. attr_flags: 0,
  764. attr_timeout: self.attr_timeout(),
  765. entry_timeout: self.entry_timeout(),
  766. }
  767. }
  768. }
  769. unsafe impl<C: Sync, A: Sync> Sync for Blocktree<C, A> {}
  770. impl<C: Creds + Clone + 'static, A: Authorizer> FileSystem for Blocktree<C, A> {
  771. type Inode = Inode;
  772. type Handle = u64;
  773. fn init(&self, _capable: FsOptions) -> io::Result<FsOptions> {
  774. debug!("Blocktree::init called");
  775. Ok(FsOptions::empty())
  776. }
  777. fn destroy(&self) {
  778. debug!("Blocktree::destroy called");
  779. }
  780. fn lookup(&self, ctx: &Context, parent: Self::Inode, name: &CStr) -> io::Result<Entry> {
  781. debug!("Blocktree::lookup called on parent {parent}");
  782. let name = name.to_str().box_err()?;
  783. let (dir, block_path) = self.borrow_block(parent, |block| {
  784. self.authorizer.exec_allowed(ctx, block.meta())?;
  785. let dir = block.read_dir()?;
  786. let path = block.meta_body().path.to_owned();
  787. Ok((dir, path))
  788. })?;
  789. let entry = dir
  790. .entries
  791. .get(name)
  792. .ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?;
  793. let inode = entry.inode().ok_or_else(|| {
  794. io::Error::new(io::ErrorKind::Unsupported, "can't lookup server entry")
  795. })?;
  796. let stat = self.open_value(inode, block_path, |value| {
  797. let stat = value.borrow_block(|block| Ok(block.meta_body().secrets()?.stat()))?;
  798. value.incr_lookup_count();
  799. Ok(stat)
  800. })?;
  801. Ok(self.fuse_entry(inode, stat))
  802. }
  803. fn open(
  804. &self,
  805. ctx: &Context,
  806. inode: Self::Inode,
  807. flags: u32,
  808. // This is the second field of the `fuse_open_in` struct, which is currently unused
  809. // by the kernel. (https://man7.org/linux/man-pages/man4/fuse.4.html)
  810. _fuse_flags: u32,
  811. ) -> io::Result<(Option<Self::Handle>, OpenOptions)> {
  812. debug!("Blocktree::open called on inode {inode}");
  813. let flags: i32 = flags.try_into().box_err()?;
  814. if flags & libc::O_APPEND != 0 {
  815. return Self::unsupported_flag("O_APPEND");
  816. }
  817. if flags & libc::O_CLOEXEC != 0 {
  818. return Self::unsupported_flag("O_CLOEXEC");
  819. }
  820. if flags & libc::O_DIRECTORY != 0 {
  821. return Self::unsupported_flag("O_DIRECTORY");
  822. }
  823. let handle = self.take_handle_if_ok(inode, |handle, block| {
  824. let ctx = AuthzContext::new(ctx, block.meta());
  825. if flags == libc::O_RDONLY || (flags & libc::O_RDWR) != 0 {
  826. self.authorizer.can_read(&ctx)?;
  827. }
  828. let write_mask = libc::O_WRONLY | libc::O_RDWR;
  829. if write_mask & flags != 0 {
  830. self.authorizer.can_write(&ctx)?;
  831. }
  832. Ok(handle)
  833. })?;
  834. Ok((Some(handle), OpenOptions::empty()))
  835. }
  836. fn release(
  837. &self,
  838. _ctx: &Context,
  839. inode: Self::Inode,
  840. _flags: u32,
  841. handle: Self::Handle,
  842. flush: bool,
  843. _flock_release: bool,
  844. _lock_owner: Option<u64>,
  845. ) -> io::Result<()> {
  846. debug!("Blocktree::release called on inode {inode}");
  847. self.access_value_mut(inode, |value| {
  848. value.access_block_mut(handle, |block| {
  849. if flush {
  850. block.flush()?;
  851. }
  852. // Be kind, rewind.
  853. block.seek(SeekFrom::Start(0))
  854. })?;
  855. value.give_handle(handle);
  856. Ok(())
  857. })
  858. }
  859. fn opendir(
  860. &self,
  861. ctx: &Context,
  862. inode: Self::Inode,
  863. _flags: u32,
  864. ) -> io::Result<(Option<Self::Handle>, OpenOptions)> {
  865. debug!("Blocktree::opendir called on inode {inode}");
  866. let handle = self.access_value_mut(inode, |value| {
  867. value.borrow_block(|block| {
  868. self.authorizer.exec_allowed(ctx, block.meta())?;
  869. Ok(())
  870. })?;
  871. let handle = value.take_handle()?;
  872. value.convert_to_dir(handle)?;
  873. Ok(handle)
  874. })?;
  875. Ok((Some(handle), OpenOptions::empty()))
  876. }
  877. fn releasedir(
  878. &self,
  879. _ctx: &Context,
  880. inode: Self::Inode,
  881. _flags: u32,
  882. handle: Self::Handle,
  883. ) -> io::Result<()> {
  884. debug!("Blocktree::releasedir called for inode {inode}");
  885. self.access_value_mut(inode, |value| {
  886. value.give_handle(handle);
  887. Ok(())
  888. })
  889. }
  890. fn create(
  891. &self,
  892. ctx: &Context,
  893. parent: Self::Inode,
  894. name: &CStr,
  895. args: CreateIn,
  896. ) -> std::io::Result<(Entry, Option<Self::Handle>, OpenOptions)> {
  897. debug!("Blocktree::create called on parent {parent}");
  898. let name = name
  899. .to_str()
  900. .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?
  901. .to_owned();
  902. // Reserve a free inode.
  903. let inode = self.next_inode()?;
  904. // Add a directory entry to the parent for the new inode.
  905. let mut block_path = self.borrow_block(parent, |block| {
  906. self.authorizer.write_allowed(ctx, block.meta())?;
  907. let mut dir = block.read_dir()?;
  908. dir.add_file(name.clone(), inode)?;
  909. block.write_dir(&dir)?;
  910. Ok(block.meta_body().path.clone())
  911. })?;
  912. block_path.push_component(name);
  913. let (handle, stat) =
  914. self.open_then_take_handle(inode, block_path, |handle, value| {
  915. let block = value.block_mut(handle)?;
  916. Ok(block.mut_meta_body().access_secrets(|secrets| {
  917. secrets.block_id.generation = self.generation;
  918. secrets.block_id.inode = inode;
  919. secrets.mode = args.mode & !args.umask;
  920. secrets.uid = ctx.uid;
  921. secrets.gid = ctx.gid;
  922. let now = Epoch::now();
  923. secrets.atime = now;
  924. secrets.ctime = now;
  925. secrets.mtime = now;
  926. secrets.nlink = 1;
  927. Ok((handle, secrets.stat()))
  928. })?)
  929. })?;
  930. Ok((
  931. self.fuse_entry(inode, stat),
  932. Some(handle),
  933. OpenOptions::empty(),
  934. ))
  935. }
  936. fn write(
  937. &self,
  938. _ctx: &Context,
  939. inode: Self::Inode,
  940. handle: Self::Handle,
  941. r: &mut dyn fuse_backend_rs::api::filesystem::ZeroCopyReader,
  942. size: u32,
  943. _offset: u64,
  944. _lock_owner: Option<u64>,
  945. _delayed_write: bool,
  946. // `flags` and `fuse_flags` are the arguments that were passed to `open` when this
  947. // handle was returned.
  948. flags: u32,
  949. _fuse_flags: u32,
  950. ) -> io::Result<usize> {
  951. debug!("Blocktree::write called called on inode {inode}");
  952. if flags as libc::c_int == libc::O_RDONLY {
  953. return Err(io::Error::new(
  954. io::ErrorKind::PermissionDenied,
  955. "file is readonly",
  956. ));
  957. }
  958. let mut size: usize = size.try_into().box_err()?;
  959. self.access_block_mut(inode, handle, |block| {
  960. let mut buf = [0u8; crate::SECTOR_SZ_DEFAULT];
  961. let mut written = 0;
  962. while size > 0 {
  963. let read = match r.read(&mut buf) {
  964. Ok(size) => size,
  965. Err(err) => {
  966. if written > 0 {
  967. error!("error while reading: {err}");
  968. return Ok(written);
  969. } else {
  970. return Err(err);
  971. }
  972. }
  973. };
  974. if 0 == read {
  975. break;
  976. }
  977. let filled = &buf[..read];
  978. size -= read;
  979. if let Err(err) = block.write_all(filled) {
  980. if written > 0 {
  981. error!("error while writing: {err}");
  982. return Ok(written);
  983. } else {
  984. return Err(err);
  985. }
  986. }
  987. written += filled.len();
  988. }
  989. Ok(written)
  990. })
  991. }
  992. fn flush(
  993. &self,
  994. _ctx: &Context,
  995. inode: Self::Inode,
  996. handle: Self::Handle,
  997. _lock_owner: u64,
  998. ) -> io::Result<()> {
  999. debug!("Blocktree::flush called for inode {inode}");
  1000. self.access_block_mut(inode, handle, |block| block.flush())
  1001. }
  1002. fn read(
  1003. &self,
  1004. _ctx: &Context,
  1005. inode: Self::Inode,
  1006. handle: Self::Handle,
  1007. w: &mut dyn fuse_backend_rs::api::filesystem::ZeroCopyWriter,
  1008. size: u32,
  1009. _offset: u64,
  1010. _lock_owner: Option<u64>,
  1011. flags: u32,
  1012. ) -> io::Result<usize> {
  1013. debug!("Blocktree::read called on inode {inode}");
  1014. if (flags as libc::c_int & libc::O_WRONLY) != 0 {
  1015. return Err(io::Error::new(
  1016. io::ErrorKind::PermissionDenied,
  1017. "file is write only",
  1018. ));
  1019. }
  1020. let mut size: usize = size.try_into().box_err()?;
  1021. self.access_block_mut(inode, handle, |block| {
  1022. let mut buf = [0u8; crate::SECTOR_SZ_DEFAULT];
  1023. let mut read = 0;
  1024. while size > 0 {
  1025. let just_read = match block.read(&mut buf) {
  1026. Ok(just_read) => just_read,
  1027. Err(err) => {
  1028. if read > 0 {
  1029. error!("error while reading from block: {err}");
  1030. return Ok(read);
  1031. } else {
  1032. return Err(err);
  1033. }
  1034. }
  1035. };
  1036. if 0 == just_read {
  1037. break;
  1038. }
  1039. read += just_read;
  1040. let filled = &buf[..just_read];
  1041. if let Err(err) = w.write_all(filled) {
  1042. if read > 0 {
  1043. error!("error while writing: {err}");
  1044. return Ok(read);
  1045. } else {
  1046. return Err(err);
  1047. }
  1048. }
  1049. size -= filled.len();
  1050. }
  1051. Ok(read)
  1052. })
  1053. }
  1054. fn readdir(
  1055. &self,
  1056. _ctx: &Context,
  1057. inode: Self::Inode,
  1058. handle: Self::Handle,
  1059. size: u32,
  1060. offset: u64,
  1061. add_entry: &mut dyn FnMut(
  1062. fuse_backend_rs::api::filesystem::DirEntry,
  1063. ) -> io::Result<usize>,
  1064. ) -> io::Result<()> {
  1065. debug!("Blocktree::readdir called on inode {inode}");
  1066. let mut size: usize = size.try_into().box_err()?;
  1067. self.access_value(inode, |value| {
  1068. let dir = value
  1069. .value(handle)
  1070. .map_err(|_| bterr!(Error::InvalidHandle { handle, inode }))?
  1071. .directory()?;
  1072. let mut index: u64 = 0;
  1073. for (name, entry) in dir.entries() {
  1074. index += 1;
  1075. if index <= offset {
  1076. continue;
  1077. }
  1078. let inode = match entry.inode() {
  1079. Some(inode) => inode,
  1080. None => continue,
  1081. };
  1082. let dir_entry = FuseDirEntry {
  1083. ino: inode,
  1084. offset: index,
  1085. type_: entry.kind() as u32,
  1086. name: name.as_bytes(),
  1087. };
  1088. size = size.saturating_sub(add_entry(dir_entry)?);
  1089. if size == 0 {
  1090. break;
  1091. }
  1092. }
  1093. Ok(())
  1094. })
  1095. }
  1096. fn getattr(
  1097. &self,
  1098. _ctx: &Context,
  1099. inode: Self::Inode,
  1100. _handle: Option<Self::Handle>,
  1101. ) -> io::Result<(stat64, Duration)> {
  1102. debug!("Blocktree::getattr called for inode {inode}");
  1103. let stat = self.access_meta(inode, |meta| Ok(meta.body.secrets()?.stat()))?;
  1104. Ok((stat, self.attr_timeout()))
  1105. }
  1106. fn forget(&self, _ctx: &Context, inode: Self::Inode, count: u64) {
  1107. debug!("Blocktree::forget called for inode {inode}");
  1108. if let Err(err) = self.inode_forget(inode, count) {
  1109. error!("Blocktree::forget failed for inode {inode}: {err}");
  1110. }
  1111. }
  1112. fn lseek(
  1113. &self,
  1114. _ctx: &Context,
  1115. inode: Self::Inode,
  1116. handle: Self::Handle,
  1117. offset: u64,
  1118. whence: u32,
  1119. ) -> io::Result<u64> {
  1120. debug!("Blocktree::lseek called for inode {inode}");
  1121. let seek_from = SeekFrom::whence_offset(whence, offset)?;
  1122. self.access_block_mut(inode, handle, |block| block.seek(seek_from))
  1123. }
  1124. fn unlink(&self, ctx: &Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
  1125. debug!("Blocktree::unlink called on parent {parent}");
  1126. let name = name.to_str().box_err()?;
  1127. let (block_path, inode) = self.borrow_block(parent, |block| {
  1128. self.authorizer.write_allowed(ctx, block.meta())?;
  1129. let mut dir = block.read_dir()?;
  1130. let entry = match dir.entries.remove(name) {
  1131. None => return Err(io::Error::from_raw_os_error(libc::ENOENT)),
  1132. Some(entry) => entry,
  1133. };
  1134. let inode = entry.inode().ok_or_else(|| {
  1135. io::Error::new(
  1136. io::ErrorKind::Other,
  1137. "no inode associated with the given name",
  1138. )
  1139. })?;
  1140. block.write_dir(&dir)?;
  1141. let mut block_path = block.meta_body().path.clone();
  1142. block_path.push_component(name.to_owned());
  1143. Ok((block_path, inode))
  1144. })?;
  1145. self.open_value(inode, block_path, |value| {
  1146. // We mark the block for deletion if `nlink` drops to zero.
  1147. value.delete = value.borrow_block(|block| {
  1148. let nlink = block.mut_meta_body().access_secrets(|secrets| {
  1149. secrets.nlink -= 1;
  1150. Ok(secrets.nlink)
  1151. })?;
  1152. block.flush_meta()?;
  1153. Ok(0 == nlink)
  1154. })?;
  1155. Ok(())
  1156. })
  1157. }
  1158. fn link(
  1159. &self,
  1160. ctx: &Context,
  1161. inode: Self::Inode,
  1162. newparent: Self::Inode,
  1163. newname: &CStr,
  1164. ) -> io::Result<Entry> {
  1165. debug!("Blocktree::link called for inode {inode}");
  1166. let newname = newname.to_str().box_err()?;
  1167. self.borrow_block(newparent, |block| {
  1168. self.authorizer.write_allowed(ctx, block.meta())?;
  1169. let mut dir = block.read_dir()?;
  1170. if dir.entries.contains_key(newname) {
  1171. return Err(io::Error::from_raw_os_error(libc::EEXIST));
  1172. }
  1173. let (file_type, stat) = self.access_value_mut(inode, |value| {
  1174. let (file_type, stat) = value.borrow_block(|block| {
  1175. let meta = block.mut_meta_body();
  1176. let (mode, stat) = meta.access_secrets(|secrets| {
  1177. secrets.nlink += 1;
  1178. Ok((secrets.mode, secrets.stat()))
  1179. })?;
  1180. let file_type = FileType::from_value(mode)?;
  1181. block.flush_meta()?;
  1182. Ok((file_type, stat))
  1183. })?;
  1184. value.incr_lookup_count();
  1185. Ok((file_type, stat))
  1186. })?;
  1187. let entry = match file_type {
  1188. FileType::Reg => DirEntry::File(BlockRecord::new(inode)),
  1189. FileType::Dir => DirEntry::Directory(BlockRecord::new(inode)),
  1190. };
  1191. dir.entries.insert(newname.to_owned(), entry);
  1192. block.write_dir(&dir)?;
  1193. Ok(self.fuse_entry(inode, stat))
  1194. })
  1195. }
  1196. fn setattr(
  1197. &self,
  1198. ctx: &Context,
  1199. inode: Self::Inode,
  1200. attr: stat64,
  1201. _handle: Option<Self::Handle>,
  1202. valid: SetattrValid,
  1203. ) -> io::Result<(stat64, Duration)> {
  1204. debug!("Blocktree::setattr called for inode {inode}");
  1205. let stat = self.borrow_block(inode, |block| {
  1206. self.authorizer.write_allowed(ctx, block.meta())?;
  1207. let stat = block.mut_meta_body().access_secrets(|secrets| {
  1208. if valid.intersects(SetattrValid::MODE) {
  1209. secrets.mode = attr.st_mode;
  1210. }
  1211. if valid.intersects(SetattrValid::UID) {
  1212. secrets.uid = attr.st_uid;
  1213. }
  1214. if valid.intersects(SetattrValid::GID) {
  1215. secrets.gid = attr.st_gid;
  1216. }
  1217. if valid.intersects(SetattrValid::SIZE) {
  1218. warn!("modifying file size with setattr is not supported");
  1219. return Err(io::Error::from_raw_os_error(libc::EINVAL).into());
  1220. }
  1221. if valid.intersects(SetattrValid::ATIME) {
  1222. secrets.atime = (attr.st_atime as u64).into();
  1223. }
  1224. if valid.intersects(SetattrValid::MTIME) {
  1225. secrets.mtime = (attr.st_mtime as u64).into();
  1226. }
  1227. if valid.intersects(SetattrValid::CTIME) {
  1228. secrets.ctime = (attr.st_ctime as u64).into();
  1229. }
  1230. let atime_now = valid.intersects(SetattrValid::ATIME_NOW);
  1231. let mtime_now = valid.intersects(SetattrValid::MTIME_NOW);
  1232. if atime_now || mtime_now {
  1233. let now = Epoch::now();
  1234. if atime_now {
  1235. secrets.atime = now;
  1236. }
  1237. if mtime_now {
  1238. secrets.mtime = now;
  1239. }
  1240. }
  1241. if valid.intersects(SetattrValid::KILL_SUIDGID) {
  1242. secrets.mode &= !(libc::S_ISUID | libc::S_ISGID)
  1243. }
  1244. Ok(secrets.stat())
  1245. })?;
  1246. block.flush_meta()?;
  1247. Ok(stat)
  1248. })?;
  1249. Ok((stat, self.attr_timeout()))
  1250. }
  1251. fn mkdir(
  1252. &self,
  1253. ctx: &Context,
  1254. parent: Self::Inode,
  1255. name: &CStr,
  1256. mode: u32,
  1257. umask: u32,
  1258. ) -> io::Result<Entry> {
  1259. debug!("Blocktree::mkdir called");
  1260. let name = name.to_str().box_err()?.to_owned();
  1261. let (inode, mut block_path) = self.borrow_block(parent, |block| {
  1262. self.authorizer.write_allowed(ctx, block.meta())?;
  1263. let mut dir = block.read_dir()?;
  1264. if dir.entries.contains_key(&name) {
  1265. return Err(io::Error::from_raw_os_error(libc::EEXIST));
  1266. }
  1267. let inode = self.next_inode()?;
  1268. dir.add_file(name.clone(), inode)?;
  1269. block.write_dir(&dir)?;
  1270. Ok((inode, block.meta_body().path.clone()))
  1271. })?;
  1272. block_path.push_component(name);
  1273. let stat = self.open_value(inode, block_path, |value| {
  1274. let stat = value.borrow_block(|block| {
  1275. let stat = block.mut_meta_body().access_secrets(|secrets| {
  1276. secrets.block_id.generation = self.generation;
  1277. secrets.block_id.inode = inode;
  1278. secrets.mode = libc::S_IFDIR | mode & !umask;
  1279. secrets.uid = ctx.uid;
  1280. secrets.gid = ctx.gid;
  1281. Ok(secrets.stat())
  1282. })?;
  1283. block.write_dir(&Directory::new())?;
  1284. block.flush_meta()?;
  1285. Ok(stat)
  1286. })?;
  1287. value.incr_lookup_count();
  1288. Ok(stat)
  1289. })?;
  1290. Ok(self.fuse_entry(inode, stat))
  1291. }
  1292. fn rmdir(&self, ctx: &Context, parent: Self::Inode, name: &CStr) -> io::Result<()> {
  1293. debug!("Blocktree::rmdir called on parent {parent}");
  1294. let name = name.to_str().box_err()?.to_owned();
  1295. self.borrow_block(parent, |block| {
  1296. self.authorizer.write_allowed(ctx, block.meta())?;
  1297. let mut dir = block.read_dir()?;
  1298. if dir.entries.remove(&name).is_none() {
  1299. return Err(io::Error::from_raw_os_error(libc::ENOENT));
  1300. }
  1301. block.write_dir(&dir)?;
  1302. Ok(())
  1303. })
  1304. }
  1305. //////////////////////////////////
  1306. // METHODS WHICH ARE NOT SUPPORTED
  1307. //////////////////////////////////
  1308. fn getxattr(
  1309. &self,
  1310. _ctx: &Context,
  1311. inode: Self::Inode,
  1312. _name: &CStr,
  1313. _size: u32,
  1314. ) -> io::Result<fuse_backend_rs::api::filesystem::GetxattrReply> {
  1315. debug!("Blocktree::getxattr called for inode {inode}");
  1316. Self::not_supported()
  1317. }
  1318. fn ioctl(
  1319. &self,
  1320. _ctx: &Context,
  1321. inode: Self::Inode,
  1322. _handle: Self::Handle,
  1323. _flags: u32,
  1324. _cmd: u32,
  1325. _data: fuse_backend_rs::api::filesystem::IoctlData,
  1326. _out_size: u32,
  1327. ) -> io::Result<fuse_backend_rs::api::filesystem::IoctlData> {
  1328. debug!("Blocktree::ioctl called for inode {inode}");
  1329. Self::not_supported()
  1330. }
  1331. fn access(&self, _ctx: &Context, inode: Self::Inode, _mask: u32) -> io::Result<()> {
  1332. debug!("Blocktree::access called for inode {inode}");
  1333. Self::not_supported()
  1334. }
  1335. fn batch_forget(&self, _ctx: &Context, _requests: Vec<(Self::Inode, u64)>) {
  1336. debug!("Blocktree::batch_forget called");
  1337. Self::not_supported().unwrap()
  1338. }
  1339. fn bmap(
  1340. &self,
  1341. _ctx: &Context,
  1342. inode: Self::Inode,
  1343. _block: u64,
  1344. _blocksize: u32,
  1345. ) -> io::Result<u64> {
  1346. debug!("Blocktree::bmap called for inode {inode}");
  1347. Self::not_supported()
  1348. }
  1349. fn fallocate(
  1350. &self,
  1351. _ctx: &Context,
  1352. inode: Self::Inode,
  1353. _handle: Self::Handle,
  1354. _mode: u32,
  1355. _offset: u64,
  1356. _length: u64,
  1357. ) -> io::Result<()> {
  1358. debug!("Blocktree::fallocate called for inode {inode}");
  1359. Self::not_supported()
  1360. }
  1361. fn fsync(
  1362. &self,
  1363. _ctx: &Context,
  1364. inode: Self::Inode,
  1365. _datasync: bool,
  1366. _handle: Self::Handle,
  1367. ) -> io::Result<()> {
  1368. debug!("Blocktree::fsync called for inode {inode}");
  1369. Self::not_supported()
  1370. }
  1371. fn fsyncdir(
  1372. &self,
  1373. _ctx: &Context,
  1374. inode: Self::Inode,
  1375. _datasync: bool,
  1376. _handle: Self::Handle,
  1377. ) -> io::Result<()> {
  1378. debug!("Blocktree::fsyncdir called for inode {inode}");
  1379. Self::not_supported()
  1380. }
  1381. fn getlk(
  1382. &self,
  1383. _ctx: &Context,
  1384. inode: Self::Inode,
  1385. _handle: Self::Handle,
  1386. _owner: u64,
  1387. _lock: fuse_backend_rs::api::filesystem::FileLock,
  1388. _flags: u32,
  1389. ) -> io::Result<fuse_backend_rs::api::filesystem::FileLock> {
  1390. debug!("Blocktree::getlk called for inode {inode}");
  1391. Self::not_supported()
  1392. }
  1393. fn listxattr(
  1394. &self,
  1395. _ctx: &Context,
  1396. inode: Self::Inode,
  1397. _size: u32,
  1398. ) -> io::Result<fuse_backend_rs::api::filesystem::ListxattrReply> {
  1399. debug!("Blocktree::listxattr called for inode {inode}");
  1400. Self::not_supported()
  1401. }
  1402. fn mknod(
  1403. &self,
  1404. _ctx: &Context,
  1405. inode: Self::Inode,
  1406. _name: &CStr,
  1407. _mode: u32,
  1408. _rdev: u32,
  1409. _umask: u32,
  1410. ) -> io::Result<Entry> {
  1411. debug!("Blocktree::mknod called for inode {inode}");
  1412. Self::not_supported()
  1413. }
  1414. fn notify_reply(&self) -> io::Result<()> {
  1415. debug!("Blocktree::notify_reply called");
  1416. Self::not_supported()
  1417. }
  1418. fn poll(
  1419. &self,
  1420. _ctx: &Context,
  1421. inode: Self::Inode,
  1422. _handle: Self::Handle,
  1423. _khandle: Self::Handle,
  1424. _flags: u32,
  1425. _events: u32,
  1426. ) -> io::Result<u32> {
  1427. debug!("Blocktree::poll called for inode {inode}");
  1428. Self::not_supported()
  1429. }
  1430. fn readdirplus(
  1431. &self,
  1432. _ctx: &Context,
  1433. _inode: Self::Inode,
  1434. _handle: Self::Handle,
  1435. _size: u32,
  1436. _offset: u64,
  1437. _add_entry: &mut dyn FnMut(
  1438. fuse_backend_rs::api::filesystem::DirEntry,
  1439. Entry,
  1440. ) -> io::Result<usize>,
  1441. ) -> io::Result<()> {
  1442. debug!("Blocktree::readdirplus called");
  1443. Self::not_supported()
  1444. }
  1445. fn readlink(&self, _ctx: &Context, inode: Self::Inode) -> io::Result<Vec<u8>> {
  1446. debug!("Blocktree::readlink called for inode {inode}");
  1447. Self::not_supported()
  1448. }
  1449. fn removexattr(&self, _ctx: &Context, inode: Self::Inode, _name: &CStr) -> io::Result<()> {
  1450. debug!("Blocktree::removexattr called for inode {inode}");
  1451. Self::not_supported()
  1452. }
  1453. fn rename(
  1454. &self,
  1455. _ctx: &Context,
  1456. _olddir: Self::Inode,
  1457. _oldname: &CStr,
  1458. _newdir: Self::Inode,
  1459. _newname: &CStr,
  1460. _flags: u32,
  1461. ) -> io::Result<()> {
  1462. debug!("Blocktree::rename called");
  1463. Self::not_supported()
  1464. }
  1465. fn setlk(
  1466. &self,
  1467. _ctx: &Context,
  1468. inode: Self::Inode,
  1469. _handle: Self::Handle,
  1470. _owner: u64,
  1471. _lock: fuse_backend_rs::api::filesystem::FileLock,
  1472. _flags: u32,
  1473. ) -> io::Result<()> {
  1474. debug!("Blocktree::setlk called for inode {inode}");
  1475. Self::not_supported()
  1476. }
  1477. fn setlkw(
  1478. &self,
  1479. _ctx: &Context,
  1480. inode: Self::Inode,
  1481. _handle: Self::Handle,
  1482. _owner: u64,
  1483. _lock: fuse_backend_rs::api::filesystem::FileLock,
  1484. _flags: u32,
  1485. ) -> io::Result<()> {
  1486. debug!("Blocktree::setlkw called for inode {inode}");
  1487. Self::not_supported()
  1488. }
  1489. fn setxattr(
  1490. &self,
  1491. _ctx: &Context,
  1492. inode: Self::Inode,
  1493. _name: &CStr,
  1494. _value: &[u8],
  1495. _flags: u32,
  1496. ) -> io::Result<()> {
  1497. debug!("Blocktree::setxattr called for inode {inode}");
  1498. Self::not_supported()
  1499. }
  1500. fn statfs(&self, _ctx: &Context, inode: Self::Inode) -> io::Result<statvfs64> {
  1501. debug!("Blocktree::statfs called for inode {inode}");
  1502. Self::not_supported()
  1503. }
  1504. fn symlink(
  1505. &self,
  1506. _ctx: &Context,
  1507. _linkname: &CStr,
  1508. _parent: Self::Inode,
  1509. _name: &CStr,
  1510. ) -> io::Result<Entry> {
  1511. debug!("Blocktree::symlink called");
  1512. Self::not_supported()
  1513. }
  1514. }
  1515. }
  1516. #[cfg(test)]
  1517. mod tests {
  1518. use fuse_backend_rs::{
  1519. abi::fuse_abi::CreateIn,
  1520. api::filesystem::{Context, FileSystem, FsOptions},
  1521. };
  1522. use std::{ffi::CString, io};
  1523. use tempdir::TempDir;
  1524. use test_helpers::*;
  1525. use crate::{crypto::ConcreteCreds, test_helpers, BlockMeta, Decompose};
  1526. use super::{private::SpecInodes, *};
  1527. /// Tests for the [ModeAuthorizer] struct.
  1528. mod mode_authorizer_tests {
  1529. use super::{
  1530. super::private::{Authorizer, AuthzContext},
  1531. *,
  1532. };
  1533. struct TestCase {
  1534. ctx_uid: u32,
  1535. ctx_gid: u32,
  1536. meta: BlockMeta,
  1537. }
  1538. impl TestCase {
  1539. const BLOCK_UID: u32 = 1000;
  1540. const BLOCK_GID: u32 = 1000;
  1541. const CTX_PID: libc::pid_t = 100;
  1542. fn new(ctx_uid: u32, ctx_gid: u32, mode: u32) -> TestCase {
  1543. let mut meta = BlockMeta::new(&*test_helpers::NODE_CREDS)
  1544. .expect("failed to create block metadata");
  1545. meta.body
  1546. .access_secrets(|secrets| {
  1547. secrets.uid = Self::BLOCK_UID;
  1548. secrets.gid = Self::BLOCK_GID;
  1549. secrets.mode = mode;
  1550. Ok(())
  1551. })
  1552. .expect("failed to update secrets");
  1553. TestCase {
  1554. ctx_uid,
  1555. ctx_gid,
  1556. meta,
  1557. }
  1558. }
  1559. fn context(&self) -> AuthzContext<'_> {
  1560. AuthzContext {
  1561. uid: self.ctx_uid,
  1562. gid: self.ctx_gid,
  1563. pid: Self::CTX_PID,
  1564. meta: &self.meta,
  1565. }
  1566. }
  1567. }
  1568. #[test]
  1569. fn cant_read_when_no_bits_set() {
  1570. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, 0);
  1571. let result = ModeAuthorizer {}.can_read(&case.context());
  1572. assert!(result.is_err())
  1573. }
  1574. #[test]
  1575. fn cant_write_when_no_bits_set() {
  1576. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, 0);
  1577. let result = ModeAuthorizer {}.can_write(&case.context());
  1578. assert!(result.is_err())
  1579. }
  1580. #[test]
  1581. fn cant_exec_when_no_bits_set() {
  1582. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, 0);
  1583. let result = ModeAuthorizer {}.can_exec(&case.context());
  1584. assert!(result.is_err())
  1585. }
  1586. #[test]
  1587. fn user_can_read_when_bit_set() {
  1588. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IRUSR);
  1589. let result = ModeAuthorizer {}.can_read(&case.context());
  1590. assert!(result.is_ok())
  1591. }
  1592. #[test]
  1593. fn user_can_write_when_bit_set() {
  1594. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IWUSR);
  1595. let result = ModeAuthorizer {}.can_write(&case.context());
  1596. assert!(result.is_ok())
  1597. }
  1598. #[test]
  1599. fn user_can_exec_when_bit_set() {
  1600. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IXUSR);
  1601. let result = ModeAuthorizer {}.can_exec(&case.context());
  1602. assert!(result.is_ok())
  1603. }
  1604. #[test]
  1605. fn group_can_read_when_bit_set() {
  1606. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IRGRP);
  1607. let result = ModeAuthorizer {}.can_read(&case.context());
  1608. assert!(result.is_ok())
  1609. }
  1610. #[test]
  1611. fn group_can_write_when_bit_set() {
  1612. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IWGRP);
  1613. let result = ModeAuthorizer {}.can_write(&case.context());
  1614. assert!(result.is_ok())
  1615. }
  1616. #[test]
  1617. fn group_can_exec_when_bit_set() {
  1618. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IXGRP);
  1619. let result = ModeAuthorizer {}.can_exec(&case.context());
  1620. assert!(result.is_ok())
  1621. }
  1622. #[test]
  1623. fn other_can_read_when_bit_set() {
  1624. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IROTH);
  1625. let result = ModeAuthorizer {}.can_read(&case.context());
  1626. assert!(result.is_ok())
  1627. }
  1628. #[test]
  1629. fn other_can_write_when_bit_set() {
  1630. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IWOTH);
  1631. let result = ModeAuthorizer {}.can_write(&case.context());
  1632. assert!(result.is_ok())
  1633. }
  1634. #[test]
  1635. fn other_can_exec_when_bit_set() {
  1636. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IXOTH);
  1637. let result = ModeAuthorizer {}.can_exec(&case.context());
  1638. assert!(result.is_ok())
  1639. }
  1640. #[test]
  1641. fn other_cant_write_even_if_user_can() {
  1642. let case = TestCase::new(
  1643. TestCase::BLOCK_UID + 1,
  1644. TestCase::BLOCK_GID + 1,
  1645. libc::S_IWUSR,
  1646. );
  1647. let result = ModeAuthorizer {}.can_write(&case.context());
  1648. assert!(result.is_err())
  1649. }
  1650. #[test]
  1651. fn other_cant_write_even_if_group_can() {
  1652. let case = TestCase::new(
  1653. TestCase::BLOCK_UID + 1,
  1654. TestCase::BLOCK_GID + 1,
  1655. libc::S_IWGRP,
  1656. );
  1657. let result = ModeAuthorizer {}.can_write(&case.context());
  1658. assert!(result.is_err())
  1659. }
  1660. #[test]
  1661. fn user_allowed_read_when_only_other_bit_set() {
  1662. let case = TestCase::new(TestCase::BLOCK_UID, TestCase::BLOCK_GID, libc::S_IROTH);
  1663. let result = ModeAuthorizer {}.can_read(&case.context());
  1664. assert!(result.is_ok())
  1665. }
  1666. #[test]
  1667. fn root_always_allowed() {
  1668. let case = TestCase::new(0, 0, 0);
  1669. let ctx = case.context();
  1670. let authorizer = ModeAuthorizer {};
  1671. assert!(authorizer.can_read(&ctx).is_ok());
  1672. assert!(authorizer.can_write(&ctx).is_ok());
  1673. assert!(authorizer.can_exec(&ctx).is_ok());
  1674. }
  1675. }
  1676. struct BtTestCase {
  1677. dir: TempDir,
  1678. bt: Blocktree<ConcreteCreds, ModeAuthorizer>,
  1679. }
  1680. impl BtTestCase {
  1681. fn new_empty() -> BtTestCase {
  1682. let dir = TempDir::new("fuse").expect("failed to create temp dir");
  1683. let bt =
  1684. Blocktree::new_empty(dir.path().to_owned(), 0, Self::creds(), ModeAuthorizer {})
  1685. .expect("failed to create empty blocktree");
  1686. bt.init(FsOptions::empty()).expect("init failed");
  1687. BtTestCase { dir, bt }
  1688. }
  1689. fn new_existing(dir: TempDir) -> BtTestCase {
  1690. let bt =
  1691. Blocktree::new_existing(dir.path().to_owned(), Self::creds(), ModeAuthorizer {})
  1692. .expect("failed to create blocktree from existing directory");
  1693. bt.init(FsOptions::empty()).expect("init failed");
  1694. BtTestCase { dir, bt }
  1695. }
  1696. fn creds() -> ConcreteCreds {
  1697. test_helpers::NODE_CREDS.clone()
  1698. }
  1699. fn context(&self) -> Context {
  1700. let (stat, ..) = self
  1701. .bt
  1702. .getattr(&Default::default(), SpecInodes::RootDir.into(), None)
  1703. .expect("getattr failed");
  1704. Context {
  1705. uid: stat.st_uid,
  1706. gid: stat.st_gid,
  1707. pid: 1,
  1708. }
  1709. }
  1710. }
  1711. /// Tests that a new file can be created, written to and the written data can be read from it.
  1712. #[test]
  1713. fn create_write_lseek_read() {
  1714. let case = BtTestCase::new_empty();
  1715. let bt = &case.bt;
  1716. let ctx = case.context();
  1717. let name = CString::new("README.md").unwrap();
  1718. let flags = libc::O_RDWR as u32;
  1719. let (entry, handle, ..) = bt
  1720. .create(
  1721. &ctx,
  1722. SpecInodes::RootDir.into(),
  1723. name.as_c_str(),
  1724. CreateIn {
  1725. mode: libc::S_IFREG | 0o644,
  1726. umask: 0,
  1727. flags,
  1728. fuse_flags: 0,
  1729. },
  1730. )
  1731. .expect("failed to create file");
  1732. let inode = entry.inode;
  1733. let handle = handle.unwrap();
  1734. const LEN: usize = 32;
  1735. let mut expected = BtCursor::new([1u8; LEN]);
  1736. let written = bt
  1737. .write(
  1738. &ctx,
  1739. inode,
  1740. handle,
  1741. &mut expected,
  1742. LEN as u32,
  1743. 0,
  1744. None,
  1745. false,
  1746. flags,
  1747. 0,
  1748. )
  1749. .expect("write failed");
  1750. assert_eq!(LEN, written);
  1751. bt.lseek(&ctx, inode, handle, 0, 0).expect("lseek failed");
  1752. let mut actual = BtCursor::new([0u8; LEN]);
  1753. let read = bt
  1754. .read(&ctx, inode, handle, &mut actual, LEN as u32, 0, None, flags)
  1755. .expect("failed to read");
  1756. assert_eq!(LEN, read);
  1757. assert_eq!(expected, actual)
  1758. }
  1759. #[test]
  1760. fn lookup() {
  1761. let case = BtTestCase::new_empty();
  1762. let bt = &case.bt;
  1763. let ctx = case.context();
  1764. let name = CString::new("README.md").unwrap();
  1765. let (expected, ..) = bt
  1766. .create(
  1767. &ctx,
  1768. SpecInodes::RootDir.into(),
  1769. name.as_c_str(),
  1770. Default::default(),
  1771. )
  1772. .expect("failed to create file");
  1773. let actual = bt
  1774. .lookup(&Default::default(), SpecInodes::RootDir.into(), &name)
  1775. .expect("lookup failed");
  1776. assert_eq!(expected.generation, actual.generation);
  1777. assert_eq!(expected.inode, actual.inode);
  1778. }
  1779. /// Tests that data written by one instance of [Blocktree] can be read by a subsequent
  1780. /// instance.
  1781. #[test]
  1782. fn new_existing() {
  1783. const EXPECTED: &[u8] = b"cool as cucumbers";
  1784. let name = CString::new("RESIGNATION.docx").unwrap();
  1785. let case = BtTestCase::new_empty();
  1786. let bt = &case.bt;
  1787. let ctx = case.context();
  1788. let flags = libc::O_RDWR as u32;
  1789. {
  1790. let (entry, handle, ..) = bt
  1791. .create(
  1792. &ctx,
  1793. SpecInodes::RootDir.into(),
  1794. name.as_c_str(),
  1795. CreateIn {
  1796. mode: libc::S_IFREG | 0o644,
  1797. umask: 0,
  1798. flags,
  1799. fuse_flags: 0,
  1800. },
  1801. )
  1802. .expect("failed to create file");
  1803. let inode = entry.inode;
  1804. let handle = handle.unwrap();
  1805. let mut vec = Vec::with_capacity(EXPECTED.len());
  1806. vec.extend_from_slice(EXPECTED);
  1807. let mut cursor = BtCursor::new(vec);
  1808. let written = bt
  1809. .write(
  1810. &Default::default(),
  1811. inode,
  1812. handle,
  1813. &mut cursor,
  1814. EXPECTED.len() as u32,
  1815. 0,
  1816. None,
  1817. false,
  1818. flags,
  1819. 0,
  1820. )
  1821. .expect("write failed");
  1822. assert_eq!(EXPECTED.len(), written);
  1823. bt.flush(&Default::default(), inode, handle, 0)
  1824. .expect("flush failed");
  1825. }
  1826. let case = BtTestCase::new_existing(case.dir);
  1827. let bt = &case.bt;
  1828. let entry = bt
  1829. .lookup(&Default::default(), SpecInodes::RootDir.into(), &name)
  1830. .expect("lookup failed");
  1831. let inode = entry.inode;
  1832. let (handle, ..) = bt
  1833. .open(&Default::default(), entry.inode, 0, 0)
  1834. .expect("open failed");
  1835. let handle = handle.unwrap();
  1836. let mut actual = BtCursor::new([0u8; EXPECTED.len()]);
  1837. let _ = bt
  1838. .read(
  1839. &Default::default(),
  1840. inode,
  1841. handle,
  1842. &mut actual,
  1843. EXPECTED.len() as u32,
  1844. 0,
  1845. None,
  1846. flags,
  1847. )
  1848. .expect("read failed");
  1849. assert_eq!(EXPECTED, actual.into_inner().as_slice())
  1850. }
  1851. /// Tests that an error is returned by the `Blocktree::write` method if the file was opened
  1852. /// read-only.
  1853. #[test]
  1854. fn open_read_only_write_is_error() {
  1855. let name = CString::new("books.ods").unwrap();
  1856. let case = BtTestCase::new_empty();
  1857. let bt = &case.bt;
  1858. let ctx = case.context();
  1859. let (entry, handle, ..) = bt
  1860. .create(
  1861. &ctx,
  1862. SpecInodes::RootDir.into(),
  1863. name.as_c_str(),
  1864. CreateIn {
  1865. mode: libc::S_IFREG | 0o644,
  1866. umask: 0,
  1867. flags: 0,
  1868. fuse_flags: 0,
  1869. },
  1870. )
  1871. .expect("failed to create file");
  1872. let inode = entry.inode;
  1873. let handle = handle.unwrap();
  1874. bt.release(&ctx, inode, 0, handle, false, false, None)
  1875. .expect("release failed");
  1876. let flags = libc::O_RDONLY as u32;
  1877. let (handle, ..) = bt.open(&ctx, inode, flags, 0).expect("open failed");
  1878. let handle = handle.unwrap();
  1879. const LEN: usize = 32;
  1880. let mut reader = BtCursor::new([1u8; LEN]);
  1881. let result = bt.write(
  1882. &ctx,
  1883. inode,
  1884. handle,
  1885. &mut reader,
  1886. LEN.try_into().unwrap(),
  1887. 0,
  1888. None,
  1889. false,
  1890. flags,
  1891. 0,
  1892. );
  1893. let err = result.err().unwrap();
  1894. assert_eq!(io::ErrorKind::PermissionDenied, err.kind());
  1895. }
  1896. /// Tests that a call to `read` fails when a file is opened write only.
  1897. #[test]
  1898. fn open_write_only_read_is_error() {
  1899. let name = CString::new("books.ods").unwrap();
  1900. let case = BtTestCase::new_empty();
  1901. let bt = &case.bt;
  1902. let ctx = case.context();
  1903. let (entry, handle, ..) = bt
  1904. .create(
  1905. &ctx,
  1906. SpecInodes::RootDir.into(),
  1907. name.as_c_str(),
  1908. CreateIn {
  1909. mode: libc::S_IFREG | 0o644,
  1910. umask: 0,
  1911. flags: 0,
  1912. fuse_flags: 0,
  1913. },
  1914. )
  1915. .expect("failed to create file");
  1916. let inode = entry.inode;
  1917. let handle = handle.unwrap();
  1918. bt.release(&ctx, inode, 0, handle, false, false, None)
  1919. .expect("release failed");
  1920. let flags = libc::O_WRONLY as u32;
  1921. let (handle, ..) = bt.open(&ctx, inode, flags, 0).expect("open failed");
  1922. let handle = handle.unwrap();
  1923. const LEN: usize = 32;
  1924. let mut reader = BtCursor::new([1u8; LEN]);
  1925. let result = bt.read(
  1926. &ctx,
  1927. inode,
  1928. handle,
  1929. &mut reader,
  1930. LEN.try_into().unwrap(),
  1931. 0,
  1932. None,
  1933. flags,
  1934. );
  1935. let err = result.err().unwrap();
  1936. assert_eq!(io::ErrorKind::PermissionDenied, err.kind());
  1937. }
  1938. }