Whamcloud - gitweb
Branch b1_8
[fs/lustre-release.git] / lustre / quota / quota_master.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/quota/quota_master.c
37  *
38  * Lustre Quota Master request handler
39  *
40  * Author: Niu YaWei <niu@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46
47 #define DEBUG_SUBSYSTEM S_LQUOTA
48
49 #include <linux/version.h>
50 #include <linux/fs.h>
51 #include <asm/unistd.h>
52 #include <linux/slab.h>
53 #include <linux/quotaops.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/quota.h>
57
58 #include <obd_class.h>
59 #include <lustre_quota.h>
60 #include <lustre_fsfilt.h>
61 #include <lustre_mds.h>
62
63 #include "quota_internal.h"
64
65 #ifdef HAVE_QUOTA_SUPPORT
66
67 /* lock ordering: mds->mds_qonoff_sem > dquot->dq_sem */
68 static struct list_head lustre_dquot_hash[NR_DQHASH];
69 static spinlock_t dquot_hash_lock = SPIN_LOCK_UNLOCKED;
70
71 cfs_mem_cache_t *lustre_dquot_cachep;
72
73 int lustre_dquot_init(void)
74 {
75         int i;
76         ENTRY;
77
78         LASSERT(lustre_dquot_cachep == NULL);
79         lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
80                                                    sizeof(struct lustre_dquot),
81                                                    0, 0);
82         if (!lustre_dquot_cachep)
83                 return (-ENOMEM);
84
85         for (i = 0; i < NR_DQHASH; i++) {
86                 INIT_LIST_HEAD(lustre_dquot_hash + i);
87         }
88         RETURN(0);
89 }
90
91 void lustre_dquot_exit(void)
92 {
93         int i;
94         ENTRY;
95         /* FIXME cleanup work ?? */
96
97         for (i = 0; i < NR_DQHASH; i++) {
98                 LASSERT(list_empty(lustre_dquot_hash + i));
99         }
100         if (lustre_dquot_cachep) {
101                 int rc;
102                 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
103                 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
104                 lustre_dquot_cachep = NULL;
105         }
106         EXIT;
107 }
108
109 static inline int
110 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
111              __attribute__((__const__));
112
113 static inline int
114 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
115 {
116         unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
117         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
118         return tmp;
119 }
120
121 /* caller must hold dquot_hash_lock */
122 static struct lustre_dquot *find_dquot(int hashent,
123                                        struct lustre_quota_info *lqi, qid_t id,
124                                        int type)
125 {
126         struct lustre_dquot *dquot;
127         ENTRY;
128
129         LASSERT_SPIN_LOCKED(&dquot_hash_lock);
130         list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
131                 if (dquot->dq_info == lqi &&
132                     dquot->dq_id == id && dquot->dq_type == type)
133                         RETURN(dquot);
134         }
135         RETURN(NULL);
136 }
137
138 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
139                                         qid_t id, int type)
140 {
141         struct lustre_dquot *dquot = NULL;
142         ENTRY;
143
144         OBD_SLAB_ALLOC(dquot, lustre_dquot_cachep, CFS_ALLOC_IO, sizeof(*dquot));
145         if (dquot == NULL)
146                 RETURN(NULL);
147
148         INIT_LIST_HEAD(&dquot->dq_hash);
149         init_mutex_locked(&dquot->dq_sem);
150         dquot->dq_refcnt = 1;
151         dquot->dq_info = lqi;
152         dquot->dq_id = id;
153         dquot->dq_type = type;
154         dquot->dq_status = DQ_STATUS_AVAIL;
155
156         RETURN(dquot);
157 }
158
159 static void free_dquot(struct lustre_dquot *dquot)
160 {
161         OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
162 }
163
164 static void insert_dquot_nolock(struct lustre_dquot *dquot)
165 {
166         struct list_head *head = lustre_dquot_hash +
167             dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
168         LASSERT(list_empty(&dquot->dq_hash));
169         list_add(&dquot->dq_hash, head);
170 }
171
172 static void remove_dquot_nolock(struct lustre_dquot *dquot)
173 {
174         LASSERT(!list_empty(&dquot->dq_hash));
175         list_del_init(&dquot->dq_hash);
176 }
177
178 static void lustre_dqput(struct lustre_dquot *dquot)
179 {
180         ENTRY;
181         spin_lock(&dquot_hash_lock);
182         LASSERT(dquot->dq_refcnt);
183         dquot->dq_refcnt--;
184         if (!dquot->dq_refcnt) {
185                 remove_dquot_nolock(dquot);
186                 free_dquot(dquot);
187         }
188         spin_unlock(&dquot_hash_lock);
189         EXIT;
190 }
191
192 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
193                                          struct lustre_quota_info *lqi,
194                                          qid_t id, int type)
195 {
196         unsigned int hashent = dquot_hashfn(lqi, id, type);
197         struct lustre_dquot *dquot, *empty;
198         ENTRY;
199
200         if ((empty = alloc_dquot(lqi, id, type)) == NULL)
201                 RETURN(ERR_PTR(-ENOMEM));
202
203         spin_lock(&dquot_hash_lock);
204         if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
205                 dquot->dq_refcnt++;
206                 spin_unlock(&dquot_hash_lock);
207                 free_dquot(empty);
208         } else {
209                 int rc;
210
211                 dquot = empty;
212                 insert_dquot_nolock(dquot);
213                 spin_unlock(&dquot_hash_lock);
214
215                 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
216                 up(&dquot->dq_sem);
217                 if (rc) {
218                         CERROR("can't read dquot from admin quotafile! "
219                                "(rc:%d)\n", rc);
220                         lustre_dqput(dquot);
221                         RETURN(ERR_PTR(rc));
222                 }
223
224         }
225
226         LASSERT(dquot);
227         RETURN(dquot);
228 }
229
230 static void init_oqaq(struct quota_adjust_qunit *oqaq,
231                       struct lustre_quota_ctxt *qctxt,
232                       qid_t id, int type)
233 {
234         struct lustre_qunit_size *lqs = NULL;
235
236         oqaq->qaq_id = id;
237         oqaq->qaq_flags = type;
238         lqs = quota_search_lqs(LQS_KEY(type, id), qctxt, 0);
239         if (lqs && !IS_ERR(lqs)) {
240                 spin_lock(&lqs->lqs_lock);
241                 oqaq->qaq_bunit_sz = lqs->lqs_bunit_sz;
242                 oqaq->qaq_iunit_sz = lqs->lqs_iunit_sz;
243                 oqaq->qaq_flags    = lqs->lqs_flags;
244                 spin_unlock(&lqs->lqs_lock);
245                 lqs_putref(lqs);
246         } else {
247                 CDEBUG(D_QUOTA, "Can't find the lustre qunit size!\n");
248                 oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
249                 oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
250         }
251 }
252
253 int dqacq_adjust_qunit_sz(struct obd_device *obd, qid_t id, int type,
254                           __u32 is_blk)
255 {
256         struct mds_obd *mds = &obd->u.mds;
257         struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
258         struct obd_device *lov_mds_obd = class_exp2obd(mds->mds_osc_exp);
259         struct lov_obd *lov = &lov_mds_obd->u.lov;
260         __u32 ost_num = lov->desc.ld_tgt_count, mdt_num = 1;
261         struct quota_adjust_qunit *oqaq = NULL;
262         unsigned int uid = 0, gid = 0;
263         struct lustre_quota_info *info = &mds->mds_quota_info;
264         struct lustre_dquot *dquot = NULL;
265         int adjust_res = 0;
266         int rc = 0;
267         ENTRY;
268
269         LASSERT(mds);
270         dquot = lustre_dqget(obd, info, id, type);
271         if (IS_ERR(dquot))
272                 RETURN(PTR_ERR(dquot));
273
274         OBD_ALLOC_PTR(oqaq);
275         if (!oqaq)
276                 GOTO(out, rc = -ENOMEM);
277
278         down(&dquot->dq_sem);
279         init_oqaq(oqaq, qctxt, id, type);
280
281         rc = dquot_create_oqaq(qctxt, dquot, ost_num, mdt_num,
282                                is_blk ? LQUOTA_FLAGS_ADJBLK :
283                                LQUOTA_FLAGS_ADJINO, oqaq);
284
285         if (rc < 0) {
286                 CDEBUG(D_ERROR, "create oqaq failed! (rc:%d)\n", rc);
287                 GOTO(out_sem, rc);
288         }
289         QAQ_DEBUG(oqaq, "show oqaq.\n")
290
291         if (!QAQ_IS_ADJBLK(oqaq) && !QAQ_IS_ADJINO(oqaq))
292                 GOTO(out_sem, rc);
293
294         /* adjust the mds slave qunit size */
295         adjust_res = quota_adjust_slave_lqs(oqaq, qctxt);
296         if (adjust_res <= 0) {
297                 if (adjust_res < 0) {
298                         rc = adjust_res;
299                         CDEBUG(D_ERROR, "adjust mds slave's qunit size failed! \
300                                (rc:%d)\n", rc);
301                 } else {
302                         CDEBUG(D_QUOTA, "qunit doesn't need to be adjusted.\n");
303                 }
304                 GOTO(out_sem, rc);
305         }
306
307         if (type)
308                 gid = dquot->dq_id;
309         else
310                 uid = dquot->dq_id;
311
312         up(&dquot->dq_sem);
313
314         rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, is_blk, 0, NULL);
315         if (rc == -EDQUOT || rc == -EBUSY) {
316                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
317                 rc = 0;
318         }
319         if (rc) {
320                 CDEBUG(D_ERROR, "mds fail to adjust file quota! \
321                                (rc:%d)\n", rc);
322                 GOTO(out, rc);
323         }
324
325         /* only when block qunit is reduced, boardcast to osts */
326         if ((adjust_res & LQS_BLK_DECREASE) && QAQ_IS_ADJBLK(oqaq))
327                 rc = obd_quota_adjust_qunit(mds->mds_osc_exp, oqaq, qctxt);
328
329 out:
330         lustre_dqput(dquot);
331         if (oqaq)
332                 OBD_FREE_PTR(oqaq);
333
334         RETURN(rc);
335 out_sem:
336         up(&dquot->dq_sem);
337         goto out;
338 }
339
340 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
341 {
342         struct mds_obd *mds = &obd->u.mds;
343         struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
344         struct lustre_quota_info *info = &mds->mds_quota_info;
345         struct lustre_dquot *dquot = NULL;
346         __u64 *usage = NULL;
347         __u64 hlimit = 0, slimit = 0;
348         time_t *time = NULL;
349         unsigned int grace = 0;
350         struct lustre_qunit_size *lqs = NULL;
351         int rc = 0;
352         ENTRY;
353
354         OBD_FAIL_RETURN(OBD_FAIL_OBD_DQACQ, -EIO);
355
356         dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata));
357         if (IS_ERR(dquot))
358                 RETURN(PTR_ERR(dquot));
359
360         DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
361         QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
362
363         down(&mds->mds_qonoff_sem);
364         down(&dquot->dq_sem);
365
366         if (dquot->dq_status & DQ_STATUS_RECOVERY) {
367                 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
368                 GOTO(out, rc = -EBUSY);
369         }
370
371         if (QDATA_IS_BLK(qdata)) {
372                 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
373                 usage = &dquot->dq_dqb.dqb_curspace;
374                 hlimit = dquot->dq_dqb.dqb_bhardlimit;
375                 slimit = dquot->dq_dqb.dqb_bsoftlimit;
376                 time = &dquot->dq_dqb.dqb_btime;
377         } else {
378                 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
379                 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
380                 hlimit = dquot->dq_dqb.dqb_ihardlimit;
381                 slimit = dquot->dq_dqb.dqb_isoftlimit;
382                 time = &dquot->dq_dqb.dqb_itime;
383         }
384
385         /* if the quota limit in admin quotafile is zero, we just inform
386          * slave to clear quota limit with zero qd_count */
387         if (hlimit == 0 && slimit == 0) {
388                 qdata->qd_count = 0;
389                 GOTO(out, rc);
390         }
391
392         switch (opc) {
393         case QUOTA_DQACQ:
394                 if (hlimit &&
395                     QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
396                 {
397                         if (QDATA_IS_CHANGE_QS(qdata) &&
398                             QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
399                                 qdata->qd_count = (hlimit -
400                                         QUSG(*usage, QDATA_IS_BLK(qdata)))
401                                         * (QDATA_IS_BLK(qdata) ?
402                                            QUOTABLOCK_SIZE : 1);
403                         else
404                                 GOTO(out, rc = -EDQUOT);
405                 }
406
407                 if (slimit &&
408                     QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
409                         if (*time && cfs_time_current_sec() >= *time)
410                                 GOTO(out, rc = -EDQUOT);
411                         else if (!*time)
412                                 *time = cfs_time_current_sec() + grace;
413                 }
414
415                 *usage += qdata->qd_count;
416                 break;
417         case QUOTA_DQREL:
418                 /* The usage in administrative file might be incorrect before
419                  * recovery done */
420                 if (*usage - qdata->qd_count < 0)
421                         *usage = 0;
422                 else
423                         *usage -= qdata->qd_count;
424
425                 /* (usage <= soft limit) but not (usage < soft limit) */
426                 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
427                         *time = 0;
428                 break;
429         default:
430                 LBUG();
431         }
432
433         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
434         EXIT;
435 out:
436         up(&dquot->dq_sem);
437         up(&mds->mds_qonoff_sem);
438         lustre_dqput(dquot);
439         if (rc != -EDQUOT)
440                 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
441                                       QDATA_IS_BLK(qdata));
442
443         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
444                                qctxt, 0);
445         if (lqs == NULL || IS_ERR(lqs)) {
446                 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
447                 qdata->qd_qunit  = QDATA_IS_BLK(qdata) ? qctxt->lqc_bunit_sz :
448                                                          qctxt->lqc_iunit_sz;
449         } else {
450                 spin_lock(&lqs->lqs_lock);
451                 qdata->qd_qunit  = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
452                                                          lqs->lqs_iunit_sz;
453                 spin_unlock(&lqs->lqs_lock);
454         }
455
456         if (QDATA_IS_BLK(qdata))
457                 QDATA_SET_ADJBLK(qdata);
458         else
459                 QDATA_SET_ADJINO(qdata);
460
461         QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
462         if (lqs)
463                 lqs_putref(lqs);
464
465         return rc;
466 }
467
468 int mds_quota_adjust(struct obd_device *obd, unsigned int qcids[],
469                      unsigned int qpids[], int rc, int opc)
470 {
471         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
472         int rc2 = 0;
473         ENTRY;
474
475         if (rc && rc != -EDQUOT && rc != ENOLCK)
476                 RETURN(0);
477
478         switch (opc) {
479         case FSFILT_OP_RENAME:
480                 /* acquire/release block quota on owner of original parent */
481                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[2], qpids[3], 1, 0,
482                                          NULL);
483                 /* fall-through */
484         case FSFILT_OP_SETATTR:
485                 /* acquire/release file quota on original owner */
486                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 0, 0,
487                                           NULL);
488                 /* fall-through */
489         case FSFILT_OP_CREATE:
490         case FSFILT_OP_UNLINK:
491                 /* acquire/release file/block quota on owner of child
492                  * (or current owner) */
493                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
494                                           NULL);
495                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
496                                           NULL);
497                 /* acquire/release block quota on owner of parent
498                  * (or original owner) */
499                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
500                                           NULL);
501                 break;
502         default:
503                 LBUG();
504                 break;
505         }
506
507         if (rc2)
508                 CDEBUG(D_QUOTA,
509                        "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
510                        rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
511         RETURN(0);
512 }
513
514 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
515                         unsigned int qpids[], int rc, int opc)
516 {
517         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
518         int rc2 = 0;
519         ENTRY;
520
521         if (rc && rc != -EDQUOT)
522                 RETURN(0);
523
524         switch (opc) {
525         case FSFILT_OP_SETATTR:
526                 /* acquire/release block quota on original & current owner */
527                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
528                                         NULL);
529                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
530                                          NULL);
531                 break;
532         case FSFILT_OP_UNLINK:
533                 /* release block quota on this owner */
534         case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
535                 /* acquire block quota on this owner */
536                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
537                                         NULL);
538                 break;
539         default:
540                 LBUG();
541                 break;
542         }
543
544         if (rc || rc2) {
545                 if (!rc)
546                         rc = rc2;
547                 CDEBUG(D_QUOTA,
548                        "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
549                        rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
550         }
551
552         RETURN(0);
553 }
554
555 static const char prefix[] = "OBJECTS/";
556
557 int mds_quota_get_version(struct obd_device *obd,
558                           lustre_quota_version_t *aver,
559                           lustre_quota_version_t *over)
560 {
561         struct mds_obd *mds = &obd->u.mds;
562         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
563
564         if (!atomic_dec_and_test(&mds->mds_obt.obt_quotachecking)) {
565                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
566                 atomic_inc(&mds->mds_obt.obt_quotachecking);
567                 RETURN(-EBUSY);
568         }
569         down(&mds->mds_qonoff_sem);
570
571         *aver = qinfo->qi_version;
572         *over = mds->mds_obt.obt_qfmt;
573
574         up(&mds->mds_qonoff_sem);
575         atomic_inc(&mds->mds_obt.obt_quotachecking);
576
577         return 0;
578 }
579
580 int mds_quota_set_version(struct obd_device *obd,
581                           lustre_quota_version_t aver,
582                           lustre_quota_version_t over)
583 {
584         struct mds_obd *mds = &obd->u.mds;
585         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
586         int rc = 0, i;
587
588         LASSERT(aver == LUSTRE_QUOTA_V1 || aver == LUSTRE_QUOTA_V2);
589         LASSERT(over == LUSTRE_QUOTA_V1 || over == LUSTRE_QUOTA_V2);
590
591         if (!atomic_dec_and_test(&mds->mds_obt.obt_quotachecking)) {
592                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
593                 atomic_inc(&mds->mds_obt.obt_quotachecking);
594                 RETURN(-EBUSY);
595         }
596
597         down(&mds->mds_qonoff_sem);
598
599         /* no need to change version? nothing to do then */
600         if (qinfo->qi_version != aver) {
601                 for (i = 0; i < MAXQUOTAS; i++) {
602                         /* quota file has been opened ? */
603                         if (qinfo->qi_files[i]) {
604                                 rc = -EBUSY;
605                                 goto out;
606                         }
607                 }
608
609                 CDEBUG(D_INFO, "changing quota version %d -> %d\n",
610                        qinfo->qi_version, aver);
611                 qinfo->qi_version = aver;
612         }
613
614         mds->mds_obt.obt_qfmt = over;
615
616 out:
617         up(&mds->mds_qonoff_sem);
618         atomic_inc(&mds->mds_obt.obt_quotachecking);
619
620         return rc;
621 }
622
623 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
624 {
625         struct mds_obd *mds = &obd->u.mds;
626         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
627         int rc = 0, i;
628         char *quotafiles_v1[] = LUSTRE_ADMIN_QUOTAFILES_V1;
629         char *quotafiles_v2[] = LUSTRE_ADMIN_QUOTAFILES_V2;
630         char name[64];
631         struct lvfs_run_ctxt saved;
632
633         if (oqctl->qc_type != USRQUOTA &&
634             oqctl->qc_type != GRPQUOTA &&
635             oqctl->qc_type != UGQUOTA)
636                 return -EINVAL;
637
638         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
639
640         down(&mds->mds_qonoff_sem);
641
642         for (i = 0; i < MAXQUOTAS; i++) {
643                 struct file *fp;
644                 char* quotafile = (qinfo->qi_version == LUSTRE_QUOTA_V1)?
645                                    quotafiles_v1[i]:quotafiles_v2[i];
646
647                 if (!Q_TYPESET(oqctl, i))
648                         continue;
649
650                 /* quota file has been opened ? */
651                 if (qinfo->qi_files[i]) {
652                         rc = -EBUSY;
653                         goto out;
654                 }
655
656                 LASSERT(strlen(quotafile) + sizeof(prefix) <= sizeof(name));
657                 sprintf(name, "%s%s", prefix, quotafile);
658
659                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
660                 if (IS_ERR(fp)) {
661                         rc = PTR_ERR(fp);
662                         CERROR("error invalidating admin quotafile %s (rc:%d)\n",
663                                name, rc);
664                 }
665                 else
666                         filp_close(fp, 0);
667         }
668
669 out:
670         up(&mds->mds_qonoff_sem);
671
672         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
673
674         return rc;
675 }
676
677 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
678 {
679         struct mds_obd *mds = &obd->u.mds;
680         int rc;
681         struct lvfs_run_ctxt saved;
682
683         if (oqctl->qc_type != USRQUOTA &&
684             oqctl->qc_type != GRPQUOTA &&
685             oqctl->qc_type != UGQUOTA)
686                 RETURN(-EINVAL);
687
688         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
689         down(&mds->mds_qonoff_sem);
690
691         oqctl->qc_cmd = Q_FINVALIDATE;
692         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
693         if (!rc)
694                 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
695
696         up(&mds->mds_qonoff_sem);
697         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
698
699         return rc;
700 }
701
702 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
703 {
704         struct mds_obd *mds = &obd->u.mds;
705         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
706         char *quotafiles_v1[] = LUSTRE_ADMIN_QUOTAFILES_V1;
707         char *quotafiles_v2[] = LUSTRE_ADMIN_QUOTAFILES_V2;
708         struct lvfs_run_ctxt saved;
709         char name[64];
710         int i, rc = 0;
711         ENTRY;
712
713         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
714
715         down(&mds->mds_qonoff_sem);
716
717         for (i = 0; i < MAXQUOTAS && !rc; i++) {
718                 struct file *fp;
719                 char* quotafile = (qinfo->qi_version == LUSTRE_QUOTA_V1)?
720                                         quotafiles_v1[i]:quotafiles_v2[i];
721
722                 if (!Q_TYPESET(oqctl, i))
723                         continue;
724
725                 /* quota file has been opened ? */
726                 if (qinfo->qi_files[i]) {
727                         CWARN("init %s admin quotafile while quota on.\n",
728                               i == USRQUOTA ? "user" : "group");
729                         continue;
730                 }
731
732                 LASSERT(strlen(quotafile) + sizeof(prefix) <= sizeof(name));
733                 sprintf(name, "%s%s", prefix, quotafile);
734
735                 /* check if quota file exists and is correct */
736                 fp = filp_open(name, O_RDONLY, 0);
737                 if (!IS_ERR(fp)) {
738                         /* irregular file is not the right place for quota */
739                         if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
740                                 CERROR("admin quota file %s is not "
741                                        "regular!", quotafile);
742                                 filp_close(fp, 0);
743                                 rc = -EINVAL;
744                                 break;
745                         }
746                         qinfo->qi_files[i] = fp;
747                         rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
748                         qinfo->qi_files[i] = 0;
749                         filp_close(fp, 0);
750                 }
751                 else
752                         rc = PTR_ERR(fp);
753
754                 if (!rc)
755                         continue;
756
757                 /* -EINVAL may be returned by quotainfo for bad quota file */
758                 if (rc != -ENOENT && rc != -EINVAL) {
759                         CERROR("error opening old quota file %s (%d)\n",
760                                name, rc);
761                         break;
762                 }
763
764                 CDEBUG(D_INFO, "%s new quota file %s\n", name,
765                        rc == -ENOENT ? "creating" : "overwriting");
766
767                 /* create quota file overwriting old if needed */
768                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
769                 if (IS_ERR(fp)) {
770                         rc = PTR_ERR(fp);
771                         CERROR("error creating admin quotafile %s (rc:%d)\n",
772                                name, rc);
773                         break;
774                 }
775
776                 qinfo->qi_files[i] = fp;
777
778                 switch (qinfo->qi_version) {
779                 case LUSTRE_QUOTA_V1:
780                         rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
781                         if (rc)
782                                 CERROR("error init %s admin quotafile! (rc:%d)\n",
783                                        i == USRQUOTA ? "user" : "group", rc);
784                         break;
785                 case LUSTRE_QUOTA_V2:
786                         rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CONVERT);
787                         if (rc)
788                                 CERROR("error convert %s admin quotafile! (rc:%d)\n",
789                                        i == USRQUOTA ? "user" : "group", rc);
790                         break;
791                 default:
792                         LBUG();
793                 }
794
795                 filp_close(fp, 0);
796                 qinfo->qi_files[i] = NULL;
797         }
798         up(&mds->mds_qonoff_sem);
799
800         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
801         RETURN(rc);
802 }
803
804 static int close_quota_files(struct obd_quotactl *oqctl,
805                              struct lustre_quota_info *qinfo)
806 {
807         int i, rc = 0;
808         ENTRY;
809
810         for (i = 0; i < MAXQUOTAS; i++) {
811                 if (!Q_TYPESET(oqctl, i))
812                         continue;
813                 if (qinfo->qi_files[i] == NULL) {
814                         rc = -ESRCH;
815                         continue;
816                 }
817                 filp_close(qinfo->qi_files[i], 0);
818                 qinfo->qi_files[i] = NULL;
819         }
820         RETURN(rc);
821 }
822
823 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
824 {
825         struct mds_obd *mds = &obd->u.mds;
826         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
827         const char *quotafiles_v1[] = LUSTRE_ADMIN_QUOTAFILES_V1;
828         const char *quotafiles_v2[] = LUSTRE_ADMIN_QUOTAFILES_V2;
829         char name[64];
830         int i, rc = 0;
831         ENTRY;
832
833         /* open admin quota files and read quotafile info */
834         for (i = 0; i < MAXQUOTAS; i++) {
835                 struct file *fp;
836                 const char* quotafile = qinfo->qi_version == LUSTRE_QUOTA_V1?
837                                         quotafiles_v1[i] : quotafiles_v2[i];
838
839                 if (!Q_TYPESET(oqctl, i))
840                         continue;
841
842                 LASSERT(strlen(quotafile)
843                         + sizeof(prefix) <= sizeof(name));
844                 sprintf(name, "%s%s", prefix, quotafile);
845
846                 if (qinfo->qi_files[i] != NULL) {
847                         rc = -EBUSY;
848                         break;
849                 }
850
851                 fp = filp_open(name, O_RDWR, 0);
852                 /* handle transparent migration to 64 bit quota file */
853                 if (IS_ERR(fp) && PTR_ERR(fp) == -ENOENT &&
854                     qinfo->qi_version == LUSTRE_QUOTA_V2) {
855                         CDEBUG(D_INFO, "attempting to convert V1 quota file to"
856                                        " V2 format\n");
857                         fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
858                         if (!IS_ERR(fp)) {
859                                 qinfo->qi_files[i] = fp;
860                                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CONVERT);
861                                 if (rc) {
862                                         CERROR("error convert %s admin "
863                                                "quotafile! (rc:%d)\n",
864                                                i == USRQUOTA ? "user" : "group",
865                                                rc);
866                                         break;
867                                 }
868                         }
869                 }
870
871                 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
872                         rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
873                         CERROR("error open/create %s! (rc:%d)\n", name, rc);
874                         break;
875                 }
876                 qinfo->qi_files[i] = fp;
877
878                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
879                 if (rc) {
880                         CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
881                         break;
882                 }
883
884                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
885                 if (rc) {
886                         CERROR("error read quotainfo of %s! (rc:%d)\n", name,
887                                rc);
888                         break;
889                 }
890         }
891
892         if (rc && rc != -EBUSY)
893                 close_quota_files(oqctl, qinfo);
894
895         RETURN(rc);
896 }
897
898 int mds_admin_quota_off(struct obd_device *obd,
899                         struct obd_quotactl *oqctl)
900 {
901         struct mds_obd *mds = &obd->u.mds;
902         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
903         int rc;
904         ENTRY;
905
906         /* close admin quota files */
907         rc = close_quota_files(oqctl, qinfo);
908         RETURN(rc);
909 }
910
911 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
912 {
913         struct mds_obd *mds = &obd->u.mds;
914         struct obd_device_target *obt = &obd->u.obt;
915         struct lvfs_run_ctxt saved;
916         int rc;
917         ENTRY;
918
919         if (oqctl->qc_type != USRQUOTA &&
920             oqctl->qc_type != GRPQUOTA &&
921             oqctl->qc_type != UGQUOTA)
922                 RETURN(-EINVAL);
923
924         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
925                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
926                 atomic_inc(&obt->obt_quotachecking);
927                 RETURN(-EBUSY);
928         }
929
930         down(&mds->mds_qonoff_sem);
931         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
932         rc = mds_admin_quota_on(obd, oqctl);
933         if (rc)
934                 GOTO(out, rc);
935
936         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
937         if (!rc)
938                 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
939         else
940                 GOTO(out, rc);
941
942         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
943
944 out:
945         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
946         up(&mds->mds_qonoff_sem);
947         atomic_inc(&obt->obt_quotachecking);
948         RETURN(rc);
949 }
950
951 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
952 {
953         struct mds_obd *mds = &obd->u.mds;
954         struct obd_device_target *obt = &obd->u.obt;
955         struct lvfs_run_ctxt saved;
956         int rc, rc2;
957         ENTRY;
958
959         if (oqctl->qc_type != USRQUOTA &&
960             oqctl->qc_type != GRPQUOTA &&
961             oqctl->qc_type != UGQUOTA)
962                 RETURN(-EINVAL);
963
964         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
965                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
966                 atomic_inc(&obt->obt_quotachecking);
967                 RETURN(-EBUSY);
968         }
969
970         down(&mds->mds_qonoff_sem);
971         /* close admin quota files */
972         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
973         mds_admin_quota_off(obd, oqctl);
974
975         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
976         rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
977         if (!rc2)
978                 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
979
980         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
981         up(&mds->mds_qonoff_sem);
982         atomic_inc(&obt->obt_quotachecking);
983
984         RETURN(rc ?: rc2);
985 }
986
987 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
988 {
989         struct mds_obd *mds = &obd->u.mds;
990         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
991         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
992         int rc;
993         ENTRY;
994
995         if (oqctl->qc_type != USRQUOTA &&
996             oqctl->qc_type != GRPQUOTA)
997                 RETURN(-EINVAL);
998
999         down(&mds->mds_qonoff_sem);
1000         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1001                 rc = -ESRCH;
1002                 goto out;
1003         }
1004
1005         qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
1006         qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
1007         qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
1008
1009         rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
1010
1011 out:
1012         up(&mds->mds_qonoff_sem);
1013         RETURN(rc);
1014 }
1015
1016 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
1017 {
1018         struct mds_obd *mds = &obd->u.mds;
1019         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1020         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
1021         int rc = 0;
1022         ENTRY;
1023
1024         if (oqctl->qc_type != USRQUOTA &&
1025             oqctl->qc_type != GRPQUOTA)
1026                 RETURN(-EINVAL);
1027
1028         down(&mds->mds_qonoff_sem);
1029         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1030                 rc = -ESRCH;
1031                 goto out;
1032         }
1033
1034         dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
1035         dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
1036         dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
1037
1038 out:
1039         up(&mds->mds_qonoff_sem);
1040         RETURN(rc);
1041 }
1042
1043 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
1044                       struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
1045                       int type, struct quota_adjust_qunit *oqaq)
1046 {
1047         __u64 bunit_curr_o, iunit_curr_o;
1048         unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
1049         unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
1050         __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
1051                 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
1052         __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
1053                 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1054         int rc = 0;
1055         ENTRY;
1056
1057         if (!dquot || !oqaq)
1058                 RETURN(-EINVAL);
1059         LASSERT_SEM_LOCKED(&dquot->dq_sem);
1060         LASSERT(oqaq->qaq_iunit_sz);
1061         LASSERT(oqaq->qaq_bunit_sz);
1062
1063         /* don't change qunit size */
1064         if (!qctxt->lqc_switch_qs)
1065                 RETURN(rc);
1066
1067         bunit_curr_o = oqaq->qaq_bunit_sz;
1068         iunit_curr_o = oqaq->qaq_iunit_sz;
1069
1070         if (dquot->dq_type == GRPQUOTA)
1071                 QAQ_SET_GRP(oqaq);
1072
1073         if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1074                 __u64 b_limitation =
1075                         oqaq->qaq_bunit_sz * ost_num * shrink_qunit_limit;
1076                 /* enlarge block qunit size */
1077                 while (blimit >
1078                        QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1079                         oqaq->qaq_bunit_sz =
1080                                 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1081                                 << QUOTABLOCK_BITS;
1082                         b_limitation = oqaq->qaq_bunit_sz * ost_num *
1083                                 shrink_qunit_limit;
1084                 }
1085
1086                 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1087                         oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1088
1089                 /* shrink block qunit size */
1090                 while (blimit <
1091                        QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1092                         do_div(oqaq->qaq_bunit_sz , cqs_factor);
1093                         oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1094                                 QUOTABLOCK_BITS;
1095                         b_limitation = oqaq->qaq_bunit_sz * ost_num *
1096                                 shrink_qunit_limit;
1097                         if (oqaq->qaq_bunit_sz <  qctxt->lqc_cqs_least_bunit)
1098                                 break;
1099                 }
1100
1101                 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1102                         oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1103
1104                 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1105                         QAQ_SET_ADJBLK(oqaq);
1106
1107         }
1108
1109         if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1110                 __u64 i_limitation =
1111                         oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1112                 /* enlarge file qunit size */
1113                 while (ilimit > dquot->dq_dqb.dqb_curinodes
1114                        + 2 * i_limitation) {
1115                         oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1116                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1117                                 shrink_qunit_limit;
1118                 }
1119
1120                 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1121                         oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1122
1123                 /* shrink file qunit size */
1124                 while (ilimit < dquot->dq_dqb.dqb_curinodes
1125                        + i_limitation) {
1126                         do_div(oqaq->qaq_iunit_sz, cqs_factor);
1127                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1128                                        shrink_qunit_limit;
1129                         if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1130                                 break;
1131                 }
1132
1133                 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1134                         oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1135
1136                 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1137                         QAQ_SET_ADJINO(oqaq);
1138
1139         }
1140
1141         QAQ_DEBUG(oqaq, "the oqaq computed\n");
1142
1143         RETURN(rc);
1144 }
1145
1146 static int mds_init_slave_ilimits(struct obd_device *obd,
1147                                   struct obd_quotactl *oqctl, int set)
1148 {
1149         /* XXX: for file limits only adjust local now */
1150         struct obd_device_target *obt = &obd->u.obt;
1151         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1152         unsigned int uid = 0, gid = 0;
1153         struct obd_quotactl *ioqc = NULL;
1154         struct lustre_qunit_size *lqs;
1155         int flag;
1156         int rc;
1157         ENTRY;
1158
1159         /* if we are going to set zero limit, needn't init slaves */
1160         if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1161             !set)
1162                 RETURN(0);
1163
1164         OBD_ALLOC_PTR(ioqc);
1165         if (!ioqc)
1166                 RETURN(-ENOMEM);
1167
1168         flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1169                oqctl->qc_dqblk.dqb_isoftlimit || !set;
1170         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1171         ioqc->qc_id = oqctl->qc_id;
1172         ioqc->qc_type = oqctl->qc_type;
1173         ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1174         ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1175
1176         /* build lqs for mds */
1177         lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1178                                qctxt, flag ? 1 : 0);
1179         if (lqs && !IS_ERR(lqs)) {
1180                 if (flag)
1181                         lqs->lqs_flags |= QI_SET;
1182                 else
1183                         lqs->lqs_flags &= ~QI_SET;
1184                 lqs_putref(lqs);
1185         } else {
1186                 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1187                        flag ? "create" : "search",
1188                        oqctl->qc_type ? "group" : "user",
1189                        oqctl->qc_id);
1190                 GOTO(out, rc = PTR_ERR(lqs));
1191         }
1192
1193         /* set local limit to MIN_QLIMIT */
1194         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1195         if (rc)
1196                 GOTO(out, rc);
1197
1198         /* trigger local qunit pre-acquire */
1199         if (oqctl->qc_type == USRQUOTA)
1200                 uid = oqctl->qc_id;
1201         else
1202                 gid = oqctl->qc_id;
1203
1204         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0,
1205                                 NULL);
1206         if (rc == -EDQUOT || rc == -EBUSY) {
1207                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1208                 rc = 0;
1209         }
1210         if (rc) {
1211                 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1212                        rc);
1213                 GOTO(out, rc);
1214         }
1215         /* FIXME initialize all slaves in CMD */
1216         EXIT;
1217 out:
1218         if (ioqc)
1219                 OBD_FREE_PTR(ioqc);
1220         return rc;
1221 }
1222
1223 static int mds_init_slave_blimits(struct obd_device *obd,
1224                                   struct obd_quotactl *oqctl, int set)
1225 {
1226         struct obd_device_target *obt = &obd->u.obt;
1227         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1228         struct mds_obd *mds = &obd->u.mds;
1229         struct obd_quotactl *ioqc;
1230         struct lustre_qunit_size *lqs;
1231         unsigned int uid = 0, gid = 0;
1232         int rc;
1233         int flag;
1234         ENTRY;
1235
1236         /* if we are going to set zero limit, needn't init slaves */
1237         if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1238             !set)
1239                 RETURN(0);
1240
1241         OBD_ALLOC_PTR(ioqc);
1242         if (!ioqc)
1243                 RETURN(-ENOMEM);
1244
1245         flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1246                oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1247         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1248         ioqc->qc_id = oqctl->qc_id;
1249         ioqc->qc_type = oqctl->qc_type;
1250         ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1251         ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1252
1253         /* build lqs for mds */
1254         lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1255                                qctxt, flag ? 1 : 0);
1256         if (lqs && !IS_ERR(lqs)) {
1257                 if (flag)
1258                         lqs->lqs_flags |= QB_SET;
1259                 else
1260                         lqs->lqs_flags &= ~QB_SET;
1261                 lqs_putref(lqs);
1262         } else {
1263                 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1264                        flag ? "create" : "search",
1265                        oqctl->qc_type ? "group" : "user",
1266                        oqctl->qc_id);
1267                 GOTO(out, rc = PTR_ERR(lqs));
1268         }
1269
1270         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1271         if (rc)
1272                 GOTO(out, rc);
1273
1274         /* trigger local qunit pre-acquire */
1275         if (oqctl->qc_type == USRQUOTA)
1276                 uid = oqctl->qc_id;
1277         else
1278                 gid = oqctl->qc_id;
1279
1280         /* initialize all slave's limit */
1281         rc = obd_quotactl(mds->mds_osc_exp, ioqc);
1282
1283         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0,
1284                                 NULL);
1285         if (rc == -EDQUOT || rc == -EBUSY) {
1286                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1287                 rc = 0;
1288         }
1289         if (rc) {
1290                 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1291                 GOTO(out, rc);
1292         }
1293
1294         EXIT;
1295 out:
1296         OBD_FREE_PTR(ioqc);
1297         return rc;
1298 }
1299
1300 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1301 {
1302         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1303         int rc = 0;
1304
1305         QAQ_SET_CREATE_LQS(qaq);
1306         /* adjust local lqs */
1307         rc = quota_adjust_slave_lqs(qaq, qctxt);
1308         if (rc < 0)
1309                 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1310
1311         /* adjust remote lqs */
1312         if (QAQ_IS_ADJBLK(qaq)) {
1313                 rc = obd_quota_adjust_qunit(obd->u.mds.mds_osc_exp, qaq, qctxt);
1314                 if (rc < 0)
1315                         CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1316
1317         }
1318 }
1319
1320 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1321 {
1322         struct mds_obd *mds = &obd->u.mds;
1323         struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1324         struct obd_device *lov_obd = class_exp2obd(mds->mds_osc_exp);
1325         struct lov_obd *lov = &lov_obd->u.lov;
1326         struct quota_adjust_qunit *oqaq = NULL;
1327         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1328         __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1329         time_t btime, itime;
1330         struct lustre_dquot *dquot;
1331         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1332         /* orig_set means if quota was set before; now_set means we are
1333          * setting/cancelling quota */
1334         int orig_set, now_set;
1335         int rc, rc2 = 0, flag = 0;
1336         ENTRY;
1337
1338         if (oqctl->qc_type != USRQUOTA &&
1339             oqctl->qc_type != GRPQUOTA)
1340                 RETURN(-EINVAL);
1341
1342         OBD_ALLOC_PTR(oqaq);
1343         if (!oqaq)
1344                 RETURN(-ENOMEM);
1345         down(&mds->mds_qonoff_sem);
1346         init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1347
1348         if (qinfo->qi_files[oqctl->qc_type] == NULL)
1349                 GOTO(out_sem, rc = -ESRCH);
1350
1351         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1352         if (IS_ERR(dquot))
1353                 GOTO(out_sem, rc = PTR_ERR(dquot));
1354         DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1355         QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1356
1357         down(&dquot->dq_sem);
1358
1359         if (dquot->dq_status) {
1360                 up(&dquot->dq_sem);
1361                 lustre_dqput(dquot);
1362                 GOTO(out_sem, rc = -EBUSY);
1363         }
1364         dquot->dq_status |= DQ_STATUS_SET;
1365
1366         ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1367         isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1368         bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1369         bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1370         btime = dquot->dq_dqb.dqb_btime;
1371         itime = dquot->dq_dqb.dqb_itime;
1372
1373         if (dqblk->dqb_valid & QIF_BTIME)
1374                 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1375         if (dqblk->dqb_valid & QIF_ITIME)
1376                 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1377
1378         if (dqblk->dqb_valid & QIF_BLIMITS) {
1379                 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1380                 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1381                 /* clear usage (limit pool) */
1382                 if (!dquot->dq_dqb.dqb_bhardlimit &&
1383                     !dquot->dq_dqb.dqb_bsoftlimit)
1384                         dquot->dq_dqb.dqb_curspace = 0;
1385
1386                 /* clear grace time */
1387                 if (!dqblk->dqb_bsoftlimit ||
1388                     toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1389                         dquot->dq_dqb.dqb_btime = 0;
1390                 /* set grace only if user hasn't provided his own */
1391                 else if (!(dqblk->dqb_valid & QIF_BTIME))
1392                         dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1393                                 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1394
1395                 flag |= LQUOTA_FLAGS_ADJBLK;
1396         }
1397
1398         if (dqblk->dqb_valid & QIF_ILIMITS) {
1399                 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1400                 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1401                 /* clear usage (limit pool) */
1402                 if (!dquot->dq_dqb.dqb_ihardlimit &&
1403                     !dquot->dq_dqb.dqb_isoftlimit)
1404                         dquot->dq_dqb.dqb_curinodes = 0;
1405
1406                 if (!dqblk->dqb_isoftlimit ||
1407                     dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1408                         dquot->dq_dqb.dqb_itime = 0;
1409                 else if (!(dqblk->dqb_valid & QIF_ITIME))
1410                         dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1411                                 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1412
1413                 flag |= LQUOTA_FLAGS_ADJINO;
1414         }
1415         QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1416         rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1417                                flag, oqaq);
1418         QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1419         if (rc < 0)
1420                 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1421
1422
1423         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1424
1425         up(&dquot->dq_sem);
1426
1427         if (rc) {
1428                 CERROR("set limit failed! (rc:%d)\n", rc);
1429                 goto out;
1430         }
1431
1432         up(&mds->mds_qonoff_sem);
1433
1434         adjust_lqs(obd, oqaq);
1435
1436         orig_set = ihardlimit || isoftlimit;
1437         now_set  = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1438         if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1439                 down(&dquot->dq_sem);
1440                 dquot->dq_dqb.dqb_curinodes = 0;
1441                 up(&dquot->dq_sem);
1442                 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1443                 if (rc) {
1444                         CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1445                         goto revoke_out;
1446                 }
1447         }
1448
1449         orig_set = bhardlimit || bsoftlimit;
1450         now_set  = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1451         if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1452                 down(&dquot->dq_sem);
1453                 dquot->dq_dqb.dqb_curspace = 0;
1454                 up(&dquot->dq_sem);
1455                 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1456                 if (rc) {
1457                         CERROR("init slave blimits failed! (rc:%d)\n", rc);
1458                         goto revoke_out;
1459                 }
1460         }
1461
1462 revoke_out:
1463         down(&mds->mds_qonoff_sem);
1464         down(&dquot->dq_sem);
1465         if (rc) {
1466                 /* cancel previous setting */
1467                 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1468                 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1469                 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1470                 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1471                 dquot->dq_dqb.dqb_btime = btime;
1472                 dquot->dq_dqb.dqb_itime = itime;
1473         }
1474         rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1475         up(&dquot->dq_sem);
1476
1477 out:
1478         down(&dquot->dq_sem);
1479         dquot->dq_status &= ~DQ_STATUS_SET;
1480         up(&dquot->dq_sem);
1481         lustre_dqput(dquot);
1482         EXIT;
1483 out_sem:
1484         up(&mds->mds_qonoff_sem);
1485
1486         if (oqaq)
1487                 OBD_FREE_PTR(oqaq);
1488
1489         return rc ? rc : rc2;
1490 }
1491
1492 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1493 {
1494         struct obd_quotactl *soqc;
1495         struct lvfs_run_ctxt saved;
1496         int rc, rc1;
1497         ENTRY;
1498
1499         OBD_ALLOC_PTR(soqc);
1500         if (!soqc)
1501                 RETURN(-ENOMEM);
1502
1503         soqc->qc_cmd = Q_GETOQUOTA;
1504         soqc->qc_id = oqctl->qc_id;
1505         soqc->qc_type = oqctl->qc_type;
1506
1507         rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
1508
1509         oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1510
1511         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1512         soqc->qc_dqblk.dqb_curspace = 0;
1513         rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1514         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1515
1516         oqctl->qc_dqblk.dqb_curinodes += soqc->qc_dqblk.dqb_curinodes;
1517         if (!rc1)
1518                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1519         oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1520         if (!rc && !rc1)
1521                 oqctl->qc_dqblk.dqb_valid |= QIF_USAGE;
1522
1523         OBD_FREE_PTR(soqc);
1524
1525         if (!rc)
1526                 rc = rc1;
1527         RETURN(rc);
1528 }
1529
1530 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1531 {
1532         struct mds_obd *mds = &obd->u.mds;
1533         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1534         struct lustre_dquot *dquot;
1535         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1536         int rc;
1537         ENTRY;
1538
1539         if (oqctl->qc_type != USRQUOTA &&
1540             oqctl->qc_type != GRPQUOTA)
1541                 RETURN(-EINVAL);
1542
1543         down(&mds->mds_qonoff_sem);
1544         dqblk->dqb_valid = 0;
1545         if (qinfo->qi_files[oqctl->qc_type] == NULL)
1546                 GOTO(out, rc = -ESRCH);
1547
1548         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1549         if (IS_ERR(dquot))
1550                 GOTO(out, rc = PTR_ERR(dquot));
1551
1552         down(&dquot->dq_sem);
1553         dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1554         dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1555         dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1556         dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1557         dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1558         dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1559         dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1560         up(&dquot->dq_sem);
1561
1562         lustre_dqput(dquot);
1563         up(&mds->mds_qonoff_sem);
1564
1565         /* the usages in admin quota file is inaccurate */
1566         dqblk->dqb_curinodes = 0;
1567         dqblk->dqb_curspace = 0;
1568         rc = mds_get_space(obd, oqctl);
1569         EXIT;
1570         return rc;
1571 out:
1572         up(&mds->mds_qonoff_sem);
1573         return rc;
1574 }
1575
1576 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1577 {
1578         struct lvfs_run_ctxt saved;
1579         int rc;
1580         ENTRY;
1581
1582         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1583         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1584         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1585
1586         RETURN(rc);
1587 }
1588
1589
1590 /* FIXME we only recovery block limit by now, need recovery inode
1591  * limits also after CMD involved in */
1592 static int 
1593 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1594 {
1595         struct mds_obd *mds = &obd->u.mds;
1596         struct lustre_quota_info *qinfo= &obd->u.mds.mds_quota_info;
1597         struct lustre_dquot *dquot;
1598         struct obd_quotactl *qctl;
1599         __u64 total_limits = 0;
1600         int rc;
1601         ENTRY;
1602
1603         OBD_ALLOC_PTR(qctl);
1604         if (qctl == NULL)
1605                 RETURN(-ENOMEM);
1606
1607         dquot = lustre_dqget(obd, qinfo, id, type);
1608         if (IS_ERR(dquot)) {
1609                 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1610                 OBD_FREE_PTR(qctl);
1611                 RETURN(PTR_ERR(dquot));
1612         }
1613
1614         down(&dquot->dq_sem);
1615
1616         /* don't recovery the dquot without limits or under setting */
1617         if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1618             dquot->dq_status)
1619                 GOTO(skip, rc = 0);
1620         dquot->dq_status |= DQ_STATUS_RECOVERY;
1621
1622         up(&dquot->dq_sem);
1623
1624         /* get real bhardlimit from all slaves. */
1625         qctl->qc_cmd = Q_GETOQUOTA;
1626         qctl->qc_type = type;
1627         qctl->qc_id = id;
1628         qctl->qc_stat = QUOTA_RECOVERING;
1629         rc = obd_quotactl(obd->u.mds.mds_osc_exp, qctl);
1630         if (rc)
1631                 GOTO(out, rc);
1632         total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1633
1634         /* get real bhardlimit from master */
1635         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1636         if (rc)
1637                 GOTO(out, rc);
1638         total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1639
1640         /* amend the usage of the administrative quotafile */
1641         down(&mds->mds_qonoff_sem);
1642         down(&dquot->dq_sem);
1643
1644         dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1645
1646         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1647         if (rc)
1648                 CERROR("write dquot failed! (rc:%d)\n", rc);
1649
1650         up(&dquot->dq_sem);
1651         up(&mds->mds_qonoff_sem);
1652         EXIT;
1653 out:
1654         down(&dquot->dq_sem);
1655         dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1656 skip:
1657         up(&dquot->dq_sem);
1658
1659         lustre_dqput(dquot);
1660         OBD_FREE_PTR(qctl);
1661         return rc;
1662 }
1663
1664 struct qmaster_recov_thread_data {
1665         struct obd_device *obd;
1666         struct completion comp;
1667 };
1668
1669 static int qmaster_recovery_main(void *arg)
1670 {
1671         struct qmaster_recov_thread_data *data = arg;
1672         struct obd_device *obd = data->obd;
1673         struct mds_obd *mds = &obd->u.mds;
1674         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1675         int rc = 0;
1676         unsigned short type;
1677         ENTRY;
1678
1679         cfs_daemonize_ctxt("qmaster_recovd");
1680
1681         class_incref(obd);
1682         complete(&data->comp);
1683
1684         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1685                 struct list_head id_list;
1686                 struct dquot_id *dqid, *tmp;
1687
1688                 down(&mds->mds_qonoff_sem);
1689                 if (qinfo->qi_files[type] == NULL) {
1690                         up(&mds->mds_qonoff_sem);
1691                         continue;
1692                 }
1693                 INIT_LIST_HEAD(&id_list);
1694                 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1695                                  &id_list);
1696                 up(&mds->mds_qonoff_sem);
1697
1698                 if (rc)
1699                         CERROR("error get ids from admin quotafile.(%d)\n", rc);
1700
1701                 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1702                         list_del_init(&dqid->di_link);
1703                         if (rc)
1704                                 goto free;
1705
1706                         rc = dquot_recovery(obd, dqid->di_id, type);
1707                         if (rc)
1708                                 CERROR("qmaster recovery failed! (id:%d type:%d"
1709                                        " rc:%d)\n", dqid->di_id, type, rc);
1710 free:
1711                         OBD_FREE_PTR(dqid);
1712                 }
1713         }
1714         class_decref(obd);
1715         RETURN(rc);
1716 }
1717
1718 int mds_quota_recovery(struct obd_device *obd)
1719 {
1720         struct mds_obd *mds = &obd->u.mds;
1721         struct qmaster_recov_thread_data data;
1722         int rc = 0;
1723         ENTRY;
1724
1725         mutex_down(&obd->obd_dev_sem);
1726         if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1727                 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1728                       mds->mds_lov_desc.ld_active_tgt_count,
1729                       mds->mds_lov_objid_count);
1730                 mutex_up(&obd->obd_dev_sem);
1731                 RETURN(rc);
1732         }
1733         mutex_up(&obd->obd_dev_sem);
1734
1735         data.obd = obd;
1736         init_completion(&data.comp);
1737
1738         rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1739         if (rc < 0)
1740                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1741
1742         wait_for_completion(&data.comp);
1743         RETURN(rc);
1744 }
1745
1746 #endif /* HAVE_QUOTA_SUPPORT */