Merge branch 'master' into 10797-ruby-2.3
[arvados.git] / services / keepstore / volume_unix.go
1 package main
2
3 import (
4         "bufio"
5         "context"
6         "flag"
7         "fmt"
8         "io"
9         "io/ioutil"
10         "os"
11         "path/filepath"
12         "regexp"
13         "strconv"
14         "strings"
15         "sync"
16         "syscall"
17         "time"
18
19         log "github.com/Sirupsen/logrus"
20 )
21
22 type unixVolumeAdder struct {
23         *Config
24 }
25
26 // String implements flag.Value
27 func (s *unixVolumeAdder) String() string {
28         return "-"
29 }
30
31 func (vs *unixVolumeAdder) Set(path string) error {
32         if dirs := strings.Split(path, ","); len(dirs) > 1 {
33                 log.Print("DEPRECATED: using comma-separated volume list.")
34                 for _, dir := range dirs {
35                         if err := vs.Set(dir); err != nil {
36                                 return err
37                         }
38                 }
39                 return nil
40         }
41         vs.Config.Volumes = append(vs.Config.Volumes, &UnixVolume{
42                 Root:      path,
43                 ReadOnly:  deprecated.flagReadonly,
44                 Serialize: deprecated.flagSerializeIO,
45         })
46         return nil
47 }
48
49 func init() {
50         VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &UnixVolume{} })
51
52         flag.Var(&unixVolumeAdder{theConfig}, "volumes", "see Volumes configuration")
53         flag.Var(&unixVolumeAdder{theConfig}, "volume", "see Volumes configuration")
54 }
55
56 // Discover adds a UnixVolume for every directory named "keep" that is
57 // located at the top level of a device- or tmpfs-backed mount point
58 // other than "/". It returns the number of volumes added.
59 func (vs *unixVolumeAdder) Discover() int {
60         added := 0
61         f, err := os.Open(ProcMounts)
62         if err != nil {
63                 log.Fatalf("opening %s: %s", ProcMounts, err)
64         }
65         scanner := bufio.NewScanner(f)
66         for scanner.Scan() {
67                 args := strings.Fields(scanner.Text())
68                 if err := scanner.Err(); err != nil {
69                         log.Fatalf("reading %s: %s", ProcMounts, err)
70                 }
71                 dev, mount := args[0], args[1]
72                 if mount == "/" {
73                         continue
74                 }
75                 if dev != "tmpfs" && !strings.HasPrefix(dev, "/dev/") {
76                         continue
77                 }
78                 keepdir := mount + "/keep"
79                 if st, err := os.Stat(keepdir); err != nil || !st.IsDir() {
80                         continue
81                 }
82                 // Set the -readonly flag (but only for this volume)
83                 // if the filesystem is mounted readonly.
84                 flagReadonlyWas := deprecated.flagReadonly
85                 for _, fsopt := range strings.Split(args[3], ",") {
86                         if fsopt == "ro" {
87                                 deprecated.flagReadonly = true
88                                 break
89                         }
90                         if fsopt == "rw" {
91                                 break
92                         }
93                 }
94                 if err := vs.Set(keepdir); err != nil {
95                         log.Printf("adding %q: %s", keepdir, err)
96                 } else {
97                         added++
98                 }
99                 deprecated.flagReadonly = flagReadonlyWas
100         }
101         return added
102 }
103
104 // A UnixVolume stores and retrieves blocks in a local directory.
105 type UnixVolume struct {
106         Root                 string // path to the volume's root directory
107         ReadOnly             bool
108         Serialize            bool
109         DirectoryReplication int
110
111         // something to lock during IO, typically a sync.Mutex (or nil
112         // to skip locking)
113         locker sync.Locker
114
115         os osWithStats
116 }
117
118 // Examples implements VolumeWithExamples.
119 func (*UnixVolume) Examples() []Volume {
120         return []Volume{
121                 &UnixVolume{
122                         Root:                 "/mnt/local-disk",
123                         Serialize:            true,
124                         DirectoryReplication: 1,
125                 },
126                 &UnixVolume{
127                         Root:                 "/mnt/network-disk",
128                         Serialize:            false,
129                         DirectoryReplication: 2,
130                 },
131         }
132 }
133
134 // Type implements Volume
135 func (v *UnixVolume) Type() string {
136         return "Directory"
137 }
138
139 // Start implements Volume
140 func (v *UnixVolume) Start() error {
141         if v.Serialize {
142                 v.locker = &sync.Mutex{}
143         }
144         if !strings.HasPrefix(v.Root, "/") {
145                 return fmt.Errorf("volume root does not start with '/': %q", v.Root)
146         }
147         if v.DirectoryReplication == 0 {
148                 v.DirectoryReplication = 1
149         }
150         _, err := v.os.Stat(v.Root)
151         return err
152 }
153
154 // Touch sets the timestamp for the given locator to the current time
155 func (v *UnixVolume) Touch(loc string) error {
156         if v.ReadOnly {
157                 return MethodDisabledError
158         }
159         p := v.blockPath(loc)
160         f, err := v.os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
161         if err != nil {
162                 return err
163         }
164         defer f.Close()
165         if err := v.lock(context.TODO()); err != nil {
166                 return err
167         }
168         defer v.unlock()
169         if e := v.lockfile(f); e != nil {
170                 return e
171         }
172         defer v.unlockfile(f)
173         ts := syscall.NsecToTimespec(time.Now().UnixNano())
174         v.os.stats.Tick(&v.os.stats.UtimesOps)
175         err = syscall.UtimesNano(p, []syscall.Timespec{ts, ts})
176         v.os.stats.TickErr(err)
177         return err
178 }
179
180 // Mtime returns the stored timestamp for the given locator.
181 func (v *UnixVolume) Mtime(loc string) (time.Time, error) {
182         p := v.blockPath(loc)
183         fi, err := v.os.Stat(p)
184         if err != nil {
185                 return time.Time{}, err
186         }
187         return fi.ModTime(), nil
188 }
189
190 // Lock the locker (if one is in use), open the file for reading, and
191 // call the given function if and when the file is ready to read.
192 func (v *UnixVolume) getFunc(ctx context.Context, path string, fn func(io.Reader) error) error {
193         if err := v.lock(ctx); err != nil {
194                 return err
195         }
196         defer v.unlock()
197         f, err := v.os.Open(path)
198         if err != nil {
199                 return err
200         }
201         defer f.Close()
202         return fn(NewCountingReader(ioutil.NopCloser(f), v.os.stats.TickInBytes))
203 }
204
205 // stat is os.Stat() with some extra sanity checks.
206 func (v *UnixVolume) stat(path string) (os.FileInfo, error) {
207         stat, err := v.os.Stat(path)
208         if err == nil {
209                 if stat.Size() < 0 {
210                         err = os.ErrInvalid
211                 } else if stat.Size() > BlockSize {
212                         err = TooLongError
213                 }
214         }
215         return stat, err
216 }
217
218 // Get retrieves a block, copies it to the given slice, and returns
219 // the number of bytes copied.
220 func (v *UnixVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
221         return getWithPipe(ctx, loc, buf, v)
222 }
223
224 // ReadBlock implements BlockReader.
225 func (v *UnixVolume) ReadBlock(ctx context.Context, loc string, w io.Writer) error {
226         path := v.blockPath(loc)
227         stat, err := v.stat(path)
228         if err != nil {
229                 return v.translateError(err)
230         }
231         return v.getFunc(ctx, path, func(rdr io.Reader) error {
232                 n, err := io.Copy(w, rdr)
233                 if err == nil && n != stat.Size() {
234                         err = io.ErrUnexpectedEOF
235                 }
236                 return err
237         })
238 }
239
240 // Compare returns nil if Get(loc) would return the same content as
241 // expect. It is functionally equivalent to Get() followed by
242 // bytes.Compare(), but uses less memory.
243 func (v *UnixVolume) Compare(ctx context.Context, loc string, expect []byte) error {
244         path := v.blockPath(loc)
245         if _, err := v.stat(path); err != nil {
246                 return v.translateError(err)
247         }
248         return v.getFunc(ctx, path, func(rdr io.Reader) error {
249                 return compareReaderWithBuf(ctx, rdr, expect, loc[:32])
250         })
251 }
252
253 // Put stores a block of data identified by the locator string
254 // "loc".  It returns nil on success.  If the volume is full, it
255 // returns a FullError.  If the write fails due to some other error,
256 // that error is returned.
257 func (v *UnixVolume) Put(ctx context.Context, loc string, block []byte) error {
258         return putWithPipe(ctx, loc, block, v)
259 }
260
261 // ReadBlock implements BlockWriter.
262 func (v *UnixVolume) WriteBlock(ctx context.Context, loc string, rdr io.Reader) error {
263         if v.ReadOnly {
264                 return MethodDisabledError
265         }
266         if v.IsFull() {
267                 return FullError
268         }
269         bdir := v.blockDir(loc)
270         if err := os.MkdirAll(bdir, 0755); err != nil {
271                 log.Printf("%s: could not create directory %s: %s",
272                         loc, bdir, err)
273                 return err
274         }
275
276         tmpfile, tmperr := v.os.TempFile(bdir, "tmp"+loc)
277         if tmperr != nil {
278                 log.Printf("ioutil.TempFile(%s, tmp%s): %s", bdir, loc, tmperr)
279                 return tmperr
280         }
281
282         bpath := v.blockPath(loc)
283
284         if err := v.lock(ctx); err != nil {
285                 return err
286         }
287         defer v.unlock()
288         n, err := io.Copy(tmpfile, rdr)
289         v.os.stats.TickOutBytes(uint64(n))
290         if err != nil {
291                 log.Printf("%s: writing to %s: %s\n", v, bpath, err)
292                 tmpfile.Close()
293                 v.os.Remove(tmpfile.Name())
294                 return err
295         }
296         if err := tmpfile.Close(); err != nil {
297                 log.Printf("closing %s: %s\n", tmpfile.Name(), err)
298                 v.os.Remove(tmpfile.Name())
299                 return err
300         }
301         if err := v.os.Rename(tmpfile.Name(), bpath); err != nil {
302                 log.Printf("rename %s %s: %s\n", tmpfile.Name(), bpath, err)
303                 return v.os.Remove(tmpfile.Name())
304         }
305         return nil
306 }
307
308 // Status returns a VolumeStatus struct describing the volume's
309 // current state, or nil if an error occurs.
310 //
311 func (v *UnixVolume) Status() *VolumeStatus {
312         fi, err := v.os.Stat(v.Root)
313         if err != nil {
314                 log.Printf("%s: os.Stat: %s\n", v, err)
315                 return nil
316         }
317         devnum := fi.Sys().(*syscall.Stat_t).Dev
318
319         var fs syscall.Statfs_t
320         if err := syscall.Statfs(v.Root, &fs); err != nil {
321                 log.Printf("%s: statfs: %s\n", v, err)
322                 return nil
323         }
324         // These calculations match the way df calculates disk usage:
325         // "free" space is measured by fs.Bavail, but "used" space
326         // uses fs.Blocks - fs.Bfree.
327         free := fs.Bavail * uint64(fs.Bsize)
328         used := (fs.Blocks - fs.Bfree) * uint64(fs.Bsize)
329         return &VolumeStatus{
330                 MountPoint: v.Root,
331                 DeviceNum:  devnum,
332                 BytesFree:  free,
333                 BytesUsed:  used,
334         }
335 }
336
337 var blockDirRe = regexp.MustCompile(`^[0-9a-f]+$`)
338 var blockFileRe = regexp.MustCompile(`^[0-9a-f]{32}$`)
339
340 // IndexTo writes (to the given Writer) a list of blocks found on this
341 // volume which begin with the specified prefix. If the prefix is an
342 // empty string, IndexTo writes a complete list of blocks.
343 //
344 // Each block is given in the format
345 //
346 //     locator+size modification-time {newline}
347 //
348 // e.g.:
349 //
350 //     e4df392f86be161ca6ed3773a962b8f3+67108864 1388894303
351 //     e4d41e6fd68460e0e3fc18cc746959d2+67108864 1377796043
352 //     e4de7a2810f5554cd39b36d8ddb132ff+67108864 1388701136
353 //
354 func (v *UnixVolume) IndexTo(prefix string, w io.Writer) error {
355         var lastErr error
356         rootdir, err := v.os.Open(v.Root)
357         if err != nil {
358                 return err
359         }
360         defer rootdir.Close()
361         v.os.stats.Tick(&v.os.stats.ReaddirOps)
362         for {
363                 names, err := rootdir.Readdirnames(1)
364                 if err == io.EOF {
365                         return lastErr
366                 } else if err != nil {
367                         return err
368                 }
369                 if !strings.HasPrefix(names[0], prefix) && !strings.HasPrefix(prefix, names[0]) {
370                         // prefix excludes all blocks stored in this dir
371                         continue
372                 }
373                 if !blockDirRe.MatchString(names[0]) {
374                         continue
375                 }
376                 blockdirpath := filepath.Join(v.Root, names[0])
377                 blockdir, err := v.os.Open(blockdirpath)
378                 if err != nil {
379                         log.Print("Error reading ", blockdirpath, ": ", err)
380                         lastErr = err
381                         continue
382                 }
383                 v.os.stats.Tick(&v.os.stats.ReaddirOps)
384                 for {
385                         fileInfo, err := blockdir.Readdir(1)
386                         if err == io.EOF {
387                                 break
388                         } else if err != nil {
389                                 log.Print("Error reading ", blockdirpath, ": ", err)
390                                 lastErr = err
391                                 break
392                         }
393                         name := fileInfo[0].Name()
394                         if !strings.HasPrefix(name, prefix) {
395                                 continue
396                         }
397                         if !blockFileRe.MatchString(name) {
398                                 continue
399                         }
400                         _, err = fmt.Fprint(w,
401                                 name,
402                                 "+", fileInfo[0].Size(),
403                                 " ", fileInfo[0].ModTime().UnixNano(),
404                                 "\n")
405                 }
406                 blockdir.Close()
407         }
408 }
409
410 // Trash trashes the block data from the unix storage
411 // If TrashLifetime == 0, the block is deleted
412 // Else, the block is renamed as path/{loc}.trash.{deadline},
413 // where deadline = now + TrashLifetime
414 func (v *UnixVolume) Trash(loc string) error {
415         // Touch() must be called before calling Write() on a block.  Touch()
416         // also uses lockfile().  This avoids a race condition between Write()
417         // and Trash() because either (a) the file will be trashed and Touch()
418         // will signal to the caller that the file is not present (and needs to
419         // be re-written), or (b) Touch() will update the file's timestamp and
420         // Trash() will read the correct up-to-date timestamp and choose not to
421         // trash the file.
422
423         if v.ReadOnly {
424                 return MethodDisabledError
425         }
426         if err := v.lock(context.TODO()); err != nil {
427                 return err
428         }
429         defer v.unlock()
430         p := v.blockPath(loc)
431         f, err := v.os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
432         if err != nil {
433                 return err
434         }
435         defer f.Close()
436         if e := v.lockfile(f); e != nil {
437                 return e
438         }
439         defer v.unlockfile(f)
440
441         // If the block has been PUT in the last blobSignatureTTL
442         // seconds, return success without removing the block. This
443         // protects data from garbage collection until it is no longer
444         // possible for clients to retrieve the unreferenced blocks
445         // anyway (because the permission signatures have expired).
446         if fi, err := v.os.Stat(p); err != nil {
447                 return err
448         } else if time.Since(fi.ModTime()) < time.Duration(theConfig.BlobSignatureTTL) {
449                 return nil
450         }
451
452         if theConfig.TrashLifetime == 0 {
453                 return v.os.Remove(p)
454         }
455         return v.os.Rename(p, fmt.Sprintf("%v.trash.%d", p, time.Now().Add(theConfig.TrashLifetime.Duration()).Unix()))
456 }
457
458 // Untrash moves block from trash back into store
459 // Look for path/{loc}.trash.{deadline} in storage,
460 // and rename the first such file as path/{loc}
461 func (v *UnixVolume) Untrash(loc string) (err error) {
462         if v.ReadOnly {
463                 return MethodDisabledError
464         }
465
466         v.os.stats.Tick(&v.os.stats.ReaddirOps)
467         files, err := ioutil.ReadDir(v.blockDir(loc))
468         if err != nil {
469                 return err
470         }
471
472         if len(files) == 0 {
473                 return os.ErrNotExist
474         }
475
476         foundTrash := false
477         prefix := fmt.Sprintf("%v.trash.", loc)
478         for _, f := range files {
479                 if strings.HasPrefix(f.Name(), prefix) {
480                         foundTrash = true
481                         err = v.os.Rename(v.blockPath(f.Name()), v.blockPath(loc))
482                         if err == nil {
483                                 break
484                         }
485                 }
486         }
487
488         if foundTrash == false {
489                 return os.ErrNotExist
490         }
491
492         return
493 }
494
495 // blockDir returns the fully qualified directory name for the directory
496 // where loc is (or would be) stored on this volume.
497 func (v *UnixVolume) blockDir(loc string) string {
498         return filepath.Join(v.Root, loc[0:3])
499 }
500
501 // blockPath returns the fully qualified pathname for the path to loc
502 // on this volume.
503 func (v *UnixVolume) blockPath(loc string) string {
504         return filepath.Join(v.blockDir(loc), loc)
505 }
506
507 // IsFull returns true if the free space on the volume is less than
508 // MinFreeKilobytes.
509 //
510 func (v *UnixVolume) IsFull() (isFull bool) {
511         fullSymlink := v.Root + "/full"
512
513         // Check if the volume has been marked as full in the last hour.
514         if link, err := os.Readlink(fullSymlink); err == nil {
515                 if ts, err := strconv.Atoi(link); err == nil {
516                         fulltime := time.Unix(int64(ts), 0)
517                         if time.Since(fulltime).Hours() < 1.0 {
518                                 return true
519                         }
520                 }
521         }
522
523         if avail, err := v.FreeDiskSpace(); err == nil {
524                 isFull = avail < MinFreeKilobytes
525         } else {
526                 log.Printf("%s: FreeDiskSpace: %s\n", v, err)
527                 isFull = false
528         }
529
530         // If the volume is full, timestamp it.
531         if isFull {
532                 now := fmt.Sprintf("%d", time.Now().Unix())
533                 os.Symlink(now, fullSymlink)
534         }
535         return
536 }
537
538 // FreeDiskSpace returns the number of unused 1k blocks available on
539 // the volume.
540 //
541 func (v *UnixVolume) FreeDiskSpace() (free uint64, err error) {
542         var fs syscall.Statfs_t
543         err = syscall.Statfs(v.Root, &fs)
544         if err == nil {
545                 // Statfs output is not guaranteed to measure free
546                 // space in terms of 1K blocks.
547                 free = fs.Bavail * uint64(fs.Bsize) / 1024
548         }
549         return
550 }
551
552 func (v *UnixVolume) String() string {
553         return fmt.Sprintf("[UnixVolume %s]", v.Root)
554 }
555
556 // Writable returns false if all future Put, Mtime, and Delete calls
557 // are expected to fail.
558 func (v *UnixVolume) Writable() bool {
559         return !v.ReadOnly
560 }
561
562 // Replication returns the number of replicas promised by the
563 // underlying device (as specified in configuration).
564 func (v *UnixVolume) Replication() int {
565         return v.DirectoryReplication
566 }
567
568 // InternalStats returns I/O and filesystem ops counters.
569 func (v *UnixVolume) InternalStats() interface{} {
570         return &v.os.stats
571 }
572
573 // lock acquires the serialize lock, if one is in use. If ctx is done
574 // before the lock is acquired, lock returns ctx.Err() instead of
575 // acquiring the lock.
576 func (v *UnixVolume) lock(ctx context.Context) error {
577         if v.locker == nil {
578                 return nil
579         }
580         locked := make(chan struct{})
581         go func() {
582                 v.locker.Lock()
583                 close(locked)
584         }()
585         select {
586         case <-ctx.Done():
587                 go func() {
588                         <-locked
589                         v.locker.Unlock()
590                 }()
591                 return ctx.Err()
592         case <-locked:
593                 return nil
594         }
595 }
596
597 // unlock releases the serialize lock, if one is in use.
598 func (v *UnixVolume) unlock() {
599         if v.locker == nil {
600                 return
601         }
602         v.locker.Unlock()
603 }
604
605 // lockfile and unlockfile use flock(2) to manage kernel file locks.
606 func (v *UnixVolume) lockfile(f *os.File) error {
607         v.os.stats.Tick(&v.os.stats.FlockOps)
608         err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
609         v.os.stats.TickErr(err)
610         return err
611 }
612
613 func (v *UnixVolume) unlockfile(f *os.File) error {
614         err := syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
615         v.os.stats.TickErr(err)
616         return err
617 }
618
619 // Where appropriate, translate a more specific filesystem error to an
620 // error recognized by handlers, like os.ErrNotExist.
621 func (v *UnixVolume) translateError(err error) error {
622         switch err.(type) {
623         case *os.PathError:
624                 // stat() returns a PathError if the parent directory
625                 // (not just the file itself) is missing
626                 return os.ErrNotExist
627         default:
628                 return err
629         }
630 }
631
632 var unixTrashLocRegexp = regexp.MustCompile(`/([0-9a-f]{32})\.trash\.(\d+)$`)
633
634 // EmptyTrash walks hierarchy looking for {hash}.trash.*
635 // and deletes those with deadline < now.
636 func (v *UnixVolume) EmptyTrash() {
637         var bytesDeleted, bytesInTrash int64
638         var blocksDeleted, blocksInTrash int
639
640         err := filepath.Walk(v.Root, func(path string, info os.FileInfo, err error) error {
641                 if err != nil {
642                         log.Printf("EmptyTrash: filepath.Walk: %v: %v", path, err)
643                         return nil
644                 }
645                 if info.Mode().IsDir() {
646                         return nil
647                 }
648                 matches := unixTrashLocRegexp.FindStringSubmatch(path)
649                 if len(matches) != 3 {
650                         return nil
651                 }
652                 deadline, err := strconv.ParseInt(matches[2], 10, 64)
653                 if err != nil {
654                         log.Printf("EmptyTrash: %v: ParseInt(%v): %v", path, matches[2], err)
655                         return nil
656                 }
657                 bytesInTrash += info.Size()
658                 blocksInTrash++
659                 if deadline > time.Now().Unix() {
660                         return nil
661                 }
662                 err = v.os.Remove(path)
663                 if err != nil {
664                         log.Printf("EmptyTrash: Remove %v: %v", path, err)
665                         return nil
666                 }
667                 bytesDeleted += info.Size()
668                 blocksDeleted++
669                 return nil
670         })
671
672         if err != nil {
673                 log.Printf("EmptyTrash error for %v: %v", v.String(), err)
674         }
675
676         log.Printf("EmptyTrash stats for %v: Deleted %v bytes in %v blocks. Remaining in trash: %v bytes in %v blocks.", v.String(), bytesDeleted, blocksDeleted, bytesInTrash-bytesDeleted, blocksInTrash-blocksDeleted)
677 }
678
679 type unixStats struct {
680         statsTicker
681         OpenOps    uint64
682         StatOps    uint64
683         FlockOps   uint64
684         UtimesOps  uint64
685         CreateOps  uint64
686         RenameOps  uint64
687         UnlinkOps  uint64
688         ReaddirOps uint64
689 }
690
691 func (s *unixStats) TickErr(err error) {
692         if err == nil {
693                 return
694         }
695         s.statsTicker.TickErr(err, fmt.Sprintf("%T", err))
696 }
697
698 type osWithStats struct {
699         stats unixStats
700 }
701
702 func (o *osWithStats) Open(name string) (*os.File, error) {
703         o.stats.Tick(&o.stats.OpenOps)
704         f, err := os.Open(name)
705         o.stats.TickErr(err)
706         return f, err
707 }
708
709 func (o *osWithStats) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
710         o.stats.Tick(&o.stats.OpenOps)
711         f, err := os.OpenFile(name, flag, perm)
712         o.stats.TickErr(err)
713         return f, err
714 }
715
716 func (o *osWithStats) Remove(path string) error {
717         o.stats.Tick(&o.stats.UnlinkOps)
718         err := os.Remove(path)
719         o.stats.TickErr(err)
720         return err
721 }
722
723 func (o *osWithStats) Rename(a, b string) error {
724         o.stats.Tick(&o.stats.RenameOps)
725         err := os.Rename(a, b)
726         o.stats.TickErr(err)
727         return err
728 }
729
730 func (o *osWithStats) Stat(path string) (os.FileInfo, error) {
731         o.stats.Tick(&o.stats.StatOps)
732         fi, err := os.Stat(path)
733         o.stats.TickErr(err)
734         return fi, err
735 }
736
737 func (o *osWithStats) TempFile(dir, base string) (*os.File, error) {
738         o.stats.Tick(&o.stats.CreateOps)
739         f, err := ioutil.TempFile(dir, base)
740         o.stats.TickErr(err)
741         return f, err
742 }