Skip to content

Commit

Permalink
fix get quota: skip 0 project id
Browse files Browse the repository at this point in the history
  • Loading branch information
silenceper committed Apr 10, 2022
1 parent d0dfede commit 6d6b5da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
19 changes: 19 additions & 0 deletions cmd/xfsquota/xfsquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ var setQuotaCmd = &cobra.Command{
},
}

var cleanQuotaCmd = &cobra.Command{
Use: "clean",
Short: "clean quota information",
Example: "xfsquota clean /home/user",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
err := xfsQuota.CleanQuota(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Println("clean quota success, path:", args[0])
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "get version",
Expand All @@ -73,6 +91,7 @@ func init() {
setQuotaCmd.Flags().StringVarP(&inodes, "inodes", "i", "0", "quota inodes")
rootCmd.AddCommand(getQuotaCmd)
rootCmd.AddCommand(setQuotaCmd)
rootCmd.AddCommand(cleanQuotaCmd)
rootCmd.AddCommand(versionCmd)
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/projectquota/projectquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,17 @@ func (p *ProjectQuota) GetQuota(targetPath string) (*DiskQuotaSize, error) {
klog.Errorf("find project id err: %v", err)
return nil, err
}
if prjId == noQuotaID {
return nil, fmt.Errorf("no project id found(maybe not set quota) for %s", targetPath)
}

return getProjectQuota(backingFsBlockDev.device, prjId)
}

// SetQuota set quota limit for the path
func (p *ProjectQuota) SetQuota(targetPath string, size *DiskQuotaSize) error {
if size == nil || (size.Quota+size.Inodes == 0) {
//if first set size is 0 , can not set nonzero size again,we must use clean quota
if size == nil {
return nil
}

Expand Down Expand Up @@ -213,6 +217,9 @@ func (p *ProjectQuota) ClearQuota(targetPath string) error {
klog.Errorf("find project id err: %v", err)
return err
}
if prjId == noQuotaID {
return fmt.Errorf("no project id found(maybe not set quota) for %s", targetPath)
}

size := &DiskQuotaSize{
Quota: 0,
Expand Down Expand Up @@ -396,6 +403,7 @@ func (p *ProjectQuota) findOrCreateProjectId(targetPath string,

isNewId = true
idFromSys, err := getProjectID(targetPath)
//fmt.Println("idFromSys:", idFromSys)
if err != nil {
klog.Errorf("get project id for path %s err: %v", targetPath, err)
return 0, isNewId, err
Expand Down
4 changes: 4 additions & 0 deletions pkg/xfsquota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ func (q *XfsQuota) SetQuota(path string, sizeVal, inodeVal string) error {
Quota: uint64(size),
Inodes: inodes,
})
}

// CleanQuota clears the quota for the given path
func (q *XfsQuota) CleanQuota(path string) error {
return q.ProjectQuota.ClearQuota(path)
}

0 comments on commit 6d6b5da

Please sign in to comment.