Whamcloud - gitweb
b=21804 make sure the request is protected by rq_refcount while
[fs/lustre-release.git] / lustre / lvfs / lustre_quota_fmt.c
index 0488c2c..c618983 100644 (file)
@@ -50,7 +50,9 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/quotaio_v1.h>
+#ifdef HAVE_QUOTAIO_V1_H
+# include <linux/quotaio_v1.h>
+#endif
 
 #include <asm/byteorder.h>
 #include <asm/uaccess.h>
 #ifdef HAVE_QUOTA_SUPPORT
 
 static const uint lustre_initqversions[][MAXQUOTAS] = {
+        [LUSTRE_QUOTA_V1] = LUSTRE_INITQVERSIONS_V1,
         [LUSTRE_QUOTA_V2] = LUSTRE_INITQVERSIONS_V2
 };
 
 static const int lustre_dqstrinblk[] = {
+        [LUSTRE_QUOTA_V1] = LUSTRE_DQSTRINBLK,
         [LUSTRE_QUOTA_V2] = LUSTRE_DQSTRINBLK_V2
 };
 
 static const int lustre_disk_dqblk_sz[] = {
+        [LUSTRE_QUOTA_V1] = sizeof(struct lustre_disk_dqblk),
         [LUSTRE_QUOTA_V2] = sizeof(struct lustre_disk_dqblk_v2)
 };
 
 static const union
 {
+        struct lustre_disk_dqblk    r0;
         struct lustre_disk_dqblk_v2 r1;
 } fakedquot[] = {
-        [LUSTRE_QUOTA_V2] = {.r1 = {.dqb_itime = __constant_cpu_to_le64(1LLU)} }
+        [LUSTRE_QUOTA_V1] = {.r0 = {.dqb_itime = __constant_cpu_to_le64(1LLU)}},
+        [LUSTRE_QUOTA_V2] = {.r1 = {.dqb_itime = __constant_cpu_to_le64(1LLU)}}
 };
 
 static const union
 {
+        struct lustre_disk_dqblk    r0;
         struct lustre_disk_dqblk_v2 r1;
 } emptydquot[] = {
-        [LUSTRE_QUOTA_V2] = {.r1 = { 0 } }
+        [LUSTRE_QUOTA_V1] = {.r0 = { 0 }},
+        [LUSTRE_QUOTA_V2] = {.r1 = { 0 }}
 };
 
