Skip to contentSkip to conversation

Quota support for ocfs2-tools (version 3)

PATCH New Jan Kara 2 participants 15 messages Aug 3, 2009 – Aug 5, 2009 OCFS2

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover Quota support for ocfs2-tools (version 3)

Hi,

below comes a new version of the series of patches implementing quota support
for ocfs2-tools. I've fixed the calls of ocfs2_malloc_blocks() which were given number of bytes instead of number of blocks. Besides that the series should be the same.

Honza

author
#1 lore raw

PATCH 1/9 Update ocfs2_fs.h to contain all necessary quota structures and constants, create quota_tree.h

---
 include/ocfs2-kernel/ocfs2_fs.h   |   10 +++++++++-
 include/ocfs2-kernel/quota_tree.h |   15 +++++++++++++++
 2 files changed, 24 insertions(+), 1 deletions(-)
 create mode 100644 include/ocfs2-kernel/quota_tree.h
include/ocfs2-kernel/ocfs2_fs.h +9 -1
index 2140bb3..f3c9b53 100644
@@ -344,13 +344,17 @@ enum {
 #define OCFS2_FIRST_ONLINE_SYSTEM_INODE SLOT_MAP_SYSTEM_INODE
 	HEARTBEAT_SYSTEM_INODE,
 	GLOBAL_BITMAP_SYSTEM_INODE,
-#define OCFS2_LAST_GLOBAL_SYSTEM_INODE GLOBAL_BITMAP_SYSTEM_INODE
+	USER_QUOTA_SYSTEM_INODE,
+	GROUP_QUOTA_SYSTEM_INODE,
+#define OCFS2_LAST_GLOBAL_SYSTEM_INODE GROUP_QUOTA_SYSTEM_INODE
 	ORPHAN_DIR_SYSTEM_INODE,
 	EXTENT_ALLOC_SYSTEM_INODE,
 	INODE_ALLOC_SYSTEM_INODE,
 	JOURNAL_SYSTEM_INODE,
 	LOCAL_ALLOC_SYSTEM_INODE,
 	TRUNCATE_LOG_SYSTEM_INODE,
+	LOCAL_USER_QUOTA_SYSTEM_INODE,
+	LOCAL_GROUP_QUOTA_SYSTEM_INODE,
 	NUM_SYSTEM_INODES
 };
 
