Whamcloud - gitweb
LU-13967 quota: change warning to cdebug
[fs/lustre-release.git] / lustre / quota / qmt_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, 2016, 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 "qmt_internal.h"
34
35 /*
36  * Initialize qmt-specific fields of quota entry.
37  *
38  * \param lqe - is the quota entry to initialize
39  * \param arg - is the pointer to the qmt_pool_info structure
40  */
41 static void qmt_lqe_init(struct lquota_entry *lqe, void *arg)
42 {
43         LASSERT(lqe_is_master(lqe));
44
45         lqe->lqe_revoke_time = 0;
46         init_rwsem(&lqe->lqe_sem);
47 }
48
49 /* Apply the default quota setting to the specified quota entry
50  *
51  * \param env           - is the environment passed by the caller
52  * \param pool          - is the quota pool of the quota entry
53  * \param lqe           - is the lquota_entry object to apply default quota on
54  * \param create_record - if true, an global quota record will be created and
55  *                        write to the disk.
56  *
57  * \retval 0            : success
58  * \retval -ve          : other appropriate errors
59  */
60 int qmt_lqe_set_default(const struct lu_env *env, struct qmt_pool_info *pool,
61                         struct lquota_entry *lqe, bool create_record)
62 {
63         struct lquota_entry     *lqe_def;
64         int                     rc = 0;
65
66         ENTRY;
67
68         if (lqe->lqe_id.qid_uid == 0)
69                 RETURN(0);
70
71         lqe_def = pool->qpi_grace_lqe[lqe_qtype(lqe)];
72
73         LQUOTA_DEBUG(lqe, "inherit default quota");
74
75         lqe->lqe_is_default = true;
76         lqe->lqe_hardlimit = lqe_def->lqe_hardlimit;
77         lqe->lqe_softlimit = lqe_def->lqe_softlimit;
78
79         if (create_record) {
80                 lqe->lqe_uptodate = true;
81                 rc = qmt_set_with_lqe(env, pool->qpi_qmt, lqe, 0, 0,
82                                       LQUOTA_GRACE_FLAG(0, LQUOTA_FLAG_DEFAULT),
83                                       QIF_TIMES, true, false);
84
85                 if (rc != 0)
86                         LQUOTA_ERROR(lqe, "failed to create the global quota"
87                                      " record: %d", rc);
88         }
89
90         if (lqe->lqe_hardlimit == 0 && lqe->lqe_softlimit == 0)
91                 lqe->lqe_enforced = false;
92         else
93                 lqe->lqe_enforced = true;
94
95         RETURN(rc);
96 }
97
98 /*
99  * Update a lquota entry. This is done by reading quota settings from the global
100  * index. The lquota entry must be write locked.
101  *
102  * \param env - the environment passed by the caller
103  * \param lqe - is the quota entry to refresh
104  * \param arg - is the pointer to the qmt_pool_info structure
105  * \param find - don't create lqe on disk in case of ENOENT if true
106  */
107 static int qmt_lqe_read(const struct lu_env *env, struct lquota_entry *lqe,
108                         void *arg, bool find)
109 {
110         struct qmt_thread_info  *qti = qmt_info(env);
111         struct qmt_pool_info    *pool = (struct qmt_pool_info *)arg;
112         int                      rc;
113         ENTRY;
114
115         LASSERT(lqe_is_master(lqe));
116
117         /* read record from disk */
118         rc = lquota_disk_read(env, pool->qpi_glb_obj[lqe->lqe_site->lqs_qtype],
119                               &lqe->lqe_id, (struct dt_rec *)&qti->qti_glb_rec);
120
121         switch (rc) {
122         case -ENOENT:
123                 if (find)
124                         RETURN(-ENOENT);
125                 qmt_lqe_set_default(env, pool, lqe, true);
126                 break;
127         case 0:
128                 /* copy quota settings from on-disk record */
129                 lqe->lqe_granted   = qti->qti_glb_rec.qbr_granted;
130                 lqe->lqe_hardlimit = qti->qti_glb_rec.qbr_hardlimit;
131                 lqe->lqe_softlimit = qti->qti_glb_rec.qbr_softlimit;
132                 lqe->lqe_gracetime = LQUOTA_GRACE(qti->qti_glb_rec.qbr_time);
133
134                 if (lqe->lqe_hardlimit == 0 && lqe->lqe_softlimit == 0 &&
135                     (LQUOTA_FLAG(qti->qti_glb_rec.qbr_time) &
136                      LQUOTA_FLAG_DEFAULT))
137                         qmt_lqe_set_default(env, pool, lqe, false);
138                 break;
139         default:
140                 LQUOTA_ERROR(lqe, "failed to read quota entry from disk, rc:%d",
141                              rc);
142                 RETURN(rc);
143         }
144
145         if (lqe->lqe_id.qid_uid == 0 ||
146             (lqe->lqe_hardlimit == 0 && lqe->lqe_softlimit == 0))
147                 /* {hard,soft}limit=0 means no quota enforced */
148                 lqe->lqe_enforced = false;
149         else
150                 lqe->lqe_enforced  = true;
151
152         if (qmt_pool_global(pool))
153                 lqe->lqe_is_global = 1;
154
155         LQUOTA_DEBUG(lqe, "read");
156         RETURN(0);
157 }
158
159 /*
160  * Print lqe information for debugging.
161  *
162  * \param lqe - is the quota entry to debug
163  * \param arg - is the pointer to the qmt_pool_info structure
164  * \param msgdata - debug message
165  * \param fmt     - format of debug message
166  */
167 static void qmt_lqe_debug(struct lquota_entry *lqe, void *arg,
168                           struct libcfs_debug_msg_data *msgdata,
169                           struct va_format *vaf)
170 {
171         struct qmt_pool_info    *pool = (struct qmt_pool_info *)arg;
172
173         libcfs_debug_msg(msgdata,
174                          "%pV qmt:%s pool:%s-%s id:%llu enforced:%d hard:%llu soft:%llu granted:%llu time:%llu qunit: %llu edquot:%d may_rel:%llu revoke:%lld default:%s\n",
175                          vaf, pool->qpi_qmt->qmt_svname,
176                          RES_NAME(pool->qpi_rtype),
177                          pool->qpi_name,
178                          lqe->lqe_id.qid_uid, lqe->lqe_enforced,
179                          lqe->lqe_hardlimit, lqe->lqe_softlimit,
180                          lqe->lqe_granted, lqe->lqe_gracetime,
181                          lqe->lqe_qunit, lqe->lqe_edquot, lqe->lqe_may_rel,
182                          lqe->lqe_revoke_time,
183                          lqe->lqe_is_default ? "yes" : "no");
184 }
185
186 /*
187  * Vector of quota entry operations supported on the master
188  */
189 struct lquota_entry_operations qmt_lqe_ops = {
190         .lqe_init       = qmt_lqe_init,
191         .lqe_read       = qmt_lqe_read,
192         .lqe_debug      = qmt_lqe_debug,
193 };
194
195 /*
196  * Reserve enough credits to update records in both the global index and
197  * the slave index identified by \slv_obj
198  *
199  * \param env     - is the environment passed by the caller
200  * \param lqe     - is the quota entry associated with the identifier
201  *                  subject to the change. If it is NULL lqes array is
202  *                  taken from env with qti_lqes_env(env).
203  * \param slv_obj - is the dt_object associated with the index file
204  * \param sync    - make transaction sync if true
205  */
206 struct thandle *qmt_trans_start_with_slv(const struct lu_env *env,
207                                          struct lquota_entry *lqe,
208                                          struct dt_object *slv_obj,
209                                          bool sync)
210 {
211         struct qmt_device       *qmt;
212         struct thandle          *th;
213         struct lquota_entry     **lqes;
214         struct qmt_lqe_restore  *restore;
215         int                      rc, i, lqes_cnt;
216         ENTRY;
217
218         restore = qti_lqes_rstr(env);
219         if (!lqe) {
220                 lqes_cnt = qti_lqes_cnt(env);
221                 lqes = qti_lqes(env);
222         } else {
223                 lqes_cnt = 1;
224                 lqes = &lqe;
225         }
226
227         /* qmt is the same for all lqes, so take it from the 1st */
228         qmt = lqe2qpi(lqes[0])->qpi_qmt;
229
230         if (slv_obj != NULL)
231                 LQUOTA_DEBUG(lqes[0], "declare write for slv "DFID,
232                              PFID(lu_object_fid(&slv_obj->do_lu)));
233
234         /* start transaction */
235         th = dt_trans_create(env, qmt->qmt_child);
236         if (IS_ERR(th))
237                 RETURN(th);
238
239         if (sync)
240                 /* quota settings on master are updated synchronously for the
241                  * time being */
242                 th->th_sync = 1;
243
244         /* reserve credits for global index update */
245         for (i = 0; i < lqes_cnt; i++) {
246                 rc = lquota_disk_declare_write(env, th,
247                                                LQE_GLB_OBJ(lqes[i]),
248                                                &lqes[i]->lqe_id);
249                 if (rc)
250                         GOTO(out, rc);
251         }
252
253         if (slv_obj != NULL) {
254                 /* reserve credits for slave index update */
255                 rc = lquota_disk_declare_write(env, th, slv_obj, &lqe->lqe_id);
256                 if (rc)
257                         GOTO(out, rc);
258         }
259
260         /* start transaction */
261         rc = dt_trans_start_local(env, qmt->qmt_child, th);
262         if (rc)
263                 GOTO(out, rc);
264
265         EXIT;
266 out:
267         if (rc) {
268                 dt_trans_stop(env, qmt->qmt_child, th);
269                 th = ERR_PTR(rc);
270                 LQUOTA_ERROR(lqe, "failed to slv declare write for "DFID
271                              ", rc:%d", PFID(lu_object_fid(&slv_obj->do_lu)),
272                              rc);
273         } else {
274                 for (i = 0; i < lqes_cnt; i++) {
275                         restore[i].qlr_hardlimit = lqes[i]->lqe_hardlimit;
276                         restore[i].qlr_softlimit = lqes[i]->lqe_softlimit;
277                         restore[i].qlr_gracetime = lqes[i]->lqe_gracetime;
278                         restore[i].qlr_granted   = lqes[i]->lqe_granted;
279                         restore[i].qlr_qunit     = lqes[i]->lqe_qunit;
280                 }
281         }
282         return th;
283 }
284
285 /*
286  * Reserve enough credits to update a record in the global index
287  *
288  * \param env     - is the environment passed by the caller
289  * \param lqe     - is the quota entry to be modified in the global index
290  * \param restore - is a temporary storage for current quota settings which will
291  *                  be restored if something goes wrong at index update time.
292  */
293 struct thandle *qmt_trans_start(const struct lu_env *env,
294                                 struct lquota_entry *lqe)
295 {
296         LQUOTA_DEBUG(lqe, "declare write");
297         return qmt_trans_start_with_slv(env, lqe, NULL, true);
298 }
299
300 int qmt_glb_write_lqes(const struct lu_env *env, struct thandle *th,
301                        __u32 flags, __u64 *ver)
302 {
303         int i, rc;
304         rc = 0;
305
306         for (i = 0; i < qti_lqes_cnt(env); i++) {
307                 rc = qmt_glb_write(env, th, qti_lqes(env)[i], flags, ver);
308                 if (rc)
309                         break;
310         }
311         return rc;
312 }
313
314 /*
315  * Update record associated with a quota entry in the global index.
316  * If LQUOTA_BUMP_VER is set, then the global index version must also be
317  * bumped.
318  * The entry must be at least read locked, dirty and up-to-date.
319  *
320  * \param env   - the environment passed by the caller
321  * \param th    - is the transaction handle to be used for the disk writes
322  * \param lqe   - is the quota entry to udpate
323  * \param obj   - is the dt_object associated with the index file
324  * \param flags - can be LQUOTA_BUMP_VER or LQUOTA_SET_VER.
325  * \param ver   - is used to return the new version of the index.
326  *
327  * \retval      - 0 on success and lqe dirty flag cleared,
328  *                appropriate error on failure and uptodate flag cleared.
329  */
330 int qmt_glb_write(const struct lu_env *env, struct thandle *th,
331                   struct lquota_entry *lqe, __u32 flags, __u64 *ver)
332 {
333         struct qmt_thread_info  *qti = qmt_info(env);
334         struct lquota_glb_rec   *rec;
335         int                      rc;
336         ENTRY;
337
338         LASSERT(lqe != NULL);
339         LASSERT(lqe_is_master(lqe));
340         LASSERT(lqe_is_locked(lqe));
341         LASSERT(lqe->lqe_uptodate);
342         LASSERT((flags & ~(LQUOTA_BUMP_VER | LQUOTA_SET_VER)) == 0);
343
344         LQUOTA_DEBUG(lqe, "write glb");
345
346         /* never delete the entry even when the id isn't enforced and
347          * no any guota granted, otherwise, this entry will not be
348          * synced to slave during the reintegration. */
349         rec = &qti->qti_glb_rec;
350
351         /* fill global index with updated quota settings */
352         rec->qbr_granted   = lqe->lqe_granted;
353         if (lqe->lqe_is_default) {
354                 rec->qbr_hardlimit = 0;
355                 rec->qbr_softlimit = 0;
356                 rec->qbr_time      = LQUOTA_GRACE_FLAG(0, LQUOTA_FLAG_DEFAULT);
357         } else {
358                 rec->qbr_hardlimit = lqe->lqe_hardlimit;
359                 rec->qbr_softlimit = lqe->lqe_softlimit;
360                 rec->qbr_time      = lqe->lqe_gracetime;
361         }
362
363         /* write new quota settings */
364         rc = lquota_disk_write(env, th, LQE_GLB_OBJ(lqe), &lqe->lqe_id,
365                                (struct dt_rec *)rec, flags, ver);
366         if (rc)
367                 /* we failed to write the new quota settings to disk, report
368                  * error to caller who will restore the initial value */
369                 LQUOTA_ERROR(lqe, "failed to update global index, rc:%d", rc);
370
371         RETURN(rc);
372 }
373
374 /*
375  * Read from disk how much quota space is allocated to a slave.
376  * This is done by reading records from the dedicated slave index file.
377  * Return in \granted how much quota space is currently allocated to the
378  * slave.
379  * The entry must be at least read locked.
380  *
381  * \param env - the environment passed by the caller
382  * \param lqe_id - is the quota id associated with the identifier to look-up
383  *              in the slave index
384  * \param slv_obj - is the dt_object associated with the slave index
385  * \param granted - is the output parameter where to return how much space
386  *                  is granted to the slave.
387  *
388  * \retval    - 0 on success, appropriate error on failure
389  */
390 int qmt_slv_read(const struct lu_env *env, union lquota_id *qid,
391                  struct dt_object *slv_obj, __u64 *granted)
392 {
393         struct qmt_thread_info  *qti = qmt_info(env);
394         struct lquota_slv_rec   *slv_rec = &qti->qti_slv_rec;
395         int                      rc;
396         ENTRY;
397
398         CDEBUG(D_QUOTA, "read id:%llu form slv "DFID"\n",
399                qid->qid_uid, PFID(lu_object_fid(&slv_obj->do_lu)));
400
401         /* read slave record from disk */
402         rc = lquota_disk_read(env, slv_obj, qid,
403                               (struct dt_rec *)slv_rec);
404         switch (rc) {
405         case -ENOENT:
406                 *granted = 0;
407                 break;
408         case 0:
409                 /* extract granted from on-disk record */
410                 *granted = slv_rec->qsr_granted;
411                 break;
412         default:
413                 CERROR("Failed to read slave record for %llu from "DFID"\n",
414                        qid->qid_uid, PFID(lu_object_fid(&slv_obj->do_lu)));
415                 RETURN(rc);
416         }
417
418         CDEBUG(D_QUOTA, "Successful slv read %llu\n", *granted);
419
420         RETURN(0);
421 }
422
423 /*
424  * Update record in slave index file.
425  * The entry must be at least read locked.
426  *
427  * \param env - the environment passed by the caller
428  * \param th  - is the transaction handle to be used for the disk writes
429  * \param lqe - is the dirty quota entry which will be updated at the same time
430  *              as the slave index
431  * \param slv_obj - is the dt_object associated with the slave index
432  * \param flags - can be LQUOTA_BUMP_VER or LQUOTA_SET_VER.
433  * \param ver   - is used to return the new version of the index.
434  * \param granted - is the new amount of quota space owned by the slave
435  *
436  * \retval    - 0 on success, appropriate error on failure
437  */
438 int qmt_slv_write(const struct lu_env *env, struct thandle *th,
439                   struct lquota_entry *lqe, struct dt_object *slv_obj,
440                   __u32 flags, __u64 *ver, __u64 granted)
441 {
442         struct qmt_thread_info  *qti = qmt_info(env);
443         struct lquota_slv_rec   *rec;
444         int                      rc;
445         ENTRY;
446
447         LASSERT(lqe != NULL);
448         LASSERT(lqe_is_master(lqe));
449         LASSERT(lqe_is_locked(lqe));
450
451         LQUOTA_DEBUG(lqe, "write slv "DFID" granted:%llu",
452                      PFID(lu_object_fid(&slv_obj->do_lu)), granted);
453
454         /* never delete the entry, otherwise, it'll not be transferred
455          * to slave during reintegration. */
456         rec = &qti->qti_slv_rec;
457
458         /* updated space granted to this slave */
459         rec->qsr_granted = granted;
460
461         /* write new granted space */
462         rc = lquota_disk_write(env, th, slv_obj, &lqe->lqe_id,
463                                (struct dt_rec *)rec, flags, ver);
464         if (rc) {
465                 LQUOTA_ERROR(lqe,
466                              "failed to update slave index "DFID" granted:%llu",
467                              PFID(lu_object_fid(&slv_obj->do_lu)),
468                              granted);
469                 RETURN(rc);
470         }
471
472         RETURN(0);
473 }
474
475 /*
476  * Check whether new limits are valid for this pool
477  *
478  * \param lqe  - is the quota entry subject to the setquota
479  * \param hard - is the new hard limit
480  * \param soft - is the new soft limit
481  */
482 int qmt_validate_limits(struct lquota_entry *lqe, __u64 hard, __u64 soft)
483 {
484         ENTRY;
485
486         if (hard != 0 && soft > hard)
487                 /* soft limit must be less than hard limit */
488                 RETURN(-EINVAL);
489         RETURN(0);
490 }
491
492 /*
493  * Set/clear edquot flag after quota space allocation/release or settings
494  * change. Slaves will be notified of changes via glimpse on per-ID lock
495  *
496  * \param lqe - is the quota entry to check
497  * \param now - is the current time in second used for grace time managment
498  */
499 bool qmt_adjust_edquot(struct lquota_entry *lqe, __u64 now)
500 {
501         struct qmt_pool_info    *pool = lqe2qpi(lqe);
502         ENTRY;
503
504         if (!lqe->lqe_enforced || lqe->lqe_id.qid_uid == 0)
505                 RETURN(false);
506
507         if (!lqe->lqe_edquot) {
508                 /* space exhausted flag not set, let's check whether it is time
509                  * to set the flag */
510
511                 if (!qmt_space_exhausted(lqe, now))
512                         /* the qmt still has available space */
513                         RETURN(false);
514
515                 /* See comment in qmt_adjust_qunit(). LU-4139 */
516                 if (qmt_hard_exhausted(lqe) ||
517                     pool->qpi_rtype != LQUOTA_RES_DT) {
518                         time64_t lapse;
519
520                         /* we haven't reached the minimal qunit yet so there is
521                          * still hope that the rebalancing process might free
522                          * up some quota space */
523                         if (lqe->lqe_qunit != pool->qpi_least_qunit)
524                                 RETURN(false);
525
526                         /* least qunit value not sent to all slaves yet */
527                         if (lqe->lqe_revoke_time == 0)
528                                 RETURN(false);
529
530                         /* Let's give more time to slave to release space */
531                         lapse = ktime_get_seconds() - QMT_REBA_TIMEOUT;
532                         if (lqe->lqe_may_rel != 0 && lqe->lqe_revoke_time > lapse)
533                                 RETURN(false);
534                 } else {
535                         if (lqe->lqe_qunit > pool->qpi_soft_least_qunit)
536                                 RETURN(false);
537                 }
538
539                 /* set edquot flag */
540                 lqe->lqe_edquot = true;
541         } else {
542                 /* space exhausted flag set, let's check whether it is time to
543                  * clear it */
544
545                 if (qmt_space_exhausted(lqe, now))
546                         /* the qmt still has not space */
547                         RETURN(false);
548
549                 if (lqe->lqe_hardlimit != 0 &&
550                     lqe->lqe_granted + pool->qpi_least_qunit >
551                                                         lqe->lqe_hardlimit)
552                         /* we clear the flag only once at least one least qunit
553                          * is available */
554                         RETURN(false);
555
556                 /* clear edquot flag */
557                 lqe->lqe_edquot = false;
558         }
559
560         LQUOTA_DEBUG(lqe, "changing edquot flag");
561
562         /* let's notify slave by issuing glimpse on per-ID lock.
563          * the rebalance thread will take care of this */
564         RETURN(true);
565 }
566
567 /* Using least_qunit when over block softlimit will seriously impact the
568  * write performance, we need to do some special tweaking on that. */
569 static __u64 qmt_calc_softlimit(struct lquota_entry *lqe, bool *oversoft)
570 {
571         struct qmt_pool_info *pool = lqe2qpi(lqe);
572
573         LASSERT(lqe->lqe_softlimit != 0);
574         *oversoft = false;
575         /* No need to do special tweaking for inode limit */
576         if (pool->qpi_rtype != LQUOTA_RES_DT)
577                 return lqe->lqe_softlimit;
578
579         if (lqe->lqe_granted <= lqe->lqe_softlimit +
580                                 pool->qpi_soft_least_qunit) {
581                 return lqe->lqe_softlimit;
582         } else if (lqe->lqe_hardlimit != 0) {
583                 *oversoft = true;
584                 return lqe->lqe_hardlimit;
585         } else {
586                 *oversoft = true;
587                 return 0;
588         }
589 }
590
591 /*
592  * Try to grant more quota space back to slave.
593  *
594  * \param lqe     - is the quota entry for which we would like to allocate more
595  *                  space
596  * \param granted - is how much was already granted as part of the request
597  *                  processing
598  * \param spare   - is how much unused quota space the slave already owns
599  *
600  * \retval return how additional space can be granted to the slave
601  */
602 __u64 qmt_alloc_expand(struct lquota_entry *lqe, __u64 granted, __u64 spare)
603 {
604         struct qmt_pool_info    *pool = lqe2qpi(lqe);
605         __u64                    remaining, qunit;
606         int                      slv_cnt;
607
608         LASSERT(lqe->lqe_enforced && lqe->lqe_qunit != 0);
609
610         slv_cnt = qpi_slv_nr(lqe2qpi(lqe), lqe_qtype(lqe));
611         qunit = lqe->lqe_qunit;
612
613         /* See comment in qmt_adjust_qunit(). LU-4139. */
614         if (lqe->lqe_softlimit != 0) {
615                 bool oversoft;
616                 remaining = qmt_calc_softlimit(lqe, &oversoft);
617                 if (remaining == 0)
618                         remaining = lqe->lqe_granted +
619                                     pool->qpi_soft_least_qunit;
620         } else {
621                 remaining = lqe->lqe_hardlimit;
622         }
623
624         if (lqe->lqe_granted >= remaining)
625                 RETURN(0);
626
627         remaining -= lqe->lqe_granted;
628
629         do {
630                 if (spare >= qunit)
631                         break;
632
633                 granted &= (qunit - 1);
634
635                 if (remaining > (slv_cnt * qunit) >> 1) {
636                         /* enough room to grant more space w/o additional
637                          * shrinking ... at least for now */
638                         remaining -= (slv_cnt * qunit) >> 1;
639                 } else if (qunit != pool->qpi_least_qunit) {
640                         qunit >>= 2;
641                         continue;
642                 }
643
644                 granted &= (qunit - 1);
645                 if (spare > 0)
646                         RETURN(min_t(__u64, qunit - spare, remaining));
647                 else
648                         RETURN(min_t(__u64, qunit - granted, remaining));
649         } while (qunit >= pool->qpi_least_qunit);
650
651         RETURN(0);
652 }
653
654 /*
655  * Adjust qunit size according to quota limits and total granted count.
656  * The caller must have locked the lqe.
657  *
658  * \param env - the environment passed by the caller
659  * \param lqe - is the qid entry to be adjusted
660  * \retval true - need reseed glbe array
661  */
662 bool qmt_adjust_qunit(const struct lu_env *env, struct lquota_entry *lqe)
663 {
664         struct qmt_pool_info    *pool = lqe2qpi(lqe);
665         bool                     need_reseed = false;
666         int                      slv_cnt;
667         __u64                    qunit, limit, qunit2 = 0;
668         ENTRY;
669
670         LASSERT(lqe_is_locked(lqe));
671
672         if (!lqe->lqe_enforced || lqe->lqe_id.qid_uid == 0)
673                 /* no quota limits */
674                 RETURN(need_reseed);
675
676         /* record how many slaves have already registered */
677         slv_cnt = qpi_slv_nr(pool, lqe_qtype(lqe));
678         if (slv_cnt == 0) {
679                 /* Pool hasn't slaves anymore. Qunit will be adjusted
680                  * again when new slaves would be added. */
681                 if (lqe->lqe_qunit) {
682                         qunit = 0;
683                         GOTO(done, qunit);
684                 }
685                 /* wait for at least one slave to join */
686                 RETURN(need_reseed);
687         }
688
689         /* Qunit calculation is based on soft limit, if any, hard limit
690          * otherwise. This means that qunit is shrunk to the minimum when
691          * beyond the soft limit. This will impact performance, but that's the
692          * price of an accurate grace time management. */
693         if (lqe->lqe_softlimit != 0) {
694                 bool oversoft;
695                 /* As a compromise of write performance and the grace time
696                  * accuracy, the block qunit size will be shrunk to
697                  * qpi_soft_least_qunit when over softlimit. LU-4139. */
698                 limit = qmt_calc_softlimit(lqe, &oversoft);
699                 if (oversoft)
700                         qunit2 = pool->qpi_soft_least_qunit;
701                 if (limit == 0)
702                         GOTO(done, qunit = qunit2);
703         } else if (lqe->lqe_hardlimit != 0) {
704                 limit = lqe->lqe_hardlimit;
705         } else {
706                 LQUOTA_ERROR(lqe, "enforced bit set, but neither hard nor soft "
707                              "limit are set");
708                 RETURN(need_reseed);
709         }
710
711         qunit = lqe->lqe_qunit == 0 ? pool->qpi_least_qunit : lqe->lqe_qunit;
712
713         /* The qunit value is computed as follows: limit / (2 * slv_cnt).
714          * Then 75% of the quota space can be granted with current qunit value.
715          * The remaining 25% are then used with reduced qunit size (by a factor
716          * of 4) which is then divided in a similar manner.
717          *
718          * |---------------------limit---------------------|
719          * |-------limit / 2-------|-limit / 4-|-limit / 4-|
720          * |qunit|qunit|qunit|qunit|           |           |
721          * |----slv_cnt * qunit----|           |           |
722          * |-grow limit-|          |           |           |
723          * |--------------shrink limit---------|           |
724          * |---space granted in qunit chunks---|-remaining-|
725          *                                    /             \
726          *                                   /               \
727          *                                  /                 \
728          *                                 /                   \
729          *                                /                     \
730          *     qunit >>= 2;            |qunit*slv_cnt|qunit*slv_cnt|
731          *                             |---space in qunit---|remain|
732          *                                  ...                               */
733         if (qunit == pool->qpi_least_qunit ||
734             limit >= lqe->lqe_granted + ((slv_cnt * qunit) >> 1)) {
735                 /* current qunit value still fits, let's see if we can afford to
736                  * increase qunit now ...
737                  * To increase qunit again, we have to be under 25% */
738                 while (qunit && limit >= lqe->lqe_granted + 6 * qunit * slv_cnt)
739                         qunit <<= 2;
740
741                 if (!qunit) {
742                         qunit = limit;
743                         do_div(qunit, 2 * slv_cnt);
744                 }
745
746         } else {
747                 /* shrink qunit until we find a suitable value */
748                 while (qunit > pool->qpi_least_qunit &&
749                        limit < lqe->lqe_granted + ((slv_cnt * qunit) >> 1))
750                         qunit >>= 2;
751         }
752
753         if (qunit2 && qunit > qunit2)
754                 qunit = qunit2;
755 done:
756         if (lqe->lqe_qunit == qunit)
757                 /* keep current qunit */
758                 RETURN(need_reseed);
759
760         LQUOTA_DEBUG(lqe, "%s qunit to %llu",
761                      lqe->lqe_qunit < qunit ? "increasing" : "decreasing",
762                      qunit);
763
764         /* store new qunit value */
765         swap(lqe->lqe_qunit, qunit);
766
767         /* reseed glbe array and notify
768          * slave if qunit was shrinked */
769         need_reseed = true;
770         /* reset revoke time */
771         lqe->lqe_revoke_time = 0;
772
773         if (lqe->lqe_qunit >= qunit &&
774             (lqe->lqe_qunit == pool->qpi_least_qunit)) {
775                 /* initial qunit value is the smallest one */
776                 lqe->lqe_revoke_time = ktime_get_seconds();
777         }
778         RETURN(need_reseed);
779 }
780
781 bool qmt_adjust_edquot_qunit_notify(const struct lu_env *env,
782                                     struct qmt_device *qmt,
783                                     __u64 now, bool edquot,
784                                     bool qunit, __u32 qb_flags)
785 {
786         struct lquota_entry *lqe_gl, *lqe;
787         bool need_reseed = false;
788         int i;
789
790         lqe_gl = qti_lqes_glbl(env);
791
792         for (i = 0; i < qti_lqes_cnt(env); i++) {
793                 lqe = qti_lqes(env)[i];
794                 if (qunit)
795                         need_reseed |= qmt_adjust_qunit(env, lqe);
796                 if (edquot)
797                         need_reseed |= qmt_adjust_edquot(lqe, now);
798         }
799
800         LASSERT(lqe_gl);
801         if (!lqe_gl->lqe_glbl_data &&
802             (req_has_rep(qb_flags) || req_is_rel(qb_flags))) {
803                 if (need_reseed)
804                         CDEBUG(D_QUOTA,
805                                "%s: can not notify - lge_glbl_data is not set\n",
806                                qmt->qmt_svname);
807                 return need_reseed;
808         }
809
810         if (lqe_gl->lqe_glbl_data && need_reseed) {
811                 qmt_seed_glbe_all(env, lqe_gl->lqe_glbl_data, qunit, edquot);
812                 qmt_id_lock_notify(qmt, lqe_gl);
813         }
814         return need_reseed;
815 }
816
817
818 /*
819  * Adjust qunit & edquot flag in case it wasn't initialized already (e.g.
820  * limit set while no slaves were connected yet)
821  */
822 bool qmt_revalidate(const struct lu_env *env, struct lquota_entry *lqe)
823 {
824         bool need_notify = false;
825
826         if (lqe->lqe_qunit == 0) {
827                 /* lqe was read from disk, but neither qunit, nor edquot flag
828                  * were initialized */
829                 need_notify = qmt_adjust_qunit(env, lqe);
830                 if (lqe->lqe_qunit != 0)
831                         need_notify |= qmt_adjust_edquot(lqe,
832                                                 ktime_get_real_seconds());
833         }
834
835         return need_notify;
836 }
837
838 void qmt_revalidate_lqes(const struct lu_env *env,
839                          struct qmt_device *qmt, __u32 qb_flags)
840 {
841         struct lquota_entry *lqe_gl = qti_lqes_glbl(env);
842         bool need_notify = false;
843         int i;
844
845         for (i = 0; i < qti_lqes_cnt(env); i++)
846                 need_notify |= qmt_revalidate(env, qti_lqes(env)[i]);
847
848         /* There could be no ID lock to the moment of reconciliation.
849          * As a result lqe global data is not initialised yet. It is ok
850          * for release and report requests. */
851         if (!lqe_gl->lqe_glbl_data &&
852             (req_is_rel(qb_flags) || req_has_rep(qb_flags)))
853                 return;
854
855         if (need_notify) {
856                 qmt_seed_glbe(env, lqe_gl->lqe_glbl_data);
857                 qmt_id_lock_notify(qmt, lqe_gl);
858         }
859 }
860
861 void qti_lqes_init(const struct lu_env *env)
862 {
863         struct qmt_thread_info  *qti = qmt_info(env);
864
865         qti->qti_lqes_cnt = 0;
866         qti->qti_glbl_lqe_idx = 0;
867         qti->qti_lqes_num = QMT_MAX_POOL_NUM;
868 }
869
870 int qti_lqes_add(const struct lu_env *env, struct lquota_entry *lqe)
871 {
872         struct qmt_thread_info  *qti = qmt_info(env);
873
874         if (qti->qti_lqes_cnt > qti->qti_lqes_num) {
875                 struct lquota_entry     **lqes;
876                 lqes = qti->qti_lqes;
877                 OBD_ALLOC(lqes, sizeof(lqe) * qti->qti_lqes_num * 2);
878                 if (!lqes)
879                         return -ENOMEM;
880                 memcpy(lqes, qti_lqes(env), qti->qti_lqes_cnt * sizeof(lqe));
881                 /* Don't need to free, if it is the very 1st allocation */
882                 if (qti->qti_lqes_num > QMT_MAX_POOL_NUM)
883                         OBD_FREE(qti->qti_lqes,
884                                  qti->qti_lqes_num * sizeof(lqe));
885                 qti->qti_lqes = lqes;
886                 qti->qti_lqes_num *= 2;
887         }
888
889         if (lqe->lqe_is_global)
890                 qti->qti_glbl_lqe_idx = qti->qti_lqes_cnt;
891         qti_lqes(env)[qti->qti_lqes_cnt++] = lqe;
892
893         /* The pool could be accessed directly from lqe, so take
894          * extra reference that is put in qti_lqes_fini */
895         qpi_getref(lqe2qpi(lqe));
896
897         CDEBUG(D_QUOTA, "LQE %p %lu is added, lqe_cnt %d lqes_num %d\n",
898                          lqe, (long unsigned)lqe->lqe_id.qid_uid,
899                          qti->qti_lqes_cnt, qti->qti_lqes_num);
900         LASSERT(qti->qti_lqes_num != 0);
901
902         return 0;
903 }
904
905 void qti_lqes_del(const struct lu_env *env, int index)
906 {
907         struct lquota_entry     **lqes;
908         int lqes_cnt = qti_lqes_cnt(env);
909         int lqep_size = sizeof(struct lquota_entry *);
910
911         if (index == 0) {
912                 /* We can't handle non global lqes correctly without
913                  * global lqe located at index 0. If we try to do so,
914                  * something goes wrong. */
915                 LQUOTA_ERROR(qti_lqes_glbl(env),
916                              "quota: cannot remove lqe at index 0 as it is global");
917                 LASSERT(qti_lqes_glbl(env)->lqe_is_global);
918                 return;
919         }
920         lqes = qti_lqes(env);
921         qpi_putref(env, lqe2qpi(lqes[index]));
922         lqe_putref(lqes[index]);
923         memcpy((unsigned char *)lqes + index * lqep_size,
924                (unsigned char *)lqes + (index + 1) * lqep_size,
925                (lqes_cnt - index - 1) * lqep_size);
926         qti_lqes_cnt(env)--;
927 }
928
929 void qti_lqes_fini(const struct lu_env *env)
930 {
931         struct qmt_thread_info  *qti = qmt_info(env);
932         struct lquota_entry     **lqes = qti->qti_lqes;
933         int i;
934
935         lqes = qti_lqes(env);
936         for (i = 0; i < qti->qti_lqes_cnt; i++) {
937                 qpi_putref(env, lqe2qpi(lqes[i]));
938                 lqe_putref(lqes[i]);
939         }
940
941         if (qti->qti_lqes_num > QMT_MAX_POOL_NUM)
942                 OBD_FREE(qti->qti_lqes,
943                          qti->qti_lqes_num * sizeof(struct lquota_entry *));
944 }
945
946 inline int qti_lqes_min_qunit(const struct lu_env *env)
947 {
948         int i, min, qunit;
949
950         for (i = 1, min = qti_lqe_qunit(env, 0); i < qti_lqes_cnt(env); i++) {
951                 qunit = qti_lqe_qunit(env, i);
952                 if (qunit < min)
953                         min = qunit;
954         }
955
956         return min;
957 }
958
959 inline int qti_lqes_edquot(const struct lu_env *env)
960 {
961         int i;
962
963         for (i = 0; i < qti_lqes_cnt(env); i++) {
964                 if (qti_lqes(env)[i]->lqe_edquot)
965                         return 1;
966         }
967
968         return 0;
969 }
970
971 inline int qti_lqes_restore_init(const struct lu_env *env)
972 {
973         int rc = 0;
974
975         if (qti_lqes_cnt(env) > QMT_MAX_POOL_NUM) {
976                 OBD_ALLOC(qmt_info(env)->qti_lqes_rstr,
977                           qti_lqes_cnt(env) * sizeof(struct qmt_lqe_restore));
978                 if (!qti_lqes_rstr(env))
979                         rc = -ENOMEM;
980         }
981
982         return rc;
983 }
984
985 inline void qti_lqes_restore_fini(const struct lu_env *env)
986 {
987         if (qti_lqes_cnt(env) > QMT_MAX_POOL_NUM)
988                 OBD_FREE(qmt_info(env)->qti_lqes_rstr,
989                          qti_lqes_cnt(env) * sizeof(struct qmt_lqe_restore));
990 }
991
992 inline void qti_lqes_write_lock(const struct lu_env *env)
993 {
994         int i;
995
996         for (i = 0; i < qti_lqes_cnt(env); i++)
997                 lqe_write_lock(qti_lqes(env)[i]);
998 }
999
1000 inline void qti_lqes_write_unlock(const struct lu_env *env)
1001 {
1002         int i;
1003
1004         for (i = 0; i < qti_lqes_cnt(env); i++)
1005                 lqe_write_unlock(qti_lqes(env)[i]);
1006 }
1007
1008 #define QMT_INIT_SLV_CNT        64
1009 struct lqe_glbl_data *qmt_alloc_lqe_gd(struct qmt_pool_info *pool, int qtype)
1010 {
1011         struct lqe_glbl_data    *lgd;
1012         struct lqe_glbl_entry   *lqeg_arr;
1013         int                      slv_cnt, glbe_num;
1014
1015         OBD_ALLOC(lgd, sizeof(struct lqe_glbl_data));
1016         if (!lgd)
1017                 RETURN(NULL);
1018
1019         slv_cnt = qpi_slv_nr_by_rtype(pool, qtype);
1020
1021         glbe_num = slv_cnt < QMT_INIT_SLV_CNT ? QMT_INIT_SLV_CNT : slv_cnt;
1022         OBD_ALLOC(lqeg_arr, sizeof(struct lqe_glbl_entry) * glbe_num);
1023         if (!lqeg_arr) {
1024                 OBD_FREE(lgd, sizeof(struct lqe_glbl_data));
1025                 RETURN(NULL);
1026         }
1027
1028         CDEBUG(D_QUOTA, "slv_cnt %d glbe_num %d\n", slv_cnt, glbe_num);
1029
1030         lgd->lqeg_num_used = slv_cnt;
1031         lgd->lqeg_num_alloc = glbe_num;
1032         lgd->lqeg_arr = lqeg_arr;
1033
1034         RETURN(lgd);
1035 }
1036
1037 void qmt_free_lqe_gd(struct lqe_glbl_data *lgd)
1038 {
1039         OBD_FREE(lgd->lqeg_arr,
1040                  sizeof(struct lqe_glbl_entry) * lgd->lqeg_num_alloc);
1041         OBD_FREE(lgd, sizeof(struct lqe_glbl_data));
1042 }
1043
1044 void qmt_seed_glbe_all(const struct lu_env *env, struct lqe_glbl_data *lgd,
1045                        bool qunit, bool edquot)
1046 {
1047         struct rw_semaphore     *sem = NULL;
1048         struct qmt_pool_info    *qpi;
1049         int                      i, j, idx;
1050         ENTRY;
1051
1052         /* lqes array is sorted by qunit - the first entry has minimum qunit.
1053          * Thus start seeding global qunit's array beginning from the 1st lqe
1054          * and appropriate pool. If pools overlapped, slaves from this
1055          * overlapping get minimum qunit value.
1056          * user1: pool1, pool2, pool_glbl;
1057          * pool1: OST1; user1_qunit = 10M;
1058          * pool2: OST0, OST1, OST2; user1_qunit = 30M;
1059          * pool_glbl: OST0, OST1, OST2, OST3; user1_qunit = 160M;
1060          * qunit array after seeding should be:
1061          * OST0: 30M; OST1: 10M; OST2: 30M; OST3: 160M; */
1062
1063         /* edquot resetup algorythm works fine
1064          * with not sorted lqes */
1065         if (qunit)
1066                 qmt_lqes_sort(env);
1067
1068         for (i = 0; i < lgd->lqeg_num_used; i++) {
1069                 lgd->lqeg_arr[i].lge_qunit_set = 0;
1070                 lgd->lqeg_arr[i].lge_qunit_nu = 0;
1071                 lgd->lqeg_arr[i].lge_edquot_nu = 0;
1072         }
1073
1074         for (i = 0; i < qti_lqes_cnt(env); i++) {
1075                 struct lquota_entry *lqe = qti_lqes(env)[i];
1076                 int slaves_cnt;
1077
1078                 CDEBUG(D_QUOTA, "lqes_cnt %d, i %d\n", qti_lqes_cnt(env), i);
1079                 qpi = lqe2qpi(lqe);
1080                 if (qmt_pool_global(qpi)) {
1081                         slaves_cnt = qpi_slv_nr_by_rtype(lqe2qpi(lqe),
1082                                                          lqe_qtype(lqe));
1083                 } else {
1084                         sem = qmt_sarr_rwsem(qpi);
1085                         down_read(sem);
1086                         slaves_cnt = qmt_sarr_count(qpi);
1087                 }
1088
1089                 for (j = 0; j < slaves_cnt; j++) {
1090                         idx = qmt_sarr_get_idx(qpi, j);
1091                         LASSERT(idx >= 0);
1092
1093                         if (edquot) {
1094                                 int lge_edquot, new_edquot, edquot_nu;
1095
1096                                 lge_edquot = lgd->lqeg_arr[idx].lge_edquot;
1097                                 edquot_nu = lgd->lqeg_arr[idx].lge_edquot_nu;
1098                                 new_edquot = lqe->lqe_edquot;
1099
1100                                 if (lge_edquot == new_edquot ||
1101                                     (edquot_nu && lge_edquot == 1))
1102                                         goto qunit_lbl;
1103                                 lgd->lqeg_arr[idx].lge_edquot = new_edquot;
1104                                 /* it is needed for the following case:
1105                                  * initial values for idx i -
1106                                  * lqe_edquot = 1, lqe_edquot_nu == 0;
1107                                  * 1: new_edquot == 0 ->
1108                                  *      lqe_edquot = 0, lqe_edquot_nu = 1;
1109                                  * 2: new_edquot == 1 ->
1110                                  *      lqe_edquot = 1, lqe_edquot_nu = 0;
1111                                  * At the 2nd iteration lge_edquot comes back
1112                                  * to 1, so no changes and we don't need
1113                                  * to notify slave. */
1114                                 lgd->lqeg_arr[idx].lge_edquot_nu = !edquot_nu;
1115                         }
1116 qunit_lbl:
1117                         if (qunit) {
1118                                 __u64 lge_qunit, new_qunit;
1119
1120                                 CDEBUG(D_QUOTA,
1121                                        "idx %d lge_qunit_set %d lge_qunit %llu new_qunit %llu\n",
1122                                        idx, lgd->lqeg_arr[idx].lge_qunit_set,
1123                                        lgd->lqeg_arr[idx].lge_qunit,
1124                                        lqe->lqe_qunit);
1125                                 /* lge for this idx is already set
1126                                  * on previous iteration */
1127                                 if (lgd->lqeg_arr[idx].lge_qunit_set)
1128                                         continue;
1129                                 lge_qunit = lgd->lqeg_arr[idx].lge_qunit;
1130                                 new_qunit = lqe->lqe_qunit;
1131                                 /* qunit could be not set,
1132                                  * so use global lqe's qunit */
1133                                 if (!new_qunit)
1134                                         continue;
1135
1136                                 if (lge_qunit != new_qunit)
1137                                         lgd->lqeg_arr[idx].lge_qunit =
1138                                                                 new_qunit;
1139
1140                                 /* TODO: initially slaves notification was done
1141                                  * only for qunit shrinking. Should we always
1142                                  * notify slaves with new qunit ? */
1143                                 if (lge_qunit > new_qunit)
1144                                         lgd->lqeg_arr[idx].lge_qunit_nu = 1;
1145                                 lgd->lqeg_arr[idx].lge_qunit_set = 1;
1146                         }
1147                 }
1148
1149                 if (!qmt_pool_global(qpi))
1150                         up_read(sem);
1151         }
1152         /* TODO: only for debug purposes - remove it later */
1153         for (i = 0; i < lgd->lqeg_num_used; i++)
1154                 CDEBUG(D_QUOTA,
1155                         "lgd ost %d, qunit %lu nu %d;  edquot %d nu %d\n",
1156                         i, (long unsigned)lgd->lqeg_arr[i].lge_qunit,
1157                         lgd->lqeg_arr[i].lge_qunit_nu,
1158                         lgd->lqeg_arr[i].lge_edquot,
1159                         lgd->lqeg_arr[i].lge_edquot_nu);
1160
1161         EXIT;
1162 }
1163
1164 void qmt_setup_lqe_gd(const struct lu_env *env, struct qmt_device *qmt,
1165                       struct lquota_entry *lqe, struct lqe_glbl_data *lgd,
1166                       int pool_type)
1167 {
1168         __u64                    qunit;
1169         bool                     edquot;
1170         int                      i;
1171
1172         qunit = lqe->lqe_qunit;
1173         edquot = lqe->lqe_edquot;
1174
1175         /* Firstly set all elements in array with
1176          * qunit and edquot of global pool */
1177         for (i = 0; i < lgd->lqeg_num_used; i++) {
1178                 lgd->lqeg_arr[i].lge_qunit = qunit;
1179                 lgd->lqeg_arr[i].lge_edquot = edquot;
1180                 /* It is the very first lvb setup - qunit and other flags
1181                  * will be sent to slaves during qmt_lvbo_fill. */
1182                 lgd->lqeg_arr[i].lge_qunit_nu = 0;
1183                 lgd->lqeg_arr[i].lge_edquot_nu = 0;
1184         }
1185
1186         qmt_pool_lqes_lookup_spec(env, qmt, pool_type,
1187                                   lqe_qtype(lqe), &lqe->lqe_id);
1188         qmt_seed_glbe(env, lgd);
1189
1190         lqe->lqe_glbl_data = lgd;
1191         qmt_id_lock_notify(qmt, lqe);
1192
1193         qti_lqes_fini(env);
1194 }