4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2012, 2014, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Johann Lombardi <johann.lombardi@intel.com>
28 * Author: Niu Yawei <yawei.niu@intel.com>
31 #define DEBUG_SUBSYSTEM S_LQUOTA
33 #include <linux/kthread.h>
34 #include "qsd_internal.h"
36 extern struct kmem_cache *upd_kmem;
39 * Allocate and fill an qsd_upd_rec structure to be processed by the writeback
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.
50 static struct qsd_upd_rec *qsd_upd_alloc(struct qsd_qtype_info *qqi,
51 struct lquota_entry *lqe,
53 union lquota_rec *rec, __u64 ver,
56 struct qsd_upd_rec *upd;
58 OBD_SLAB_ALLOC_PTR_GFP(upd, upd_kmem, GFP_NOFS);
64 INIT_LIST_HEAD(&upd->qur_link);
72 upd->qur_global = global;
77 static void qsd_upd_free(struct qsd_upd_rec *upd)
80 lqe_putref(upd->qur_lqe);
81 OBD_SLAB_FREE_PTR(upd, upd_kmem);
84 /* must hold the qsd_lock */
85 static void qsd_upd_add(struct qsd_instance *qsd, struct qsd_upd_rec *upd)
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);
92 CWARN("%s: discard update.\n", qsd->qsd_svname);
94 LQUOTA_WARN(upd->qur_lqe, "discard update.");
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)
103 struct qsd_upd_rec *tmp, *n;
105 if (qsd->qsd_stopping) {
106 CWARN("%s: discard deferred udpate.\n", qsd->qsd_svname);
108 LQUOTA_WARN(upd->qur_lqe, "discard deferred update.");
113 /* Sort the updates in ascending order */
114 list_for_each_entry_safe_reverse(tmp, n, list, qur_link) {
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
123 if (upd->qur_ver == tmp->qur_ver) {
125 LQUOTA_WARN(tmp->qur_lqe, "Found a conflict "
126 "record with ver:%llu",
129 CWARN("%s: Found a conflict record with ver: "
130 "%llu\n", qsd->qsd_svname, tmp->qur_ver);
132 list_del_init(&tmp->qur_link);
134 } else if (upd->qur_ver < tmp->qur_ver) {
137 list_add_tail(&upd->qur_link, &tmp->qur_link);
141 list_add(&upd->qur_link, list);
144 /* must hold the qsd_lock */
145 static void qsd_kickoff_deferred(struct qsd_qtype_info *qqi,
146 struct list_head *list, __u64 ver)
148 struct qsd_upd_rec *upd, *tmp;
151 /* Get the first update record in the list, which has the smallest
152 * version, discard all records with versions smaller than the current
154 list_for_each_entry_safe(upd, tmp, list, qur_link) {
155 if (upd->qur_ver <= ver) {
156 /* drop this update */
157 list_del_init(&upd->qur_link);
158 CDEBUG(D_QUOTA, "%s: skipping deferred update ver:"
159 "%llu/%llu, global:%d, qid:%llu\n",
160 qqi->qqi_qsd->qsd_svname, upd->qur_ver, ver,
161 upd->qur_global, upd->qur_qid.qid_uid);
168 /* No remaining deferred update */
169 if (list_empty(list))
172 CDEBUG(D_QUOTA, "%s: found deferred update record. "
173 "version:%llu/%llu, global:%d, qid:%llu\n",
174 qqi->qqi_qsd->qsd_svname, upd->qur_ver, ver,
175 upd->qur_global, upd->qur_qid.qid_uid);
177 LASSERTF(upd->qur_ver > ver, "lur_ver:%llu, cur_ver:%llu\n",
180 /* Kick off the deferred udpate */
181 if (upd->qur_ver == ver + 1) {
182 list_del_init(&upd->qur_link);
183 qsd_upd_add(qqi->qqi_qsd, upd);
188 /* Bump version of global or slave index copy
190 * \param qqi - qsd_qtype_info
191 * \param ver - version to be bumped to
192 * \param global - global or slave index copy?
194 void qsd_bump_version(struct qsd_qtype_info *qqi, __u64 ver, bool global)
196 struct list_head *list;
199 idx_ver = global ? &qqi->qqi_glb_ver : &qqi->qqi_slv_ver;
200 list = global ? &qqi->qqi_deferred_glb : &qqi->qqi_deferred_slv;
202 write_lock(&qqi->qqi_qsd->qsd_lock);
205 qqi->qqi_glb_uptodate = 1;
207 qqi->qqi_slv_uptodate = 1;
208 qsd_kickoff_deferred(qqi, list, ver);
209 write_unlock(&qqi->qqi_qsd->qsd_lock);
213 * Schedule a commit of a lquota entry
215 * \param qqi - qsd_qtype_info
216 * \param lqe - lquota_entry
217 * \param qid - quota id
218 * \param rec - global or slave record to be updated to disk
219 * \param ver - new index file version
220 * \param global- true: master record; false: slave record
222 void qsd_upd_schedule(struct qsd_qtype_info *qqi, struct lquota_entry *lqe,
223 union lquota_id *qid, union lquota_rec *rec, __u64 ver,
226 struct qsd_upd_rec *upd;
227 struct qsd_instance *qsd = qqi->qqi_qsd;
231 CDEBUG(D_QUOTA, "%s: schedule update. global:%s, version:%llu\n",
232 qsd->qsd_svname, global ? "true" : "false", ver);
234 upd = qsd_upd_alloc(qqi, lqe, qid, rec, ver, global);
238 /* If we don't want update index version, no need to sort the
239 * records in version order, just schedule the updates instantly. */
241 write_lock(&qsd->qsd_lock);
242 qsd_upd_add(qsd, upd);
243 write_unlock(&qsd->qsd_lock);
247 write_lock(&qsd->qsd_lock);
249 cur_ver = global ? qqi->qqi_glb_ver : qqi->qqi_slv_ver;
251 if (ver <= cur_ver) {
253 /* legitimate race between glimpse AST and
255 CDEBUG(D_QUOTA, "%s: discarding glb update from glimpse"
256 " ver:%llu local ver:%llu\n",
257 qsd->qsd_svname, ver, cur_ver);
259 CERROR("%s: discard slv update, ver:%llu local ver:"
260 "%llu\n", qsd->qsd_svname, ver, cur_ver);
262 } else if ((ver == cur_ver + 1) && qqi->qqi_glb_uptodate &&
263 qqi->qqi_slv_uptodate) {
264 /* In order update, and reintegration has been done. */
265 qsd_upd_add(qsd, upd);
267 /* Out of order update (the one with smaller version hasn't
268 * reached slave or hasn't been flushed to disk yet), or
269 * the reintegration is in progress. Defer the update. */
270 struct list_head *list = global ? &qqi->qqi_deferred_glb :
271 &qqi->qqi_deferred_slv;
272 qsd_add_deferred(qsd, list, upd);
275 write_unlock(&qsd->qsd_lock);
280 static int qsd_process_upd(const struct lu_env *env, struct qsd_upd_rec *upd)
282 struct lquota_entry *lqe = upd->qur_lqe;
283 struct qsd_qtype_info *qqi = upd->qur_qqi;
288 lqe = lqe_locate(env, qqi->qqi_site, &upd->qur_qid);
290 GOTO(out, rc = PTR_ERR(lqe));
293 /* The in-memory lqe update for slave index copy isn't deferred,
294 * we shouldn't touch it here. */
295 if (upd->qur_global) {
296 rc = qsd_update_lqe(env, lqe, upd->qur_global, &upd->qur_rec);
300 qsd_refresh_usage(env, lqe);
301 /* Report usage asynchronously */
302 rc = qsd_adjust(env, lqe);
304 LQUOTA_ERROR(lqe, "failed to report usage, rc:%d", rc);
307 rc = qsd_update_index(env, qqi, &upd->qur_qid, upd->qur_global,
308 upd->qur_ver, &upd->qur_rec);
310 if (lqe && !IS_ERR(lqe)) {
317 void qsd_adjust_schedule(struct lquota_entry *lqe, bool defer, bool cancel)
319 struct qsd_instance *qsd = lqe2qqi(lqe)->qqi_qsd;
322 read_lock(&qsd->qsd_lock);
323 if (qsd->qsd_stopping) {
324 read_unlock(&qsd->qsd_lock);
327 read_unlock(&qsd->qsd_lock);
330 spin_lock(&qsd->qsd_adjust_lock);
332 /* the lqe is being queued for the per-ID lock cancel, we should
333 * cancel the lock cancel and re-add it for quota adjust */
334 if (!list_empty(&lqe->lqe_link) &&
335 lqe->lqe_adjust_time == 0) {
336 list_del_init(&lqe->lqe_link);
340 if (list_empty(&lqe->lqe_link)) {
342 lqe->lqe_adjust_time = 0;
344 lqe->lqe_adjust_time = defer ?
345 cfs_time_shift_64(QSD_WB_INTERVAL) :
346 cfs_time_current_64();
347 /* lqe reference transferred to list */
349 list_add_tail(&lqe->lqe_link,
350 &qsd->qsd_adjust_list);
352 list_add(&lqe->lqe_link, &qsd->qsd_adjust_list);
355 spin_unlock(&qsd->qsd_adjust_lock);
358 wake_up(&qsd->qsd_upd_thread.t_ctl_waitq);
363 /* return true if there is pending writeback records or the pending
365 static bool qsd_job_pending(struct qsd_instance *qsd, struct list_head *upd,
368 bool job_pending = false;
371 LASSERT(list_empty(upd));
374 spin_lock(&qsd->qsd_adjust_lock);
375 if (!list_empty(&qsd->qsd_adjust_list)) {
376 struct lquota_entry *lqe;
377 lqe = list_entry(qsd->qsd_adjust_list.next,
378 struct lquota_entry, lqe_link);
379 if (cfs_time_beforeq_64(lqe->lqe_adjust_time,
380 cfs_time_current_64()))
383 spin_unlock(&qsd->qsd_adjust_lock);
385 write_lock(&qsd->qsd_lock);
386 if (!list_empty(&qsd->qsd_upd_list)) {
387 list_splice_init(&qsd->qsd_upd_list, upd);
391 if (qsd->qsd_acct_failed) {
392 /* don't bother kicking off reintegration if space accounting
393 * failed to be enabled */
394 write_unlock(&qsd->qsd_lock);
398 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
399 struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
401 if (!qsd_type_enabled(qsd, qtype))
404 if ((!qqi->qqi_glb_uptodate || !qqi->qqi_slv_uptodate) &&
406 /* global or slave index not up to date and reint
407 * thread not running */
411 write_unlock(&qsd->qsd_lock);
415 static int qsd_upd_thread(void *arg)
417 struct qsd_instance *qsd = (struct qsd_instance *)arg;
418 struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
419 struct l_wait_info lwi;
420 struct list_head queue;
421 struct qsd_upd_rec *upd, *n;
425 struct lquota_entry *lqe;
433 rc = lu_env_init(env, LCT_DT_THREAD);
435 CERROR("%s: cannot init env: rc = %d\n", qsd->qsd_svname, rc);
440 thread_set_flags(thread, SVC_RUNNING);
441 wake_up(&thread->t_ctl_waitq);
443 INIT_LIST_HEAD(&queue);
444 lwi = LWI_TIMEOUT(cfs_time_seconds(QSD_WB_INTERVAL), NULL, NULL);
446 l_wait_event(thread->t_ctl_waitq,
447 qsd_job_pending(qsd, &queue, &uptodate) ||
448 !thread_is_running(thread), &lwi);
450 list_for_each_entry_safe(upd, n, &queue, qur_link) {
451 list_del_init(&upd->qur_link);
452 qsd_process_upd(env, upd);
456 spin_lock(&qsd->qsd_adjust_lock);
457 cur_time = cfs_time_current_64();
458 while (!list_empty(&qsd->qsd_adjust_list)) {
459 lqe = list_entry(qsd->qsd_adjust_list.next,
460 struct lquota_entry, lqe_link);
461 /* deferred items are sorted by time */
462 if (!cfs_time_beforeq_64(lqe->lqe_adjust_time,
466 list_del_init(&lqe->lqe_link);
467 spin_unlock(&qsd->qsd_adjust_lock);
469 if (thread_is_running(thread) && uptodate) {
470 qsd_refresh_usage(env, lqe);
471 if (lqe->lqe_adjust_time == 0)
472 qsd_id_lock_cancel(env, lqe);
474 qsd_adjust(env, lqe);
478 spin_lock(&qsd->qsd_adjust_lock);
480 spin_unlock(&qsd->qsd_adjust_lock);
482 if (!thread_is_running(thread))
488 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++)
489 qsd_start_reint_thread(qsd->qsd_type_array[qtype]);
493 thread_set_flags(thread, SVC_STOPPED);
494 wake_up(&thread->t_ctl_waitq);
498 int qsd_start_upd_thread(struct qsd_instance *qsd)
500 struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
501 struct l_wait_info lwi = { 0 };
502 struct task_struct *task;
505 task = kthread_run(qsd_upd_thread, (void *)qsd,
506 "lquota_wb_%s", qsd->qsd_svname);
508 CERROR("fail to start quota update thread: rc = %ld\n",
510 thread_set_flags(thread, SVC_STOPPED);
511 RETURN(PTR_ERR(task));
514 l_wait_event(thread->t_ctl_waitq,
515 thread_is_running(thread) || thread_is_stopped(thread),
520 static void qsd_cleanup_deferred(struct qsd_instance *qsd)
524 for (qtype = USRQUOTA; qtype < LL_MAXQUOTAS; qtype++) {
525 struct qsd_upd_rec *upd, *tmp;
526 struct qsd_qtype_info *qqi = qsd->qsd_type_array[qtype];
531 write_lock(&qsd->qsd_lock);
532 list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_glb,
534 CWARN("%s: Free global deferred upd: ID:%llu, "
535 "ver:%llu/%llu\n", qsd->qsd_svname,
536 upd->qur_qid.qid_uid, upd->qur_ver,
538 list_del_init(&upd->qur_link);
541 list_for_each_entry_safe(upd, tmp, &qqi->qqi_deferred_slv,
543 CWARN("%s: Free slave deferred upd: ID:%llu, "
544 "ver:%llu/%llu\n", qsd->qsd_svname,
545 upd->qur_qid.qid_uid, upd->qur_ver,
547 list_del_init(&upd->qur_link);
550 write_unlock(&qsd->qsd_lock);
554 static void qsd_cleanup_adjust(struct qsd_instance *qsd)
556 struct lquota_entry *lqe;
558 spin_lock(&qsd->qsd_adjust_lock);
559 while (!list_empty(&qsd->qsd_adjust_list)) {
560 lqe = list_entry(qsd->qsd_adjust_list.next,
561 struct lquota_entry, lqe_link);
562 list_del_init(&lqe->lqe_link);
565 spin_unlock(&qsd->qsd_adjust_lock);
568 void qsd_stop_upd_thread(struct qsd_instance *qsd)
570 struct ptlrpc_thread *thread = &qsd->qsd_upd_thread;
571 struct l_wait_info lwi = { 0 };
573 if (!thread_is_stopped(thread)) {
574 thread_set_flags(thread, SVC_STOPPING);
575 wake_up(&thread->t_ctl_waitq);
577 l_wait_event(thread->t_ctl_waitq, thread_is_stopped(thread),
580 qsd_cleanup_deferred(qsd);
581 qsd_cleanup_adjust(qsd);