Whamcloud - gitweb
LU-3105 osd: remove capa related stuff from servers
[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  *
84  * \retval +ve - success : exact match
85  * \retval -ve - failure
86  */
87 static int osd_acct_index_lookup(const struct lu_env *env,
88                                 struct dt_object *dtobj,
89                                 struct dt_rec *dtrec,
90                                 const struct dt_key *dtkey)
91 {
92         struct osd_thread_info  *info = osd_oti_get(env);
93         char                    *buf  = info->oti_buf;
94         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
95         struct osd_object       *obj = osd_dt_obj(dtobj);
96         struct osd_device       *osd = osd_obj2dev(obj);
97         int                      rc;
98         uint64_t                 oid;
99         ENTRY;
100
101         rec->bspace = rec->ispace = 0;
102
103         /* convert the 64-bit uid/gid into a string */
104         sprintf(buf, "%llx", *((__u64 *)dtkey));
105         /* fetch DMU object ID (DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT) to be
106          * used */
107         oid = osd_quota_fid2dmu(lu_object_fid(&dtobj->do_lu));
108
109         /* disk usage (in bytes) is maintained by DMU.
110          * DMU_USERUSED_OBJECT/DMU_GROUPUSED_OBJECT are special objects which
111          * not associated with any dmu_but_t (see dnode_special_open()).
112          * As a consequence, we cannot use udmu_zap_lookup() here since it
113          * requires a valid oo_db. */
114         rc = -zap_lookup(osd->od_os, oid, buf, sizeof(uint64_t), 1,
115                         &rec->bspace);
116         if (rc == -ENOENT)
117                 /* user/group has not created anything yet */
118                 CDEBUG(D_QUOTA, "%s: id %s not found in DMU accounting ZAP\n",
119                        osd->od_svname, buf);
120         else if (rc)
121                 RETURN(rc);
122
123         if (osd->od_quota_iused_est) {
124                 if (rec->bspace != 0)
125                         /* estimate #inodes in use */
126                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
127                 RETURN(+1);
128         }
129
130         /* as for inode accounting, it is not maintained by DMU, so we just
131          * use our own ZAP to track inode usage */
132         rc = -zap_lookup(osd->od_os, obj->oo_db->db_object,
133                          buf, sizeof(uint64_t), 1, &rec->ispace);
134         if (rc == -ENOENT)
135                 /* user/group has not created any file yet */
136                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
137                        osd->od_svname, buf);
138         else if (rc)
139                 RETURN(rc);
140
141         RETURN(+1);
142 }
143
144 /**
145  * Initialize osd Iterator for given osd index object.
146  *
147  * \param  dt    - osd index object
148  * \param  attr  - not used
149  */
150 static struct dt_it *osd_it_acct_init(const struct lu_env *env,
151                                       struct dt_object *dt,
152                                       __u32 attr)
153 {
154         struct osd_thread_info  *info = osd_oti_get(env);
155         struct osd_it_quota     *it;
156         struct lu_object        *lo   = &dt->do_lu;
157         struct osd_device       *osd  = osd_dev(lo->lo_dev);
158         int                      rc;
159         ENTRY;
160
161         LASSERT(lu_object_exists(lo));
162
163         if (info == NULL)
164                 RETURN(ERR_PTR(-ENOMEM));
165
166         OBD_ALLOC_PTR(it);
167         if (it == NULL)
168                 RETURN(ERR_PTR(-ENOMEM));
169
170         memset(it, 0, sizeof(*it));
171         it->oiq_oid = osd_quota_fid2dmu(lu_object_fid(lo));
172
173         /* initialize zap cursor */
174         rc = osd_zap_cursor_init(&it->oiq_zc, osd->od_os, it->oiq_oid, 0);
175         if (rc != 0) {
176                 OBD_FREE_PTR(it);
177                 RETURN(ERR_PTR(rc));
178         }
179
180         /* take object reference */
181         lu_object_get(lo);
182         it->oiq_obj   = osd_dt_obj(dt);
183         it->oiq_reset = 1;
184
185         RETURN((struct dt_it *)it);
186 }
187
188 /**
189  * Free given iterator.
190  *
191  * \param  di   - osd iterator
192  */
193 static void osd_it_acct_fini(const struct lu_env *env, struct dt_it *di)
194 {
195         struct osd_it_quota     *it     = (struct osd_it_quota *)di;
196         ENTRY;
197
198         osd_zap_cursor_fini(it->oiq_zc);
199         lu_object_put(env, &it->oiq_obj->oo_dt.do_lu);
200         OBD_FREE_PTR(it);
201
202         EXIT;
203 }
204
205 /**
206  * Move on to the next valid entry.
207  *
208  * \param  di   - osd iterator
209  *
210  * \retval +ve  - iterator reached the end
211  * \retval   0  - iterator has not reached the end yet
212  * \retval -ve  - unexpected failure
213  */
214 static int osd_it_acct_next(const struct lu_env *env, struct dt_it *di)
215 {
216         struct osd_it_quota     *it = (struct osd_it_quota *)di;
217         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
218         int                      rc;
219         ENTRY;
220
221         if (it->oiq_reset == 0)
222                 zap_cursor_advance(it->oiq_zc);
223         it->oiq_reset = 0;
224         rc = -zap_cursor_retrieve(it->oiq_zc, za);
225         if (rc == -ENOENT) /* reached the end */
226                 rc = 1;
227         RETURN(rc);
228 }
229
230 /**
231  * Return pointer to the key under iterator.
232  *
233  * \param  di   - osd iterator
234  */
235 static struct dt_key *osd_it_acct_key(const struct lu_env *env,
236                                       const struct dt_it *di)
237 {
238         struct osd_it_quota     *it = (struct osd_it_quota *)di;
239         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
240         int                      rc;
241         ENTRY;
242
243         it->oiq_reset = 0;
244         rc = -zap_cursor_retrieve(it->oiq_zc, za);
245         if (rc)
246                 RETURN(ERR_PTR(rc));
247         rc = kstrtoull(za->za_name, 16, &it->oiq_id);
248
249         RETURN((struct dt_key *) &it->oiq_id);
250 }
251
252 /**
253  * Return size of key under iterator (in bytes)
254  *
255  * \param  di   - osd iterator
256  */
257 static int osd_it_acct_key_size(const struct lu_env *env,
258                                 const struct dt_it *di)
259 {
260         ENTRY;
261         RETURN((int)sizeof(uint64_t));
262 }
263
264 /*
265  * zap_cursor_retrieve read from current record.
266  * to read bytes we need to call zap_lookup explicitly.
267  */
268 static int osd_zap_cursor_retrieve_value(const struct lu_env *env,
269                                          zap_cursor_t *zc,  char *buf,
270                                          int buf_size, int *bytes_read)
271 {
272         zap_attribute_t *za = &osd_oti_get(env)->oti_za;
273         int rc, actual_size;
274
275         rc = -zap_cursor_retrieve(zc, za);
276         if (unlikely(rc != 0))
277                 return -rc;
278
279         if (unlikely(za->za_integer_length <= 0))
280                 return -ERANGE;
281
282         actual_size = za->za_integer_length * za->za_num_integers;
283
284         if (actual_size > buf_size) {
285                 actual_size = buf_size;
286                 buf_size = actual_size / za->za_integer_length;
287         } else {
288                 buf_size = za->za_num_integers;
289         }
290
291         rc = -zap_lookup(zc->zc_objset, zc->zc_zapobj,
292                          za->za_name, za->za_integer_length,
293                          buf_size, buf);
294
295         if (likely(rc == 0))
296                 *bytes_read = actual_size;
297
298         return rc;
299 }
300
301 /**
302  * Return pointer to the record under iterator.
303  *
304  * \param  di    - osd iterator
305  * \param  attr  - not used
306  */
307 static int osd_it_acct_rec(const struct lu_env *env,
308                            const struct dt_it *di,
309                            struct dt_rec *dtrec, __u32 attr)
310 {
311         struct osd_thread_info  *info = osd_oti_get(env);
312         zap_attribute_t         *za = &info->oti_za;
313         struct osd_it_quota     *it = (struct osd_it_quota *)di;
314         struct lquota_acct_rec  *rec  = (struct lquota_acct_rec *)dtrec;
315         struct osd_object       *obj = it->oiq_obj;
316         struct osd_device       *osd = osd_obj2dev(obj);
317         int                      bytes_read;
318         int                      rc;
319         ENTRY;
320
321         it->oiq_reset = 0;
322         rec->ispace = rec->bspace = 0;
323
324         /* retrieve block usage from the DMU accounting object */
325         rc = osd_zap_cursor_retrieve_value(env, it->oiq_zc,
326                                            (char *)&rec->bspace,
327                                            sizeof(uint64_t), &bytes_read);
328         if (rc)
329                 RETURN(rc);
330
331         if (osd->od_quota_iused_est) {
332                 if (rec->bspace != 0)
333                         /* estimate #inodes in use */
334                         rec->ispace = osd_objset_user_iused(osd, rec->bspace);
335                 RETURN(0);
336         }
337
338         /* retrieve key associated with the current cursor */
339         rc = -zap_cursor_retrieve(it->oiq_zc, za);
340         if (unlikely(rc != 0))
341                 RETURN(rc);
342
343         /* inode accounting is not maintained by DMU, so we use our own ZAP to
344          * track inode usage */
345         rc = -zap_lookup(osd->od_os, it->oiq_obj->oo_db->db_object,
346                          za->za_name, sizeof(uint64_t), 1, &rec->ispace);
347         if (rc == -ENOENT)
348                 /* user/group has not created any file yet */
349                 CDEBUG(D_QUOTA, "%s: id %s not found in accounting ZAP\n",
350                        osd->od_svname, za->za_name);
351         else if (rc)
352                 RETURN(rc);
353
354         RETURN(0);
355 }
356
357 /**
358  * Returns cookie for current Iterator position.
359  *
360  * \param  di    - osd iterator
361  */
362 static __u64 osd_it_acct_store(const struct lu_env *env,
363                                const struct dt_it *di)
364 {
365         struct osd_it_quota *it = (struct osd_it_quota *)di;
366         ENTRY;
367         it->oiq_reset = 0;
368         RETURN(osd_zap_cursor_serialize(it->oiq_zc));
369 }
370
371 /**
372  * Restore iterator from cookie. if the \a hash isn't found,
373  * restore the first valid record.
374  *
375  * \param  di    - osd iterator
376  * \param  hash  - iterator location cookie
377  *
378  * \retval +ve  - di points to exact matched key
379  * \retval  0   - di points to the first valid record
380  * \retval -ve  - failure
381  */
382 static int osd_it_acct_load(const struct lu_env *env,
383                             const struct dt_it *di, __u64 hash)
384 {
385         struct osd_it_quota     *it  = (struct osd_it_quota *)di;
386         struct osd_device       *osd = osd_obj2dev(it->oiq_obj);
387         zap_attribute_t         *za = &osd_oti_get(env)->oti_za;
388         zap_cursor_t            *zc;
389         int                      rc;
390         ENTRY;
391
392         /* create new cursor pointing to the new hash */
393         rc = osd_zap_cursor_init(&zc, osd->od_os, it->oiq_oid, hash);
394         if (rc)
395                 RETURN(rc);
396         osd_zap_cursor_fini(it->oiq_zc);
397         it->oiq_zc = zc;
398         it->oiq_reset = 0;
399
400         rc = -zap_cursor_retrieve(it->oiq_zc, za);
401         if (rc == 0)
402                 rc = 1;
403         else if (rc == -ENOENT)
404                 rc = 0;
405
406         RETURN(rc);
407 }
408
409 /**
410  * Move Iterator to record specified by \a key, if the \a key isn't found,
411  * move to the first valid record.
412  *
413  * \param  di   - osd iterator
414  * \param  key  - uid or gid
415  *
416  * \retval +ve  - di points to exact matched key
417  * \retval 0    - di points to the first valid record
418  * \retval -ve  - failure
419  */
420 static int osd_it_acct_get(const struct lu_env *env, struct dt_it *di,
421                 const struct dt_key *key)
422 {
423         ENTRY;
424
425         /* XXX: like osd_zap_it_get(), API is currently broken */
426         LASSERT(*((__u64 *)key) == 0);
427
428         RETURN(osd_it_acct_load(env, di, 0));
429 }
430
431 /**
432  * Release Iterator
433  *
434  * \param  di   - osd iterator
435  */
436 static void osd_it_acct_put(const struct lu_env *env, struct dt_it *di)
437 {
438 }
439
440 /**
441  * Index and Iterator operations for accounting objects
442  */
443 const struct dt_index_operations osd_acct_index_ops = {
444         .dio_lookup = osd_acct_index_lookup,
445         .dio_it     = {
446                 .init           = osd_it_acct_init,
447                 .fini           = osd_it_acct_fini,
448                 .get            = osd_it_acct_get,
449                 .put            = osd_it_acct_put,
450                 .next           = osd_it_acct_next,
451                 .key            = osd_it_acct_key,
452                 .key_size       = osd_it_acct_key_size,
453                 .rec            = osd_it_acct_rec,
454                 .store          = osd_it_acct_store,
455                 .load           = osd_it_acct_load
456         }
457 };
458
459 /**
460  * Quota Enforcement Management
461  */
462
463 /*
464  * Wrapper for qsd_op_begin().
465  *
466  * \param env    - the environment passed by the caller
467  * \param osd    - is the osd_device
468  * \param uid    - user id of the inode
469  * \param gid    - group id of the inode
470  * \param space  - how many blocks/inodes will be consumed/released
471  * \param oh     - osd transaction handle
472  * \param is_blk - block quota or inode quota?
473  * \param flags  - if the operation is write, return no user quota, no
474  *                  group quota, or sync commit flags to the caller
475  * \param force  - set to 1 when changes are performed by root user and thus
476  *                  can't failed with EDQUOT
477  *
478  * \retval 0      - success
479  * \retval -ve    - failure
480  */
481 int osd_declare_quota(const struct lu_env *env, struct osd_device *osd,
482                       qid_t uid, qid_t gid, long long space,
483                       struct osd_thandle *oh, bool is_blk, int *flags,
484                       bool force)
485 {
486         struct osd_thread_info  *info = osd_oti_get(env);
487         struct lquota_id_info   *qi = &info->oti_qi;
488         struct qsd_instance     *qsd = osd->od_quota_slave;
489         int                      rcu, rcg; /* user & group rc */
490         ENTRY;
491
492         if (unlikely(qsd == NULL))
493                 /* quota slave instance hasn't been allocated yet */
494                 RETURN(0);
495
496         /* let's start with user quota */
497         qi->lqi_id.qid_uid = uid;
498         qi->lqi_type       = USRQUOTA;
499         qi->lqi_space      = space;
500         qi->lqi_is_blk     = is_blk;
501         rcu = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
502
503         if (force && (rcu == -EDQUOT || rcu == -EINPROGRESS))
504                 /* ignore EDQUOT & EINPROGRESS when changes are done by root */
505                 rcu = 0;
506
507         /* For non-fatal error, we want to continue to get the noquota flags
508          * for group id. This is only for commit write, which has @flags passed
509          * in. See osd_declare_write_commit().
510          * When force is set to true, we also want to proceed with the gid */
511         if (rcu && (rcu != -EDQUOT || flags == NULL))
512                 RETURN(rcu);
513
514         /* and now group quota */
515         qi->lqi_id.qid_gid = gid;
516         qi->lqi_type       = GRPQUOTA;
517         rcg = qsd_op_begin(env, qsd, &oh->ot_quota_trans, qi, flags);
518
519         if (force && (rcg == -EDQUOT || rcg == -EINPROGRESS))
520                 /* as before, ignore EDQUOT & EINPROGRESS for root */
521                 rcg = 0;
522
523         RETURN(rcu ? rcu : rcg);
524 }