Whamcloud - gitweb
LU-4017 quota: add setting/getting project id function
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_quota.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, 2015, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann@whamcloud.com>
28  * Author: Niu    Yawei    <niu@whamcloud.com>
29  */
30
31 #include <lustre_quota.h>
32 #include "osd_internal.h"
33
34 /**
35  * Helpers function to find out the quota type (USRQUOTA/GRPQUOTA) of a
36  * given object
37  */
38 static inline int fid2type(const struct lu_fid *fid)
39 {
40         LASSERT(fid_is_acct(fid));
41         switch (fid_oid(fid)) {
42         case ACCT_USER_OID:
43                 return USRQUOTA;
44         case ACCT_GROUP_OID:
45                 return GRPQUOTA;
46         case ACCT_PROJECT_OID:
47                 return PRJQUOTA;
48         }
49
50         LASSERTF(0, "invalid fid for quota type: %u", fid_oid(fid));
51         return USRQUOTA;
52 }
53
54 /**
55  * Space Accounting Management
56  */
57
58 /**
59  * Look up an accounting object based on its fid.
60  *
61  * \param info - is the osd thread info passed by the caller
62  * \param osd  - is the osd device
63  * \param fid  - is the fid of the accounting object we want to look up
64  * \param id   - is the osd_inode_id struct to fill with the inode number of
65  *               the quota file if the lookup is successful
66  */
67 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
68                         const struct lu_fid *fid, struct osd_inode_id *id)
69 {
70         struct super_block *sb = osd_sb(osd);
71
72         ENTRY;
73         LASSERT(fid_is_acct(fid));
74
75         if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
76                                            LDISKFS_FEATURE_RO_COMPAT_QUOTA))
77                 RETURN(-ENOENT);
78
79         id->oii_gen = OSD_OII_NOGEN;
80         switch (fid2type(fid)) {
81         case USRQUOTA:
82                 id->oii_ino =
83                         le32_to_cpu(LDISKFS_SB(sb)->s_es->s_usr_quota_inum);
84                 break;
85         case GRPQUOTA:
86                 id->oii_ino =
87                         le32_to_cpu(LDISKFS_SB(sb)->s_es->s_grp_quota_inum);
88                 break;
89         case PRJQUOTA:
90 #ifdef  HAVE_PROJECT_QUOTA
91                 if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
92                                 LDISKFS_FEATURE_RO_COMPAT_PROJECT))
93                         RETURN(-ENOTSUPP);
94                 id->oii_ino =
95                         le32_to_cpu(LDISKFS_SB(sb)->s_es->s_prj_quota_inum);
96                 break;
97 #else
98                 RETURN(-ENOTSUPP);
99 #endif
100         }
101         if (!ldiskfs_valid_inum(sb, id->oii_ino))
102                 RETURN(-ENOENT);
103         RETURN(0);
104 }
105
106 /**
107  * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
108  *
109  * \param env   - is the environment passed by the caller
110  * \param dtobj - is the accounting object
111  * \param dtrec - is the record to fill with space usage information
112  * \param dtkey - is the id of the user or group for which we would
113  *                like to access disk usage.
114  *
115  * \retval +ve - success : exact match
116  * \retval -ve - failure
117  */
118 static int osd_acct_index_lookup(const struct lu_env *env,
119                                  struct dt_object *dtobj,
120                                  struct dt_rec *dtrec,
121                                  const struct dt_key *dtkey)
122 {
123         struct osd_thread_info  *info = osd_oti_get(env);
124 #if defined(HAVE_DQUOT_QC_DQBLK)
125         struct qc_dqblk         *dqblk = &info->oti_qdq;
126 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
127         struct fs_disk_quota    *dqblk = &info->oti_fdq;
128 #else
129         struct if_dqblk         *dqblk = &info->oti_dqblk;
130 #endif
131         struct super_block      *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
132         struct lquota_acct_rec  *rec = (struct lquota_acct_rec *)dtrec;
133         __u64                    id = *((__u64 *)dtkey);
134         int                      rc;
135 #ifdef HAVE_DQUOT_KQID
136         struct kqid              qid;
137 #endif
138         int type;
139
140         ENTRY;
141
142         type = fid2type(lu_object_fid(&dtobj->do_lu));
143         memset(dqblk, 0, sizeof(*dqblk));
144 #ifdef HAVE_DQUOT_KQID
145         qid = make_kqid(&init_user_ns, type, id);
146         rc = sb->s_qcop->get_dqblk(sb, qid, dqblk);
147 #else
148         rc = sb->s_qcop->get_dqblk(sb, type, (qid_t) id, dqblk);
149 #endif
150         if (rc)
151                 RETURN(rc);
152 #if defined(HAVE_DQUOT_QC_DQBLK)
153         rec->bspace = dqblk->d_space;
154         rec->ispace = dqblk->d_ino_count;
155 #elif defined(HAVE_DQUOT_FS_DISK_QUOTA)
156         rec->bspace = dqblk->d_bcount;
157         rec->ispace = dqblk->d_icount;
158 #else
159         rec->bspace = dqblk->dqb_curspace;
160         rec->ispace = dqblk->dqb_curinodes;
161 #endif
162         RETURN(+1);
163 }
164
165 #define QUOTA_IT_READ_ERROR(it, rc)                                    \
166         CERROR("%s: Error while trying to read quota information, "    \
167                "failed with %d\n",                                     \
168                osd_dev(it->oiq_obj->oo_dt.do_lu.lo_dev)->od_svname, rc); \
169
170 /**
171  * Initialize osd Iterator for given osd index object.
172  *
173  * \param  dt    - osd index object
174  * \param  attr  - not used
175  */
176 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
177                                       struct dt_object *dt,
178                                       __u32 attr)
179 {
180         struct osd_it_quota     *it;
181         struct lu_object        *lo = &dt->do_lu;
182         struct osd_object       *obj = osd_dt_obj(dt);
183
184         ENTRY;
185
186         LASSERT(lu_object_exists(lo));
187
188         OBD_ALLOC_PTR(it);
189         if (it == NULL)
190                 RETURN(ERR_PTR(-ENOMEM));
191
192         lu_object_get(lo);
193         it->oiq_obj = obj;
194         INIT_LIST_HEAD(&it->oiq_list);
195
196         /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
197         it->oiq_blk[0] = LUSTRE_DQTREEOFF;
198
199         /* NB: we don't need to store the tree depth since it is always
200          * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
201          * block. */
202         RETURN((struct dt_it *)it);
203 }
204
205 /**
206  * Free given iterator.
207  *
208  * \param  di   - osd iterator
209  */
210 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
211 {
212         struct osd_it_quota *it = (struct osd_it_quota *)di;
213         struct osd_quota_leaf *leaf, *tmp;
214         ENTRY;
215
216         osd_object_put(env, it->oiq_obj);
217
218         list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
219                 list_del_init(&leaf->oql_link);
220                 OBD_FREE_PTR(leaf);
221         }
222
223         OBD_FREE_PTR(it);
224
225         EXIT;
226 }
227
228 /**
229  * Move Iterator to record specified by \a key, if the \a key isn't found,
230  * move to the first valid record.
231  *
232  * \param  di   - osd iterator
233  * \param  key  - uid or gid
234  *
235  * \retval +ve  - di points to the first valid record
236  * \retval  +1  - di points to exact matched key
237  * \retval -ve  - failure
238  */
239 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
240                            const struct dt_key *key)
241 {
242         struct osd_it_quota     *it = (struct osd_it_quota *)di;
243         const struct lu_fid     *fid =
244                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
245         int                      type;
246         qid_t                    dqid = *(qid_t *)key;
247         loff_t                   offset;
248         int                      rc;
249
250         ENTRY;
251         type = fid2type(fid);
252
253         offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
254                                    LUSTRE_DQTREEOFF, 0, it);
255         if (offset > 0) { /* Found */
256                 RETURN(+1);
257         } else if (offset < 0) { /* Error */
258                 QUOTA_IT_READ_ERROR(it, (int)offset);
259                 RETURN((int)offset);
260         }
261
262         /* The @key is not found, move to the first valid entry */
263         rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
264                                0, it);
265         if (rc == 0)
266                 rc = 1;
267         else if (rc > 0)
268                 rc = -ENOENT;
269
270         RETURN(rc);
271 }
272
273 /**
274  * Release Iterator
275  *
276  * \param  di   - osd iterator
277  */
278 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
279 {
280         return;
281 }
282
283 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
284 {
285         struct osd_quota_leaf *leaf;
286
287         OBD_ALLOC_PTR(leaf);
288         if (leaf == NULL)
289                 RETURN(-ENOMEM);
290         INIT_LIST_HEAD(&leaf->oql_link);
291         leaf->oql_blk = it->oiq_blk[depth];
292         list_add_tail(&leaf->oql_link, &it->oiq_list);
293         RETURN(0);
294 }
295
296 /**
297  * Move on to the next valid entry.
298  *
299  * \param  di   - osd iterator
300  *
301  * \retval +ve  - iterator reached the end
302  * \retval   0  - iterator has not reached the end yet
303  * \retval -ve  - unexpected failure
304  */
305 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
306 {
307         struct osd_it_quota     *it = (struct osd_it_quota *)di;
308         const struct lu_fid     *fid =
309                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
310         int                      type;
311         int                      depth, rc;
312         uint                     index;
313
314         ENTRY;
315
316         type = fid2type(fid);
317
318         /* Let's first check if there are any remaining valid entry in the
319          * current leaf block. Start with the next entry after the current one.
320          */
321         depth = LUSTRE_DQTREEDEPTH;
322         index = it->oiq_index[depth];
323         if (++index < LUSTRE_DQSTRINBLK) {
324                 /* Search for the next valid entry from current index */
325                 rc = walk_block_dqentry(env, it->oiq_obj, type,
326                                         it->oiq_blk[depth], index, it);
327                 if (rc < 0) {
328                         QUOTA_IT_READ_ERROR(it, rc);
329                         RETURN(rc);
330                 } else if (rc == 0) {
331                         /* Found on entry, @it is already updated to the
332                          * new position in walk_block_dqentry(). */
333                         RETURN(0);
334                 } else {
335                         rc = osd_it_add_processed(it, depth);
336                         if (rc)
337                                 RETURN(rc);
338                 }
339         } else {
340                 rc = osd_it_add_processed(it, depth);
341                 if (rc)
342                         RETURN(rc);
343         }
344         rc = 1;
345
346         /* We have consumed all the entries of the current leaf block, move on
347          * to the next one. */
348         depth--;
349
350         /* We keep searching as long as walk_tree_dqentry() returns +1
351          * (= no valid entry found). */
352         for (; depth >= 0 && rc > 0; depth--) {
353                 index = it->oiq_index[depth];
354                 if (++index > 0xff)
355                         continue;
356                 rc = walk_tree_dqentry(env, it->oiq_obj, type,
357                                        it->oiq_blk[depth], depth, index, it);
358         }
359
360         if (rc < 0)
361                 QUOTA_IT_READ_ERROR(it, rc);
362         RETURN(rc);
363 }
364
365 /**
366  * Return pointer to the key under iterator.
367  *
368  * \param  di   - osd iterator
369  */
370 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
371                                       const struct dt_it *di)
372 {
373         struct osd_it_quota *it = (struct osd_it_quota *)di;
374
375         ENTRY;
376         RETURN((struct dt_key *)&it->oiq_id);
377 }
378
379 /**
380  * Return size of key under iterator (in bytes)
381  *
382  * \param  di   - osd iterator
383  */
384 static int osd_it_acct_key_size(const struct lu_env *env,
385                                 const struct dt_it *di)
386 {
387         struct osd_it_quota *it = (struct osd_it_quota *)di;
388
389         ENTRY;
390         RETURN((int)sizeof(it->oiq_id));
391 }
392
393 /**
394  * Return pointer to the record under iterator.
395  *
396  * \param  di    - osd iterator
397  * \param  attr  - not used
398  */
399 static int osd_it_acct_rec(const struct lu_env *env,
400                            const struct dt_it *di,
401                            struct dt_rec *dtrec, __u32 attr)
402 {
403         struct osd_it_quota     *it = (struct osd_it_quota *)di;
404         const struct dt_key     *key = osd_it_acct_key(env, di);
405         int                      rc;
406
407         ENTRY;
408
409         rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key);
410         RETURN(rc > 0 ? 0 : rc);
411 }
412
413 /**
414  * Returns cookie for current Iterator position.
415  *
416  * \param  di    - osd iterator
417  */
418 static __u64 osd_it_acct_store(const struct lu_env *env,
419                                const struct dt_it *di)
420 {
421         struct osd_it_quota *it = (struct osd_it_quota *)di;
422
423         ENTRY;
424         RETURN(it->oiq_id);
425 }
426
427 /**
428  * Restore iterator from cookie. if the \a hash isn't found,
429  * restore the first valid record.
430  *
431  * \param  di    - osd iterator
432  * \param  hash  - iterator location cookie
433  *
434  * \retval +ve   - di points to the first valid record
435  * \retval  +1   - di points to exact matched hash
436  * \retval -ve   - failure
437  */
438 static int osd_it_acct_load(const struct lu_env *env,
439                             const struct dt_it *di, __u64 hash)
440 {
441         ENTRY;
442         RETURN(osd_it_acct_get(env, (struct dt_it *)di,
443                                (const struct dt_key *)&hash));
444 }
445
446 /**
447  * Index and Iterator operations for accounting objects
448  */
449 const struct dt_index_operations osd_acct_index_ops = {
450         .dio_lookup     = osd_acct_index_lookup,
451         .dio_it         = {
452                 .init           = osd_it_acct_init,
453                 .fini           = osd_it_acct_fini,
454                 .get            = osd_it_acct_get,
455                 .put            = osd_it_acct_put,
456                 .next           = osd_it_acct_next,
457                 .key            = osd_it_acct_key,
458                 .key_size       = osd_it_acct_key_size,
459                 .rec            = osd_it_acct_rec,
460                 .store          = osd_it_acct_store,
461                 .load           = osd_it_acct_load
462         }
463 };
464
465 static inline void osd_quota_swab(char *ptr, size_t size)
466 {
467         int offset;
468
469         LASSERT((size & (sizeof(__u64) - 1)) == 0);
470
471         for (offset = 0; offset < size; offset += sizeof(__u64))
472              __swab64s((__u64 *)(ptr + offset));
473 }
474
475 const struct dt_rec *osd_quota_pack(struct osd_object *obj,
476                                     const struct dt_rec *rec,
477                                     union lquota_rec *quota_rec)
478 {
479 #ifdef __BIG_ENDIAN
480         struct iam_descr        *descr;
481
482         LASSERT(obj->oo_dir != NULL);
483         descr = obj->oo_dir->od_container.ic_descr;
484
485         memcpy(quota_rec, rec, descr->id_rec_size);
486
487         osd_quota_swab((char *)quota_rec, descr->id_rec_size);
488         return (const struct dt_rec *)quota_rec;
489 #else
490         return rec;
491 #endif
492 }
493
494 void osd_quota_unpack(struct osd_object *obj, const struct dt_rec *rec)
495 {
496 #ifdef __BIG_ENDIAN
497         struct iam_descr *descr;
498
499         LASSERT(obj->oo_dir != NULL);
500         descr = obj->oo_dir->od_container.ic_descr;
501
502         osd_quota_swab((char *)rec, descr->id_rec_size);
503 #else
504         return;
505 #endif
506 }
507
508 static inline int osd_qid_type(struct osd_thandle *oh, int i)
509 {
510         return oh->ot_id_types[i];
511 }
512
513 /**
514  * Reserve journal credits for quota files update first, then call
515  * ->op_begin() to perform quota enforcement.
516  *
517  * \param  env     - the environment passed by the caller
518  * \param  oh      - osd transaction handle
519  * \param  qi      - quota id & space required for this operation
520  * \param  obj     - osd object, could be NULL when it's under create
521  * \param  enforce - whether to perform quota enforcement
522  * \param  flags   - if the operation is write, return no user quota, no
523  *                   group quota, or sync commit flags to the caller
524  *
525  * \retval 0       - success
526  * \retval -ve     - failure
527  */
528 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
529                     struct lquota_id_info *qi, struct osd_object *obj,
530                     bool enforce, int *flags)
531 {
532         struct osd_device       *dev;
533         struct qsd_instance     *qsd;
534         struct inode            *inode = NULL;
535         int                      i, rc = 0, crd;
536         bool                     found = false;
537         ENTRY;
538
539         LASSERT(oh != NULL);
540         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
541                  oh->ot_id_cnt);
542
543         dev = osd_dt_dev(oh->ot_super.th_dev);
544         LASSERT(dev != NULL);
545
546         qsd = dev->od_quota_slave;
547
548         for (i = 0; i < oh->ot_id_cnt; i++) {
549                 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
550                     oh->ot_id_types[i] == qi->lqi_type) {
551                         found = true;
552                         break;
553                 }
554         }
555
556         if (!found) {
557                 /* we need to account for credits for this new ID */
558                 if (i >= OSD_MAX_UGID_CNT) {
559                         CERROR("Too many(%d) trans qids!\n", i + 1);
560                         RETURN(-EOVERFLOW);
561                 }
562
563                 if (obj != NULL)
564                         inode = obj->oo_inode;
565
566                 /* root ID entry should be always present in the quota file */
567                 if (qi->lqi_id.qid_uid == 0) {
568                         crd = 1;
569                 } else {
570                         /* used space for this ID could be dropped to zero,
571                          * reserve extra credits for removing ID entry from
572                          * the quota file */
573                         if (qi->lqi_space < 0)
574                                 crd = LDISKFS_QUOTA_DEL_BLOCKS(osd_sb(dev));
575                         /* reserve credits for adding ID entry to the quota
576                          * file if the i_dquot isn't initialized yet. */
577                         else if (inode == NULL ||
578 #ifdef HAVE_EXT4_INFO_DQUOT
579                                  LDISKFS_I(inode)->i_dquot[qi->lqi_type] == NULL)
580 #else
581                                  inode->i_dquot[qi->lqi_type] == NULL)
582 #endif
583                                 crd = LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
584                         else
585                                 crd = 1;
586                 }
587
588                 osd_trans_declare_op(env, oh, OSD_OT_QUOTA, crd);
589
590                 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
591                 oh->ot_id_types[i] = qi->lqi_type;
592                 oh->ot_id_cnt++;
593         }
594
595         if (unlikely(qsd == NULL))
596                 /* quota slave instance hasn't been allocated yet */
597                 RETURN(0);
598
599         /* check quota */
600         if (enforce)
601                 rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
602         RETURN(rc);
603 }
604
605 /**
606  * Wrapper for osd_declare_qid()
607  *
608  * \param  env    - the environment passed by the caller
609  * \param  uid    - user id of the inode
610  * \param  gid    - group id of the inode
611  * \param  space  - how many blocks/inodes will be consumed/released
612  * \param  oh     - osd transaction handle
613  * \param  obj    - osd object, could be NULL when it's under create
614  * \param  is_blk - block quota or inode quota?
615  * \param  flags  - if the operation is write, return no user quota, no
616  *                  group quota, or sync commit flags to the caller
617  * \param force   - set to 1 when changes are performed by root user and thus
618  *                  can't failed with EDQUOT
619  *
620  * \retval 0      - success
621  * \retval -ve    - failure
622  */
623 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
624                           __u32 projid, long long space, struct osd_thandle *oh,
625                           struct osd_object *obj, bool is_blk, int *flags,
626                           bool force)
627 {
628         struct osd_thread_info  *info = osd_oti_get(env);
629         struct lquota_id_info   *qi = &info->oti_qi;
630         int                      rcu, rcg, rcp; /* user & group & project rc */
631         ENTRY;
632
633         /* let's start with user quota */
634         qi->lqi_id.qid_uid = uid;
635         qi->lqi_type       = USRQUOTA;
636         qi->lqi_space      = space;
637         qi->lqi_is_blk     = is_blk;
638         rcu = osd_declare_qid(env, oh, qi, obj, true, flags);
639
640         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
641                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
642                 rcu = 0;
643
644         /* For non-fatal error, we want to continue to get the noquota flags
645          * for group id. This is only for commit write, which has @flags passed
646          * in. See osd_declare_write_commit().
647          * When force is set to true, we also want to proceed with the gid */
648         if (rcu && (rcu != -EDQUOT || flags == NULL))
649                 RETURN(rcu);
650
651         /* and now group quota */
652         qi->lqi_id.qid_gid = gid;
653         qi->lqi_type       = GRPQUOTA;
654         rcg = osd_declare_qid(env, oh, qi, obj, true, flags);
655
656         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
657                 /* as before, ignore EDQUOT & EINPROGRESS for root */
658                 rcg = 0;
659         if (rcg && (rcg != -EDQUOT || flags == NULL))
660                 RETURN(rcg);
661
662         /* and now project quota */
663         qi->lqi_id.qid_gid = projid;
664         qi->lqi_type       = PRJQUOTA;
665         rcp = osd_declare_qid(env, oh, qi, obj, true, flags);
666
667         if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
668                 /* as before, ignore EDQUOT & EINPROGRESS for root */
669                 rcp = 0;
670
671         if (rcu)
672                 RETURN(rcu);
673         if (rcg)
674                 RETURN(rcg);
675         if (rcp)
676                 RETURN(rcp);
677
678         RETURN(0);
679 }