@@ -364,6 +368,8 @@ static struct ocfs2_system_inode_info ocfs2_system_inodes[NUM_SYSTEM_INODES] = {
 	[SLOT_MAP_SYSTEM_INODE]			= { "slot_map", 0, S_IFREG | 0644 },
 	[HEARTBEAT_SYSTEM_INODE]		= { "heartbeat", OCFS2_HEARTBEAT_FL, S_IFREG | 0644 },
 	[GLOBAL_BITMAP_SYSTEM_INODE]		= { "global_bitmap", 0, S_IFREG | 0644 },
+	[USER_QUOTA_SYSTEM_INODE]		= { "aquota.user", OCFS2_QUOTA_FL, S_IFREG | 0644 },
+	[GROUP_QUOTA_SYSTEM_INODE]		= { "aquota.group", OCFS2_QUOTA_FL, S_IFREG | 0644 },
 
 	/* Slot-specific system inodes (one copy per slot) */
 	[ORPHAN_DIR_SYSTEM_INODE]		= { "orphan_dir:%04d", 0, S_IFDIR | 0755 },
@@ -372,6 +378,8 @@ static struct ocfs2_system_inode_info ocfs2_system_inodes[NUM_SYSTEM_INODES] = {
 	[JOURNAL_SYSTEM_INODE]			= { "journal:%04d", OCFS2_JOURNAL_FL, S_IFREG | 0644 },
 	[LOCAL_ALLOC_SYSTEM_INODE]		= { "local_alloc:%04d", OCFS2_BITMAP_FL | OCFS2_LOCAL_ALLOC_FL, S_IFREG | 0644 },
 	[TRUNCATE_LOG_SYSTEM_INODE]		= { "truncate_log:%04d", OCFS2_DEALLOC_FL, S_IFREG | 0644 },
+	[LOCAL_USER_QUOTA_SYSTEM_INODE]		= { "aquota.user:%04d", OCFS2_QUOTA_FL, S_IFREG | 0644 },
+	[LOCAL_GROUP_QUOTA_SYSTEM_INODE]	= { "aquota.group:%04d", OCFS2_QUOTA_FL, S_IFREG | 0644 },
 };
 
 /* Parameter passed from mount.ocfs2 to module */
include/ocfs2-kernel/quota_tree.h new file +15 -0
new file mode 100644
index 0000000..9dbe835
@@ -0,0 +1,15 @@
+#ifndef _QUOTA_TREE_H
+#define _QUOTA_TREE_H
+
+#define QT_TREEOFF 1
+
+/* Header of leaf tree block */
+struct qt_disk_dqdbheader {
+	__le32 dqdh_next_free;	/* Number of next block with free entry */
+	__le32 dqdh_prev_free;	/* Number of previous block with free entry */
+	__le16 dqdh_entries;	/* Number of valid entries in block */
+	__le16 dqdh_pad1;
+	__le32 dqdh_pad2;
+};
+
+#endif
Signature · 1 line
1.6.0.2
author
#2 lore raw

PATCH 2/9 Provide ocfs2_cached_inode_extend_allocation()

In reply to Jan Kara

So far we had only ocfs2_extend_allocation() which read the inode from disk. Provide also the cached variant.

---
 include/ocfs2/ocfs2.h  |    3 ++
 libocfs2/extend_file.c |   52 +++++++++++++++++++++++++----------------------
 2 files changed, 31 insertions(+), 24 deletions(-)
include/ocfs2/ocfs2.h +3 -0
index 4dbbf43..47fede3 100644
@@ -505,6 +505,9 @@ errcode_t ocfs2_delete_extent_block(ocfs2_filesys *fs, uint64_t blkno);
  */
 errcode_t ocfs2_extend_allocation(ocfs2_filesys *fs, uint64_t ino,
 				  uint32_t new_clusters);
+/* Ditto for cached inode */
+errcode_t ocfs2_cached_inode_extend_allocation(ocfs2_cached_inode *ci,
+					       uint32_t new_clusters);
 /* Extend the file to the new size. No clusters will be allocated. */
 errcode_t ocfs2_extend_file(ocfs2_filesys *fs, uint64_t ino, uint64_t new_size);
 
libocfs2/extend_file.c +28 -24

80 lines not on this page · raw message

Signature · 1 line
1.6.0.2
author
#3 lore raw

PATCH 3/9 Implement quota functions to libocfs2

In reply to Jan Kara
---
 include/ocfs2/ocfs2.h     |   87 ++++
 libocfs2/Makefile         |    1 +
 libocfs2/feature_string.c |   18 +
 libocfs2/ocfs2_err.et     |    6 +
 libocfs2/quota.c          | 1217 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1329 insertions(+), 0 deletions(-)
 create mode 100644 libocfs2/quota.c
include/ocfs2/ocfs2.h +87 -0
index 47fede3..9f861d6 100644
@@ -50,6 +50,7 @@
 #include <ocfs2-kernel/kernel-list.h>
 #include <ocfs2-kernel/sparse_endian_types.h>
 #include <ocfs2-kernel/ocfs2_fs.h>
+#include <ocfs2-kernel/quota_tree.h>
 #include <o2dlm/o2dlm.h>
 #include <o2cb/o2cb.h>
 #include <ocfs2/ocfs2_err.h>
@@ -125,16 +126,36 @@
 #define OCFS2_CHB_WAITING	2
 #define OCFS2_CHB_COMPLETE	3
 
+/* Flags for global quotafile info */
+#define OCFS2_QF_INFO_DIRTY 1
+
 typedef void (*ocfs2_chb_notify)(int state, char *progress, void *data);
 
 typedef struct _ocfs2_filesys ocfs2_filesys;
 typedef struct _ocfs2_cached_inode ocfs2_cached_inode;
+typedef struct _ocfs2_cached_dquot ocfs2_cached_dquot;
 typedef struct _io_channel io_channel;
 typedef struct _ocfs2_inode_scan ocfs2_inode_scan;
 typedef struct _ocfs2_dir_scan ocfs2_dir_scan;
 typedef struct _ocfs2_bitmap ocfs2_bitmap;
 typedef struct _ocfs2_devices ocfs2_devices;
 
+#define MAXQUOTAS 2
+#define USRQUOTA 0
+#define GRPQUOTA 1
+
+#define OCFS2_DEF_BLOCK_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_INODE_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_QUOTA_SYNC 10000   /* 10 seconds */
+
+struct _ocfs2_quota_info {
+	ocfs2_cached_inode *qi_inode;
+	int flags;
+	struct ocfs2_global_disk_dqinfo qi_info;
+};
+
+typedef struct _ocfs2_quota_info ocfs2_quota_info;
+
 struct _ocfs2_filesys {
 	char *fs_devname;
 	uint32_t fs_flags;
@@ -161,6 +182,8 @@ struct _ocfs2_filesys {
 	struct o2dlm_ctxt *fs_dlm_ctxt;
 	struct ocfs2_image_state *ost;
 
+	ocfs2_quota_info qinfo[MAXQUOTAS];
+
 	/* Reserved for the use of the calling application. */
 	void *fs_private;
 };
@@ -172,6 +195,15 @@ struct _ocfs2_cached_inode {
 	ocfs2_bitmap *ci_chains;
 };
 
+typedef unsigned int qid_t;
+
+struct _ocfs2_cached_dquot {
+	loff_t d_off;	/* Offset of structure in the file */
+	struct _ocfs2_cached_dquot *d_next;	/* Next entry in hashchain */
+	struct _ocfs2_cached_dquot **d_pprev;	/* Previous pointer in hashchain */
+	struct ocfs2_global_disk_dqblk d_ddquot;	/* Quota entry */
+};
+
 struct ocfs2_slot_data {
 	int		sd_valid;
 	unsigned int	sd_node_num;
@@ -205,6 +237,14 @@ struct _ocfs2_fs_options {
 	uint32_t opt_ro_compat;
 };
 
+struct _ocfs2_quota_hash {
+	int alloc_entries;
+	int used_entries;
+	ocfs2_cached_dquot **hash;
+};
+
+typedef struct _ocfs2_quota_hash ocfs2_quota_hash;
+
 errcode_t ocfs2_malloc(unsigned long size, void *ptr);
 errcode_t ocfs2_malloc0(unsigned long size, void *ptr);
 errcode_t ocfs2_free(void *ptr);
@@ -581,6 +621,53 @@ errcode_t ocfs2_meta_lock(ocfs2_filesys *fs, ocfs2_cached_inode *inode,
 
 errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs, ocfs2_cached_inode *ci);
 
+/* Quota operations */
+static inline int ocfs2_global_dqstr_in_blk(int blocksize)
+{
+	return (blocksize - OCFS2_QBLK_RESERVED_SPACE -
+		sizeof(struct qt_disk_dqdbheader)) /
+		sizeof(struct ocfs2_global_disk_dqblk);
+}
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header);
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info);
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk);
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info);
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk);
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader);
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno);
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type);
+int ocfs2_qtree_depth(int blocksize);
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot);
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot);
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp);
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash);
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash);
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data);
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash);
+
 /* Low level */
 void ocfs2_swap_slot_map(struct ocfs2_slot_map *sm, int num_slots);
 void ocfs2_swap_slot_map_extended(struct ocfs2_slot_map_extended *se,
libocfs2/Makefile +1 -0
index 48cfe80..eeb854a 100644
@@ -73,6 +73,7 @@ CFILES = 		\
 	lockid.c	\
 	backup_super.c	\
 	feature_string.c\
+	quota.c		\
 	image.c		\
 	xattr.c
 
libocfs2/feature_string.c +18 -0
index 17e2675..18ae6e9 100644
@@ -123,6 +123,16 @@ static struct fs_feature_flags ocfs2_supported_features[] = {
 		{0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		"usrquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		"grpquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		NULL,
 		{0, 0, 0},
 		{0, 0, 0}
@@ -190,6 +200,14 @@ static struct feature_name ocfs2_feature_names[] = {
 		.fn_flag = {0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		.fn_name = "usrquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		.fn_name = "grpquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		.fn_name = NULL,
 	},
 };
libocfs2/ocfs2_err.et +6 -0
index ddfa07c..13b03c4 100644
@@ -183,7 +183,13 @@ ec	OCFS2_ET_BAD_XATTR_BLOCK_MAGIC,
 ec	OCFS2_ET_UNKNOWN_FEATURE,
 	"Unknown feature"
 
+ec	OCFS2_ET_CORRUPT_QUOTA_FILE,
+	"Quota file is corrupted"
+
 ec	OCFS2_ET_CANNOT_DETERMINE_SECTOR_SIZE,
 	"Cannot determine sector size"
 
+ec	OCFS2_ET_NONEMTY_QUOTA_HASH,
+	"Freeing non-empty quota hash"
+
 	end
libocfs2/quota.c new file +1217 -0

1,217 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 3/9 Implement quota functions to libocfs2

Quoted 1,415 lines · Jan Kara
---
 include/ocfs2/ocfs2.h     |   87 ++++
 libocfs2/Makefile         |    1 +
 libocfs2/feature_string.c |   18 +
 libocfs2/ocfs2_err.et     |    6 +
 libocfs2/quota.c          | 1217 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1329 insertions(+), 0 deletions(-)
 create mode 100644 libocfs2/quota.c
include/ocfs2/ocfs2.h +87 -0
index 47fede3..9f861d6 100644
@@ -50,6 +50,7 @@
 #include <ocfs2-kernel/kernel-list.h>
 #include <ocfs2-kernel/sparse_endian_types.h>
 #include <ocfs2-kernel/ocfs2_fs.h>
+#include <ocfs2-kernel/quota_tree.h>
 #include <o2dlm/o2dlm.h>
 #include <o2cb/o2cb.h>
 #include <ocfs2/ocfs2_err.h>
@@ -125,16 +126,36 @@
 #define OCFS2_CHB_WAITING	2
 #define OCFS2_CHB_COMPLETE	3
 
+/* Flags for global quotafile info */
+#define OCFS2_QF_INFO_DIRTY 1
+
 typedef void (*ocfs2_chb_notify)(int state, char *progress, void *data);
 
 typedef struct _ocfs2_filesys ocfs2_filesys;
 typedef struct _ocfs2_cached_inode ocfs2_cached_inode;
+typedef struct _ocfs2_cached_dquot ocfs2_cached_dquot;
 typedef struct _io_channel io_channel;
 typedef struct _ocfs2_inode_scan ocfs2_inode_scan;
 typedef struct _ocfs2_dir_scan ocfs2_dir_scan;
 typedef struct _ocfs2_bitmap ocfs2_bitmap;
 typedef struct _ocfs2_devices ocfs2_devices;
 
+#define MAXQUOTAS 2
+#define USRQUOTA 0
+#define GRPQUOTA 1
+
+#define OCFS2_DEF_BLOCK_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_INODE_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_QUOTA_SYNC 10000   /* 10 seconds */
+
+struct _ocfs2_quota_info {
+	ocfs2_cached_inode *qi_inode;
+	int flags;
+	struct ocfs2_global_disk_dqinfo qi_info;
+};
+
+typedef struct _ocfs2_quota_info ocfs2_quota_info;
+
 struct _ocfs2_filesys {
 	char *fs_devname;
 	uint32_t fs_flags;
@@ -161,6 +182,8 @@ struct _ocfs2_filesys {
 	struct o2dlm_ctxt *fs_dlm_ctxt;
 	struct ocfs2_image_state *ost;
 
+	ocfs2_quota_info qinfo[MAXQUOTAS];
+
 	/* Reserved for the use of the calling application. */
 	void *fs_private;
 };
@@ -172,6 +195,15 @@ struct _ocfs2_cached_inode {
 	ocfs2_bitmap *ci_chains;
 };
 
+typedef unsigned int qid_t;
+
+struct _ocfs2_cached_dquot {
+	loff_t d_off;	/* Offset of structure in the file */
+	struct _ocfs2_cached_dquot *d_next;	/* Next entry in hashchain */
+	struct _ocfs2_cached_dquot **d_pprev;	/* Previous pointer in hashchain */
+	struct ocfs2_global_disk_dqblk d_ddquot;	/* Quota entry */
+};
+
 struct ocfs2_slot_data {
 	int		sd_valid;
 	unsigned int	sd_node_num;
@@ -205,6 +237,14 @@ struct _ocfs2_fs_options {
 	uint32_t opt_ro_compat;
 };
 
+struct _ocfs2_quota_hash {
+	int alloc_entries;
+	int used_entries;
+	ocfs2_cached_dquot **hash;
+};
+
+typedef struct _ocfs2_quota_hash ocfs2_quota_hash;
+
 errcode_t ocfs2_malloc(unsigned long size, void *ptr);
 errcode_t ocfs2_malloc0(unsigned long size, void *ptr);
 errcode_t ocfs2_free(void *ptr);
@@ -581,6 +621,53 @@ errcode_t ocfs2_meta_lock(ocfs2_filesys *fs, ocfs2_cached_inode *inode,
 
 errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs, ocfs2_cached_inode *ci);
 
+/* Quota operations */
+static inline int ocfs2_global_dqstr_in_blk(int blocksize)
+{
+	return (blocksize - OCFS2_QBLK_RESERVED_SPACE -
+		sizeof(struct qt_disk_dqdbheader)) /
+		sizeof(struct ocfs2_global_disk_dqblk);
+}
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header);
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info);
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk);
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info);
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk);
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader);
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno);
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type);
+int ocfs2_qtree_depth(int blocksize);
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot);
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot);
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp);
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash);
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash);
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data);
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash);
+
 /* Low level */
 void ocfs2_swap_slot_map(struct ocfs2_slot_map *sm, int num_slots);
 void ocfs2_swap_slot_map_extended(struct ocfs2_slot_map_extended *se,
libocfs2/Makefile +1 -0
index 48cfe80..eeb854a 100644
@@ -73,6 +73,7 @@ CFILES = 		\
 	lockid.c	\
 	backup_super.c	\
 	feature_string.c\
+	quota.c		\
 	image.c		\
 	xattr.c
 
libocfs2/feature_string.c +18 -0
index 17e2675..18ae6e9 100644
@@ -123,6 +123,16 @@ static struct fs_feature_flags ocfs2_supported_features[] = {
 		{0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		"usrquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		"grpquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		NULL,
 		{0, 0, 0},
 		{0, 0, 0}
@@ -190,6 +200,14 @@ static struct feature_name ocfs2_feature_names[] = {
 		.fn_flag = {0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		.fn_name = "usrquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		.fn_name = "grpquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		.fn_name = NULL,
 	},
 };
libocfs2/ocfs2_err.et +6 -0
index ddfa07c..13b03c4 100644
@@ -183,7 +183,13 @@ ec	OCFS2_ET_BAD_XATTR_BLOCK_MAGIC,
 ec	OCFS2_ET_UNKNOWN_FEATURE,
 	"Unknown feature"
 
+ec	OCFS2_ET_CORRUPT_QUOTA_FILE,
+	"Quota file is corrupted"
+
 ec	OCFS2_ET_CANNOT_DETERMINE_SECTOR_SIZE,
 	"Cannot determine sector size"
 
+ec	OCFS2_ET_NONEMTY_QUOTA_HASH,
+	"Freeing non-empty quota hash"
+
 	end
libocfs2/quota.c new file +1190 -0
new file mode 100644
index 0000000..85ba576
@@ -0,0 +1,1217 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * quota.c
+ *
+ * Quota operations for the OCFS2 userspace library.
+ *
+ * Copyright (C) 2008 Novell.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <inttypes.h>
+
+#include "ocfs2/byteorder.h"
+#include "ocfs2/ocfs2.h"
+
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header)
+{
+	if (cpu_is_little_endian)
+		return;
+	header->dqh_magic = bswap_32(header->dqh_magic);
+	header->dqh_version = bswap_32(header->dqh_version);
+}
+
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_flags = bswap_32(info->dqi_flags);
+	info->dqi_chunks = bswap_32(info->dqi_chunks);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+}
+
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk)
+{
+	if (cpu_is_little_endian)
+		return;
+	chunk->dqc_free = bswap_32(chunk->dqc_free);
+}
+
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_bgrace = bswap_32(info->dqi_bgrace);
+	info->dqi_igrace = bswap_32(info->dqi_igrace);
+	info->dqi_syncms = bswap_32(info->dqi_syncms);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+	info->dqi_free_blk = bswap_32(info->dqi_free_blk);
+	info->dqi_free_entry = bswap_32(info->dqi_free_entry);
+}
+
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk)
+{
+	if (cpu_is_little_endian)
+		return;
+	dqblk->dqb_id = bswap_32(dqblk->dqb_id);
+	dqblk->dqb_use_count = bswap_32(dqblk->dqb_use_count);
+	dqblk->dqb_ihardlimit = bswap_64(dqblk->dqb_ihardlimit);
+	dqblk->dqb_isoftlimit = bswap_64(dqblk->dqb_isoftlimit);
+	dqblk->dqb_curinodes = bswap_64(dqblk->dqb_curinodes);
+	dqblk->dqb_bhardlimit = bswap_64(dqblk->dqb_bhardlimit);
+	dqblk->dqb_bsoftlimit = bswap_64(dqblk->dqb_bsoftlimit);
+	dqblk->dqb_curspace = bswap_64(dqblk->dqb_curspace);
+	dqblk->dqb_btime = bswap_64(dqblk->dqb_btime);
+	dqblk->dqb_itime = bswap_64(dqblk->dqb_itime);
+}
+
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader)
+{
+	if (cpu_is_little_endian)
+		return;
+	bheader->dqdh_next_free = bswap_32(bheader->dqdh_next_free);
+	bheader->dqdh_prev_free = bswap_32(bheader->dqdh_prev_free);
+	bheader->dqdh_entries = bswap_16(bheader->dqdh_entries);
+}
+
+/* Should be power of two */
+#define DEFAULT_QUOTA_HASH_SIZE 8192
+/* Maxinum number of hash buckets - use at most 16 MB on a 64-bit arch */
+#define MAX_QUOTA_HASH_SIZE (1<<21)
+
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp)
+{
+	ocfs2_quota_hash *hash;
+	errcode_t err;
+
+	err = ocfs2_malloc(sizeof(ocfs2_quota_hash), &hash);
+	if (err)
+		return err;
+	hash->alloc_entries = DEFAULT_QUOTA_HASH_SIZE;
+	hash->used_entries = 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+			    DEFAULT_QUOTA_HASH_SIZE, &hash->hash);
+	if (err) {
+		ocfs2_free(&hash);
+		return err;
+	}
+	*hashp = hash;
+	return 0;
+}
+
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash)
+{
+	errcode_t err = 0, ret;
+
+	if (hash->used_entries)
+		return OCFS2_ET_NONEMTY_QUOTA_HASH;
+	ret = ocfs2_free(&hash->hash);
+	if (!err && ret)
+		err = ret;
+	ret = ocfs2_free(&hash);
+	if (!err && ret)
+		err = ret;
+	return err;
+}
+
+static int quota_hash(ocfs2_quota_hash *hash, qid_t id)
+{
+	return (((unsigned long)id) * 5) & (hash->alloc_entries - 1);
+}
+
+static void quota_add_hash_chain(ocfs2_quota_hash *hash,
+				 ocfs2_cached_dquot *dquot)
+{
+	int hash_val = quota_hash(hash, dquot->d_ddquot.dqb_id);
+
+	dquot->d_next = hash->hash[hash_val];
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = &dquot->d_next;
+	hash->hash[hash_val] = dquot;
+	dquot->d_pprev = hash->hash + hash_val;
+}
+
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+
+	if (hash->used_entries > hash->alloc_entries &&
+	    hash->alloc_entries * 2 < MAX_QUOTA_HASH_SIZE) {
+		ocfs2_cached_dquot **new_hash, **old_hash;
+		ocfs2_cached_dquot *h_dquot, *h_next;
+		int i;
+		int old_entries;
+
+		err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+				    hash->alloc_entries * 2, &new_hash);
+		if (err)
+			return err;
+		old_entries = hash->alloc_entries;
+		old_hash = hash->hash;
+		hash->alloc_entries *= 2;
+		hash->hash = new_hash;
+		/* Rehash */
+		for (i = 0; i < old_entries; i++) {
+			for (h_dquot = old_hash[i]; h_dquot; h_dquot = h_next) {
+				h_next = h_dquot->d_next;
+				quota_add_hash_chain(hash, h_dquot);
+			}
+		}
+		err = ocfs2_free(&old_hash);
+		if (err)
+			return err;
+	}
+	quota_add_hash_chain(hash, dquot);
+	hash->used_entries++;
+	return 0;
+}
+
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	*(dquot->d_pprev) = dquot->d_next;
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = dquot->d_pprev;
+	hash->used_entries--;
+	return 0;
+}
+
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp)
+{
+	int hash_val = quota_hash(hash, id);
+	ocfs2_cached_dquot *dquot;
+
+	for (dquot = hash->hash[hash_val]; dquot; dquot = dquot->d_next) {
+		if (dquot->d_ddquot.dqb_id == id) {
+			*dquotp = dquot;
+			return 0;
+		}
+	}
+	*dquotp = NULL;
+	return 0;
+}
+
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp)
+{
+	errcode_t err;
+
+	err = ocfs2_find_quota_hash(hash, id, dquotp);
+	if (err)
+		return err;
+	if (*dquotp)
+		return 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), dquotp);
+	if (err)
+		return err;
+	(*dquotp)->d_ddquot.dqb_id = id;
+	err = ocfs2_insert_quota_hash(hash, *dquotp);
+	if (err) {
+		ocfs2_free(dquotp);
+		return err;
+	}
+	return 0;
+}
+
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash)
+{
+	errcode_t err = 0;
+	ocfs2_inode_scan *scan;
+	uint64_t blkno;
+	char *buf;
+	int close_scan = 0;
+	struct ocfs2_dinode *di;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	di = (struct ocfs2_dinode *)buf;
+
+	err = ocfs2_open_inode_scan(fs, &scan);
+	if (err)
+		goto out;
+	close_scan = 1;
+
+	while (1) {
+		err = ocfs2_get_next_inode(scan, &blkno, buf);
+		if (err || !blkno)
+			break;
+		/*
+		 * Check whether the inode looks reasonable and interesting
+		 * for quota
+		 */
+		if (memcmp(di->i_signature, OCFS2_INODE_SIGNATURE,
+			   strlen(OCFS2_INODE_SIGNATURE)))
+			continue;
+		ocfs2_swap_inode_to_cpu(di, fs->fs_blocksize);
+		if (di->i_fs_generation != fs->fs_super->i_fs_generation)
+			continue;
+		if (!(di->i_flags & OCFS2_VALID_FL))
+			continue;
+		if (di->i_flags & OCFS2_SYSTEM_FL &&
+		    blkno != OCFS2_RAW_SB(fs->fs_super)->s_root_blkno)
+			continue;
+		if (usr_hash) {
+			err = ocfs2_find_create_quota_hash(usr_hash, di->i_uid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+		if (grp_hash) {
+			err = ocfs2_find_create_quota_hash(grp_hash, di->i_gid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+	}
+out:
+	if (close_scan)
+		ocfs2_close_inode_scan(scan);
+	ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data)
+{
+	errcode_t err = 0;
+	int i;
+	ocfs2_cached_dquot *dquot, *next;
+
+	for (i = 0; i < hash->alloc_entries; i++)
+		for (dquot = hash->hash[i]; dquot; dquot = next) {
+			next = dquot->d_next;
+			err = f(dquot, data);
+			if (err)
+				goto out;
+		}
+out:
+	return err;
+}
+
+struct write_rel_ctx {
+	ocfs2_filesys *fs;
+	ocfs2_quota_hash *hash;
+	int type;
+};
+
+static errcode_t write_release_quota_hash(ocfs2_cached_dquot *dquot, void *p)
+{
+	struct write_rel_ctx *ctx = p;
+	errcode_t err;
+
+	if (!dquot->d_ddquot.dqb_isoftlimit ||
+	    dquot->d_ddquot.dqb_curinodes < dquot->d_ddquot.dqb_isoftlimit)
+		dquot->d_ddquot.dqb_itime = 0;
+	if (!dquot->d_ddquot.dqb_bsoftlimit ||
+	    dquot->d_ddquot.dqb_curspace < dquot->d_ddquot.dqb_bsoftlimit)
+		dquot->d_ddquot.dqb_btime = 0;
+
+	err = ocfs2_write_dquot(ctx->fs, ctx->type, dquot);
+	if (err)
+		return err;
+	err = ocfs2_remove_quota_hash(ctx->hash, dquot);
+	if (err)
+		return err;
+	return ocfs2_free(&dquot);
+}
+
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash)
+{
+	struct write_rel_ctx ctx;
+
+	ctx.fs = fs;
+	ctx.hash = hash;
+	ctx.type = type;
+
+	return ocfs2_iterate_quota_hash(hash, write_release_quota_hash, &ctx);
+}
+
+static void mark_quotafile_info_dirty(ocfs2_filesys *fs, int type)
+{
+	fs->qinfo[type].flags |= OCFS2_QF_INFO_DIRTY;
+}
+
+static void ocfs2_checksum_quota_block(ocfs2_filesys *fs, char *buf)
+{
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	ocfs2_compute_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+#define OCFS2_LOCAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno)
+{
+	ocfs2_cached_inode *ci = NULL;
+	struct ocfs2_dinode *di;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_local_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_LOCAL_QMAGICS;
+	int versions[] = OCFS2_LOCAL_QVERSIONS;
+	char *buf = NULL;
+	unsigned int written;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_LOCAL_QF_INIT_BLOCKS);
+	errcode_t err;
+
+	err = ocfs2_read_cached_inode(fs, blkno, &ci);
+	if (err)
+		goto out;
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	di = ci->ci_inode;
+
+	/* We need at least two blocks */
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_LOCAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+	err = ocfs2_write_inode(fs, blkno, (char *)di);
+	if (err)
+		goto out;
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_LOCAL_QF_INIT_BLOCKS, &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_local_disk_dqinfo *)(buf + OCFS2_LOCAL_INFO_OFF);
+	info->dqi_chunks = 1;
+	info->dqi_blocks = OCFS2_LOCAL_QF_INIT_BLOCKS;
+	info->dqi_flags = OLQF_CLEAN;
+	ocfs2_swap_quota_local_info(info);
+
+	/* There are no free chunks because there are no blocks allocated for
+	 * them yet. So chunk header is all-zero and needs no initialization */
+	ocfs2_checksum_quota_block(fs, buf);
+	ocfs2_checksum_quota_block(fs, buf + fs->fs_blocksize);
+	err = ocfs2_file_write(ci, buf, bytes, 0, &written);
+	if (!err && written != bytes) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+out:
+	if (ci)
+		ocfs2_free_cached_inode(fs, ci);
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type)
+{
+	int num_slots = OCFS2_RAW_SB(fs->fs_super)->s_max_slots;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+	uint64_t blkno;
+	int local_type = (type == USRQUOTA) ? LOCAL_USER_QUOTA_SYSTEM_INODE :
+					      LOCAL_GROUP_QUOTA_SYSTEM_INODE;
+	int i;
+
+	for (i = 0; i < num_slots; i++) {
+		ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+						local_type, i);
+		ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname,
+				   strlen(fname), NULL, &blkno);
+		if (ret)
+			return ret;
+		/* This is here mainly for fsck... */
+		ret = ocfs2_truncate(fs, blkno, 0);
+		if (ret)
+			return ret;
+		ret = ocfs2_init_local_quota_file(fs, type, blkno);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+/* Return depth of quota tree in global file */
+int ocfs2_qtree_depth(int blocksize)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+	unsigned long long entries = epb;
+	int i;
+
+	for (i = 1; entries < (1ULL << 32); i++)
+		entries *= epb;
+	return i;
+}
+
+/* Returns index of next block in the tree of dquots */
+static int ocfs2_qtree_index(int blocksize, qid_t id, int depth)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+
+	depth = ocfs2_qtree_depth(blocksize) - depth - 1;
+	while (depth--)
+		id /= epb;
+	return id % epb;
+}
+
+/* Is given leaf entry unused? */
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddquot, sizeof(empty));
+}
+
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type)
+{
+	int global_type = (type == USRQUOTA) ?
+				USER_QUOTA_SYSTEM_INODE :
+				GROUP_QUOTA_SYSTEM_INODE;
+	uint64_t blkno;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+
+	ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+		global_type, 0);
+	ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname, strlen(fname),
+			   NULL, &blkno);
+	if (ret)
+		return ret;
+	ret = ocfs2_read_cached_inode(fs, blkno, &(fs->qinfo[type].qi_inode));
+	if (ret)
+		return ret;
+	return 0;
+}
+
+/* Read given block */
+static errcode_t read_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			  char *buf)
+{
+	errcode_t err;
+	uint32_t got;
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	err = ocfs2_file_read(fs->qinfo[type].qi_inode, buf,
+			      fs->fs_blocksize, blk * fs->fs_blocksize, &got);
+	if (err)
+		return err;
+	if (got != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_READ;
+
+	return ocfs2_validate_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+/* Write given block */
+static errcode_t write_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			   char *buf)
+{
+	errcode_t err;
+	uint32_t written;
+
+	ocfs2_checksum_quota_block(fs, buf);
+
+	err = ocfs2_file_write(fs->qinfo[type].qi_inode, buf, fs->fs_blocksize,
+			       blk * fs->fs_blocksize, &written);
+	if (err)
+		return err;
+	if (written != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_WRITE;
+	return 0;
+}
+
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	char *buf;
+	errcode_t ret;
+	struct ocfs2_global_disk_dqinfo *info;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+
+	ret = read_blk(fs, type, 0, buf);
+	if (ret)
+		return ret;
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	ocfs2_swap_quota_global_info(info);
+	memcpy(&(fs->qinfo[type].qi_info), info,
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	return 0;
+}
+
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	errcode_t ret;
+	char *buf;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	memcpy(info, &(fs->qinfo[type].qi_info),
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	ocfs2_swap_quota_global_info(info);
+	ret = write_blk(fs, type, 0, buf);
+	if (ret)
+		goto bail;
+bail:
+	ocfs2_free(&buf);
+	return ret;
+}
+
+#define OCFS2_GLOBAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type)
+{
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+	struct ocfs2_dinode *di;
+	char *buf = NULL;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+	errcode_t err;
+	int i;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS);
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+
+	/* Mark info dirty so that quota inode gets written */
+	mark_quotafile_info_dirty(fs, type);
+
+	di = ci->ci_inode;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_GLOBAL_QF_INIT_BLOCKS,
+				  &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	fs->qinfo[type].qi_info.dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	fs->qinfo[type].qi_info.dqi_free_blk = 0;
+	fs->qinfo[type].qi_info.dqi_free_entry = 0;
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	info->dqi_bgrace = fs->qinfo[type].qi_info.dqi_bgrace;
+	info->dqi_igrace = fs->qinfo[type].qi_info.dqi_igrace;
+	info->dqi_syncms = fs->qinfo[type].qi_info.dqi_syncms;
+	info->dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	info->dqi_free_blk = 0;
+	info->dqi_free_entry = 0;
+	ocfs2_swap_quota_global_info(info);
+
+	/*
+	 * Write the buffer here so that all the headers are properly written.
+	 * Normally we don't write tree root block.
+	 */
+	for (i = 0; i < OCFS2_GLOBAL_QF_INIT_BLOCKS; i++) {
+		err = write_blk(fs, type, i, buf + (i * fs->fs_blocksize));
+		if (err)
+			goto out;
+	}
+out:
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+/* Is given dquot empty? */
+static int ocfs2_global_entry_unused(struct ocfs2_global_disk_dqblk *ddqblk)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddqblk, sizeof(empty));
+}
+
+/* Get free block in file (either from free list or create new one) */
+static errcode_t ocfs2_get_free_dqblk(ocfs2_filesys *fs, int type,
+				      unsigned int *blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	if (info->dqi_free_blk) {
+		*blk = info->dqi_free_blk;
+		err = read_blk(fs, type, *blk, buf);
+		if (err)
+			goto bail;
+		info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
+	}
+	else {
+		if (info->dqi_blocks ==
+		    ocfs2_clusters_to_blocks(fs, ci->ci_inode->i_clusters)) {
+			err = ocfs2_cached_inode_extend_allocation(ci, 1);
+			if (err)
+				goto bail;
+		}
+		*blk = info->dqi_blocks++;
+		ci->ci_inode->i_size =
+				ocfs2_blocks_to_bytes(fs, info->dqi_blocks);
+	}
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Put given block to free list */
+static errcode_t ocfs2_put_free_dqblk(ocfs2_filesys *fs, int type,
+				      char *buf, unsigned int blk)
+{
+	errcode_t err;
+	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	dh->dqdh_next_free = info->dqi_free_blk;
+	dh->dqdh_prev_free = 0;
+	dh->dqdh_entries = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		return err;
+	info->dqi_free_blk = blk;
+	mark_quotafile_info_dirty(fs, type);
+	return 0;
+}
+
+/* Remove given block from the list of blocks with free entries */
+static errcode_t ocfs2_remove_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *dh, *tdh;
+	unsigned int nextblk, prevblk;
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+	nextblk = dh->dqdh_next_free;
+	prevblk = dh->dqdh_prev_free;
+
+	if (nextblk) {
+		err = read_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = prevblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	if (prevblk) {
+		/* Failure here is bad since we potentially corrupt free list.
+		 * OTOH something must be really wrong when read/write fails */
+		err = read_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_next_free = nextblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	else {
+		fs->qinfo[type].qi_info.dqi_free_entry = nextblk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	dh->dqdh_next_free = dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	/* No matter whether write succeeds block is out of list */
+	write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Insert given block to the beginning of list with free entries */
+static errcode_t ocfs2_insert_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *tdh, *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh->dqdh_next_free = info->dqi_free_entry;
+	dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		goto bail;
+
+	if (info->dqi_free_entry) {
+		tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+		err = read_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = blk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	info->dqi_free_entry = blk;
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Find space for dquot */
+static errcode_t ocfs2_find_free_dqentry(ocfs2_filesys *fs, int type,
+					 unsigned int *treeblk, loff_t *off)
+{
+	errcode_t err;
+	unsigned int blk, i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	char *buf;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+	if (info->dqi_free_entry) {
+		blk = info->dqi_free_entry;
+		err = read_blk(fs, type, blk, buf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(dh);
+	}
+	else {
+		err = ocfs2_get_free_dqblk(fs, type, &blk);
+		if (err)
+			goto bail;
+		memset(buf, 0, fs->fs_blocksize);
+		info->dqi_free_entry = blk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	/* Block will be full? */
+	if (dh->dqdh_entries + 1 >=
+	    ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	dh->dqdh_entries++;
+	/* Find free structure in block */
+	for (i = 0;
+	     i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize) &&
+	     !ocfs2_global_entry_unused(ddquot + i);
+	     i++);
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	*off = (blk * fs->fs_blocksize) + sizeof(struct qt_disk_dqdbheader) +
+	       i * sizeof(struct ocfs2_global_disk_dqblk);
+	*treeblk = blk;
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Insert reference to structure into the trie */
+static errcode_t ocfs2_do_insert_tree(ocfs2_filesys *fs, int type, qid_t id,
+				      unsigned int *treeblk, int depth,
+				      loff_t *off)
+{
+	char *buf;
+	int newson = 0, newact = 0;
+	u_int32_t *ref;
+	unsigned int newblk;
+	errcode_t err;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	if (!*treeblk) {
+		err = ocfs2_get_free_dqblk(fs, type, &newblk);
+		if (err)
+			goto bail;
+		*treeblk = newblk;
+		memset(buf, 0, fs->fs_blocksize);
+		newact = 1;
+	}
+	else {
+		err = read_blk(fs, type, *treeblk, buf);
+		if (err)
+			goto bail;
+	}
+	ref = (u_int32_t *) buf;
+	newblk = le32_to_cpu(ref[
+		 ocfs2_qtree_index(fs->fs_blocksize, id, depth)]);
+	if (!newblk)
+		newson = 1;
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		if (newblk) {
+			err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+			goto bail;
+		}
+		err = ocfs2_find_free_dqentry(fs, type, &newblk, off);
+	}
+	else
+		err = ocfs2_do_insert_tree(fs, type, id, &newblk, depth + 1,
+					   off);
+	if (newson && !err) {
+		ref[ocfs2_qtree_index(fs->fs_blocksize, id, depth)] =
+							cpu_to_le32(newblk);
+		err = write_blk(fs, type, *treeblk, buf);
+	}
+	else if (newact && err)
+		ocfs2_put_free_dqblk(fs, type, buf, *treeblk);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Wrapper for inserting quota structure into tree */
+static errcode_t ocfs2_insert_qtree(ocfs2_filesys *fs, int type, qid_t id,
+				    loff_t *off)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	return ocfs2_do_insert_tree(fs, type, id, &tmp, 0, off);
+}
+
+/* Write dquot to file */
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+	char *buf;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	if (!dquot->d_off) {
+		err = ocfs2_insert_qtree(fs, type, dquot->d_ddquot.dqb_id,
+					 &dquot->d_off);
+		if (err)
+			goto bail;
+	}
+	err = read_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+	if (err)
+		goto bail;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+					(dquot->d_off % fs->fs_blocksize));
+	memcpy(ddquot, &dquot->d_ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ddquot->dqb_pad1 = 0;
+	ddquot->dqb_pad2 = 0;
+	ocfs2_swap_quota_global_dqblk(ddquot);
+	err = write_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Remove dquot entry from its data block */
+static errcode_t ocfs2_remove_leaf_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+
+	if (blk != dquot->d_off / fs->fs_blocksize)
+		return OCFS2_ET_CORRUPT_QUOTA_FILE;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	dh->dqdh_entries--;
+	if (!dh->dqdh_entries) {	/* Block got free? */
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+		err = ocfs2_put_free_dqblk(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	else {
+		memset(buf + (dquot->d_off & (fs->fs_blocksize - 1)), 0,
+		       sizeof(struct ocfs2_global_disk_dqblk));
+
+		/* First free entry? */
+		if (dh->dqdh_entries ==
+		    ocfs2_global_dqstr_in_blk(fs->fs_blocksize) - 1) {
+			/* This will also write data block */
+			err = ocfs2_insert_free_dqentry(fs, type, buf, blk);
+		}
+		else
+			err = write_blk(fs, type, blk, buf);
+	}
+	dquot->d_off = 0;
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Remove reference to dquot from tree */
+static errcode_t ocfs2_remove_tree_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int *blk,
+					   int depth)
+{
+	errcode_t err;
+	char *buf;
+	unsigned int newblk;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, *blk, buf);
+	if (err)
+		goto bail;
+
+	ref = (u_int32_t *)buf;
+	newblk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+		 dquot->d_ddquot.dqb_id, depth)]);
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		err = ocfs2_remove_leaf_dqentry(fs, type, dquot, newblk);
+		newblk = 0;
+	}
+	else
+		err = ocfs2_remove_tree_dqentry(fs, type, dquot, &newblk,
+						depth + 1);
+	if (err)
+		goto bail;
+
+	if (!newblk) {
+		int i;
+
+		ref[ocfs2_qtree_index(fs->fs_blocksize,
+				      dquot->d_ddquot.dqb_id,
+				      depth)] = cpu_to_le32(0);
+		/* Block got empty? */
+		for (i = 0; i < fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		     !buf[i]; i++);
+		/* Don't put the root block into the free block list */
+		if (i == fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		    *blk != QT_TREEOFF) {
+			err = ocfs2_put_free_dqblk(fs, type, buf, *blk);
+			if (err)
+				goto bail;
+			*blk = 0;
+		}
+		else
+			err = write_blk(fs, type, *blk, buf);
+	}
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Delete dquot from tree */
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	if (!dquot->d_off)	/* Even not allocated? */
+		return 0;
+	return ocfs2_remove_tree_dqentry(fs, type, dquot, &tmp, 0);
+}
+
+/* Find entry in block */
+static errcode_t ocfs2_find_block_dqentry(ocfs2_filesys *fs, int type,
+					  ocfs2_cached_dquot *dquot,
+					  unsigned int blk)
+{
+	char *buf;
+	errcode_t err;
+	int i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+
+	for (i = 0; i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize);
+	     i++, ddquot++) {
+		if (le32_to_cpu(ddquot->dqb_id) == dquot->d_ddquot.dqb_id) {
+			if (dquot->d_ddquot.dqb_id == 0 &&
+			    ocfs2_qtree_entry_unused(ddquot))
+				continue;
+			break;
+		}
+	}
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	dquot->d_off = blk * fs->fs_blocksize + ((char *)ddquot - buf);
+	memcpy(&dquot->d_ddquot, ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ocfs2_swap_quota_global_dqblk(&dquot->d_ddquot);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Find entry for given id in the tree */
+static errcode_t ocfs2_find_tree_dqentry(ocfs2_filesys *fs,
+					 int type,
+					 ocfs2_cached_dquot *dquot,
+					 unsigned int blk,
+					 int depth)
+{
+	errcode_t err;
+	char *buf;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	ref = (u_int32_t *)buf;
+	blk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+	      dquot->d_ddquot.dqb_id, depth)]);
+	if (!blk)		/* No reference? */
+		goto bail;
+	if (depth < ocfs2_qtree_depth(fs->fs_blocksize) - 1)
+		err = ocfs2_find_tree_dqentry(fs, type, dquot, blk, depth + 1);
+	else
+		err = ocfs2_find_block_dqentry(fs, type, dquot, blk);

libocfs2/quota.c line 1200

Quoted 10 lines
libocfs2/quota.c new file +10 -0
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/*
+ *  Read dquot from disk
+ */
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot)

One concern, the second argument 'id' never in function.

Another thing is, you malloc the space for &dquot, then pass it to caller's arguments, and we're noticing that the caller will be responsible for the relasing of allocated space instead of function itself, if it run without error. right?

unfortunately, i didn't see any caller(such as fill_sparse_files() in tunefs.ocfs2) finally release the space...

To keep consistent with other tool's code(such as ocfs2_read_inode()), i think you may allocate/free the space by caller?

Quoted 17 lines
libocfs2/quota.c new file +17 -0
+{
+	errcode_t err;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), &dquot);
+	if (err)
+		return err;
+
+	err = ocfs2_find_tree_dqentry(fs, type, dquot, QT_TREEOFF, 0);
+	if (err)
+		goto bail;
+	*ret_dquot = dquot;
+	return 0;
+bail:
+	ocfs2_free(&dquot);
+	return err;
+}
#5 lore raw

