Directory.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /** @file
  2. Directory related routines
  3. Copyright (c) 2021 - 2023 Pedro Falcato All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Ext4Dxe.h"
  7. #include <Library/BaseUcs2Utf8Lib.h>
  8. /**
  9. Retrieves the filename of the directory entry and converts it to UTF-16/UCS-2
  10. @param[in] Entry Pointer to a EXT4_DIR_ENTRY.
  11. @param[out] Ucs2FileName Pointer to an array of CHAR16's, of size EXT4_NAME_MAX + 1.
  12. @retval EFI_SUCCESS The filename was successfully retrieved and converted to UCS2.
  13. @retval EFI_INVALID_PARAMETER The filename is not valid UTF-8.
  14. @retval !EFI_SUCCESS Failure.
  15. **/
  16. EFI_STATUS
  17. Ext4GetUcs2DirentName (
  18. IN EXT4_DIR_ENTRY *Entry,
  19. OUT CHAR16 Ucs2FileName[EXT4_NAME_MAX + 1]
  20. )
  21. {
  22. CHAR8 Utf8NameBuf[EXT4_NAME_MAX + 1];
  23. UINT16 *Str;
  24. UINT8 Index;
  25. EFI_STATUS Status;
  26. for (Index = 0; Index < Entry->name_len; ++Index) {
  27. if (Entry->name[Index] == '\0') {
  28. return EFI_INVALID_PARAMETER;
  29. }
  30. Utf8NameBuf[Index] = Entry->name[Index];
  31. }
  32. Utf8NameBuf[Entry->name_len] = '\0';
  33. // Unfortunately, BaseUcs2Utf8Lib doesn't have a convert-buffer-to-buffer-like
  34. // function. Therefore, we need to allocate from the pool (inside UTF8StrToUCS2),
  35. // copy it to our out buffer (Ucs2FileName) and free.
  36. Status = UTF8StrToUCS2 (Utf8NameBuf, &Str);
  37. if (EFI_ERROR (Status)) {
  38. return Status;
  39. }
  40. Status = StrCpyS (Ucs2FileName, EXT4_NAME_MAX + 1, Str);
  41. FreePool (Str);
  42. return Status;
  43. }
  44. /**
  45. Validates a directory entry.
  46. @param[in] Dirent Pointer to the directory entry.
  47. @retval TRUE Valid directory entry.
  48. FALSE Invalid directory entry.
  49. **/
  50. STATIC
  51. BOOLEAN
  52. Ext4ValidDirent (
  53. IN CONST EXT4_DIR_ENTRY *Dirent
  54. )
  55. {
  56. UINTN RequiredSize;
  57. RequiredSize = Dirent->name_len + EXT4_MIN_DIR_ENTRY_LEN;
  58. if (Dirent->rec_len < RequiredSize) {
  59. DEBUG ((DEBUG_ERROR, "[ext4] dirent size %lu too small (compared to %lu)\n", Dirent->rec_len, RequiredSize));
  60. return FALSE;
  61. }
  62. // Dirent sizes need to be 4 byte aligned
  63. if ((Dirent->rec_len % 4) != 0) {
  64. return FALSE;
  65. }
  66. return TRUE;
  67. }
  68. /**
  69. Retrieves a directory entry.
  70. @param[in] Directory Pointer to the opened directory.
  71. @param[in] NameUnicode Pointer to the UCS-2 formatted filename.
  72. @param[in] Partition Pointer to the ext4 partition.
  73. @param[out] Result Pointer to the destination directory entry.
  74. @return The result of the operation.
  75. **/
  76. EFI_STATUS
  77. Ext4RetrieveDirent (
  78. IN EXT4_FILE *Directory,
  79. IN CONST CHAR16 *Name,
  80. IN EXT4_PARTITION *Partition,
  81. OUT EXT4_DIR_ENTRY *Result
  82. )
  83. {
  84. EFI_STATUS Status;
  85. CHAR8 *Buf;
  86. UINT64 Off;
  87. EXT4_INODE *Inode;
  88. UINT64 DirInoSize;
  89. UINT32 BlockRemainder;
  90. UINTN Length;
  91. EXT4_DIR_ENTRY *Entry;
  92. UINTN RemainingBlock;
  93. CHAR16 DirentUcs2Name[EXT4_NAME_MAX + 1];
  94. UINTN ToCopy;
  95. UINTN BlockOffset;
  96. Buf = AllocatePool (Partition->BlockSize);
  97. if (Buf == NULL) {
  98. return EFI_OUT_OF_RESOURCES;
  99. }
  100. Off = 0;
  101. Inode = Directory->Inode;
  102. DirInoSize = EXT4_INODE_SIZE (Inode);
  103. DivU64x32Remainder (DirInoSize, Partition->BlockSize, &BlockRemainder);
  104. if (BlockRemainder != 0) {
  105. // Directory inodes need to have block aligned sizes
  106. Status = EFI_VOLUME_CORRUPTED;
  107. goto Out;
  108. }
  109. while (Off < DirInoSize) {
  110. Length = Partition->BlockSize;
  111. Status = Ext4Read (Partition, Directory, Buf, Off, &Length);
  112. if (Status != EFI_SUCCESS) {
  113. goto Out;
  114. }
  115. for (BlockOffset = 0; BlockOffset < Partition->BlockSize; ) {
  116. Entry = (EXT4_DIR_ENTRY *)(Buf + BlockOffset);
  117. RemainingBlock = Partition->BlockSize - BlockOffset;
  118. // Check if the minimum directory entry fits inside [BlockOffset, EndOfBlock]
  119. if (RemainingBlock < EXT4_MIN_DIR_ENTRY_LEN) {
  120. Status = EFI_VOLUME_CORRUPTED;
  121. goto Out;
  122. }
  123. if (!Ext4ValidDirent (Entry)) {
  124. Status = EFI_VOLUME_CORRUPTED;
  125. goto Out;
  126. }
  127. if ((Entry->name_len > RemainingBlock) || (Entry->rec_len > RemainingBlock)) {
  128. // Corrupted filesystem
  129. Status = EFI_VOLUME_CORRUPTED;
  130. goto Out;
  131. }
  132. // Unused entry
  133. if (Entry->inode == 0) {
  134. BlockOffset += Entry->rec_len;
  135. continue;
  136. }
  137. Status = Ext4GetUcs2DirentName (Entry, DirentUcs2Name);
  138. /* In theory, this should never fail.
  139. * In reality, it's quite possible that it can fail, considering filenames in
  140. * Linux (and probably other nixes) are just null-terminated bags of bytes, and don't
  141. * need to form valid ASCII/UTF-8 sequences.
  142. */
  143. if (EFI_ERROR (Status)) {
  144. if (Status == EFI_INVALID_PARAMETER) {
  145. // If we error out due to a bad UTF-8 sequence (see Ext4GetUcs2DirentName), skip this entry.
  146. // I'm not sure if this is correct behaviour, but I don't think there's a precedent here.
  147. BlockOffset += Entry->rec_len;
  148. continue;
  149. }
  150. // Other sorts of errors should just error out.
  151. FreePool (Buf);
  152. return Status;
  153. }
  154. if ((Entry->name_len == StrLen (Name)) &&
  155. !Ext4StrCmpInsensitive (DirentUcs2Name, (CHAR16 *)Name))
  156. {
  157. ToCopy = MIN (Entry->rec_len, sizeof (EXT4_DIR_ENTRY));
  158. CopyMem (Result, Entry, ToCopy);
  159. Status = EFI_SUCCESS;
  160. goto Out;
  161. }
  162. BlockOffset += Entry->rec_len;
  163. }
  164. Off += Partition->BlockSize;
  165. }
  166. Status = EFI_NOT_FOUND;
  167. Out:
  168. FreePool (Buf);
  169. return Status;
  170. }
  171. /**
  172. Opens a file using a directory entry.
  173. @param[in] Partition Pointer to the ext4 partition.
  174. @param[in] OpenMode Mode in which the file is supposed to be open.
  175. @param[out] OutFile Pointer to the newly opened file.
  176. @param[in] Entry Directory entry to be used.
  177. @param[in] Directory Pointer to the opened directory.
  178. @retval EFI_STATUS Result of the operation
  179. **/
  180. EFI_STATUS
  181. Ext4OpenDirent (
  182. IN EXT4_PARTITION *Partition,
  183. IN UINT64 OpenMode,
  184. OUT EXT4_FILE **OutFile,
  185. IN EXT4_DIR_ENTRY *Entry,
  186. IN EXT4_FILE *Directory
  187. )
  188. {
  189. EFI_STATUS Status;
  190. CHAR16 FileName[EXT4_NAME_MAX + 1];
  191. EXT4_FILE *File;
  192. File = AllocateZeroPool (sizeof (EXT4_FILE));
  193. if (File == NULL) {
  194. Status = EFI_OUT_OF_RESOURCES;
  195. goto Error;
  196. }
  197. Status = Ext4GetUcs2DirentName (Entry, FileName);
  198. if (EFI_ERROR (Status)) {
  199. goto Error;
  200. }
  201. if (StrCmp (FileName, L".") == 0) {
  202. // We're using the parent directory's dentry
  203. File->Dentry = Directory->Dentry;
  204. ASSERT (File->Dentry != NULL);
  205. Ext4RefDentry (File->Dentry);
  206. } else if (StrCmp (FileName, L"..") == 0) {
  207. // Using the parent's parent's dentry
  208. File->Dentry = Directory->Dentry->Parent;
  209. if (!File->Dentry) {
  210. // Someone tried .. on root, so direct them to /
  211. // This is an illegal EFI Open() but is possible to hit from a variety of internal code
  212. File->Dentry = Directory->Dentry;
  213. }
  214. Ext4RefDentry (File->Dentry);
  215. } else {
  216. File->Dentry = Ext4CreateDentry (FileName, Directory->Dentry);
  217. if (File->Dentry == NULL) {
  218. Status = EFI_OUT_OF_RESOURCES;
  219. goto Error;
  220. }
  221. }
  222. Status = Ext4InitExtentsMap (File);
  223. if (EFI_ERROR (Status)) {
  224. goto Error;
  225. }
  226. File->InodeNum = Entry->inode;
  227. Ext4SetupFile (File, Partition);
  228. Status = Ext4ReadInode (Partition, Entry->inode, &File->Inode);
  229. if (EFI_ERROR (Status)) {
  230. goto Error;
  231. }
  232. *OutFile = File;
  233. InsertTailList (&Partition->OpenFiles, &File->OpenFilesListNode);
  234. return EFI_SUCCESS;
  235. Error:
  236. if (File != NULL) {
  237. if (File->Dentry != NULL) {
  238. Ext4UnrefDentry (File->Dentry);
  239. }
  240. if (File->ExtentsMap != NULL) {
  241. OrderedCollectionUninit (File->ExtentsMap);
  242. }
  243. FreePool (File);
  244. }
  245. return Status;
  246. }
  247. /**
  248. Opens a file.
  249. @param[in] Directory Pointer to the opened directory.
  250. @param[in] Name Pointer to the UCS-2 formatted filename.
  251. @param[in] Partition Pointer to the ext4 partition.
  252. @param[in] OpenMode Mode in which the file is supposed to be open.
  253. @param[out] OutFile Pointer to the newly opened file.
  254. @return Result of the operation.
  255. **/
  256. EFI_STATUS
  257. Ext4OpenFile (
  258. IN EXT4_FILE *Directory,
  259. IN CONST CHAR16 *Name,
  260. IN EXT4_PARTITION *Partition,
  261. IN UINT64 OpenMode,
  262. OUT EXT4_FILE **OutFile
  263. )
  264. {
  265. EXT4_DIR_ENTRY Entry;
  266. EFI_STATUS Status;
  267. Status = Ext4RetrieveDirent (Directory, Name, Partition, &Entry);
  268. if (EFI_ERROR (Status)) {
  269. return Status;
  270. }
  271. // EFI requires us to error out on ".." opens for the root directory
  272. if (Entry.inode == Directory->InodeNum) {
  273. return EFI_NOT_FOUND;
  274. }
  275. return Ext4OpenDirent (Partition, OpenMode, OutFile, &Entry, Directory);
  276. }
  277. /**
  278. Open the root directory on a volume.
  279. @param[in] This A pointer to the volume to open the root directory.
  280. @param[out] Root A pointer to the location to return the opened file handle for the
  281. root directory.
  282. @retval EFI_SUCCESS The device was opened.
  283. @retval EFI_UNSUPPORTED This volume does not support the requested file system type.
  284. @retval EFI_NO_MEDIA The device has no medium.
  285. @retval EFI_DEVICE_ERROR The device reported an error.
  286. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  287. @retval EFI_ACCESS_DENIED The service denied access to the file.
  288. @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
  289. @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
  290. longer supported. Any existing file handles for this volume are
  291. no longer valid. To access the files on the new medium, the
  292. volume must be reopened with OpenVolume().
  293. **/
  294. EFI_STATUS
  295. EFIAPI
  296. Ext4OpenVolume (
  297. IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
  298. OUT EFI_FILE_PROTOCOL **Root
  299. )
  300. {
  301. EXT4_INODE *RootInode;
  302. EFI_STATUS Status;
  303. EXT4_FILE *RootDir;
  304. EXT4_PARTITION *Partition;
  305. Partition = (EXT4_PARTITION *)This;
  306. Status = Ext4ReadInode (Partition, EXT4_ROOT_INODE_NR, &RootInode);
  307. if (EFI_ERROR (Status)) {
  308. DEBUG ((DEBUG_ERROR, "[ext4] Could not open root inode - error %r\n", Status));
  309. return Status;
  310. }
  311. RootDir = AllocateZeroPool (sizeof (EXT4_FILE));
  312. if (RootDir == NULL) {
  313. FreePool (RootInode);
  314. return EFI_OUT_OF_RESOURCES;
  315. }
  316. RootDir->Inode = RootInode;
  317. RootDir->InodeNum = EXT4_ROOT_INODE_NR;
  318. Status = Ext4InitExtentsMap (RootDir);
  319. if (EFI_ERROR (Status)) {
  320. FreePool (RootInode);
  321. FreePool (RootDir);
  322. return EFI_OUT_OF_RESOURCES;
  323. }
  324. Ext4SetupFile (RootDir, Partition);
  325. *Root = &RootDir->Protocol;
  326. InsertTailList (&Partition->OpenFiles, &RootDir->OpenFilesListNode);
  327. ASSERT (Partition->RootDentry != NULL);
  328. RootDir->Dentry = Partition->RootDentry;
  329. Ext4RefDentry (RootDir->Dentry);
  330. return EFI_SUCCESS;
  331. }
  332. /**
  333. Reads a directory entry.
  334. @param[in] Partition Pointer to the ext4 partition.
  335. @param[in] File Pointer to the open directory.
  336. @param[out] Buffer Pointer to the output buffer.
  337. @param[in] Offset Initial directory position.
  338. @param[in out] OutLength Pointer to a UINTN that contains the length of the buffer,
  339. and the length of the actual EFI_FILE_INFO after the call.
  340. @return Result of the operation.
  341. **/
  342. EFI_STATUS
  343. Ext4ReadDir (
  344. IN EXT4_PARTITION *Partition,
  345. IN EXT4_FILE *File,
  346. OUT VOID *Buffer,
  347. IN UINT64 Offset,
  348. IN OUT UINTN *OutLength
  349. )
  350. {
  351. EXT4_INODE *DirIno;
  352. EFI_STATUS Status;
  353. UINT64 DirInoSize;
  354. UINTN Len;
  355. UINT32 BlockRemainder;
  356. EXT4_DIR_ENTRY Entry;
  357. EXT4_FILE *TempFile;
  358. BOOLEAN ShouldSkip;
  359. BOOLEAN IsDotOrDotDot;
  360. CHAR16 DirentUcs2Name[EXT4_NAME_MAX + 1];
  361. DirIno = File->Inode;
  362. Status = EFI_SUCCESS;
  363. DirInoSize = EXT4_INODE_SIZE (DirIno);
  364. DivU64x32Remainder (DirInoSize, Partition->BlockSize, &BlockRemainder);
  365. if (BlockRemainder != 0) {
  366. // Directory inodes need to have block aligned sizes
  367. return EFI_VOLUME_CORRUPTED;
  368. }
  369. while (TRUE) {
  370. TempFile = NULL;
  371. // We (try to) read the maximum size of a directory entry at a time
  372. // Note that we don't need to read any padding that may exist after it.
  373. Len = sizeof (Entry);
  374. Status = Ext4Read (Partition, File, &Entry, Offset, &Len);
  375. if (EFI_ERROR (Status)) {
  376. goto Out;
  377. }
  378. if (Len == 0) {
  379. *OutLength = 0;
  380. Status = EFI_SUCCESS;
  381. goto Out;
  382. }
  383. if (Len < EXT4_MIN_DIR_ENTRY_LEN) {
  384. Status = EFI_VOLUME_CORRUPTED;
  385. goto Out;
  386. }
  387. // Invalid directory entry length
  388. if (!Ext4ValidDirent (&Entry)) {
  389. DEBUG ((DEBUG_ERROR, "[ext4] Invalid dirent at offset %lu\n", Offset));
  390. Status = EFI_VOLUME_CORRUPTED;
  391. goto Out;
  392. }
  393. // Check if the entire dir entry length fits in Len
  394. if (Len < (UINTN)(EXT4_MIN_DIR_ENTRY_LEN + Entry.name_len)) {
  395. Status = EFI_VOLUME_CORRUPTED;
  396. goto Out;
  397. }
  398. // We don't care about passing . or .. entries to the caller of ReadDir(),
  399. // since they're generally useless entries *and* may break things if too
  400. // many callers assume FAT32.
  401. // Entry.name_len may be 0 if it's a nameless entry, like an unused entry
  402. // or a checksum at the end of the directory block.
  403. // memcmp (and CompareMem) return 0 when the passed length is 0.
  404. // We must bound name_len as > 0 and <= 2 to avoid any out-of-bounds accesses or bad detection of
  405. // "." and "..".
  406. IsDotOrDotDot = Entry.name_len > 0 && Entry.name_len <= 2 &&
  407. CompareMem (Entry.name, "..", Entry.name_len) == 0;
  408. // When inode = 0, it's unused. When name_len == 0, it's a nameless entry
  409. // (which we should not expose to ReadDir).
  410. ShouldSkip = Entry.inode == 0 || Entry.name_len == 0 || IsDotOrDotDot;
  411. if (ShouldSkip) {
  412. Offset += Entry.rec_len;
  413. continue;
  414. }
  415. // Test if the dirent is valid utf-8. This is already done inside Ext4OpenDirent but EFI_INVALID_PARAMETER
  416. // has the danger of its meaning being overloaded in many places, so we can't skip according to that.
  417. // So test outside of it, explicitly.
  418. Status = Ext4GetUcs2DirentName (&Entry, DirentUcs2Name);
  419. if (EFI_ERROR (Status)) {
  420. if (Status == EFI_INVALID_PARAMETER) {
  421. // Bad UTF-8, skip.
  422. Offset += Entry.rec_len;
  423. continue;
  424. }
  425. goto Out;
  426. }
  427. Status = Ext4OpenDirent (Partition, EFI_FILE_MODE_READ, &TempFile, &Entry, File);
  428. if (EFI_ERROR (Status)) {
  429. goto Out;
  430. }
  431. Status = Ext4GetFileInfo (TempFile, Buffer, OutLength);
  432. if (!EFI_ERROR (Status)) {
  433. File->Position = Offset + Entry.rec_len;
  434. }
  435. Ext4CloseInternal (TempFile);
  436. goto Out;
  437. }
  438. Status = EFI_SUCCESS;
  439. Out:
  440. return Status;
  441. }
  442. /**
  443. Removes a dentry from the other's list.
  444. @param[in out] Parent Pointer to the parent EXT4_DENTRY.
  445. @param[in out] ToBeRemoved Pointer to the child EXT4_DENTRY.
  446. **/
  447. STATIC
  448. VOID
  449. Ext4RemoveDentry (
  450. IN OUT EXT4_DENTRY *Parent,
  451. IN OUT EXT4_DENTRY *ToBeRemoved
  452. )
  453. {
  454. ASSERT (IsNodeInList (&ToBeRemoved->ListNode, &Parent->Children));
  455. RemoveEntryList (&ToBeRemoved->ListNode);
  456. }
  457. /**
  458. Adds a dentry to the other's list.
  459. The dentry that is added to the other one's list gets ->Parent set to Parent,
  460. and the parent gets its reference count incremented.
  461. @param[in out] Parent Pointer to the parent EXT4_DENTRY.
  462. @param[in out] ToBeAdded Pointer to the child EXT4_DENTRY.
  463. **/
  464. STATIC
  465. VOID
  466. Ext4AddDentry (
  467. IN OUT EXT4_DENTRY *Parent,
  468. IN OUT EXT4_DENTRY *ToBeAdded
  469. )
  470. {
  471. ToBeAdded->Parent = Parent;
  472. InsertTailList (&Parent->Children, &ToBeAdded->ListNode);
  473. Ext4RefDentry (Parent);
  474. }
  475. /**
  476. Creates a new dentry object.
  477. @param[in] Name Name of the dentry.
  478. @param[in out opt] Parent Parent dentry, if it's not NULL.
  479. @return The new allocated and initialised dentry.
  480. The ref count will be set to 1.
  481. **/
  482. EXT4_DENTRY *
  483. Ext4CreateDentry (
  484. IN CONST CHAR16 *Name,
  485. IN OUT EXT4_DENTRY *Parent OPTIONAL
  486. )
  487. {
  488. EXT4_DENTRY *Dentry;
  489. EFI_STATUS Status;
  490. Dentry = AllocateZeroPool (sizeof (EXT4_DENTRY));
  491. if (Dentry == NULL) {
  492. return NULL;
  493. }
  494. Dentry->RefCount = 1;
  495. // This StrCpyS should not fail.
  496. Status = StrCpyS (Dentry->Name, ARRAY_SIZE (Dentry->Name), Name);
  497. ASSERT_EFI_ERROR (Status);
  498. InitializeListHead (&Dentry->Children);
  499. if (Parent != NULL) {
  500. Ext4AddDentry (Parent, Dentry);
  501. }
  502. DEBUG ((DEBUG_FS, "[ext4] Created dentry %s\n", Name));
  503. return Dentry;
  504. }
  505. /**
  506. Increments the ref count of the dentry.
  507. @param[in out] Dentry Pointer to a valid EXT4_DENTRY.
  508. **/
  509. VOID
  510. Ext4RefDentry (
  511. IN OUT EXT4_DENTRY *Dentry
  512. )
  513. {
  514. UINTN OldRef;
  515. OldRef = Dentry->RefCount;
  516. Dentry->RefCount++;
  517. // I'm not sure if this (Refcount overflow) is a valid concern,
  518. // but it's better to be safe than sorry.
  519. ASSERT (OldRef < Dentry->RefCount);
  520. }
  521. /**
  522. Deletes the dentry.
  523. @param[in out] Dentry Pointer to a valid EXT4_DENTRY.
  524. **/
  525. STATIC
  526. VOID
  527. Ext4DeleteDentry (
  528. IN OUT EXT4_DENTRY *Dentry
  529. )
  530. {
  531. if (Dentry->Parent) {
  532. Ext4RemoveDentry (Dentry->Parent, Dentry);
  533. Ext4UnrefDentry (Dentry->Parent);
  534. }
  535. DEBUG ((DEBUG_FS, "[ext4] Deleted dentry %s\n", Dentry->Name));
  536. FreePool (Dentry);
  537. }
  538. /**
  539. Decrements the ref count of the dentry.
  540. If the ref count is 0, it's destroyed.
  541. @param[in out] Dentry Pointer to a valid EXT4_DENTRY.
  542. @retval True if it was destroyed, false if it's alive.
  543. **/
  544. BOOLEAN
  545. Ext4UnrefDentry (
  546. IN OUT EXT4_DENTRY *Dentry
  547. )
  548. {
  549. Dentry->RefCount--;
  550. if (Dentry->RefCount == 0) {
  551. Ext4DeleteDentry (Dentry);
  552. return TRUE;
  553. }
  554. return FALSE;
  555. }