Whamcloud - gitweb
LU-11303 quota: enforce block quota for chgrp
[fs/lustre-release.git] / lustre / osd-zfs / 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, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann@whamcloud.com>
28  */
29
30 #include <dt_object.h>
31 #include <lustre_quota.h>
32 #include <obd.h>
33 #include "osd_internal.h"
34
35 /**
36  * Helper function to estimate the number of inodes in use for the given
37  * uid/gid/projid from the block usage
38  */
39 static uint64_t osd_objset_user_iused(struct osd_device *osd, uint64_t uidbytes)
40 {
41         uint64_t refdbytes, availbytes, usedobjs, availobjs;
42         uint64_t uidobjs, bshift;
43
44         /* get fresh statfs info */
45         dmu_objset_space(osd->od_os, &refdbytes, &availbytes,
46                          &usedobjs, &availobjs);
47
48         /* estimate the number of objects based on the disk usage */
49         bshift = fls64(osd->od_max_blksz) - 1;
50         uidobjs = osd_objs_count_estimate(refdbytes, usedobjs,
51                                           uidbytes >> bshift, bshift);
52         if (uidbytes > 0)
53                 /* if we have at least 1 byte, we have at least one dnode ... */
54                 uidobjs = max_t(uint64_t, uidobjs, 1);
55
56         return uidobjs;
57 }
58
59 /**
60  * Space Accounting Management
61  */
62
63 /**
64  * Return space usage consumed by a given uid or gid or projid.
65  * Block usage is accurrate since it is maintained by DMU itself.
66  * However, DMU does not provide inode accounting, so the #inodes in use
67  * is estimated from the block usage and statfs information.
68  *
69  * \param env   - is the environment passed by the caller
70  * \param dtobj - is the accounting object
71  * \param dtrec - is the record to fill with space usage information
72  * \param dtkey - is the id the of the user or group for which we would
73  *                like to access disk usage.
74  *
75  * \retval +ve - success : exact match
76  * \retval -ve - failure
77  */
78 static int osd_acct_index_lookup(const struct lu_env *env,
79                                 struct dt_object *dtobj,
80                                 struct dt_rec *dtrec,
81                                 const struct dt_key *dtkey)
82 {
83         struct osd_thread_info *info = osd_oti_get(env);
84         char *buf = info->oti_buf;
85         struct lquota_acct_rec *rec = (struct lquota_acct_rec *)dtrec;
86         struct osd_object *obj = osd_dt_obj(dtobj);
87         struct osd_device *osd = osd_obj2dev(obj);
88         dnode_t *dn = obj->oo_dn;
89         size_t buflen = sizeof(info->oti_buf);
90         int rc;
91         ENTRY;
92
93         rec->bspace = rec->ispace = 0;
94
95         /* convert the 64-bit uid/gid/projid into a string */
96         snprintf(buf, buflen, "%llx", *((__u64 *)dtkey));
97         if (unlikely(!dn)) {
98                 CDEBUG(D_QUOTA, "%s: miss accounting obj for %s\n",
99                        osd->od_svname, buf);
100
101                 RETURN(-ENOENT);
102         }
103
104         /* disk usage (in bytes) is maintained by DMU.
105          * DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT are special objects which
106          * not associated with any dmu_but_t (see dnode_special_open()). */
107         rc = osd_zap_lookup(osd, dn->dn_object, dn, buf, sizeof(uint64_t), 1,
108                             &rec->bspace);
109         if (rc == -ENOENT) {
110                 /* user/group/project has not created anything yet */
111                 CDEBUG(D_QUOTA, "%s: id %s not found in DMU accounting ZAP\n",
112                        osd->od_svname, buf);
113                 /* -ENOENT is normal case, convert it as 1. */
114                 rc = 1;
115         } else if (rc) {
116                 RETURN(rc);
117         }
118
119         if (!osd_dmu_userobj_accounting_available(osd)) {
120                 if (rec->bspace != 0)
121                         /* estimate #inodes in use */
122                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
123                 rc = 1;
124         } else {
125                 snprintf(buf, buflen, OSD_DMU_USEROBJ_PREFIX "%llx",
126                          *((__u64 *)dtkey));
127                 rc = osd_zap_lookup(osd, dn->dn_object, dn, buf,
128                                     sizeof(uint64_t), 1, &rec->ispace);
129                 if (rc == -ENOENT) {
130                         CDEBUG(D_QUOTA,
131                                "%s: id %s not found dnode accounting\n",
132                                osd->od_svname, buf);
133                         /* -ENOENT is normal case, convert it as 1. */
134                         rc = 1;
135                 } else if (rc == 0) {
136                         rc = 1;
137                 }
138         }
139
140         RETURN(rc);
141 }
142
143 /**
144  * Initialize osd Iterator for given osd index object.
145  *
146  * \param  dt    - osd index object
147  * \param  attr  - not used
148  */
149 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
150                                       struct dt_object *dt,
151                                       __u32 attr)
152 {
153         struct osd_thread_info *info = osd_oti_get(env);
154         struct osd_it_quota *it;
155         struct osd_object *obj = osd_dt_obj(dt);
156         struct osd_device *osd = osd_obj2dev(obj);
157         dnode_t *dn = obj->oo_dn;
158         int rc;
159         ENTRY;
160
161         if (unlikely(!dn)) {
162                 CDEBUG(D_QUOTA, "%s: Not found in DMU accounting ZAP\n",
163                        osd->od_svname);
164
165                 RETURN(ERR_PTR(-ENOENT));
166         }
167
168         if (info == NULL)
169                 RETURN(ERR_PTR(-ENOMEM));
170
171         OBD_ALLOC_PTR(it);
172         if (it == NULL)
173                 RETURN(ERR_PTR(-ENOMEM));
174
175         memset(it, 0, sizeof(*it));
176         it->oiq_oid = dn->dn_object;
177
178         /* initialize zap cursor */
179         rc = osd_zap_cursor_init(&it->oiq_zc, osd->od_os, it->oiq_oid, 0);
180         if (rc != 0) {
181                 OBD_FREE_PTR(it);
182                 RETURN(ERR_PTR(rc));
183         }
184
185         /* take object reference */
186         lu_object_get(&dt->do_lu);
187         it->oiq_obj   = osd_dt_obj(dt);
188         it->oiq_reset = 1;
189
190         RETURN((struct dt_it *)it);
191 }
192
193 /**
194  * Free given iterator.
195  *
196  * \param  di   - osd iterator
197  */
198 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
199 {
200         struct osd_it_quota     *it     = (struct osd_it_quota *)di;
201         ENTRY;
202
203         osd_zap_cursor_fini(it->oiq_zc);
204         osd_object_put(env, it->oiq_obj);
205         OBD_FREE_PTR(it);
206
207         EXIT;
208 }
209
210 /**
211  * Locate the first entry that is for space accounting.
212  */
213 static int osd_zap_locate(struct osd_it_quota *it, zap_attribute_t *za)
214 {
215         int rc;
216         ENTRY;
217
218         while (1) {
219                 rc = -zap_cursor_retrieve(it->oiq_zc, za);
220                 if (rc)
221                         break;
222
223                 if (strncmp(za->za_name, OSD_DMU_USEROBJ_PREFIX,
224                             OSD_DMU_USEROBJ_PREFIX_LEN))
225                         break;
226
227                 zap_cursor_advance(it->oiq_zc);
228         }
229
230         RETURN(rc);
231 }
232
233 /**
234  * Move on to the next valid entry.
235  *
236  * \param  di   - osd iterator
237  *
238  * \retval +ve  - iterator reached the end
239  * \retval   0  - iterator has not reached the end yet
240  * \retval -ve  - unexpected failure
241  */
242 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
243 {
244         struct osd_it_quota     *it = (struct osd_it_quota *)di;
245         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
246         int                      rc;
247         ENTRY;
248
249         if (it->oiq_reset == 0)
250                 zap_cursor_advance(it->oiq_zc);
251         it->oiq_reset = 0;
252
253         rc = osd_zap_locate(it, za);
254         RETURN(rc == -ENOENT ? 1 : rc);
255 }
256
257 /**
258  * Return pointer to the key under iterator.
259  *
260  * \param  di   - osd iterator
261  */
262 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
263                                       const struct dt_it *di)
264 {
265         struct osd_it_quota     *it = (struct osd_it_quota *)di;
266         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
267         int                      rc;
268         ENTRY;
269
270         it->oiq_reset = 0;
271         rc = osd_zap_locate(it, za);
272         if (rc)
273                 RETURN(ERR_PTR(rc));
274
275         rc = kstrtoull(za->za_name, 16, &it->oiq_id);
276         if (rc)
277                 CERROR("couldn't parse name %s\n", za->za_name);
278
279         RETURN((struct dt_key *) &it->oiq_id);
280 }
281
282 /**
283  * Return size of key under iterator (in bytes)
284  *
285  * \param  di   - osd iterator
286  */
287 static int osd_it_acct_key_size(const struct lu_env *env,
288                                 const struct dt_it *di)
289 {
290         ENTRY;
291         RETURN((int)sizeof(uint64_t));
292 }
293
294 /*
295  * zap_cursor_retrieve read from current record.
296  * to read bytes we need to call zap_lookup explicitly.
297  */
298 static int osd_zap_cursor_retrieve_value(const struct lu_env *env,
299                                          struct osd_it_quota *it,
300                                          char *buf, int buf_size,
301                                          int *bytes_read)
302 {
303         const struct lu_fid *fid = lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
304         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
305         zap_cursor_t *zc = it->oiq_zc;
306         struct osd_device *osd = osd_obj2dev(it->oiq_obj);
307         int rc, actual_size;
308
309         rc = -zap_cursor_retrieve(zc, za);
310         if (unlikely(rc != 0))
311                 return rc;
312
313         if (unlikely(za->za_integer_length <= 0))
314                 return -ERANGE;
315
316         actual_size = za->za_integer_length * za->za_num_integers;
317
318         if (actual_size > buf_size) {
319                 actual_size = buf_size;
320                 buf_size = actual_size / za->za_integer_length;
321         } else {
322                 buf_size = za->za_num_integers;
323         }
324
325         /* use correct special ID to request bytes used */
326         rc = osd_zap_lookup(osd, fid_oid(fid) == ACCT_GROUP_OID ?
327                             DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT, NULL,
328                             za->za_name, za->za_integer_length, buf_size, buf);
329         if (likely(rc == 0))
330                 *bytes_read = actual_size;
331
332         return rc;
333 }
334
335 /**
336  * Return pointer to the record under iterator.
337  *
338  * \param  di    - osd iterator
339  * \param  attr  - not used
340  */
341 static int osd_it_acct_rec(const struct lu_env *env,
342                            const struct dt_it *di,
343                            struct dt_rec *dtrec, __u32 attr)
344 {
345         struct osd_thread_info  *info = osd_oti_get(env);
346         zap_attribute_t         *za = &info->oti_za;
347         struct osd_it_quota     *it = (struct osd_it_quota *)di;
348         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
349         struct osd_object       *obj = it->oiq_obj;
350         struct osd_device       *osd = osd_obj2dev(obj);
351         int                      bytes_read;
352         int                      rc;
353         ENTRY;
354
355         it->oiq_reset = 0;
356         rec->ispace = rec->bspace = 0;
357
358         /* retrieve block usage from the DMU accounting object */
359         rc = osd_zap_cursor_retrieve_value(env, it, (char *)&rec->bspace,
360                                            sizeof(uint64_t), &bytes_read);
361         if (rc)
362                 RETURN(rc);
363
364         if (!osd_dmu_userobj_accounting_available(osd)) {
365                 if (rec->bspace != 0)
366                         /* estimate #inodes in use */
367                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
368                 RETURN(0);
369         }
370
371         /* retrieve key associated with the current cursor */
372         rc = -zap_cursor_retrieve(it->oiq_zc, za);
373         if (unlikely(rc != 0))
374                 RETURN(rc);
375
376         /* inode accounting is maintained by DMU since 0.7.0 */
377         strncpy(info->oti_buf, OSD_DMU_USEROBJ_PREFIX,
378                 OSD_DMU_USEROBJ_PREFIX_LEN);
379         strlcpy(info->oti_buf + OSD_DMU_USEROBJ_PREFIX_LEN, za->za_name,
380                 sizeof(info->oti_buf) - OSD_DMU_USEROBJ_PREFIX_LEN);
381         rc = osd_zap_lookup(osd, it->oiq_obj->oo_dn->dn_object,
382                             it->oiq_obj->oo_dn, info->oti_buf, sizeof(uint64_t),
383                             1, &rec->ispace);
384         if (rc == -ENOENT)
385                 /* user/group has not created any file yet */
386                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
387                        osd->od_svname, info->oti_buf);
388         else if (rc)
389                 RETURN(rc);
390
391         RETURN(0);
392 }
393
394 /**
395  * Returns cookie for current Iterator position.
396  *
397  * \param  di    - osd iterator
398  */
399 static __u64 osd_it_acct_store(const struct lu_env *env,
400                                const struct dt_it *di)
401 {
402         struct osd_it_quota *it = (struct osd_it_quota *)di;
403         ENTRY;
404         it->oiq_reset = 0;
405         RETURN(osd_zap_cursor_serialize(it->oiq_zc));
406 }
407
408 /**
409  * Restore iterator from cookie. if the \a hash isn't found,
410  * restore the first valid record.
411  *
412  * \param  di    - osd iterator
413  * \param  hash  - iterator location cookie
414  *
415  * \retval +ve  - di points to exact matched key
416  * \retval  0   - di points to the first valid record
417  * \retval -ve  - failure
418  */
419 static int osd_it_acct_load(const struct lu_env *env,
420                             const struct dt_it *di, __u64 hash)
421 {
422         struct osd_it_quota     *it  = (struct osd_it_quota *)di;
423         struct osd_device       *osd = osd_obj2dev(it->oiq_obj);
424         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
425         zap_cursor_t            *zc;
426         int                      rc;
427         ENTRY;
428
429         /* create new cursor pointing to the new hash */
430         rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
431         if (rc)
432                 RETURN(rc);
433         osd_zap_cursor_fini(it->oiq_zc);
434         it->oiq_zc = zc;
435         it->oiq_reset = 0;
436
437         rc = osd_zap_locate(it, za);
438         if (rc == 0)
439                 rc = 1;
440         else if (rc == -ENOENT)
441                 rc = 0;
442         RETURN(rc);
443 }
444
445 /**
446  * Move Iterator to record specified by \a key, if the \a key isn't found,
447  * move to the first valid record.
448  *
449  * \param  di   - osd iterator
450  * \param  key  - uid or gid or projid
451  *
452  * \retval +ve  - di points to exact matched key
453  * \retval 0    - di points to the first valid record
454  * \retval -ve  - failure
455  */
456 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
457                 const struct dt_key *key)
458 {
459         ENTRY;
460
461         /* XXX: like osd_zap_it_get(), API is currently broken */
462         LASSERT(*((__u64 *)key) == 0);
463
464         RETURN(osd_it_acct_load(env, di, 0));
465 }
466
467 /**
468  * Release Iterator
469  *
470  * \param  di   - osd iterator
471  */
472 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
473 {
474 }
475
476 /**
477  * Index and Iterator operations for accounting objects
478  */
479 const struct dt_index_operations osd_acct_index_ops = {
480         .dio_lookup = osd_acct_index_lookup,
481         .dio_it     = {
482                 .init           = osd_it_acct_init,
483                 .fini           = osd_it_acct_fini,
484                 .get            = osd_it_acct_get,
485                 .put            = osd_it_acct_put,
486                 .next           = osd_it_acct_next,
487                 .key            = osd_it_acct_key,
488                 .key_size       = osd_it_acct_key_size,
489                 .rec            = osd_it_acct_rec,
490                 .store          = osd_it_acct_store,
491                 .load           = osd_it_acct_load
492         }
493 };
494
495 /**
496  * Quota Enforcement Management
497  */
498
499 /*
500  * Wrapper for qsd_op_begin().
501  *
502  * \param env    - the environment passed by the caller
503  * \param osd    - is the osd_device
504  * \param uid    - user id of the inode
505  * \param gid    - group id of the inode
506  * \param projid - project id of the inode
507  * \param space  - how many blocks/inodes will be consumed/released
508  * \param oh     - osd transaction handle
509  * \param flags  - if the operation is write, return no user quota, no
510  *                  group quota, or sync commit flags to the caller
511  * \param osd_qid_declare_flags - indicate this is a inode/block accounting
512  *                  and whether changes are performed by root user
513  *
514  * \retval 0      - success
515  * \retval -ve    - failure
516  */
517 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
518                       qid_t uid, qid_t gid, qid_t projid, long long space,
519                       struct osd_thandle *oh,
520                       enum osd_quota_local_flags *local_flags,
521                       enum osd_qid_declare_flags osd_qid_declare_flags)
522 {
523         struct osd_thread_info *info = osd_oti_get(env);
524         struct lquota_id_info *qi = &info->oti_qi;
525         struct qsd_instance *qsd = NULL;
526         int rcu, rcg, rcp = 0; /* user & group & project rc */
527         struct thandle *th = &oh->ot_super;
528         bool force = !!(osd_qid_declare_flags & OSD_QID_FORCE) ||
529                         th->th_ignore_quota;
530         ENTRY;
531
532         if (osd_qid_declare_flags & OSD_QID_INODE)
533                 qsd = osd->od_quota_slave_md;
534         else if (osd_qid_declare_flags & OSD_QID_BLK)
535                 qsd = osd->od_quota_slave_dt;
536         else
537                 RETURN(0);
538
539         if (unlikely(qsd == NULL))
540                 /* quota slave instance hasn't been allocated yet */
541                 RETURN(0);
542
543         /* let's start with user quota */
544         qi->lqi_id.qid_uid = uid;
545         qi->lqi_type       = USRQUOTA;
546         qi->lqi_space      = space;
547         qi->lqi_is_blk     = !!(osd_qid_declare_flags & OSD_QID_BLK);
548         rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, local_flags);
549         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
550                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
551                 rcu = 0;
552
553         /* For non-fatal error, we want to continue to get the noquota flags
554          * for group id. This is only for commit write, which has @flags passed
555          * in. See osd_declare_write_commit().
556          * When force is set to true, we also want to proceed with the gid */
557         if (rcu && (rcu != -EDQUOT || local_flags == NULL))
558                 RETURN(rcu);
559
560         /* and now group quota */
561         qi->lqi_id.qid_gid = gid;
562         qi->lqi_type       = GRPQUOTA;
563         rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, local_flags);
564         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
565                 /* as before, ignore EDQUOT & EINPROGRESS for root */
566                 rcg = 0;
567
568 #ifdef ZFS_PROJINHERIT
569         if (rcg && (rcg != -EDQUOT || local_flags == NULL))
570                 RETURN(rcg);
571
572         /* for project quota */
573         if (osd->od_projectused_dn) {
574                 qi->lqi_id.qid_projid = projid;
575                 qi->lqi_type = PRJQUOTA;
576                 rcp = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi,
577                                    local_flags);
578                 if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
579                         rcp = 0;
580         }
581 #endif
582
583         RETURN(rcu ? rcu : (rcg ? rcg : rcp));
584 }