1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "nacl_io/fusefs/fuse_fs.h"
13 #include "nacl_io/getdents_helper.h"
14 #include "nacl_io/kernel_handle.h"
15 #include "nacl_io/log.h"
16 #include "sdk_util/macros.h"
23 FillDirInfo(GetDentsHelper* getdents, size_t num_bytes)
24 : getdents(getdents), num_bytes(num_bytes), wrote_offset(false) {}
26 GetDentsHelper* getdents;
33 FuseFs::FuseFs() : fuse_ops_(NULL), fuse_user_data_(NULL) {
36 Error FuseFs::Init(const FsInitArgs& args) {
37 Error error = Filesystem::Init(args);
41 fuse_ops_ = args.fuse_ops;
42 if (fuse_ops_ == NULL) {
43 LOG_ERROR("fuse_ops_ is NULL.");
47 if (fuse_ops_->init) {
48 struct fuse_conn_info info;
49 fuse_user_data_ = fuse_ops_->init(&info);
55 void FuseFs::Destroy() {
56 if (fuse_ops_ && fuse_ops_->destroy)
57 fuse_ops_->destroy(fuse_user_data_);
60 Error FuseFs::Access(const Path& path, int a_mode) {
61 if (!fuse_ops_->access) {
62 LOG_TRACE("fuse_ops_->access is NULL.");
66 int result = fuse_ops_->access(path.Join().c_str(), a_mode);
73 Error FuseFs::Open(const Path& path, int open_flags, ScopedNode* out_node) {
74 std::string path_str = path.Join();
75 const char* path_cstr = path_str.c_str();
78 struct fuse_file_info fi;
79 memset(&fi, 0, sizeof(fi));
80 fi.flags = open_flags;
82 if (open_flags & (O_CREAT | O_EXCL)) {
83 // According to the FUSE docs, open() is not called when O_CREAT or O_EXCL
85 mode_t mode = S_IRALL | S_IWALL;
86 if (fuse_ops_->create) {
87 result = fuse_ops_->create(path_cstr, mode, &fi);
90 } else if (fuse_ops_->mknod) {
91 result = fuse_ops_->mknod(path_cstr, mode, dev_);
95 LOG_TRACE("fuse_ops_->create and fuse_ops_->mknod are NULL.");
99 // First determine if this is a regular file or a directory.
100 if (fuse_ops_->getattr) {
102 result = fuse_ops_->getattr(path_cstr, &statbuf);
106 if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
107 // This is a directory. Don't try to open, just create a new node with
109 ScopedNode node(new DirFuseFsNode(this, fuse_ops_, fi, path_cstr));
110 Error error = node->Init(open_flags);
120 if (open_flags & O_TRUNC) {
121 // According to the FUSE docs, O_TRUNC does two calls: first truncate()
123 if (!fuse_ops_->truncate) {
124 LOG_TRACE("fuse_ops_->truncate is NULL.");
127 result = fuse_ops_->truncate(path_cstr, 0);
132 if (!fuse_ops_->open) {
133 LOG_TRACE("fuse_ops_->open is NULL.");
136 result = fuse_ops_->open(path_cstr, &fi);
141 ScopedNode node(new FileFuseFsNode(this, fuse_ops_, fi, path_cstr));
142 Error error = node->Init(open_flags);
150 Error FuseFs::Unlink(const Path& path) {
151 if (!fuse_ops_->unlink) {
152 LOG_TRACE("fuse_ops_->unlink is NULL.");
156 int result = fuse_ops_->unlink(path.Join().c_str());
163 Error FuseFs::Mkdir(const Path& path, int perm) {
164 if (!fuse_ops_->mkdir) {
165 LOG_TRACE("fuse_ops_->mkdir is NULL.");
169 int result = fuse_ops_->mkdir(path.Join().c_str(), perm);
176 Error FuseFs::Rmdir(const Path& path) {
177 if (!fuse_ops_->rmdir) {
178 LOG_TRACE("fuse_ops_->rmdir is NULL.");
182 int result = fuse_ops_->rmdir(path.Join().c_str());
189 Error FuseFs::Remove(const Path& path) {
191 Error error = Open(path, O_RDONLY, &node);
196 error = node->GetStat(&statbuf);
202 if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
209 Error FuseFs::Rename(const Path& path, const Path& newpath) {
210 if (!fuse_ops_->rename) {
211 LOG_TRACE("fuse_ops_->rename is NULL.");
215 int result = fuse_ops_->rename(path.Join().c_str(), newpath.Join().c_str());
222 FuseFsNode::FuseFsNode(Filesystem* filesystem,
223 struct fuse_operations* fuse_ops,
224 struct fuse_file_info& info,
225 const std::string& path)
226 : Node(filesystem), fuse_ops_(fuse_ops), info_(info), path_(path) {
229 bool FuseFsNode::CanOpen(int open_flags) {
231 Error error = GetStat(&statbuf);
235 // GetStat cached the mode in stat_.st_mode. Forward to Node::CanOpen,
236 // which will check this mode against open_flags.
237 return Node::CanOpen(open_flags);
240 Error FuseFsNode::GetStat(struct stat* stat) {
242 if (fuse_ops_->fgetattr) {
243 result = fuse_ops_->fgetattr(path_.c_str(), stat, &info_);
246 } else if (fuse_ops_->getattr) {
247 result = fuse_ops_->getattr(path_.c_str(), stat);
251 LOG_TRACE("fuse_ops_->fgetattr and fuse_ops_->getattr are NULL.");
255 // Also update the cached stat values.
260 Error FuseFsNode::VIoctl(int request, va_list args) {
261 LOG_ERROR("Ioctl not implemented for fusefs.");
265 Error FuseFsNode::Tcflush(int queue_selector) {
266 LOG_ERROR("Tcflush not implemented for fusefs.");
270 Error FuseFsNode::Tcgetattr(struct termios* termios_p) {
271 LOG_ERROR("Tcgetattr not implemented for fusefs.");
275 Error FuseFsNode::Tcsetattr(int optional_actions,
276 const struct termios* termios_p) {
277 LOG_ERROR("Tcsetattr not implemented for fusefs.");
281 Error FuseFsNode::GetSize(off_t* out_size) {
283 Error error = GetStat(&statbuf);
287 *out_size = stat_.st_size;
291 FileFuseFsNode::FileFuseFsNode(Filesystem* filesystem,
292 struct fuse_operations* fuse_ops,
293 struct fuse_file_info& info,
294 const std::string& path)
295 : FuseFsNode(filesystem, fuse_ops, info, path) {
298 void FileFuseFsNode::Destroy() {
299 if (!fuse_ops_->release)
301 fuse_ops_->release(path_.c_str(), &info_);
304 Error FileFuseFsNode::FSync() {
305 if (!fuse_ops_->fsync) {
306 LOG_ERROR("fuse_ops_->fsync is NULL.");
311 int result = fuse_ops_->fsync(path_.c_str(), datasync, &info_);
317 Error FileFuseFsNode::FTruncate(off_t length) {
318 if (!fuse_ops_->ftruncate) {
319 LOG_ERROR("fuse_ops_->ftruncate is NULL.");
323 int result = fuse_ops_->ftruncate(path_.c_str(), length, &info_);
329 Error FileFuseFsNode::Read(const HandleAttr& attr,
333 if (!fuse_ops_->read) {
334 LOG_ERROR("fuse_ops_->read is NULL.");
338 char* cbuf = static_cast<char*>(buf);
340 int result = fuse_ops_->read(path_.c_str(), cbuf, count, attr.offs, &info_);
344 // Fuse docs say that a read() call will always completely fill the buffer
345 // (padding with zeroes) unless the direct_io filesystem flag is set.
346 // TODO(binji): support the direct_io flag
347 if (static_cast<size_t>(result) < count)
348 memset(&cbuf[result], 0, count - result);
354 Error FileFuseFsNode::Write(const HandleAttr& attr,
358 if (!fuse_ops_->write) {
359 LOG_ERROR("fuse_ops_->write is NULL.");
363 int result = fuse_ops_->write(
364 path_.c_str(), static_cast<const char*>(buf), count, attr.offs, &info_);
368 // Fuse docs say that a write() call will always write the entire buffer
369 // unless the direct_io filesystem flag is set.
370 // TODO(binji): What should we do if the user breaks this contract? Warn?
371 // TODO(binji): support the direct_io flag
376 DirFuseFsNode::DirFuseFsNode(Filesystem* filesystem,
377 struct fuse_operations* fuse_ops,
378 struct fuse_file_info& info,
379 const std::string& path)
380 : FuseFsNode(filesystem, fuse_ops, info, path) {
383 void DirFuseFsNode::Destroy() {
384 if (!fuse_ops_->releasedir)
386 fuse_ops_->releasedir(path_.c_str(), &info_);
389 Error DirFuseFsNode::FSync() {
390 if (!fuse_ops_->fsyncdir) {
391 LOG_ERROR("fuse_ops_->fsyncdir is NULL.");
396 int result = fuse_ops_->fsyncdir(path_.c_str(), datasync, &info_);
402 Error DirFuseFsNode::GetDents(size_t offs,
406 if (!fuse_ops_->readdir) {
407 LOG_ERROR("fuse_ops_->readdir is NULL.");
411 bool opened_dir = false;
414 // Opendir is not necessary, only readdir. Call it anyway, if it is defined.
415 if (fuse_ops_->opendir) {
416 result = fuse_ops_->opendir(path_.c_str(), &info_);
424 GetDentsHelper getdents;
425 FillDirInfo fill_info(&getdents, count);
426 result = fuse_ops_->readdir(
427 path_.c_str(), &fill_info, &DirFuseFsNode::FillDirCallback, offs, &info_);
431 // If the fill function ever wrote an entry with |offs| != 0, then assume it
432 // was not given the full list of entries. In that case, GetDentsHelper's
433 // buffers start with the entry at offset |offs|, so the call to
434 // GetDentsHelpers::GetDents should use an offset of 0.
435 if (fill_info.wrote_offset)
438 // The entries have been filled in from the FUSE filesystem, now write them
439 // out to the buffer.
440 error = getdents.GetDents(offs, pdir, count, out_bytes);
447 if (opened_dir && fuse_ops_->releasedir) {
448 // Ignore this result, we're already failing.
449 fuse_ops_->releasedir(path_.c_str(), &info_);
455 int DirFuseFsNode::FillDirCallback(void* buf,
457 const struct stat* stbuf,
459 FillDirInfo* fill_info = static_cast<FillDirInfo*>(buf);
461 // It is OK for the FUSE filesystem to pass a NULL stbuf. In that case, just
463 ino_t ino = stbuf ? stbuf->st_ino : 1;
465 // The FUSE docs say that the implementor of readdir can choose to ignore the
466 // offset given, and instead return all entries. To do this, they pass
467 // |off| == 0 for each call.
469 if (fill_info->num_bytes < sizeof(dirent))
470 return 1; // 1 => buffer is full
472 fill_info->wrote_offset = true;
473 fill_info->getdents->AddDirent(ino, name, strlen(name));
474 fill_info->num_bytes -= sizeof(dirent);
475 // return 0 => request more data. return 1 => buffer full.
476 return fill_info->num_bytes > 0 ? 0 : 1;
478 fill_info->getdents->AddDirent(ino, name, strlen(name));
479 fill_info->num_bytes -= sizeof(dirent);
480 // According to the docs, we can never return 1 (buffer full) when the
481 // offset is zero (the user is probably ignoring the result anyway).
486 } // namespace nacl_io