+ /* 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);