volume_super_block.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  9. )
  10. func (v *Volume) maybeWriteSuperBlock() error {
  11. datSize, _, e := v.DataBackend.GetStat()
  12. if e != nil {
  13. glog.V(0).Infof("failed to stat datafile %s: %v", v.DataBackend.Name(), e)
  14. return e
  15. }
  16. if datSize == 0 {
  17. v.SuperBlock.Version = needle.CurrentVersion
  18. _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
  19. if e != nil && os.IsPermission(e) {
  20. //read-only, but zero length - recreate it!
  21. var dataFile *os.File
  22. if dataFile, e = os.Create(v.DataBackend.Name()); e == nil {
  23. v.DataBackend = backend.NewDiskFile(dataFile)
  24. if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
  25. v.noWriteLock.Lock()
  26. v.noWriteOrDelete = false
  27. v.noWriteCanDelete = false
  28. v.noWriteLock.Unlock()
  29. }
  30. }
  31. }
  32. }
  33. return e
  34. }
  35. func (v *Volume) readSuperBlock() (err error) {
  36. v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
  37. if v.volumeInfo != nil && v.volumeInfo.Replication != "" {
  38. if replication, err := super_block.NewReplicaPlacementFromString(v.volumeInfo.Replication); err != nil {
  39. return fmt.Errorf("Error parse volume %d replication %s : %v", v.Id, v.volumeInfo.Replication, err)
  40. } else {
  41. v.SuperBlock.ReplicaPlacement = replication
  42. }
  43. }
  44. return err
  45. }