Merge branch 'master' into 4467-project-contents-select-all
[arvados.git] / services / keepstore / volume_unix.go
1 // A UnixVolume is a Volume backed by a locally mounted disk.
2 //
3 package main
4
5 import (
6         "fmt"
7         "io"
8         "io/ioutil"
9         "log"
10         "os"
11         "path/filepath"
12         "strconv"
13         "strings"
14         "sync"
15         "syscall"
16         "time"
17 )
18
19 // A UnixVolume stores and retrieves blocks in a local directory.
20 type UnixVolume struct {
21         root      string // path to the volume's root directory
22         serialize bool
23         readonly  bool
24         mutex     sync.Mutex
25 }
26
27 func (v *UnixVolume) Touch(loc string) error {
28         if v.readonly {
29                 return MethodDisabledError
30         }
31         p := v.blockPath(loc)
32         f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
33         if err != nil {
34                 return err
35         }
36         defer f.Close()
37         if v.serialize {
38                 v.mutex.Lock()
39                 defer v.mutex.Unlock()
40         }
41         if e := lockfile(f); e != nil {
42                 return e
43         }
44         defer unlockfile(f)
45         now := time.Now().Unix()
46         utime := syscall.Utimbuf{now, now}
47         return syscall.Utime(p, &utime)
48 }
49
50 func (v *UnixVolume) Mtime(loc string) (time.Time, error) {
51         p := v.blockPath(loc)
52         if fi, err := os.Stat(p); err != nil {
53                 return time.Time{}, err
54         } else {
55                 return fi.ModTime(), nil
56         }
57 }
58
59 // Get retrieves a block identified by the locator string "loc", and
60 // returns its contents as a byte slice.
61 //
62 // If the block could not be found, opened, or read, Get returns a nil
63 // slice and whatever non-nil error was returned by Stat or ReadFile.
64 func (v *UnixVolume) Get(loc string) ([]byte, error) {
65         path := v.blockPath(loc)
66         if _, err := os.Stat(path); err != nil {
67                 return nil, err
68         }
69         if v.serialize {
70                 v.mutex.Lock()
71                 defer v.mutex.Unlock()
72         }
73         buf, err := ioutil.ReadFile(path)
74         return buf, err
75 }
76
77 // Put stores a block of data identified by the locator string
78 // "loc".  It returns nil on success.  If the volume is full, it
79 // returns a FullError.  If the write fails due to some other error,
80 // that error is returned.
81 func (v *UnixVolume) Put(loc string, block []byte) error {
82         if v.readonly {
83                 return MethodDisabledError
84         }
85         if v.IsFull() {
86                 return FullError
87         }
88         bdir := v.blockDir(loc)
89         if err := os.MkdirAll(bdir, 0755); err != nil {
90                 log.Printf("%s: could not create directory %s: %s",
91                         loc, bdir, err)
92                 return err
93         }
94
95         tmpfile, tmperr := ioutil.TempFile(bdir, "tmp"+loc)
96         if tmperr != nil {
97                 log.Printf("ioutil.TempFile(%s, tmp%s): %s", bdir, loc, tmperr)
98                 return tmperr
99         }
100         bpath := v.blockPath(loc)
101
102         if v.serialize {
103                 v.mutex.Lock()
104                 defer v.mutex.Unlock()
105         }
106         if _, err := tmpfile.Write(block); err != nil {
107                 log.Printf("%s: writing to %s: %s\n", v, bpath, err)
108                 tmpfile.Close()
109                 os.Remove(tmpfile.Name())
110                 return err
111         }
112         if err := tmpfile.Close(); err != nil {
113                 log.Printf("closing %s: %s\n", tmpfile.Name(), err)
114                 os.Remove(tmpfile.Name())
115                 return err
116         }
117         if err := os.Rename(tmpfile.Name(), bpath); err != nil {
118                 log.Printf("rename %s %s: %s\n", tmpfile.Name(), bpath, err)
119                 os.Remove(tmpfile.Name())
120                 return err
121         }
122         return nil
123 }
124
125 // Status returns a VolumeStatus struct describing the volume's
126 // current state.
127 //
128 func (v *UnixVolume) Status() *VolumeStatus {
129         var fs syscall.Statfs_t
130         var devnum uint64
131
132         if fi, err := os.Stat(v.root); err == nil {
133                 devnum = fi.Sys().(*syscall.Stat_t).Dev
134         } else {
135                 log.Printf("%s: os.Stat: %s\n", v, err)
136                 return nil
137         }
138
139         err := syscall.Statfs(v.root, &fs)
140         if err != nil {
141                 log.Printf("%s: statfs: %s\n", v, err)
142                 return nil
143         }
144         // These calculations match the way df calculates disk usage:
145         // "free" space is measured by fs.Bavail, but "used" space
146         // uses fs.Blocks - fs.Bfree.
147         free := fs.Bavail * uint64(fs.Bsize)
148         used := (fs.Blocks - fs.Bfree) * uint64(fs.Bsize)
149         return &VolumeStatus{v.root, devnum, free, used}
150 }
151
152 // IndexTo writes (to the given Writer) a list of blocks found on this
153 // volume which begin with the specified prefix. If the prefix is an
154 // empty string, IndexTo writes a complete list of blocks.
155 //
156 // Each block is given in the format
157 //
158 //     locator+size modification-time {newline}
159 //
160 // e.g.:
161 //
162 //     e4df392f86be161ca6ed3773a962b8f3+67108864 1388894303
163 //     e4d41e6fd68460e0e3fc18cc746959d2+67108864 1377796043
164 //     e4de7a2810f5554cd39b36d8ddb132ff+67108864 1388701136
165 //
166 func (v *UnixVolume) IndexTo(prefix string, w io.Writer) error {
167         return filepath.Walk(v.root,
168                 func(path string, info os.FileInfo, err error) error {
169                         if err != nil {
170                                 log.Printf("%s: IndexTo Walk error at %s: %s",
171                                         v, path, err)
172                                 return nil
173                         }
174                         basename := filepath.Base(path)
175                         if info.IsDir() &&
176                                 !strings.HasPrefix(basename, prefix) &&
177                                 !strings.HasPrefix(prefix, basename) {
178                                 // Skip directories that do not match
179                                 // prefix. We know there is nothing
180                                 // interesting inside.
181                                 return filepath.SkipDir
182                         }
183                         if info.IsDir() ||
184                                 !IsValidLocator(basename) ||
185                                 !strings.HasPrefix(basename, prefix) {
186                                 return nil
187                         }
188                         _, err = fmt.Fprintf(w, "%s+%d %d\n",
189                                 basename, info.Size(), info.ModTime().Unix())
190                         return err
191                 })
192 }
193
194 func (v *UnixVolume) Delete(loc string) error {
195         // Touch() must be called before calling Write() on a block.  Touch()
196         // also uses lockfile().  This avoids a race condition between Write()
197         // and Delete() because either (a) the file will be deleted and Touch()
198         // will signal to the caller that the file is not present (and needs to
199         // be re-written), or (b) Touch() will update the file's timestamp and
200         // Delete() will read the correct up-to-date timestamp and choose not to
201         // delete the file.
202
203         if v.readonly {
204                 return MethodDisabledError
205         }
206         if v.serialize {
207                 v.mutex.Lock()
208                 defer v.mutex.Unlock()
209         }
210         p := v.blockPath(loc)
211         f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
212         if err != nil {
213                 return err
214         }
215         defer f.Close()
216         if e := lockfile(f); e != nil {
217                 return e
218         }
219         defer unlockfile(f)
220
221         // If the block has been PUT in the last blob_signature_ttl
222         // seconds, return success without removing the block. This
223         // protects data from garbage collection until it is no longer
224         // possible for clients to retrieve the unreferenced blocks
225         // anyway (because the permission signatures have expired).
226         if fi, err := os.Stat(p); err != nil {
227                 return err
228         } else {
229                 if time.Since(fi.ModTime()) < blob_signature_ttl {
230                         return nil
231                 }
232         }
233         return os.Remove(p)
234 }
235
236 // blockDir returns the fully qualified directory name for the directory
237 // where loc is (or would be) stored on this volume.
238 func (v *UnixVolume) blockDir(loc string) string {
239         return filepath.Join(v.root, loc[0:3])
240 }
241
242 // blockPath returns the fully qualified pathname for the path to loc
243 // on this volume.
244 func (v *UnixVolume) blockPath(loc string) string {
245         return filepath.Join(v.blockDir(loc), loc)
246 }
247
248 // IsFull returns true if the free space on the volume is less than
249 // MIN_FREE_KILOBYTES.
250 //
251 func (v *UnixVolume) IsFull() (isFull bool) {
252         fullSymlink := v.root + "/full"
253
254         // Check if the volume has been marked as full in the last hour.
255         if link, err := os.Readlink(fullSymlink); err == nil {
256                 if ts, err := strconv.Atoi(link); err == nil {
257                         fulltime := time.Unix(int64(ts), 0)
258                         if time.Since(fulltime).Hours() < 1.0 {
259                                 return true
260                         }
261                 }
262         }
263
264         if avail, err := v.FreeDiskSpace(); err == nil {
265                 isFull = avail < MIN_FREE_KILOBYTES
266         } else {
267                 log.Printf("%s: FreeDiskSpace: %s\n", v, err)
268                 isFull = false
269         }
270
271         // If the volume is full, timestamp it.
272         if isFull {
273                 now := fmt.Sprintf("%d", time.Now().Unix())
274                 os.Symlink(now, fullSymlink)
275         }
276         return
277 }
278
279 // FreeDiskSpace returns the number of unused 1k blocks available on
280 // the volume.
281 //
282 func (v *UnixVolume) FreeDiskSpace() (free uint64, err error) {
283         var fs syscall.Statfs_t
284         err = syscall.Statfs(v.root, &fs)
285         if err == nil {
286                 // Statfs output is not guaranteed to measure free
287                 // space in terms of 1K blocks.
288                 free = fs.Bavail * uint64(fs.Bsize) / 1024
289         }
290         return
291 }
292
293 func (v *UnixVolume) String() string {
294         return fmt.Sprintf("[UnixVolume %s]", v.root)
295 }
296
297 func (v *UnixVolume) Writable() bool {
298         return !v.readonly
299 }
300
301 // lockfile and unlockfile use flock(2) to manage kernel file locks.
302 func lockfile(f *os.File) error {
303         return syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
304 }
305
306 func unlockfile(f *os.File) error {
307         return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
308 }