[GFS2] Fix warning on impossible event in eattr code
[powerpc.git] / fs / gfs2 / quota.c
index a0320f2..f752b01 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
- * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
  *
  * This copyrighted material is made available to anyone wishing to use,
  * modify, copy, or redistribute it subject to the terms and conditions
 #include <linux/buffer_head.h>
 #include <linux/tty.h>
 #include <linux/sort.h>
-#include <asm/semaphore.h>
+#include <linux/fs.h>
+#include <linux/gfs2_ondisk.h>
 
 #include "gfs2.h"
+#include "lm_interface.h"
+#include "incore.h"
 #include "bmap.h"
 #include "glock.h"
 #include "glops.h"
-#include "jdata.h"
 #include "log.h"
+#include "lvb.h"
 #include "meta_io.h"
 #include "quota.h"
 #include "rgrp.h"
 #include "super.h"
 #include "trans.h"
+#include "inode.h"
+#include "ops_file.h"
+#include "ops_address.h"
+#include "util.h"
 
 #define QUOTA_USER 1
 #define QUOTA_GROUP 0
@@ -241,24 +248,25 @@ static void slot_put(struct gfs2_quota_data *qd)
 static int bh_get(struct gfs2_quota_data *qd)
 {
        struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
-       struct gfs2_inode *ip = sdp->sd_qc_inode;
+       struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
        unsigned int block, offset;
        uint64_t dblock;
        int new = 0;
        struct buffer_head *bh;
        int error;
+       int boundary;
 
-       down(&sdp->sd_quota_mutex);
+       mutex_lock(&sdp->sd_quota_mutex);
 
        if (qd->qd_bh_count++) {
-               up(&sdp->sd_quota_mutex);
+               mutex_unlock(&sdp->sd_quota_mutex);
                return 0;
        }
 
        block = qd->qd_slot / sdp->sd_qc_per_block;
        offset = qd->qd_slot % sdp->sd_qc_per_block;;
 
-       error = gfs2_block_map(ip, block, &new, &dblock, NULL);
+       error = gfs2_block_map(ip->i_vnode, block, &new, &dblock, &boundary);
        if (error)
                goto fail;
        error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
@@ -273,7 +281,7 @@ static int bh_get(struct gfs2_quota_data *qd)
                (bh->b_data + sizeof(struct gfs2_meta_header) +
                 offset * sizeof(struct gfs2_quota_change));
 
-       up(&sdp->sd_quota_mutex);
+       mutex_lock(&sdp->sd_quota_mutex);
 
        return 0;
 
@@ -282,7 +290,7 @@ static int bh_get(struct gfs2_quota_data *qd)
 
  fail:
        qd->qd_bh_count--;
-       up(&sdp->sd_quota_mutex);
+       mutex_unlock(&sdp->sd_quota_mutex);
        return error;
 }
 
@@ -290,14 +298,14 @@ static void bh_put(struct gfs2_quota_data *qd)
 {
        struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
 
-       down(&sdp->sd_quota_mutex);
+       mutex_lock(&sdp->sd_quota_mutex);
        gfs2_assert(sdp, qd->qd_bh_count);
        if (!--qd->qd_bh_count) {
                brelse(qd->qd_bh);
                qd->qd_bh = NULL;
                qd->qd_bh_qc = NULL;
        }
-       up(&sdp->sd_quota_mutex);
+       mutex_unlock(&sdp->sd_quota_mutex);
 }
 
 static int qd_fish(struct gfs2_sbd *sdp, struct gfs2_quota_data **qdp)
