@@ -19,12 +19,19 @@ package xfs
19
19
import (
20
20
"context"
21
21
"encoding/binary"
22
+ "errors"
22
23
"fmt"
23
- simd "github.com/minio/sha256-simd"
24
24
"math"
25
25
"os/exec"
26
26
"strconv"
27
27
"strings"
28
+
29
+ "github.com/golang/glog"
30
+ simd "github.com/minio/sha256-simd"
31
+ )
32
+
33
+ var (
34
+ ErrProjNotFound = errors .New ("xfs project not found" )
28
35
)
29
36
30
37
type XFSQuota struct {
@@ -47,20 +54,35 @@ func getProjectIDHash(id string) string {
47
54
// SetQuota creates a projectID and sets the hardlimit for the path
48
55
func (xfsq * XFSQuota ) SetQuota (ctx context.Context , limit int64 ) error {
49
56
57
+ _ , err := xfsq .GetVolumeStats (ctx )
58
+ // error getting quota value
59
+ if err != nil && err != ErrProjNotFound {
60
+ return err
61
+ }
62
+ // this means quota has already been set
63
+ if err == nil {
64
+ return nil
65
+ }
66
+
50
67
limitInStr := strconv .FormatInt (limit , 10 )
51
68
pid := getProjectIDHash (xfsq .ProjectID )
52
69
53
- cmd := exec .CommandContext (ctx , "xfs_quota" , "-x" , "-c" , fmt .Sprintf ("project -s -p %s %s" , xfsq .Path , pid ))
70
+ glog .V (3 ).Infof ("setting prjquota proj_id=%s path=%s" , pid , xfsq .Path )
71
+
72
+ cmd := exec .CommandContext (ctx , "xfs_quota" , "-x" , "-c" , fmt .Sprintf ("project -d 0 -s -p %s %s" , xfsq .Path , pid ))
54
73
out , err := cmd .CombinedOutput ()
55
74
if err != nil {
75
+ glog .Errorf ("could not set prjquota proj_id=%s path=%s err=%v" , pid , xfsq .Path , err )
56
76
return fmt .Errorf ("SetQuota failed for %s with error: (%v), output: (%s)" , xfsq .ProjectID , err , out )
57
77
}
58
78
59
79
cmd = exec .CommandContext (ctx , "xfs_quota" , "-x" , "-c" , fmt .Sprintf ("limit -p bhard=%s %s" , limitInStr , pid ), xfsq .Path )
60
80
out , err = cmd .CombinedOutput ()
61
81
if err != nil {
82
+ glog .Errorf ("could not set prjquota proj_id=%s path=%s err=%v" , pid , xfsq .Path , err )
62
83
return fmt .Errorf ("xfs_quota failed with error: %v, output: %s" , err , out )
63
84
}
85
+ glog .V (3 ).Infof ("prjquota set successfully proj_id=%s path=%s" , pid , xfsq .Path )
64
86
65
87
return nil
66
88
}
@@ -112,12 +134,18 @@ func ParseQuotaList(output, projectID string) (XFSVolumeStats, error) {
112
134
var pErr error
113
135
var f float64
114
136
lines := strings .Split (output , "\n " )
115
- for _ , line := range lines {
137
+ prjFound := false
138
+ for _ , l := range lines {
139
+ line := strings .TrimSpace (l )
116
140
if len (line ) == 0 {
117
141
continue
118
142
}
119
- cleanLine := strings .TrimSpace (line )
120
- splits := strings .Split (cleanLine , " " )
143
+ if ! strings .HasPrefix (line , "#" + projectID ) {
144
+ continue
145
+ }
146
+ prjFound = true
147
+
148
+ splits := strings .Split (line , " " )
121
149
var values []string
122
150
for _ , split := range splits {
123
151
tSplit := strings .TrimSpace (split )
@@ -140,6 +168,11 @@ func ParseQuotaList(output, projectID string) (XFSVolumeStats, error) {
140
168
totalInBytes = int64 (f )
141
169
break
142
170
}
171
+ break
172
+ }
173
+
174
+ if ! prjFound {
175
+ return XFSVolumeStats {}, ErrProjNotFound
143
176
}
144
177
return XFSVolumeStats {
145
178
AvailableBytes : totalInBytes - usedInBytes ,
0 commit comments