PATCH 3/9 Implement quota functions to libocfs2

Quoted 1,420 lines · tristan.ye
Quoted 1,419 lines · Jan Kara
---
 include/ocfs2/ocfs2.h     |   87 ++++
 libocfs2/Makefile         |    1 +
 libocfs2/feature_string.c |   18 +
 libocfs2/ocfs2_err.et     |    6 +
 libocfs2/quota.c          | 1217 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1329 insertions(+), 0 deletions(-)
 create mode 100644 libocfs2/quota.c
include/ocfs2/ocfs2.h +87 -0
index 47fede3..9f861d6 100644
@@ -50,6 +50,7 @@
 #include <ocfs2-kernel/kernel-list.h>
 #include <ocfs2-kernel/sparse_endian_types.h>
 #include <ocfs2-kernel/ocfs2_fs.h>
+#include <ocfs2-kernel/quota_tree.h>
 #include <o2dlm/o2dlm.h>
 #include <o2cb/o2cb.h>
 #include <ocfs2/ocfs2_err.h>
@@ -125,16 +126,36 @@
 #define OCFS2_CHB_WAITING	2
 #define OCFS2_CHB_COMPLETE	3
 
+/* Flags for global quotafile info */
+#define OCFS2_QF_INFO_DIRTY 1
+
 typedef void (*ocfs2_chb_notify)(int state, char *progress, void *data);
 
 typedef struct _ocfs2_filesys ocfs2_filesys;
 typedef struct _ocfs2_cached_inode ocfs2_cached_inode;
+typedef struct _ocfs2_cached_dquot ocfs2_cached_dquot;
 typedef struct _io_channel io_channel;
 typedef struct _ocfs2_inode_scan ocfs2_inode_scan;
 typedef struct _ocfs2_dir_scan ocfs2_dir_scan;
 typedef struct _ocfs2_bitmap ocfs2_bitmap;
 typedef struct _ocfs2_devices ocfs2_devices;
 
