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