Whamcloud - gitweb
LU-1842 iam: store key & rec in LE order for quota IAM files
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_quota.c
index e636c6d..26257ea 100644 (file)
@@ -102,7 +102,7 @@ static int osd_acct_index_lookup(const struct lu_env *env,
        struct osd_thread_info  *info = osd_oti_get(env);
        struct if_dqblk         *dqblk = &info->oti_dqblk;
        struct super_block      *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
-       struct acct_rec         *rec = (struct acct_rec *)dtrec;
+       struct lquota_acct_rec  *rec = (struct lquota_acct_rec *)dtrec;
        __u64                    id = *((__u64 *)dtkey);
        int                      rc;
 
@@ -149,6 +149,7 @@ static struct dt_it *osd_it_acct_init(const struct lu_env *env,
        memset(it, 0, sizeof(*it));
        lu_object_get(lo);
        it->oiq_obj = obj;
+       CFS_INIT_LIST_HEAD(&it->oiq_list);
 
        /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
        it->oiq_blk[0] = LUSTRE_DQTREEOFF;
@@ -167,9 +168,15 @@ static struct dt_it *osd_it_acct_init(const struct lu_env *env,
 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
 {
        struct osd_it_quota *it = (struct osd_it_quota *)di;
-
+       struct osd_quota_leaf *leaf, *tmp;
        ENTRY;
+
        lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
+
+       cfs_list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
+               cfs_list_del_init(&leaf->oql_link);
+               OBD_FREE_PTR(leaf);
+       }
        EXIT;
 }
 
@@ -227,6 +234,19 @@ static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
        return;
 }
 
+static int osd_it_add_processed(struct osd_it_quota *it, int depth)
+{
+       struct osd_quota_leaf *leaf;
+
+       OBD_ALLOC_PTR(leaf);
+       if (leaf == NULL)
+               RETURN(-ENOMEM);
+       CFS_INIT_LIST_HEAD(&leaf->oql_link);
+       leaf->oql_blk = it->oiq_blk[depth];
+       cfs_list_add_tail(&leaf->oql_link, &it->oiq_list);
+       RETURN(0);
+}
+
 /**
  * Move on to the next valid entry.
  *
@@ -250,8 +270,8 @@ static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
        /* Let's first check if there are any remaining valid entry in the
         * current leaf block. Start with the next entry after the current one.
         */
-       depth = LUSTRE_DQTREEDEPTH - 1;
-       index = GETIDINDEX(it->oiq_id, depth);
+       depth = LUSTRE_DQTREEDEPTH;
+       index = it->oiq_index[depth];
        if (++index < LUSTRE_DQSTRINBLK) {
                /* Search for the next valid entry from current index */
                rc = walk_block_dqentry(env, it->oiq_obj, type,
@@ -263,7 +283,15 @@ static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
                        /* Found on entry, @it is already updated to the
                         * new position in walk_block_dqentry(). */
                        RETURN(0);
+               } else {
+                       rc = osd_it_add_processed(it, depth);
+                       if (rc)
+                               RETURN(rc);
                }
+       } else {
+               rc = osd_it_add_processed(it, depth);
+               if (rc)
+                       RETURN(rc);
        }
        rc = 1;
 
@@ -274,7 +302,7 @@ static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
        /* We keep searching as long as walk_tree_dqentry() returns +1
         * (= no valid entry found). */
        for (; depth >= 0 && rc > 0; depth--) {
-               index = GETIDINDEX(it->oiq_id, depth);
+               index = it->oiq_index[depth];
                if (++index > 0xff)
                        continue;
                rc = walk_tree_dqentry(env, it->oiq_obj, type,
@@ -387,3 +415,172 @@ const struct dt_index_operations osd_acct_index_ops = {
        }
 };
 
+static inline void osd_quota_swab(char *ptr, size_t size)
+{
+       int offset;
+
+       LASSERT((size & (sizeof(__u64) - 1)) == 0);
+
+       for (offset = 0; offset < size; offset += sizeof(__u64))
+            __swab64s((__u64 *)(ptr + offset));
+}
+
+const struct dt_rec *osd_quota_pack(struct osd_object *obj,
+                                   const struct dt_rec *rec,
+                                   union lquota_rec *quota_rec)
+{
+#ifdef __BIG_ENDIAN
+       struct iam_descr        *descr;
+
+       LASSERT(obj->oo_dir != NULL);
+       descr = obj->oo_dir->od_container.ic_descr;
+
+       memcpy(quota_rec, rec, descr->id_rec_size);
+
+       osd_quota_swab((char *)quota_rec, descr->id_rec_size);
+       return (const struct dt_rec *)quota_rec;
+#else
+       return rec;
+#endif
+}
+
+void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
+{
+#ifdef __BIG_ENDIAN
+       struct iam_descr *descr;
+
+       LASSERT(obj->oo_dir != NULL);
+       descr = obj->oo_dir->od_container.ic_descr;
+
+       osd_quota_swab((char *)rec, descr->id_rec_size);
+#else
+       return;
+#endif
+}
+
+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);
+}