Whamcloud - gitweb
LU-1842 quota: ldiskfs local enforcement
[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) 2011, 2012, Whamcloud, Inc.
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 <lquota.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         if (fid_oid(fid) == ACCT_GROUP_OID)
42                 return GRPQUOTA;
43         return USRQUOTA;
44 }
45
46 static inline int obj2type(struct dt_object *obj)
47 {
48         return fid2type(lu_object_fid(&obj->do_lu));
49 }
50
51 /**
52  * Space Accounting Management
53  */
54
55 /**
56  * Look up an accounting object based on its fid.
57  *
58  * \param info - is the osd thread info passed by the caller
59  * \param osd  - is the osd device
60  * \param fid  - is the fid of the accounting object we want to look up
61  * \param id   - is the osd_inode_id struct to fill with the inode number of
62  *               the quota file if the lookup is successful
63  */
64 int osd_acct_obj_lookup(struct osd_thread_info *info, struct osd_device *osd,
65                         const struct lu_fid *fid, struct osd_inode_id *id)
66 {
67         struct super_block *sb = osd_sb(osd);
68
69         ENTRY;
70         LASSERT(fid_is_acct(fid));
71
72         if (!LDISKFS_HAS_RO_COMPAT_FEATURE(sb,
73                                            LDISKFS_FEATURE_RO_COMPAT_QUOTA))
74                 RETURN(-ENOENT);
75
76         id->oii_gen = OSD_OII_NOGEN;
77         id->oii_ino = LDISKFS_SB(sb)->s_qf_inums[fid2type(fid)];
78         if (!ldiskfs_valid_inum(sb, id->oii_ino))
79                 RETURN(-ENOENT);
80         RETURN(0);
81 }
82
83 /**
84  * Return space usage (#blocks & #inodes) consumed by a given uid or gid.
85  *
86  * \param env   - is the environment passed by the caller
87  * \param dtobj - is the accounting object
88  * \param dtrec - is the record to fill with space usage information
89  * \param dtkey - is the id the of the user or group for which we would
90  *                like to access disk usage.
91  * \param capa - is the capability, not used.
92  *
93  * \retval +ve - success : exact match
94  * \retval -ve - failure
95  */
96 static int osd_acct_index_lookup(const struct lu_env *env,
97                                  struct dt_object *dtobj,
98                                  struct dt_rec *dtrec,
99                                  const struct dt_key *dtkey,
100                                  struct lustre_capa *capa)
101 {
102         struct osd_thread_info  *info = osd_oti_get(env);
103         struct if_dqblk         *dqblk = &info->oti_dqblk;
104         struct super_block      *sb = osd_sb(osd_obj2dev(osd_dt_obj(dtobj)));
105         struct lquota_acct_rec  *rec = (struct lquota_acct_rec *)dtrec;
106         __u64                    id = *((__u64 *)dtkey);
107         int                      rc;
108
109         ENTRY;
110
111         memset((void *)dqblk, 0, sizeof(struct obd_dqblk));
112         rc = sb->s_qcop->get_dqblk(sb, obj2type(dtobj), (qid_t) id, dqblk);
113         if (rc)
114                 RETURN(rc);
115         rec->bspace = dqblk->dqb_curspace;
116         rec->ispace = dqblk->dqb_curinodes;
117         RETURN(+1);
118 }
119
120 #define QUOTA_IT_READ_ERROR(it, rc)                                    \
121         CERROR("%s: Error while trying to read quota information, "    \
122                "failed with %d\n",                                     \
123                it->oiq_obj->oo_dt.do_lu.lo_dev->ld_obd->obd_name, rc); \
124
125 /**
126  * Initialize osd Iterator for given osd index object.
127  *
128  * \param  dt    - osd index object
129  * \param  attr  - not used
130  * \param  capa  - BYPASS_CAPA
131  */
132 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
133                                       struct dt_object *dt,
134                                       __u32 attr, struct lustre_capa *capa)
135 {
136         struct osd_thread_info  *info = osd_oti_get(env);
137         struct osd_it_quota     *it;
138         struct lu_object        *lo = &dt->do_lu;
139         struct osd_object       *obj = osd_dt_obj(dt);
140
141         ENTRY;
142
143         LASSERT(lu_object_exists(lo));
144
145         if (info == NULL)
146                 RETURN(ERR_PTR(-ENOMEM));
147
148         it = &info->oti_it_quota;
149         memset(it, 0, sizeof(*it));
150         lu_object_get(lo);
151         it->oiq_obj = obj;
152         CFS_INIT_LIST_HEAD(&it->oiq_list);
153
154         /* LUSTRE_DQTREEOFF is the initial offset where the tree can be found */
155         it->oiq_blk[0] = LUSTRE_DQTREEOFF;
156
157         /* NB: we don't need to store the tree depth since it is always
158          * equal to LUSTRE_DQTREEDEPTH - 1 (root has depth = 0) for a leaf
159          * block. */
160         RETURN((struct dt_it *)it);
161 }
162
163 /**
164  * Free given iterator.
165  *
166  * \param  di   - osd iterator
167  */
168 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
169 {
170         struct osd_it_quota *it = (struct osd_it_quota *)di;
171         struct osd_quota_leaf *leaf, *tmp;
172         ENTRY;
173
174         lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
175
176         cfs_list_for_each_entry_safe(leaf, tmp, &it->oiq_list, oql_link) {
177                 cfs_list_del_init(&leaf->oql_link);
178                 OBD_FREE_PTR(leaf);
179         }
180         EXIT;
181 }
182
183 /**
184  * Move Iterator to record specified by \a key, if the \a key isn't found,
185  * move to the first valid record.
186  *
187  * \param  di   - osd iterator
188  * \param  key  - uid or gid
189  *
190  * \retval +ve  - di points to the first valid record
191  * \retval  +1  - di points to exact matched key
192  * \retval -ve  - failure
193  */
194 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
195                            const struct dt_key *key)
196 {
197         struct osd_it_quota     *it = (struct osd_it_quota *)di;
198         const struct lu_fid     *fid =
199                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
200         int                      type = fid2type(fid);
201         qid_t                    dqid = *(qid_t *)key;
202         loff_t                   offset;
203         int                      rc;
204
205         ENTRY;
206
207         offset = find_tree_dqentry(env, it->oiq_obj, type, dqid,
208                                    LUSTRE_DQTREEOFF, 0, it);
209         if (offset > 0) { /* Found */
210                 RETURN(+1);
211         } else if (offset < 0) { /* Error */
212                 QUOTA_IT_READ_ERROR(it, (int)offset);
213                 RETURN((int)offset);
214         }
215
216         /* The @key is not found, move to the first valid entry */
217         rc = walk_tree_dqentry(env, it->oiq_obj, type, it->oiq_blk[0], 0,
218                                0, it);
219         if (rc == 0)
220                 rc = 1;
221         else if (rc > 0)
222                 rc = -ENOENT;
223
224         RETURN(rc);
225 }
226
227 /**
228  * Release Iterator
229  *
230  * \param  di   - osd iterator
231  */
232 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
233 {
234         return;
235 }
236
237 static int osd_it_add_processed(struct osd_it_quota *it, int depth)
238 {
239         struct osd_quota_leaf *leaf;
240
241         OBD_ALLOC_PTR(leaf);
242         if (leaf == NULL)
243                 RETURN(-ENOMEM);
244         CFS_INIT_LIST_HEAD(&leaf->oql_link);
245         leaf->oql_blk = it->oiq_blk[depth];
246         cfs_list_add_tail(&leaf->oql_link, &it->oiq_list);
247         RETURN(0);
248 }
249
250 /**
251  * Move on to the next valid entry.
252  *
253  * \param  di   - osd iterator
254  *
255  * \retval +ve  - iterator reached the end
256  * \retval   0  - iterator has not reached the end yet
257  * \retval -ve  - unexpected failure
258  */
259 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
260 {
261         struct osd_it_quota     *it = (struct osd_it_quota *)di;
262         const struct lu_fid     *fid =
263                                 lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
264         int                      type = fid2type(fid);
265         int                      depth, rc;
266         uint                     index;
267
268         ENTRY;
269
270         /* Let's first check if there are any remaining valid entry in the
271          * current leaf block. Start with the next entry after the current one.
272          */
273         depth = LUSTRE_DQTREEDEPTH;
274         index = it->oiq_index[depth];
275         if (++index < LUSTRE_DQSTRINBLK) {
276                 /* Search for the next valid entry from current index */
277                 rc = walk_block_dqentry(env, it->oiq_obj, type,
278                                         it->oiq_blk[depth], index, it);
279                 if (rc < 0) {
280                         QUOTA_IT_READ_ERROR(it, rc);
281                         RETURN(rc);
282                 } else if (rc == 0) {
283                         /* Found on entry, @it is already updated to the
284                          * new position in walk_block_dqentry(). */
285                         RETURN(0);
286                 } else {
287                         rc = osd_it_add_processed(it, depth);
288                         if (rc)
289                                 RETURN(rc);
290                 }
291         } else {
292                 rc = osd_it_add_processed(it, depth);
293                 if (rc)
294                         RETURN(rc);
295         }
296         rc = 1;
297
298         /* We have consumed all the entries of the current leaf block, move on
299          * to the next one. */
300         depth--;
301
302         /* We keep searching as long as walk_tree_dqentry() returns +1
303          * (= no valid entry found). */
304         for (; depth >= 0 && rc > 0; depth--) {
305                 index = it->oiq_index[depth];
306                 if (++index > 0xff)
307                         continue;
308                 rc = walk_tree_dqentry(env, it->oiq_obj, type,
309                                        it->oiq_blk[depth], depth, index, it);
310         }
311
312         if (rc < 0)
313                 QUOTA_IT_READ_ERROR(it, rc);
314         RETURN(rc);
315 }
316
317 /**
318  * Return pointer to the key under iterator.
319  *
320  * \param  di   - osd iterator
321  */
322 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
323                                       const struct dt_it *di)
324 {
325         struct osd_it_quota *it = (struct osd_it_quota *)di;
326
327         ENTRY;
328         RETURN((struct dt_key *)&it->oiq_id);
329 }
330
331 /**
332  * Return size of key under iterator (in bytes)
333  *
334  * \param  di   - osd iterator
335  */
336 static int osd_it_acct_key_size(const struct lu_env *env,
337                                 const struct dt_it *di)
338 {
339         struct osd_it_quota *it = (struct osd_it_quota *)di;
340
341         ENTRY;
342         RETURN((int)sizeof(it->oiq_id));
343 }
344
345 /**
346  * Return pointer to the record under iterator.
347  *
348  * \param  di    - osd iterator
349  * \param  attr  - not used
350  */
351 static int osd_it_acct_rec(const struct lu_env *env,
352                            const struct dt_it *di,
353                            struct dt_rec *dtrec, __u32 attr)
354 {
355         struct osd_it_quota     *it = (struct osd_it_quota *)di;
356         const struct dt_key     *key = osd_it_acct_key(env, di);
357         int                      rc;
358
359         ENTRY;
360
361         rc = osd_acct_index_lookup(env, &it->oiq_obj->oo_dt, dtrec, key,
362                                    BYPASS_CAPA);
363         RETURN(rc > 0 ? 0 : rc);
364 }
365
366 /**
367  * Returns cookie for current Iterator position.
368  *
369  * \param  di    - osd iterator
370  */
371 static __u64 osd_it_acct_store(const struct lu_env *env,
372                                const struct dt_it *di)
373 {
374         struct osd_it_quota *it = (struct osd_it_quota *)di;
375
376         ENTRY;
377         RETURN(it->oiq_id);
378 }
379
380 /**
381  * Restore iterator from cookie. if the \a hash isn't found,
382  * restore the first valid record.
383  *
384  * \param  di    - osd iterator
385  * \param  hash  - iterator location cookie
386  *
387  * \retval +ve   - di points to the first valid record
388  * \retval  +1   - di points to exact matched hash
389  * \retval -ve   - failure
390  */
391 static int osd_it_acct_load(const struct lu_env *env,
392                             const struct dt_it *di, __u64 hash)
393 {
394         ENTRY;
395         RETURN(osd_it_acct_get(env, (struct dt_it *)di,
396                                (const struct dt_key *)&hash));
397 }
398
399 /**
400  * Index and Iterator operations for accounting objects
401  */
402 const struct dt_index_operations osd_acct_index_ops = {
403         .dio_lookup     = osd_acct_index_lookup,
404         .dio_it         = {
405                 .init           = osd_it_acct_init,
406                 .fini           = osd_it_acct_fini,
407                 .get            = osd_it_acct_get,
408                 .put            = osd_it_acct_put,
409                 .next           = osd_it_acct_next,
410                 .key            = osd_it_acct_key,
411                 .key_size       = osd_it_acct_key_size,
412                 .rec            = osd_it_acct_rec,
413                 .store          = osd_it_acct_store,
414                 .load           = osd_it_acct_load
415         }
416 };
417
418 static inline int osd_qid_type(struct osd_thandle *oh, int i)
419 {
420         return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
421 }
422
423 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
424 {
425         oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
426 }
427
428 /**
429  * Reserve journal credits for quota files update first, then call
430  * ->op_begin() to perform quota enforcement.
431  *
432  * \param  env    - the environment passed by the caller
433  * \param  oh     - osd transaction handle
434  * \param  qi     - quota id & space required for this operation
435  * \param  allocated - dquot entry in quota accounting file has been allocated
436  * \param  flags  - if the operation is write, return no user quota, no
437  *                  group quota, or sync commit flags to the caller
438  *
439  * \retval 0      - success
440  * \retval -ve    - failure
441  */
442 int osd_declare_qid(const struct lu_env *env, struct osd_thandle *oh,
443                     struct lquota_id_info *qi, bool allocated, int *flags)
444 {
445         struct osd_thread_info  *info = osd_oti_get(env);
446         struct osd_device       *dev = info->oti_dev;
447         struct qsd_instance     *qsd = dev->od_quota_slave;
448         int                      i, rc;
449         bool                     found = false;
450         ENTRY;
451
452         LASSERT(oh != NULL);
453         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%d\n",
454                  oh->ot_id_cnt);
455
456         for (i = 0; i < oh->ot_id_cnt; i++) {
457                 if (oh->ot_id_array[i] == qi->lqi_id.qid_uid &&
458                     osd_qid_type(oh, i) == qi->lqi_type) {
459                         found = true;
460                         break;
461                 }
462         }
463
464         if (!found) {
465                 /* we need to account for credits for this new ID */
466                 if (i >= OSD_MAX_UGID_CNT) {
467                         CERROR("Too many(%d) trans qids!\n", i + 1);
468                         RETURN(-EOVERFLOW);
469                 }
470
471                 oh->ot_credits += (allocated || qi->lqi_id.qid_uid == 0) ?
472                         1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(dev));
473
474                 oh->ot_id_array[i] = qi->lqi_id.qid_uid;
475                 osd_qid_set_type(oh, i, qi->lqi_type);
476                 oh->ot_id_cnt++;
477         }
478
479         if (unlikely(qsd == NULL))
480                 /* quota slave instance hasn't been allocated yet */
481                 RETURN(0);
482
483         /* check quota */
484         rc = qsd_op_begin(env, qsd, oh->ot_quota_trans, qi, flags);
485         RETURN(rc);
486 }
487
488 /**
489  * Wrapper for osd_declare_qid()
490  *
491  * \param  env    - the environment passed by the caller
492  * \param  uid    - user id of the inode
493  * \param  gid    - group id of the inode
494  * \param  space  - how many blocks/inodes will be consumed/released
495  * \param  oh     - osd transaction handle
496  * \param  is_blk - block quota or inode quota?
497  * \param  allocated - dquot entry in quota accounting file has been allocated
498  * \param  flags  - if the operation is write, return no user quota, no
499  *                  group quota, or sync commit flags to the caller
500  * \param force   - set to 1 when changes are performed by root user and thus
501  *                  can't failed with EDQUOT
502  *
503  * \retval 0      - success
504  * \retval -ve    - failure
505  */
506 int osd_declare_inode_qid(const struct lu_env *env, qid_t uid, qid_t gid,
507                           long long space, struct osd_thandle *oh,
508                           bool is_blk, bool allocated, int *flags, bool force)
509 {
510         struct osd_thread_info  *info = osd_oti_get(env);
511         struct lquota_id_info   *qi = &info->oti_qi;
512         int                      rcu, rcg; /* user & group rc */
513         ENTRY;
514
515         /* let's start with user quota */
516         qi->lqi_id.qid_uid = uid;
517         qi->lqi_type       = USRQUOTA;
518         qi->lqi_space      = space;
519         qi->lqi_is_blk     = is_blk;
520         rcu = osd_declare_qid(env, oh, qi, allocated, flags);
521
522         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
523                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
524                 rcu = 0;
525
526         /* For non-fatal error, we want to continue to get the noquota flags
527          * for group id. This is only for commit write, which has @flags passed
528          * in. See osd_declare_write_commit().
529          * When force is set to true, we also want to proceed with the gid */
530         if (rcu && (rcu != -EDQUOT || flags == NULL))
531                 RETURN(rcu);
532
533         /* and now group quota */
534         qi->lqi_id.qid_gid = gid;
535         qi->lqi_type       = GRPQUOTA;
536         rcg = osd_declare_qid(env, oh, qi, allocated, flags);
537
538         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
539                 /* as before, ignore EDQUOT & EINPROGRESS for root */
540                 rcg = 0;
541
542         RETURN(rcu ? rcu : rcg);
543 }
544