Whamcloud - gitweb
LU-12624 lod: alloc dir stripes by QoS
[fs/lustre-release.git] / lustre / quota / qsd_entry.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, 2017, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * Author: Johann Lombardi <johann.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include "qsd_internal.h"
34
35 /*
36  * Initialize qsd-specific fields of quota entry.
37  *
38  * \param lqe - is the quota entry to initialize
39  * \param arg - is the pointer to the qsd_qtype_info structure
40  */
41 static void qsd_lqe_init(struct lquota_entry *lqe, void *arg)
42 {
43         LASSERT(!lqe_is_master(lqe));
44
45         /* initialize slave parameters */
46         rwlock_init(&lqe->lqe_lock);
47         memset(&lqe->lqe_lockh, 0, sizeof(lqe->lqe_lockh));
48         lqe->lqe_pending_write = 0;
49         lqe->lqe_pending_req   = 0;
50         init_waitqueue_head(&lqe->lqe_waiters);
51         lqe->lqe_usage    = 0;
52         lqe->lqe_nopreacq = false;
53 }
54
55 /*
56  * Update a slave quota entry. This is done by reading enforcement status from
57  * the copy of the global index and then how much is the slave currenly owns
58  * for this user from the slave index copy.
59  *
60  * \param env - the environment passed by the caller
61  * \param lqe - is the quota entry to refresh
62  * \param arg - is the pointer to the qsd_qtype_info structure
63  */
64 static int qsd_lqe_read(const struct lu_env *env, struct lquota_entry *lqe,
65                         void *arg)
66 {
67         struct qsd_thread_info *qti = qsd_info(env);
68         struct qsd_qtype_info  *qqi = (struct qsd_qtype_info *)arg;
69         int                     rc;
70
71         LASSERT(!lqe_is_master(lqe));
72
73         /* read record from global index copy to know whether quota is
74          * enforced for this user */
75         rc = lquota_disk_read(env, qqi->qqi_glb_obj, &lqe->lqe_id,
76                               (struct dt_rec *)&qti->qti_glb_rec);
77
78         switch(rc) {
79         case -ENOENT:
80                 /* no such entry, assume quota isn't enforced for this user */
81                 lqe->lqe_enforced = false;
82                 break;
83         case 0:
84                 if (lqe->lqe_id.qid_uid == 0) {
85                         qqi->qqi_default_hardlimit =
86                                                 qti->qti_glb_rec.qbr_hardlimit;
87                         qqi->qqi_default_softlimit =
88                                                 qti->qti_glb_rec.qbr_softlimit;
89                         qqi->qqi_default_gracetime =
90                                                 qti->qti_glb_rec.qbr_granted;
91                 }
92
93                 if (lqe->lqe_id.qid_uid != 0 &&
94                     (qti->qti_glb_rec.qbr_hardlimit != 0 ||
95                      qti->qti_glb_rec.qbr_softlimit != 0))
96                         lqe->lqe_enforced = true;
97                 else
98                         lqe->lqe_enforced = false;
99                 break;
100         default:
101                 LQUOTA_ERROR(lqe, "failed to read quota entry from global "
102                              "index copy, rc:%d", rc);
103                 return rc;
104         }
105
106         if (lqe->lqe_id.qid_uid != 0 &&
107             (rc == -ENOENT ||
108              (LQUOTA_FLAG(qti->qti_glb_rec.qbr_time) & LQUOTA_FLAG_DEFAULT &&
109               qti->qti_glb_rec.qbr_hardlimit == 0 &&
110               qti->qti_glb_rec.qbr_softlimit == 0))) {
111                 struct lquota_entry *lqe_def;
112                 union lquota_id qid = { {0} };
113
114                 /* ensure the lqe storing the default quota setting loaded */
115                 lqe_def = lqe_locate(env, qqi->qqi_site, &qid);
116
117                 lqe->lqe_is_default = true;
118
119                 if (qqi->qqi_default_hardlimit != 0 ||
120                     qqi->qqi_default_softlimit != 0) {
121                         LQUOTA_DEBUG(lqe, "enforced by default quota");
122                         lqe->lqe_enforced = true;
123                 }
124
125                 if (!IS_ERR(lqe_def))
126                         lqe_putref(lqe_def);
127         }
128
129         /* read record from slave index copy to find out how much space is
130          * currently owned by this slave */
131         rc = lquota_disk_read(env, qqi->qqi_slv_obj, &lqe->lqe_id,
132                               (struct dt_rec *)&qti->qti_slv_rec);
133         switch(rc) {
134         case -ENOENT:
135                 lqe->lqe_granted = 0;
136                 break;
137         case 0:
138                 lqe->lqe_granted = qti->qti_slv_rec.qsr_granted;
139                 break;
140         default:
141                 LQUOTA_ERROR(lqe, "failed to read quota entry from slave "
142                              "index copy, rc:%d", rc);
143                 return rc;
144         }
145
146         /* don't know what the qunit value is yet */
147         qsd_set_qunit(lqe, 0);
148
149         /* read current disk-usage from disk */
150         rc = qsd_refresh_usage(env, lqe);
151         if (rc)
152                 return rc;
153
154         LQUOTA_DEBUG(lqe, "successfully read from disk");
155         return 0;
156 }
157
158 /*
159  * Print lqe information for debugging.
160  *
161  * \param lqe - is the quota entry to debug
162  * \param arg - is the pointer to the qsd_qtype_info structure
163  * \param msgdata - debug message
164  * \param fmt     - format of debug message
165  */
166 static void qsd_lqe_debug(struct lquota_entry *lqe, void *arg,
167                           struct libcfs_debug_msg_data *msgdata,
168                           struct va_format *vaf)
169 {
170         struct qsd_qtype_info   *qqi = (struct qsd_qtype_info *)arg;
171
172         libcfs_debug_msg(msgdata,
173                          "%pV qsd:%s qtype:%s id:%llu enforced:%d granted: %llu pending:%llu waiting:%llu req:%d usage: %llu qunit:%llu qtune:%llu edquot:%d default:%s\n",
174                          vaf,
175                          qqi->qqi_qsd->qsd_svname, qtype_name(qqi->qqi_qtype),
176                          lqe->lqe_id.qid_uid, lqe->lqe_enforced,
177                          lqe->lqe_granted, lqe->lqe_pending_write,
178                          lqe->lqe_waiting_write, lqe->lqe_pending_req,
179                          lqe->lqe_usage, lqe->lqe_qunit, lqe->lqe_qtune,
180                          lqe->lqe_edquot, lqe->lqe_is_default ? "yes" : "no");
181 }
182
183 /*
184  * Vector of quota entry operations supported on the slave
185  */
186 struct lquota_entry_operations qsd_lqe_ops = {
187         .lqe_init               = qsd_lqe_init,
188         .lqe_read               = qsd_lqe_read,
189         .lqe_debug              = qsd_lqe_debug,
190 };
191
192 int qsd_write_version(const struct lu_env *env, struct qsd_qtype_info *qqi,
193                       __u64 ver, bool global)
194 {
195         struct qsd_instance *qsd = qqi->qqi_qsd;
196         struct dt_object    *obj = global ? qqi->qqi_glb_obj :
197                                             qqi->qqi_slv_obj;
198         int                  rc;
199         ENTRY;
200
201         rc = lquota_disk_update_ver(env, qsd->qsd_dev, obj, ver);
202         if (rc)
203                 RETURN(rc);
204
205         qsd_bump_version(qqi, ver, global);
206         RETURN(0);
207 }
208
209 /*
210  * Consult current disk space consumed by a given identifier.
211  *
212  * \param env   - the environment passed by the caller
213  * \param qqi   - is the pointer to the qsd_qtype_info structure associated
214  *                with the identifier.
215  * \param lqe   - is the quota entry associated with the identifier
216  */
217 int qsd_refresh_usage(const struct lu_env *env, struct lquota_entry *lqe)
218 {
219         struct qsd_thread_info  *qti = qsd_info(env);
220         struct lquota_acct_rec  *rec = &qti->qti_acct_rec;
221         struct qsd_qtype_info   *qqi = lqe2qqi(lqe);
222         int                      rc = 0;
223         ENTRY;
224
225         LASSERT(qqi->qqi_acct_obj);
226
227         /* read disk usage */
228         rc = lquota_disk_read(env, qqi->qqi_acct_obj, &lqe->lqe_id,
229                               (struct dt_rec *)rec);
230         switch(rc) {
231         case -ENOENT:
232                 lqe->lqe_usage = 0;
233                 rc = 0;
234                 break;
235         case 0:
236                 if (qqi->qqi_qsd->qsd_is_md)
237                         lqe->lqe_usage = rec->ispace;
238                 else
239                         lqe->lqe_usage = toqb(rec->bspace);
240                 break;
241         default:
242                 LQUOTA_ERROR(lqe, "failed to read disk usage, rc:%d", rc);
243                 RETURN(rc);
244         }
245
246         LQUOTA_DEBUG(lqe, "disk usage: %llu", lqe->lqe_usage);
247         RETURN(0);
248 }
249
250 /*
251  * Update slave or global index copy.
252  *
253  * \param env    - the environment passed by the caller
254  * \param qqi    - is the qsd_type_info structure managing the index to be
255  *                 update
256  * \param qid    - is the identifier for which we need to update the quota
257  *                 settings
258  * \param global - is set to true when updating the global index copy and to
259  *                 false for the slave index copy.
260  * \param ver    - is the new version of the index. If equal to 0, the version
261  *                 of the index isn't changed
262  * \param rec    - is the updated record to insert in the index file
263  */
264 int qsd_update_index(const struct lu_env *env, struct qsd_qtype_info *qqi,
265                      union lquota_id *qid, bool global, __u64 ver, void *rec)
266 {
267         struct thandle          *th = NULL;
268         struct dt_object        *obj;
269         __u64                   *new_verp = NULL;
270         int                      flags = 0;
271         int                      rc;
272         ENTRY;
273
274         obj = global ? qqi->qqi_glb_obj : qqi->qqi_slv_obj;
275
276         /* allocate transaction */
277         th = dt_trans_create(env, qqi->qqi_qsd->qsd_dev);
278         if (IS_ERR(th))
279                 RETURN(PTR_ERR(th));
280
281         /* reserve enough credits to update record in index file */
282         rc = lquota_disk_declare_write(env, th, obj, qid);
283         if (rc)
284                 GOTO(out, rc);
285
286         /* start local transaction */
287         rc = dt_trans_start_local(env, qqi->qqi_qsd->qsd_dev, th);
288         if (rc)
289                 GOTO(out, rc);
290
291         if (global) {
292                 /* Update record in global index copy */
293                 struct lquota_glb_rec *glb_rec = (struct lquota_glb_rec *)rec;
294
295                 CDEBUG(D_QUOTA, "%s: updating global index hardlimit: %llu, "
296                        "softlimit: %llu for id %llu\n",
297                        qqi->qqi_qsd->qsd_svname, glb_rec->qbr_hardlimit,
298                        glb_rec->qbr_softlimit, qid->qid_uid);
299         } else {
300                 /* Update record in slave index copy */
301                 struct lquota_slv_rec *slv_rec = (struct lquota_slv_rec *)rec;
302
303                 CDEBUG(D_QUOTA, "%s: update granted to %llu for id %llu"
304                        "\n", qqi->qqi_qsd->qsd_svname, slv_rec->qsr_granted,
305                        qid->qid_uid);
306         }
307
308         if (ver != 0) {
309                 new_verp = &ver;
310                 flags = LQUOTA_SET_VER;
311         }
312
313         /* write new record to index file */
314         rc = lquota_disk_write(env, th, obj, qid, (struct dt_rec *)rec, flags,
315                                new_verp);
316         EXIT;
317 out:
318         dt_trans_stop(env, qqi->qqi_qsd->qsd_dev, th);
319         if (rc)
320                 CERROR("%s: failed to update %s index copy for id %llu, : rc = %d\n",
321                        qqi->qqi_qsd->qsd_svname,
322                        global ? "global" : "slave", qid->qid_uid, rc);
323         else if (flags == LQUOTA_SET_VER)
324                 qsd_bump_version(qqi, ver, global);
325         return rc;
326 }
327
328 /*
329  * Update in-memory lquota entry with new quota setting from record \rec.
330  * The record can either be a global record (i.e. lquota_glb_rec) or a slave
331  * index record (i.e. lquota_slv_rec). In the former case, \global should be
332  * set to true.
333  *
334  * \param env    - the environment passed by the caller
335  * \param lqe    - is the quota entry associated with the identifier
336  * \param global - is set to true when updating the record is of type
337  *                 lquota_glb_rec. Otherwise, it is a lquota_slv_rec record.
338  * \param rec    - is the updated record received from the master.
339  */
340 int qsd_update_lqe(const struct lu_env *env, struct lquota_entry *lqe,
341                    bool global, void *rec)
342 {
343         ENTRY;
344
345         LASSERT(lqe != NULL);
346         LASSERT(!lqe_is_master(lqe));
347
348         /* updating lqe is always serialized, no locking needed. */
349         if (global) {
350                 struct lquota_glb_rec *glb_rec = (struct lquota_glb_rec *)rec;
351
352                 /* doesn't change quota enforcement if the quota entry is still
353                  * using default quota. */
354                 if (LQUOTA_FLAG(glb_rec->qbr_time) & LQUOTA_FLAG_DEFAULT &&
355                     glb_rec->qbr_hardlimit == 0 && glb_rec->qbr_softlimit == 0)
356                         RETURN(0);
357
358                 LQUOTA_DEBUG(lqe, "the ID has been set quota, so clear the"
359                              " default quota flag");
360                 lqe->lqe_is_default = false;
361
362                 /* change enforcement status based on new hard/soft limit */
363                 if (lqe->lqe_id.qid_uid != 0 && (glb_rec->qbr_hardlimit != 0 ||
364                     glb_rec->qbr_softlimit != 0))
365                         lqe->lqe_enforced = true;
366                 else
367                         lqe->lqe_enforced = false;
368
369                 LQUOTA_DEBUG(lqe, "updating global index hardlimit: %llu, "
370                              "softlimit: %llu", glb_rec->qbr_hardlimit,
371                              glb_rec->qbr_softlimit);
372         } else {
373                 struct lquota_slv_rec *slv_rec = (struct lquota_slv_rec *)rec;
374
375                 lqe->lqe_granted = slv_rec->qsr_granted;
376
377                 LQUOTA_DEBUG(lqe, "updating slave index, granted:%llu",
378                              slv_rec->qsr_granted);
379         }
380
381         RETURN(0);
382 }