-int check_quota_file(struct file *f, struct inode *inode, int type, 
+extern void *lustre_quota_journal_start(struct inode *inode, int delete);
+extern void lustre_quota_journal_stop(void *handle);
+extern ssize_t lustre_read_quota(struct file *f, struct inode *inode, int type,
+                                 char *buf, int count, loff_t pos);
+extern ssize_t lustre_write_quota(struct file *f, char *buf, int count, loff_t pos);
+
+int check_quota_file(struct file *f, struct inode *inode, int type,
                      lustre_quota_version_t version)
 {
         struct lustre_disk_dqheader dqhead;
-        mm_segment_t fs;
         ssize_t size;
-        loff_t offset = 0;
         static const uint quota_magics[] = LUSTRE_INITQMAGICS;
         const uint *quota_versions = lustre_initqversions[version];
 
-        if (!inode && !f) {
+        if (!f && !inode) {
                 CERROR("check_quota_file failed!\n");
                 libcfs_debug_dumpstack(NULL);
                 return -EINVAL;
         }
 
-        if (f) {
-                fs = get_fs();
-                set_fs(KERNEL_DS);
-                size = f->f_op->read(f, (char *)&dqhead,
-                                     sizeof(struct lustre_disk_dqheader), 
-                                     &offset);
-                set_fs(fs);
-        } else { 
-#ifndef KERNEL_SUPPORTS_QUOTA_READ
-                size = 0;
-#else
-                struct super_block *sb = inode->i_sb;
-                size = sb->s_op->quota_read(sb, type, (char *)&dqhead, 
-                                            sizeof(struct lustre_disk_dqheader),
-                                            0);
-#endif
-        }
+        size = lustre_read_quota(f, inode, type, (char *)&dqhead,
+                                 sizeof(struct lustre_disk_dqheader), 0);
+
         if (size != sizeof(struct lustre_disk_dqheader))
                 return -EINVAL;
         if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
@@ -128,9 +127,7 @@ int check_quota_file(struct file *f, struct inode *inode, int type,
         return 0;
 }
 
-/**
- * Check whether given file is really lustre admin quotafile
- */
+/* Check whether given file is really lustre admin quotafile */
 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
 {
         struct file *f = lqi->qi_files[type];
@@ -139,18 +136,15 @@ int lustre_check_quota_file(struct lustre_quota_info *lqi, int type)
 
 int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
 {
-        mm_segment_t fs;
         struct lustre_disk_dqinfo dinfo;
         ssize_t size;
-        loff_t offset = LUSTRE_DQINFOOFF;
 
-        fs = get_fs();
-        set_fs(KERNEL_DS);
-        size = f->f_op->read(f, (char *)&dinfo, 
-                             sizeof(struct lustre_disk_dqinfo), &offset);
-        set_fs(fs);
+        size = lustre_read_quota(f, NULL, 0, (char *)&dinfo,
+                                 sizeof(struct lustre_disk_dqinfo),
+                                 LUSTRE_DQINFOOFF);
+
         if (size != sizeof(struct lustre_disk_dqinfo)) {
-                CDEBUG(D_ERROR, "Can't read info structure on device %s.\n",
+                CERROR("Can't read info structure on device %s.\n",
                        f->f_vfsmnt->mnt_sb->s_id);
                 return -EINVAL;
         }
@@ -163,26 +157,19 @@ int lustre_read_quota_file_info(struct file* f, struct lustre_mem_dqinfo* info)
         return 0;
 }
 
-/**
- * Read information header from quota file
- */
+/* Read information header from quota file */
 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type)
 {
-        return lustre_read_quota_file_info(lqi->qi_files[type],
-                                           &lqi->qi_info[type]);
+        return lustre_read_quota_file_info(lqi->qi_files[type], &lqi->qi_info[type]);
 }
 
-/**
- * Write information header to quota file
- */
+/* Write information header to quota file */
 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
 {
-        mm_segment_t fs;
         struct lustre_disk_dqinfo dinfo;
         struct lustre_mem_dqinfo *info = &lqi->qi_info[type];
         struct file *f = lqi->qi_files[type];
         ssize_t size;
-        loff_t offset = LUSTRE_DQINFOOFF;
 
         info->dqi_flags &= ~DQF_INFO_DIRTY;
         dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
@@ -191,53 +178,73 @@ int lustre_write_quota_info(struct lustre_quota_info *lqi, int type)
         dinfo.dqi_blocks = cpu_to_le32(info->dqi_blocks);
         dinfo.dqi_free_blk = cpu_to_le32(info->dqi_free_blk);
         dinfo.dqi_free_entry = cpu_to_le32(info->dqi_free_entry);
-        fs = get_fs();
-        set_fs(KERNEL_DS);
-        size = f->f_op->write(f, (char *)&dinfo, 
-                              sizeof(struct lustre_disk_dqinfo), &offset);
-        set_fs(fs);
+
+        size = lustre_write_quota(f, (char *)&dinfo,
+                                  sizeof(struct lustre_disk_dqinfo),
+                                  LUSTRE_DQINFOOFF);
+
         if (size != sizeof(struct lustre_disk_dqinfo)) {
-                CDEBUG(D_WARNING, 
-                       "Can't write info structure on device %s.\n",
-                       f->f_vfsmnt->mnt_sb->s_id);
+                CWARN("Can't write info structure on device %s.\n",
+                      f->f_vfsmnt->mnt_sb->s_id);
                 return -1;
         }
         return 0;
 }
 
+#define DQ2MQ(v) ((sizeof(v) == sizeof(__u64)) ? \
+                le64_to_cpu(v) : le32_to_cpu(v))
+
+#define MQ2DQ(v,newv) ((sizeof(v) == sizeof(__u64)) ? \
+                (v = cpu_to_le64((__u64)newv)) : (v = cpu_to_le32((__u32)newv)))
+
+#define DQF_GET(var,ver,field) ((ver == LUSTRE_QUOTA_V1)?\
+                DQ2MQ(((struct lustre_disk_dqblk*)(var))->field):\
+                DQ2MQ(((struct lustre_disk_dqblk_v2*)(var))->field))
+
+#define DQF_PUT(var,ver,field,val) ((ver == LUSTRE_QUOTA_V1)?\
+                MQ2DQ(((struct lustre_disk_dqblk*)(var))->field, val):\
+                MQ2DQ(((struct lustre_disk_dqblk_v2*)(var))->field, val))
+
 void disk2memdqb(struct lustre_mem_dqblk *m, void *d,
                  lustre_quota_version_t version)
 {
-        struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
-
-        LASSERT(version == LUSTRE_QUOTA_V2);
-
-        m->dqb_ihardlimit = le64_to_cpu(dqblk->dqb_ihardlimit);
-        m->dqb_isoftlimit = le64_to_cpu(dqblk->dqb_isoftlimit);
-        m->dqb_curinodes = le64_to_cpu(dqblk->dqb_curinodes);
-        m->dqb_itime = le64_to_cpu(dqblk->dqb_itime);
-        m->dqb_bhardlimit = le64_to_cpu(dqblk->dqb_bhardlimit);
-        m->dqb_bsoftlimit = le64_to_cpu(dqblk->dqb_bsoftlimit);
-        m->dqb_curspace = le64_to_cpu(dqblk->dqb_curspace);
-        m->dqb_btime = le64_to_cpu(dqblk->dqb_btime);
+        m->dqb_ihardlimit = DQF_GET(d, version, dqb_ihardlimit);
+        m->dqb_isoftlimit = DQF_GET(d, version, dqb_isoftlimit);
+        m->dqb_curinodes = DQF_GET(d, version, dqb_curinodes);
+        m->dqb_itime = DQF_GET(d, version, dqb_itime);
+        m->dqb_bhardlimit = DQF_GET(d, version, dqb_bhardlimit);
+        m->dqb_bsoftlimit = DQF_GET(d, version, dqb_bsoftlimit);
+        m->dqb_curspace = DQF_GET(d, version, dqb_curspace);
+        m->dqb_btime = DQF_GET(d, version, dqb_btime);
+}
+
+static int check_quota_bounds(struct lustre_mem_dqblk *m, 
+                              lustre_quota_version_t version)
+{
+        return (version == LUSTRE_QUOTA_V1  &&
+                m->dqb_ihardlimit <= MAX_UL &&
+                m->dqb_isoftlimit <= MAX_UL &&
+                m->dqb_curinodes <= MAX_UL  &&
+                m->dqb_bhardlimit <= MAX_UL &&
+                m->dqb_bsoftlimit <= MAX_UL) ||
+                version != LUSTRE_QUOTA_V1;
 }
 
 static int mem2diskdqb(void *d, struct lustre_mem_dqblk *m,
                        qid_t id, lustre_quota_version_t version)
 {
-        struct lustre_disk_dqblk_v2 *dqblk = (struct lustre_disk_dqblk_v2 *)d;
-
-        LASSERT(version == LUSTRE_QUOTA_V2);
+        if (!check_quota_bounds(m, version))
+                return -EINVAL;
 
-        dqblk->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
-        dqblk->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
-        dqblk->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
-        dqblk->dqb_itime = cpu_to_le64(m->dqb_itime);
-        dqblk->dqb_bhardlimit = cpu_to_le64(m->dqb_bhardlimit);
-        dqblk->dqb_bsoftlimit = cpu_to_le64(m->dqb_bsoftlimit);
-        dqblk->dqb_curspace = cpu_to_le64(m->dqb_curspace);
-        dqblk->dqb_btime = cpu_to_le64(m->dqb_btime);
-        dqblk->dqb_id = cpu_to_le32(id);
+        DQF_PUT(d, version, dqb_ihardlimit, m->dqb_ihardlimit);
+        DQF_PUT(d, version, dqb_isoftlimit, m->dqb_isoftlimit);
+        DQF_PUT(d, version, dqb_curinodes, m->dqb_curinodes);
+        DQF_PUT(d, version, dqb_itime, m->dqb_itime);
+        DQF_PUT(d, version, dqb_bhardlimit, m->dqb_bhardlimit);
+        DQF_PUT(d, version, dqb_bsoftlimit, m->dqb_bsoftlimit);
+        DQF_PUT(d, version, dqb_curspace, m->dqb_curspace);
+        DQF_PUT(d, version, dqb_btime, m->dqb_btime);
+        DQF_PUT(d, version, dqb_id, id);
 
         return 0;
 }
