Whamcloud - gitweb
LU-9052 lod: accept lfs mkdir from old client
[fs/lustre-release.git] / lustre / quota / qsd_reint.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 <linux/kthread.h>
34 #include <lustre_swab.h>
35 #include "qsd_internal.h"
36
37 /*
38  * Completion function invoked when the global quota lock enqueue has completed
39  */
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,
46                                  void *arg, int rc)
47 {
48         struct qsd_instance     *qsd = qqi->qqi_qsd;
49         __u64                   *slv_ver = (__u64 *)arg;
50         ENTRY;
51
52         if (rc) {
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);
57                 RETURN_EXIT;
58         }
59
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);
65
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);
70         EXIT;
71 }
72
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)
75 {
76         struct lquota_entry     *lqe;
77         int                      rc;
78         ENTRY;
79
80         lqe = lqe_locate(env, qqi->qqi_site, qid);
81         if (IS_ERR(lqe))
82                 RETURN(PTR_ERR(lqe));
83
84         LQUOTA_DEBUG(lqe, "reintegrating entry");
85
86         rc = qsd_update_lqe(env, lqe, global, rec);
87         if (rc)
88                 GOTO(out, rc);
89
90         rc = qsd_update_index(env, qqi, qid, global, 0, rec);
91 out:
92         lqe_putref(lqe);
93         RETURN(rc);
94 }
95
96 static int qsd_reint_entries(const struct lu_env *env,
97                              struct qsd_qtype_info *qqi,
98                              struct idx_info *ii, bool global,
99                              struct page **pages,
100                              unsigned int npages, bool need_swab)
101 {
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;
105         int                      i, j, k, size;
106         int                      rc = 0;
107         ENTRY;
108
109         CDEBUG(D_QUOTA, "%s: processing %d pages for %s index\n",
110                qsd->qsd_svname, npages, global ? "global" : "slave");
111
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");
118                 RETURN(-EINVAL);
119         }
120
121         size = ii->ii_recsize + ii->ii_keysize;
122
123         for (i = 0; i < npages; i++) {
124                 union lu_page   *lip = kmap(pages[i]);
125
126                 for (j = 0; j < LU_PAGE_COUNT; j++) {
127                         if (need_swab)
128                                 /* swab header */
129                                 lustre_swab_lip_header(&lip->lp_idx);
130
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);
138                         }
139
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");
144
145                         for (k = 0; k < lip->lp_idx.lip_nr; k++) {
146                                 char *entry;
147
148                                 entry = lip->lp_idx.lip_entries + k * size;
149                                 memcpy(qid, entry, ii->ii_keysize); /* key */
150                                 entry += ii->ii_keysize;            /* value */
151
152                                 if (need_swab) {
153                                         int offset = 0;
154
155                                         /* swab key */
156                                         __swab64s(&qid->qid_uid);
157                                         /* quota records only include 64-bit
158                                          * fields */
159                                         while (offset < ii->ii_recsize) {
160                                                 __swab64s((__u64 *)
161                                                               (entry + offset));
162                                                 offset += sizeof(__u64);
163                                         }
164                                 }
165
166                                 rc = qsd_reint_qid(env, qqi, global, qid,
167                                                    (void *)entry);
168                                 if (rc)
169                                         GOTO(out, rc);
170                         }
171                         lip++;
172                 }
173 out:
174                 kunmap(pages[i]);
175                 if (rc)
176                         break;
177         }
178         RETURN(rc);
179 }
180
181 static int qsd_reint_index(const struct lu_env *env, struct qsd_qtype_info *qqi,
182                            bool global)
183 {
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;
187         struct lu_fid           *fid;
188         struct page             **pages = NULL;
189         unsigned int             npages, pg_cnt;
190         __u64                    start_hash = 0, ver = 0;
191         bool                     need_swab = false;
192         int                      i, rc;
193         ENTRY;
194
195         fid = global ? &qqi->qqi_fid : &qqi->qqi_slv_fid;
196
197         /* let's do a 1MB bulk */
198         npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
199         npages /= PAGE_SIZE;
200
201         /* allocate pages for bulk index read */
202         OBD_ALLOC(pages, npages * sizeof(*pages));
203         if (pages == NULL)
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);
209         }
210
211 repeat:
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;
219
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);
222         if (rc) {
223                 CWARN("%s: failed to fetch index for "DFID". %d\n",
224                       qsd->qsd_svname, PFID(fid), rc);
225                 GOTO(out, rc);
226         }
227
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);
233         }
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);
245         }
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);
249
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);
253
254         if (start_hash == 0)
255                 /* record version associated with the first bulk transfer */
256                 ver = ii->ii_version;
257
258         pg_cnt = (ii->ii_count + (LU_PAGE_COUNT) - 1);
259         pg_cnt >>= PAGE_SHIFT - LU_PAGE_SHIFT;
260
261         if (pg_cnt > npages) {
262                 CERROR("%s: master returned more pages than expected, %u > %u"
263                        "\n", qsd->qsd_svname, pg_cnt, npages);
264                 pg_cnt = npages;
265         }
266
267         rc = qsd_reint_entries(env, qqi, ii, global, pages, pg_cnt, need_swab);
268         if (rc)
269                 GOTO(out, rc);
270
271         if (ii->ii_hash_end != II_END_OFF) {
272                 start_hash = ii->ii_hash_end;
273                 goto repeat;
274         }
275 out:
276         if (pages != NULL) {
277                 for (i = 0; i < npages; i++)
278                         if (pages[i] != NULL)
279                                 __free_page(pages[i]);
280                 OBD_FREE(pages, npages * sizeof(*pages));
281         }
282
283         /* Update index version */
284         if (rc == 0) {
285                 rc = qsd_write_version(env, qqi, ver, global);
286                 if (rc)
287                         CERROR("%s: write version %llu to "DFID" failed : rc = %d\n",
288                                qsd->qsd_svname, ver, PFID(fid), rc);
289         }
290
291         RETURN(rc);
292 }
293
294 static int qsd_reconciliation(const struct lu_env *env,
295                               struct qsd_qtype_info *qqi)
296 {
297         struct qsd_thread_info  *qti = qsd_info(env);
298         struct qsd_instance     *qsd = qqi->qqi_qsd;
299         const struct dt_it_ops  *iops;
300         struct dt_it            *it;
301         struct dt_key           *key;
302         struct lquota_entry     *lqe;
303         union lquota_id         *qid = &qti->qti_id;
304         int                      rc;
305         ENTRY;
306
307         LASSERT(qqi->qqi_glb_obj != NULL);
308         iops = &qqi->qqi_glb_obj->do_index_ops->dio_it;
309
310         it = iops->init(env, qqi->qqi_glb_obj, 0);
311         if (IS_ERR(it)) {
312                 CWARN("%s: Initialize it for "DFID" failed. %ld\n",
313                       qsd->qsd_svname, PFID(&qqi->qqi_fid), PTR_ERR(it));
314                 RETURN(PTR_ERR(it));
315         }
316
317         rc = iops->load(env, it, 0);
318         if (rc < 0) {
319                 CWARN("%s: Load first entry for "DFID" failed. %d\n",
320                       qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
321                 GOTO(out, rc);
322         } else if (rc == 0) {
323                 rc = iops->next(env, it);
324                 if (rc != 0)
325                         GOTO(out, rc = (rc < 0) ? rc : 0);
326         }
327
328         do {
329                 key = iops->key(env, it);
330                 if (IS_ERR(key)) {
331                         CWARN("%s: Error key for "DFID". %ld\n",
332                               qsd->qsd_svname, PFID(&qqi->qqi_fid),
333                               PTR_ERR(key));
334                         GOTO(out, rc = PTR_ERR(key));
335                 }
336
337                 /* skip the root user/group */
338                 if (*((__u64 *)key) == 0)
339                         goto next;
340
341                 qid->qid_uid = *((__u64 *)key);
342
343                 lqe = lqe_locate(env, qqi->qqi_site, qid);
344                 if (IS_ERR(lqe)) {
345                         CWARN("%s: failed to locate lqe. "DFID", %ld\n",
346                               qsd->qsd_svname, PFID(&qqi->qqi_fid),
347                               PTR_ERR(lqe));
348                         GOTO(out, rc = PTR_ERR(lqe));
349                 }
350
351                 rc = qsd_refresh_usage(env, lqe);
352                 if (rc) {
353                         CWARN("%s: failed to get usage. "DFID", %d\n",
354                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
355                         lqe_putref(lqe);
356                         GOTO(out, rc);
357                 }
358
359                 rc = qsd_adjust(env, lqe);
360                 lqe_putref(lqe);
361                 if (rc) {
362                         CWARN("%s: failed to report quota. "DFID", %d\n",
363                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
364                         GOTO(out, rc);
365                 }
366 next:
367                 rc = iops->next(env, it);
368                 if (rc < 0)
369                         CWARN("%s: failed to parse index, ->next error:%d "DFID
370                               "\n", qsd->qsd_svname, rc, PFID(&qqi->qqi_fid));
371         } while (rc == 0);
372
373         /* reach the end */
374         if (rc > 0)
375                 rc = 0;
376 out:
377         iops->put(env, it);
378         iops->fini(env, it);
379         RETURN(rc);
380 }
381
382 static int qsd_connected(struct qsd_instance *qsd)
383 {
384         int     connected;
385
386         read_lock(&qsd->qsd_lock);
387         connected = qsd->qsd_exp_valid ? 1 : 0;
388         read_unlock(&qsd->qsd_lock);
389
390         return connected;
391 }
392
393 static int qsd_started(struct qsd_instance *qsd)
394 {
395         int     started;
396
397         read_lock(&qsd->qsd_lock);
398         started = qsd->qsd_started ? 1 : 0;
399         read_unlock(&qsd->qsd_lock);
400
401         return started;
402 }
403
404 /*
405  * Routine executed by the reintegration thread.
406  */
407 static int qsd_reint_main(void *args)
408 {
409         struct lu_env           *env;
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 };
415         int                      rc;
416         ENTRY;
417
418         CDEBUG(D_QUOTA, "%s: Starting reintegration thread for "DFID"\n",
419                qsd->qsd_svname, PFID(&qqi->qqi_fid));
420
421         qqi_getref(qqi);
422         lu_ref_add(&qqi->qqi_reference, "reint_thread", thread);
423
424         thread_set_flags(thread, SVC_RUNNING);
425         wake_up(&thread->t_ctl_waitq);
426
427         OBD_ALLOC_PTR(env);
428         if (env == NULL)
429                 GOTO(out, rc = -ENOMEM);
430
431         /* initialize environment */
432         rc = lu_env_init(env, LCT_DT_THREAD);
433         if (rc)
434                 GOTO(out_env, rc);
435         qti = qsd_info(env);
436
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);
440
441         /* Step 1: enqueue global index lock */
442         if (!thread_is_running(thread))
443                 GOTO(out_env_init, rc = 0);
444
445         LASSERT(qsd->qsd_exp != NULL);
446         LASSERT(qqi->qqi_glb_uptodate == 0 || qqi->qqi_slv_uptodate == 0);
447
448         memset(&qti->qti_lvb, 0, sizeof(qti->qti_lvb));
449
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;
458         } else {
459                 /* no valid lock found, let's enqueue a new one */
460                 read_unlock(&qsd->qsd_lock);
461
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));
465
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);
469                 if (rc)
470                         GOTO(out_env_init, rc);
471
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);
476         }
477
478         /* Step 2: reintegrate global index */
479         if (!thread_is_running(thread))
480                 GOTO(out_lock, rc = 0);
481
482         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REINT, 10);
483
484         if (qqi->qqi_glb_ver != qti->qti_lvb.lvb_glb_ver) {
485                 rc = qsd_reint_index(env, qqi, true);
486                 if (rc) {
487                         CWARN("%s: reint global for "DFID" failed. %d\n",
488                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
489                         GOTO(out_lock, rc);
490                 }
491         } else {
492                 qsd_bump_version(qqi, qqi->qqi_glb_ver, true);
493         }
494
495         /* Step 3: reintegrate slave index */
496         if (!thread_is_running(thread))
497                 GOTO(out_lock, rc = 0);
498
499         if (qqi->qqi_slv_ver != qti->qti_slv_ver) {
500                 rc = qsd_reint_index(env, qqi, false);
501                 if (rc) {
502                         CWARN("%s: reintegration for "DFID" failed with %d\n",
503                               qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
504                         GOTO(out_lock, rc);
505                 }
506         } else {
507                 qsd_bump_version(qqi, qqi->qqi_slv_ver, false);
508         }
509
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);
513
514         if (!thread_is_running(thread))
515                 GOTO(out_lock, rc = 0);
516
517         /* Step 4: start reconciliation for each enforced ID */
518         rc = qsd_reconciliation(env, qqi);
519         if (rc)
520                 CWARN("%s: reconciliation for "DFID" failed with %d\n",
521                       qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
522
523         EXIT;
524 out_lock:
525         ldlm_lock_decref(&qqi->qqi_lockh, qsd_glb_einfo.ei_mode);
526 out_env_init:
527         lu_env_fini(env);
528 out_env:
529         OBD_FREE_PTR(env);
530 out:
531         write_lock(&qsd->qsd_lock);
532         qqi->qqi_reint = 0;
533         write_unlock(&qsd->qsd_lock);
534
535         thread_set_flags(thread, SVC_STOPPED);
536         wake_up(&thread->t_ctl_waitq);
537
538         lu_ref_del(&qqi->qqi_reference, "reint_thread", thread);
539         qqi_putref(qqi);
540
541         return rc;
542 }
543
544 void qsd_stop_reint_thread(struct qsd_qtype_info *qqi)
545 {
546         struct ptlrpc_thread    *thread = &qqi->qqi_reint_thread;
547         struct l_wait_info       lwi = { 0 };
548
549         if (!thread_is_stopped(thread)) {
550                 thread_set_flags(thread, SVC_STOPPING);
551                 wake_up(&thread->t_ctl_waitq);
552
553                 l_wait_event(thread->t_ctl_waitq,
554                              thread_is_stopped(thread), &lwi);
555         }
556 }
557
558 static int qsd_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
559                              struct hlist_node *hnode, void *data)
560 {
561         struct lquota_entry     *lqe;
562         int                     *pending = (int *)data;
563
564         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
565         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
566
567         lqe_read_lock(lqe);
568         *pending += lqe->lqe_pending_req;
569         lqe_read_unlock(lqe);
570
571         return 0;
572 }
573
574 static bool qsd_pending_updates(struct qsd_qtype_info *qqi)
575 {
576         struct qsd_instance     *qsd = qqi->qqi_qsd;
577         struct qsd_upd_rec      *upd;
578         struct lquota_entry     *lqe, *n;
579         int                      dqacq = 0;
580         bool                     updates = false;
581         ENTRY;
582
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);
588                         lqe_putref(lqe);
589                 }
590         }
591         spin_unlock(&qsd->qsd_adjust_lock);
592
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",
599                                qsd->qsd_svname,
600                                upd->qur_global ? "global" : "slave",
601                                qqi->qqi_qtype);
602                         GOTO(out, updates = true);
603                 }
604         }
605         read_unlock(&qsd->qsd_lock);
606
607         /* any pending quota request? */
608         cfs_hash_for_each_safe(qqi->qqi_site->lqs_hash, qsd_entry_iter_cb,
609                                &dqacq);
610         if (dqacq) {
611                 CDEBUG(D_QUOTA, "%s: pending dqacq for type:%d.\n",
612                        qsd->qsd_svname, qqi->qqi_qtype);
613                 updates = true;
614         }
615         EXIT;
616 out:
617         if (updates)
618                 CERROR("%s: Delaying reintegration for qtype:%d until pending "
619                        "updates are flushed.\n",
620                        qsd->qsd_svname, qqi->qqi_qtype);
621         return updates;
622 }
623
624 int qsd_start_reint_thread(struct qsd_qtype_info *qqi)
625 {
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;
630         int                      rc;
631         char                    *name;
632         ENTRY;
633
634         if (qsd->qsd_dev->dd_rdonly)
635                 RETURN(0);
636
637         /* don't bother to do reintegration when quota isn't enabled */
638         if (!qsd_type_enabled(qsd, qqi->qqi_qtype))
639                 RETURN(0);
640
641         if (qqi->qqi_acct_failed)
642                 /* no space accounting support, can't enable enforcement */
643                 RETURN(0);
644
645         /* check if the reintegration has already started or finished */
646         write_lock(&qsd->qsd_lock);
647
648         if ((qqi->qqi_glb_uptodate && qqi->qqi_slv_uptodate) ||
649              qqi->qqi_reint || qsd->qsd_stopping) {
650                 write_unlock(&qsd->qsd_lock);
651                 RETURN(0);
652         }
653         qqi->qqi_reint = 1;
654
655         write_unlock(&qsd->qsd_lock);
656
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);
662                 qqi->qqi_reint = 0;
663                 write_unlock(&qsd->qsd_lock);
664                 RETURN(0);
665         }
666
667         OBD_ALLOC(name, MTI_NAME_MAXLEN);
668         if (name == NULL)
669                 RETURN(-ENOMEM);
670
671         snprintf(name, MTI_NAME_MAXLEN, "qsd_reint_%d.%s",
672                  qqi->qqi_qtype, qsd->qsd_svname);
673
674         task = kthread_run(qsd_reint_main, qqi, name);
675         OBD_FREE(name, MTI_NAME_MAXLEN);
676
677         if (IS_ERR(task)) {
678                 rc = PTR_ERR(task);
679                 thread_set_flags(thread, SVC_STOPPED);
680                 write_lock(&qsd->qsd_lock);
681                 qqi->qqi_reint = 0;
682                 write_unlock(&qsd->qsd_lock);
683                 RETURN(rc);
684         }
685
686         l_wait_event(thread->t_ctl_waitq,
687                      thread_is_running(thread) || thread_is_stopped(thread),
688                      &lwi);
689         RETURN(0);
690 }