1 // A UnixVolume is a Volume backed by a locally mounted disk.
17 // IORequests are encapsulated Get or Put requests. They are used to
18 // implement serialized I/O (i.e. only one read/write operation per
19 // volume). When running in serialized mode, the Keep front end sends
20 // IORequests on a channel to an IORunner, which handles them one at a
21 // time and returns an IOResponse.
26 KeepGet IOMethod = iota
30 type IORequest struct {
34 reply chan *IOResponse
37 type IOResponse struct {
42 // A UnixVolume has the following properties:
45 // the path to the volume's root directory
47 // A channel of IORequests. If non-nil, all I/O requests for
48 // this volume should be queued on this channel; the result
49 // will be delivered on the IOResponse channel supplied in the
52 type UnixVolume struct {
53 root string // path to this volume
57 func (v *UnixVolume) IOHandler() {
58 for req := range v.queue {
62 result.data, result.err = v.Read(req.loc)
64 result.err = v.Write(req.loc, req.data)
70 func MakeUnixVolume(root string, serialize bool) (v UnixVolume) {
72 v = UnixVolume{root, make(chan *IORequest)}
75 v = UnixVolume{root, nil}
80 func (v *UnixVolume) Get(loc string) ([]byte, error) {
84 reply := make(chan *IOResponse)
85 v.queue <- &IORequest{KeepGet, loc, nil, reply}
87 return response.data, response.err
90 func (v *UnixVolume) Put(loc string, block []byte) error {
92 return v.Write(loc, block)
94 reply := make(chan *IOResponse)
95 v.queue <- &IORequest{KeepPut, loc, block, reply}
100 // Read retrieves a block identified by the locator string "loc", and
101 // returns its contents as a byte slice.
103 // If the block could not be opened or read, Read returns a nil slice
104 // and the os.Error that was generated.
106 // If the block is present but its content hash does not match loc,
107 // Read returns the block and a CorruptError. It is the caller's
108 // responsibility to decide what (if anything) to do with the
109 // corrupted data block.
111 func (v *UnixVolume) Read(loc string) ([]byte, error) {
116 blockFilename := filepath.Join(v.root, loc[0:3], loc)
118 f, err = os.Open(blockFilename)
123 if buf, err = ioutil.ReadAll(f); err != nil {
124 log.Printf("%s: reading %s: %s\n", v, blockFilename, err)
132 // Write stores a block of data identified by the locator string
133 // "loc". It returns nil on success. If the volume is full, it
134 // returns a FullError. If the write fails due to some other error,
135 // that error is returned.
137 func (v *UnixVolume) Write(loc string, block []byte) error {
141 blockDir := filepath.Join(v.root, loc[0:3])
142 if err := os.MkdirAll(blockDir, 0755); err != nil {
143 log.Printf("%s: could not create directory %s: %s",
148 tmpfile, tmperr := ioutil.TempFile(blockDir, "tmp"+loc)
150 log.Printf("ioutil.TempFile(%s, tmp%s): %s", blockDir, loc, tmperr)
153 blockFilename := filepath.Join(blockDir, loc)
155 if _, err := tmpfile.Write(block); err != nil {
156 log.Printf("%s: writing to %s: %s\n", v, blockFilename, err)
159 if err := tmpfile.Close(); err != nil {
160 log.Printf("closing %s: %s\n", tmpfile.Name(), err)
161 os.Remove(tmpfile.Name())
164 if err := os.Rename(tmpfile.Name(), blockFilename); err != nil {
165 log.Printf("rename %s %s: %s\n", tmpfile.Name(), blockFilename, err)
166 os.Remove(tmpfile.Name())
172 // Status returns a VolumeStatus struct describing the volume's
175 func (v *UnixVolume) Status() *VolumeStatus {
176 var fs syscall.Statfs_t
179 if fi, err := os.Stat(v.root); err == nil {
180 devnum = fi.Sys().(*syscall.Stat_t).Dev
182 log.Printf("%s: os.Stat: %s\n", v, err)
186 err := syscall.Statfs(v.root, &fs)
188 log.Printf("%s: statfs: %s\n", v, err)
191 // These calculations match the way df calculates disk usage:
192 // "free" space is measured by fs.Bavail, but "used" space
193 // uses fs.Blocks - fs.Bfree.
194 free := fs.Bavail * uint64(fs.Bsize)
195 used := (fs.Blocks - fs.Bfree) * uint64(fs.Bsize)
196 return &VolumeStatus{v.root, devnum, free, used}
199 // Index returns a list of blocks found on this volume which begin with
200 // the specified prefix. If the prefix is an empty string, Index returns
201 // a complete list of blocks.
203 // The return value is a multiline string (separated by
204 // newlines). Each line is in the format
206 // locator+size modification-time
210 // e4df392f86be161ca6ed3773a962b8f3+67108864 1388894303
211 // e4d41e6fd68460e0e3fc18cc746959d2+67108864 1377796043
212 // e4de7a2810f5554cd39b36d8ddb132ff+67108864 1388701136
214 func (v *UnixVolume) Index(prefix string) (output string) {
215 filepath.Walk(v.root,
216 func(path string, info os.FileInfo, err error) error {
217 // This WalkFunc inspects each path in the volume
218 // and prints an index line for all files that begin
221 log.Printf("IndexHandler: %s: walking to %s: %s",
225 locator := filepath.Base(path)
226 // Skip directories that do not match prefix.
227 // We know there is nothing interesting inside.
229 !strings.HasPrefix(locator, prefix) &&
230 !strings.HasPrefix(prefix, locator) {
231 return filepath.SkipDir
233 // Skip any file that is not apparently a locator, e.g. .meta files
234 if !IsValidLocator(locator) {
237 // Print filenames beginning with prefix
238 if !info.IsDir() && strings.HasPrefix(locator, prefix) {
239 output = output + fmt.Sprintf(
240 "%s+%d %d\n", locator, info.Size(), info.ModTime().Unix())
248 // IsFull returns true if the free space on the volume is less than
249 // MIN_FREE_KILOBYTES.
251 func (v *UnixVolume) IsFull() (isFull bool) {
252 fullSymlink := v.root + "/full"
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 {
264 if avail, err := v.FreeDiskSpace(); err == nil {
265 isFull = avail < MIN_FREE_KILOBYTES
267 log.Printf("%s: FreeDiskSpace: %s\n", v, err)
271 // If the volume is full, timestamp it.
273 now := fmt.Sprintf("%d", time.Now().Unix())
274 os.Symlink(now, fullSymlink)
279 // FreeDiskSpace returns the number of unused 1k blocks available on
282 func (v *UnixVolume) FreeDiskSpace() (free uint64, err error) {
283 var fs syscall.Statfs_t
284 err = syscall.Statfs(v.root, &fs)
286 // Statfs output is not guaranteed to measure free
287 // space in terms of 1K blocks.
288 free = fs.Bavail * uint64(fs.Bsize) / 1024
293 func (v *UnixVolume) String() string {
294 return fmt.Sprintf("[UnixVolume %s]", v.root)