OLD | NEW |
---|---|
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | |
6 #include <assert.h> | |
7 #include <dirent.h> | 5 #include <dirent.h> |
8 #include <errno.h> | 6 #include <errno.h> |
9 #include <fcntl.h> | 7 #include <fcntl.h> |
10 #include <sys/stat.h> | 8 #include <sys/stat.h> |
11 #include <string> | 9 #include <string> |
12 | 10 |
13 #include <vector> | 11 #include "mount.h" |
14 | |
15 #include "mount_mem.h" | 12 #include "mount_mem.h" |
13 #include "mount_node.h" | |
14 #include "mount_node_dir.h" | |
15 #include "mount_node_mem.h" | |
16 #include "path.h" | 16 #include "path.h" |
17 #include "util/simple_auto_lock.h" | 17 |
18 | 18 #include "auto_lock.h" |
19 static const size_t s_blksize = 1024; | 19 #include "ref_object.h" |
20 | 20 |
21 class MountNodeMem { | 21 // TODO(noelallen) : Grab/Redefine these in the kernel object once available. |
22 public: | 22 #define USR_ID 1002 |
23 MountNodeMem(int ino) { | 23 #define GRP_ID 1003 |
24 ino_ = ino; | 24 |
25 ref_count_ = 0; | 25 MountMem::MountMem(int dev) |
26 data_ = NULL; | 26 : Mount(dev), |
27 memset(&stat_, 0, sizeof(stat_)); | 27 root_(NULL), |
28 Resize(s_blksize); | 28 max_ino_(0) { |
29 }; | 29 } |
30 | 30 |
31 size_t Size() const { | 31 bool MountMem::Init(const std::string& args) { |
32 return stat_.st_size; | 32 root_ = AllocatePath(_S_IREAD | _S_IWRITE); |
33 } | 33 return (bool) (root_ != NULL); |
34 | 34 } |
35 void Resize(size_t size) { | 35 |
36 size_t cap = blocks_; | 36 void MountMem::Destroy() { |
37 size_t req = (size + s_blksize - 1) / s_blksize; | 37 if (root_) |
38 | 38 root_->Release(); |
39 if ((req > cap) || (req < (cap / 2))) { | 39 root_ = NULL; |
40 char *newdata = (char *) malloc(req * s_blksize); | 40 } |
41 memcpy(newdata, data_, size); | 41 |
42 free(data_); | 42 MountNode* MountMem::AllocatePath(int mode) { |
43 stat_.st_size = size; | 43 ino_t ino = AllocateINO(); |
44 data_ = newdata; | 44 |
45 blocks_ = req; | 45 MountNode *ptr = new MountNodeDir(this, ino, dev_); |
46 if (!ptr->Init(mode, 1002, 1003)) { | |
binji
2012/04/23 17:51:40
USR_ID, GRP_ID
noelallen1
2012/05/02 23:35:24
Done.
| |
47 ptr->Release(); | |
48 FreeINO(ino); | |
49 return NULL; | |
50 } | |
51 return ptr; | |
52 } | |
53 | |
54 MountNode* MountMem::AllocateData(int mode) { | |
55 ino_t ino = AllocateINO(); | |
56 | |
57 MountNode* ptr = new MountNodeMem(this, ino, dev_); | |
58 if (!ptr->Init(mode, USR_ID, GRP_ID)) { | |
59 ptr->Release(); | |
60 FreeINO(ino); | |
61 return NULL; | |
62 } | |
63 return ptr; | |
64 } | |
65 | |
66 void MountMem::ReleaseNode(MountNode* node) { | |
67 node->Release(); | |
68 } | |
69 | |
70 int MountMem::AllocateINO() { | |
71 const int INO_CNT = 8; | |
72 | |
73 // If we run out of INO numbers, then allocate 8 more | |
74 if (inos_.size() == 0) { | |
75 max_ino_ += INO_CNT; | |
76 // Add eight more to the stack in reverse order, offset by 1 | |
77 // since '0' refers to no INO. | |
78 for (int a = 0; a < INO_CNT; a++) { | |
79 inos_.push_back((max_ino_ - a) + 1); | |
binji
2012/04/23 17:51:40
If I'm reading this right, when this is first call
noelallen1
2012/05/02 23:35:24
Done.
| |
46 } | 80 } |
47 } | 81 } |
48 | 82 |
49 int ino_; | 83 // Return the INO at the top of the stack. |
50 int ref_count_; | 84 int val = inos_.back(); |
51 struct stat stat_; | 85 inos_.pop_back(); |
52 void *data_; | 86 return val; |
53 size_t blocks_; | 87 } |
54 pthread_mutex_t lock_; | 88 |
55 }; | 89 void MountMem::FreeINO(int ino) { |
56 | 90 inos_.push_back(ino); |
57 MountMem::MountMem() {} | 91 } |
58 MountMem::~MountMem() {} | 92 |
59 | 93 MountNode* MountMem::FindNode(const Path& path, int type) { |
60 int MountMem::AddDirEntry(MountMemNode* dir_node, MountMemNode* obj_node, const char *name) { | 94 MountNode* node = root_; |
61 if (strlen(name > 255)) { | 95 |
96 // If there is no root there, we have an error. | |
97 if (node == NULL) { | |
98 errno = ENOTDIR; | |
99 return NULL; | |
100 } | |
101 | |
102 // We are expecting an "absolute" path from this mount point. | |
103 if (!path.IsAbsolute()) { | |
62 errno = EINVAL; | 104 errno = EINVAL; |
63 return -1; | 105 return NULL; |
64 } | 106 } |
65 | 107 |
66 struct dirent* d = (struct dirent *) dir_node->data_; | 108 // Starting at the root, traverse the path parts. |
67 size_t cnt = dir_node->Size() / sizeof(struct dirent); | 109 for (size_t index = 1; node && index < path.Size(); index++) { |
68 size_t off; | 110 // If not a directory, then we have an error so return. |
69 | 111 if (!node->IsaDir()) { |
70 // Find a free location | 112 errno = ENOTDIR; |
71 for (off = 0; off < cnt; off++) { | 113 return NULL; |
72 if (d->d_name[0] == 0) break; | |
73 d++; | |
74 } | |
75 | |
76 // Otherwise regrow and take the last spot. | |
77 if (off == cnt) { | |
78 dir_node->Resize(dir_node->Size() + sizeof(struct dirent)); | |
79 d = &((struct dirent *) dir_node->data_)[off]; | |
80 } | |
81 | |
82 strcpy(d->d_name, name); | |
83 d->d_ino = obj_node->ino_; | |
84 d->d_reclen = sizeof(dirent); | |
85 d->d_off = off * sizeof(dirent); | |
86 | |
87 // Add a ref_count_ and link count for the directory | |
88 obj_node->Acquire(); | |
89 obj_node->stat_.st_nlink++; | |
90 return 0; | |
91 } | |
92 | |
93 | |
94 | |
95 | |
96 int MountMem::DelDirEntry_locked(int dir_ino, const char *name) { | |
97 MountMemNode* dir_node = inodes_.At(dir_ino); | |
98 struct dirent* d = (struct dirent *) dir_node->data_; | |
99 size_t cnt = dir_node->Size() / sizeof(struct dirent); | |
100 size_t off = 0; | |
101 | |
102 // Find a free location | |
103 for (off = 0; off < cnt; off++) { | |
104 if (!strcmp(d->d_name, name)) { | |
105 d->d_name[0] = 0; | |
106 obj_node->stat_.st_nlink--; | |
107 if (0 == --obj_node->ref_count_) FreeNode(obj_node->ino_); | |
108 dir_node->ref_count_--; | |
109 return 0; | |
110 } | 114 } |
111 } | 115 |
112 | 116 // Find the child node |
113 errno = ENOENT; | 117 node = node->FindChild(path.Part(index)); |
114 return -1; | 118 } |
115 } | 119 |
116 | 120 // node should be root, a found child, or a failed 'FindChild' |
117 MountNodeMem* MountMem::AcquireNode(int ino) { | 121 // which already has the correct errno set. |
118 SimpleAutoLock lock(&lock_); | 122 if (NULL == node) return NULL; |
119 MountNodeMem *node = inodes_.At(ino); | 123 |
120 if (node) node->ref_count_++; | 124 // If a directory is expected, but it's not a directory, then fail. |
125 if ((type & _S_IFDIR) && !node->IsaDir()) { | |
126 errno = ENOTDIR; | |
127 return NULL; | |
128 } | |
129 | |
130 // If a file is expected, but it's not a file, then fail. | |
131 if ((type & _S_IFREG) && node->IsaDir()) { | |
132 errno = EISDIR; | |
133 return NULL; | |
134 } | |
135 | |
136 // We now have a valid object of the expected type, so return it. | |
121 return node; | 137 return node; |
122 } | 138 } |
123 | 139 |
124 void MountMem::ReleaseNode(MountMemNode* node) { | 140 MountNode* MountMem::Open(const Path& path, int mode) { |
125 if (node) { | 141 AutoLock lock(&lock_); |
126 SimpleAutoLock lock(&lock_); | 142 MountNode* node = FindNode(path); |
127 if (--node->ref_count_) inodes_.Free(node->ino_); | |
128 } | |
129 } | |
130 | |
131 void MountMem::Init(void) { | |
132 int root = inodes_.Alloc(); | |
133 | |
134 assert(root == 0); | |
135 AddDirEntry(root, root, "/"); | |
136 } | |
137 | |
138 int MountMem::Mkdir(const std::string& path, mode_t mode, struct stat *buf) { | |
139 SimpleAutoLock lock(&lock_); | |
140 | |
141 int ino = AcquireNode(path); | |
142 | |
143 // Make sure it doesn't already exist. | |
144 child = GetMemNode(path); | |
145 if (child) { | |
146 errno = EEXIST; | |
147 return -1; | |
148 } | |
149 // Get the parent node. | |
150 int parent_slot = GetParentSlot(path); | |
151 if (parent_slot == -1) { | |
152 errno = ENOENT; | |
153 return -1; | |
154 } | |
155 parent = slots_.At(parent_slot); | |
156 if (!parent->is_dir()) { | |
157 errno = ENOTDIR; | |
158 return -1; | |
159 } | |
160 | |
161 // Create a new node | |
162 int slot = slots_.Alloc(); | |
163 child = slots_.At(slot); | |
164 child->set_slot(slot); | |
165 child->set_mount(this); | |
166 child->set_is_dir(true); | |
167 Path p(path); | |
168 child->set_name(p.Last()); | |
169 child->set_parent(parent_slot); | |
170 parent->AddChild(slot); | |
171 if (!buf) { | |
172 return 0; | |
173 } | |
174 | |
175 return Stat(slot, buf); | |
176 } | |
177 | |
178 int MountMem::Rmdir(int node) { | |
179 } | |
180 | |
181 int MountMem::Chmod(int ino, int mode) { | |
182 MountMemNode* node = AcquireNode(ino); | |
183 | 143 |
184 if (NULL == node) { | 144 if (NULL == node) { |
185 errno = BADF; | 145 // Now first find the parent directory to see if we can add it |
186 return -1; | 146 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); |
187 } | 147 if (NULL == parent) return NULL; |
188 | 148 |
189 node->stat_.st_mode = mode; | 149 // If the node does not exist and we can't create it, fail |
150 if ((mode & O_CREAT) == 0) return NULL; | |
151 | |
152 // Otherwise, create it with a single refernece | |
153 mode = OpenModeToPermission(mode); | |
154 node = AllocateData(mode); | |
155 if (NULL == node) return NULL; | |
156 | |
157 if (parent->AddChild(path.Filename(), node) == -1) { | |
158 // Or if it fails, release it | |
159 node->Release(); | |
160 return NULL; | |
161 } | |
162 return node; | |
163 } | |
164 | |
165 // If we were expected to create it exclusively, fail | |
166 if (mode & O_EXCL) { | |
167 errno = EEXIST; | |
168 return NULL; | |
169 } | |
170 | |
171 // Verify we got the requested permisions. | |
172 int req_mode = OpenModeToPermission(mode); | |
173 int obj_mode = node->GetMode() & OpenModeToPermission(_O_RDWR); | |
174 if ((obj_mode & req_mode) != req_mode) { | |
175 errno = EACCES; | |
176 return NULL; | |
177 } | |
178 | |
179 // We opened it, so ref count it before passing it back. | |
180 node->Acquire(); | |
181 return node; | |
182 } | |
183 | |
184 int MountMem::Close(MountNode* node) { | |
185 AutoLock lock(&lock_); | |
186 node->Close(); | |
190 ReleaseNode(node); | 187 ReleaseNode(node); |
191 return 0; | 188 return 0; |
192 } | 189 } |
193 | 190 |
194 int MountMem::Stat(int ino, struct stat *buf) { | 191 int MountMem::Unlink(const Path& path) { |
195 MountMemNode* node = AcquireNode(ino); | 192 AutoLock lock(&lock_); |
196 | 193 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); |
194 | |
195 if (NULL == parent) return -1; | |
196 | |
197 MountNode* child = parent->FindChild(path.Filename()); | |
198 if (NULL == child) { | |
199 errno = ENOENT; | |
200 return -1; | |
201 } | |
202 if (child->IsaDir()) { | |
203 errno = EISDIR; | |
204 return -1; | |
205 } | |
206 return parent->RemoveChild(path.Filename()); | |
207 } | |
208 | |
209 int MountMem::Mkdir(const Path& path, int mode) { | |
210 AutoLock lock(&lock_); | |
211 | |
212 // We expect a Mount "absolute" path | |
213 if (!path.IsAbsolute()) { | |
214 errno = ENOENT; | |
215 return -1; | |
216 } | |
217 | |
218 // The root of the mount is already created by the mount | |
219 if (path.Size() == 1) { | |
220 errno = EEXIST; | |
221 return -1; | |
222 } | |
223 | |
224 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); | |
225 MountNode* node; | |
226 | |
227 // If we failed to find the parent, the error code is already set. | |
228 if (NULL == parent) return -1; | |
229 | |
230 node = parent->FindChild(path.Filename()); | |
231 if (NULL != node) { | |
232 errno = EEXIST; | |
233 return -1; | |
234 } | |
235 | |
236 // Otherwise, create a new node and attempt to add it | |
237 mode = OpenModeToPermission(mode); | |
238 | |
239 // Allocate a node, with a RefCount of 1. If added to the parent | |
240 // it will get ref counted again. In either case, release the | |
241 // recount we have on exit. | |
242 node = AllocatePath(_S_IREAD | _S_IWRITE); | |
243 if (NULL == node) return -1; | |
244 | |
245 if (parent->AddChild(path.Filename(), node) == -1) { | |
246 node->Release(); | |
247 return -1; | |
248 } | |
249 | |
250 node->Release(); | |
251 return 0; | |
252 } | |
253 | |
254 int MountMem::Rmdir(const Path& path) { | |
255 AutoLock lock(&lock_); | |
256 | |
257 // We expect a Mount "absolute" path | |
258 if (!path.IsAbsolute()) { | |
259 errno = ENOENT; | |
260 return -1; | |
261 } | |
262 | |
263 // The root of the mount is already created by the mount | |
264 if (path.Size() == 1) { | |
265 errno = EEXIST; | |
266 return -1; | |
267 } | |
268 | |
269 MountNode* parent = FindNode(path.Parent(), _S_IFDIR); | |
270 MountNode* node; | |
271 | |
272 // If we failed to find the parent, the error code is already set. | |
273 if (NULL == parent) return -1; | |
274 | |
275 // Verify we find a child which is also a directory | |
276 node = parent->FindChild(path.Filename()); | |
197 if (NULL == node) { | 277 if (NULL == node) { |
198 errno = BADF; | 278 errno = ENOENT; |
199 return -1; | 279 return -1; |
200 } | 280 } |
201 | 281 if (!node->IsaDir()) { |
202 memcpy(buf, node->stat_, sizeof(struct stat)); | 282 errno = ENOTDIR; |
203 ReleaseNode(node); | 283 return -1; |
204 return 0; | 284 } |
205 } | 285 if (node->ChildCount() > 0) { |
206 | 286 errno = ENOTEMPTY; |
207 int MountMem::Fsync(int ino) { | 287 return -1; |
208 // Acquire the node in case he node | 288 } |
209 MountMemNode* node = AcquireNode(ino); | 289 return parent->RemoveChild(path.Filename()); |
210 if (node) { | 290 } |
211 ReleaseNode(node); | |
212 return 0 | |
213 } | |
214 | |
215 errno = BADF; | |
216 return -1; | |
217 } | |
218 | |
219 int MountMem::Getdents(int ino, off_t offset, struct dirent *dirp, unsigned int count) { | |
220 MountMemNode* node = AcquireNode(ino); | |
221 if ((NULL == node) == 0) { | |
222 errno = EBADF; | |
223 return -1; | |
224 } | |
225 | |
226 if ((node->stat_.st_mode & S_IFDIR) == 0) { | |
227 errno =ENOTDIR; | |
228 return -1; | |
229 } | |
230 | |
231 if (offset + count > node->Size()) { | |
232 count = node->Size() - offset; | |
233 } | |
234 | |
235 memcpy(dirp, &node->data_[offset], count); | |
236 return count; | |
237 } | |
238 | |
239 ssize_t MountMem::Write(int ino, off_t offset, const void *buf, size_t count) { | |
240 MountMemNode* node = AcquireNode(ino); | |
241 | |
242 if (NULL == node || (node->stat_.st_mode & S_IWUSER) == 0) { | |
243 errno = EBADF; | |
244 return -1; | |
245 } | |
246 | |
247 if (offset + count > node->Size()) { | |
248 int err = node->Resize(); | |
249 if (err) { | |
250 errno = err; | |
251 ReleaseNode(node); | |
252 return -1; | |
253 } | |
254 } | |
255 | |
256 mempcy(&node->data_[offset], buf, count); | |
257 ReleaseNode(node); | |
258 return count; | |
259 } | |
260 | |
261 ssize_t MountMem::Read(int ino, off_t offset, const void *buf, size_t count) { | |
262 MountMemNode* node = AcquireNode(ino); | |
263 | |
264 if (NULL == node || (node->stat_.st_mode & S_IRUSER) == 0) { | |
265 errno = EBADF; | |
266 return -1; | |
267 } | |
268 | |
269 if (offset + count > node->Size()) { | |
270 count = node->Size() - offset; | |
271 } | |
272 | |
273 mempcy(buf, &node->data_[offset], count); | |
274 ReleaseNode(node); | |
275 return count; | |
276 } | |
277 | |
278 int MountMem::Isatty(int ino) { | |
279 // Acquire the node in case he node array is in flux | |
280 MountMemNode* node = AcquireNode(ino); | |
281 | |
282 if (node) { | |
283 errno = ENOTTY; | |
284 ReleaseNode(node); | |
285 } | |
286 else { | |
287 errno = BADF; | |
288 } | |
289 return 0; | |
290 } | |
291 | |
OLD | NEW |