]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephfs-shell: add quota management
authorMilind Changire <mchangir@redhat.com>
Thu, 9 May 2019 06:25:05 +0000 (11:55 +0530)
committerPatrick Donnelly <pdonnell@redhat.com>
Thu, 20 Jun 2019 22:28:49 +0000 (15:28 -0700)
Quotas can be managed by:
$ quota get dir
$ quota set {--max_files MAX_FILES} {--max_bytes MAX_BYTES} dir

Fixes: http://tracker.ceph.com/issues/39165
Signed-off-by: Milind Changire <mchangir@redhat.com>
(cherry picked from commit d187e9b0a917b828530bda89866d6a4867e9f9ff)

src/tools/cephfs/cephfs-shell

index 32c4f0840fe677a7bffd1bc4d82109d9b59cf537..a4d792d46023bdc34b21174d67b80419721d3d22 100644 (file)
@@ -1124,6 +1124,78 @@ sub-directories, files')
                 self.poutput('{:10s} {}'.format(humansize(int(cephfs.getxattr(to_bytes(
                     dir_), 'ceph.dir.rbytes').decode('utf-8'))), '.' + dir_))
 
+    quota_parser = argparse.ArgumentParser(
+        description='Quota management for a Directory')
+    quota_parser.add_argument('op', choices=['get', 'set'],
+                              help='Quota operation type.')
+    quota_parser.add_argument('dir', type=str, help='Name of the directory.')
+    quota_parser.add_argument('--max_bytes', type=int, default=-1, nargs='?',
+                              help='Max cumulative size of the data under '
+                                   'this directory.')
+    quota_parser.add_argument('--max_files', type=int, default=-1, nargs='?',
+                              help='Total number of files under this '
+                                   'directory tree.')
+
+    @with_argparser(quota_parser)
+    def do_quota(self, args):
+        """
+        Quota management.
+        """
+        if not is_dir_exists(args.dir):
+            self.poutput("error: no such directory '%s'" % args.dir)
+            return
+
+        if args.op == 'set':
+            if (args.max_bytes == -1) and (args.max_files == -1):
+                self.poutput('please specify either --max_bytes or '
+                             '--max_files or both')
+                return
+
+            if args.max_bytes >= 0:
+                max_bytes = to_bytes(str(args.max_bytes))
+                try:
+                    cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_bytes',
+                                    max_bytes, len(max_bytes),
+                                    os.XATTR_CREATE)
+                    self.poutput('max_bytes set to %d' % args.max_bytes)
+                except:
+                    cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_bytes',
+                                    max_bytes, len(max_bytes),
+                                    os.XATTR_REPLACE)
+                    self.poutput('max_bytes reset to %d' % args.max_bytes)
+
+            if args.max_files >= 0:
+                max_files = to_bytes(str(args.max_files))
+                try:
+                    cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_files',
+                                    max_files, len(max_files),
+                                    os.XATTR_CREATE)
+                    self.poutput('max_files set to %d' % args.max_files)
+                except:
+                    cephfs.setxattr(to_bytes(args.dir), 'ceph.quota.max_files',
+                                    max_files, len(max_files),
+                                    os.XATTR_REPLACE)
+                    self.poutput('max_files reset to %d' % args.max_files)
+        elif args.op == 'get':
+            max_bytes = '0'
+            max_files = '0'
+            try:
+                max_bytes = cephfs.getxattr(to_bytes(args.dir),
+                                            'ceph.quota.max_bytes')
+                self.poutput('max_bytes: %s' % max_bytes)
+            except:
+                self.poutput('max_bytes is not set')
+                pass
+
+            try:
+                max_files = cephfs.getxattr(to_bytes(args.dir),
+                                            'ceph.quota.max_files')
+                self.poutput('max_files: %s' % max_files)
+            except:
+                self.poutput('max_files is not set')
+                pass
+
+
     def do_help(self, line):
         """
         Get details about a command.