@@ -392,7 +400,8 @@ static int qd_trylock(struct gfs2_quota_data *qd)
 
 static void qd_unlock(struct gfs2_quota_data *qd)
 {
-       gfs2_assert_warn(qd->qd_gl->gl_sbd, test_bit(QDF_LOCKED, &qd->qd_flags));
+       gfs2_assert_warn(qd->qd_gl->gl_sbd,
+                        test_bit(QDF_LOCKED, &qd->qd_flags));
        clear_bit(QDF_LOCKED, &qd->qd_flags);
        bh_put(qd);
        slot_put(qd);
@@ -522,12 +531,12 @@ static int sort_qd(const void *a, const void *b)
 static void do_qc(struct gfs2_quota_data *qd, int64_t change)
 {
        struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
-       struct gfs2_inode *ip = sdp->sd_qc_inode;
+       struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
        struct gfs2_quota_change *qc = qd->qd_bh_qc;
        int64_t x;
 
-       down(&sdp->sd_quota_mutex);
-       gfs2_trans_add_bh(ip->i_gl, qd->qd_bh);
+       mutex_lock(&sdp->sd_quota_mutex);
+       gfs2_trans_add_bh(ip->i_gl, qd->qd_bh, 1);
 
        if (!test_bit(QDF_CHANGE, &qd->qd_flags)) {
                qc->qc_change = 0;
@@ -557,18 +566,94 @@ static void do_qc(struct gfs2_quota_data *qd, int64_t change)
                slot_hold(qd);
        }
                        
-       up(&sdp->sd_quota_mutex);
+       mutex_unlock(&sdp->sd_quota_mutex);
+}
+
+/**
+ * gfs2_adjust_quota
+ *
+ * This function was mostly borrowed from gfs2_block_truncate_page which was
+ * in turn mostly borrowed from ext3
+ */
+static int gfs2_adjust_quota(struct gfs2_inode *ip, loff_t loc,
+                            int64_t change, struct gfs2_quota_data *qd)
+{
+       struct inode *inode = ip->i_vnode;
+       struct address_space *mapping = inode->i_mapping;
+       unsigned long index = loc >> PAGE_CACHE_SHIFT;
+       unsigned offset = loc & (PAGE_CACHE_SHIFT - 1);
+       unsigned blocksize, iblock, pos;
+       struct buffer_head *bh;
+       struct page *page;
+       void *kaddr;
+       __be64 *ptr;
+       u64 value;
+       int err = -EIO;
+
+       page = grab_cache_page(mapping, index);
+       if (!page)
+               return -ENOMEM;
+
+       blocksize = inode->i_sb->s_blocksize;
+       iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
+
+       if (!page_has_buffers(page))
+               create_empty_buffers(page, blocksize, 0);
+
+       bh = page_buffers(page);
+       pos = blocksize;
+       while (offset >= pos) {
+               bh = bh->b_this_page;
+               iblock++;
+               pos += blocksize;
+       }
+
+       if (!buffer_mapped(bh)) {
+               gfs2_get_block(inode, iblock, bh, 1);
+               if (!buffer_mapped(bh))
+                       goto unlock;
+       }
+
+       if (PageUptodate(page))
+               set_buffer_uptodate(bh);
+
+       if (!buffer_uptodate(bh)) {
+               ll_rw_block(READ, 1, &bh);
+               wait_on_buffer(bh);
+               if (!buffer_uptodate(bh))
+                       goto unlock;
+       }
+
+       gfs2_trans_add_bh(ip->i_gl, bh, 0);
+
+       kaddr = kmap_atomic(page, KM_USER0);
+       ptr = (__be64 *)(kaddr + offset);
+       value = *ptr = cpu_to_be64(be64_to_cpu(*ptr) + change);
+       flush_dcache_page(page);
+       kunmap_atomic(kaddr, KM_USER0);
+       err = 0;
+       qd->qd_qb.qb_magic = cpu_to_be32(GFS2_MAGIC);
+#if 0
+       qd->qd_qb.qb_limit = cpu_to_be64(q.qu_limit);
+       qd->qd_qb.qb_warn = cpu_to_be64(q.qu_warn);
+#endif
+       qd->qd_qb.qb_value = cpu_to_be64(value);
+unlock:
+       unlock_page(page);
+       page_cache_release(page);
+       return err;
 }
 
 static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
 {
        struct gfs2_sbd *sdp = (*qda)->qd_gl->gl_sbd;
-       struct gfs2_inode *ip = sdp->sd_quota_inode;
+       struct gfs2_inode *ip = sdp->sd_quota_inode->u.generic_ip;
        unsigned int data_blocks, ind_blocks;
+       struct file_ra_state ra_state;
        struct gfs2_holder *ghs, i_gh;
        unsigned int qx, x;
        struct gfs2_quota_data *qd;
-       uint64_t offset;
+       loff_t offset;
        unsigned int nalloc = 0;
        struct gfs2_alloc *al = NULL;
        int error;
@@ -631,44 +716,17 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
                        goto out_gunlock;
        }
 
+       file_ra_state_init(&ra_state, ip->i_vnode->i_mapping);
        for (x = 0; x < num_qd; x++) {
-               char buf[sizeof(struct gfs2_quota)];
-               struct gfs2_quota q;
-
                qd = qda[x];
                offset = qd2offset(qd);
-
-               /* The quota file may not be a multiple of
-                  sizeof(struct gfs2_quota) bytes. */
-               memset(buf, 0, sizeof(struct gfs2_quota));
-
-               error = gfs2_jdata_read_mem(ip, buf, offset,
-                                           sizeof(struct gfs2_quota));
-               if (error < 0)
-                       goto out_end_trans;
-
-               gfs2_quota_in(&q, buf);
-               q.qu_value += qda[x]->qd_change_sync;
-               gfs2_quota_out(&q, buf);
-
-               error = gfs2_jdata_write_mem(ip, buf, offset,
-                                            sizeof(struct gfs2_quota));
-               if (error < 0)
-                       goto out_end_trans;
-               else if (error != sizeof(struct gfs2_quota)) {
-                       error = -EIO;
+               error = gfs2_adjust_quota(ip, offset, qd->qd_change_sync,
+                                         (struct gfs2_quota_data *)
+                                         qd->qd_gl->gl_lvb);
+               if (error)
                        goto out_end_trans;
-               }
 
                do_qc(qd, -qd->qd_change_sync);
-
-               memset(&qd->qd_qb, 0, sizeof(struct gfs2_quota_lvb));
-               qd->qd_qb.qb_magic = GFS2_MAGIC;
-               qd->qd_qb.qb_limit = q.qu_limit;
-               qd->qd_qb.qb_warn = q.qu_warn;
-               qd->qd_qb.qb_value = q.qu_value;
-
-               gfs2_quota_lvb_out(&qd->qd_qb, qd->qd_gl->gl_lvb);
        }
 
        error = 0;
@@ -691,7 +749,7 @@ static int do_sync(unsigned int num_qd, struct gfs2_quota_data **qda)
        while (qx--)
                gfs2_glock_dq_uninit(&ghs[qx]);
        kfree(ghs);
-       gfs2_log_flush_glock(ip->i_gl);
+       gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl);
 
        return error;
 }
