}
};
+static inline int osd_qid_type(struct osd_thandle *oh, int i)
+{
+ return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
+}
+
+static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
+{
+ oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
+}
+
+/**
+ * Reserve journal credits for quota files update first, then call
+ * ->op_begin() to perform quota enforcement.
+ *
+ * \param env - the environment passed by the caller
+ * \param oh - osd transaction handle
+ * \param qi - quota id & space required for this operation
+ * \param allocated - dquot entry in quota accounting file has been allocated
+ * \param flags - if the operation is write, return no user quota, no
+ * group quota, or sync commit flags to the caller
+ *
+ * \retval 0 - success
+ * \retval -ve - failure
+ */
+int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
+ struct lquota_id_info *qi, bool allocated, int *flags)
+{
+ struct osd_thread_info *info = osd_oti_get(env);
+ struct osd_device *dev = info->oti_dev;
+ struct qsd_instance *qsd = dev->od_quota_slave;
+ int i, rc;
+ bool found = false;
+ ENTRY;
+
+ LASSERT(oh != NULL);
+ LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
+ oh->ot_id_cnt);
+
+ for (i = 0; i < oh->ot_id_cnt; i++) {
+ if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
+ osd_qid_type(oh, i) == qi->lqi_type) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ /* we need to account for credits for this new ID */
+ if (i >= OSD_MAX_UGID_CNT) {
+ CERROR("Too many(%d) trans qids!\n", i + 1);
+ RETURN(-EOVERFLOW);
+ }
+
+ oh->ot_credits += (allocated || qi->lqi_id.qid_uid == 0) ?
+ 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
+
+ oh->ot_id_array[i] = qi->lqi_id.qid_uid;
+ osd_qid_set_type(oh, i, qi->lqi_type);
+ oh->ot_id_cnt++;
+ }
+
+ if (unlikely(qsd == NULL))
+ /* quota slave instance hasn't been allocated yet */
+ RETURN(0);
+
+ /* check quota */
+ rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
+ RETURN(rc);
+}
+
+/**
+ * Wrapper for osd_declare_qid()
+ *
+ * \param env - the environment passed by the caller
+ * \param uid - user id of the inode
+ * \param gid - group id of the inode
+ * \param space - how many blocks/inodes will be consumed/released
+ * \param oh - osd transaction handle
+ * \param is_blk - block quota or inode quota?
+ * \param allocated - dquot entry in quota accounting file has been allocated
+ * \param flags - if the operation is write, return no user quota, no
+ * group quota, or sync commit flags to the caller
+ * \param force - set to 1 when changes are performed by root user and thus
+ * can't failed with EDQUOT
+ *
+ * \retval 0 - success
+ * \retval -ve - failure
+ */
+int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
+ long long space, struct osd_thandle *oh,
+ bool is_blk, bool allocated, int *flags, bool force)
+{
+ struct osd_thread_info *info = osd_oti_get(env);
+ struct lquota_id_info *qi = &info->oti_qi;
+ int rcu, rcg; /* user & group rc */
+ ENTRY;
+
+ /* let's start with user quota */
+ qi->lqi_id.qid_uid = uid;
+ qi->lqi_type = USRQUOTA;
+ qi->lqi_space = space;
+ qi->lqi_is_blk = is_blk;
+ rcu = osd_declare_qid(env, oh, qi, allocated, flags);
+
+ if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
+ /* ignore EDQUOT & EINPROGRESS when changes are done by root */
+ rcu = 0;
+
+ /* For non-fatal error, we want to continue to get the noquota flags
+ * for group id. This is only for commit write, which has @flags passed
+ * in. See osd_declare_write_commit().
+ * When force is set to true, we also want to proceed with the gid */
+ if (rcu && (rcu != -EDQUOT || flags == NULL))
+ RETURN(rcu);
+
+ /* and now group quota */
+ qi->lqi_id.qid_gid = gid;
+ qi->lqi_type = GRPQUOTA;
+ rcg = osd_declare_qid(env, oh, qi, allocated, flags);
+
+ if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
+ /* as before, ignore EDQUOT & EINPROGRESS for root */
+ rcg = 0;
+
+ RETURN(rcu ? rcu : rcg);
+}
+