swarmfs_unix.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build linux darwin freebsd
  17. package fuse
  18. import (
  19. "errors"
  20. "fmt"
  21. "os"
  22. "path/filepath"
  23. "strings"
  24. "sync"
  25. "time"
  26. "bazil.org/fuse"
  27. "bazil.org/fuse/fs"
  28. "github.com/ethereum/go-ethereum/common"
  29. "github.com/ethereum/go-ethereum/log"
  30. "github.com/ethereum/go-ethereum/swarm/api"
  31. )
  32. var (
  33. errEmptyMountPoint = errors.New("need non-empty mount point")
  34. errMaxMountCount = errors.New("max FUSE mount count reached")
  35. errMountTimeout = errors.New("mount timeout")
  36. errAlreadyMounted = errors.New("mount point is already serving")
  37. )
  38. func isFUSEUnsupportedError(err error) bool {
  39. if perr, ok := err.(*os.PathError); ok {
  40. return perr.Op == "open" && perr.Path == "/dev/fuse"
  41. }
  42. return err == fuse.ErrOSXFUSENotFound
  43. }
  44. // information about every active mount
  45. type MountInfo struct {
  46. MountPoint string
  47. StartManifest string
  48. LatestManifest string
  49. rootDir *SwarmDir
  50. fuseConnection *fuse.Conn
  51. swarmApi *api.Api
  52. lock *sync.RWMutex
  53. }
  54. func NewMountInfo(mhash, mpoint string, sapi *api.Api) *MountInfo {
  55. newMountInfo := &MountInfo{
  56. MountPoint: mpoint,
  57. StartManifest: mhash,
  58. LatestManifest: mhash,
  59. rootDir: nil,
  60. fuseConnection: nil,
  61. swarmApi: sapi,
  62. lock: &sync.RWMutex{},
  63. }
  64. return newMountInfo
  65. }
  66. func (self *SwarmFS) Mount(mhash, mountpoint string) (*MountInfo, error) {
  67. if mountpoint == "" {
  68. return nil, errEmptyMountPoint
  69. }
  70. cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint))
  71. if err != nil {
  72. return nil, err
  73. }
  74. self.swarmFsLock.Lock()
  75. defer self.swarmFsLock.Unlock()
  76. noOfActiveMounts := len(self.activeMounts)
  77. if noOfActiveMounts >= maxFuseMounts {
  78. return nil, errMaxMountCount
  79. }
  80. if _, ok := self.activeMounts[cleanedMountPoint]; ok {
  81. return nil, errAlreadyMounted
  82. }
  83. log.Info(fmt.Sprintf("Attempting to mount %s ", cleanedMountPoint))
  84. _, manifestEntryMap, err := self.swarmApi.BuildDirectoryTree(mhash, true)
  85. if err != nil {
  86. return nil, err
  87. }
  88. mi := NewMountInfo(mhash, cleanedMountPoint, self.swarmApi)
  89. dirTree := map[string]*SwarmDir{}
  90. rootDir := NewSwarmDir("/", mi)
  91. dirTree["/"] = rootDir
  92. mi.rootDir = rootDir
  93. for suffix, entry := range manifestEntryMap {
  94. key := common.Hex2Bytes(entry.Hash)
  95. fullpath := "/" + suffix
  96. basepath := filepath.Dir(fullpath)
  97. parentDir := rootDir
  98. dirUntilNow := ""
  99. paths := strings.Split(basepath, "/")
  100. for i := range paths {
  101. if paths[i] != "" {
  102. thisDir := paths[i]
  103. dirUntilNow = dirUntilNow + "/" + thisDir
  104. if _, ok := dirTree[dirUntilNow]; !ok {
  105. dirTree[dirUntilNow] = NewSwarmDir(dirUntilNow, mi)
  106. parentDir.directories = append(parentDir.directories, dirTree[dirUntilNow])
  107. parentDir = dirTree[dirUntilNow]
  108. } else {
  109. parentDir = dirTree[dirUntilNow]
  110. }
  111. }
  112. }
  113. thisFile := NewSwarmFile(basepath, filepath.Base(fullpath), mi)
  114. thisFile.key = key
  115. parentDir.files = append(parentDir.files, thisFile)
  116. }
  117. fconn, err := fuse.Mount(cleanedMountPoint, fuse.FSName("swarmfs"), fuse.VolumeName(mhash))
  118. if isFUSEUnsupportedError(err) {
  119. log.Warn("Fuse not installed", "mountpoint", cleanedMountPoint, "err", err)
  120. return nil, err
  121. } else if err != nil {
  122. fuse.Unmount(cleanedMountPoint)
  123. log.Warn("Error mounting swarm manifest", "mountpoint", cleanedMountPoint, "err", err)
  124. return nil, err
  125. }
  126. mi.fuseConnection = fconn
  127. serverr := make(chan error, 1)
  128. go func() {
  129. log.Info(fmt.Sprintf("Serving %s at %s", mhash, cleanedMountPoint))
  130. filesys := &SwarmRoot{root: rootDir}
  131. if err := fs.Serve(fconn, filesys); err != nil {
  132. log.Warn(fmt.Sprintf("Could not Serve SwarmFileSystem error: %v", err))
  133. serverr <- err
  134. }
  135. }()
  136. // Check if the mount process has an error to report.
  137. select {
  138. case <-time.After(mountTimeout):
  139. fuse.Unmount(cleanedMountPoint)
  140. return nil, errMountTimeout
  141. case err := <-serverr:
  142. fuse.Unmount(cleanedMountPoint)
  143. log.Warn("Error serving swarm FUSE FS", "mountpoint", cleanedMountPoint, "err", err)
  144. return nil, err
  145. case <-fconn.Ready:
  146. log.Info("Now serving swarm FUSE FS", "manifest", mhash, "mountpoint", cleanedMountPoint)
  147. }
  148. self.activeMounts[cleanedMountPoint] = mi
  149. return mi, nil
  150. }
  151. func (self *SwarmFS) Unmount(mountpoint string) (*MountInfo, error) {
  152. self.swarmFsLock.Lock()
  153. defer self.swarmFsLock.Unlock()
  154. cleanedMountPoint, err := filepath.Abs(filepath.Clean(mountpoint))
  155. if err != nil {
  156. return nil, err
  157. }
  158. mountInfo := self.activeMounts[cleanedMountPoint]
  159. if mountInfo == nil || mountInfo.MountPoint != cleanedMountPoint {
  160. return nil, fmt.Errorf("%s is not mounted", cleanedMountPoint)
  161. }
  162. err = fuse.Unmount(cleanedMountPoint)
  163. if err != nil {
  164. err1 := externalUnmount(cleanedMountPoint)
  165. if err1 != nil {
  166. errStr := fmt.Sprintf("UnMount error: %v", err)
  167. log.Warn(errStr)
  168. return nil, err1
  169. }
  170. }
  171. mountInfo.fuseConnection.Close()
  172. delete(self.activeMounts, cleanedMountPoint)
  173. succString := fmt.Sprintf("UnMounting %v succeeded", cleanedMountPoint)
  174. log.Info(succString)
  175. return mountInfo, nil
  176. }
  177. func (self *SwarmFS) Listmounts() []*MountInfo {
  178. self.swarmFsLock.RLock()
  179. defer self.swarmFsLock.RUnlock()
  180. rows := make([]*MountInfo, 0, len(self.activeMounts))
  181. for _, mi := range self.activeMounts {
  182. rows = append(rows, mi)
  183. }
  184. return rows
  185. }
  186. func (self *SwarmFS) Stop() bool {
  187. for mp := range self.activeMounts {
  188. mountInfo := self.activeMounts[mp]
  189. self.Unmount(mountInfo.MountPoint)
  190. }
  191. return true
  192. }