+#define MAXQUOTAS 2
+#define USRQUOTA 0
+#define GRPQUOTA 1
+
+#define OCFS2_DEF_BLOCK_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_INODE_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_QUOTA_SYNC 10000   /* 10 seconds */
+
+struct _ocfs2_quota_info {
+	ocfs2_cached_inode *qi_inode;
+	int flags;
+	struct ocfs2_global_disk_dqinfo qi_info;
+};
+
+typedef struct _ocfs2_quota_info ocfs2_quota_info;
+
 struct _ocfs2_filesys {
 	char *fs_devname;
 	uint32_t fs_flags;
@@ -161,6 +182,8 @@ struct _ocfs2_filesys {
 	struct o2dlm_ctxt *fs_dlm_ctxt;
 	struct ocfs2_image_state *ost;
 
+	ocfs2_quota_info qinfo[MAXQUOTAS];
+
 	/* Reserved for the use of the calling application. */
 	void *fs_private;
 };
@@ -172,6 +195,15 @@ struct _ocfs2_cached_inode {
 	ocfs2_bitmap *ci_chains;
 };
 
+typedef unsigned int qid_t;
+
+struct _ocfs2_cached_dquot {
+	loff_t d_off;	/* Offset of structure in the file */
+	struct _ocfs2_cached_dquot *d_next;	/* Next entry in hashchain */
+	struct _ocfs2_cached_dquot **d_pprev;	/* Previous pointer in hashchain */
+	struct ocfs2_global_disk_dqblk d_ddquot;	/* Quota entry */
+};
+
 struct ocfs2_slot_data {
 	int		sd_valid;
 	unsigned int	sd_node_num;
@@ -205,6 +237,14 @@ struct _ocfs2_fs_options {
 	uint32_t opt_ro_compat;
 };
 
+struct _ocfs2_quota_hash {
+	int alloc_entries;
+	int used_entries;
+	ocfs2_cached_dquot **hash;
+};
+
+typedef struct _ocfs2_quota_hash ocfs2_quota_hash;
+
 errcode_t ocfs2_malloc(unsigned long size, void *ptr);
 errcode_t ocfs2_malloc0(unsigned long size, void *ptr);
 errcode_t ocfs2_free(void *ptr);
@@ -581,6 +621,53 @@ errcode_t ocfs2_meta_lock(ocfs2_filesys *fs, ocfs2_cached_inode *inode,
 
 errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs, ocfs2_cached_inode *ci);
 
+/* Quota operations */
+static inline int ocfs2_global_dqstr_in_blk(int blocksize)
+{
+	return (blocksize - OCFS2_QBLK_RESERVED_SPACE -
+		sizeof(struct qt_disk_dqdbheader)) /
+		sizeof(struct ocfs2_global_disk_dqblk);
+}
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header);
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info);
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk);
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info);
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk);
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader);
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno);
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type);
+int ocfs2_qtree_depth(int blocksize);
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot);
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot);
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp);
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash);
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash);
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data);
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash);
+
 /* Low level */
 void ocfs2_swap_slot_map(struct ocfs2_slot_map *sm, int num_slots);
 void ocfs2_swap_slot_map_extended(struct ocfs2_slot_map_extended *se,
libocfs2/Makefile +1 -0
index 48cfe80..eeb854a 100644
@@ -73,6 +73,7 @@ CFILES = 		\
 	lockid.c	\
 	backup_super.c	\
 	feature_string.c\
+	quota.c		\
 	image.c		\
 	xattr.c
 
libocfs2/feature_string.c +18 -0
index 17e2675..18ae6e9 100644
@@ -123,6 +123,16 @@ static struct fs_feature_flags ocfs2_supported_features[] = {
 		{0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		"usrquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		"grpquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		NULL,
 		{0, 0, 0},
 		{0, 0, 0}
@@ -190,6 +200,14 @@ static struct feature_name ocfs2_feature_names[] = {
 		.fn_flag = {0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		.fn_name = "usrquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		.fn_name = "grpquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		.fn_name = NULL,
 	},
 };
libocfs2/ocfs2_err.et +6 -0
index ddfa07c..13b03c4 100644
@@ -183,7 +183,13 @@ ec	OCFS2_ET_BAD_XATTR_BLOCK_MAGIC,
 ec	OCFS2_ET_UNKNOWN_FEATURE,
 	"Unknown feature"
 
+ec	OCFS2_ET_CORRUPT_QUOTA_FILE,
+	"Quota file is corrupted"
+
 ec	OCFS2_ET_CANNOT_DETERMINE_SECTOR_SIZE,
 	"Cannot determine sector size"
 
+ec	OCFS2_ET_NONEMTY_QUOTA_HASH,
+	"Freeing non-empty quota hash"
+
 	end
libocfs2/quota.c new file +1194 -0
new file mode 100644
index 0000000..85ba576
@@ -0,0 +1,1217 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * quota.c
+ *
+ * Quota operations for the OCFS2 userspace library.
+ *
+ * Copyright (C) 2008 Novell.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <inttypes.h>
+
+#include "ocfs2/byteorder.h"
+#include "ocfs2/ocfs2.h"
+
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header)
+{
+	if (cpu_is_little_endian)
+		return;
+	header->dqh_magic = bswap_32(header->dqh_magic);
+	header->dqh_version = bswap_32(header->dqh_version);
+}
+
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_flags = bswap_32(info->dqi_flags);
+	info->dqi_chunks = bswap_32(info->dqi_chunks);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+}
+
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk)
+{
+	if (cpu_is_little_endian)
+		return;
+	chunk->dqc_free = bswap_32(chunk->dqc_free);
+}
+
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_bgrace = bswap_32(info->dqi_bgrace);
+	info->dqi_igrace = bswap_32(info->dqi_igrace);
+	info->dqi_syncms = bswap_32(info->dqi_syncms);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+	info->dqi_free_blk = bswap_32(info->dqi_free_blk);
+	info->dqi_free_entry = bswap_32(info->dqi_free_entry);
+}
+
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk)
+{
+	if (cpu_is_little_endian)
+		return;
+	dqblk->dqb_id = bswap_32(dqblk->dqb_id);
+	dqblk->dqb_use_count = bswap_32(dqblk->dqb_use_count);
+	dqblk->dqb_ihardlimit = bswap_64(dqblk->dqb_ihardlimit);
+	dqblk->dqb_isoftlimit = bswap_64(dqblk->dqb_isoftlimit);
+	dqblk->dqb_curinodes = bswap_64(dqblk->dqb_curinodes);
+	dqblk->dqb_bhardlimit = bswap_64(dqblk->dqb_bhardlimit);
+	dqblk->dqb_bsoftlimit = bswap_64(dqblk->dqb_bsoftlimit);
+	dqblk->dqb_curspace = bswap_64(dqblk->dqb_curspace);
+	dqblk->dqb_btime = bswap_64(dqblk->dqb_btime);
+	dqblk->dqb_itime = bswap_64(dqblk->dqb_itime);
+}
+
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader)
+{
+	if (cpu_is_little_endian)
+		return;
+	bheader->dqdh_next_free = bswap_32(bheader->dqdh_next_free);
+	bheader->dqdh_prev_free = bswap_32(bheader->dqdh_prev_free);
+	bheader->dqdh_entries = bswap_16(bheader->dqdh_entries);
+}
+
+/* Should be power of two */
+#define DEFAULT_QUOTA_HASH_SIZE 8192
+/* Maxinum number of hash buckets - use at most 16 MB on a 64-bit arch */
+#define MAX_QUOTA_HASH_SIZE (1<<21)
+
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp)
+{
+	ocfs2_quota_hash *hash;
+	errcode_t err;
+
+	err = ocfs2_malloc(sizeof(ocfs2_quota_hash), &hash);
+	if (err)
+		return err;
+	hash->alloc_entries = DEFAULT_QUOTA_HASH_SIZE;
+	hash->used_entries = 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+			    DEFAULT_QUOTA_HASH_SIZE, &hash->hash);
+	if (err) {
+		ocfs2_free(&hash);
+		return err;
+	}
+	*hashp = hash;
+	return 0;
+}
+
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash)
+{
+	errcode_t err = 0, ret;
+
+	if (hash->used_entries)
+		return OCFS2_ET_NONEMTY_QUOTA_HASH;
+	ret = ocfs2_free(&hash->hash);
+	if (!err && ret)
+		err = ret;
+	ret = ocfs2_free(&hash);
+	if (!err && ret)
+		err = ret;
+	return err;
+}
+
+static int quota_hash(ocfs2_quota_hash *hash, qid_t id)
+{
+	return (((unsigned long)id) * 5) & (hash->alloc_entries - 1);
+}
+
+static void quota_add_hash_chain(ocfs2_quota_hash *hash,
+				 ocfs2_cached_dquot *dquot)
+{
+	int hash_val = quota_hash(hash, dquot->d_ddquot.dqb_id);
+
+	dquot->d_next = hash->hash[hash_val];
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = &dquot->d_next;
+	hash->hash[hash_val] = dquot;
+	dquot->d_pprev = hash->hash + hash_val;
+}
+
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+
+	if (hash->used_entries > hash->alloc_entries &&
+	    hash->alloc_entries * 2 < MAX_QUOTA_HASH_SIZE) {
+		ocfs2_cached_dquot **new_hash, **old_hash;
+		ocfs2_cached_dquot *h_dquot, *h_next;
+		int i;
+		int old_entries;
+
+		err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+				    hash->alloc_entries * 2, &new_hash);
+		if (err)
+			return err;
+		old_entries = hash->alloc_entries;
+		old_hash = hash->hash;
+		hash->alloc_entries *= 2;
+		hash->hash = new_hash;
+		/* Rehash */
+		for (i = 0; i < old_entries; i++) {
+			for (h_dquot = old_hash[i]; h_dquot; h_dquot = h_next) {
+				h_next = h_dquot->d_next;
+				quota_add_hash_chain(hash, h_dquot);
+			}
+		}
+		err = ocfs2_free(&old_hash);
+		if (err)
+			return err;
+	}
+	quota_add_hash_chain(hash, dquot);
+	hash->used_entries++;
+	return 0;
+}
+
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	*(dquot->d_pprev) = dquot->d_next;
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = dquot->d_pprev;
+	hash->used_entries--;
+	return 0;
+}
+
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp)
+{
+	int hash_val = quota_hash(hash, id);
+	ocfs2_cached_dquot *dquot;
+
+	for (dquot = hash->hash[hash_val]; dquot; dquot = dquot->d_next) {
+		if (dquot->d_ddquot.dqb_id == id) {
+			*dquotp = dquot;
+			return 0;
+		}
+	}
+	*dquotp = NULL;
+	return 0;
+}
+
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp)
+{
+	errcode_t err;
+
+	err = ocfs2_find_quota_hash(hash, id, dquotp);
+	if (err)
+		return err;
+	if (*dquotp)
+		return 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), dquotp);
+	if (err)
+		return err;
+	(*dquotp)->d_ddquot.dqb_id = id;
+	err = ocfs2_insert_quota_hash(hash, *dquotp);
+	if (err) {
+		ocfs2_free(dquotp);
+		return err;
+	}
+	return 0;
+}
+
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash)
+{
+	errcode_t err = 0;
+	ocfs2_inode_scan *scan;
+	uint64_t blkno;
+	char *buf;
+	int close_scan = 0;
+	struct ocfs2_dinode *di;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	di = (struct ocfs2_dinode *)buf;
+
+	err = ocfs2_open_inode_scan(fs, &scan);
+	if (err)
+		goto out;
+	close_scan = 1;
+
+	while (1) {
+		err = ocfs2_get_next_inode(scan, &blkno, buf);
+		if (err || !blkno)
+			break;
+		/*
+		 * Check whether the inode looks reasonable and interesting
+		 * for quota
+		 */
+		if (memcmp(di->i_signature, OCFS2_INODE_SIGNATURE,
+			   strlen(OCFS2_INODE_SIGNATURE)))
+			continue;
+		ocfs2_swap_inode_to_cpu(di, fs->fs_blocksize);
+		if (di->i_fs_generation != fs->fs_super->i_fs_generation)
+			continue;
+		if (!(di->i_flags & OCFS2_VALID_FL))
+			continue;
+		if (di->i_flags & OCFS2_SYSTEM_FL &&
+		    blkno != OCFS2_RAW_SB(fs->fs_super)->s_root_blkno)
+			continue;
+		if (usr_hash) {
+			err = ocfs2_find_create_quota_hash(usr_hash, di->i_uid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+		if (grp_hash) {
+			err = ocfs2_find_create_quota_hash(grp_hash, di->i_gid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+	}
+out:
+	if (close_scan)
+		ocfs2_close_inode_scan(scan);
+	ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data)
+{
+	errcode_t err = 0;
+	int i;
+	ocfs2_cached_dquot *dquot, *next;
+
+	for (i = 0; i < hash->alloc_entries; i++)
+		for (dquot = hash->hash[i]; dquot; dquot = next) {
+			next = dquot->d_next;
+			err = f(dquot, data);
+			if (err)
+				goto out;
+		}
+out:
+	return err;
+}
+
+struct write_rel_ctx {
+	ocfs2_filesys *fs;
+	ocfs2_quota_hash *hash;
+	int type;
+};
+
+static errcode_t write_release_quota_hash(ocfs2_cached_dquot *dquot, void *p)
+{
+	struct write_rel_ctx *ctx = p;
+	errcode_t err;
+
+	if (!dquot->d_ddquot.dqb_isoftlimit ||
+	    dquot->d_ddquot.dqb_curinodes < dquot->d_ddquot.dqb_isoftlimit)
+		dquot->d_ddquot.dqb_itime = 0;
+	if (!dquot->d_ddquot.dqb_bsoftlimit ||
+	    dquot->d_ddquot.dqb_curspace < dquot->d_ddquot.dqb_bsoftlimit)
+		dquot->d_ddquot.dqb_btime = 0;
+
+	err = ocfs2_write_dquot(ctx->fs, ctx->type, dquot);
+	if (err)
+		return err;
+	err = ocfs2_remove_quota_hash(ctx->hash, dquot);
+	if (err)
+		return err;
+	return ocfs2_free(&dquot);
+}
+
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash)
+{
+	struct write_rel_ctx ctx;
+
+	ctx.fs = fs;
+	ctx.hash = hash;
+	ctx.type = type;
+
+	return ocfs2_iterate_quota_hash(hash, write_release_quota_hash, &ctx);
+}
+
+static void mark_quotafile_info_dirty(ocfs2_filesys *fs, int type)
+{
+	fs->qinfo[type].flags |= OCFS2_QF_INFO_DIRTY;
+}
+
+static void ocfs2_checksum_quota_block(ocfs2_filesys *fs, char *buf)
+{
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	ocfs2_compute_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+#define OCFS2_LOCAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno)
+{
+	ocfs2_cached_inode *ci = NULL;
+	struct ocfs2_dinode *di;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_local_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_LOCAL_QMAGICS;
+	int versions[] = OCFS2_LOCAL_QVERSIONS;
+	char *buf = NULL;
+	unsigned int written;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_LOCAL_QF_INIT_BLOCKS);
+	errcode_t err;
+
+	err = ocfs2_read_cached_inode(fs, blkno, &ci);
+	if (err)
+		goto out;
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	di = ci->ci_inode;
+
+	/* We need at least two blocks */
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_LOCAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+	err = ocfs2_write_inode(fs, blkno, (char *)di);
+	if (err)
+		goto out;
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_LOCAL_QF_INIT_BLOCKS, &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_local_disk_dqinfo *)(buf + OCFS2_LOCAL_INFO_OFF);
+	info->dqi_chunks = 1;
+	info->dqi_blocks = OCFS2_LOCAL_QF_INIT_BLOCKS;
+	info->dqi_flags = OLQF_CLEAN;
+	ocfs2_swap_quota_local_info(info);
+
+	/* There are no free chunks because there are no blocks allocated for
+	 * them yet. So chunk header is all-zero and needs no initialization */
+	ocfs2_checksum_quota_block(fs, buf);
+	ocfs2_checksum_quota_block(fs, buf + fs->fs_blocksize);
+	err = ocfs2_file_write(ci, buf, bytes, 0, &written);
+	if (!err && written != bytes) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+out:
+	if (ci)
+		ocfs2_free_cached_inode(fs, ci);
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type)
+{
+	int num_slots = OCFS2_RAW_SB(fs->fs_super)->s_max_slots;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+	uint64_t blkno;
+	int local_type = (type == USRQUOTA) ? LOCAL_USER_QUOTA_SYSTEM_INODE :
+					      LOCAL_GROUP_QUOTA_SYSTEM_INODE;
+	int i;
+
+	for (i = 0; i < num_slots; i++) {
+		ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+						local_type, i);
+		ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname,
+				   strlen(fname), NULL, &blkno);
+		if (ret)
+			return ret;
+		/* This is here mainly for fsck... */
+		ret = ocfs2_truncate(fs, blkno, 0);
+		if (ret)
+			return ret;
+		ret = ocfs2_init_local_quota_file(fs, type, blkno);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+/* Return depth of quota tree in global file */
+int ocfs2_qtree_depth(int blocksize)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+	unsigned long long entries = epb;
+	int i;
+
+	for (i = 1; entries < (1ULL << 32); i++)
+		entries *= epb;
+	return i;
+}
+
+/* Returns index of next block in the tree of dquots */
+static int ocfs2_qtree_index(int blocksize, qid_t id, int depth)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+
+	depth = ocfs2_qtree_depth(blocksize) - depth - 1;
+	while (depth--)
+		id /= epb;
+	return id % epb;
+}
+
+/* Is given leaf entry unused? */
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddquot, sizeof(empty));
+}
+
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type)
+{
+	int global_type = (type == USRQUOTA) ?
+				USER_QUOTA_SYSTEM_INODE :
+				GROUP_QUOTA_SYSTEM_INODE;
+	uint64_t blkno;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+
+	ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+		global_type, 0);
+	ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname, strlen(fname),
+			   NULL, &blkno);
+	if (ret)
+		return ret;
+	ret = ocfs2_read_cached_inode(fs, blkno, &(fs->qinfo[type].qi_inode));
+	if (ret)
+		return ret;
+	return 0;
+}
+
+/* Read given block */
+static errcode_t read_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			  char *buf)
+{
+	errcode_t err;
+	uint32_t got;
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	err = ocfs2_file_read(fs->qinfo[type].qi_inode, buf,
+			      fs->fs_blocksize, blk * fs->fs_blocksize, &got);
+	if (err)
+		return err;
+	if (got != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_READ;
+
+	return ocfs2_validate_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+/* Write given block */
+static errcode_t write_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			   char *buf)
+{
+	errcode_t err;
+	uint32_t written;
+
+	ocfs2_checksum_quota_block(fs, buf);
+
+	err = ocfs2_file_write(fs->qinfo[type].qi_inode, buf, fs->fs_blocksize,
+			       blk * fs->fs_blocksize, &written);
+	if (err)
+		return err;
+	if (written != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_WRITE;
+	return 0;
+}
+
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	char *buf;
+	errcode_t ret;
+	struct ocfs2_global_disk_dqinfo *info;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+
+	ret = read_blk(fs, type, 0, buf);
+	if (ret)
+		return ret;
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	ocfs2_swap_quota_global_info(info);
+	memcpy(&(fs->qinfo[type].qi_info), info,
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	return 0;
+}
+
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	errcode_t ret;
+	char *buf;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	memcpy(info, &(fs->qinfo[type].qi_info),
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	ocfs2_swap_quota_global_info(info);
+	ret = write_blk(fs, type, 0, buf);
+	if (ret)
+		goto bail;
+bail:
+	ocfs2_free(&buf);
+	return ret;
+}
+
+#define OCFS2_GLOBAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type)
+{
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+	struct ocfs2_dinode *di;
+	char *buf = NULL;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+	errcode_t err;
+	int i;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS);
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+
+	/* Mark info dirty so that quota inode gets written */
+	mark_quotafile_info_dirty(fs, type);
+
+	di = ci->ci_inode;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_GLOBAL_QF_INIT_BLOCKS,
+				  &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	fs->qinfo[type].qi_info.dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	fs->qinfo[type].qi_info.dqi_free_blk = 0;
+	fs->qinfo[type].qi_info.dqi_free_entry = 0;
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	info->dqi_bgrace = fs->qinfo[type].qi_info.dqi_bgrace;
+	info->dqi_igrace = fs->qinfo[type].qi_info.dqi_igrace;
+	info->dqi_syncms = fs->qinfo[type].qi_info.dqi_syncms;
+	info->dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	info->dqi_free_blk = 0;
+	info->dqi_free_entry = 0;
+	ocfs2_swap_quota_global_info(info);
+
+	/*
+	 * Write the buffer here so that all the headers are properly written.
+	 * Normally we don't write tree root block.
+	 */
+	for (i = 0; i < OCFS2_GLOBAL_QF_INIT_BLOCKS; i++) {
+		err = write_blk(fs, type, i, buf + (i * fs->fs_blocksize));
+		if (err)
+			goto out;
+	}
+out:
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+/* Is given dquot empty? */
+static int ocfs2_global_entry_unused(struct ocfs2_global_disk_dqblk *ddqblk)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddqblk, sizeof(empty));
+}
+
+/* Get free block in file (either from free list or create new one) */
+static errcode_t ocfs2_get_free_dqblk(ocfs2_filesys *fs, int type,
+				      unsigned int *blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	if (info->dqi_free_blk) {
+		*blk = info->dqi_free_blk;
+		err = read_blk(fs, type, *blk, buf);
+		if (err)
+			goto bail;
+		info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
+	}
+	else {
+		if (info->dqi_blocks ==
+		    ocfs2_clusters_to_blocks(fs, ci->ci_inode->i_clusters)) {
+			err = ocfs2_cached_inode_extend_allocation(ci, 1);
+			if (err)
+				goto bail;
+		}
+		*blk = info->dqi_blocks++;
+		ci->ci_inode->i_size =
+				ocfs2_blocks_to_bytes(fs, info->dqi_blocks);
+	}
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Put given block to free list */
+static errcode_t ocfs2_put_free_dqblk(ocfs2_filesys *fs, int type,
+				      char *buf, unsigned int blk)
+{
+	errcode_t err;
+	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	dh->dqdh_next_free = info->dqi_free_blk;
+	dh->dqdh_prev_free = 0;
+	dh->dqdh_entries = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		return err;
+	info->dqi_free_blk = blk;
+	mark_quotafile_info_dirty(fs, type);
+	return 0;
+}
+
+/* Remove given block from the list of blocks with free entries */
+static errcode_t ocfs2_remove_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *dh, *tdh;
+	unsigned int nextblk, prevblk;
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+	nextblk = dh->dqdh_next_free;
+	prevblk = dh->dqdh_prev_free;
+
+	if (nextblk) {
+		err = read_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = prevblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	if (prevblk) {
+		/* Failure here is bad since we potentially corrupt free list.
+		 * OTOH something must be really wrong when read/write fails */
+		err = read_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_next_free = nextblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	else {
+		fs->qinfo[type].qi_info.dqi_free_entry = nextblk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	dh->dqdh_next_free = dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	/* No matter whether write succeeds block is out of list */
+	write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Insert given block to the beginning of list with free entries */
+static errcode_t ocfs2_insert_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *tdh, *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh->dqdh_next_free = info->dqi_free_entry;
+	dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		goto bail;
+
+	if (info->dqi_free_entry) {
+		tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+		err = read_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = blk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	info->dqi_free_entry = blk;
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Find space for dquot */
+static errcode_t ocfs2_find_free_dqentry(ocfs2_filesys *fs, int type,
+					 unsigned int *treeblk, loff_t *off)
+{
+	errcode_t err;
+	unsigned int blk, i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	char *buf;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+	if (info->dqi_free_entry) {
+		blk = info->dqi_free_entry;
+		err = read_blk(fs, type, blk, buf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(dh);
+	}
+	else {
+		err = ocfs2_get_free_dqblk(fs, type, &blk);
+		if (err)
+			goto bail;
+		memset(buf, 0, fs->fs_blocksize);
+		info->dqi_free_entry = blk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	/* Block will be full? */
+	if (dh->dqdh_entries + 1 >=
+	    ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	dh->dqdh_entries++;
+	/* Find free structure in block */
+	for (i = 0;
+	     i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize) &&
+	     !ocfs2_global_entry_unused(ddquot + i);
+	     i++);
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	*off = (blk * fs->fs_blocksize) + sizeof(struct qt_disk_dqdbheader) +
+	       i * sizeof(struct ocfs2_global_disk_dqblk);
+	*treeblk = blk;
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Insert reference to structure into the trie */
+static errcode_t ocfs2_do_insert_tree(ocfs2_filesys *fs, int type, qid_t id,
+				      unsigned int *treeblk, int depth,
+				      loff_t *off)
+{
+	char *buf;
+	int newson = 0, newact = 0;
+	u_int32_t *ref;
+	unsigned int newblk;
+	errcode_t err;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	if (!*treeblk) {
+		err = ocfs2_get_free_dqblk(fs, type, &newblk);
+		if (err)
+			goto bail;
+		*treeblk = newblk;
+		memset(buf, 0, fs->fs_blocksize);
+		newact = 1;
+	}
+	else {
+		err = read_blk(fs, type, *treeblk, buf);
+		if (err)
+			goto bail;
+	}
+	ref = (u_int32_t *) buf;
+	newblk = le32_to_cpu(ref[
+		 ocfs2_qtree_index(fs->fs_blocksize, id, depth)]);
+	if (!newblk)
+		newson = 1;
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		if (newblk) {
+			err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+			goto bail;
+		}
+		err = ocfs2_find_free_dqentry(fs, type, &newblk, off);
+	}
+	else
+		err = ocfs2_do_insert_tree(fs, type, id, &newblk, depth + 1,
+					   off);
+	if (newson && !err) {
+		ref[ocfs2_qtree_index(fs->fs_blocksize, id, depth)] =
+							cpu_to_le32(newblk);
+		err = write_blk(fs, type, *treeblk, buf);
+	}
+	else if (newact && err)
+		ocfs2_put_free_dqblk(fs, type, buf, *treeblk);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Wrapper for inserting quota structure into tree */
+static errcode_t ocfs2_insert_qtree(ocfs2_filesys *fs, int type, qid_t id,
+				    loff_t *off)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	return ocfs2_do_insert_tree(fs, type, id, &tmp, 0, off);
+}
+
+/* Write dquot to file */
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+	char *buf;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	if (!dquot->d_off) {
+		err = ocfs2_insert_qtree(fs, type, dquot->d_ddquot.dqb_id,
+					 &dquot->d_off);
+		if (err)
+			goto bail;
+	}
+	err = read_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+	if (err)
+		goto bail;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+					(dquot->d_off % fs->fs_blocksize));
+	memcpy(ddquot, &dquot->d_ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ddquot->dqb_pad1 = 0;
+	ddquot->dqb_pad2 = 0;
+	ocfs2_swap_quota_global_dqblk(ddquot);
+	err = write_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Remove dquot entry from its data block */
+static errcode_t ocfs2_remove_leaf_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+
+	if (blk != dquot->d_off / fs->fs_blocksize)
+		return OCFS2_ET_CORRUPT_QUOTA_FILE;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	dh->dqdh_entries--;
+	if (!dh->dqdh_entries) {	/* Block got free? */
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+		err = ocfs2_put_free_dqblk(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	else {
+		memset(buf + (dquot->d_off & (fs->fs_blocksize - 1)), 0,
+		       sizeof(struct ocfs2_global_disk_dqblk));
+
+		/* First free entry? */
+		if (dh->dqdh_entries ==
+		    ocfs2_global_dqstr_in_blk(fs->fs_blocksize) - 1) {
+			/* This will also write data block */
+			err = ocfs2_insert_free_dqentry(fs, type, buf, blk);
+		}
+		else
+			err = write_blk(fs, type, blk, buf);
+	}
+	dquot->d_off = 0;
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Remove reference to dquot from tree */
+static errcode_t ocfs2_remove_tree_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int *blk,
+					   int depth)
+{
+	errcode_t err;
+	char *buf;
+	unsigned int newblk;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, *blk, buf);
+	if (err)
+		goto bail;
+
+	ref = (u_int32_t *)buf;
+	newblk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+		 dquot->d_ddquot.dqb_id, depth)]);
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		err = ocfs2_remove_leaf_dqentry(fs, type, dquot, newblk);
+		newblk = 0;
+	}
+	else
+		err = ocfs2_remove_tree_dqentry(fs, type, dquot, &newblk,
+						depth + 1);
+	if (err)
+		goto bail;
+
+	if (!newblk) {
+		int i;
+
+		ref[ocfs2_qtree_index(fs->fs_blocksize,
+				      dquot->d_ddquot.dqb_id,
+				      depth)] = cpu_to_le32(0);
+		/* Block got empty? */
+		for (i = 0; i < fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		     !buf[i]; i++);
+		/* Don't put the root block into the free block list */
+		if (i == fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		    *blk != QT_TREEOFF) {
+			err = ocfs2_put_free_dqblk(fs, type, buf, *blk);
+			if (err)
+				goto bail;
+			*blk = 0;
+		}
+		else
+			err = write_blk(fs, type, *blk, buf);
+	}
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Delete dquot from tree */
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	if (!dquot->d_off)	/* Even not allocated? */
+		return 0;
+	return ocfs2_remove_tree_dqentry(fs, type, dquot, &tmp, 0);
+}
+
+/* Find entry in block */
+static errcode_t ocfs2_find_block_dqentry(ocfs2_filesys *fs, int type,
+					  ocfs2_cached_dquot *dquot,
+					  unsigned int blk)
+{
+	char *buf;
+	errcode_t err;
+	int i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+
+	for (i = 0; i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize);
+	     i++, ddquot++) {
+		if (le32_to_cpu(ddquot->dqb_id) == dquot->d_ddquot.dqb_id) {
+			if (dquot->d_ddquot.dqb_id == 0 &&
+			    ocfs2_qtree_entry_unused(ddquot))
+				continue;
+			break;
+		}
+	}
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	dquot->d_off = blk * fs->fs_blocksize + ((char *)ddquot - buf);
+	memcpy(&dquot->d_ddquot, ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ocfs2_swap_quota_global_dqblk(&dquot->d_ddquot);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Find entry for given id in the tree */
+static errcode_t ocfs2_find_tree_dqentry(ocfs2_filesys *fs,
+					 int type,
+					 ocfs2_cached_dquot *dquot,
+					 unsigned int blk,
+					 int depth)
+{
+	errcode_t err;
+	char *buf;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	ref = (u_int32_t *)buf;
+	blk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+	      dquot->d_ddquot.dqb_id, depth)]);
+	if (!blk)		/* No reference? */
+		goto bail;
+	if (depth < ocfs2_qtree_depth(fs->fs_blocksize) - 1)
+		err = ocfs2_find_tree_dqentry(fs, type, dquot, blk, depth + 1);
+	else
+		err = ocfs2_find_block_dqentry(fs, type, dquot, blk);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
Quoted 8 lines

libocfs2/quota.c line 1200

Quoted 6 lines
libocfs2/quota.c new file +6 -0
+
+/*
+ *  Read dquot from disk
+ */
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot)

One concern, the second argument 'id' never in function.

sorry for the type, it should be:
the second argument 'id' never used in function.

Quoted 35 lines

Another thing is, you malloc the space for &dquot, then pass it to caller's arguments, and we're noticing that the caller will be responsible for the relasing of allocated space instead of function itself, if it run without error. right?

unfortunately, i didn't see any caller(such as fill_sparse_files() in tunefs.ocfs2) finally release the space...

To keep consistent with other tool's code(such as ocfs2_read_inode()), i think you may allocate/free the space by caller?

Quoted 7 lines
libocfs2/quota.c new file +7 -0
+{
+	errcode_t err;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), &dquot);
+	if (err)
+		return err;

