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 <lustre_quota.h>
32 #include "osd_internal.h"
35 * Helper function to estimate the number of inodes in use for the given
36 * uid/gid/projid from the block usage
38 static uint64_t osd_objset_user_iused(struct osd_device *osd, uint64_t uidbytes)
40 uint64_t refdbytes, availbytes, usedobjs, availobjs;
41 uint64_t uidobjs, bshift;
43 /* get fresh statfs info */
44 dmu_objset_space(osd->od_os, &refdbytes, &availbytes,
45 &usedobjs, &availobjs);
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);
52 /* if we have at least 1 byte, we have at least one dnode ... */
53 uidobjs = max_t(uint64_t, uidobjs, 1);
59 * Space Accounting Management
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.
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.
74 * \retval +ve - success : exact match
75 * \retval -ve - failure
77 static int osd_acct_index_lookup(const struct lu_env *env,
78 struct dt_object *dtobj,
80 const struct dt_key *dtkey)
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);
92 rec->bspace = rec->ispace = 0;
94 /* convert the 64-bit uid/gid/projid into a string */
95 snprintf(buf, buflen, "%llx", *((__u64 *)dtkey));
97 CDEBUG(D_QUOTA, "%s: miss accounting obj for %s\n",
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,
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. */
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);
124 snprintf(buf, buflen, OSD_DMU_USEROBJ_PREFIX "%llx",
126 rc = osd_zap_lookup(osd, dn->dn_object, dn, buf,
127 sizeof(uint64_t), 1, &rec->ispace);
130 "%s: id %s not found dnode accounting\n",
131 osd->od_svname, buf);
132 /* -ENOENT is normal case, convert it as 1. */
134 } else if (rc == 0) {
143 * Initialize osd Iterator for given osd index object.
145 * \param dt - osd index object
146 * \param attr - not used
148 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
149 struct dt_object *dt,
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;
161 CDEBUG(D_QUOTA, "%s: Not found in DMU accounting ZAP\n",
164 RETURN(ERR_PTR(-ENOENT));
168 RETURN(ERR_PTR(-ENOMEM));
172 RETURN(ERR_PTR(-ENOMEM));
174 memset(it, 0, sizeof(*it));
175 it->oiq_oid = dn->dn_object;
177 /* initialize zap cursor */
178 rc = osd_zap_cursor_init(&it->oiq_zc, osd->od_os, it->oiq_oid, 0);
184 /* take object reference */
185 lu_object_get(&dt->do_lu);
186 it->oiq_obj = osd_dt_obj(dt);
189 RETURN((struct dt_it *)it);
193 * Free given iterator.
195 * \param di - osd iterator
197 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
199 struct osd_it_quota *it = (struct osd_it_quota *)di;
202 osd_zap_cursor_fini(it->oiq_zc);
203 osd_object_put(env, it->oiq_obj);
210 * Locate the first entry that is for space accounting.
212 static int osd_zap_locate(struct osd_it_quota *it, zap_attribute_t *za)
218 rc = -zap_cursor_retrieve(it->oiq_zc, za);
222 if (strncmp(za->za_name, OSD_DMU_USEROBJ_PREFIX,
223 OSD_DMU_USEROBJ_PREFIX_LEN))
226 zap_cursor_advance(it->oiq_zc);
233 * Move on to the next valid entry.
235 * \param di - osd iterator
237 * \retval +ve - iterator reached the end
238 * \retval 0 - iterator has not reached the end yet
239 * \retval -ve - unexpected failure
241 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
243 struct osd_it_quota *it = (struct osd_it_quota *)di;
244 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
248 if (it->oiq_reset == 0)
249 zap_cursor_advance(it->oiq_zc);
252 rc = osd_zap_locate(it, za);
253 RETURN(rc == -ENOENT ? 1 : rc);
257 * Return pointer to the key under iterator.
259 * \param di - osd iterator
261 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
262 const struct dt_it *di)
264 struct osd_it_quota *it = (struct osd_it_quota *)di;
265 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
270 rc = osd_zap_locate(it, za);
274 rc = kstrtoull(za->za_name, 16, &it->oiq_id);
276 CERROR("couldn't parse name %s\n", za->za_name);
278 RETURN((struct dt_key *) &it->oiq_id);
282 * Return size of key under iterator (in bytes)
284 * \param di - osd iterator
286 static int osd_it_acct_key_size(const struct lu_env *env,
287 const struct dt_it *di)
290 RETURN((int)sizeof(uint64_t));
294 * zap_cursor_retrieve read from current record.
295 * to read bytes we need to call zap_lookup explicitly.
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,
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);
308 rc = -zap_cursor_retrieve(zc, za);
309 if (unlikely(rc != 0))
312 if (unlikely(za->za_integer_length <= 0))
315 actual_size = za->za_integer_length * za->za_num_integers;
317 if (actual_size > buf_size) {
318 actual_size = buf_size;
319 buf_size = actual_size / za->za_integer_length;
321 buf_size = za->za_num_integers;
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);
329 *bytes_read = actual_size;
335 * Return pointer to the record under iterator.
337 * \param di - osd iterator
338 * \param attr - not used
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)
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);
355 rec->ispace = rec->bspace = 0;
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);
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);
370 /* retrieve key associated with the current cursor */
371 rc = -zap_cursor_retrieve(it->oiq_zc, za);
372 if (unlikely(rc != 0))
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),
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);
394 * Returns cookie for current Iterator position.
396 * \param di - osd iterator
398 static __u64 osd_it_acct_store(const struct lu_env *env,
399 const struct dt_it *di)
401 struct osd_it_quota *it = (struct osd_it_quota *)di;
404 RETURN(osd_zap_cursor_serialize(it->oiq_zc));
408 * Restore iterator from cookie. if the \a hash isn't found,
409 * restore the first valid record.
411 * \param di - osd iterator
412 * \param hash - iterator location cookie
414 * \retval +ve - di points to exact matched key
415 * \retval 0 - di points to the first valid record
416 * \retval -ve - failure
418 static int osd_it_acct_load(const struct lu_env *env,
419 const struct dt_it *di, __u64 hash)
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;
428 /* create new cursor pointing to the new hash */
429 rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
432 osd_zap_cursor_fini(it->oiq_zc);
436 rc = osd_zap_locate(it, za);
439 else if (rc == -ENOENT)
445 * Move Iterator to record specified by \a key, if the \a key isn't found,
446 * move to the first valid record.
448 * \param di - osd iterator
449 * \param key - uid or gid or projid
451 * \retval +ve - di points to exact matched key
452 * \retval 0 - di points to the first valid record
453 * \retval -ve - failure
455 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
456 const struct dt_key *key)
460 /* XXX: like osd_zap_it_get(), API is currently broken */
461 LASSERT(*((__u64 *)key) == 0);
463 RETURN(osd_it_acct_load(env, di, 0));
469 * \param di - osd iterator
471 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
476 * Index and Iterator operations for accounting objects
478 const struct dt_index_operations osd_acct_index_ops = {
479 .dio_lookup = osd_acct_index_lookup,
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
495 * Quota Enforcement Management
499 * Wrapper for qsd_op_begin().
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
513 * \retval 0 - success
514 * \retval -ve - failure
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)
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) ||
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;
538 if (unlikely(qsd == NULL))
539 /* quota slave instance hasn't been allocated yet */
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 */
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))
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 */
567 #ifdef ZFS_PROJINHERIT
568 if (rcg && (rcg != -EDQUOT || local_flags == NULL))
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,
577 if (force && (rcp == -EDQUOT || rcp == -EINPROGRESS))
582 RETURN(rcu ? rcu : (rcg ? rcg : rcp));