|
1 | | -// +build linux |
| 1 | +// +build linux,!exclude_disk_quota |
2 | 2 |
|
3 | 3 | // |
4 | 4 | // projectquota.go - implements XFS project quota controls |
@@ -63,19 +63,6 @@ import ( |
63 | 63 | "golang.org/x/sys/unix" |
64 | 64 | ) |
65 | 65 |
|
66 | | -// Quota limit params - currently we only control blocks hard limit |
67 | | -type Quota struct { |
68 | | - Size uint64 |
69 | | -} |
70 | | - |
71 | | -// Control - Context to be used by storage driver (e.g. overlay) |
72 | | -// who wants to apply project quotas to container dirs |
73 | | -type Control struct { |
74 | | - backingFsBlockDev string |
75 | | - nextProjectID uint32 |
76 | | - quotas map[string]uint32 |
77 | | -} |
78 | | - |
79 | 66 | // NewControl - initialize project quota support. |
80 | 67 | // Test to make sure that quota can be set on a test dir and find |
81 | 68 | // the first project id to be used for the next container create. |
@@ -166,21 +153,24 @@ func NewControl(basePath string) (*Control, error) { |
166 | 153 | // SetQuota - assign a unique project id to directory and set the quota limits |
167 | 154 | // for that project id |
168 | 155 | func (q *Control) SetQuota(targetPath string, quota Quota) error { |
169 | | - |
| 156 | + q.RLock() |
170 | 157 | projectID, ok := q.quotas[targetPath] |
| 158 | + q.RUnlock() |
171 | 159 | if !ok { |
| 160 | + q.Lock() |
172 | 161 | projectID = q.nextProjectID |
173 | 162 |
|
174 | 163 | // |
175 | 164 | // assign project id to new container directory |
176 | 165 | // |
177 | 166 | err := setProjectID(targetPath, projectID) |
178 | 167 | if err != nil { |
| 168 | + q.Unlock() |
179 | 169 | return err |
180 | 170 | } |
181 | | - |
182 | 171 | q.quotas[targetPath] = projectID |
183 | 172 | q.nextProjectID++ |
| 173 | + q.Unlock() |
184 | 174 | } |
185 | 175 |
|
186 | 176 | // |
@@ -217,8 +207,9 @@ func setProjectQuota(backingFsBlockDev string, projectID uint32, quota Quota) er |
217 | 207 |
|
218 | 208 | // GetQuota - get the quota limits of a directory that was configured with SetQuota |
219 | 209 | func (q *Control) GetQuota(targetPath string, quota *Quota) error { |
220 | | - |
| 210 | + q.RLock() |
221 | 211 | projectID, ok := q.quotas[targetPath] |
| 212 | + q.RUnlock() |
222 | 213 | if !ok { |
223 | 214 | return errors.Errorf("quota not found for path: %s", targetPath) |
224 | 215 | } |
@@ -289,6 +280,8 @@ func setProjectID(targetPath string, projectID uint32) error { |
289 | 280 | // findNextProjectID - find the next project id to be used for containers |
290 | 281 | // by scanning driver home directory to find used project ids |
291 | 282 | func (q *Control) findNextProjectID(home string) error { |
| 283 | + q.Lock() |
| 284 | + defer q.Unlock() |
292 | 285 | files, err := ioutil.ReadDir(home) |
293 | 286 | if err != nil { |
294 | 287 | return errors.Errorf("read directory failed: %s", home) |
|
0 commit comments