libocfs2/quota.c line 1217

Quoted 10 lines
libocfs2/quota.c new file +10 -0
+
+	err = ocfs2_find_tree_dqentry(fs, type, dquot, QT_TREEOFF, 0);
+	if (err)
+		goto bail;
+	*ret_dquot = dquot;
+	return 0;
+bail:
+	ocfs2_free(&dquot);
+	return err;
+}

_______________________________________________
Ocfs2-devel mailing list
Ocfs2-devel at oss.oracle.com
http://oss.oracle.com/mailman/listinfo/ocfs2-devel

author
#6 lore raw

PATCH 3/9 Implement quota functions to libocfs2

In reply to tristan.ye
Quoted 1,420 lines · tristan.ye
Quoted 1,419 lines · Jan Kara
---
 include/ocfs2/ocfs2.h     |   87 ++++
 libocfs2/Makefile         |    1 +
 libocfs2/feature_string.c |   18 +
 libocfs2/ocfs2_err.et     |    6 +
 libocfs2/quota.c          | 1217 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1329 insertions(+), 0 deletions(-)
 create mode 100644 libocfs2/quota.c
include/ocfs2/ocfs2.h +87 -0
index 47fede3..9f861d6 100644
@@ -50,6 +50,7 @@
 #include <ocfs2-kernel/kernel-list.h>
 #include <ocfs2-kernel/sparse_endian_types.h>
 #include <ocfs2-kernel/ocfs2_fs.h>
+#include <ocfs2-kernel/quota_tree.h>
 #include <o2dlm/o2dlm.h>
 #include <o2cb/o2cb.h>
 #include <ocfs2/ocfs2_err.h>
@@ -125,16 +126,36 @@
 #define OCFS2_CHB_WAITING	2
 #define OCFS2_CHB_COMPLETE	3
 
+/* Flags for global quotafile info */
+#define OCFS2_QF_INFO_DIRTY 1
+
 typedef void (*ocfs2_chb_notify)(int state, char *progress, void *data);
 
 typedef struct _ocfs2_filesys ocfs2_filesys;
 typedef struct _ocfs2_cached_inode ocfs2_cached_inode;
+typedef struct _ocfs2_cached_dquot ocfs2_cached_dquot;
 typedef struct _io_channel io_channel;
 typedef struct _ocfs2_inode_scan ocfs2_inode_scan;
 typedef struct _ocfs2_dir_scan ocfs2_dir_scan;
 typedef struct _ocfs2_bitmap ocfs2_bitmap;
 typedef struct _ocfs2_devices ocfs2_devices;
 
