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, 2016, 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 <lustre_swab.h>
35 #include "qsd_internal.h"
38 * Completion function invoked when the global quota lock enqueue has completed
40 static void qsd_reint_completion(const struct lu_env *env,
41 struct qsd_qtype_info *qqi,
42 struct quota_body *req_qbody,
43 struct quota_body *rep_qbody,
44 struct lustre_handle *lockh,
45 struct lquota_lvb *lvb,
48 struct qsd_instance *qsd = qqi->qqi_qsd;
49 __u64 *slv_ver = (__u64 *)arg;
53 CDEBUG_LIMIT(rc != -EAGAIN ? D_ERROR : D_QUOTA,
54 "%s: failed to enqueue global quota lock, glb fid:"
55 DFID", rc:%d\n", qsd->qsd_svname,
56 PFID(&req_qbody->qb_fid), rc);
60 CDEBUG(D_QUOTA, "%s: global quota lock successfully acquired, glb "
61 "fid:"DFID", glb ver:%llu, slv fid:"DFID", slv ver:%llu\n",
62 qsd->qsd_svname, PFID(&req_qbody->qb_fid),
63 lvb->lvb_glb_ver, PFID(&rep_qbody->qb_slv_fid),
64 rep_qbody->qb_slv_ver);
66 *slv_ver = rep_qbody->qb_slv_ver;
67 memcpy(&qqi->qqi_slv_fid, &rep_qbody->qb_slv_fid,
68 sizeof(struct lu_fid));
69 lustre_handle_copy(&qqi->qqi_lockh, lockh);
73 static int qsd_reint_qid(const struct lu_env *env, struct qsd_qtype_info *qqi,
74 bool global, union lquota_id *qid, void *rec)
76 struct lquota_entry *lqe;
80 lqe = lqe_locate(env, qqi->qqi_site, qid);
84 LQUOTA_DEBUG(lqe, "reintegrating entry");
86 rc = qsd_update_lqe(env, lqe, global, rec);
90 rc = qsd_update_index(env, qqi, qid, global, 0, rec);
96 static int qsd_reint_entries(const struct lu_env *env,
97 struct qsd_qtype_info *qqi,
98 struct idx_info *ii, bool global,
100 unsigned int npages, bool need_swab)
102 struct qsd_thread_info *qti = qsd_info(env);
103 struct qsd_instance *qsd = qqi->qqi_qsd;
104 union lquota_id *qid = &qti->qti_id;
109 CDEBUG(D_QUOTA, "%s: processing %d pages for %s index\n",
110 qsd->qsd_svname, npages, global ? "global" : "slave");
112 /* sanity check on the record size */
113 if ((global && ii->ii_recsize != sizeof(struct lquota_glb_rec)) ||
114 (!global && ii->ii_recsize != sizeof(struct lquota_slv_rec))) {
115 CERROR("%s: invalid record size (%d) for %s index\n",
116 qsd->qsd_svname, ii->ii_recsize,
117 global ? "global" : "slave");
121 size = ii->ii_recsize + ii->ii_keysize;
123 for (i = 0; i < npages; i++) {
124 union lu_page *lip = kmap(pages[i]);
126 for (j = 0; j < LU_PAGE_COUNT; j++) {
129 lustre_swab_lip_header(&lip->lp_idx);
131 if (lip->lp_idx.lip_magic != LIP_MAGIC) {
132 CERROR("%s: invalid magic (%x != %x) for page "
133 "%d/%d while transferring %s index\n",
134 qsd->qsd_svname, lip->lp_idx.lip_magic,
135 LIP_MAGIC, i + 1, npages,
136 global ? "global" : "slave");
137 GOTO(out, rc = -EINVAL);
140 CDEBUG(D_QUOTA, "%s: processing page %d/%d with %d "
141 "entries for %s index\n", qsd->qsd_svname, i + 1,
142 npages, lip->lp_idx.lip_nr,
143 global ? "global" : "slave");
145 for (k = 0; k < lip->lp_idx.lip_nr; k++) {
148 entry = lip->lp_idx.lip_entries + k * size;
149 memcpy(qid, entry, ii->ii_keysize); /* key */
150 entry += ii->ii_keysize; /* value */
156 __swab64s(&qid->qid_uid);
157 /* quota records only include 64-bit
159 while (offset < ii->ii_recsize) {
162 offset += sizeof(__u64);
166 rc = qsd_reint_qid(env, qqi, global, qid,
181 static int qsd_reint_index(const struct lu_env *env, struct qsd_qtype_info *qqi,
184 struct qsd_thread_info *qti = qsd_info(env);
185 struct qsd_instance *qsd = qqi->qqi_qsd;
186 struct idx_info *ii = &qti->qti_ii;
188 struct page **pages = NULL;
189 unsigned int npages, pg_cnt;
190 __u64 start_hash = 0, ver = 0;
191 bool need_swab = false;
195 fid = global ? &qqi->qqi_fid : &qqi->qqi_slv_fid;
197 /* let's do a 1MB bulk */
198 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
201 /* allocate pages for bulk index read */
202 OBD_ALLOC(pages, npages * sizeof(*pages));
204 GOTO(out, rc = -ENOMEM);
205 for (i = 0; i < npages; i++) {
206 pages[i] = alloc_page(GFP_NOFS);
207 if (pages[i] == NULL)
208 GOTO(out, rc = -ENOMEM);
212 /* initialize index_info request with FID of global index */
213 memset(ii, 0, sizeof(*ii));
214 memcpy(&ii->ii_fid, fid, sizeof(*fid));
215 ii->ii_magic = IDX_INFO_MAGIC;
216 ii->ii_flags = II_FL_NOHASH;
217 ii->ii_count = npages * LU_PAGE_COUNT;
218 ii->ii_hash_start = start_hash;
220 /* send bulk request to quota master to read global index */
221 rc = qsd_fetch_index(env, qsd->qsd_exp, ii, npages, pages, &need_swab);
223 CWARN("%s: failed to fetch index for "DFID". %d\n",
224 qsd->qsd_svname, PFID(fid), rc);
228 /* various sanity checks */
229 if (ii->ii_magic != IDX_INFO_MAGIC) {
230 CERROR("%s: invalid magic in index transfer %x != %x\n",
231 qsd->qsd_svname, ii->ii_magic, IDX_INFO_MAGIC);
232 GOTO(out, rc = -EPROTO);
234 if ((ii->ii_flags & II_FL_VARKEY) != 0)
235 CWARN("%s: II_FL_VARKEY is set on index transfer for fid "DFID
236 ", it shouldn't be\n", qsd->qsd_svname, PFID(fid));
237 if ((ii->ii_flags & II_FL_NONUNQ) != 0)
238 CWARN("%s: II_FL_NONUNQ is set on index transfer for fid "DFID
239 ", it shouldn't be\n", qsd->qsd_svname, PFID(fid));
240 if (ii->ii_keysize != sizeof(__u64)) {
241 CERROR("%s: invalid key size reported on index transfer for "
242 "fid "DFID", %u != %u\n", qsd->qsd_svname, PFID(fid),
243 ii->ii_keysize, (int)sizeof(__u64));
244 GOTO(out, rc = -EPROTO);
246 if (ii->ii_version == 0 && ii->ii_count != 0)
247 CWARN("%s: index version for fid "DFID" is 0, but index isn't "
248 "empty (%d)\n", qsd->qsd_svname, PFID(fid), ii->ii_count);
250 CDEBUG(D_QUOTA, "%s: reintegration process for fid "DFID" successfully "
251 "fetched %s index, count = %d\n", qsd->qsd_svname,
252 PFID(fid), global ? "global" : "slave", ii->ii_count);
255 /* record version associated with the first bulk transfer */
256 ver = ii->ii_version;
258 pg_cnt = (ii->ii_count + (LU_PAGE_COUNT) - 1);
259 pg_cnt >>= PAGE_SHIFT - LU_PAGE_SHIFT;
261 if (pg_cnt > npages) {
262 CERROR("%s: master returned more pages than expected, %u > %u"
263 "\n", qsd->qsd_svname, pg_cnt, npages);
267 rc = qsd_reint_entries(env, qqi, ii, global, pages, pg_cnt, need_swab);
271 if (ii->ii_hash_end != II_END_OFF) {
272 start_hash = ii->ii_hash_end;
277 for (i = 0; i < npages; i++)
278 if (pages[i] != NULL)
279 __free_page(pages[i]);
280 OBD_FREE(pages, npages * sizeof(*pages));
283 /* Update index version */
285 rc = qsd_write_version(env, qqi, ver, global);
287 CERROR("%s: write version %llu to "DFID" failed : rc = %d\n",
288 qsd->qsd_svname, ver, PFID(fid), rc);
294 static int qsd_reconciliation(const struct lu_env *env,
295 struct qsd_qtype_info *qqi)
297 struct qsd_thread_info *qti = qsd_info(env);
298 struct qsd_instance *qsd = qqi->qqi_qsd;
299 const struct dt_it_ops *iops;
302 struct lquota_entry *lqe;
303 union lquota_id *qid = &qti->qti_id;
307 LASSERT(qqi->qqi_glb_obj != NULL);
308 iops = &qqi->qqi_glb_obj->do_index_ops->dio_it;
310 it = iops->init(env, qqi->qqi_glb_obj, 0);
312 CWARN("%s: Initialize it for "DFID" failed. %ld\n",
313 qsd->qsd_svname, PFID(&qqi->qqi_fid), PTR_ERR(it));
317 rc = iops->load(env, it, 0);
319 CWARN("%s: Load first entry for "DFID" failed. %d\n",
320 qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
322 } else if (rc == 0) {
323 rc = iops->next(env, it);
325 GOTO(out, rc = (rc < 0) ? rc : 0);
329 key = iops->key(env, it);
331 CWARN("%s: Error key for "DFID". %ld\n",
332 qsd->qsd_svname, PFID(&qqi->qqi_fid),
334 GOTO(out, rc = PTR_ERR(key));
337 /* skip the root user/group */
338 if (*((__u64 *)key) == 0)
341 qid->qid_uid = *((__u64 *)key);
343 lqe = lqe_locate(env, qqi->qqi_site, qid);
345 CWARN("%s: failed to locate lqe. "DFID", %ld\n",
346 qsd->qsd_svname, PFID(&qqi->qqi_fid),
348 GOTO(out, rc = PTR_ERR(lqe));
351 rc = qsd_refresh_usage(env, lqe);
353 CWARN("%s: failed to get usage. "DFID", %d\n",
354 qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
359 rc = qsd_adjust(env, lqe);
362 CWARN("%s: failed to report quota. "DFID", %d\n",
363 qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
367 rc = iops->next(env, it);
369 CWARN("%s: failed to parse index, ->next error:%d "DFID
370 "\n", qsd->qsd_svname, rc, PFID(&qqi->qqi_fid));
382 static int qsd_connected(struct qsd_instance *qsd)
386 read_lock(&qsd->qsd_lock);
387 connected = qsd->qsd_exp_valid ? 1 : 0;
388 read_unlock(&qsd->qsd_lock);
393 static int qsd_started(struct qsd_instance *qsd)
397 read_lock(&qsd->qsd_lock);
398 started = qsd->qsd_started ? 1 : 0;
399 read_unlock(&qsd->qsd_lock);
405 * Routine executed by the reintegration thread.
407 static int qsd_reint_main(void *args)
410 struct qsd_thread_info *qti;
411 struct qsd_qtype_info *qqi = (struct qsd_qtype_info *)args;
412 struct qsd_instance *qsd = qqi->qqi_qsd;
413 struct ptlrpc_thread *thread = &qqi->qqi_reint_thread;
414 struct l_wait_info lwi = { 0 };
418 CDEBUG(D_QUOTA, "%s: Starting reintegration thread for "DFID"\n",
419 qsd->qsd_svname, PFID(&qqi->qqi_fid));
422 lu_ref_add(&qqi->qqi_reference, "reint_thread", thread);
424 thread_set_flags(thread, SVC_RUNNING);
425 wake_up(&thread->t_ctl_waitq);
429 GOTO(out, rc = -ENOMEM);
431 /* initialize environment */
432 rc = lu_env_init(env, LCT_DT_THREAD);
437 /* wait for the connection to master established */
438 l_wait_event(thread->t_ctl_waitq,
439 qsd_connected(qsd) || !thread_is_running(thread), &lwi);
441 /* Step 1: enqueue global index lock */
442 if (!thread_is_running(thread))
443 GOTO(out_env_init, rc = 0);
445 LASSERT(qsd->qsd_exp != NULL);
446 LASSERT(qqi->qqi_glb_uptodate == 0 || qqi->qqi_slv_uptodate == 0);
448 memset(&qti->qti_lvb, 0, sizeof(qti->qti_lvb));
450 read_lock(&qsd->qsd_lock);
451 /* check whether we already own a global quota lock for this type */
452 if (lustre_handle_is_used(&qqi->qqi_lockh) &&
453 ldlm_lock_addref_try(&qqi->qqi_lockh, qsd_glb_einfo.ei_mode) == 0) {
454 read_unlock(&qsd->qsd_lock);
455 /* force refresh of global & slave index copy */
456 qti->qti_lvb.lvb_glb_ver = ~0ULL;
457 qti->qti_slv_ver = ~0ULL;
459 /* no valid lock found, let's enqueue a new one */
460 read_unlock(&qsd->qsd_lock);
462 memset(&qti->qti_body, 0, sizeof(qti->qti_body));
463 memcpy(&qti->qti_body.qb_fid, &qqi->qqi_fid,
464 sizeof(qqi->qqi_fid));
466 rc = qsd_intent_lock(env, qsd->qsd_exp, &qti->qti_body, true,
467 IT_QUOTA_CONN, qsd_reint_completion, qqi,
468 &qti->qti_lvb, (void *)&qti->qti_slv_ver);
470 GOTO(out_env_init, rc);
472 CDEBUG(D_QUOTA, "%s: glb_ver:%llu/%llu,slv_ver:%llu/"
473 "%llu\n", qsd->qsd_svname,
474 qti->qti_lvb.lvb_glb_ver, qqi->qqi_glb_ver,
475 qti->qti_slv_ver, qqi->qqi_slv_ver);
478 /* Step 2: reintegrate global index */
479 if (!thread_is_running(thread))
480 GOTO(out_lock, rc = 0);
482 OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REINT, 10);
484 if (qqi->qqi_glb_ver != qti->qti_lvb.lvb_glb_ver) {
485 rc = qsd_reint_index(env, qqi, true);
487 CWARN("%s: reint global for "DFID" failed. %d\n",
488 qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
492 qsd_bump_version(qqi, qqi->qqi_glb_ver, true);
495 /* Step 3: reintegrate slave index */
496 if (!thread_is_running(thread))
497 GOTO(out_lock, rc = 0);
499 if (qqi->qqi_slv_ver != qti->qti_slv_ver) {
500 rc = qsd_reint_index(env, qqi, false);
502 CWARN("%s: reintegration for "DFID" failed with %d\n",
503 qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
507 qsd_bump_version(qqi, qqi->qqi_slv_ver, false);
510 /* wait for the qsd instance started (target recovery done) */
511 l_wait_event(thread->t_ctl_waitq,
512 qsd_started(qsd) || !thread_is_running(thread), &lwi);
514 if (!thread_is_running(thread))
515 GOTO(out_lock, rc = 0);
517 /* Step 4: start reconciliation for each enforced ID */
518 rc = qsd_reconciliation(env, qqi);
520 CWARN("%s: reconciliation for "DFID" failed with %d\n",
521 qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
525 ldlm_lock_decref(&qqi->qqi_lockh, qsd_glb_einfo.ei_mode);
531 write_lock(&qsd->qsd_lock);
533 write_unlock(&qsd->qsd_lock);
535 thread_set_flags(thread, SVC_STOPPED);
536 wake_up(&thread->t_ctl_waitq);
538 lu_ref_del(&qqi->qqi_reference, "reint_thread", thread);
544 void qsd_stop_reint_thread(struct qsd_qtype_info *qqi)
546 struct ptlrpc_thread *thread = &qqi->qqi_reint_thread;
547 struct l_wait_info lwi = { 0 };
549 if (!thread_is_stopped(thread)) {
550 thread_set_flags(thread, SVC_STOPPING);
551 wake_up(&thread->t_ctl_waitq);
553 l_wait_event(thread->t_ctl_waitq,
554 thread_is_stopped(thread), &lwi);
558 static int qsd_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
559 struct hlist_node *hnode, void *data)
561 struct lquota_entry *lqe;
562 int *pending = (int *)data;
564 lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
565 LASSERT(atomic_read(&lqe->lqe_ref) > 0);
568 *pending += lqe->lqe_pending_req;
569 lqe_read_unlock(lqe);
574 static bool qsd_pending_updates(struct qsd_qtype_info *qqi)
576 struct qsd_instance *qsd = qqi->qqi_qsd;
577 struct qsd_upd_rec *upd;
578 struct lquota_entry *lqe, *n;
580 bool updates = false;
583 /* any pending quota adjust? */
584 spin_lock(&qsd->qsd_adjust_lock);
585 list_for_each_entry_safe(lqe, n, &qsd->qsd_adjust_list, lqe_link) {
586 if (lqe2qqi(lqe) == qqi) {
587 list_del_init(&lqe->lqe_link);
591 spin_unlock(&qsd->qsd_adjust_lock);
593 /* any pending updates? */
594 read_lock(&qsd->qsd_lock);
595 list_for_each_entry(upd, &qsd->qsd_upd_list, qur_link) {
596 if (upd->qur_qqi == qqi) {
597 read_unlock(&qsd->qsd_lock);
598 CDEBUG(D_QUOTA, "%s: pending %s updates for type:%d.\n",
600 upd->qur_global ? "global" : "slave",
602 GOTO(out, updates = true);
605 read_unlock(&qsd->qsd_lock);
607 /* any pending quota request? */
608 cfs_hash_for_each_safe(qqi->qqi_site->lqs_hash, qsd_entry_iter_cb,
611 CDEBUG(D_QUOTA, "%s: pending dqacq for type:%d.\n",
612 qsd->qsd_svname, qqi->qqi_qtype);
618 CERROR("%s: Delaying reintegration for qtype:%d until pending "
619 "updates are flushed.\n",
620 qsd->qsd_svname, qqi->qqi_qtype);
624 int qsd_start_reint_thread(struct qsd_qtype_info *qqi)
626 struct ptlrpc_thread *thread = &qqi->qqi_reint_thread;
627 struct qsd_instance *qsd = qqi->qqi_qsd;
628 struct l_wait_info lwi = { 0 };
629 struct task_struct *task;
634 if (qsd->qsd_dev->dd_rdonly)
637 /* don't bother to do reintegration when quota isn't enabled */
638 if (!qsd_type_enabled(qsd, qqi->qqi_qtype))
641 if (qqi->qqi_acct_failed)
642 /* no space accounting support, can't enable enforcement */
645 /* check if the reintegration has already started or finished */
646 write_lock(&qsd->qsd_lock);
648 if ((qqi->qqi_glb_uptodate && qqi->qqi_slv_uptodate) ||
649 qqi->qqi_reint || qsd->qsd_stopping) {
650 write_unlock(&qsd->qsd_lock);
655 write_unlock(&qsd->qsd_lock);
657 /* there could be some unfinished global or index entry updates
658 * (very unlikely), to avoid them messing up with the reint
659 * procedure, we just return and try to re-start reint later. */
660 if (qsd_pending_updates(qqi)) {
661 write_lock(&qsd->qsd_lock);
663 write_unlock(&qsd->qsd_lock);
667 OBD_ALLOC(name, MTI_NAME_MAXLEN);
671 snprintf(name, MTI_NAME_MAXLEN, "qsd_reint_%d.%s",
672 qqi->qqi_qtype, qsd->qsd_svname);
674 task = kthread_run(qsd_reint_main, qqi, name);
675 OBD_FREE(name, MTI_NAME_MAXLEN);
679 thread_set_flags(thread, SVC_STOPPED);
680 write_lock(&qsd->qsd_lock);
682 write_unlock(&qsd->qsd_lock);
686 l_wait_event(thread->t_ctl_waitq,
687 thread_is_running(thread) || thread_is_stopped(thread),