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.
4
5#ifndef LIBRARIES_NACL_IO_FILESYSTEM_H_
6#define LIBRARIES_NACL_IO_FILESYSTEM_H_
7
8#include <map>
9#include <string>
10
11#include "nacl_io/error.h"
12#include "nacl_io/inode_pool.h"
13#include "nacl_io/node.h"
14#include "nacl_io/path.h"
15
16#include "sdk_util/macros.h"
17#include "sdk_util/ref_object.h"
18#include "sdk_util/scoped_ref.h"
19
20struct fuse_operations;
21
22namespace nacl_io {
23
24class Filesystem;
25class Node;
26class PepperInterface;
27
28typedef sdk_util::ScopedRef<Filesystem> ScopedFilesystem;
29typedef std::map<std::string, std::string> StringMap_t;
30
31// This structure is passed to all filesystems via the Filesystem::Init virtual
32// function.  With it, we can add or remove initialization values without
33// changing the function signature.
34struct FsInitArgs {
35  FsInitArgs() : dev(0), ppapi(NULL), fuse_ops(NULL) {}
36  explicit FsInitArgs(int dev) : dev(dev), ppapi(NULL), fuse_ops(NULL) {}
37
38  // Device number of the new filesystem.
39  int dev;
40  StringMap_t string_map;
41  PepperInterface* ppapi;
42  fuse_operations* fuse_ops;
43};
44
45// NOTE: The KernelProxy is the only class that should be setting errno. All
46// other classes should return Error (as defined by nacl_io/error.h).
47class Filesystem : public sdk_util::RefObject {
48 protected:
49  // The protected functions are only used internally and will not
50  // acquire or release the filesystem's lock.
51  Filesystem();
52  virtual ~Filesystem();
53
54  // Init must be called by the factory before the filesystem is used.
55  // |ppapi| can be NULL. If so, this filesystem cannot make any pepper calls.
56  virtual Error Init(const FsInitArgs& args);
57  virtual void Destroy();
58
59 public:
60  PepperInterface* ppapi() { return ppapi_; }
61  int dev() { return dev_; }
62
63  // All paths in functions below are expected to containing a leading "/".
64
65  // Open a node at |path| with the specified open and modeflags. The resulting
66  // Node is created with a ref count of 1.
67  // Assumes that |out_node| is non-NULL.
68  virtual Error OpenWithMode(const Path& path,
69                             int open_flags,
70                             mode_t mode,
71                             ScopedNode* out_node) = 0;
72
73  // Open a node at |path| with the specified open flags. The resulting
74  // Node is created with a ref count of 1.
75  // Assumes that |out_node| is non-NULL.
76  Error Open(const Path& path,
77             int open_flags,
78             ScopedNode* out_node);
79
80  // OpenResource is only used to read files from the NaCl NMF file. No
81  // filesystem except PassthroughFs should implement it.
82  // Assumes that |out_node| is non-NULL.
83  virtual Error OpenResource(const Path& path, ScopedNode* out_node);
84
85  // Unlink, Mkdir, Rmdir will affect the both the RefCount
86  // and the nlink number in the stat object.
87  virtual Error Unlink(const Path& path) = 0;
88  virtual Error Mkdir(const Path& path, int permissions) = 0;
89  virtual Error Rmdir(const Path& path) = 0;
90  virtual Error Remove(const Path& path) = 0;
91  virtual Error Rename(const Path& path, const Path& newpath) = 0;
92  virtual Error Filesystem_VIoctl(int request, va_list args);
93
94  // Helper function that forwards to Filesystem_VIoctl.
95  Error Filesystem_Ioctl(int request, ...);
96
97  // Assumes that |node| is non-NULL.
98  virtual void OnNodeCreated(Node* node);
99
100  // Assumes that |node| is non-NULL.
101  virtual void OnNodeDestroyed(Node* node);
102
103 protected:
104  // Device number for the filesystem.
105  int dev_;
106  PepperInterface* ppapi_;  // Weak reference.
107  INodePool inode_pool_;
108
109 private:
110  // May only be called by the KernelProxy when the Kernel's
111  // lock is held, so we make it private.
112  friend class KernelObject;
113  friend class KernelProxy;
114  DISALLOW_COPY_AND_ASSIGN(Filesystem);
115};
116
117}  // namespace nacl_io
118
119#endif  // LIBRARIES_NACL_IO_FILESYSTEM_H_
120