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