volume_super_block.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "github.com/chrislusf/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.noWriteOrDelete = false
  26. v.noWriteCanDelete = false
  27. }
  28. }
  29. }
  30. }
  31. return e
  32. }
  33. func (v *Volume) readSuperBlock() (err error) {
  34. v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
  35. if v.volumeInfo != nil && v.volumeInfo.Replication != "" {
  36. if replication, err := super_block.NewReplicaPlacementFromString(v.volumeInfo.Replication); err != nil {
  37. return fmt.Errorf("Error parse volume %d replication %s : %v", v.Id, v.volumeInfo.Replication, err)
  38. } else {
  39. v.SuperBlock.ReplicaPlacement = replication
  40. }
  41. }
  42. return err
  43. }