Whamcloud - gitweb
LU-7991 osd-zfs: remove obsolete quota code
[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, 2015, 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 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.
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 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 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  * Move on to the next valid entry.
211  *
212  * \param  di   - osd iterator
213  *
214  * \retval +ve  - iterator reached the end
215  * \retval   0  - iterator has not reached the end yet
216  * \retval -ve  - unexpected failure
217  */
218 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
219 {
220         struct osd_it_quota     *it = (struct osd_it_quota *)di;
221         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
222         int                      rc;
223         ENTRY;
224
225         if (it->oiq_reset == 0)
226                 zap_cursor_advance(it->oiq_zc);
227         it->oiq_reset = 0;
228         rc = -zap_cursor_retrieve(it->oiq_zc, za);
229         if (rc == -ENOENT) /* reached the end */
230                 rc = 1;
231         RETURN(rc);
232 }
233
234 /**
235  * Return pointer to the key under iterator.
236  *
237  * \param  di   - osd iterator
238  */
239 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
240                                       const struct dt_it *di)
241 {
242         struct osd_it_quota     *it = (struct osd_it_quota *)di;
243         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
244         int                      rc;
245         ENTRY;
246
247         it->oiq_reset = 0;
248         rc = -zap_cursor_retrieve(it->oiq_zc, za);
249         if (rc)
250                 RETURN(ERR_PTR(rc));
251         rc = kstrtoull(za->za_name, 16, &it->oiq_id);
252
253         RETURN((struct dt_key *) &it->oiq_id);
254 }
255
256 /**
257  * Return size of key under iterator (in bytes)
258  *
259  * \param  di   - osd iterator
260  */
261 static int osd_it_acct_key_size(const struct lu_env *env,
262                                 const struct dt_it *di)
263 {
264         ENTRY;
265         RETURN((int)sizeof(uint64_t));
266 }
267
268 /*
269  * zap_cursor_retrieve read from current record.
270  * to read bytes we need to call zap_lookup explicitly.
271  */
272 static int osd_zap_cursor_retrieve_value(const struct lu_env *env,
273                                          struct osd_it_quota *it,
274                                          char *buf, int buf_size,
275                                          int *bytes_read)
276 {
277         const struct lu_fid *fid = lu_object_fid(&it->oiq_obj->oo_dt.do_lu);
278         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
279         zap_cursor_t *zc = it->oiq_zc;
280         struct osd_device *osd = osd_obj2dev(it->oiq_obj);
281         int rc, actual_size;
282
283         rc = -zap_cursor_retrieve(zc, za);
284         if (unlikely(rc != 0))
285                 return rc;
286
287         if (unlikely(za->za_integer_length <= 0))
288                 return -ERANGE;
289
290         actual_size = za->za_integer_length * za->za_num_integers;
291
292         if (actual_size > buf_size) {
293                 actual_size = buf_size;
294                 buf_size = actual_size / za->za_integer_length;
295         } else {
296                 buf_size = za->za_num_integers;
297         }
298
299         /* use correct special ID to request bytes used */
300         rc = osd_zap_lookup(osd, fid_oid(fid) == ACCT_GROUP_OID ?
301                             DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT, NULL,
302                             za->za_name, za->za_integer_length, buf_size, buf);
303         if (likely(rc == 0))
304                 *bytes_read = actual_size;
305
306         return rc;
307 }
308
309 /**
310  * Return pointer to the record under iterator.
311  *
312  * \param  di    - osd iterator
313  * \param  attr  - not used
314  */
315 static int osd_it_acct_rec(const struct lu_env *env,
316                            const struct dt_it *di,
317                            struct dt_rec *dtrec, __u32 attr)
318 {
319         struct osd_thread_info  *info = osd_oti_get(env);
320         zap_attribute_t         *za = &info->oti_za;
321         struct osd_it_quota     *it = (struct osd_it_quota *)di;
322         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
323         struct osd_object       *obj = it->oiq_obj;
324         struct osd_device       *osd = osd_obj2dev(obj);
325         int                      bytes_read;
326         int                      rc;
327         ENTRY;
328
329         it->oiq_reset = 0;
330         rec->ispace = rec->bspace = 0;
331
332         /* retrieve block usage from the DMU accounting object */
333         rc = osd_zap_cursor_retrieve_value(env, it, (char *)&rec->bspace,
334                                            sizeof(uint64_t), &bytes_read);
335         if (rc)
336                 RETURN(rc);
337
338         if (!osd_dmu_userobj_accounting_available(osd)) {
339                 if (rec->bspace != 0)
340                         /* estimate #inodes in use */
341                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
342                 RETURN(0);
343         }
344
345         /* retrieve key associated with the current cursor */
346         rc = -zap_cursor_retrieve(it->oiq_zc, za);
347         if (unlikely(rc != 0))
348                 RETURN(rc);
349
350         /* inode accounting is not maintained by DMU, so we use our own ZAP to
351          * track inode usage */
352         rc = osd_zap_lookup(osd, it->oiq_obj->oo_dn->dn_object,
353                             it->oiq_obj->oo_dn, za->za_name, sizeof(uint64_t),
354                             1, &rec->ispace);
355         if (rc == -ENOENT)
356                 /* user/group has not created any file yet */
357                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
358                        osd->od_svname, za->za_name);
359         else if (rc)
360                 RETURN(rc);
361
362         RETURN(0);
363 }
364
365 /**
366  * Returns cookie for current Iterator position.
367  *
368  * \param  di    - osd iterator
369  */
370 static __u64 osd_it_acct_store(const struct lu_env *env,
371                                const struct dt_it *di)
372 {
373         struct osd_it_quota *it = (struct osd_it_quota *)di;
374         ENTRY;
375         it->oiq_reset = 0;
376         RETURN(osd_zap_cursor_serialize(it->oiq_zc));
377 }
378
379 /**
380  * Restore iterator from cookie. if the \a hash isn't found,
381  * restore the first valid record.
382  *
383  * \param  di    - osd iterator
384  * \param  hash  - iterator location cookie
385  *
386  * \retval +ve  - di points to exact matched key
387  * \retval  0   - di points to the first valid record
388  * \retval -ve  - failure
389  */
390 static int osd_it_acct_load(const struct lu_env *env,
391                             const struct dt_it *di, __u64 hash)
392 {
393         struct osd_it_quota     *it  = (struct osd_it_quota *)di;
394         struct osd_device       *osd = osd_obj2dev(it->oiq_obj);
395         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
396         zap_cursor_t            *zc;
397         int                      rc;
398         ENTRY;
399
400         /* create new cursor pointing to the new hash */
401         rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
402         if (rc)
403                 RETURN(rc);
404         osd_zap_cursor_fini(it->oiq_zc);
405         it->oiq_zc = zc;
406         it->oiq_reset = 0;
407
408         rc = -zap_cursor_retrieve(it->oiq_zc, za);
409         if (rc == 0)
410                 rc = 1;
411         else if (rc == -ENOENT)
412                 rc = 0;
413
414         RETURN(rc);
415 }
416
417 /**
418  * Move Iterator to record specified by \a key, if the \a key isn't found,
419  * move to the first valid record.
420  *
421  * \param  di   - osd iterator
422  * \param  key  - uid or gid
423  *
424  * \retval +ve  - di points to exact matched key
425  * \retval 0    - di points to the first valid record
426  * \retval -ve  - failure
427  */
428 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
429                 const struct dt_key *key)
430 {
431         ENTRY;
432
433         /* XXX: like osd_zap_it_get(), API is currently broken */
434         LASSERT(*((__u64 *)key) == 0);
435
436         RETURN(osd_it_acct_load(env, di, 0));
437 }
438
439 /**
440  * Release Iterator
441  *
442  * \param  di   - osd iterator
443  */
444 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
445 {
446 }
447
448 /**
449  * Index and Iterator operations for accounting objects
450  */
451 const struct dt_index_operations osd_acct_index_ops = {
452         .dio_lookup = osd_acct_index_lookup,
453         .dio_it     = {
454                 .init           = osd_it_acct_init,
455                 .fini           = osd_it_acct_fini,
456                 .get            = osd_it_acct_get,
457                 .put            = osd_it_acct_put,
458                 .next           = osd_it_acct_next,
459                 .key            = osd_it_acct_key,
460                 .key_size       = osd_it_acct_key_size,
461                 .rec            = osd_it_acct_rec,
462                 .store          = osd_it_acct_store,
463                 .load           = osd_it_acct_load
464         }
465 };
466
467 /**
468  * Quota Enforcement Management
469  */
470
471 /*
472  * Wrapper for qsd_op_begin().
473  *
474  * \param env    - the environment passed by the caller
475  * \param osd    - is the osd_device
476  * \param uid    - user id of the inode
477  * \param gid    - group id of the inode
478  * \param space  - how many blocks/inodes will be consumed/released
479  * \param oh     - osd transaction handle
480  * \param is_blk - block quota or inode quota?
481  * \param flags  - if the operation is write, return no user quota, no
482  *                  group quota, or sync commit flags to the caller
483  * \param force  - set to 1 when changes are performed by root user and thus
484  *                  can't failed with EDQUOT
485  *
486  * \retval 0      - success
487  * \retval -ve    - failure
488  */
489 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
490                       qid_t uid, qid_t gid, long long space,
491                       struct osd_thandle *oh, bool is_blk, int *flags,
492                       bool force)
493 {
494         struct osd_thread_info  *info = osd_oti_get(env);
495         struct lquota_id_info   *qi = &info->oti_qi;
496         struct qsd_instance     *qsd = osd->od_quota_slave;
497         int                      rcu, rcg; /* user & group rc */
498         ENTRY;
499
500         if (unlikely(qsd == NULL))
501                 /* quota slave instance hasn't been allocated yet */
502                 RETURN(0);
503
504         /* let's start with user quota */
505         qi->lqi_id.qid_uid = uid;
506         qi->lqi_type       = USRQUOTA;
507         qi->lqi_space      = space;
508         qi->lqi_is_blk     = is_blk;
509         rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
510
511         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
512                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
513                 rcu = 0;
514
515         /* For non-fatal error, we want to continue to get the noquota flags
516          * for group id. This is only for commit write, which has @flags passed
517          * in. See osd_declare_write_commit().
518          * When force is set to true, we also want to proceed with the gid */
519         if (rcu && (rcu != -EDQUOT || flags == NULL))
520                 RETURN(rcu);
521
522         /* and now group quota */
523         qi->lqi_id.qid_gid = gid;
524         qi->lqi_type       = GRPQUOTA;
525         rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
526
527         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
528                 /* as before, ignore EDQUOT & EINPROGRESS for root */
529                 rcg = 0;
530
531         RETURN(rcu ? rcu : rcg);
532 }