Whamcloud - gitweb
LU-12018 quota: do not start a thread under memory pressure
[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, 2017, 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
93         if (global && qid->qid_uid == 0) {
94                 struct lquota_glb_rec *glb_rec = (struct lquota_glb_rec *)rec;
95                 qsd_update_default_quota(qqi, glb_rec->qbr_hardlimit,
96                                          glb_rec->qbr_softlimit,
97                                          glb_rec->qbr_time);
98         }
99
100         lqe_putref(lqe);
101         RETURN(rc);
102 }
103
104 static int qsd_reint_entries(const struct lu_env *env,
105                              struct qsd_qtype_info *qqi,
106                              struct idx_info *ii, bool global,
107                              struct page **pages,
108                              unsigned int npages, bool need_swab)
109 {
110         struct qsd_thread_info  *qti = qsd_info(env);
111         struct qsd_instance     *qsd = qqi->qqi_qsd;
112         union lquota_id         *qid = &qti->qti_id;
113         int                      i, j, k, size;
114         int                      rc = 0;
115         ENTRY;
116
117         CDEBUG(D_QUOTA, "%s: processing %d pages for %s index\n",
118                qsd->qsd_svname, npages, global ? "global" : "slave");
119
120         /* sanity check on the record size */
121         if ((global && ii->ii_recsize != sizeof(struct lquota_glb_rec)) ||
122             (!global && ii->ii_recsize != sizeof(struct lquota_slv_rec))) {
123                 CERROR("%s: invalid record size (%d) for %s index\n",
124                        qsd->qsd_svname, ii->ii_recsize,
125                        global ? "global" : "slave");
126                 RETURN(-EINVAL);
127         }
128
129         size = ii->ii_recsize + ii->ii_keysize;
130
131         for (i = 0; i < npages; i++) {
132                 union lu_page   *lip = kmap(pages[i]);
133
134                 for (j = 0; j < LU_PAGE_COUNT; j++) {
135                         if (need_swab)
136                                 /* swab header */
137                                 lustre_swab_lip_header(&lip->lp_idx);
138
139                         if (lip->lp_idx.lip_magic != LIP_MAGIC) {
140                                 CERROR("%s: invalid magic (%x != %x) for page "
141                                        "%d/%d while transferring %s index\n",
142                                        qsd->qsd_svname, lip->lp_idx.lip_magic,
143                                        LIP_MAGIC, i + 1, npages,
144                                        global ? "global" : "slave");
145                                 GOTO(out, rc = -EINVAL);
146                         }
147
148                         CDEBUG(D_QUOTA, "%s: processing page %d/%d with %d "
149                                "entries for %s index\n", qsd->qsd_svname, i + 1,
150                                npages, lip->lp_idx.lip_nr,
151                                global ? "global" : "slave");
152
153                         for (k = 0; k < lip->lp_idx.lip_nr; k++) {
154                                 char *entry;
155
156                                 entry = lip->lp_idx.lip_entries + k * size;
157                                 memcpy(qid, entry, ii->ii_keysize); /* key */
158                                 entry += ii->ii_keysize;            /* value */
159
160                                 if (need_swab) {
161                                         int offset = 0;
162
163                                         /* swab key */
164                                         __swab64s(&qid->qid_uid);
165                                         /* quota records only include 64-bit
166                                          * fields */
167                                         while (offset < ii->ii_recsize) {
168                                                 __swab64s((__u64 *)
169                                                               (entry + offset));
170                                                 offset += sizeof(__u64);
171                                         }
172                                 }
173
174                                 rc = qsd_reint_qid(env, qqi, global, qid,
175                                                    (void *)entry);
176                                 if (rc)
177                                         GOTO(out, rc);
178                         }
179                         lip++;
180                 }
181 out:
182                 kunmap(pages[i]);
183                 if (rc)
184                         break;
185         }
186         RETURN(rc);
187 }
188
189 static int qsd_reint_index(const struct lu_env *env, struct qsd_qtype_info *qqi,
190                            bool global)
191 {
192         struct qsd_thread_info  *qti = qsd_info(env);
193         struct qsd_instance     *qsd = qqi->qqi_qsd;
194         struct idx_info         *ii = &qti->qti_ii;
195         struct lu_fid           *fid;
196         struct page             **pages = NULL;
197         unsigned int             npages, pg_cnt;
198         __u64                    start_hash = 0, ver = 0;
199         bool                     need_swab = false;
200         int                      i, rc;
201         ENTRY;
202
203         fid = global ? &qqi->qqi_fid : &qqi->qqi_slv_fid;
204
205         /* let's do a 1MB bulk */
206         npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
207         npages /= PAGE_SIZE;
208
209         /* allocate pages for bulk index read */
210         OBD_ALLOC(pages, npages * sizeof(*pages));
211         if (pages == NULL)
212                 GOTO(out, rc = -ENOMEM);
213         for (i = 0; i < npages; i++) {
214                 pages[i] = alloc_page(GFP_NOFS);
215                 if (pages[i] == NULL)
216                         GOTO(out, rc = -ENOMEM);
217         }
218
219 repeat:
220         /* initialize index_info request with FID of global index */
221         memset(ii, 0, sizeof(*ii));
222         memcpy(&ii->ii_fid, fid, sizeof(*fid));
223         ii->ii_magic = IDX_INFO_MAGIC;
224         ii->ii_flags = II_FL_NOHASH;
225         ii->ii_count = npages * LU_PAGE_COUNT;
226         ii->ii_hash_start = start_hash;
227
228         /* send bulk request to quota master to read global index */
229         rc = qsd_fetch_index(env, qsd->qsd_exp, ii, npages, pages, &need_swab);
230         if (rc) {
231                 CWARN("%s: failed to fetch index for "DFID". %d\n",
232                       qsd->qsd_svname, PFID(fid), rc);
233                 GOTO(out, rc);
234         }
235
236         /* various sanity checks */
237         if (ii->ii_magic != IDX_INFO_MAGIC) {
238                 CERROR("%s: invalid magic in index transfer %x != %x\n",
239                        qsd->qsd_svname, ii->ii_magic, IDX_INFO_MAGIC);
240                 GOTO(out, rc = -EPROTO);
241         }
242         if ((ii->ii_flags & II_FL_VARKEY) != 0)
243                 CWARN("%s: II_FL_VARKEY is set on index transfer for fid "DFID
244                       ", it shouldn't be\n", qsd->qsd_svname, PFID(fid));
245         if ((ii->ii_flags & II_FL_NONUNQ) != 0)
246                 CWARN("%s: II_FL_NONUNQ is set on index transfer for fid "DFID
247                       ", it shouldn't be\n", qsd->qsd_svname, PFID(fid));
248         if (ii->ii_keysize != sizeof(__u64)) {
249                 CERROR("%s: invalid key size reported on index transfer for "
250                        "fid "DFID", %u != %u\n", qsd->qsd_svname, PFID(fid),
251                        ii->ii_keysize, (int)sizeof(__u64));
252                 GOTO(out, rc = -EPROTO);
253         }
254         if (ii->ii_version == 0 && ii->ii_count != 0)
255                 CWARN("%s: index version for fid "DFID" is 0, but index isn't "
256                       "empty (%d)\n", qsd->qsd_svname, PFID(fid), ii->ii_count);
257
258         CDEBUG(D_QUOTA, "%s: reintegration process for fid "DFID" successfully "
259                "fetched %s index, count = %d\n", qsd->qsd_svname,
260                PFID(fid), global ? "global" : "slave", ii->ii_count);
261
262         if (start_hash == 0)
263                 /* record version associated with the first bulk transfer */
264                 ver = ii->ii_version;
265
266         pg_cnt = (ii->ii_count + (LU_PAGE_COUNT) - 1);
267         pg_cnt >>= PAGE_SHIFT - LU_PAGE_SHIFT;
268
269         if (pg_cnt > npages) {
270                 CERROR("%s: master returned more pages than expected, %u > %u"
271                        "\n", qsd->qsd_svname, pg_cnt, npages);
272                 pg_cnt = npages;
273         }
274
275         rc = qsd_reint_entries(env, qqi, ii, global, pages, pg_cnt, need_swab);
276         if (rc)
277                 GOTO(out, rc);
278
279         if (ii->ii_hash_end != II_END_OFF) {
280                 start_hash = ii->ii_hash_end;
281                 goto repeat;
282         }
283 out:
284         if (pages != NULL) {
285                 for (i = 0; i < npages; i++)
286                         if (pages[i] != NULL)
287                                 __free_page(pages[i]);
288                 OBD_FREE(pages, npages * sizeof(*pages));
289         }
290
291         /* Update index version */
292         if (rc == 0) {
293                 rc = qsd_write_version(env, qqi, ver, global);
294                 if (rc)
295                         CERROR("%s: write version %llu to "DFID" failed : rc = %d\n",
296                                qsd->qsd_svname, ver, PFID(fid), rc);
297         }
298
299         RETURN(rc);
300 }
301
302 static int qsd_reconciliation(const struct lu_env *env,
303                               struct qsd_qtype_info *qqi)
304 {
305         struct qsd_thread_info  *qti = qsd_info(env);
306         struct qsd_instance     *qsd = qqi->qqi_qsd;
307         const struct dt_it_ops  *iops;
308         struct dt_it            *it;
309         struct dt_key           *key;
310         struct lquota_entry     *lqe;
311         union lquota_id         *qid = &qti->qti_id;
312         int                      rc;
313         ENTRY;
314
315         LASSERT(qqi->qqi_glb_obj != NULL);
316         iops = &qqi->qqi_glb_obj->do_index_ops->dio_it;
317
318         it = iops->init(env, qqi->qqi_glb_obj, 0);
319         if (IS_ERR(it)) {
320                 CWARN("%s: Initialize it for "DFID" failed. %ld\n",
321                       qsd->qsd_svname, PFID(&qqi->qqi_fid), PTR_ERR(it));
322                 RETURN(PTR_ERR(it));
323         }
324
325         rc = iops->load(env, it, 0);
326         if (rc < 0) {
327                 CWARN("%s: Load first entry for "DFID" failed. %d\n",
328                       qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
329                 GOTO(out, rc);
330         } else if (rc == 0) {
331                 rc = iops->next(env, it);
332                 if (rc != 0)
333                         GOTO(out, rc = (rc < 0) ? rc : 0);
334         }
335
336         do {
337                 key = iops->key(env, it);
338                 if (IS_ERR(key)) {
339                         CWARN("%s: Error key for "DFID". %ld\n",
340                               qsd->qsd_svname, PFID(&qqi->qqi_fid),
341                               PTR_ERR(key));
342                         GOTO(out, rc = PTR_ERR(key));
343                 }
344
345                 /* skip the root user/group */
346                 if (*((__u64 *)key) == 0)
347                         goto next;
348
349                 qid->qid_uid = *((__u64 *)key);
350
351                 lqe = lqe_locate(env, qqi->qqi_site, qid);
352                 if (IS_ERR(lqe)) {
353                         CWARN("%s: failed to locate lqe. "DFID", %ld\n",
354                               qsd->qsd_svname, PFID(&qqi->qqi_fid),
355                               PTR_ERR(lqe));
356                         GOTO(out, rc = PTR_ERR(lqe));
357                 }
358
359                 rc = qsd_refresh_usage(env, lqe);
360                 if (rc) {
361                         CWARN("%s: failed to get usage. "DFID", %d\n",
362                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
363                         lqe_putref(lqe);
364                         GOTO(out, rc);
365                 }
366
367                 rc = qsd_adjust(env, lqe);
368                 lqe_putref(lqe);
369                 if (rc) {
370                         CWARN("%s: failed to report quota. "DFID", %d\n",
371                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
372                         GOTO(out, rc);
373                 }
374 next:
375                 rc = iops->next(env, it);
376                 if (rc < 0)
377                         CWARN("%s: failed to parse index, ->next error:%d "DFID
378                               "\n", qsd->qsd_svname, rc, PFID(&qqi->qqi_fid));
379         } while (rc == 0);
380
381         /* reach the end */
382         if (rc > 0)
383                 rc = 0;
384 out:
385         iops->put(env, it);
386         iops->fini(env, it);
387         RETURN(rc);
388 }
389
390 static int qsd_connected(struct qsd_instance *qsd)
391 {
392         int     connected;
393
394         read_lock(&qsd->qsd_lock);
395         connected = qsd->qsd_exp_valid ? 1 : 0;
396         read_unlock(&qsd->qsd_lock);
397
398         return connected;
399 }
400
401 static int qsd_started(struct qsd_instance *qsd)
402 {
403         int     started;
404
405         read_lock(&qsd->qsd_lock);
406         started = qsd->qsd_started ? 1 : 0;
407         read_unlock(&qsd->qsd_lock);
408
409         return started;
410 }
411
412 /*
413  * Routine executed by the reintegration thread.
414  */
415 static int qsd_reint_main(void *args)
416 {
417         struct lu_env           *env;
418         struct qsd_thread_info  *qti;
419         struct qsd_qtype_info   *qqi = (struct qsd_qtype_info *)args;
420         struct qsd_instance     *qsd = qqi->qqi_qsd;
421         struct ptlrpc_thread    *thread = &qqi->qqi_reint_thread;
422         struct l_wait_info       lwi = { 0 };
423         int                      rc;
424         ENTRY;
425
426         CDEBUG(D_QUOTA, "%s: Starting reintegration thread for "DFID"\n",
427                qsd->qsd_svname, PFID(&qqi->qqi_fid));
428
429         qqi_getref(qqi);
430         lu_ref_add(&qqi->qqi_reference, "reint_thread", thread);
431
432         thread_set_flags(thread, SVC_RUNNING);
433         wake_up(&thread->t_ctl_waitq);
434
435         OBD_ALLOC_PTR(env);
436         if (env == NULL)
437                 GOTO(out, rc = -ENOMEM);
438
439         /* initialize environment */
440         rc = lu_env_init(env, LCT_DT_THREAD);
441         if (rc)
442                 GOTO(out_env, rc);
443         qti = qsd_info(env);
444
445         /* wait for the connection to master established */
446         l_wait_event(thread->t_ctl_waitq,
447                      qsd_connected(qsd) || !thread_is_running(thread), &lwi);
448
449         /* Step 1: enqueue global index lock */
450         if (!thread_is_running(thread))
451                 GOTO(out_env_init, rc = 0);
452
453         LASSERT(qsd->qsd_exp != NULL);
454         LASSERT(qqi->qqi_glb_uptodate == 0 || qqi->qqi_slv_uptodate == 0);
455
456         memset(&qti->qti_lvb, 0, sizeof(qti->qti_lvb));
457
458         read_lock(&qsd->qsd_lock);
459         /* check whether we already own a global quota lock for this type */
460         if (lustre_handle_is_used(&qqi->qqi_lockh) &&
461             ldlm_lock_addref_try(&qqi->qqi_lockh, qsd_glb_einfo.ei_mode) == 0) {
462                 read_unlock(&qsd->qsd_lock);
463                 /* force refresh of global & slave index copy */
464                 qti->qti_lvb.lvb_glb_ver = ~0ULL;
465                 qti->qti_slv_ver = ~0ULL;
466         } else {
467                 /* no valid lock found, let's enqueue a new one */
468                 read_unlock(&qsd->qsd_lock);
469
470                 memset(&qti->qti_body, 0, sizeof(qti->qti_body));
471                 memcpy(&qti->qti_body.qb_fid, &qqi->qqi_fid,
472                        sizeof(qqi->qqi_fid));
473
474                 rc = qsd_intent_lock(env, qsd->qsd_exp, &qti->qti_body, true,
475                                      IT_QUOTA_CONN, qsd_reint_completion, qqi,
476                                      &qti->qti_lvb, (void *)&qti->qti_slv_ver);
477                 if (rc)
478                         GOTO(out_env_init, rc);
479
480                 CDEBUG(D_QUOTA, "%s: glb_ver:%llu/%llu,slv_ver:%llu/"
481                        "%llu\n", qsd->qsd_svname,
482                        qti->qti_lvb.lvb_glb_ver, qqi->qqi_glb_ver,
483                        qti->qti_slv_ver, qqi->qqi_slv_ver);
484         }
485
486         /* Step 2: reintegrate global index */
487         if (!thread_is_running(thread))
488                 GOTO(out_lock, rc = 0);
489
490         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REINT, 10);
491
492         if (qqi->qqi_glb_ver != qti->qti_lvb.lvb_glb_ver) {
493                 rc = qsd_reint_index(env, qqi, true);
494                 if (rc) {
495                         CWARN("%s: reint global for "DFID" failed. %d\n",
496                               qsd->qsd_svname, PFID(&qqi->qqi_fid), rc);
497                         GOTO(out_lock, rc);
498                 }
499         } else {
500                 qsd_bump_version(qqi, qqi->qqi_glb_ver, true);
501         }
502
503         /* Step 3: reintegrate slave index */
504         if (!thread_is_running(thread))
505                 GOTO(out_lock, rc = 0);
506
507         if (qqi->qqi_slv_ver != qti->qti_slv_ver) {
508                 rc = qsd_reint_index(env, qqi, false);
509                 if (rc) {
510                         CWARN("%s: reintegration for "DFID" failed with %d\n",
511                               qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
512                         GOTO(out_lock, rc);
513                 }
514         } else {
515                 qsd_bump_version(qqi, qqi->qqi_slv_ver, false);
516         }
517
518         /* wait for the qsd instance started (target recovery done) */
519         l_wait_event(thread->t_ctl_waitq,
520                      qsd_started(qsd) || !thread_is_running(thread), &lwi);
521
522         if (!thread_is_running(thread))
523                 GOTO(out_lock, rc = 0);
524
525         /* Step 4: start reconciliation for each enforced ID */
526         rc = qsd_reconciliation(env, qqi);
527         if (rc)
528                 CWARN("%s: reconciliation for "DFID" failed with %d\n",
529                       qsd->qsd_svname, PFID(&qqi->qqi_slv_fid), rc);
530
531         EXIT;
532 out_lock:
533         ldlm_lock_decref(&qqi->qqi_lockh, qsd_glb_einfo.ei_mode);
534 out_env_init:
535         lu_env_fini(env);
536 out_env:
537         OBD_FREE_PTR(env);
538 out:
539         write_lock(&qsd->qsd_lock);
540         qqi->qqi_reint = 0;
541         write_unlock(&qsd->qsd_lock);
542
543         thread_set_flags(thread, SVC_STOPPED);
544         wake_up(&thread->t_ctl_waitq);
545
546         lu_ref_del(&qqi->qqi_reference, "reint_thread", thread);
547         qqi_putref(qqi);
548
549         return rc;
550 }
551
552 void qsd_stop_reint_thread(struct qsd_qtype_info *qqi)
553 {
554         struct ptlrpc_thread    *thread = &qqi->qqi_reint_thread;
555         struct l_wait_info       lwi = { 0 };
556
557         if (!thread_is_stopped(thread)) {
558                 thread_set_flags(thread, SVC_STOPPING);
559                 wake_up(&thread->t_ctl_waitq);
560
561                 l_wait_event(thread->t_ctl_waitq,
562                              thread_is_stopped(thread), &lwi);
563         }
564 }
565
566 static int qsd_entry_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
567                              struct hlist_node *hnode, void *data)
568 {
569         struct lquota_entry     *lqe;
570         int                     *pending = (int *)data;
571
572         lqe = hlist_entry(hnode, struct lquota_entry, lqe_hash);
573         LASSERT(atomic_read(&lqe->lqe_ref) > 0);
574
575         lqe_read_lock(lqe);
576         *pending += lqe->lqe_pending_req;
577         lqe_read_unlock(lqe);
578
579         return 0;
580 }
581
582 static bool qsd_pending_updates(struct qsd_qtype_info *qqi)
583 {
584         struct qsd_instance     *qsd = qqi->qqi_qsd;
585         struct qsd_upd_rec      *upd;
586         struct lquota_entry     *lqe, *n;
587         int                      dqacq = 0;
588         bool                     updates = false;
589         ENTRY;
590
591         /* any pending quota adjust? */
592         spin_lock(&qsd->qsd_adjust_lock);
593         list_for_each_entry_safe(lqe, n, &qsd->qsd_adjust_list, lqe_link) {
594                 if (lqe2qqi(lqe) == qqi) {
595                         list_del_init(&lqe->lqe_link);
596                         lqe_putref(lqe);
597                 }
598         }
599         spin_unlock(&qsd->qsd_adjust_lock);
600
601         /* any pending updates? */
602         read_lock(&qsd->qsd_lock);
603         list_for_each_entry(upd, &qsd->qsd_upd_list, qur_link) {
604                 if (upd->qur_qqi == qqi) {
605                         read_unlock(&qsd->qsd_lock);
606                         CDEBUG(D_QUOTA, "%s: pending %s updates for type:%d.\n",
607                                qsd->qsd_svname,
608                                upd->qur_global ? "global" : "slave",
609                                qqi->qqi_qtype);
610                         GOTO(out, updates = true);
611                 }
612         }
613         read_unlock(&qsd->qsd_lock);
614
615         /* any pending quota request? */
616         cfs_hash_for_each_safe(qqi->qqi_site->lqs_hash, qsd_entry_iter_cb,
617                                &dqacq);
618         if (dqacq) {
619                 CDEBUG(D_QUOTA, "%s: pending dqacq for type:%d.\n",
620                        qsd->qsd_svname, qqi->qqi_qtype);
621                 updates = true;
622         }
623         EXIT;
624 out:
625         if (updates)
626                 CERROR("%s: Delaying reintegration for qtype:%d until pending "
627                        "updates are flushed.\n",
628                        qsd->qsd_svname, qqi->qqi_qtype);
629         return updates;
630 }
631
632 int qsd_start_reint_thread(struct qsd_qtype_info *qqi)
633 {
634         struct ptlrpc_thread    *thread = &qqi->qqi_reint_thread;
635         struct qsd_instance     *qsd = qqi->qqi_qsd;
636         struct l_wait_info       lwi = { 0 };
637         struct task_struct      *task;
638         int                      rc;
639         char                    *name;
640         ENTRY;
641
642         /* do not try to start a new thread as this can lead to a deadlock */
643         if (current->flags & (PF_MEMALLOC | PF_KSWAPD))
644                 RETURN(0);
645
646         if (qsd->qsd_dev->dd_rdonly)
647                 RETURN(0);
648
649         /* don't bother to do reintegration when quota isn't enabled */
650         if (!qsd_type_enabled(qsd, qqi->qqi_qtype))
651                 RETURN(0);
652
653         if (qqi->qqi_acct_failed)
654                 /* no space accounting support, can't enable enforcement */
655                 RETURN(0);
656
657         /* check if the reintegration has already started or finished */
658         write_lock(&qsd->qsd_lock);
659
660         if ((qqi->qqi_glb_uptodate && qqi->qqi_slv_uptodate) ||
661              qqi->qqi_reint || qsd->qsd_stopping) {
662                 write_unlock(&qsd->qsd_lock);
663                 RETURN(0);
664         }
665         qqi->qqi_reint = 1;
666
667         write_unlock(&qsd->qsd_lock);
668
669         /* there could be some unfinished global or index entry updates
670          * (very unlikely), to avoid them messing up with the reint
671          * procedure, we just return and try to re-start reint later. */
672         if (qsd_pending_updates(qqi)) {
673                 write_lock(&qsd->qsd_lock);
674                 qqi->qqi_reint = 0;
675                 write_unlock(&qsd->qsd_lock);
676                 RETURN(0);
677         }
678
679         OBD_ALLOC(name, MTI_NAME_MAXLEN);
680         if (name == NULL)
681                 RETURN(-ENOMEM);
682
683         snprintf(name, MTI_NAME_MAXLEN, "qsd_reint_%d.%s",
684                  qqi->qqi_qtype, qsd->qsd_svname);
685
686         task = kthread_run(qsd_reint_main, qqi, name);
687         OBD_FREE(name, MTI_NAME_MAXLEN);
688
689         if (IS_ERR(task)) {
690                 rc = PTR_ERR(task);
691                 thread_set_flags(thread, SVC_STOPPED);
692                 write_lock(&qsd->qsd_lock);
693                 qqi->qqi_reint = 0;
694                 write_unlock(&qsd->qsd_lock);
695                 RETURN(rc);
696         }
697
698         l_wait_event(thread->t_ctl_waitq,
699                      thread_is_running(thread) || thread_is_stopped(thread),
700                      &lwi);
701         RETURN(0);
702 }