@@ -246,8 +253,7 @@ dqbuf_t getdqbuf(void)
 {
         dqbuf_t buf = kmalloc(LUSTRE_DQBLKSIZE, GFP_NOFS);
         if (!buf)
-                CDEBUG(D_WARNING, 
-                       "VFS: Not enough memory for quota buffers.\n");
+                CWARN("VFS: Not enough memory for quota buffers.\n");
         return buf;
 }
 
@@ -256,30 +262,29 @@ void freedqbuf(dqbuf_t buf)
         kfree(buf);
 }
 
-ssize_t read_blk(struct file *filp, uint blk, dqbuf_t buf)
+ssize_t read_blk(struct file *filp, struct inode *inode, int type,
+                 uint blk, dqbuf_t buf)
 {
-        mm_segment_t fs;
         ssize_t ret;
-        loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
 
         memset(buf, 0, LUSTRE_DQBLKSIZE);
-        fs = get_fs();
-        set_fs(KERNEL_DS);
-        ret = filp->f_op->read(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
-        set_fs(fs);
+        ret = lustre_read_quota(filp, inode, type, (char *)buf, LUSTRE_DQBLKSIZE,
+                                blk << LUSTRE_DQBLKSIZE_BITS);
+
+        /* Reading past EOF just returns a block of zeros */
+        if (ret == -EBADR)
+                ret = 0;
+
         return ret;
 }
 
 ssize_t write_blk(struct file *filp, uint blk, dqbuf_t buf)
 {
-        mm_segment_t fs;
         ssize_t ret;
-        loff_t offset = blk << LUSTRE_DQBLKSIZE_BITS;
 
-        fs = get_fs();
-        set_fs(KERNEL_DS);
-        ret = filp->f_op->write(filp, (char *)buf, LUSTRE_DQBLKSIZE, &offset);
-        set_fs(fs);
+        ret = lustre_write_quota(filp, (char *)buf, LUSTRE_DQBLKSIZE,
+                                 blk << LUSTRE_DQBLKSIZE_BITS);
+
         return ret;
 }
 
@@ -288,9 +293,7 @@ void lustre_mark_info_dirty(struct lustre_mem_dqinfo *info)
         set_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
 }
 
-/**
- * Remove empty block from list and return it
- */
+/* Remove empty block from list and return it */
 int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
 {
         dqbuf_t buf = getdqbuf();
@@ -302,7 +305,7 @@ int get_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info)
                 return -ENOMEM;
         if (info->dqi_free_blk) {
                 blk = info->dqi_free_blk;
-                if ((ret = read_blk(filp, blk, buf)) < 0)
+                if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0)
                         goto out_buf;
                 info->dqi_free_blk = le32_to_cpu(dh->dqdh_next_free);
         } else {
@@ -319,9 +322,7 @@ out_buf:
         return ret;
 }
 
