Whamcloud - gitweb
LU-6245 server: remove types abstraction from quota/target/nodemap code
[fs/lustre-release.git] / lustre / quota / qsd_writeback.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.lombardi@intel.com>
28  * Author: Niu    Yawei    <yawei.niu@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LQUOTA
32
33 #include <linux/kthread.h>
34 #include "qsd_internal.h"
35
36 extern struct kmem_cache *upd_kmem;
37
38 /*
39  * Allocate and fill an qsd_upd_rec structure to be processed by the writeback
40  * thread.
41  *
42  * \param qqi - is the qsd_qtype_info structure relevant to the update
43  * \param lqe - is the lquota entry subject to the update
44  * \param qid - is the identifier subject to the update
45  * \param rec - is the record storing the new quota settings
46  * \param ver - is the version associated with the update
47  * \param global - is a boolean set to true if this is an update of the global
48  *                 index and false for a slave index.
49  */
50 static struct qsd_upd_rec *qsd_upd_alloc(struct qsd_qtype_info *qqi,
51                                          struct lquota_entry *lqe,
52                                          union lquota_id *qid,
53                                          union lquota_rec *rec, __u64 ver,
54                                          bool global)
55 {
56         struct qsd_upd_rec      *upd;
57
58         OBD_SLAB_ALLOC_PTR_GFP(upd, upd_kmem, GFP_NOFS);
59         if (upd == NULL) {
60                 return NULL;
61         }
62
63         /* fill it */
64         INIT_LIST_HEAD(&upd->qur_link);
65         upd->qur_qqi = qqi;
66         upd->qur_lqe = lqe;
67         if (lqe)
68                 lqe_getref(lqe);
69         upd->qur_qid    = *qid;
70         upd->qur_rec    = *rec;
71         upd->qur_ver    = ver;
72         upd->qur_global = global;
73
74         return upd;
75 }
76
77 static void qsd_upd_free(struct qsd_upd_rec *upd)
78 {
79         if (upd->qur_lqe)
80                 lqe_putref(upd->qur_lqe);
81         OBD_SLAB_FREE_PTR(upd, upd_kmem);
82 }
83
84 /* must hold the qsd_lock */
85 static void qsd_upd_add(struct qsd_instance *qsd, struct qsd_upd_rec *upd)
86 {
87         if (!qsd->qsd_stopping) {
88                 list_add_tail(&upd->qur_link, &qsd->qsd_upd_list);
89                 /* wake up the upd thread */
90                 wake_up(&qsd->qsd_upd_thread.t_ctl_waitq);
91         } else {
92                 CWARN("%s: discard update.\n", qsd->qsd_svname);
93                 if (upd->qur_lqe)
94                         LQUOTA_WARN(upd->qur_lqe, "discard update.");
95                 qsd_upd_free(upd);
96         }
97 }
98
99 /* must hold the qsd_lock */
100 static void qsd_add_deferred(struct qsd_instance *qsd, struct list_head *list,
101                              struct qsd_upd_rec *upd)
102 {
103         struct qsd_upd_rec      *tmp, *n;
104
105         if (qsd->qsd_stopping) {
106                 CWARN("%s: discard deferred udpate.\n", qsd->qsd_svname);
107                 if (upd->qur_lqe)
108                         LQUOTA_WARN(upd->qur_lqe, "discard deferred update.");
109                 qsd_upd_free(upd);
110                 return;
111         }
112
113         /* Sort the updates in ascending order */
114         list_for_each_entry_safe_reverse(tmp, n, list, qur_link) {
115
116                 /* There could be some legacy records which have duplicated
117                  * version. Imagine following scenario: slave received global
118                  * glimpse and queued a record in the deferred list, then
119                  * master crash and rollback to an ealier version, then the
120                  * version of queued record will be conflicting with later
121                  * updates. We should just delete the legacy record in such
122                  * case. */
123                 if (upd->qur_ver == tmp->qur_ver) {
124                         LASSERT(tmp->qur_lqe);
125                         LQUOTA_ERROR(tmp->qur_lqe, "Found a conflict record "
126                                      "with ver:%llu", tmp->qur_ver);
127                         list_del_init(&tmp->qur_link);
128                         qsd_upd_free(tmp);
129                 } else if (upd->qur_ver < tmp->qur_ver) {
130                         continue;
131                 } else {
132                         list_add_tail(&upd->qur_link, &tmp->qur_link);
133                         return;
134                 }
135         }
136         list_add(&upd->qur_link, list);
137 }
138
139 /* must hold the qsd_lock */
140 static void qsd_kickoff_deferred(struct qsd_qtype_info *qqi,
141                                  struct list_head *list, __u64 ver)
142 {
143         struct qsd_upd_rec      *upd, *tmp;
144         ENTRY;
145
146         /* Get the first update record in the list, which has the smallest
147          * version, discard all records with versions smaller than the current
148          * one */
149         list_for_each_entry_safe(upd, tmp, list, qur_link) {
150                 if (upd->qur_ver <= ver) {
151                         /* drop this update */
152                         list_del_init(&upd->qur_link);
153                         CDEBUG(D_QUOTA, "%s: skipping deferred update ver:"
154                                "%llu/%llu, global:%d, qid:%llu\n",
155                                qqi->qqi_qsd->qsd_svname, upd->qur_ver, ver,
156                                upd->qur_global, upd->qur_qid.qid_uid);
157                         qsd_upd_free(upd);
158                 } else {
159                         break;
160                 }
161         }
162
163         /* No remaining deferred update */
164         if (list_empty(list))
165                 RETURN_EXIT;
166
167         CDEBUG(D_QUOTA, "%s: found deferred update record. "
168                "version:%llu/%llu, global:%d, qid:%llu\n",
169                qqi->qqi_qsd->qsd_svname, upd->qur_ver, ver,
170                upd->qur_global, upd->qur_qid.qid_uid);
171
172         LASSERTF(upd->qur_ver > ver, "lur_ver:%llu, cur_ver:%llu\n",
173                  upd->qur_ver, ver);
174
175         /* Kick off the deferred udpate */
176         if (upd->qur_ver == ver + 1) {
177                 list_del_init(&upd->qur_link);
178                 qsd_upd_add(qqi->qqi_qsd, upd);
179         }
180         EXIT;
181 }
182
183 /* Bump version of global or slave index copy
184  *
185  * \param qqi    - qsd_qtype_info
186  * \param ver    - version to be bumped to
187  * \param global - global or slave index copy?
188  */
189 void qsd_bump_version(struct qsd_qtype_info *qqi, __u64 ver, bool global)
190 {
191         struct list_head *list;
192         __u64            *idx_ver;
193
194         idx_ver = global ? &qqi->qqi_glb_ver : &qqi->qqi_slv_ver;
195         list    = global ? &qqi->qqi_deferred_glb : &qqi->qqi_deferred_slv;
196
197         write_lock(&qqi->qqi_qsd->qsd_lock);
198         *idx_ver = ver;
199         if (global)
200                 qqi->qqi_glb_uptodate = 1;
201         else
202                 qqi->qqi_slv_uptodate = 1;
203         qsd_kickoff_deferred(qqi, list, ver);
204         write_unlock(&qqi->qqi_qsd->qsd_lock);
205 }
206
207 /*
208  * Schedule a commit of a lquota entry
209  *
210  * \param  qqi   - qsd_qtype_info
211  * \param  lqe   - lquota_entry
212  * \param  qid   - quota id
213  * \param  rec   - global or slave record to be updated to disk
214  * \param  ver   - new index file version
215  * \param  global- true: master record; false: slave record
216  */
217 void qsd_upd_schedule(struct qsd_qtype_info *qqi, struct lquota_entry *lqe,
218                       union lquota_id *qid, union lquota_rec *rec, __u64 ver,
219                       bool global)
220 {
221         struct qsd_upd_rec      *upd;
222         struct qsd_instance     *qsd = qqi->qqi_qsd;
223         __u64                    cur_ver;
224         ENTRY;
225
226         CDEBUG(D_QUOTA, "%s: schedule update. global:%s, version:%llu\n",
227                qsd->qsd_svname, global ? "true" : "false", ver);
228
229         upd = qsd_upd_alloc(qqi, lqe, qid, rec, ver, global);
230         if (upd == NULL)
231                 RETURN_EXIT;
232
233         /* If we don't want update index version, no need to sort the
234          * records in version order, just schedule the updates instantly. */
235         if (ver == 0) {
236                 write_lock(&qsd->qsd_lock);
237                 qsd_upd_add(qsd, upd);
238                 write_unlock(&qsd->qsd_lock);
239                 RETURN_EXIT;
240         }
241
242         write_lock(&qsd->qsd_lock);
243
244         cur_ver = global ? qqi->qqi_glb_ver : qqi->qqi_slv_ver;
245
246         if (ver <= cur_ver) {
247                 if (global)
248                         /* legitimate race between glimpse AST and
249                          * reintegration */
250                         CDEBUG(D_QUOTA, "%s: discarding glb update from glimpse"
251                                " ver:%llu local ver:%llu\n",
252                                qsd->qsd_svname, ver, cur_ver);
253                 else
254                         CERROR("%s: discard slv update, ver:%llu local ver:"
255                                "%llu\n", qsd->qsd_svname, ver, cur_ver);
256                 qsd_upd_free(upd);
257         } else if ((ver == cur_ver + 1) && qqi->qqi_glb_uptodate &&
258                    qqi->qqi_slv_uptodate) {
259                 /* In order update, and reintegration has been done. */
260                 qsd_upd_add(qsd, upd);
261         } else {
262                 /* Out of order update (the one with smaller version hasn't
263                  * reached slave or hasn't been flushed to disk yet), or
264                  * the reintegration is in progress. Defer the update. */
265                 struct list_head *list = global ? &qqi->qqi_deferred_glb :
266                                                   &qqi->qqi_deferred_slv;
267                 qsd_add_deferred(qsd, list, upd);
268         }
269
270         write_unlock(&qsd->qsd_lock);
271
272         EXIT;
273 }
274
275 static int qsd_process_upd(const struct lu_env *env, struct qsd_upd_rec *upd)
276 {
277         struct lquota_entry     *lqe = upd->qur_lqe;
278         struct qsd_qtype_info   *qqi = upd->qur_qqi;
279         int                      rc;
280         ENTRY;
281
282         if (lqe == NULL) {
283                 lqe = lqe_locate(env, qqi->qqi_site, &upd->qur_qid);
284                 if (IS_ERR(lqe))
285                         GOTO(out, rc = PTR_ERR(lqe));
286         }
287
288         /* The in-memory lqe update for slave index copy isn't deferred,
289          * we shouldn't touch it here. */
290         if (upd->qur_global) {
291                 rc = qsd_update_lqe(env, lqe, upd->qur_global, &upd->qur_rec);
292                 if (rc)
293                         GOTO(out, rc);
294                 /* refresh usage */
295                 qsd_refresh_usage(env, lqe);
296                 /* Report usage asynchronously */
297                 rc = qsd_adjust(env, lqe);
298                 if (rc)
299                         LQUOTA_ERROR(lqe, "failed to report usage, rc:%d", rc);
300         }
301
302         rc = qsd_update_index(env, qqi, &upd->qur_qid, upd->qur_global,
303                               upd->qur_ver, &upd->qur_rec);
304 out:
305         if (lqe && !IS_ERR(lqe)) {
306                 lqe_putref(lqe);
307                 upd->qur_lqe = NULL;
308         }
309         RETURN(rc);
310 }
311
312 void qsd_adjust_schedule(struct lquota_entry *lqe, bool defer, bool cancel)
313 {
314         struct qsd_instance     *qsd = lqe2qqi(lqe)->qqi_qsd;
315         bool                     added = false;
316
317         read_lock(&qsd->qsd_lock);
318         if (qsd->qsd_stopping) {
319                 read_unlock(&qsd->qsd_lock);
320                 return;
321         }
322         read_unlock(&qsd->qsd_lock);
323
324         lqe_getref(lqe);
325         spin_lock(&qsd->qsd_adjust_lock);
326
327         /* the lqe is being queued for the per-ID lock cancel, we should
328          * cancel the lock cancel and re-add it for quota adjust */
329         if (!list_empty(&lqe->lqe_link) &&
330             lqe->lqe_adjust_time == 0) {
331                 list_del_init(&lqe->lqe_link);
332                 lqe_putref(lqe);
333         }
334
335         if (list_empty(&lqe->lqe_link)) {
336                 if (cancel)
337                         lqe->lqe_adjust_time = 0;
338                 else
339                         lqe->lqe_adjust_time = defer ?
340                                 cfs_time_shift_64(QSD_WB_INTERVAL) :
341                                 cfs_time_current_64();
342                 /* lqe reference transferred to list */
343                 if (defer)
344                         list_add_tail(&lqe->lqe_link,
345                                           &qsd->qsd_adjust_list);
346                 else
347                         list_add(&lqe->lqe_link, &qsd->qsd_adjust_list);
348                 added = true;
349         }
350         spin_unlock(&qsd->qsd_adjust_lock);
351
352         if (added)
353                 wake_up(&qsd->qsd_upd_thread.t_ctl_waitq);
354         else
355                 lqe_putref(lqe);
356 }
357
358 /* return true if there is pending writeback records or the pending
359  * adjust requests */
360 static bool qsd_job_pending(struct qsd_instance *qsd, struct list_head *upd,
361                             bool *uptodate)
362 {
363         bool    job_pending = false;
364         int     qtype;
365
366         LASSERT(list_empty(upd));
367         *uptodate = true;
368
369         spin_lock(&qsd->qsd_adjust_lock);
370         if (!list_empty(&qsd->qsd_adjust_list)) {
371                 struct lquota_entry *lqe;
372                 lqe = list_entry(qsd->qsd_adjust_list.next,
373                                      struct lquota_entry, lqe_link);
374                 if (cfs_time_beforeq_64(lqe->lqe_adjust_time,
375                                         cfs_time_current_64()))
376                         job_pending = true;
377         }
378         spin_unlock(&qsd->qsd_adjust_lock);
379
380         write_lock(&qsd->qsd_lock);
381         if (!list_empty(&qsd->qsd_upd_list)) {
382                 list_splice_init(&qsd->qsd_upd_list, upd);
383                 job_pending = true;
384         }
385
386         if (qsd->qsd_acct_failed) {
387                 /* don't bother kicking off reintegration if space accounting
388                  * failed to be enabled */
389                 write_unlock(&qsd->qsd_lock);
390                 return job_pending;
391         }
392
393         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
394                 struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
395
396                 if (!qsd_type_enabled(qsd, qtype))
397                         continue;
398
399                 if ((!qqi->qqi_glb_uptodate || !qqi->qqi_slv_uptodate) &&
400                      !qqi->qqi_reint)
401                         /* global or slave index not up to date and reint
402                          * thread not running */
403                         *uptodate = false;
404         }
405
406         write_unlock(&qsd->qsd_lock);
407         return job_pending;
408 }
409
410 static int qsd_upd_thread(void *arg)
411 {
412         struct qsd_instance     *qsd = (struct qsd_instance *)arg;
413         struct ptlrpc_thread    *thread = &qsd->qsd_upd_thread;
414         struct l_wait_info       lwi;
415         struct list_head         queue;
416         struct qsd_upd_rec      *upd, *n;
417         struct lu_env           *env;
418         int                      qtype, rc = 0;
419         bool                     uptodate;
420         struct lquota_entry     *lqe;
421         __u64                    cur_time;
422         ENTRY;
423
424         OBD_ALLOC_PTR(env);
425         if (env == NULL)
426                 RETURN(-ENOMEM);
427
428         rc = lu_env_init(env, LCT_DT_THREAD);
429         if (rc) {
430                 CERROR("%s: cannot init env: rc = %d\n", qsd->qsd_svname, rc);
431                 OBD_FREE_PTR(env);
432                 RETURN(rc);
433         }
434
435         thread_set_flags(thread, SVC_RUNNING);
436         wake_up(&thread->t_ctl_waitq);
437
438         INIT_LIST_HEAD(&queue);
439         lwi = LWI_TIMEOUT(cfs_time_seconds(QSD_WB_INTERVAL), NULL, NULL);
440         while (1) {
441                 l_wait_event(thread->t_ctl_waitq,
442                              qsd_job_pending(qsd, &queue, &uptodate) ||
443                              !thread_is_running(thread), &lwi);
444
445                 list_for_each_entry_safe(upd, n, &queue, qur_link) {
446                         list_del_init(&upd->qur_link);
447                         qsd_process_upd(env, upd);
448                         qsd_upd_free(upd);
449                 }
450
451                 spin_lock(&qsd->qsd_adjust_lock);
452                 cur_time = cfs_time_current_64();
453                 while (!list_empty(&qsd->qsd_adjust_list)) {
454                         lqe = list_entry(qsd->qsd_adjust_list.next,
455                                          struct lquota_entry, lqe_link);
456                         /* deferred items are sorted by time */
457                         if (!cfs_time_beforeq_64(lqe->lqe_adjust_time,
458                                                  cur_time))
459                                 break;
460
461                         list_del_init(&lqe->lqe_link);
462                         spin_unlock(&qsd->qsd_adjust_lock);
463
464                         if (thread_is_running(thread) && uptodate) {
465                                 qsd_refresh_usage(env, lqe);
466                                 if (lqe->lqe_adjust_time == 0)
467                                         qsd_id_lock_cancel(env, lqe);
468                                 else
469                                         qsd_adjust(env, lqe);
470                         }
471
472                         lqe_putref(lqe);
473                         spin_lock(&qsd->qsd_adjust_lock);
474                 }
475                 spin_unlock(&qsd->qsd_adjust_lock);
476
477                 if (!thread_is_running(thread))
478                         break;
479
480                 if (uptodate)
481                         continue;
482
483                 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++)
484                         qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
485         }
486         lu_env_fini(env);
487         OBD_FREE_PTR(env);
488         thread_set_flags(thread, SVC_STOPPED);
489         wake_up(&thread->t_ctl_waitq);
490         RETURN(rc);
491 }
492
493 int qsd_start_upd_thread(struct qsd_instance *qsd)
494 {
495         struct ptlrpc_thread    *thread = &qsd->qsd_upd_thread;
496         struct l_wait_info       lwi = { 0 };
497         struct task_struct              *task;
498         ENTRY;
499
500         task = kthread_run(qsd_upd_thread, (void *)qsd,
501                            "lquota_wb_%s", qsd->qsd_svname);
502         if (IS_ERR(task)) {
503                 CERROR("fail to start quota update thread: rc = %ld\n",
504                         PTR_ERR(task));
505                 thread_set_flags(thread, SVC_STOPPED);
506                 RETURN(PTR_ERR(task));
507         }
508
509         l_wait_event(thread->t_ctl_waitq,
510                      thread_is_running(thread) || thread_is_stopped(thread),
511                      &lwi);
512         RETURN(0);
513 }
514
515 static void qsd_cleanup_deferred(struct qsd_instance *qsd)
516 {
517         int     qtype;
518
519         for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
520                 struct qsd_upd_rec      *upd, *tmp;
521                 struct qsd_qtype_info   *qqi = qsd->qsd_type_array[qtype];
522
523                 if (qqi == NULL)
524                         continue;
525
526                 write_lock(&qsd->qsd_lock);
527                 list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_glb,
528                                          qur_link) {
529                         CWARN("%s: Free global deferred upd: ID:%llu, "
530                               "ver:%llu/%llu\n", qsd->qsd_svname,
531                               upd->qur_qid.qid_uid, upd->qur_ver,
532                               qqi->qqi_glb_ver);
533                         list_del_init(&upd->qur_link);
534                         qsd_upd_free(upd);
535                 }
536                 list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_slv,
537                                          qur_link) {
538                         CWARN("%s: Free slave deferred upd: ID:%llu, "
539                               "ver:%llu/%llu\n", qsd->qsd_svname,
540                               upd->qur_qid.qid_uid, upd->qur_ver,
541                               qqi->qqi_slv_ver);
542                         list_del_init(&upd->qur_link);
543                         qsd_upd_free(upd);
544                 }
545                 write_unlock(&qsd->qsd_lock);
546         }
547 }
548
549 static void qsd_cleanup_adjust(struct qsd_instance *qsd)
550 {
551         struct lquota_entry     *lqe;
552
553         spin_lock(&qsd->qsd_adjust_lock);
554         while (!list_empty(&qsd->qsd_adjust_list)) {
555                 lqe = list_entry(qsd->qsd_adjust_list.next,
556                                  struct lquota_entry, lqe_link);
557                 list_del_init(&lqe->lqe_link);
558                 lqe_putref(lqe);
559         }
560         spin_unlock(&qsd->qsd_adjust_lock);
561 }
562
563 void qsd_stop_upd_thread(struct qsd_instance *qsd)
564 {
565         struct ptlrpc_thread    *thread = &qsd->qsd_upd_thread;
566         struct l_wait_info       lwi    = { 0 };
567
568         if (!thread_is_stopped(thread)) {
569                 thread_set_flags(thread, SVC_STOPPING);
570                 wake_up(&thread->t_ctl_waitq);
571
572                 l_wait_event(thread->t_ctl_waitq, thread_is_stopped(thread),
573                              &lwi);
574         }
575         qsd_cleanup_deferred(qsd);
576         qsd_cleanup_adjust(qsd);
577 }