+#ifdef HAVE_QUOTA_SUPPORT
+#define DQINFO_COPY(out, in) \
+do { \
+ Q_COPY(out, in, dqi_bgrace); \
+ Q_COPY(out, in, dqi_igrace); \
+ Q_COPY(out, in, dqi_flags); \
+ Q_COPY(out, in, dqi_valid); \
+} while (0)
+
+#define DQBLK_COPY(out, in) \
+do { \
+ Q_COPY(out, in, dqb_bhardlimit); \
+ Q_COPY(out, in, dqb_bsoftlimit); \
+ Q_COPY(out, in, dqb_curspace); \
+ Q_COPY(out, in, dqb_ihardlimit); \
+ Q_COPY(out, in, dqb_isoftlimit); \
+ Q_COPY(out, in, dqb_curinodes); \
+ Q_COPY(out, in, dqb_btime); \
+ Q_COPY(out, in, dqb_itime); \
+ Q_COPY(out, in, dqb_valid); \
+} while (0)
+
+static int fsfilt_ext3_quotactl(struct super_block *sb,
+ struct obd_quotactl *oqc)
+{
+ int i, rc = 0, error = 0;
+ const struct quotactl_ops *qcop;
+ struct if_dqinfo *info;
+ struct if_dqblk *dqblk;
+ ENTRY;
+
+ if (!sb->s_qcop)
+ RETURN(-ENOSYS);
+
+ OBD_ALLOC_PTR(info);
+ if (!info)
+ RETURN(-ENOMEM);
+ OBD_ALLOC_PTR(dqblk);
+ if (!dqblk) {
+ OBD_FREE_PTR(info);
+ RETURN(-ENOMEM);
+ }
+
+ DQINFO_COPY(info, &oqc->qc_dqinfo);
+ DQBLK_COPY(dqblk, &oqc->qc_dqblk);
+
+ qcop = sb->s_qcop;
+ if (oqc->qc_cmd == Q_QUOTAON || oqc->qc_cmd == Q_QUOTAOFF) {
+ for (i = 0; i < MAXQUOTAS; i++) {
+ if (!Q_TYPESET(oqc, i))
+ continue;
+
+ if (oqc->qc_cmd == Q_QUOTAON) {
+ char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
+
+ LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
+
+ rc = ll_quota_on(sb, i, QFMT_VFS_V0,
+ name[i], 0);
+ } else if (oqc->qc_cmd == Q_QUOTAOFF) {
+ rc = ll_quota_off(sb, i, 0);
+ }
+
+ if (rc == -EBUSY)
+ error = rc;
+ else if (rc)
+ GOTO(out, rc);
+ }
+ GOTO(out, rc ?: error);
+ }
+
+ switch (oqc->qc_cmd) {
+ case Q_GETOINFO:
+ case Q_GETINFO:
+ if (!qcop->get_info)
+ GOTO(out, rc = -ENOSYS);
+ rc = qcop->get_info(sb, oqc->qc_type, info);
+ break;
+ case Q_SETQUOTA:
+ case Q_INITQUOTA:
+ if (!qcop->set_dqblk)
+ GOTO(out, rc = -ENOSYS);
+ rc = qcop->set_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
+ break;
+ case Q_GETOQUOTA:
+ case Q_GETQUOTA:
+ if (!qcop->get_dqblk)
+ GOTO(out, rc = -ENOSYS);
+ rc = qcop->get_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
+ if (!rc)
+ dqblk->dqb_valid = QIF_LIMITS | QIF_USAGE;
+ break;
+ case Q_SYNC:
+ if (!sb->s_qcop->quota_sync)
+ GOTO(out, rc = -ENOSYS);
+ qcop->quota_sync(sb, oqc->qc_type);
+ break;
+ case Q_FINVALIDATE:
+ CDEBUG(D_WARNING, "invalidating operational quota files\n");
+ for (i = 0; i < MAXQUOTAS; i++) {
+ struct file *fp;
+ char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
+
+ LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
+
+ if (!Q_TYPESET(oqc, i))
+ continue;
+
+ fp = filp_open(name[i], O_CREAT | O_TRUNC | O_RDWR, 0644);
+ if (IS_ERR(fp)) {
+ rc = PTR_ERR(fp);
+ CERROR("error invalidating operational quota file"
+ " %s (rc:%d)\n", name[i], rc);
+ } else {
+ filp_close(fp, 0);
+ }
+
+ }
+ break;
+ default:
+ CERROR("unsupported quotactl command: %d\n", oqc->qc_cmd);
+ LBUG();
+ }
+out:
+ DQINFO_COPY(&oqc->qc_dqinfo, info);
+ DQBLK_COPY(&oqc->qc_dqblk, dqblk);
+
+ OBD_FREE_PTR(info);
+ OBD_FREE_PTR(dqblk);
+
+ if (rc)
+ CDEBUG(D_QUOTA, "quotactl command %#x, id %u, type %u "
+ "failed: %d\n",
+ oqc->qc_cmd, oqc->qc_id, oqc->qc_type, rc);
+ RETURN(rc);
+}
+
+struct chk_dqblk{
+ cfs_hlist_node_t dqb_hash; /** quotacheck hash */
+ cfs_list_t dqb_list; /** in list also */
+ qid_t dqb_id; /** uid/gid */
+ short dqb_type; /** USRQUOTA/GRPQUOTA */
+ qsize_t dqb_bhardlimit; /** block hard limit */
+ qsize_t dqb_bsoftlimit; /** block soft limit */
+ qsize_t dqb_curspace; /** current space */
+ qsize_t dqb_ihardlimit; /** inode hard limit */
+ qsize_t dqb_isoftlimit; /** inode soft limit */
+ qsize_t dqb_curinodes; /** current inodes */
+ obd_time dqb_btime; /** block grace time */
+ obd_time dqb_itime; /** inode grace time */
+ __u32 dqb_valid; /** flag for above fields */
+};
+
+static inline unsigned int chkquot_hash(qid_t id, int type)
+ __attribute__((__const__));
+
+static inline unsigned int chkquot_hash(qid_t id, int type)
+{
+ return (id * (MAXQUOTAS - type)) % NR_DQHASH;
+}
+
+static inline struct chk_dqblk *
+find_chkquot(cfs_hlist_head_t *head, qid_t id, int type)
+{
+ cfs_hlist_node_t *node;
+ struct chk_dqblk *cdqb;
+
+ cfs_hlist_for_each(node, head) {
+ cdqb = cfs_hlist_entry(node, struct chk_dqblk, dqb_hash);
+ if (cdqb->dqb_id == id && cdqb->dqb_type == type)
+ return cdqb;
+ }
+
+ return NULL;
+}
+
+static struct chk_dqblk *alloc_chkquot(qid_t id, int type)
+{
+ struct chk_dqblk *cdqb;
+
+ OBD_ALLOC_PTR(cdqb);
+ if (cdqb) {
+ CFS_INIT_HLIST_NODE(&cdqb->dqb_hash);
+ CFS_INIT_LIST_HEAD(&cdqb->dqb_list);
+ cdqb->dqb_id = id;
+ cdqb->dqb_type = type;
+ }
+
+ return cdqb;
+}
+
+static struct chk_dqblk *
+cqget(struct super_block *sb, cfs_hlist_head_t *hash,
+ cfs_list_t *list, qid_t id, int type, int first_check)
+{
+ cfs_hlist_head_t *head = hash + chkquot_hash(id, type);
+ struct if_dqblk dqb;
+ struct chk_dqblk *cdqb;
+ int rc;
+
+ cdqb = find_chkquot(head, id, type);
+ if (cdqb)
+ return cdqb;
+
+ cdqb = alloc_chkquot(id, type);
+ if (!cdqb)
+ return NULL;
+
+ if (!first_check) {
+ rc = sb->s_qcop->get_dqblk(sb, type, id, &dqb);
+ if (rc) {
+ CERROR("get_dqblk of id %u, type %d failed: %d\n",
+ id, type, rc);
+ } else {
+ DQBLK_COPY(cdqb, &dqb);
+ cdqb->dqb_curspace = 0;
+ cdqb->dqb_curinodes = 0;
+ }
+ }
+
+ cfs_hlist_add_head(&cdqb->dqb_hash, head);
+ cfs_list_add_tail(&cdqb->dqb_list, list);
+
+ return cdqb;
+}
+
+static inline int quota_onoff(struct super_block *sb, int cmd, int type, int qfmt)
+{
+ struct obd_quotactl *oqctl;
+ int rc;
+
+ OBD_ALLOC_PTR(oqctl);
+ if (!oqctl)
+ RETURN(-ENOMEM);
+
+ oqctl->qc_cmd = cmd;
+ oqctl->qc_id = qfmt;
+ oqctl->qc_type = type;
+ rc = fsfilt_ext3_quotactl(sb, oqctl);
+
+ OBD_FREE_PTR(oqctl);
+ return rc;
+}
+
+static inline int read_old_dqinfo(struct super_block *sb, int type,
+ struct if_dqinfo *dqinfo)
+{
+ struct obd_quotactl *oqctl;
+ int rc;
+ ENTRY;
+
+ OBD_ALLOC_PTR(oqctl);
+ if (!oqctl)
+ RETURN(-ENOMEM);
+
+ oqctl->qc_cmd = Q_GETINFO;
+ oqctl->qc_type = type;
+ rc = fsfilt_ext3_quotactl(sb, oqctl);
+ if (!rc)
+ ((struct obd_dqinfo *)dqinfo)[type] = oqctl->qc_dqinfo;
+
+ OBD_FREE_PTR(oqctl);
+ RETURN(rc);
+}
+
+#ifndef HAVE_EXT4_LDISKFS
+static inline struct ext3_group_desc *
+get_group_desc(struct super_block *sb, int group, struct buffer_head **bh)
+{
+ unsigned long desc_block, desc;
+ struct ext3_group_desc *gdp;
+
+ desc_block = group / EXT3_DESC_PER_BLOCK(sb);
+ desc = group % EXT3_DESC_PER_BLOCK(sb);
+ gdp = (struct ext3_group_desc *)
+ EXT3_SB(sb)->s_group_desc[desc_block]->b_data;
+
+ return gdp + desc;
+}
+
+static inline struct buffer_head *
+ext3_read_inode_bitmap(struct super_block *sb, unsigned long group)
+{
+ struct ext3_group_desc *desc;
+ struct buffer_head *bh;
+
+ desc = get_group_desc(sb, group, NULL);
+ bh = sb_bread(sb, ext3_inode_bitmap(sb, desc));
+ return bh;
+}
+
+static __u32 ext3_itable_unused_count(struct super_block *sb,
+ struct ext3_group_desc *bg) {
+ return le16_to_cpu(bg->bg_itable_unused);
+}
+#else
+#define get_group_desc ext3_get_group_desc
+#endif
+
+struct qchk_ctxt {
+ cfs_hlist_head_t qckt_hash[NR_DQHASH]; /* quotacheck hash */
+ cfs_list_t qckt_list; /* quotacheck list */
+ int qckt_first_check[MAXQUOTAS]; /* 1 if no old quotafile */
+ struct if_dqinfo qckt_dqinfo[MAXQUOTAS]; /* old dqinfo */
+};
+
+static int add_inode_quota(struct inode *inode, struct qchk_ctxt *qctxt,
+ struct obd_quotactl *oqc)
+{
+ struct chk_dqblk *cdqb[MAXQUOTAS] = { NULL, };
+ loff_t size = 0;
+ qid_t qid[MAXQUOTAS];
+ int cnt, i, rc = 0;
+
+ if (!inode)
+ return 0;
+
+ qid[USRQUOTA] = inode->i_uid;
+ qid[GRPQUOTA] = inode->i_gid;
+
+ if (S_ISDIR(inode->i_mode) ||
+ S_ISREG(inode->i_mode) ||
+ S_ISLNK(inode->i_mode))
+ size = inode_get_bytes(inode);
+
+ for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
+ if (!Q_TYPESET(oqc, cnt))
+ continue;
+
+ cdqb[cnt] = cqget(inode->i_sb, qctxt->qckt_hash,
+ &qctxt->qckt_list, qid[cnt], cnt,
+ qctxt->qckt_first_check[cnt]);
+ if (!cdqb[cnt]) {
+ rc = -ENOMEM;
+ break;
+ }
+
+ cdqb[cnt]->dqb_curspace += size;
+ cdqb[cnt]->dqb_curinodes++;
+ }
+
+ if (rc) {
+ for (i = 0; i < cnt; i++) {
+ if (!Q_TYPESET(oqc, i))
+ continue;
+ LASSERT(cdqb[i]);
+ cdqb[i]->dqb_curspace -= size;
+ cdqb[i]->dqb_curinodes--;
+ }
+ }
+
+ return rc;
+}
+
+/* write dqinfo struct in a new quota file */
+static int v3_write_dqinfo(struct file *f, int type, struct if_dqinfo *info)
+{
+ struct v2_disk_dqinfo dqinfo;
+ __u32 blocks = V2_DQTREEOFF + 1;
+ loff_t offset = V2_DQINFOOFF;
+
+ if (info) {
+ dqinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
+ dqinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
+ dqinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK &
+ ~DQF_INFO_DIRTY);
+ } else {
+ dqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
+ dqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
+ dqinfo.dqi_flags = 0;
+ }
+
+ dqinfo.dqi_blocks = cpu_to_le32(blocks);
+ dqinfo.dqi_free_blk = 0;
+ dqinfo.dqi_free_entry = 0;
+
+ return cfs_user_write(f, (char *)&dqinfo, sizeof(dqinfo), &offset);
+}
+
+static int v3_write_dqheader(struct file *f, int type)
+{
+ static const __u32 quota_magics[] = V2_INITQMAGICS;
+ static const __u32 quota_versions[] = LUSTRE_INITQVERSIONS_V2;
+ struct v2_disk_dqheader dqhead;
+ loff_t offset = 0;
+
+ CLASSERT(ARRAY_SIZE(quota_magics) == ARRAY_SIZE(quota_versions));
+ LASSERT(0 <= type && type < ARRAY_SIZE(quota_magics));
+
+ dqhead.dqh_magic = cpu_to_le32(quota_magics[type]);
+ dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
+
+ return cfs_user_write(f, (char *)&dqhead, sizeof(dqhead), &offset);
+}
+
+static int create_new_quota_files(struct qchk_ctxt *qctxt,
+ struct obd_quotactl *oqc)
+{
+ int i, rc = 0;
+ ENTRY;
+
+ for (i = 0; i < MAXQUOTAS; i++) {
+ struct if_dqinfo *info = qctxt->qckt_first_check[i]?
+ NULL : &qctxt->qckt_dqinfo[i];
+ struct file *file;
+ const char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
+
+ if (!Q_TYPESET(oqc, i))
+ continue;
+
+ LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
+
+ file = filp_open(name[i], O_RDWR | O_CREAT | O_TRUNC, 0644);
+ if (IS_ERR(file)) {
+ rc = PTR_ERR(file);
+ CERROR("can't create %s file: rc = %d\n",
+ name[i], rc);
+ GOTO(out, rc);
+ }
+
+ if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
+ CERROR("file %s is not regular", name[i]);
+ filp_close(file, 0);
+ GOTO(out, rc = -EINVAL);
+ }
+
+ ll_vfs_dq_drop(file->f_dentry->d_inode);
+
+ rc = v3_write_dqheader(file, i);
+ if (rc) {
+ filp_close(file, 0);
+ GOTO(out, rc);
+ }
+
+ rc = v3_write_dqinfo(file, i, info);
+ filp_close(file, 0);
+ if (rc)
+ GOTO(out, rc);
+ }
+
+out:
+ RETURN(rc);
+}
+
+
+static int commit_chkquot(struct super_block *sb, struct qchk_ctxt *qctxt,
+ struct chk_dqblk *cdqb)
+{
+ struct obd_quotactl *oqc;
+ long now;
+ int rc;
+ ENTRY;
+
+ OBD_ALLOC_PTR(oqc);
+ if (!oqc)
+ RETURN(-ENOMEM);
+
+ now = cfs_time_current_sec();
+
+ if (cdqb->dqb_bsoftlimit &&
+ toqb(cdqb->dqb_curspace) >= cdqb->dqb_bsoftlimit &&
+ !cdqb->dqb_btime)
+ cdqb->dqb_btime =
+ now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_bgrace;
+
+ if (cdqb->dqb_isoftlimit &&
+ cdqb->dqb_curinodes >= cdqb->dqb_isoftlimit &&
+ !cdqb->dqb_itime)
+ cdqb->dqb_itime =
+ now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_igrace;
+
+ cdqb->dqb_valid = QIF_ALL;
+
+ oqc->qc_cmd = Q_SETQUOTA;
+ oqc->qc_type = cdqb->dqb_type;
+ oqc->qc_id = cdqb->dqb_id;
+ DQBLK_COPY(&oqc->qc_dqblk, cdqb);
+
+ rc = fsfilt_ext3_quotactl(sb, oqc);
+ OBD_FREE_PTR(oqc);
+ RETURN(rc);
+}
+
+static int prune_chkquots(struct super_block *sb,
+ struct qchk_ctxt *qctxt, int error)
+{
+ struct chk_dqblk *cdqb, *tmp;
+ int rc;
+
+ cfs_list_for_each_entry_safe(cdqb, tmp, &qctxt->qckt_list, dqb_list) {
+ if (!error) {
+ rc = commit_chkquot(sb, qctxt, cdqb);
+ if (rc)
+ error = rc;
+ }
+ cfs_hlist_del_init(&cdqb->dqb_hash);
+ cfs_list_del(&cdqb->dqb_list);
+ OBD_FREE_PTR(cdqb);
+ }
+
+ return error;
+}
+
+#ifndef EXT3_FEATURE_RO_COMPAT_GDT_CSUM
+#define EXT3_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
+#endif
+
+static int fsfilt_ext3_quotacheck(struct super_block *sb,
+ struct obd_quotactl *oqc)
+{
+ struct ext3_sb_info *sbi = EXT3_SB(sb);
+ int i, group, uninit_feat = 0;
+ struct qchk_ctxt *qctxt;
+ struct buffer_head *bitmap_bh = NULL;
+ unsigned long ino, inode_inuse;
+ struct inode *inode;
+ int rc = 0;
+ ENTRY;
+
+ /* turn on quota and read dqinfo if existed */
+ OBD_ALLOC_PTR(qctxt);
+ if (!qctxt) {
+ oqc->qc_stat = -ENOMEM;
+ RETURN(-ENOMEM);
+ }
+
+ for (i = 0; i < NR_DQHASH; i++)
+ CFS_INIT_HLIST_HEAD(&qctxt->qckt_hash[i]);
+ CFS_INIT_LIST_HEAD(&qctxt->qckt_list);
+
+ for (i = 0; i < MAXQUOTAS; i++) {
+ if (!Q_TYPESET(oqc, i))
+ continue;
+
+ rc = quota_onoff(sb, Q_QUOTAON, i, oqc->qc_id);
+ if (!rc || rc == -EBUSY) {
+ rc = read_old_dqinfo(sb, i, qctxt->qckt_dqinfo);
+ if (rc)
+ GOTO(out, rc);
+ } else if (rc == -ENOENT || rc == -EINVAL || rc == -EEXIST) {
+ qctxt->qckt_first_check[i] = 1;
+ } else if (rc) {
+ GOTO(out, rc);
+ }
+ }
+ if (EXT3_HAS_RO_COMPAT_FEATURE(sb, EXT3_FEATURE_RO_COMPAT_GDT_CSUM))
+ /* This filesystem supports the uninit group feature */
+ uninit_feat = 1;
+
+ /* number of inodes that have been allocated */
+ inode_inuse = sbi->s_inodes_per_group * sbi->s_groups_count -
+ percpu_counter_sum(&sbi->s_freeinodes_counter);
+
+ /* check quota and update in hash */
+ for (group = 0; group < sbi->s_groups_count && inode_inuse > 0;
+ group++) {
+ unsigned long used_count = sbi->s_inodes_per_group;
+
+ if (uninit_feat) {
+ struct ext3_group_desc *desc;
+ desc = get_group_desc(sb, group, NULL);
+ if (!desc)
+ GOTO(out, -EIO);
+
+ /* we don't really need to take the group lock here,
+ * but it may be useful if one day we support online
+ * quotacheck */
+#ifdef HAVE_EXT4_LDISKFS
+ ext4_lock_group(sb, group);
+#else
+ spin_lock(sb_bgl_lock(sbi, group));
+#endif
+ if (desc->bg_flags & cpu_to_le16(EXT3_BG_INODE_UNINIT)) {
+ /* no inode in use in this group, just skip it */
+#ifdef HAVE_EXT4_LDISKFS
+ ext3_unlock_group(sb, group);
+#else
+ spin_unlock(sb_bgl_lock(sbi, group));
+#endif
+ continue;
+ }
+
+ used_count -= ext3_itable_unused_count(sb, desc);
+#ifdef HAVE_EXT4_LDISKFS
+ ext3_unlock_group(sb, group);
+#else
+ spin_unlock(sb_bgl_lock(sbi, group));
+#endif
+ }
+
+ ino = group * sbi->s_inodes_per_group + 1;
+ bitmap_bh = ext3_read_inode_bitmap(sb, group);
+ if (!bitmap_bh) {
+ CERROR("%s: ext3_read_inode_bitmap group %d failed\n",
+ sb->s_id, group);
+ GOTO(out, -EIO);
+ }
+
+ i = 0;
+ while (i < used_count &&
+ (i = ext3_find_next_bit(bitmap_bh->b_data,
+ used_count, i)) < used_count) {
+ inode_inuse--;
+ i++;
+ ino = i + group * sbi->s_inodes_per_group;
+ if (ino < sbi->s_first_ino)
+ continue;
+#if defined(HAVE_EXT4_LDISKFS) || !defined(HAVE_READ_INODE_IN_SBOPS)
+ inode = ext3_iget(sb, ino);
+#else
+ inode = iget(sb, ino);
+#endif
+ if (!inode || IS_ERR(inode))
+ continue;
+
+ rc = add_inode_quota(inode, qctxt, oqc);
+ iput(inode);
+ if (rc) {
+ brelse(bitmap_bh);
+ GOTO(out, rc);
+ }
+ }
+ brelse(bitmap_bh);
+ }
+
+ /* read old quota limits from old quota file. (only for the user
+ * has limits but hasn't file) */
+#ifdef HAVE_QUOTA_SUPPORT
+ for (i = 0; i < MAXQUOTAS; i++) {
+ cfs_list_t id_list;
+ struct dquot_id *dqid, *tmp;
+
+ if (!Q_TYPESET(oqc, i))
+ continue;
+
+ if (qctxt->qckt_first_check[i])
+ continue;
+
+
+ LASSERT(sb_dqopt(sb)->files[i] != NULL);
+ CFS_INIT_LIST_HEAD(&id_list);
+#ifndef KERNEL_SUPPORTS_QUOTA_READ
+ rc = lustre_get_qids(sb_dqopt(sb)->files[i], NULL, i, &id_list);
+#else
+ rc = lustre_get_qids(NULL, sb_dqopt(sb)->files[i], i, &id_list);
+#endif
+ if (rc)
+ CERROR("read old limits failed. (rc:%d)\n", rc);
+
+ cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
+ cfs_list_del_init(&dqid->di_link);
+
+ if (!rc)
+ cqget(sb, qctxt->qckt_hash, &qctxt->qckt_list,
+ dqid->di_id, i,
+ qctxt->qckt_first_check[i]);
+ OBD_FREE_PTR(dqid);
+ }
+ }
+#endif
+ /* turn off quota cause we are to dump chk_dqblk to files */
+ quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
+
+ rc = create_new_quota_files(qctxt, oqc);
+ if (rc)
+ GOTO(out, rc);
+
+ /* we use vfs functions to set dqblk, so turn quota on */
+ rc = quota_onoff(sb, Q_QUOTAON, oqc->qc_type, oqc->qc_id);
+out:
+ /* dump and free chk_dqblk */
+ rc = prune_chkquots(sb, qctxt, rc);
+ OBD_FREE_PTR(qctxt);
+
+ /* turn off quota, `lfs quotacheck` will turn on when all
+ * nodes quotacheck finish. */
+ quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
+
+ oqc->qc_stat = rc;
+ if (rc)
+ CERROR("quotacheck failed: rc = %d\n", rc);
+
+ RETURN(rc);
+}
+
+static int fsfilt_ext3_quotainfo(struct lustre_quota_info *lqi, int type,
+ int cmd)
+{
+ int rc = 0;
+ ENTRY;
+
+ if (lqi->qi_files[type] == NULL) {
+ CERROR("operate qinfo before it's enabled!\n");
+ RETURN(-ESRCH);
+ }
+
+ switch (cmd) {
+ case QFILE_CHK:
+ rc = lustre_check_quota_file(lqi, type);
+ break;
+ case QFILE_RD_INFO:
+ rc = lustre_read_quota_info(lqi, type);
+ break;
+ case QFILE_WR_INFO:
+ rc = lustre_write_quota_info(lqi, type);
+ break;
+ case QFILE_INIT_INFO:
+ rc = lustre_init_quota_info(lqi, type);
+ break;
+ case QFILE_CONVERT:
+ rc = -ENOTSUPP;
+ CERROR("quota CONVERT command is not supported\n");
+ break;
+ default:
+ rc = -ENOTSUPP;
+ CERROR("Unsupported admin quota file cmd %d\n"
+ "Are lquota.ko and fsfilt_ldiskfs.ko modules in sync?\n",
+ cmd);
+ break;
+ }
+ RETURN(rc);
+}
+
+static int fsfilt_ext3_qids(struct file *file, struct inode *inode, int type,
+ cfs_list_t *list)
+{
+ return lustre_get_qids(file, inode, type, list);
+}
+
+static int fsfilt_ext3_dquot(struct lustre_dquot *dquot, int cmd)
+{
+ int rc = 0;
+ ENTRY;
+
+ if (dquot->dq_info->qi_files[dquot->dq_type] == NULL) {
+ CERROR("operate dquot before it's enabled!\n");
+ RETURN(-ESRCH);
+ }
+
+ switch (cmd) {
+ case QFILE_RD_DQUOT:
+ rc = lustre_read_dquot(dquot);
+ break;
+ case QFILE_WR_DQUOT:
+ if (dquot->dq_dqb.dqb_ihardlimit ||
+ dquot->dq_dqb.dqb_isoftlimit ||
+ dquot->dq_dqb.dqb_bhardlimit ||
+ dquot->dq_dqb.dqb_bsoftlimit)
+ cfs_clear_bit(DQ_FAKE_B, &dquot->dq_flags);
+ else
+ cfs_set_bit(DQ_FAKE_B, &dquot->dq_flags);
+
+ rc = lustre_commit_dquot(dquot);
+ if (rc >= 0)
+ rc = 0;
+ break;
+ default:
+ CERROR("Unsupported admin quota file cmd %d\n", cmd);
+ LBUG();
+ break;
+ }
+ RETURN(rc);
+}
+
+static int fsfilt_ext3_get_mblk(struct super_block *sb, int *count,
+ struct inode *inode, int frags)
+{
+#ifdef EXT3_EXT_HAS_NO_TREE
+ struct ext3_ext_base *base = inode;
+#else
+ struct ext3_extents_tree tree;
+ struct ext3_ext_base *base = &tree;
+
+ ext3_init_tree_desc(base, inode);
+#endif
+ /* for an ost_write request, it needs <#fragments> * <tree depth + 1>
+ * metablocks at maxium b=16542 */
+ *count = frags * (EXT_DEPTH(base) + 1) * EXT3_BLOCK_SIZE(sb);
+ return 0;
+}
+
+#endif
+
+lvfs_sbdev_type fsfilt_ext3_journal_sbdev(struct super_block *sb)
+{
+ return (EXT3_SB(sb)->journal_bdev);
+}
+EXPORT_SYMBOL(fsfilt_ext3_journal_sbdev);
+
+ssize_t lustre_read_quota(struct file *f, struct inode *inode, int type,
+ char *buf, int count, loff_t pos)
+{
+ loff_t p = pos;
+ int rc;
+
+ if (!f && !inode) {
+ CERROR("lustre_read_quota failed for no quota file!\n");
+ libcfs_debug_dumpstack(NULL);
+ return -EINVAL;
+ }
+
+ /* Support for both adm and op quota files must be provided */
+ if (f) {
+ rc = fsfilt_ext3_read_record(f, buf, count, &p);
+ rc = rc < 0 ? rc : p - pos;
+ } else {
+ struct super_block *sb = inode->i_sb;
+ rc = sb->s_op->quota_read(sb, type, buf, count, pos);
+ }
+ return rc;
+}
+
+ssize_t lustre_write_quota(struct file *f, char *buf, int count, loff_t pos)
+{
+ loff_t p = pos;
+ int rc;
+
+ /* Only adm quota files are supported, op updates are handled by vfs */
+ rc = fsfilt_ext3_write_record(f, buf, count, &p, 0);
+ rc = rc < 0 ? rc : p - pos;
+
+ return rc;
+}
+
+void *lustre_quota_journal_start(struct inode *inode, int delete)
+{
+ handle_t *handle;
+ unsigned block_count;
+
+ if (delete) {
+ /* each indirect block (+4) may become free, attaching to the
+ * header list of free blocks (+1); the data block (+1) may
+ * become a free block (+0) or a block with free dqentries (+0) */
+ block_count = (4 + 1) + 1;
+ handle = ext3_journal_start(inode,
+ block_count*FSFILT_DATA_TRANS_BLOCKS(inode->i_sb)+2);
+ } else {
+ /* indirect blocks are touched (+4), each causes file expansion (+0) or
+ * freeblk reusage with a header update (+1); dqentry is either reused
+ * causing update of the entry block (+1), prev (+1) and next (+1) or
+ * a new block allocation (+1) with a header update (+1) */
+ block_count = (4 + 1) + 3;
+ handle = ext3_journal_start(inode,
+ block_count*FSFILT_DATA_TRANS_BLOCKS(inode->i_sb)+2);
+
+ }
+
+ return handle;
+}
+
+void lustre_quota_journal_stop(void *handle)
+{
+ ext3_journal_stop((handle_t *)handle);
+}
+