The Pedigree Project 0.1
Ext2Node.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef EXT2_NODE_H
21#define EXT2_NODE_H
22
23#include "pedigree/kernel/process/Mutex.h"
24#include "pedigree/kernel/processor/types.h"
25#include "pedigree/kernel/utilities/Vector.h"
26
27#include "ext2.h"
28#include "modules/system/vfs/File.h"
29
30struct Inode;
31class Ext2File;
32class Ext2Filesystem;
33
35 Ext2InodeState(Inode* inode, Ext2Filesystem* filesystem);
37 void reloadMappings(Inode* inode, Ext2Filesystem* filesystem);
38 void loadMappings(Ext2Filesystem* filesystem, uint32_t block, unsigned depth, size_t first,
39 size_t span);
40 Vector<uint32_t> blocks;
41 uint32_t metadataBlocks;
42 uint32_t allocatedDataBlocks;
43 size_t size;
44 bool allocationValid = true;
45 Mutex dataLock;
46 Mutex writeLock;
47 Mutex writebackLock;
48 Vector<uint32_t> namespaceSyncBlocks;
49 size_t pageLoans;
50 size_t references;
51 size_t syncReferences = 0;
52 bool orphan;
53 bool quotaFile = false;
54 bool quotaInternalWrite = false;
55 InodeEventSource inodeEvents;
56 uintptr_t futexIdentity;
58 File::CacheState* cache = nullptr;
59 Inode* metadata;
60 Inode removedMetadata = {};
61 Ext2Filesystem* filesystem;
62};
63
65class Ext2Node {
66 friend class Ext2Filesystem;
67
68 private:
70 Ext2Node(uintptr_t inode, Inode* metadata, Ext2Filesystem* filesystem, Ext2InodeState& admitted);
71 Ext2Node(const Ext2Node& file);
72 Ext2Node& operator=(const Ext2Node&);
73
74 public:
76 Ext2Node(uintptr_t inode_num, Inode* pInode, class Ext2Filesystem* pFs);
78 virtual ~Ext2Node();
79
80 Inode* getInode() {
81 return m_pInode;
82 }
83
84 uint32_t getInodeNumber() {
85 return m_InodeNumber;
86 }
87
89 void fileAttributeChanged(size_t size, size_t atime, size_t mtime, size_t ctime);
90
91 File::Attributes inodeAttributes() const;
92 void updateInodeAttributes(const File::Attributes& attributes, uint32_t mask);
93 bool changeInodeOwnership(size_t uid, size_t gid, bool changeUid, bool changeGid);
94
96 bool wipe(bool allocationLockHeld = false);
97
98 void extend(size_t newSize);
99 void extend(size_t newSize, uint64_t location, uint64_t size);
100
101 uint64_t maximumFileSize() const;
102
103 static bool decodeAllocation(const Inode&, uint32_t blockSize, uint32_t& blocks,
104 bool& inlineSymlink);
105 static bool encodeAllocation(uint32_t data, uint32_t indirect, bool hasEa, uint32_t blockSize,
106 uint32_t& sectors);
107 bool isInlineSymlink() const;
108 void updateAllocatedSectorCount();
109 XattrStatus getXattr(const StringView&, void*, size_t, size_t&);
110 XattrStatus listXattrs(void*, size_t, size_t&);
111 XattrStatus setXattr(const StringView&, const void*, size_t, unsigned);
112 XattrStatus removeXattr(const StringView&);
113
114 uintptr_t readBlock(uint64_t location);
115 void writeBlock(uint64_t location);
116
117 void trackBlock(uint32_t block, bool writeInode = true);
118
119 bool pinBlock(uint64_t location);
120 void unpinBlock(uint64_t location);
121
122 bool sync(size_t offset, bool async);
123
124 protected:
125 XattrStatus changeXattr(const StringView&, const void*, size_t, unsigned, bool remove);
126 bool resizeData(size_t size);
127 bool trimToBlocks(size_t keep, bool allocationLockHeld = false);
128 bool zeroRange(size_t start, size_t end);
129 struct MappingPage {
130 uint32_t block;
131 uintptr_t buffer;
132 size_t first;
133 size_t span;
134 unsigned depth;
135 };
136 bool collectMappingPages(uint32_t block, unsigned depth, size_t first, size_t span,
137 Vector<MappingPage>& pages);
138 struct TrimPlan {
139 explicit TrimPlan(Ext2Filesystem& filesystem);
140 ~TrimPlan();
141 Ext2Filesystem& filesystem;
142 size_t keep = 0;
143 size_t retainedData = 0;
145 Vector<uint32_t> retiredData;
146 };
147 class DataShrinkPlan;
148 bool prepareTrim(size_t keep, TrimPlan& plan, bool allocationLockHeld = false);
149 void commitTrim(TrimPlan& plan, bool allocationLockHeld = false);
150 bool prepareDataShrink(size_t size, UniquePointer<File::PreparedShrink>& prepared);
151
157 bool ensureLargeEnough(size_t size, uint64_t location, uint64_t opsize, bool onlyBlocks = false,
158 bool nozeroblocks = false);
159
160 bool addBlock(uint32_t blockValue, Vector<uint32_t>* pendingWrites = 0);
161
162 void writeBlockOrQueue(uint32_t block, Vector<uint32_t>* pendingWrites);
163
164 bool ensureBlockLoaded(size_t nBlock);
165 bool getBlockNumber(size_t nBlock);
166 bool getBlockNumberIndirect(uint32_t inode_block, size_t nBlocks, size_t nBlock);
167 bool getBlockNumberBiindirect(uint32_t inode_block, size_t nBlocks, size_t nBlock);
168 bool getBlockNumberTriindirect(uint32_t inode_block, size_t nBlocks, size_t nBlock);
169
170 bool setBlockNumber(size_t blockNum, uint32_t blockValue,
171 Vector<uint32_t>* pendingWrites = nullptr);
172 bool ensureWritableRange(size_t location, size_t length);
173
174 uint32_t modeToPermissions(uint32_t mode) const;
175 uint32_t permissionsToMode(uint32_t permissions) const;
176
177 Ext2InodeState* m_State;
178 Inode*& m_pInode;
179 uint32_t m_InodeNumber;
180 class Ext2Filesystem* m_pExt2Fs;
181
182 Vector<uint32_t>& m_Blocks;
183 uint32_t& m_nMetadataBlocks;
184
185 size_t& m_nSize;
186};
187
188#endif
bool wipe(bool allocationLockHeld=false)
Definition Ext2Node.cc:217
bool ensureLargeEnough(size_t size, uint64_t location, uint64_t opsize, bool onlyBlocks=false, bool nozeroblocks=false)
Definition Ext2Node.cc:258
void fileAttributeChanged(size_t size, size_t atime, size_t mtime, size_t ctime)
Definition Ext2Node.cc:567
virtual ~Ext2Node()
Definition Ext2Node.cc:155
Definition Mutex.h:56
A vector / dynamic array.
Definition Vector.h:33
Definition ext2.h:152