From 6d6b5dad6afdd19f447e07ed14b467bd7ef393e2 Mon Sep 17 00:00:00 2001 From: silenceper Date: Sun, 10 Apr 2022 22:19:53 +0800 Subject: [PATCH] fix get quota: skip 0 project id --- cmd/xfsquota/xfsquota.go | 19 +++++++++++++++++++ pkg/projectquota/projectquota.go | 10 +++++++++- pkg/xfsquota/quota.go | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/xfsquota/xfsquota.go b/cmd/xfsquota/xfsquota.go index 03f9ddf..b56a2bf 100644 --- a/cmd/xfsquota/xfsquota.go +++ b/cmd/xfsquota/xfsquota.go @@ -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", @@ -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) } diff --git a/pkg/projectquota/projectquota.go b/pkg/projectquota/projectquota.go index 2392993..ba88040 100644 --- a/pkg/projectquota/projectquota.go +++ b/pkg/projectquota/projectquota.go @@ -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 } @@ -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, @@ -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 diff --git a/pkg/xfsquota/quota.go b/pkg/xfsquota/quota.go index bd991fd..758dc8f 100644 --- a/pkg/xfsquota/quota.go +++ b/pkg/xfsquota/quota.go @@ -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) }