+#define MAXQUOTAS 2
+#define USRQUOTA 0
+#define GRPQUOTA 1
+
+#define OCFS2_DEF_BLOCK_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_INODE_GRACE 604800 /* 1 week */
+#define OCFS2_DEF_QUOTA_SYNC 10000   /* 10 seconds */
+
+struct _ocfs2_quota_info {
+	ocfs2_cached_inode *qi_inode;
+	int flags;
+	struct ocfs2_global_disk_dqinfo qi_info;
+};
+
+typedef struct _ocfs2_quota_info ocfs2_quota_info;
+
 struct _ocfs2_filesys {
 	char *fs_devname;
 	uint32_t fs_flags;
@@ -161,6 +182,8 @@ struct _ocfs2_filesys {
 	struct o2dlm_ctxt *fs_dlm_ctxt;
 	struct ocfs2_image_state *ost;
 
+	ocfs2_quota_info qinfo[MAXQUOTAS];
+
 	/* Reserved for the use of the calling application. */
 	void *fs_private;
 };
@@ -172,6 +195,15 @@ struct _ocfs2_cached_inode {
 	ocfs2_bitmap *ci_chains;
 };
 
+typedef unsigned int qid_t;
+
+struct _ocfs2_cached_dquot {
+	loff_t d_off;	/* Offset of structure in the file */
+	struct _ocfs2_cached_dquot *d_next;	/* Next entry in hashchain */
+	struct _ocfs2_cached_dquot **d_pprev;	/* Previous pointer in hashchain */
+	struct ocfs2_global_disk_dqblk d_ddquot;	/* Quota entry */
+};
+
 struct ocfs2_slot_data {
 	int		sd_valid;
 	unsigned int	sd_node_num;
@@ -205,6 +237,14 @@ struct _ocfs2_fs_options {
 	uint32_t opt_ro_compat;
 };
 
+struct _ocfs2_quota_hash {
+	int alloc_entries;
+	int used_entries;
+	ocfs2_cached_dquot **hash;
+};
+
+typedef struct _ocfs2_quota_hash ocfs2_quota_hash;
+
 errcode_t ocfs2_malloc(unsigned long size, void *ptr);
 errcode_t ocfs2_malloc0(unsigned long size, void *ptr);
 errcode_t ocfs2_free(void *ptr);
@@ -581,6 +621,53 @@ errcode_t ocfs2_meta_lock(ocfs2_filesys *fs, ocfs2_cached_inode *inode,
 
 errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs, ocfs2_cached_inode *ci);
 
+/* Quota operations */
+static inline int ocfs2_global_dqstr_in_blk(int blocksize)
+{
+	return (blocksize - OCFS2_QBLK_RESERVED_SPACE -
+		sizeof(struct qt_disk_dqdbheader)) /
+		sizeof(struct ocfs2_global_disk_dqblk);
+}
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header);
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info);
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk);
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info);
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk);
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader);
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno);
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type);
+int ocfs2_qtree_depth(int blocksize);
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot);
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type);
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot);
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp);
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash);
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot);
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp);
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash);
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data);
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash);
+
 /* Low level */
 void ocfs2_swap_slot_map(struct ocfs2_slot_map *sm, int num_slots);
 void ocfs2_swap_slot_map_extended(struct ocfs2_slot_map_extended *se,
libocfs2/Makefile +1 -0
index 48cfe80..eeb854a 100644
@@ -73,6 +73,7 @@ CFILES = 		\
 	lockid.c	\
 	backup_super.c	\
 	feature_string.c\
+	quota.c		\
 	image.c		\
 	xattr.c
 
libocfs2/feature_string.c +18 -0
index 17e2675..18ae6e9 100644
@@ -123,6 +123,16 @@ static struct fs_feature_flags ocfs2_supported_features[] = {
 		{0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		"usrquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		"grpquota",
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+		{0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		NULL,
 		{0, 0, 0},
 		{0, 0, 0}
@@ -190,6 +200,14 @@ static struct feature_name ocfs2_feature_names[] = {
 		.fn_flag = {0, OCFS2_FEATURE_INCOMPAT_XATTR, 0},
 	},
 	{
+		.fn_name = "usrquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_USRQUOTA},
+	},
+	{
+		.fn_name = "grpquota",
+		.fn_flag = {0, 0, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA},
+	},
+	{
 		.fn_name = NULL,
 	},
 };
libocfs2/ocfs2_err.et +6 -0
index ddfa07c..13b03c4 100644
@@ -183,7 +183,13 @@ ec	OCFS2_ET_BAD_XATTR_BLOCK_MAGIC,
 ec	OCFS2_ET_UNKNOWN_FEATURE,
 	"Unknown feature"
 
+ec	OCFS2_ET_CORRUPT_QUOTA_FILE,
+	"Quota file is corrupted"
+
 ec	OCFS2_ET_CANNOT_DETERMINE_SECTOR_SIZE,
 	"Cannot determine sector size"
 
+ec	OCFS2_ET_NONEMTY_QUOTA_HASH,
+	"Freeing non-empty quota hash"
+
 	end
libocfs2/quota.c new file +1194 -0
new file mode 100644
index 0000000..85ba576
@@ -0,0 +1,1217 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * quota.c
+ *
+ * Quota operations for the OCFS2 userspace library.
+ *
+ * Copyright (C) 2008 Novell.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <inttypes.h>
+
+#include "ocfs2/byteorder.h"
+#include "ocfs2/ocfs2.h"
+
+void ocfs2_swap_quota_header(struct ocfs2_disk_dqheader *header)
+{
+	if (cpu_is_little_endian)
+		return;
+	header->dqh_magic = bswap_32(header->dqh_magic);
+	header->dqh_version = bswap_32(header->dqh_version);
+}
+
+void ocfs2_swap_quota_local_info(struct ocfs2_local_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_flags = bswap_32(info->dqi_flags);
+	info->dqi_chunks = bswap_32(info->dqi_chunks);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+}
+
+void ocfs2_swap_quota_chunk_header(struct ocfs2_local_disk_chunk *chunk)
+{
+	if (cpu_is_little_endian)
+		return;
+	chunk->dqc_free = bswap_32(chunk->dqc_free);
+}
+
+void ocfs2_swap_quota_global_info(struct ocfs2_global_disk_dqinfo *info)
+{
+	if (cpu_is_little_endian)
+		return;
+	info->dqi_bgrace = bswap_32(info->dqi_bgrace);
+	info->dqi_igrace = bswap_32(info->dqi_igrace);
+	info->dqi_syncms = bswap_32(info->dqi_syncms);
+	info->dqi_blocks = bswap_32(info->dqi_blocks);
+	info->dqi_free_blk = bswap_32(info->dqi_free_blk);
+	info->dqi_free_entry = bswap_32(info->dqi_free_entry);
+}
+
+void ocfs2_swap_quota_global_dqblk(struct ocfs2_global_disk_dqblk *dqblk)
+{
+	if (cpu_is_little_endian)
+		return;
+	dqblk->dqb_id = bswap_32(dqblk->dqb_id);
+	dqblk->dqb_use_count = bswap_32(dqblk->dqb_use_count);
+	dqblk->dqb_ihardlimit = bswap_64(dqblk->dqb_ihardlimit);
+	dqblk->dqb_isoftlimit = bswap_64(dqblk->dqb_isoftlimit);
+	dqblk->dqb_curinodes = bswap_64(dqblk->dqb_curinodes);
+	dqblk->dqb_bhardlimit = bswap_64(dqblk->dqb_bhardlimit);
+	dqblk->dqb_bsoftlimit = bswap_64(dqblk->dqb_bsoftlimit);
+	dqblk->dqb_curspace = bswap_64(dqblk->dqb_curspace);
+	dqblk->dqb_btime = bswap_64(dqblk->dqb_btime);
+	dqblk->dqb_itime = bswap_64(dqblk->dqb_itime);
+}
+
+void ocfs2_swap_quota_leaf_block_header(struct qt_disk_dqdbheader *bheader)
+{
+	if (cpu_is_little_endian)
+		return;
+	bheader->dqdh_next_free = bswap_32(bheader->dqdh_next_free);
+	bheader->dqdh_prev_free = bswap_32(bheader->dqdh_prev_free);
+	bheader->dqdh_entries = bswap_16(bheader->dqdh_entries);
+}
+
+/* Should be power of two */
+#define DEFAULT_QUOTA_HASH_SIZE 8192
+/* Maxinum number of hash buckets - use at most 16 MB on a 64-bit arch */
+#define MAX_QUOTA_HASH_SIZE (1<<21)
+
+errcode_t ocfs2_new_quota_hash(ocfs2_quota_hash **hashp)
+{
+	ocfs2_quota_hash *hash;
+	errcode_t err;
+
+	err = ocfs2_malloc(sizeof(ocfs2_quota_hash), &hash);
+	if (err)
+		return err;
+	hash->alloc_entries = DEFAULT_QUOTA_HASH_SIZE;
+	hash->used_entries = 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+			    DEFAULT_QUOTA_HASH_SIZE, &hash->hash);
+	if (err) {
+		ocfs2_free(&hash);
+		return err;
+	}
+	*hashp = hash;
+	return 0;
+}
+
+errcode_t ocfs2_free_quota_hash(ocfs2_quota_hash *hash)
+{
+	errcode_t err = 0, ret;
+
+	if (hash->used_entries)
+		return OCFS2_ET_NONEMTY_QUOTA_HASH;
+	ret = ocfs2_free(&hash->hash);
+	if (!err && ret)
+		err = ret;
+	ret = ocfs2_free(&hash);
+	if (!err && ret)
+		err = ret;
+	return err;
+}
+
+static int quota_hash(ocfs2_quota_hash *hash, qid_t id)
+{
+	return (((unsigned long)id) * 5) & (hash->alloc_entries - 1);
+}
+
+static void quota_add_hash_chain(ocfs2_quota_hash *hash,
+				 ocfs2_cached_dquot *dquot)
+{
+	int hash_val = quota_hash(hash, dquot->d_ddquot.dqb_id);
+
+	dquot->d_next = hash->hash[hash_val];
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = &dquot->d_next;
+	hash->hash[hash_val] = dquot;
+	dquot->d_pprev = hash->hash + hash_val;
+}
+
+errcode_t ocfs2_insert_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+
+	if (hash->used_entries > hash->alloc_entries &&
+	    hash->alloc_entries * 2 < MAX_QUOTA_HASH_SIZE) {
+		ocfs2_cached_dquot **new_hash, **old_hash;
+		ocfs2_cached_dquot *h_dquot, *h_next;
+		int i;
+		int old_entries;
+
+		err = ocfs2_malloc0(sizeof(ocfs2_quota_hash *) *
+				    hash->alloc_entries * 2, &new_hash);
+		if (err)
+			return err;
+		old_entries = hash->alloc_entries;
+		old_hash = hash->hash;
+		hash->alloc_entries *= 2;
+		hash->hash = new_hash;
+		/* Rehash */
+		for (i = 0; i < old_entries; i++) {
+			for (h_dquot = old_hash[i]; h_dquot; h_dquot = h_next) {
+				h_next = h_dquot->d_next;
+				quota_add_hash_chain(hash, h_dquot);
+			}
+		}
+		err = ocfs2_free(&old_hash);
+		if (err)
+			return err;
+	}
+	quota_add_hash_chain(hash, dquot);
+	hash->used_entries++;
+	return 0;
+}
+
+errcode_t ocfs2_remove_quota_hash(ocfs2_quota_hash *hash,
+				  ocfs2_cached_dquot *dquot)
+{
+	*(dquot->d_pprev) = dquot->d_next;
+	if (dquot->d_next)
+		dquot->d_next->d_pprev = dquot->d_pprev;
+	hash->used_entries--;
+	return 0;
+}
+
+errcode_t ocfs2_find_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				ocfs2_cached_dquot **dquotp)
+{
+	int hash_val = quota_hash(hash, id);
+	ocfs2_cached_dquot *dquot;
+
+	for (dquot = hash->hash[hash_val]; dquot; dquot = dquot->d_next) {
+		if (dquot->d_ddquot.dqb_id == id) {
+			*dquotp = dquot;
+			return 0;
+		}
+	}
+	*dquotp = NULL;
+	return 0;
+}
+
+errcode_t ocfs2_find_create_quota_hash(ocfs2_quota_hash *hash, qid_t id,
+				       ocfs2_cached_dquot **dquotp)
+{
+	errcode_t err;
+
+	err = ocfs2_find_quota_hash(hash, id, dquotp);
+	if (err)
+		return err;
+	if (*dquotp)
+		return 0;
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), dquotp);
+	if (err)
+		return err;
+	(*dquotp)->d_ddquot.dqb_id = id;
+	err = ocfs2_insert_quota_hash(hash, *dquotp);
+	if (err) {
+		ocfs2_free(dquotp);
+		return err;
+	}
+	return 0;
+}
+
+errcode_t ocfs2_compute_quota_usage(ocfs2_filesys *fs,
+				    ocfs2_quota_hash *usr_hash,
+				    ocfs2_quota_hash *grp_hash)
+{
+	errcode_t err = 0;
+	ocfs2_inode_scan *scan;
+	uint64_t blkno;
+	char *buf;
+	int close_scan = 0;
+	struct ocfs2_dinode *di;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	di = (struct ocfs2_dinode *)buf;
+
+	err = ocfs2_open_inode_scan(fs, &scan);
+	if (err)
+		goto out;
+	close_scan = 1;
+
+	while (1) {
+		err = ocfs2_get_next_inode(scan, &blkno, buf);
+		if (err || !blkno)
+			break;
+		/*
+		 * Check whether the inode looks reasonable and interesting
+		 * for quota
+		 */
+		if (memcmp(di->i_signature, OCFS2_INODE_SIGNATURE,
+			   strlen(OCFS2_INODE_SIGNATURE)))
+			continue;
+		ocfs2_swap_inode_to_cpu(di, fs->fs_blocksize);
+		if (di->i_fs_generation != fs->fs_super->i_fs_generation)
+			continue;
+		if (!(di->i_flags & OCFS2_VALID_FL))
+			continue;
+		if (di->i_flags & OCFS2_SYSTEM_FL &&
+		    blkno != OCFS2_RAW_SB(fs->fs_super)->s_root_blkno)
+			continue;
+		if (usr_hash) {
+			err = ocfs2_find_create_quota_hash(usr_hash, di->i_uid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+		if (grp_hash) {
+			err = ocfs2_find_create_quota_hash(grp_hash, di->i_gid,
+							   &dquot);
+			if (err)
+				break;
+			dquot->d_ddquot.dqb_curspace +=
+				ocfs2_clusters_to_bytes(fs, di->i_clusters);
+			dquot->d_ddquot.dqb_curinodes++;
+		}
+	}
+out:
+	if (close_scan)
+		ocfs2_close_inode_scan(scan);
+	ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_iterate_quota_hash(ocfs2_quota_hash *hash,
+				   errcode_t (*f)(ocfs2_cached_dquot *, void *),
+				   void *data)
+{
+	errcode_t err = 0;
+	int i;
+	ocfs2_cached_dquot *dquot, *next;
+
+	for (i = 0; i < hash->alloc_entries; i++)
+		for (dquot = hash->hash[i]; dquot; dquot = next) {
+			next = dquot->d_next;
+			err = f(dquot, data);
+			if (err)
+				goto out;
+		}
+out:
+	return err;
+}
+
+struct write_rel_ctx {
+	ocfs2_filesys *fs;
+	ocfs2_quota_hash *hash;
+	int type;
+};
+
+static errcode_t write_release_quota_hash(ocfs2_cached_dquot *dquot, void *p)
+{
+	struct write_rel_ctx *ctx = p;
+	errcode_t err;
+
+	if (!dquot->d_ddquot.dqb_isoftlimit ||
+	    dquot->d_ddquot.dqb_curinodes < dquot->d_ddquot.dqb_isoftlimit)
+		dquot->d_ddquot.dqb_itime = 0;
+	if (!dquot->d_ddquot.dqb_bsoftlimit ||
+	    dquot->d_ddquot.dqb_curspace < dquot->d_ddquot.dqb_bsoftlimit)
+		dquot->d_ddquot.dqb_btime = 0;
+
+	err = ocfs2_write_dquot(ctx->fs, ctx->type, dquot);
+	if (err)
+		return err;
+	err = ocfs2_remove_quota_hash(ctx->hash, dquot);
+	if (err)
+		return err;
+	return ocfs2_free(&dquot);
+}
+
+errcode_t ocfs2_write_release_dquots(ocfs2_filesys *fs, int type,
+				     ocfs2_quota_hash *hash)
+{
+	struct write_rel_ctx ctx;
+
+	ctx.fs = fs;
+	ctx.hash = hash;
+	ctx.type = type;
+
+	return ocfs2_iterate_quota_hash(hash, write_release_quota_hash, &ctx);
+}
+
+static void mark_quotafile_info_dirty(ocfs2_filesys *fs, int type)
+{
+	fs->qinfo[type].flags |= OCFS2_QF_INFO_DIRTY;
+}
+
+static void ocfs2_checksum_quota_block(ocfs2_filesys *fs, char *buf)
+{
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	ocfs2_compute_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+#define OCFS2_LOCAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_local_quota_file(ocfs2_filesys *fs, int type,
+				      uint64_t blkno)
+{
+	ocfs2_cached_inode *ci = NULL;
+	struct ocfs2_dinode *di;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_local_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_LOCAL_QMAGICS;
+	int versions[] = OCFS2_LOCAL_QVERSIONS;
+	char *buf = NULL;
+	unsigned int written;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_LOCAL_QF_INIT_BLOCKS);
+	errcode_t err;
+
+	err = ocfs2_read_cached_inode(fs, blkno, &ci);
+	if (err)
+		goto out;
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	di = ci->ci_inode;
+
+	/* We need at least two blocks */
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_LOCAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+	err = ocfs2_write_inode(fs, blkno, (char *)di);
+	if (err)
+		goto out;
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_LOCAL_QF_INIT_BLOCKS, &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_local_disk_dqinfo *)(buf + OCFS2_LOCAL_INFO_OFF);
+	info->dqi_chunks = 1;
+	info->dqi_blocks = OCFS2_LOCAL_QF_INIT_BLOCKS;
+	info->dqi_flags = OLQF_CLEAN;
+	ocfs2_swap_quota_local_info(info);
+
+	/* There are no free chunks because there are no blocks allocated for
+	 * them yet. So chunk header is all-zero and needs no initialization */
+	ocfs2_checksum_quota_block(fs, buf);
+	ocfs2_checksum_quota_block(fs, buf + fs->fs_blocksize);
+	err = ocfs2_file_write(ci, buf, bytes, 0, &written);
+	if (!err && written != bytes) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+out:
+	if (ci)
+		ocfs2_free_cached_inode(fs, ci);
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+errcode_t ocfs2_init_local_quota_files(ocfs2_filesys *fs, int type)
+{
+	int num_slots = OCFS2_RAW_SB(fs->fs_super)->s_max_slots;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+	uint64_t blkno;
+	int local_type = (type == USRQUOTA) ? LOCAL_USER_QUOTA_SYSTEM_INODE :
+					      LOCAL_GROUP_QUOTA_SYSTEM_INODE;
+	int i;
+
+	for (i = 0; i < num_slots; i++) {
+		ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+						local_type, i);
+		ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname,
+				   strlen(fname), NULL, &blkno);
+		if (ret)
+			return ret;
+		/* This is here mainly for fsck... */
+		ret = ocfs2_truncate(fs, blkno, 0);
+		if (ret)
+			return ret;
+		ret = ocfs2_init_local_quota_file(fs, type, blkno);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+/* Return depth of quota tree in global file */
+int ocfs2_qtree_depth(int blocksize)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+	unsigned long long entries = epb;
+	int i;
+
+	for (i = 1; entries < (1ULL << 32); i++)
+		entries *= epb;
+	return i;
+}
+
+/* Returns index of next block in the tree of dquots */
+static int ocfs2_qtree_index(int blocksize, qid_t id, int depth)
+{
+	unsigned int epb = (blocksize - OCFS2_QBLK_RESERVED_SPACE) >> 2;
+
+	depth = ocfs2_qtree_depth(blocksize) - depth - 1;
+	while (depth--)
+		id /= epb;
+	return id % epb;
+}
+
+/* Is given leaf entry unused? */
+int ocfs2_qtree_entry_unused(struct ocfs2_global_disk_dqblk *ddquot)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddquot, sizeof(empty));
+}
+
+errcode_t ocfs2_init_fs_quota_info(ocfs2_filesys *fs, int type)
+{
+	int global_type = (type == USRQUOTA) ?
+				USER_QUOTA_SYSTEM_INODE :
+				GROUP_QUOTA_SYSTEM_INODE;
+	uint64_t blkno;
+	char fname[OCFS2_MAX_FILENAME_LEN];
+	errcode_t ret;
+
+	ocfs2_sprintf_system_inode_name(fname, sizeof(fname),
+		global_type, 0);
+	ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, fname, strlen(fname),
+			   NULL, &blkno);
+	if (ret)
+		return ret;
+	ret = ocfs2_read_cached_inode(fs, blkno, &(fs->qinfo[type].qi_inode));
+	if (ret)
+		return ret;
+	return 0;
+}
+
+/* Read given block */
+static errcode_t read_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			  char *buf)
+{
+	errcode_t err;
+	uint32_t got;
+	struct ocfs2_disk_dqtrailer *dqt =
+			ocfs2_block_dqtrailer(fs->fs_blocksize, buf);
+
+	err = ocfs2_file_read(fs->qinfo[type].qi_inode, buf,
+			      fs->fs_blocksize, blk * fs->fs_blocksize, &got);
+	if (err)
+		return err;
+	if (got != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_READ;
+
+	return ocfs2_validate_meta_ecc(fs, buf, &dqt->dq_check);
+}
+
+/* Write given block */
+static errcode_t write_blk(ocfs2_filesys *fs, int type, unsigned int blk,
+			   char *buf)
+{
+	errcode_t err;
+	uint32_t written;
+
+	ocfs2_checksum_quota_block(fs, buf);
+
+	err = ocfs2_file_write(fs->qinfo[type].qi_inode, buf, fs->fs_blocksize,
+			       blk * fs->fs_blocksize, &written);
+	if (err)
+		return err;
+	if (written != fs->fs_blocksize)
+		return OCFS2_ET_SHORT_WRITE;
+	return 0;
+}
+
+errcode_t ocfs2_read_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	char *buf;
+	errcode_t ret;
+	struct ocfs2_global_disk_dqinfo *info;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+
+	ret = read_blk(fs, type, 0, buf);
+	if (ret)
+		return ret;
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	ocfs2_swap_quota_global_info(info);
+	memcpy(&(fs->qinfo[type].qi_info), info,
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	return 0;
+}
+
+errcode_t ocfs2_write_global_quota_info(ocfs2_filesys *fs, int type)
+{
+	errcode_t ret;
+	char *buf;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	memcpy(info, &(fs->qinfo[type].qi_info),
+	       sizeof(struct ocfs2_global_disk_dqinfo));
+	ocfs2_swap_quota_global_info(info);
+	ret = write_blk(fs, type, 0, buf);
+	if (ret)
+		goto bail;
+bail:
+	ocfs2_free(&buf);
+	return ret;
+}
+
+#define OCFS2_GLOBAL_QF_INIT_BLOCKS 2
+
+errcode_t ocfs2_init_global_quota_file(ocfs2_filesys *fs, int type)
+{
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+	struct ocfs2_dinode *di;
+	char *buf = NULL;
+	struct ocfs2_disk_dqheader *header;
+	struct ocfs2_global_disk_dqinfo *info;
+	unsigned int magics[] = OCFS2_GLOBAL_QMAGICS;
+	int versions[] = OCFS2_GLOBAL_QVERSIONS;
+	errcode_t err;
+	int i;
+	int bytes = ocfs2_blocks_to_bytes(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS);
+
+	if (!(ci->ci_inode->i_flags & OCFS2_VALID_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_SYSTEM_FL) ||
+	    !(ci->ci_inode->i_flags & OCFS2_QUOTA_FL)) {
+		err = OCFS2_ET_INTERNAL_FAILURE;
+		goto out;
+	}
+	err = ocfs2_cached_inode_extend_allocation(ci,
+		ocfs2_clusters_in_blocks(fs, OCFS2_GLOBAL_QF_INIT_BLOCKS));
+	if (err)
+		goto out;
+
+	/* Mark info dirty so that quota inode gets written */
+	mark_quotafile_info_dirty(fs, type);
+
+	di = ci->ci_inode;
+	di->i_size = bytes;
+	di->i_mtime = time(NULL);
+
+	err = ocfs2_malloc_blocks(fs->fs_io, OCFS2_GLOBAL_QF_INIT_BLOCKS,
+				  &buf);
+	if (err)
+		goto out;
+	memset(buf, 0, bytes);
+
+	header = (struct ocfs2_disk_dqheader *)buf;
+	header->dqh_magic = magics[type];
+	header->dqh_version = versions[type];
+	ocfs2_swap_quota_header(header);
+
+	fs->qinfo[type].qi_info.dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	fs->qinfo[type].qi_info.dqi_free_blk = 0;
+	fs->qinfo[type].qi_info.dqi_free_entry = 0;
+
+	info = (struct ocfs2_global_disk_dqinfo *)(buf + OCFS2_GLOBAL_INFO_OFF);
+	info->dqi_bgrace = fs->qinfo[type].qi_info.dqi_bgrace;
+	info->dqi_igrace = fs->qinfo[type].qi_info.dqi_igrace;
+	info->dqi_syncms = fs->qinfo[type].qi_info.dqi_syncms;
+	info->dqi_blocks = OCFS2_GLOBAL_QF_INIT_BLOCKS;
+	info->dqi_free_blk = 0;
+	info->dqi_free_entry = 0;
+	ocfs2_swap_quota_global_info(info);
+
+	/*
+	 * Write the buffer here so that all the headers are properly written.
+	 * Normally we don't write tree root block.
+	 */
+	for (i = 0; i < OCFS2_GLOBAL_QF_INIT_BLOCKS; i++) {
+		err = write_blk(fs, type, i, buf + (i * fs->fs_blocksize));
+		if (err)
+			goto out;
+	}
+out:
+	if (buf)
+		ocfs2_free(&buf);
+	return err;
+}
+
+/* Is given dquot empty? */
+static int ocfs2_global_entry_unused(struct ocfs2_global_disk_dqblk *ddqblk)
+{
+	static struct ocfs2_global_disk_dqblk empty;
+
+	return !memcmp(&empty, ddqblk, sizeof(empty));
+}
+
+/* Get free block in file (either from free list or create new one) */
+static errcode_t ocfs2_get_free_dqblk(ocfs2_filesys *fs, int type,
+				      unsigned int *blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	ocfs2_cached_inode *ci = fs->qinfo[type].qi_inode;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	if (info->dqi_free_blk) {
+		*blk = info->dqi_free_blk;
+		err = read_blk(fs, type, *blk, buf);
+		if (err)
+			goto bail;
+		info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
+	}
+	else {
+		if (info->dqi_blocks ==
+		    ocfs2_clusters_to_blocks(fs, ci->ci_inode->i_clusters)) {
+			err = ocfs2_cached_inode_extend_allocation(ci, 1);
+			if (err)
+				goto bail;
+		}
+		*blk = info->dqi_blocks++;
+		ci->ci_inode->i_size =
+				ocfs2_blocks_to_bytes(fs, info->dqi_blocks);
+	}
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Put given block to free list */
+static errcode_t ocfs2_put_free_dqblk(ocfs2_filesys *fs, int type,
+				      char *buf, unsigned int blk)
+{
+	errcode_t err;
+	struct qt_disk_dqdbheader *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	dh->dqdh_next_free = info->dqi_free_blk;
+	dh->dqdh_prev_free = 0;
+	dh->dqdh_entries = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		return err;
+	info->dqi_free_blk = blk;
+	mark_quotafile_info_dirty(fs, type);
+	return 0;
+}
+
+/* Remove given block from the list of blocks with free entries */
+static errcode_t ocfs2_remove_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *dh, *tdh;
+	unsigned int nextblk, prevblk;
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+	nextblk = dh->dqdh_next_free;
+	prevblk = dh->dqdh_prev_free;
+
+	if (nextblk) {
+		err = read_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = prevblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, nextblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	if (prevblk) {
+		/* Failure here is bad since we potentially corrupt free list.
+		 * OTOH something must be really wrong when read/write fails */
+		err = read_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_next_free = nextblk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, prevblk, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	else {
+		fs->qinfo[type].qi_info.dqi_free_entry = nextblk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	dh->dqdh_next_free = dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	/* No matter whether write succeeds block is out of list */
+	write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Insert given block to the beginning of list with free entries */
+static errcode_t ocfs2_insert_free_dqentry(ocfs2_filesys *fs, int type,
+					   char *buf, unsigned int blk)
+{
+	errcode_t err;
+	char *tmpbuf;
+	struct qt_disk_dqdbheader *tdh, *dh = (struct qt_disk_dqdbheader *)buf;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+
+	err = ocfs2_malloc_block(fs->fs_io, &tmpbuf);
+	if (err)
+		return err;
+	dh->dqdh_next_free = info->dqi_free_entry;
+	dh->dqdh_prev_free = 0;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	ocfs2_swap_quota_leaf_block_header(dh);
+	if (err)
+		goto bail;
+
+	if (info->dqi_free_entry) {
+		tdh = (struct qt_disk_dqdbheader *)tmpbuf;
+		err = read_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		tdh->dqdh_prev_free = blk;
+		ocfs2_swap_quota_leaf_block_header(tdh);
+		err = write_blk(fs, type, info->dqi_free_entry, tmpbuf);
+		if (err)
+			goto bail;
+	}
+	info->dqi_free_entry = blk;
+	mark_quotafile_info_dirty(fs, type);
+bail:
+	ocfs2_free(&tmpbuf);
+	return err;
+}
+
+/* Find space for dquot */
+static errcode_t ocfs2_find_free_dqentry(ocfs2_filesys *fs, int type,
+					 unsigned int *treeblk, loff_t *off)
+{
+	errcode_t err;
+	unsigned int blk, i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+	struct qt_disk_dqdbheader *dh;
+	struct ocfs2_global_disk_dqinfo *info = &(fs->qinfo[type].qi_info);
+	char *buf;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+	if (info->dqi_free_entry) {
+		blk = info->dqi_free_entry;
+		err = read_blk(fs, type, blk, buf);
+		if (err)
+			goto bail;
+		ocfs2_swap_quota_leaf_block_header(dh);
+	}
+	else {
+		err = ocfs2_get_free_dqblk(fs, type, &blk);
+		if (err)
+			goto bail;
+		memset(buf, 0, fs->fs_blocksize);
+		info->dqi_free_entry = blk;
+		mark_quotafile_info_dirty(fs, type);
+	}
+	/* Block will be full? */
+	if (dh->dqdh_entries + 1 >=
+	    ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	dh->dqdh_entries++;
+	/* Find free structure in block */
+	for (i = 0;
+	     i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize) &&
+	     !ocfs2_global_entry_unused(ddquot + i);
+	     i++);
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	ocfs2_swap_quota_leaf_block_header(dh);
+	err = write_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	*off = (blk * fs->fs_blocksize) + sizeof(struct qt_disk_dqdbheader) +
+	       i * sizeof(struct ocfs2_global_disk_dqblk);
+	*treeblk = blk;
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Insert reference to structure into the trie */
+static errcode_t ocfs2_do_insert_tree(ocfs2_filesys *fs, int type, qid_t id,
+				      unsigned int *treeblk, int depth,
+				      loff_t *off)
+{
+	char *buf;
+	int newson = 0, newact = 0;
+	u_int32_t *ref;
+	unsigned int newblk;
+	errcode_t err;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+	if (!*treeblk) {
+		err = ocfs2_get_free_dqblk(fs, type, &newblk);
+		if (err)
+			goto bail;
+		*treeblk = newblk;
+		memset(buf, 0, fs->fs_blocksize);
+		newact = 1;
+	}
+	else {
+		err = read_blk(fs, type, *treeblk, buf);
+		if (err)
+			goto bail;
+	}
+	ref = (u_int32_t *) buf;
+	newblk = le32_to_cpu(ref[
+		 ocfs2_qtree_index(fs->fs_blocksize, id, depth)]);
+	if (!newblk)
+		newson = 1;
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		if (newblk) {
+			err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+			goto bail;
+		}
+		err = ocfs2_find_free_dqentry(fs, type, &newblk, off);
+	}
+	else
+		err = ocfs2_do_insert_tree(fs, type, id, &newblk, depth + 1,
+					   off);
+	if (newson && !err) {
+		ref[ocfs2_qtree_index(fs->fs_blocksize, id, depth)] =
+							cpu_to_le32(newblk);
+		err = write_blk(fs, type, *treeblk, buf);
+	}
+	else if (newact && err)
+		ocfs2_put_free_dqblk(fs, type, buf, *treeblk);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Wrapper for inserting quota structure into tree */
+static errcode_t ocfs2_insert_qtree(ocfs2_filesys *fs, int type, qid_t id,
+				    loff_t *off)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	return ocfs2_do_insert_tree(fs, type, id, &tmp, 0, off);
+}
+
+/* Write dquot to file */
+errcode_t ocfs2_write_dquot(ocfs2_filesys *fs, int type,
+			    ocfs2_cached_dquot *dquot)
+{
+	errcode_t err;
+	char *buf;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	if (!dquot->d_off) {
+		err = ocfs2_insert_qtree(fs, type, dquot->d_ddquot.dqb_id,
+					 &dquot->d_off);
+		if (err)
+			goto bail;
+	}
+	err = read_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+	if (err)
+		goto bail;
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+					(dquot->d_off % fs->fs_blocksize));
+	memcpy(ddquot, &dquot->d_ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ddquot->dqb_pad1 = 0;
+	ddquot->dqb_pad2 = 0;
+	ocfs2_swap_quota_global_dqblk(ddquot);
+	err = write_blk(fs, type, dquot->d_off / fs->fs_blocksize, buf);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Remove dquot entry from its data block */
+static errcode_t ocfs2_remove_leaf_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int blk)
+{
+	errcode_t err;
+	char *buf;
+	struct qt_disk_dqdbheader *dh;
+
+	if (blk != dquot->d_off / fs->fs_blocksize)
+		return OCFS2_ET_CORRUPT_QUOTA_FILE;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	dh = (struct qt_disk_dqdbheader *)buf;
+	ocfs2_swap_quota_leaf_block_header(dh);
+	dh->dqdh_entries--;
+	if (!dh->dqdh_entries) {	/* Block got free? */
+		err = ocfs2_remove_free_dqentry(fs, type, buf, blk);
+		if (err)
+			goto bail;
+		err = ocfs2_put_free_dqblk(fs, type, buf, blk);
+		if (err)
+			goto bail;
+	}
+	else {
+		memset(buf + (dquot->d_off & (fs->fs_blocksize - 1)), 0,
+		       sizeof(struct ocfs2_global_disk_dqblk));
+
+		/* First free entry? */
+		if (dh->dqdh_entries ==
+		    ocfs2_global_dqstr_in_blk(fs->fs_blocksize) - 1) {
+			/* This will also write data block */
+			err = ocfs2_insert_free_dqentry(fs, type, buf, blk);
+		}
+		else
+			err = write_blk(fs, type, blk, buf);
+	}
+	dquot->d_off = 0;
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Remove reference to dquot from tree */
+static errcode_t ocfs2_remove_tree_dqentry(ocfs2_filesys *fs,
+					   int type,
+					   ocfs2_cached_dquot *dquot,
+					   unsigned int *blk,
+					   int depth)
+{
+	errcode_t err;
+	char *buf;
+	unsigned int newblk;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, *blk, buf);
+	if (err)
+		goto bail;
+
+	ref = (u_int32_t *)buf;
+	newblk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+		 dquot->d_ddquot.dqb_id, depth)]);
+	if (depth == ocfs2_qtree_depth(fs->fs_blocksize) - 1) {
+		err = ocfs2_remove_leaf_dqentry(fs, type, dquot, newblk);
+		newblk = 0;
+	}
+	else
+		err = ocfs2_remove_tree_dqentry(fs, type, dquot, &newblk,
+						depth + 1);
+	if (err)
+		goto bail;
+
+	if (!newblk) {
+		int i;
+
+		ref[ocfs2_qtree_index(fs->fs_blocksize,
+				      dquot->d_ddquot.dqb_id,
+				      depth)] = cpu_to_le32(0);
+		/* Block got empty? */
+		for (i = 0; i < fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		     !buf[i]; i++);
+		/* Don't put the root block into the free block list */
+		if (i == fs->fs_blocksize - OCFS2_QBLK_RESERVED_SPACE &&
+		    *blk != QT_TREEOFF) {
+			err = ocfs2_put_free_dqblk(fs, type, buf, *blk);
+			if (err)
+				goto bail;
+			*blk = 0;
+		}
+		else
+			err = write_blk(fs, type, *blk, buf);
+	}
+bail:
+	ocfs2_free(&buf);
+
+	return err;
+}
+
+/* Delete dquot from tree */
+errcode_t ocfs2_delete_dquot(ocfs2_filesys *fs, int type,
+			     ocfs2_cached_dquot *dquot)
+{
+	unsigned int tmp = QT_TREEOFF;
+
+	if (!dquot->d_off)	/* Even not allocated? */
+		return 0;
+	return ocfs2_remove_tree_dqentry(fs, type, dquot, &tmp, 0);
+}
+
+/* Find entry in block */
+static errcode_t ocfs2_find_block_dqentry(ocfs2_filesys *fs, int type,
+					  ocfs2_cached_dquot *dquot,
+					  unsigned int blk)
+{
+	char *buf;
+	errcode_t err;
+	int i;
+	struct ocfs2_global_disk_dqblk *ddquot;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+
+	ddquot = (struct ocfs2_global_disk_dqblk *)(buf +
+		 sizeof(struct qt_disk_dqdbheader));
+
+	for (i = 0; i < ocfs2_global_dqstr_in_blk(fs->fs_blocksize);
+	     i++, ddquot++) {
+		if (le32_to_cpu(ddquot->dqb_id) == dquot->d_ddquot.dqb_id) {
+			if (dquot->d_ddquot.dqb_id == 0 &&
+			    ocfs2_qtree_entry_unused(ddquot))
+				continue;
+			break;
+		}
+	}
+	if (i == ocfs2_global_dqstr_in_blk(fs->fs_blocksize)) {
+		err = OCFS2_ET_CORRUPT_QUOTA_FILE;
+		goto bail;
+	}
+	dquot->d_off = blk * fs->fs_blocksize + ((char *)ddquot - buf);
+	memcpy(&dquot->d_ddquot, ddquot,
+	       sizeof(struct ocfs2_global_disk_dqblk));
+	ocfs2_swap_quota_global_dqblk(&dquot->d_ddquot);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
+
+/* Find entry for given id in the tree */
+static errcode_t ocfs2_find_tree_dqentry(ocfs2_filesys *fs,
+					 int type,
+					 ocfs2_cached_dquot *dquot,
+					 unsigned int blk,
+					 int depth)
+{
+	errcode_t err;
+	char *buf;
+	u_int32_t *ref;
+
+	err = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (err)
+		return err;
+
+	err = read_blk(fs, type, blk, buf);
+	if (err)
+		goto bail;
+	ref = (u_int32_t *)buf;
+	blk = le32_to_cpu(ref[ocfs2_qtree_index(fs->fs_blocksize,
+	      dquot->d_ddquot.dqb_id, depth)]);
+	if (!blk)		/* No reference? */
+		goto bail;
+	if (depth < ocfs2_qtree_depth(fs->fs_blocksize) - 1)
+		err = ocfs2_find_tree_dqentry(fs, type, dquot, blk, depth + 1);
+	else
+		err = ocfs2_find_block_dqentry(fs, type, dquot, blk);
+bail:
+	ocfs2_free(&buf);
+	return err;
+}
Quoted 8 lines

libocfs2/quota.c line 1200

Quoted 6 lines
libocfs2/quota.c new file +6 -0
+
+/*
+ *  Read dquot from disk
+ */
+errcode_t ocfs2_read_dquot(ocfs2_filesys *fs, int type, qid_t id,
+			   ocfs2_cached_dquot **ret_dquot)

One concern, the second argument 'id' never in function.

Oh, good catch. The function works correctly only for the root user.
Because it is used only when disabling sparse feature and I've tested that as root, I didn't notice.

Quoted 7 lines

Another thing is, you malloc the space for &dquot, then pass it to caller's arguments, and we're noticing that the caller will be responsible for the relasing of allocated space instead of function itself, if it run without error. right?

unfortunately, i didn't see any caller(such as fill_sparse_files() in tunefs.ocfs2) finally release the space...

And this is true as well. Thanks.

Quoted 2 lines

To keep consistent with other tool's code(such as ocfs2_read_inode()), i think you may allocate/free the space by caller?

This is consistent with ocfs2_read_cached_inode() which allocates the
cached structure. So I don't think it needs to be changed. I'll just fix fill_sparse_files() to properly release the structure.

Quoted 9 lines
Quoted 9 lines
libocfs2/quota.c new file +9 -0
+{
+	errcode_t err;
+	ocfs2_cached_dquot *dquot;
+
+	err = ocfs2_malloc0(sizeof(ocfs2_cached_dquot), &dquot);
+	if (err)
+		return err;
+
+	err = ocfs2_find_tree_dqentry(fs, type, dquot, QT_TREEOFF, 0);
Quoted 8 lines

libocfs2/quota.c line 1217

Quoted 8 lines
libocfs2/quota.c new file +8 -0
+	if (err)
+		goto bail;
+	*ret_dquot = dquot;
+	return 0;
+bail:
+	ocfs2_free(&dquot);
+	return err;
+}

Thanks for review.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#7 lore raw

PATCH 4/9 Write out quota info changes on ocfs2_close()

In reply to Jan Kara

We don't write out change of information in quota file header on each change, we rather cache it in ocfs2_filesys structure. So write out all the information when ocfs2_close() is called.

---
 libocfs2/closefs.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
libocfs2/closefs.c +12 -0
index 54411da..290bfd1 100644
@@ -39,6 +39,7 @@ errcode_t ocfs2_flush(ocfs2_filesys *fs)
 errcode_t ocfs2_close(ocfs2_filesys *fs)
 {
 	errcode_t ret;
+	int type;
 
 	if (fs->fs_flags & OCFS2_FLAG_DIRTY) {
 		ret = ocfs2_flush(fs);
@@ -46,6 +47,17 @@ errcode_t ocfs2_close(ocfs2_filesys *fs)
 			return ret;
 	}
 
+	for (type = 0; type < MAXQUOTAS; type++)
+		if (fs->qinfo[type].flags & OCFS2_QF_INFO_DIRTY) {
+			ret = ocfs2_write_global_quota_info(fs, type);
+			if (ret)
+				return ret;
+			ret = ocfs2_write_cached_inode(fs,
+						fs->qinfo[type].qi_inode);
+			if (ret)
+				return ret;
+		}
+
 	ocfs2_freefs(fs);
 	return 0;
 }
Signature · 1 line
1.6.0.2
#8 lore raw

PATCH 4/9 Write out quota info changes on ocfs2_close()

Quoted 22 lines · Jan Kara

We don't write out change of information in quota file header on each change, we rather cache it in ocfs2_filesys structure. So write out all the information when ocfs2_close() is called.

---
 libocfs2/closefs.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
libocfs2/closefs.c +1 -0
index 54411da..290bfd1 100644
@@ -39,6 +39,7 @@ errcode_t ocfs2_flush(ocfs2_filesys *fs)
 errcode_t ocfs2_close(ocfs2_filesys *fs)
 {
 	errcode_t ret;
+	int type;
 
 	if (fs->fs_flags & OCFS2_FLAG_DIRTY) {
 		ret = ocfs2_flush(fs);

libocfs2/closefs.c errcode_t ocfs2_close(ocfs2_filesys *fs) · line 52

Quoted 6 lines
libocfs2/closefs.c +3 -0
@@ -46,6 +47,17 @@ errcode_t ocfs2_close(ocfs2_filesys *fs)
 			return ret;
 	}
 
+	for (type = 0; type < MAXQUOTAS; type++)
+		if (fs->qinfo[type].flags & OCFS2_QF_INFO_DIRTY) {
+			ret = ocfs2_write_global_quota_info(fs, type);

Sometimes, some fields of 'fs' may get freed somehow(e.g, such as tunefs.ocfs2 case) before we call ocfs2_close(), then your ocfs2_write_global_quota_info() may suffer a segfault disaster somehow when it refers to 'fs_io' or some other fields.

Quoted 11 lines
libocfs2/closefs.c +8 -0
+			if (ret)
+				return ret;
+			ret = ocfs2_write_cached_inode(fs,
+						fs->qinfo[type].qi_inode);
+			if (ret)
+				return ret;
+		}
+
 	ocfs2_freefs(fs);
 	return 0;
 }
author
#9 lore raw

PATCH 4/9 Write out quota info changes on ocfs2_close()

Quoted 26 lines · tristan.ye
Quoted 25 lines · Jan Kara

We don't write out change of information in quota file header on each change, we rather cache it in ocfs2_filesys structure. So write out all the information when ocfs2_close() is called.

---
 libocfs2/closefs.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
libocfs2/closefs.c +1 -0
index 54411da..290bfd1 100644
@@ -39,6 +39,7 @@ errcode_t ocfs2_flush(ocfs2_filesys *fs)
 errcode_t ocfs2_close(ocfs2_filesys *fs)
 {
 	errcode_t ret;
+	int type;
 
 	if (fs->fs_flags & OCFS2_FLAG_DIRTY) {
 		ret = ocfs2_flush(fs);
@@ -46,6 +47,17 @@ errcode_t ocfs2_close(ocfs2_filesys *fs)
 			return ret;
 	}
 
Quoted 8 lines

libocfs2/closefs.c errcode_t ocfs2_close(ocfs2_filesys *fs) · line 52

Quoted 3 lines
libocfs2/closefs.c +3 -0
+	for (type = 0; type < MAXQUOTAS; type++)
+		if (fs->qinfo[type].flags & OCFS2_QF_INFO_DIRTY) {
+			ret = ocfs2_write_global_quota_info(fs, type);

Sometimes, some fields of 'fs' may get freed somehow(e.g, such as tunefs.ocfs2 case) before we call ocfs2_close(), then your ocfs2_write_global_quota_info() may suffer a segfault disaster somehow when it refers to 'fs_io' or some other fields.

I see, it is Joel's commit 21277dec029c5453847ebd77315a663eb317e80c for
tunefs to use one IO cache. It was not present in 1.4.2 ocfs2-tools with which I was testing my patches.
Joel, how is ocfs2_closefs() supposed to flush dirty data when fs_io is
cleared? Your patch seems to assume that ocfs2_filesys keeps no cached information about the filesystem and sharing fs_io is enough for a consistent view. That might be true, although I see some ocfs2_cached_inode pointers in ocfs2_filesys structure, but it seems like an unlucky decision since firstly caching some data in ocfs2_filesys seems beneficial to me and secondly it breaks my quota code ;-). I admit I don't know the details about the tunefs.ocfs2 internals but I don't quite get why it just does not have one ocfs2_filesys structure and doesn't do all the IO through it. That would seem like the most natural thing to me and you'd save some hassle with sharing fs_io...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#10 lore raw

PATCH 5/9 Quota support for fsck.ocfs2

In reply to Jan Kara

After the filesystem is fixed, scan quota files and try to gather as much information about set limits as possible. Then scan the filesystem to compute correct quota usage and dump all the gathered information into freshly created quota files.

---
 fsck.ocfs2/Makefile               |    2 +
 fsck.ocfs2/fsck.c                 |    9 +
 fsck.ocfs2/fsck.ocfs2.checks.8.in |   30 ++
 fsck.ocfs2/include/pass5.h        |   26 ++
 fsck.ocfs2/pass5.c                |  533 +++++++++++++++++++++++++++++++++++++
 5 files changed, 600 insertions(+), 0 deletions(-)
 create mode 100644 fsck.ocfs2/include/pass5.h
 create mode 100644 fsck.ocfs2/pass5.c
fsck.ocfs2/Makefile +2 -0
index 7f6e547..850e106 100644
@@ -28,6 +28,7 @@ CFILES =	fsck.c		\
 		pass2.c 	\
 		pass3.c 	\
 		pass4.c 	\
+		pass5.c		\
 		problem.c 	\
 		slot_recovery.c \
 		strings.c 	\
@@ -46,6 +47,7 @@ HFILES = 	include/fsck.h		\
 		include/pass2.h		\
 		include/pass3.h		\
 		include/pass4.h		\
+		include/pass5.h		\
 		include/problem.h	\
 		include/slot_recovery.h	\
 		include/strings.h	\
fsck.ocfs2/fsck.c +9 -0
index a686886..fe15783 100644
@@ -30,6 +30,8 @@
  * pass2.c: verify directory entries, record some linkage metadata
  * pass3.c: make sure all dirs are reachable
  * pass4.c: resolve inode's link counts, move disconnected inodes to lost+found
+ * pass5.c: load global quota file, merge node-local quota files to global
+ *          quota file, recompute quota usage and recreate quota files
  *
  * When hacking on this keep the following in mind:
  *
@@ -64,6 +66,7 @@
 #include "pass2.h"
 #include "pass3.h"
 #include "pass4.h"
+#include "pass5.h"
 #include "problem.h"
 #include "util.h"
 #include "slot_recovery.h"
@@ -917,6 +920,12 @@ int main(int argc, char **argv)
 		goto done;
 	}
 
+	ret = o2fsck_pass5(ost);
+	if (ret) {
+		com_err(whoami, ret, "while performing pass 5");
+		goto done;
+	}
+
 done:
 	if (ret)
 		fsck_mask |= FSCK_ERROR;
fsck.ocfs2/fsck.ocfs2.checks.8.in +30 -0
index 739b867..f70f48d 100644
@@ -851,6 +851,36 @@ does not match the number of buckets found by fsck.
 
 Answering yes will change this to the correct count.
 
+\" pass5.c
+
+.SS "QMAGIC_INVALID"
+The magic number in the header of quota file does not match the proper
+number.
+
+Answering yes will make fsck use values in the quota file header anyway.
+
+.SS "QTREE_BLK_INVALID"
+Block with references to other blocks with quota data is corrupted.
+
+Answering yes will make fsck use references in the block.
+
+.SS "DQBLK_INVALID"
+The structure with quota limits was found in a corrupted block.
+
+Answering yes will use the values of limits for the user / group.
+
+.SS "DUP_DQBLK_INVALID"
+The structure with quota limits was found in a corrupted block
+and fsck has already found quota limits for this user / group.
+
+Answering yes will use new values of limits for the user / group.
+
+.SS "DUP_DQBLK_VALID"
+The structure with quota limits was found in a correct block
+but fsck has already found quota limits for this user / group.
+
+Answering yes will use new values of limits for the user / group.
+
 .SH "SEE ALSO"
 .BR fsck.ocfs2(8)
 
fsck.ocfs2/include/pass5.h new file +26 -0
new file mode 100644
index 0000000..6c0fc13
@@ -0,0 +1,26 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * pass5.h
+ *
+ * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef __O2FSCK_PASS5_H__
+#define __O2FSCK_PASS5_H__
+
+#include "fsck.h"
+
+errcode_t o2fsck_pass5(o2fsck_state *ost);
+
+#endif /* __O2FSCK_PASS4_H__ */
+
fsck.ocfs2/pass5.c new file +533 -0

533 lines not on this page · raw message

Signature · 1 line
1.6.0.2
author
#11 lore raw

PATCH 6/9 Quota support for mkfs.ocfs2

In reply to Jan Kara
---
 mkfs.ocfs2/mkfs.c          |  146 +++++++++++++++++++++++++++++++++++++++++++-
 mkfs.ocfs2/mkfs.h          |    1 +
 mkfs.ocfs2/mkfs.ocfs2.8.in |   16 +++++
 3 files changed, 162 insertions(+), 1 deletions(-)
mkfs.ocfs2/mkfs.c +145 -1
index 6acc6e6..031b8a6 100644
@@ -98,12 +98,16 @@ static SystemFileInfo system_files[] = {
 	{ "slot_map", SFI_OTHER, 1, S_IFREG | 0644 },
 	{ "heartbeat", SFI_HEARTBEAT, 1, S_IFREG | 0644 },
 	{ "global_bitmap", SFI_CLUSTER, 1, S_IFREG | 0644 },
+	{ "aquota.user", SFI_QUOTA, 1, S_IFREG | 0644 },
+	{ "aquota.group", SFI_QUOTA, 1, S_IFREG | 0644 },
 	{ "orphan_dir:%04d", SFI_OTHER, 0, S_IFDIR | 0755 },
 	{ "extent_alloc:%04d", SFI_CHAIN, 0, S_IFREG | 0644 },
 	{ "inode_alloc:%04d", SFI_CHAIN, 0, S_IFREG | 0644 },
 	{ "journal:%04d", SFI_JOURNAL, 0, S_IFREG | 0644 },
 	{ "local_alloc:%04d", SFI_LOCAL_ALLOC, 0, S_IFREG | 0644 },
-	{ "truncate_log:%04d", SFI_TRUNCATE_LOG, 0, S_IFREG | 0644 }
+	{ "truncate_log:%04d", SFI_TRUNCATE_LOG, 0, S_IFREG | 0644 },
+	{ "aquota.user:%04d", SFI_QUOTA, 0, S_IFREG | 0644 },
+	{ "aquota.group:%04d", SFI_QUOTA, 0, S_IFREG | 0644 },
 };
 
 struct fs_type_translation {
@@ -226,6 +230,23 @@ static void mkfs_init_dir_trailer(State *s, DirData *dir, void *buf)
 	}
 }
 
+/* Should we skip this inode because of features enabled / disabled? */
+static int feature_skip(State *s, int system_inode)
+{
+	switch (system_inode) {
+		case USER_QUOTA_SYSTEM_INODE:
+		case LOCAL_USER_QUOTA_SYSTEM_INODE:
+			return !(s->feature_flags.opt_ro_compat &
+					OCFS2_FEATURE_RO_COMPAT_USRQUOTA);
+		case GROUP_QUOTA_SYSTEM_INODE:
+		case LOCAL_GROUP_QUOTA_SYSTEM_INODE:
+			return !(s->feature_flags.opt_ro_compat &
+					OCFS2_FEATURE_RO_COMPAT_GRPQUOTA);
+		default:
+			return 0;
+	}
+}
+
 static inline uint32_t system_dir_bytes_needed(State *s)
 {
 	int each = OCFS2_DIR_REC_LEN(SYSTEM_FILE_NAME_MAX);
@@ -233,6 +254,114 @@ static inline uint32_t system_dir_bytes_needed(State *s)
 	return each * sys_blocks_needed(s->initial_slots);
 }
 
+static void format_quota_files(State *s, ocfs2_filesys *fs)
+{
+	errcode_t ret;
+	ocfs2_quota_hash *usr_hash = NULL, *grp_hash = NULL;
+
+	/* Write correct data into quota files */
+	if (!feature_skip(s, USER_QUOTA_SYSTEM_INODE)) {
+		ret = ocfs2_init_fs_quota_info(fs, USRQUOTA);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while looking up global user quota file");
+			goto error;
+		}
+		fs->qinfo[USRQUOTA].flags = 0;
+		fs->qinfo[USRQUOTA].qi_info.dqi_syncms = OCFS2_DEF_QUOTA_SYNC;
+		fs->qinfo[USRQUOTA].qi_info.dqi_bgrace = OCFS2_DEF_BLOCK_GRACE;
+		fs->qinfo[USRQUOTA].qi_info.dqi_igrace = OCFS2_DEF_INODE_GRACE;
+
+		ret = ocfs2_new_quota_hash(&usr_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while creating user quota hash.");
+			goto error;
+		}
+		ret = ocfs2_init_global_quota_file(fs, USRQUOTA);
+		if (ret) {
+			com_err(s->progname, ret, "while creating global user "
+				"quota file");
+			goto error;
+		}
+		ret = ocfs2_init_local_quota_files(fs, USRQUOTA);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while initializing local user quota files");
+			goto error;
+		}
+	}
+	if (!feature_skip(s, GROUP_QUOTA_SYSTEM_INODE)) {
+		ret = ocfs2_init_fs_quota_info(fs, GRPQUOTA);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while looking up global group quota file");
+			goto error;
+		}
+		fs->qinfo[GRPQUOTA].flags = 0;
+		fs->qinfo[GRPQUOTA].qi_info.dqi_syncms = OCFS2_DEF_QUOTA_SYNC;
+		fs->qinfo[GRPQUOTA].qi_info.dqi_bgrace = OCFS2_DEF_BLOCK_GRACE;
+		fs->qinfo[GRPQUOTA].qi_info.dqi_igrace = OCFS2_DEF_INODE_GRACE;
+		ret = ocfs2_new_quota_hash(&grp_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while creating group quota hash.");
+			goto error;
+		}
+		ret = ocfs2_init_global_quota_file(fs, GRPQUOTA);
+		if (ret) {
+			com_err(s->progname, ret, "while creating global group "
+				"quota file");
+			goto error;
+		}
+
+		ret = ocfs2_init_local_quota_files(fs, GRPQUOTA);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while initializing local group quota files");
+			goto error;
+		}
+	}
+
+	ret = ocfs2_compute_quota_usage(fs, usr_hash, grp_hash);
+	if (ret) {
+		com_err(s->progname, ret, "while computing quota usage");
+		goto error;
+	}
+	if (usr_hash) {
+		ret = ocfs2_write_release_dquots(fs, USRQUOTA, usr_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while writing user quota usage");
+			goto error;
+		}
+		ret = ocfs2_free_quota_hash(usr_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while releasing user quota hash");
+			goto error;
+		}
+	}
+	if (grp_hash) {
+		ret = ocfs2_write_release_dquots(fs, GRPQUOTA, grp_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while writing group quota usage");
+			goto error;
+		}
+		ret = ocfs2_free_quota_hash(grp_hash);
+		if (ret) {
+			com_err(s->progname, ret,
+				"while releasing group quota hash");
+			goto error;
+		}
+	}
+	return;
+error:
+	clear_both_ends(s);
+	exit(1);
+}
+
 static void finish_normal_format(State *s)
 {
 	errcode_t ret;
@@ -304,6 +433,14 @@ static void finish_normal_format(State *s)
 	if (!s->quiet)
 		printf("done\n");
 
+	if (!s->quiet)
+		printf("Formatting quota files: ");
+
+	format_quota_files(s, fs);
+
+	if (!s->quiet)
+		printf("done\n");
+
 	ocfs2_close(fs);
 }
 
