2014-08-25 18:37:00 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2020-02-02 23:37:23 +00:00
|
|
|
"fmt"
|
2014-08-25 18:37:00 +00:00
|
|
|
"os"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-11-09 08:10:59 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/backend"
|
2019-09-12 13:18:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-12-23 20:48:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
|
2014-08-25 18:37:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (v *Volume) maybeWriteSuperBlock() error {
|
2019-08-28 13:48:06 +00:00
|
|
|
|
2019-11-09 08:10:59 +00:00
|
|
|
datSize, _, e := v.DataBackend.GetStat()
|
|
|
|
if e != nil {
|
2019-12-09 03:44:16 +00:00
|
|
|
glog.V(0).Infof("failed to stat datafile %s: %v", v.DataBackend.Name(), e)
|
2019-11-09 08:10:59 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
if datSize == 0 {
|
2019-12-23 20:48:20 +00:00
|
|
|
v.SuperBlock.Version = needle.CurrentVersion
|
2019-11-09 08:10:59 +00:00
|
|
|
_, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
|
|
|
|
if e != nil && os.IsPermission(e) {
|
|
|
|
//read-only, but zero length - recreate it!
|
|
|
|
var dataFile *os.File
|
2019-12-09 03:44:16 +00:00
|
|
|
if dataFile, e = os.Create(v.DataBackend.Name()); e == nil {
|
2019-11-09 08:10:59 +00:00
|
|
|
v.DataBackend = backend.NewDiskFile(dataFile)
|
|
|
|
if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
|
2019-12-19 08:42:46 +00:00
|
|
|
v.noWriteOrDelete = false
|
|
|
|
v.noWriteCanDelete = false
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 08:10:59 +00:00
|
|
|
return e
|
2014-08-25 18:37:00 +00:00
|
|
|
}
|
2018-06-24 18:37:08 +00:00
|
|
|
|
2014-08-25 18:37:00 +00:00
|
|
|
func (v *Volume) readSuperBlock() (err error) {
|
2019-12-23 20:48:20 +00:00
|
|
|
v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
|
2020-02-27 00:52:57 +00:00
|
|
|
if v.volumeInfo != nil && v.volumeInfo.Replication != "" {
|
2020-02-02 23:37:23 +00:00
|
|
|
if replication, err := super_block.NewReplicaPlacementFromString(v.volumeInfo.Replication); err != nil {
|
|
|
|
return fmt.Errorf("Error parse volume %d replication %s : %v", v.Id, v.volumeInfo.Replication, err)
|
|
|
|
} else {
|
|
|
|
v.SuperBlock.ReplicaPlacement = replication
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 18:37:00 +00:00
|
|
|
return err
|
|
|
|
}
|