Whamcloud - gitweb
LU-3536 osd: allocate it for each iteration.
[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, 2014, 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 retrieve DMU object id from fid for accounting object
36  */
37 uint64_t osd_quota_fid2dmu(const struct lu_fid *fid)
38 {
39         LASSERT(fid_is_acct(fid));
40         if (fid_oid(fid) == ACCT_GROUP_OID)
41                 return DMU_GROUPUSED_OBJECT;
42         return DMU_USERUSED_OBJECT;
43 }
44
45 /**
46  * Helper function to estimate the number of inodes in use for a give uid/gid
47  * from the block usage
48  */
49 static uint64_t osd_objset_user_iused(struct osd_device *osd, uint64_t uidbytes)
50 {
51         uint64_t refdbytes, availbytes, usedobjs, availobjs;
52         uint64_t uidobjs;
53
54         /* get fresh statfs info */
55         dmu_objset_space(osd->od_os, &refdbytes, &availbytes,
56                          &usedobjs, &availobjs);
57
58         /* estimate the number of objects based on the disk usage */
59         uidobjs = osd_objs_count_estimate(refdbytes, usedobjs,
60                                           uidbytes >> SPA_MAXBLOCKSHIFT);
61         if (uidbytes > 0)
62                 /* if we have at least 1 byte, we have at least one dnode ... */
63                 uidobjs = max_t(uint64_t, uidobjs, 1);
64
65         return uidobjs;
66 }
67
68 /**
69  * Space Accounting Management
70  */
71
72 /**
73  * Return space usage consumed by a given uid or gid.
74  * Block usage is accurrate since it is maintained by DMU itself.
75  * However, DMU does not provide inode accounting, so the #inodes in use
76  * is estimated from the block usage and statfs information.
77  *
78  * \param env   - is the environment passed by the caller
79  * \param dtobj - is the accounting object
80  * \param dtrec - is the record to fill with space usage information
81  * \param dtkey - is the id the of the user or group for which we would
82  *                like to access disk usage.
83  * \param capa - is the capability, not used.
84  *
85  * \retval +ve - success : exact match
86  * \retval -ve - failure
87  */
88 static int osd_acct_index_lookup(const struct lu_env *env,
89                                 struct dt_object *dtobj,
90                                 struct dt_rec *dtrec,
91                                 const struct dt_key *dtkey,
92                                 struct lustre_capa *capa)
93 {
94         struct osd_thread_info  *info = osd_oti_get(env);
95         char                    *buf  = info->oti_buf;
96         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
97         struct osd_object       *obj = osd_dt_obj(dtobj);
98         struct osd_device       *osd = osd_obj2dev(obj);
99         int                      rc;
100         uint64_t                 oid;
101         ENTRY;
102
103         rec->bspace = rec->ispace = 0;
104
105         /* convert the 64-bit uid/gid into a string */
106         sprintf(buf, "%llx", *((__u64 *)dtkey));
107         /* fetch DMU object ID (DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT) to be
108          * used */
109         oid = osd_quota_fid2dmu(lu_object_fid(&dtobj->do_lu));
110
111         /* disk usage (in bytes) is maintained by DMU.
112          * DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT are special objects which
113          * not associated with any dmu_but_t (see dnode_special_open()).
114          * As a consequence, we cannot use udmu_zap_lookup() here since it
115          * requires a valid oo_db. */
116         rc = -zap_lookup(osd->od_os, oid, buf, sizeof(uint64_t), 1,
117                         &rec->bspace);
118         if (rc == -ENOENT)
119                 /* user/group has not created anything yet */
120                 CDEBUG(D_QUOTA, "%s: id %s not found in DMU accounting ZAP\n",
121                        osd->od_svname, buf);
122         else if (rc)
123                 RETURN(rc);
124
125         if (osd->od_quota_iused_est) {
126                 if (rec->bspace != 0)
127                         /* estimate #inodes in use */
128                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
129                 RETURN(+1);
130         }
131
132         /* as for inode accounting, it is not maintained by DMU, so we just
133          * use our own ZAP to track inode usage */
134         rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
135                          buf, sizeof(uint64_t), 1, &rec->ispace);
136         if (rc == -ENOENT)
137                 /* user/group has not created any file yet */
138                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
139                        osd->od_svname, buf);
140         else if (rc)
141                 RETURN(rc);
142
143         RETURN(+1);
144 }
145
146 /**
147  * Initialize osd Iterator for given osd index object.
148  *
149  * \param  dt    - osd index object
150  * \param  attr  - not used
151  * \param  capa  - BYPASS_CAPA
152  */
153 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
154                                       struct dt_object *dt,
155                                       __u32 attr,
156                                       struct lustre_capa *capa)
157 {
158         struct osd_thread_info  *info = osd_oti_get(env);
159         struct osd_it_quota     *it;
160         struct lu_object        *lo   = &dt->do_lu;
161         struct osd_device       *osd  = osd_dev(lo->lo_dev);
162         int                      rc;
163         ENTRY;
164
165         LASSERT(lu_object_exists(lo));
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 = osd_quota_fid2dmu(lu_object_fid(lo));
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(lo);
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         lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
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                                          zap_cursor_t *zc,  char *buf,
274                                          int buf_size, int *bytes_read)
275 {
276         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
277         int rc, actual_size;
278
279         rc = -zap_cursor_retrieve(zc, za);
280         if (unlikely(rc != 0))
281                 return -rc;
282
283         if (unlikely(za->za_integer_length <= 0))
284                 return -ERANGE;
285
286         actual_size = za->za_integer_length * za->za_num_integers;
287
288         if (actual_size > buf_size) {
289                 actual_size = buf_size;
290                 buf_size = actual_size / za->za_integer_length;
291         } else {
292                 buf_size = za->za_num_integers;
293         }
294
295         rc = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
296                          za->za_name, za->za_integer_length,
297                          buf_size, buf);
298
299         if (likely(rc == 0))
300                 *bytes_read = actual_size;
301
302         return rc;
303 }
304
305 /**
306  * Return pointer to the record under iterator.
307  *
308  * \param  di    - osd iterator
309  * \param  attr  - not used
310  */
311 static int osd_it_acct_rec(const struct lu_env *env,
312                            const struct dt_it *di,
313                            struct dt_rec *dtrec, __u32 attr)
314 {
315         struct osd_thread_info  *info = osd_oti_get(env);
316         zap_attribute_t         *za = &info->oti_za;
317         struct osd_it_quota     *it = (struct osd_it_quota *)di;
318         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
319         struct osd_object       *obj = it->oiq_obj;
320         struct osd_device       *osd = osd_obj2dev(obj);
321         int                      bytes_read;
322         int                      rc;
323         ENTRY;
324
325         it->oiq_reset = 0;
326         rec->ispace = rec->bspace = 0;
327
328         /* retrieve block usage from the DMU accounting object */
329         rc = osd_zap_cursor_retrieve_value(env, it->oiq_zc,
330                                            (char *)&rec->bspace,
331                                            sizeof(uint64_t), &bytes_read);
332         if (rc)
333                 RETURN(rc);
334
335         if (osd->od_quota_iused_est) {
336                 if (rec->bspace != 0)
337                         /* estimate #inodes in use */
338                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
339                 RETURN(0);
340         }
341
342         /* retrieve key associated with the current cursor */
343         rc = -zap_cursor_retrieve(it->oiq_zc, za);
344         if (unlikely(rc != 0))
345                 RETURN(rc);
346
347         /* inode accounting is not maintained by DMU, so we use our own ZAP to
348          * track inode usage */
349         rc = -zap_lookup(osd->od_os, it->oiq_obj->oo_db->db_object,
350                          za->za_name, sizeof(uint64_t), 1, &rec->ispace);
351         if (rc == -ENOENT)
352                 /* user/group has not created any file yet */
353                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
354                        osd->od_svname, za->za_name);
355         else if (rc)
356                 RETURN(rc);
357
358         RETURN(0);
359 }
360
361 /**
362  * Returns cookie for current Iterator position.
363  *
364  * \param  di    - osd iterator
365  */
366 static __u64 osd_it_acct_store(const struct lu_env *env,
367                                const struct dt_it *di)
368 {
369         struct osd_it_quota *it = (struct osd_it_quota *)di;
370         ENTRY;
371         it->oiq_reset = 0;
372         RETURN(osd_zap_cursor_serialize(it->oiq_zc));
373 }
374
375 /**
376  * Restore iterator from cookie. if the \a hash isn't found,
377  * restore the first valid record.
378  *
379  * \param  di    - osd iterator
380  * \param  hash  - iterator location cookie
381  *
382  * \retval +ve  - di points to exact matched key
383  * \retval  0   - di points to the first valid record
384  * \retval -ve  - failure
385  */
386 static int osd_it_acct_load(const struct lu_env *env,
387                             const struct dt_it *di, __u64 hash)
388 {
389         struct osd_it_quota     *it  = (struct osd_it_quota *)di;
390         struct osd_device       *osd = osd_obj2dev(it->oiq_obj);
391         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
392         zap_cursor_t            *zc;
393         int                      rc;
394         ENTRY;
395
396         /* create new cursor pointing to the new hash */
397         rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
398         if (rc)
399                 RETURN(rc);
400         osd_zap_cursor_fini(it->oiq_zc);
401         it->oiq_zc = zc;
402         it->oiq_reset = 0;
403
404         rc = -zap_cursor_retrieve(it->oiq_zc, za);
405         if (rc == 0)
406                 rc = 1;
407         else if (rc == -ENOENT)
408                 rc = 0;
409
410         RETURN(rc);
411 }
412
413 /**
414  * Move Iterator to record specified by \a key, if the \a key isn't found,
415  * move to the first valid record.
416  *
417  * \param  di   - osd iterator
418  * \param  key  - uid or gid
419  *
420  * \retval +ve  - di points to exact matched key
421  * \retval 0    - di points to the first valid record
422  * \retval -ve  - failure
423  */
424 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
425                 const struct dt_key *key)
426 {
427         ENTRY;
428
429         /* XXX: like osd_zap_it_get(), API is currently broken */
430         LASSERT(*((__u64 *)key) == 0);
431
432         RETURN(osd_it_acct_load(env, di, 0));
433 }
434
435 /**
436  * Release Iterator
437  *
438  * \param  di   - osd iterator
439  */
440 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
441 {
442 }
443
444 /**
445  * Index and Iterator operations for accounting objects
446  */
447 const struct dt_index_operations osd_acct_index_ops = {
448         .dio_lookup = osd_acct_index_lookup,
449         .dio_it     = {
450                 .init           = osd_it_acct_init,
451                 .fini           = osd_it_acct_fini,
452                 .get            = osd_it_acct_get,
453                 .put            = osd_it_acct_put,
454                 .next           = osd_it_acct_next,
455                 .key            = osd_it_acct_key,
456                 .key_size       = osd_it_acct_key_size,
457                 .rec            = osd_it_acct_rec,
458                 .store          = osd_it_acct_store,
459                 .load           = osd_it_acct_load
460         }
461 };
462
463 /**
464  * Quota Enforcement Management
465  */
466
467 /*
468  * Wrapper for qsd_op_begin().
469  *
470  * \param env    - the environment passed by the caller
471  * \param osd    - is the osd_device
472  * \param uid    - user id of the inode
473  * \param gid    - group id of the inode
474  * \param space  - how many blocks/inodes will be consumed/released
475  * \param oh     - osd transaction handle
476  * \param is_blk - block quota or inode quota?
477  * \param flags  - if the operation is write, return no user quota, no
478  *                  group quota, or sync commit flags to the caller
479  * \param force  - set to 1 when changes are performed by root user and thus
480  *                  can't failed with EDQUOT
481  *
482  * \retval 0      - success
483  * \retval -ve    - failure
484  */
485 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
486                       qid_t uid, qid_t gid, long long space,
487                       struct osd_thandle *oh, bool is_blk, int *flags,
488                       bool force)
489 {
490         struct osd_thread_info  *info = osd_oti_get(env);
491         struct lquota_id_info   *qi = &info->oti_qi;
492         struct qsd_instance     *qsd = osd->od_quota_slave;
493         int                      rcu, rcg; /* user & group rc */
494         ENTRY;
495
496         if (unlikely(qsd == NULL))
497                 /* quota slave instance hasn't been allocated yet */
498                 RETURN(0);
499
500         /* let's start with user quota */
501         qi->lqi_id.qid_uid = uid;
502         qi->lqi_type       = USRQUOTA;
503         qi->lqi_space      = space;
504         qi->lqi_is_blk     = is_blk;
505         rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
506
507         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
508                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
509                 rcu = 0;
510
511         /* For non-fatal error, we want to continue to get the noquota flags
512          * for group id. This is only for commit write, which has @flags passed
513          * in. See osd_declare_write_commit().
514          * When force is set to true, we also want to proceed with the gid */
515         if (rcu && (rcu != -EDQUOT || flags == NULL))
516                 RETURN(rcu);
517
518         /* and now group quota */
519         qi->lqi_id.qid_gid = gid;
520         qi->lqi_type       = GRPQUOTA;
521         rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
522
523         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
524                 /* as before, ignore EDQUOT & EINPROGRESS for root */
525                 rcg = 0;
526
527         RETURN(rcu ? rcu : rcg);
528 }