@@ -700,11 +758,14 @@ static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
                    struct gfs2_holder *q_gh)
 {
        struct gfs2_sbd *sdp = qd->qd_gl->gl_sbd;
+       struct gfs2_inode *ip = sdp->sd_quota_inode->u.generic_ip;
        struct gfs2_holder i_gh;
        struct gfs2_quota q;
        char buf[sizeof(struct gfs2_quota)];
+       struct file_ra_state ra_state;
        int error;
 
+       file_ra_state_init(&ra_state, sdp->sd_quota_inode->i_mapping);
  restart:
        error = gfs2_glock_nq_init(qd->qd_gl, LM_ST_SHARED, 0, q_gh);
        if (error)
@@ -713,6 +774,7 @@ static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
        gfs2_quota_lvb_in(&qd->qd_qb, qd->qd_gl->gl_lvb);
 
        if (force_refresh || qd->qd_qb.qb_magic != GFS2_MAGIC) {
+               loff_t pos;
                gfs2_glock_dq_uninit(q_gh);
                error = gfs2_glock_nq_init(qd->qd_gl,
                                          LM_ST_EXCLUSIVE, GL_NOCACHE,
@@ -720,16 +782,17 @@ static int do_glock(struct gfs2_quota_data *qd, int force_refresh,
                if (error)
                        return error;
 
-               error = gfs2_glock_nq_init(sdp->sd_quota_inode->i_gl,
+               error = gfs2_glock_nq_init(ip->i_gl,
                                          LM_ST_SHARED, 0,
                                          &i_gh);
                if (error)
                        goto fail;
 
                memset(buf, 0, sizeof(struct gfs2_quota));
-
-               error = gfs2_jdata_read_mem(sdp->sd_quota_inode, buf,
-                                           qd2offset(qd),
+               pos = qd2offset(qd);
+               error = gfs2_internal_read(ip,
+                                           &ra_state, buf,
+                                           &pos,
                                            sizeof(struct gfs2_quota));
                if (error < 0)
                        goto fail_gunlock;
@@ -877,7 +940,8 @@ static int print_message(struct gfs2_quota_data *qd, char *type)
        if (!line)
                return -ENOMEM;
 
-       len = snprintf(line, MAX_LINE-1, "GFS2: fsid=%s: quota %s for %s %u\r\n",
+       len = snprintf(line, MAX_LINE-1,
+                      "GFS2: fsid=%s: quota %s for %s %u\r\n",
                       sdp->sd_fsname, type,
                       (test_bit(QDF_USER, &qd->qd_flags)) ? "user" : "group",
                       qd->qd_id);
@@ -926,7 +990,8 @@ int gfs2_quota_check(struct gfs2_inode *ip, uint32_t uid, uint32_t gid)
                } else if (qd->qd_qb.qb_warn &&
                           (int64_t)qd->qd_qb.qb_warn < value &&
                           time_after_eq(jiffies, qd->qd_last_warn +
-                                        gfs2_tune_get(sdp, gt_quota_warn_period) * HZ)) {
+                                        gfs2_tune_get(sdp,
+                                               gt_quota_warn_period) * HZ)) {
                        error = print_message(qd, "warning");
                        qd->qd_last_warn = jiffies;
                }
@@ -1021,6 +1086,7 @@ int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, uint32_t id)
        return error;
 }
 
+#if 0
 int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
                    struct gfs2_quota *q)
 {
@@ -1056,10 +1122,11 @@ int gfs2_quota_read(struct gfs2_sbd *sdp, int user, uint32_t id,
 
        return error;
 }
+#endif  /*  0  */
 
 int gfs2_quota_init(struct gfs2_sbd *sdp)
 {
-       struct gfs2_inode *ip = sdp->sd_qc_inode;
+       struct gfs2_inode *ip = sdp->sd_qc_inode->u.generic_ip;
        unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
        unsigned int x, slot = 0;
        unsigned int found = 0;
@@ -1074,7 +1141,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
                return -EIO;            
        }
        sdp->sd_quota_slots = blocks * sdp->sd_qc_per_block;
-       sdp->sd_quota_chunks = DIV_RU(sdp->sd_quota_slots, 8 * PAGE_SIZE);
+       sdp->sd_quota_chunks = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * PAGE_SIZE);
 
        error = -ENOMEM;
 
@@ -1095,7 +1162,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 
                if (!extlen) {
                        int new = 0;
-                       error = gfs2_block_map(ip, x, &new, &dblock, &extlen);
+                       error = gfs2_extent_map(ip->i_vnode, x, &new, &dblock, &extlen);
                        if (error)
                                goto fail;
                }