4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012, 2017, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann@whamcloud.com>
30 #include <dt_object.h>
31 #include <lustre_quota.h>
33 #include "osd_internal.h"
36 * Helper function to estimate the number of inodes in use for the given
37 * uid/gid/projid from the block usage
39 static uint64_t osd_objset_user_iused(struct osd_device *osd, uint64_t uidbytes)
41 uint64_t refdbytes, availbytes, usedobjs, availobjs;
42 uint64_t uidobjs, bshift;
44 /* get fresh statfs info */
45 dmu_objset_space(osd->od_os, &refdbytes, &availbytes,
46 &usedobjs, &availobjs);
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);
53 /* if we have at least 1 byte, we have at least one dnode ... */
54 uidobjs = max_t(uint64_t, uidobjs, 1);
60 * Space Accounting Management
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.
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.
75 * \retval +ve - success : exact match
76 * \retval -ve - failure
78 static int osd_acct_index_lookup(const struct lu_env *env,
79 struct dt_object *dtobj,
81 const struct dt_key *dtkey)
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);
93 rec->bspace = rec->ispace = 0;
95 /* convert the 64-bit uid/gid/projid into a string */
96 snprintf(buf, buflen, "%llx", *((__u64 *)dtkey));
98 CDEBUG(D_QUOTA, "%s: miss accounting obj for %s\n",
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,
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. */
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);
125 snprintf(buf, buflen, OSD_DMU_USEROBJ_PREFIX "%llx",
127 rc = osd_zap_lookup(osd, dn->dn_object, dn, buf,
128 sizeof(uint64_t), 1, &rec->ispace);
131 "%s: id %s not found dnode accounting\n",
132 osd->od_svname, buf);
133 /* -ENOENT is normal case, convert it as 1. */
135 } else if (rc == 0) {
144 * Initialize osd Iterator for given osd index object.
146 * \param dt - osd index object
147 * \param attr - not used
149 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
150 struct dt_object *dt,
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;
162 CDEBUG(D_QUOTA, "%s: Not found in DMU accounting ZAP\n",
165 RETURN(ERR_PTR(-ENOENT));
169 RETURN(ERR_PTR(-ENOMEM));
173 RETURN(ERR_PTR(-ENOMEM));
175 memset(it, 0, sizeof(*it));
176 it->oiq_oid = dn->dn_object;
178 /* initialize zap cursor */
179 rc = osd_zap_cursor_init(&it->oiq_zc, osd->od_os, it->oiq_oid, 0);
185 /* take object reference */
186 lu_object_get(&dt->do_lu);
187 it->oiq_obj = osd_dt_obj(dt);
190 RETURN((struct dt_it *)it);
194 * Free given iterator.
196 * \param di - osd iterator
198 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
200 struct osd_it_quota *it = (struct osd_it_quota *)di;
203 osd_zap_cursor_fini(it->oiq_zc);
204 osd_object_put(env, it->oiq_obj);
211 * Locate the first entry that is for space accounting.
213 static int osd_zap_locate(struct osd_it_quota *it, zap_attribute_t *za)
219 rc = -zap_cursor_retrieve(it->oiq_zc, za);
223 if (strncmp(za->za_name, OSD_DMU_USEROBJ_PREFIX,
224 OSD_DMU_USEROBJ_PREFIX_LEN))
227 zap_cursor_advance(it->oiq_zc);
234 * Move on to the next valid entry.
236 * \param di - osd iterator
238 * \retval +ve - iterator reached the end
239 * \retval 0 - iterator has not reached the end yet
240 * \retval -ve - unexpected failure
242 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
244 struct osd_it_quota *it = (struct osd_it_quota *)di;
245 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
249 if (it->oiq_reset == 0)
250 zap_cursor_advance(it->oiq_zc);
253 rc = osd_zap_locate(it, za);
254 RETURN(rc == -ENOENT ? 1 : rc);
258 * Return pointer to the key under iterator.
260 * \param di - osd iterator
262 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
263 const struct dt_it *di)
265 struct osd_it_quota *it = (struct osd_it_quota *)di;
266 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
271 rc = osd_zap_locate(it, za);
275 rc = kstrtoull(za->za_name, 16, &it->oiq_id);
277 CERROR("couldn't parse name %s\n", za->za_name);
279 RETURN((struct dt_key *) &it->oiq_id);
283 * Return size of key under iterator (in bytes)
285 * \param di - osd iterator
287 static int osd_it_acct_key_size(const struct lu_env *env,
288 const struct dt_it *di)
291 RETURN((int)sizeof(uint64_t));
295 * zap_cursor_retrieve read from current record.
296 * to read bytes we need to call zap_lookup explicitly.
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,
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);
309 rc = -zap_cursor_retrieve(zc, za);
310 if (unlikely(rc != 0))
313 if (unlikely(za->za_integer_length <= 0))
316 actual_size = za->za_integer_length * za->za_num_integers;
318 if (actual_size > buf_size) {
319 actual_size = buf_size;
320 buf_size = actual_size / za->za_integer_length;
322 buf_size = za->za_num_integers;
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);
330 *bytes_read = actual_size;
336 * Return pointer to the record under iterator.
338 * \param di - osd iterator
339 * \param attr - not used
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)
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);
356 rec->ispace = rec->bspace = 0;
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);
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);
371 /* retrieve key associated with the current cursor */
372 rc = -zap_cursor_retrieve(it->oiq_zc, za);
373 if (unlikely(rc != 0))
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),
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);
395 * Returns cookie for current Iterator position.
397 * \param di - osd iterator
399 static __u64 osd_it_acct_store(const struct lu_env *env,
400 const struct dt_it *di)
402 struct osd_it_quota *it = (struct osd_it_quota *)di;
405 RETURN(osd_zap_cursor_serialize(it->oiq_zc));
409 * Restore iterator from cookie. if the \a hash isn't found,
410 * restore the first valid record.
412 * \param di - osd iterator
413 * \param hash - iterator location cookie
415 * \retval +ve - di points to exact matched key
416 * \retval 0 - di points to the first valid record
417 * \retval -ve - failure
419 static int osd_it_acct_load(const struct lu_env *env,
420 const struct dt_it *di, __u64 hash)
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;
429 /* create new cursor pointing to the new hash */
430 rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
433 osd_zap_cursor_fini(it->oiq_zc);
437 rc = osd_zap_locate(it, za);
440 else if (rc == -ENOENT)
446 * Move Iterator to record specified by \a key, if the \a key isn't found,
447 * move to the first valid record.
449 * \param di - osd iterator
450 * \param key - uid or gid or projid
452 * \retval +ve - di points to exact matched key
453 * \retval 0 - di points to the first valid record
454 * \retval -ve - failure
456 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
457 const struct dt_key *key)
461 /* XXX: like osd_zap_it_get(), API is currently broken */
462 LASSERT(*((__u64 *)key) == 0);
464 RETURN(osd_it_acct_load(env, di, 0));
470 * \param di - osd iterator
472 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
477 * Index and Iterator operations for accounting objects
479 const struct dt_index_operations osd_acct_index_ops = {
480 .dio_lookup = osd_acct_index_lookup,
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
496 * Quota Enforcement Management
500 * Wrapper for qsd_op_begin().
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
514 * \retval 0 - success
515 * \retval -ve - failure
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)
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) ||
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;
539 if (unlikely(qsd == NULL))
540 /* quota slave instance hasn't been allocated yet */
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 */
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))
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 */
568 #ifdef ZFS_PROJINHERIT
569 if (rcg && (rcg != -EDQUOT || local_flags == NULL))
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,
578 if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
583 RETURN(rcu ? rcu : (rcg ? rcg : rcp));