-/**
- * Insert empty block to the list
- */
+/* Insert empty block to the list */
 int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
                    dqbuf_t buf, uint blk)
 {
@@ -340,9 +341,7 @@ int put_free_dqblk(struct file *filp, struct lustre_mem_dqinfo *info,
         return 0;
 }
 
-/**
- * Remove given block from the list of blocks with free entries
- */
+/* Remove given block from the list of blocks with free entries */
 int remove_free_dqentry(struct file *filp,
                         struct lustre_mem_dqinfo *info, dqbuf_t buf,
                         uint blk)
@@ -357,7 +356,7 @@ int remove_free_dqentry(struct file *filp,
         if (!tmpbuf)
                 return -ENOMEM;
         if (nextblk) {
-                if ((err = read_blk(filp, nextblk, tmpbuf)) < 0)
+                if ((err = read_blk(filp, NULL, 0, nextblk, tmpbuf)) < 0)
                         goto out_buf;
                 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
                     dh->dqdh_prev_free;
@@ -365,7 +364,7 @@ int remove_free_dqentry(struct file *filp,
                         goto out_buf;
         }
         if (prevblk) {
-                if ((err = read_blk(filp, prevblk, tmpbuf)) < 0)
+                if ((err = read_blk(filp, NULL, 0, prevblk, tmpbuf)) < 0)
                         goto out_buf;
                 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_next_free =
                     dh->dqdh_next_free;
@@ -377,19 +376,17 @@ int remove_free_dqentry(struct file *filp,
         }
         freedqbuf(tmpbuf);
         dh->dqdh_next_free = dh->dqdh_prev_free = cpu_to_le32(0);
-        if (write_blk(filp, blk, buf) < 0)
-                /* No matter whether write succeeds block is out of list */
-                CDEBUG(D_ERROR, 
-                       "VFS: Can't write block (%u) with free entries.\n", blk);
+        err = write_blk(filp, blk, buf);
+        if (err < 0)      /* No matter whether write succeeds block is out of list */
+                CERROR("VFS: Can't write block (%u) with "
+                       "free entries (rc=%d).\n", blk, err);
         return 0;
 out_buf:
         freedqbuf(tmpbuf);
         return err;
 }
 
-/**
- * Insert given block to the beginning of list with free entries
- */
+/* Insert given block to the beginning of list with free entries */
 int insert_free_dqentry(struct file *filp,
                         struct lustre_mem_dqinfo *info, dqbuf_t buf,
                         uint blk)
@@ -406,7 +403,7 @@ int insert_free_dqentry(struct file *filp,
         if ((err = write_blk(filp, blk, buf)) < 0)
                 goto out_buf;
         if (info->dqi_free_entry) {
-                if ((err = read_blk(filp, info->dqi_free_entry, tmpbuf)) < 0)
+                if ((err = read_blk(filp, NULL, 0, info->dqi_free_entry, tmpbuf)) < 0)
                         goto out_buf;
                 ((struct lustre_disk_dqdbheader *)tmpbuf)->dqdh_prev_free =
                     cpu_to_le32(blk);
@@ -424,9 +421,7 @@ out_buf:
 
 
 
-/**
- * Find space for dquot
- */
+/* Find space for dquot */
 static uint find_free_dqentry(struct lustre_dquot *dquot, int *err, 
                               lustre_quota_version_t version)
 {
@@ -449,7 +444,7 @@ static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
         ddquot = GETENTRIES(buf, version);
         if (info->dqi_free_entry) {
                 blk = info->dqi_free_entry;
-                if ((*err = read_blk(filp, blk, buf)) < 0)
+                if ((*err = read_blk(filp, NULL, 0, blk, buf)) < 0)
                         goto out_buf;
         } else {
                 blk = get_free_dqblk(filp, info);
@@ -468,30 +463,25 @@ static uint find_free_dqentry(struct lustre_dquot *dquot, int *err,
         /* Will block be full */
         if (le16_to_cpu(dh->dqdh_entries) + 1 >= dqstrinblk)
                 if ((*err = remove_free_dqentry(filp, info, buf, blk)) < 0) {
-                        CDEBUG(D_ERROR, 
-                               "VFS: find_free_dqentry(): Can't remove block "
-                               "(%u) from entry free list.\n", blk);
+                        CERROR("VFS: Can't remove block %u"
+                               " from entry free list.\n", blk);
                         goto out_buf;
                 }
         dh->dqdh_entries = cpu_to_le16(le16_to_cpu(dh->dqdh_entries) + 1);
         /* Find free structure in block */
         for (i = 0; i < dqstrinblk &&
              memcmp((char *)&emptydquot[version],
-                    (char *)ddquot + i * dqblk_sz, dqblk_sz);
-             i++);
+                    (char*)ddquot + i * dqblk_sz,
+                    dqblk_sz); i++);
 
         if (i == dqstrinblk) {
-                CDEBUG(D_ERROR, 
-                       "VFS: find_free_dqentry(): Data block full but it "
-                       "shouldn't.\n");
+                CERROR("VFS: Data block full but it shouldn't.\n");
                 *err = -EIO;
                 goto out_buf;
         }
 
         if ((*err = write_blk(filp, blk, buf)) < 0) {
-                CDEBUG(D_ERROR,
-                       "VFS: find_free_dqentry(): Can't write quota data "
-                       "block %u.\n", blk);
+                CERROR("VFS: Can't write quota data block %u.\n", blk);
                 goto out_buf;
         }
         dquot->dq_off =
@@ -505,10 +495,8 @@ out_buf:
         return 0;
 }
 
-/**
- * Insert reference to structure into the trie
- */
-static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
+/* Insert reference to structure into the trie */
+static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth, 
                           lustre_quota_version_t version)
 {
         struct lustre_quota_info *lqi = dquot->dq_info;
@@ -529,9 +517,8 @@ static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
                 memset(buf, 0, LUSTRE_DQBLKSIZE);
                 newact = 1;
         } else {
-                if ((ret = read_blk(filp, *treeblk, buf)) < 0) {
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't read tree quota block %u.\n",
+                if ((ret = read_blk(filp, NULL, 0, *treeblk, buf)) < 0) {
+                        CERROR("VFS: Can't read tree quota block %u.\n",
                                *treeblk);
                         goto out_buf;
                 }
@@ -543,9 +530,8 @@ static int do_insert_tree(struct lustre_dquot *dquot, uint * treeblk, int depth,
         if (depth == LUSTRE_DQTREEDEPTH - 1) {
 
                 if (newblk) {
-                        CDEBUG(D_ERROR, 
-                               "VFS: Inserting already present quota entry "
-                               "(block %u).\n",
+                        CERROR("VFS: Inserting already present quota entry "
+                               "(block %u).\n", 
                                ref[GETIDINDEX(dquot->dq_id, depth)]);
                         ret = -EIO;
                         goto out_buf;
@@ -564,9 +550,7 @@ out_buf:
         return ret;
 }
 
-/**
- * Wrapper for inserting quota structure into tree
- */
+/* Wrapper for inserting quota structure into tree */
 static inline int dq_insert_tree(struct lustre_dquot *dquot, 
                                  lustre_quota_version_t version)
 {
@@ -574,48 +558,42 @@ static inline int dq_insert_tree(struct lustre_dquot *dquot,
         return do_insert_tree(dquot, &tmp, 0, version);
 }
 
-/**
- * We don't have to be afraid of deadlocks as we never have quotas on
- * quota files...
+/*
+ *  We don't have to be afraid of deadlocks as we never have quotas on quota files...
  */
 static int lustre_write_dquot(struct lustre_dquot *dquot, 
                               lustre_quota_version_t version)
 {
         int type = dquot->dq_type;
         struct file *filp;
-        mm_segment_t fs;
         loff_t offset;
         ssize_t ret;
         int dqblk_sz = lustre_disk_dqblk_sz[version];
-        struct lustre_disk_dqblk_v2 ddquot;
+        char ddquot[sizeof(union lustre_disk_dqblk_un)];
 
-        ret = mem2diskdqb(&ddquot, &dquot->dq_dqb, dquot->dq_id, version);
+        ret = mem2diskdqb(ddquot, &dquot->dq_dqb, dquot->dq_id, version);
         if (ret < 0)
                 return ret;
 
         if (!dquot->dq_off)
                 if ((ret = dq_insert_tree(dquot, version)) < 0) {
-                        CDEBUG(D_ERROR,
-                               "VFS: Error %Zd occurred while creating "
-                               "quota.\n", ret);
+                        CERROR("VFS: Error %Zd occurred while creating quota.\n",
+                               ret);
                         return ret;
                 }
         filp = dquot->dq_info->qi_files[type];
         offset = dquot->dq_off;
-        /* Argh... We may need to write structure full of zeroes but that would
-         * be treated as an empty place by the rest of the code. Format change
-         * would be definitely cleaner but the problems probably are not worth
-         * it */
-        if (!memcmp((char *)&emptydquot[version], (char *)&ddquot, dqblk_sz))
-                ddquot.dqb_itime = cpu_to_le64(1);
-        fs = get_fs();
-        set_fs(KERNEL_DS);
-        ret = filp->f_op->write(filp, (char *)&ddquot,
-                                dqblk_sz, &offset);
-        set_fs(fs);
+        /* Argh... We may need to write structure full of zeroes but that would be
+         * treated as an empty place by the rest of the code. Format change would
+         * be definitely cleaner but the problems probably are not worth it */
+        if (!memcmp((char *)&emptydquot[version], ddquot, dqblk_sz))
+                DQF_PUT(ddquot, version, dqb_itime, 1);
+
+        ret = lustre_write_quota(filp, ddquot,
+                                 dqblk_sz, offset);
         if (ret != dqblk_sz) {
-                CDEBUG(D_WARNING, "VFS: dquota write failed on dev %s\n",
-                       filp->f_dentry->d_sb->s_id);
+                CWARN("VFS: dquota write failed on dev %s\n",
+                      filp->f_dentry->d_sb->s_id);
                 if (ret >= 0)
                         ret = -ENOSPC;
         } else
@@ -624,9 +602,7 @@ static int lustre_write_dquot(struct lustre_dquot *dquot,
         return ret;
 }
 
-/**
- * Free dquot entry in data block
- */
+/* Free dquot entry in data block */
 static int free_dqentry(struct lustre_dquot *dquot, uint blk, 
                         lustre_quota_version_t version)
 {
@@ -641,14 +617,13 @@ static int free_dqentry(struct lustre_dquot *dquot, uint blk,
         if (!buf)
                 return -ENOMEM;
         if (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS != blk) {
-                CDEBUG(D_ERROR,
-                       "VFS: Quota structure has offset to other block (%u) "
-                       "than it should (%u).\n",
-                       blk, (uint) (dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
+                CERROR("VFS: Quota structure has offset to other block (%u) "
+                       "than it should (%u).\n", blk, 
+                       (uint)(dquot->dq_off >> LUSTRE_DQBLKSIZE_BITS));
                 goto out_buf;
         }
-        if ((ret = read_blk(filp, blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", blk);
+        if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota data block %u\n", blk);
                 goto out_buf;
         }
         dh = (struct lustre_disk_dqdbheader *)buf;
@@ -656,9 +631,8 @@ static int free_dqentry(struct lustre_dquot *dquot, uint blk,
         if (!le16_to_cpu(dh->dqdh_entries)) {   /* Block got free? */
                 if ((ret = remove_free_dqentry(filp, info, buf, blk)) < 0 ||
                     (ret = put_free_dqblk(filp, info, buf, blk)) < 0) {
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't move quota data block (%u) to free "
-                               "list.\n", blk);
+                        CERROR("VFS: Can't move quota data block (%u) "
+                               "to free list.\n", blk);
                         goto out_buf;
                 }
         } else {
@@ -668,14 +642,12 @@ static int free_dqentry(struct lustre_dquot *dquot, uint blk,
                         /* Insert will write block itself */
                         if ((ret =
                              insert_free_dqentry(filp, info, buf, blk)) < 0) {
-                                CDEBUG(D_ERROR,
-                                       "VFS: Can't insert quota data block "
-                                       "(%u) to free entry list.\n", blk);
+                                CERROR("VFS: Can't insert quota data block (%u) "
+                                       "to free entry list.\n", blk);
                                 goto out_buf;
                         }
                 } else if ((ret = write_blk(filp, blk, buf)) < 0) {
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't write quota data block %u\n", blk);
+                        CERROR("VFS: Can't write quota data block %u\n", blk);
                         goto out_buf;
                 }
         }
@@ -685,9 +657,7 @@ out_buf:
         return ret;
 }
 
-/**
- * Remove reference to dquot from tree
- */
+/* Remove reference to dquot from tree */
 static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth, 
                        lustre_quota_version_t version)
 {
@@ -701,8 +671,8 @@ static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
 
         if (!buf)
                 return -ENOMEM;
-        if ((ret = read_blk(filp, *blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota data block %u\n", *blk);
+        if ((ret = read_blk(filp, NULL, 0, *blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota data block %u\n", *blk);
                 goto out_buf;
         }
         newblk = le32_to_cpu(ref[GETIDINDEX(dquot->dq_id, depth)]);
@@ -721,17 +691,14 @@ static int remove_tree(struct lustre_dquot *dquot, uint * blk, int depth,
                         put_free_dqblk(filp, info, buf, *blk);
                         *blk = 0;
                 } else if ((ret = write_blk(filp, *blk, buf)) < 0)
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't write quota tree block %u.\n", *blk);
+                        CERROR("VFS: Can't write quota tree block %u.\n", *blk);
         }
 out_buf:
         freedqbuf(buf);
         return ret;
 }
 
-/**
- * Delete dquot from tree
- */
+/* Delete dquot from tree */
 static int lustre_delete_dquot(struct lustre_dquot *dquot, 
                                 lustre_quota_version_t version)
 {
@@ -742,9 +709,7 @@ static int lustre_delete_dquot(struct lustre_dquot *dquot,
         return remove_tree(dquot, &tmp, 0, version);
 }
 
-/**
- * Find entry in block
- */
+/* Find entry in block */
 static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk, 
                                  lustre_quota_version_t version)
 {
@@ -752,33 +717,30 @@ static loff_t find_block_dqentry(struct lustre_dquot *dquot, uint blk,
         dqbuf_t buf = getdqbuf();
         loff_t ret = 0;
         int i;
-        struct lustre_disk_dqblk_v2 *ddquot =
-                (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
+        char *ddquot = GETENTRIES(buf, version);
         int dqblk_sz = lustre_disk_dqblk_sz[version];
         int dqstrinblk = lustre_dqstrinblk[version];
 
-        LASSERT(version == LUSTRE_QUOTA_V2);
-
         if (!buf)
                 return -ENOMEM;
-        if ((ret = read_blk(filp, blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
+        if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota tree block %u.\n", blk);
                 goto out_buf;
         }
         if (dquot->dq_id)
                 for (i = 0; i < dqstrinblk && 
-                     le32_to_cpu(ddquot[i].dqb_id) != dquot->dq_id;
+                     DQF_GET(ddquot+i*dqblk_sz, version, dqb_id) != dquot->dq_id;
                      i++) ;
         else {                  /* ID 0 as a bit more complicated searching... */
                 for (i = 0; i < dqstrinblk; i++)
-                        if (!le32_to_cpu(ddquot[i].dqb_id)
+                        if (!DQF_GET(ddquot + i*dqblk_sz, version, dqb_id)
                             && memcmp((char *)&emptydquot[version],
-                                      (char *)&ddquot[i], dqblk_sz))
+                                      ddquot + i*dqblk_sz,
+                                      dqblk_sz))
                                 break;
         }
         if (i == dqstrinblk) {
-                CDEBUG(D_ERROR,
-                       "VFS: Quota for id %u referenced but not present.\n",
+                CERROR("VFS: Quota for id %u referenced but not present.\n",
                        dquot->dq_id);
                 ret = -EIO;
                 goto out_buf;
@@ -792,10 +754,8 @@ out_buf:
         return ret;
 }
 
-/**
- * Find entry for given id in the tree
- */
-static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
+/* Find entry for given id in the tree */
+static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth, 
                                 lustre_quota_version_t version)
 {
         struct file *filp = dquot->dq_info->qi_files[dquot->dq_type];
@@ -805,8 +765,8 @@ static loff_t find_tree_dqentry(struct lustre_dquot *dquot, uint blk, int depth,
 
         if (!buf)
                 return -ENOMEM;
-        if ((ret = read_blk(filp, blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
+        if ((ret = read_blk(filp, NULL, 0, blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota tree block %u.\n", blk);
                 goto out_buf;
         }
         ret = 0;
@@ -822,80 +782,71 @@ out_buf:
         return ret;
 }
 
-/**
- * Find entry for given id in the tree - wrapper function
- */
+/* Find entry for given id in the tree - wrapper function */
 static inline loff_t find_dqentry(struct lustre_dquot *dquot, 
                                   lustre_quota_version_t version)
 {
         return find_tree_dqentry(dquot, LUSTRE_DQTREEOFF, 0, version);
 }
 
+
 int lustre_read_dquot(struct lustre_dquot *dquot)
 {
         int type = dquot->dq_type;
         struct file *filp;
-        mm_segment_t fs;
         loff_t offset;
         int ret = 0, dqblk_sz;
         lustre_quota_version_t version;
 
         /* Invalidated quota? */
         if (!dquot->dq_info || !(filp = dquot->dq_info->qi_files[type])) {
-                CDEBUG(D_ERROR, "VFS: Quota invalidated while reading!\n");
+                CERROR("VFS: Quota invalidated while reading!\n");
                 return -EIO;
         }
 
         version = dquot->dq_info->qi_version;
-        LASSERT(version == LUSTRE_QUOTA_V2);
         dqblk_sz = lustre_disk_dqblk_sz[version];
 
         offset = find_dqentry(dquot, version);
         if (offset <= 0) {      /* Entry not present? */
                 if (offset < 0)
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't read quota structure for id %u.\n",
+                        CERROR("VFS: Can't read quota structure for id %u.\n",
                                dquot->dq_id);
                 dquot->dq_off = 0;
                 set_bit(DQ_FAKE_B, &dquot->dq_flags);
                 memset(&dquot->dq_dqb, 0, sizeof(struct lustre_mem_dqblk));
                 ret = offset;
         } else {
-                struct lustre_disk_dqblk_v2 ddquot;
+                char ddquot[sizeof(union lustre_disk_dqblk_un)];
 
                 dquot->dq_off = offset;
-                fs = get_fs();
-                set_fs(KERNEL_DS);
-                if ((ret = filp->f_op->read(filp, (char *)&ddquot,
-                                            dqblk_sz, &offset)) != dqblk_sz) {
+                if ((ret = lustre_read_quota(filp, NULL, type, ddquot,
+                                             dqblk_sz, offset)) != dqblk_sz) {
                         if (ret >= 0)
                                 ret = -EIO;
-                        CDEBUG(D_ERROR,
-                               "VFS: Error while reading quota structure for id "
-                               "%u.\n", dquot->dq_id);
-                        memset((char *)&ddquot, 0, dqblk_sz);
+                        CERROR("VFS: Error while reading quota structure "
+                               "for id %u.\n", dquot->dq_id);
+                        memset(ddquot, 0, dqblk_sz);
                 } else {
                         ret = 0;
                         /* We need to escape back all-zero structure */
                         if (!memcmp((char *)&fakedquot[version],
-                                    (char *)&ddquot, dqblk_sz))
-                                ddquot.dqb_itime = cpu_to_le64(0);
+                                    ddquot, dqblk_sz))
+                                DQF_PUT(ddquot, version, dqb_itime, 0);
                 }
-                set_fs(fs);
-                disk2memdqb(&dquot->dq_dqb, &ddquot, version);
+                disk2memdqb(&dquot->dq_dqb, ddquot, version);
         }
 
         return ret;
 }
 
-/**
- * Commit changes of dquot to disk - it might also mean deleting
- * it when quota became fake.
- */
+/* Commit changes of dquot to disk - it might also mean deleting it when quota became fake */
 int lustre_commit_dquot(struct lustre_dquot *dquot)
 {
         int rc = 0;
         lustre_quota_version_t version = dquot->dq_info->qi_version;
+        void *handle;
+        struct inode *inode = dquot->dq_info->qi_files[dquot->dq_type]->f_dentry->d_inode;
 
         /* always clear the flag so we don't loop on an IO error... */
         clear_bit(DQ_MOD_B, &dquot->dq_flags);
@@ -903,10 +854,15 @@ int lustre_commit_dquot(struct lustre_dquot *dquot)
         /* The block/inode usage in admin quotafile isn't the real usage
          * over all cluster, so keep the fake dquot entry on disk is
          * meaningless, just remove it */
-        if (test_bit(DQ_FAKE_B, &dquot->dq_flags))
+        if (test_bit(DQ_FAKE_B, &dquot->dq_flags)) {
+                handle = lustre_quota_journal_start(inode, 1);
                 rc = lustre_delete_dquot(dquot, version);
-        else
+                lustre_quota_journal_stop(handle);
+        } else {
+                handle = lustre_quota_journal_start(inode, 0);
                 rc = lustre_write_dquot(dquot, version);
+                lustre_quota_journal_stop(handle);
+        }
 
         if (rc < 0)
                 return rc;
@@ -917,15 +873,13 @@ int lustre_commit_dquot(struct lustre_dquot *dquot)
         return rc;
 }
 
-int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
-                             int fakemagics)
+int lustre_init_quota_header(struct lustre_quota_info *lqi, int type, int fakemagics)
 {
         static const uint quota_magics[] = LUSTRE_INITQMAGICS;
         static const uint fake_magics[] = LUSTRE_BADQMAGICS;
         const uint* quota_versions = lustre_initqversions[lqi->qi_version];
         struct lustre_disk_dqheader dqhead;
         ssize_t size;
-        loff_t offset = 0;
         struct file *fp = lqi->qi_files[type];
         int rc = 0;
 
@@ -933,21 +887,19 @@ int lustre_init_quota_header(struct lustre_quota_info *lqi, int type,
         dqhead.dqh_magic = cpu_to_le32(fakemagics ? 
                                        fake_magics[type] : quota_magics[type]);
         dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
-        size = fp->f_op->write(fp, (char *)&dqhead,
-                               sizeof(struct lustre_disk_dqheader), &offset);
+        size = lustre_write_quota(fp, (char *)&dqhead,
+                                  sizeof(struct lustre_disk_dqheader), 0);
 
         if (size != sizeof(struct lustre_disk_dqheader)) {
-                CDEBUG(D_ERROR, "error writing quoafile header (rc:%d)\n", rc);
+                CERROR("error writing quotafile header (rc:%d)\n", rc);
                 rc = size;
         }
 
         return rc;
 }
 
-/**
- * We need to export this function to initialize quotafile, because we haven't
- * user level check utility
- */
+/* We need to export this function to initialize quotafile, because we haven't
+ * user level check utility */
 int lustre_init_quota_info_generic(struct lustre_quota_info *lqi, int type,
                                    int fakemagics)
 {
@@ -972,24 +924,6 @@ int lustre_init_quota_info(struct lustre_quota_info *lqi, int type)
         return lustre_init_quota_info_generic(lqi, type, 0);
 }
 
-ssize_t quota_read(struct file *file, struct inode *inode, int type,
-                   uint blk, dqbuf_t buf)
-{
-        if (file) {
-                return read_blk(file, blk, buf);
-        } else {
-#ifndef KERNEL_SUPPORTS_QUOTA_READ
-                return -ENOTSUPP;
-#else
-                struct super_block *sb = inode->i_sb;
-                memset(buf, 0, LUSTRE_DQBLKSIZE);
-                return sb->s_op->quota_read(sb, type, (char *)buf,
-                                            LUSTRE_DQBLKSIZE, 
-                                            blk << LUSTRE_DQBLKSIZE_BITS);
-#endif
-        }
-}
-
 static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
                               uint blk, struct list_head *list)
 {
@@ -1003,8 +937,8 @@ static int walk_block_dqentry(struct file *filp, struct inode *inode, int type,
 
         if (!buf)
                 return -ENOMEM;
-        if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
+        if ((ret = read_blk(filp, inode, type, blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota tree block %u.\n", blk);
                 goto out_buf;
         }
         ret = 0;
@@ -1051,8 +985,8 @@ int walk_tree_dqentry(struct file *filp, struct inode *inode, int type,
 
         if (!buf)
                 return -ENOMEM;
-        if ((ret = quota_read(filp, inode, type, blk, buf)) < 0) {
-                CDEBUG(D_ERROR, "VFS: Can't read quota tree block %u.\n", blk);
+        if ((ret = read_blk(filp, inode, type, blk, buf)) < 0) {
+                CERROR("VFS: Can't read quota tree block %u.\n", blk);
                 goto out_buf;
         }
         ret = 0;
@@ -1073,16 +1007,14 @@ out_buf:
         return ret;
 }
 
-/**
- * Walk through the quota file (v2 format) to get all ids with quota limit
- */
+/* Walk through the quota file (v2 format) to get all ids with quota limit */
 int lustre_get_qids(struct file *fp, struct inode *inode, int type,
                     struct list_head *list)
 {
         struct list_head blk_list;
         struct dqblk *blk_item, *tmp;
         dqbuf_t buf = NULL;
-        struct lustre_disk_dqblk_v2 *ddquot;
+        char *ddquot;
         int rc;
         lustre_quota_version_t version;
 
@@ -1090,22 +1022,24 @@ int lustre_get_qids(struct file *fp, struct inode *inode, int type,
 
         LASSERT(ergo(fp == NULL, inode != NULL));
 
-        if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
+        if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V1) == 0)
+                version = LUSTRE_QUOTA_V1;
+        else if (check_quota_file(fp, inode, type, LUSTRE_QUOTA_V2) == 0)
                 version = LUSTRE_QUOTA_V2;
         else {
-                CDEBUG(D_ERROR, "unknown quota file format!\n");
+                CERROR("unknown quota file format!\n");
                 RETURN(-EINVAL);
         }
 
         if (!list_empty(list)) {
-                CDEBUG(D_ERROR, "not empty list\n");
+                CERROR("not empty list\n");
                 RETURN(-EINVAL);
         }
 
         INIT_LIST_HEAD(&blk_list);
         rc = walk_tree_dqentry(fp, inode, type, LUSTRE_DQTREEOFF, 0, &blk_list);
         if (rc) {
-                CDEBUG(D_ERROR, "walk through quota file failed!(%d)\n", rc);
+                CERROR("walk through quota file failed!(%d)\n", rc);
                 GOTO(out_free, rc);
         }
         if (list_empty(&blk_list))
@@ -1114,16 +1048,15 @@ int lustre_get_qids(struct file *fp, struct inode *inode, int type,
         buf = getdqbuf();
         if (!buf)
                 RETURN(-ENOMEM);
-        ddquot = (struct lustre_disk_dqblk_v2 *)GETENTRIES(buf, version);
+        ddquot = GETENTRIES(buf, version);
 
         list_for_each_entry(blk_item, &blk_list, link) {
                 loff_t ret = 0;
                 int i, dqblk_sz = lustre_disk_dqblk_sz[version];
 
                 memset(buf, 0, LUSTRE_DQBLKSIZE);
-                if ((ret = quota_read(fp, inode, type, blk_item->blk, buf))<0) {
-                        CDEBUG(D_ERROR,
-                               "VFS: Can't read quota tree block %u.\n",
+                if ((ret = read_blk(fp, inode, type, blk_item->blk, buf)) < 0) {
+                        CERROR("VFS: Can't read quota tree block %u.\n",
                                blk_item->blk);
                         GOTO(out_free, rc = ret);
                 }
@@ -1132,19 +1065,19 @@ int lustre_get_qids(struct file *fp, struct inode *inode, int type,
                         struct dquot_id *dqid;
                         /* skip empty entry */
                         if (!memcmp((char *)&emptydquot[version],
-                                    (char *)&ddquot[i], dqblk_sz))
+                                    ddquot + i*dqblk_sz, dqblk_sz))
                                 continue;
 
                         OBD_ALLOC_GFP(dqid, sizeof(*dqid), GFP_NOFS);
                         if (!dqid)
                                 GOTO(out_free, rc = -ENOMEM);
 
-                        dqid->di_id    = le32_to_cpu(ddquot[i].dqb_id);
-                        dqid->di_flag  = le64_to_cpu(ddquot[i].dqb_ihardlimit) ?
-                                         QI_SET : 0;
-                        dqid->di_flag |= le64_to_cpu(ddquot[i].dqb_bhardlimit) ?
-                                         QB_SET : 0;
-
+                        dqid->di_id = DQF_GET(ddquot + i * dqblk_sz,
+                                              version, dqb_id);
+                        dqid->di_flag = DQF_GET(ddquot + i * dqblk_sz, version,
+                                                dqb_ihardlimit) ? QI_SET : 0;
+                        dqid->di_flag |= DQF_GET(ddquot + i * dqblk_sz, version,
+                                                 dqb_bhardlimit) ? QB_SET : 0;
                         INIT_LIST_HEAD(&dqid->di_link);
                         list_add(&dqid->di_link, list);
                 }