mount_config.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fuse
  15. import (
  16. "fmt"
  17. "log"
  18. "runtime"
  19. "strings"
  20. "golang.org/x/net/context"
  21. )
  22. // Optional configuration accepted by Mount.
  23. type MountConfig struct {
  24. // The context from which every op read from the connetion by the sever
  25. // should inherit. If nil, context.Background() will be used.
  26. OpContext context.Context
  27. // If non-empty, the name of the file system as displayed by e.g. `mount`.
  28. // This is important because the `umount` command requires root privileges if
  29. // it doesn't agree with /etc/fstab.
  30. FSName string
  31. // Mount the file system in read-only mode. File modes will appear as normal,
  32. // but opening a file for writing and metadata operations like chmod,
  33. // chtimes, etc. will fail.
  34. ReadOnly bool
  35. // A logger to use for logging errors. All errors are logged, with the
  36. // exception of a few blacklisted errors that are expected. If nil, no error
  37. // logging is performed.
  38. ErrorLogger *log.Logger
  39. // A logger to use for logging debug information. If nil, no debug logging is
  40. // performed.
  41. DebugLogger *log.Logger
  42. // Linux only. OS X always behaves as if writeback caching is disabled.
  43. //
  44. // By default on Linux we allow the kernel to perform writeback caching
  45. // (cf. http://goo.gl/LdZzo1):
  46. //
  47. // * When the user calls write(2), the kernel sticks the user's data into
  48. // its page cache. Only later does it call through to the file system,
  49. // potentially after coalescing multiple small user writes.
  50. //
  51. // * The file system may receive multiple write ops from the kernel
  52. // concurrently if there is a lot of page cache data to flush.
  53. //
  54. // * Write performance may be significantly improved due to the user and
  55. // the kernel not waiting for serial round trips to the file system. This
  56. // is especially true if the user makes tiny writes.
  57. //
  58. // * close(2) (and anything else calling f_op->flush) causes all dirty
  59. // pages to be written out before it proceeds to send a FlushFileOp
  60. // (cf. https://goo.gl/TMrY6X).
  61. //
  62. // * Similarly, close(2) causes the kernel to send a setattr request
  63. // filling in the mtime if any dirty pages were flushed, since the time
  64. // at which the pages were written to the file system can't be trusted.
  65. //
  66. // * close(2) (and anything else calling f_op->flush) writes out all dirty
  67. // pages, then sends a setattr request with an appropriate mtime for
  68. // those writes if there were any, and only then proceeds to send a
  69. // flush.
  70. //
  71. // Code walk:
  72. //
  73. // * (https://goo.gl/zTIZQ9) fuse_flush calls write_inode_now before
  74. // calling the file system. The latter eventually calls into
  75. // __writeback_single_inode.
  76. //
  77. // * (https://goo.gl/L7Z2w5) __writeback_single_inode calls
  78. // do_writepages, which writes out any dirty pages.
  79. //
  80. // * (https://goo.gl/DOPgla) __writeback_single_inode later calls
  81. // write_inode, which calls into the superblock op struct's write_inode
  82. // member. For fuse, this is fuse_write_inode
  83. // (cf. https://goo.gl/eDSKOX).
  84. //
  85. // * (https://goo.gl/PbkGA1) fuse_write_inode calls fuse_flush_times.
  86. //
  87. // * (https://goo.gl/ig8x9V) fuse_flush_times sends a setttr request
  88. // for setting the inode's mtime.
  89. //
  90. // However, this brings along some caveats:
  91. //
  92. // * The file system must handle SetInodeAttributesOp or close(2) will fail,
  93. // due to the call chain into fuse_flush_times listed above.
  94. //
  95. // * The kernel caches mtime and ctime regardless of whether the file
  96. // system tells it to do so, disregarding the result of further getattr
  97. // requests (cf. https://goo.gl/3ZZMUw, https://goo.gl/7WtQUp). It
  98. // appears this may be true of the file size, too. Writeback caching may
  99. // therefore not be suitable for file systems where these attributes can
  100. // spontaneously change for reasons the kernel doesn't observe. See
  101. // http://goo.gl/V5WQCN for more discussion.
  102. //
  103. // Setting DisableWritebackCaching disables this behavior. Instead the file
  104. // system is called one or more times for each write(2), and the user's
  105. // syscall doesn't return until the file system returns.
  106. DisableWritebackCaching bool
  107. // OS X only.
  108. //
  109. // Normally on OS X we mount with the novncache option
  110. // (cf. http://goo.gl/1pTjuk), which disables entry caching in the kernel.
  111. // This is because osxfuse does not honor the entry expiration values we
  112. // return to it, instead caching potentially forever (cf.
  113. // http://goo.gl/8yR0Ie), and it is probably better to fail to cache than to
  114. // cache for too long, since the latter is more likely to hide consistency
  115. // bugs that are difficult to detect and diagnose.
  116. //
  117. // This field disables the use of novncache, restoring entry caching. Beware:
  118. // the value of ChildInodeEntry.EntryExpiration is ignored by the kernel, and
  119. // entries will be cached for an arbitrarily long time.
  120. EnableVnodeCaching bool
  121. // OS X only.
  122. //
  123. // The name of the mounted volume, as displayed in the Finder. If empty, a
  124. // default name involving the string 'osxfuse' is used.
  125. VolumeName string
  126. // Additional key=value options to pass unadulterated to the underlying mount
  127. // command. See `man 8 mount`, the fuse documentation, etc. for
  128. // system-specific information.
  129. //
  130. // For expert use only! May invalidate other guarantees made in the
  131. // documentation for this package.
  132. Options map[string]string
  133. }
  134. // Create a map containing all of the key=value mount options to be given to
  135. // the mount helper.
  136. func (c *MountConfig) toMap() (opts map[string]string) {
  137. isDarwin := runtime.GOOS == "darwin"
  138. opts = make(map[string]string)
  139. // Enable permissions checking in the kernel. See the comments on
  140. // InodeAttributes.Mode.
  141. opts["default_permissions"] = ""
  142. // HACK(jacobsa): Work around what appears to be a bug in systemd v219, as
  143. // shipped in Ubuntu 15.04, where it automatically unmounts any file system
  144. // that doesn't set an explicit name.
  145. //
  146. // When Ubuntu contains systemd v220, this workaround should be removed and
  147. // the systemd bug reopened if the problem persists.
  148. //
  149. // Cf. https://github.com/bazil/fuse/issues/89
  150. // Cf. https://bugs.freedesktop.org/show_bug.cgi?id=90907
  151. fsname := c.FSName
  152. if runtime.GOOS == "linux" && fsname == "" {
  153. fsname = "cloudmount"
  154. }
  155. // Special file system name?
  156. if fsname != "" {
  157. opts["fsname"] = fsname
  158. }
  159. // Read only?
  160. if c.ReadOnly {
  161. opts["ro"] = ""
  162. }
  163. // Handle OS X options.
  164. if isDarwin {
  165. if !c.EnableVnodeCaching {
  166. opts["novncache"] = ""
  167. }
  168. if c.VolumeName != "" {
  169. // Cf. https://github.com/osxfuse/osxfuse/wiki/Mount-options#volname
  170. opts["volname"] = c.VolumeName
  171. }
  172. }
  173. // OS X: disable the use of "Apple Double" (._foo and .DS_Store) files, which
  174. // just add noise to debug output and can have significant cost on
  175. // network-based file systems.
  176. //
  177. // Cf. https://github.com/osxfuse/osxfuse/wiki/Mount-options
  178. if isDarwin {
  179. opts["noappledouble"] = ""
  180. }
  181. // Last but not least: other user-supplied options.
  182. for k, v := range c.Options {
  183. opts[k] = v
  184. }
  185. return
  186. }
  187. func escapeOptionsKey(s string) (res string) {
  188. res = s
  189. res = strings.Replace(res, `\`, `\\`, -1)
  190. res = strings.Replace(res, `,`, `\,`, -1)
  191. return
  192. }
  193. // Create an options string suitable for passing to the mount helper.
  194. func (c *MountConfig) toOptionsString() string {
  195. var components []string
  196. for k, v := range c.toMap() {
  197. k = escapeOptionsKey(k)
  198. component := k
  199. if v != "" {
  200. component = fmt.Sprintf("%s=%s", k, v)
  201. }
  202. components = append(components, component)
  203. }
  204. return strings.Join(components, ",")
  205. }