@@ -471,6 +608,8 @@ main(int argc, char **argv)
 	for (i = 0; i < NUM_SYSTEM_INODES; i++) {
 		if (hb_dev_skip(s, i))
 			continue;
+		if (feature_skip(s, i))
+			continue;
 
 		num = (system_files[i].global) ? 1 : s->initial_slots;
 		for (j = 0; j < num; j++) {
@@ -529,6 +668,8 @@ main(int argc, char **argv)
 	for (i = 0; i < NUM_SYSTEM_INODES; i++) {
 		if (hb_dev_skip(s, i))
 			continue;
+		if (feature_skip(s, i))
+			continue;
 
 		num = system_files[i].global ? 1 : s->initial_slots;
 		for (j = 0; j < num; j++) {
@@ -2432,6 +2573,9 @@ init_record(State *s, SystemFileDiskRecord *rec, int type, int mode)
 	case SFI_TRUNCATE_LOG:
 		rec->flags |= OCFS2_DEALLOC_FL;
 		break;
+	case SFI_QUOTA:
+		rec->flags |= OCFS2_QUOTA_FL;
+		break;
 	case SFI_OTHER:
 		break;
 	}
mkfs.ocfs2/mkfs.h +1 -0
index bd8ac45..969e4df 100644
@@ -96,6 +96,7 @@ enum {
 	SFI_HEARTBEAT,
 	SFI_CHAIN,
 	SFI_TRUNCATE_LOG,
+	SFI_QUOTA,
 	SFI_OTHER
 };
 
mkfs.ocfs2/mkfs.ocfs2.8.in +16 -0
index 38433ee..d1f4011 100644
@@ -146,6 +146,22 @@ arbitrary binary data. Attributes can be attached to all types of inodes: regula
 symbolic links, device nodes, etc. This feature is required for users wanting to use extended security
 facilities like POSIX ACLs or SELinux.
 .RE
+.RS 1.2i
+.TP
+\fBusrquota\fR
+Enable user quota support. With this feature enabled, filesystem will track amount of space
+and number of inodes (files, directories, symbolic links) each user owns. It is then possible
+to limit the maximum amount of space or inodes user can have. See a documentation of
+quota-tools package for more details.
+.RE
+.RS 1.2i
+.TP
+\fBgrpquota\fR
+Enable group quota support. With this feature enabled, filesystem will track amount of space
+and number of inodes (files, directories, symbolic links) each group owns. It is then possible
+to limit the maximum amount of space or inodes user can have. See a documentation of
+quota-tools package for more details.
+.RE
 
 .TP
 \fB\-\-fs\-feature\-level=\fR\fR\fIfeature\-level\fR
Signature · 1 line
1.6.0.2
author
#12 lore raw

PATCH 7/9 Add quota support to tunefs.ocfs2

In reply to Jan Kara

Implement setting of quota feature via tunefs.ocfs2. Also properly create / delete local quota files when number of slots increases / decreases, update quota information when sparse feature gets disabled.

Implement setting of interval in which we sync changes in local quota file to the global quota file.

---
 tunefs.ocfs2/Makefile                     |    6 +-
 tunefs.ocfs2/feature_quota.c              |  418 +++++++++++++++++++++++++++++
 tunefs.ocfs2/feature_sparse_files.c       |   68 +++++-
 tunefs.ocfs2/ocfs2ne.c                    |   26 ++
 tunefs.ocfs2/op_features.c                |    4 +
 tunefs.ocfs2/op_set_quota_sync_interval.c |  170 ++++++++++++
 tunefs.ocfs2/op_set_slot_count.c          |   91 ++++++-
 7 files changed, 776 insertions(+), 7 deletions(-)
 create mode 100644 tunefs.ocfs2/feature_quota.c
 create mode 100644 tunefs.ocfs2/op_set_quota_sync_interval.c
tunefs.ocfs2/Makefile +4 -2
index fd244d8..d28da81 100644
@@ -24,7 +24,8 @@ OCFS2NE_FEATURES =			\
 	feature_metaecc			\
 	feature_sparse_files		\
 	feature_unwritten_extents	\
-	feature_xattr
+	feature_xattr			\
+	feature_quota
 
 OCFS2NE_OPERATIONS =			\
 	op_cloned_volume		\
@@ -36,7 +37,8 @@ OCFS2NE_OPERATIONS =			\
 	op_set_label			\
 	op_set_journal_size		\
 	op_set_slot_count		\
-	op_update_cluster_stack
+	op_update_cluster_stack		\
+	op_set_quota_sync_interval	\
 
 sbindir = $(root_sbindir)
 SBIN_PROGRAMS = tunefs.ocfs2
tunefs.ocfs2/feature_quota.c new file +418 -0

418 lines not on this page · raw message

tunefs.ocfs2/feature_sparse_files.c +64 -4

104 lines not on this page · raw message

tunefs.ocfs2/ocfs2ne.c +26 -0
index c9051a4..48760c6 100644
@@ -98,6 +98,8 @@ extern struct tunefs_operation set_label_op;
 extern struct tunefs_operation set_slot_count_op;
 extern struct tunefs_operation update_cluster_stack_op;
 extern struct tunefs_operation cloned_volume_op;
+extern struct tunefs_operation set_usrquota_sync_interval_op;
+extern struct tunefs_operation set_grpquota_sync_interval_op;
 
 /* List of operations we're going to run */
 static LIST_HEAD(tunefs_run_list);
@@ -583,6 +585,28 @@ static struct tunefs_option journal_option = {
 	.opt_handle	= handle_journal_arg,
 };
 
+static struct tunefs_option set_usrquota_sync_interval_option = {
+	.opt_option	= {
+		.name		= "usrquota-sync-interval",
+		.val		= 256,
+		.has_arg	= 1,
+	},
+	.opt_help	= "   --usrquota-sync-interval <interval>",
+	.opt_handle	= generic_handle_arg,
+	.opt_op		= &set_usrquota_sync_interval_op,
+};
+
+static struct tunefs_option set_grpquota_sync_interval_option = {
+	.opt_option	= {
+		.name		= "grpquota-sync-interval",
+		.val		= 257,
+		.has_arg	= 1,
+	},
+	.opt_help	= "   --grpquota-sync-interval <interval>",
+	.opt_handle	= generic_handle_arg,
+	.opt_op		= &set_grpquota_sync_interval_op,
+};
+
 /* The order here creates the order in print_usage() */
 static struct tunefs_option *options[] = {
 	&help_option,
@@ -603,6 +627,8 @@ static struct tunefs_option *options[] = {
 	&features_option,
 	&update_cluster_stack_option,
 	&cloned_volume_option,
+	&set_usrquota_sync_interval_option,
+	&set_grpquota_sync_interval_option,
 	&yes_option,
 	&no_option,
 	NULL,
tunefs.ocfs2/op_features.c +4 -0
index ee9442e..afcc1f4 100644
@@ -41,6 +41,8 @@ extern struct tunefs_feature metaecc_feature;
 extern struct tunefs_feature sparse_files_feature;
 extern struct tunefs_feature unwritten_extents_feature;
 extern struct tunefs_feature xattr_feature;
+extern struct tunefs_feature usrquota_feature;
+extern struct tunefs_feature grpquota_feature;
 
 /* List of features supported by ocfs2ne */
 static struct tunefs_feature *features[] = {
@@ -52,6 +54,8 @@ static struct tunefs_feature *features[] = {
 	&sparse_files_feature,
 	&unwritten_extents_feature,
 	&xattr_feature,
+	&usrquota_feature,
+	&grpquota_feature,
 	NULL,
 };
 
tunefs.ocfs2/op_set_quota_sync_interval.c new file +170 -0

170 lines not on this page · raw message

tunefs.ocfs2/op_set_slot_count.c +90 -1

127 lines not on this page · raw message

Signature · 1 line
1.6.0.2
author
#13 lore raw

PATCH 8/9 Change headers to reflect that quota is now fully supported.

In reply to Jan Kara
---
 include/ocfs2-kernel/ocfs2_fs.h |    4 +++-
 libocfs2/feature_string.c       |    4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
include/ocfs2-kernel/ocfs2_fs.h +3 -1
index f3c9b53..1650315 100644
@@ -96,7 +96,9 @@
 					 | OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK \
 					 | OCFS2_FEATURE_INCOMPAT_META_ECC \
 					 | OCFS2_FEATURE_INCOMPAT_XATTR)
-#define OCFS2_FEATURE_RO_COMPAT_SUPP	OCFS2_FEATURE_RO_COMPAT_UNWRITTEN
+#define OCFS2_FEATURE_RO_COMPAT_SUPP	(OCFS2_FEATURE_RO_COMPAT_UNWRITTEN \
+					 | OCFS2_FEATURE_RO_COMPAT_USRQUOTA \
+					 | OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
 
 /*
  * Heartbeat-only devices are missing journals and other files.  The
libocfs2/feature_string.c +3 -1
index 18ae6e9..cf3a0dd 100644
@@ -76,7 +76,9 @@ static ocfs2_fs_options feature_level_defaults[] = {
 	 OCFS2_FEATURE_INCOMPAT_INLINE_DATA |
 	 OCFS2_FEATURE_INCOMPAT_META_ECC |
 	 OCFS2_FEATURE_INCOMPAT_XATTR,
-	 OCFS2_FEATURE_RO_COMPAT_UNWRITTEN}, /* OCFS2_FEATURE_LEVEL_MAX_FEATURES */
+	 OCFS2_FEATURE_RO_COMPAT_UNWRITTEN |
+	 OCFS2_FEATURE_RO_COMPAT_USRQUOTA |
+	 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA }, /* OCFS2_FEATURE_LEVEL_MAX_FEATURES */
 };
 
 /* These are the features we support in mkfs/tunefs via --fs-features */
Signature · 1 line
1.6.0.2
author
#14 lore raw

PATCH 9/9 Fix tunefs space check when disabling SPARSE feature

In reply to Jan Kara

Tunefs missed addition of number clusters needed to fill holes when disabling SPARSE feature and hence the check whether there's enough space in the filesystem didn't work.

---
 tunefs.ocfs2/feature_sparse_files.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
tunefs.ocfs2/feature_sparse_files.c +1 -0
index 65db46b..e5b3dab 100644
@@ -321,6 +321,7 @@ static errcode_t hole_iterate(ocfs2_filesys *fs, struct ocfs2_dinode *di,
 
 	list_add_tail(&file->list, &ctxt->files);
 	ctxt->holecount += file->holes_num;
+	ctxt->more_clusters += file->hole_clusters;
 
 	tools_progress_step(ctxt->prog, 1);
 
Signature · 1 line
1.6.0.2