Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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 cfs_list_t lustre_dquot_hash[NR_DQHASH];
69 static cfs_spinlock_t dquot_hash_lock = CFS_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                 CFS_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(cfs_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         cfs_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_PTR_GFP(dquot, lustre_dquot_cachep, CFS_ALLOC_IO);
145         if (dquot == NULL)
146                 RETURN(NULL);
147
148         CFS_INIT_LIST_HEAD(&dquot->dq_hash);
149         cfs_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         cfs_list_t *head = lustre_dquot_hash +
167             dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
168         LASSERT(cfs_list_empty(&dquot->dq_hash));
169         cfs_list_add(&dquot->dq_hash, head);
170 }
171
172 static void remove_dquot_nolock(struct lustre_dquot *dquot)
173 {
174         LASSERT(!cfs_list_empty(&dquot->dq_hash));
175         cfs_list_del_init(&dquot->dq_hash);
176 }
177
178 static void lustre_dqput(struct lustre_dquot *dquot)
179 {
180         ENTRY;
181         cfs_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         cfs_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         cfs_spin_lock(&dquot_hash_lock);
204         if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
205                 dquot->dq_refcnt++;
206                 cfs_spin_unlock(&dquot_hash_lock);
207                 free_dquot(empty);
208         } else {
209                 int rc;
210
211                 dquot = empty;
212                 insert_dquot_nolock(dquot);
213                 cfs_spin_unlock(&dquot_hash_lock);
214
215                 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
216                 cfs_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                 cfs_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                 cfs_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         __u32 ost_num = mds->mds_lov_objid_count, mdt_num = 1;
259         struct quota_adjust_qunit *oqaq = NULL;
260         unsigned int qid[MAXQUOTAS] = { 0, 0 };
261         struct lustre_quota_info *info = &mds->mds_quota_info;
262         struct lustre_dquot *dquot = NULL;
263         int adjust_res = 0;
264         int rc = 0;
265         ENTRY;
266
267         LASSERT(mds);
268         cfs_down_read(&mds->mds_qonoff_sem);
269         dquot = lustre_dqget(obd, info, id, type);
270         if (IS_ERR(dquot))
271                 RETURN(PTR_ERR(dquot));
272
273         cfs_up_read(&mds->mds_qonoff_sem);
274         OBD_ALLOC_PTR(oqaq);
275         if (!oqaq)
276                 GOTO(out, rc = -ENOMEM);
277
278         cfs_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                 CERROR("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                         CERROR("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                 qid[GRPQUOTA] = dquot->dq_id;
309         else
310                 qid[USRQUOTA] = dquot->dq_id;
311
312         cfs_up(&dquot->dq_sem);
313
314         rc = qctxt_adjust_qunit(obd, qctxt, qid, 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                 CERROR("%s: mds fail to adjust file quota! (rc:%d)\n",
321                        obd->obd_name, 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_lov_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         cfs_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         if (OBD_FAIL_CHECK(OBD_FAIL_OBD_DQACQ))
355                 RETURN(-EIO);
356
357         cfs_down_write(&mds->mds_qonoff_sem);
358         dquot = lustre_dqget(obd, info, qdata->qd_id, QDATA_IS_GRP(qdata));
359         if (IS_ERR(dquot)) {
360                 cfs_up_write(&mds->mds_qonoff_sem);
361                 RETURN(PTR_ERR(dquot));
362         }
363
364         DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
365         QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
366
367         cfs_down(&dquot->dq_sem);
368
369         if (dquot->dq_status & DQ_STATUS_RECOVERY) {
370                 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
371                 GOTO(out, rc = -EBUSY);
372         }
373
374         if (QDATA_IS_BLK(qdata)) {
375                 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_bgrace;
376                 usage = &dquot->dq_dqb.dqb_curspace;
377                 hlimit = dquot->dq_dqb.dqb_bhardlimit;
378                 slimit = dquot->dq_dqb.dqb_bsoftlimit;
379                 time = &dquot->dq_dqb.dqb_btime;
380         } else {
381                 grace = info->qi_info[QDATA_IS_GRP(qdata)].dqi_igrace;
382                 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
383                 hlimit = dquot->dq_dqb.dqb_ihardlimit;
384                 slimit = dquot->dq_dqb.dqb_isoftlimit;
385                 time = &dquot->dq_dqb.dqb_itime;
386         }
387
388         /* if the quota limit in admin quotafile is zero, we just inform
389          * slave to clear quota limit with zero qd_count */
390         if (hlimit == 0 && slimit == 0) {
391                 qdata->qd_count = 0;
392                 GOTO(out, rc);
393         }
394
395         switch (opc) {
396         case QUOTA_DQACQ:
397                 if (hlimit &&
398                     QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > hlimit)
399                 {
400                         if (QDATA_IS_CHANGE_QS(qdata) &&
401                             QUSG(*usage, QDATA_IS_BLK(qdata)) < hlimit)
402                                 qdata->qd_count = (hlimit -
403                                         QUSG(*usage, QDATA_IS_BLK(qdata)))
404                                         * (QDATA_IS_BLK(qdata) ?
405                                            QUOTABLOCK_SIZE : 1);
406                         else
407                                 GOTO(out, rc = -EDQUOT);
408                 }
409
410                 if (slimit &&
411                     QUSG(*usage + qdata->qd_count, QDATA_IS_BLK(qdata)) > slimit) {
412                         if (*time && cfs_time_current_sec() >= *time)
413                                 GOTO(out, rc = -EDQUOT);
414                         else if (!*time)
415                                 *time = cfs_time_current_sec() + grace;
416                 }
417
418                 *usage += qdata->qd_count;
419                 break;
420         case QUOTA_DQREL:
421                 /* The usage in administrative file might be incorrect before
422                  * recovery done */
423                 if (*usage - qdata->qd_count < 0)
424                         *usage = 0;
425                 else
426                         *usage -= qdata->qd_count;
427
428                 /* (usage <= soft limit) but not (usage < soft limit) */
429                 if (!slimit || QUSG(*usage, QDATA_IS_BLK(qdata)) <= slimit)
430                         *time = 0;
431                 break;
432         default:
433                 LBUG();
434         }
435
436         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
437         EXIT;
438 out:
439         cfs_up(&dquot->dq_sem);
440         cfs_up_write(&mds->mds_qonoff_sem);
441         lustre_dqput(dquot);
442         if (rc != -EDQUOT)
443                 dqacq_adjust_qunit_sz(obd, qdata->qd_id, QDATA_IS_GRP(qdata),
444                                       QDATA_IS_BLK(qdata));
445         lqs = quota_search_lqs(LQS_KEY(QDATA_IS_GRP(qdata), qdata->qd_id),
446                                qctxt, 0);
447         if (lqs == NULL || IS_ERR(lqs)) {
448                 CDEBUG(D_INFO, "Can't find the lustre qunit size!\n");
449                 qdata->qd_qunit  = QDATA_IS_BLK(qdata) ? qctxt->lqc_bunit_sz :
450                                                          qctxt->lqc_iunit_sz;
451         } else {
452                 cfs_spin_lock(&lqs->lqs_lock);
453                 qdata->qd_qunit  = QDATA_IS_BLK(qdata) ? lqs->lqs_bunit_sz :
454                                                          lqs->lqs_iunit_sz;
455                 cfs_spin_unlock(&lqs->lqs_lock);
456         }
457
458         if (QDATA_IS_BLK(qdata))
459                 QDATA_SET_ADJBLK(qdata);
460         else
461                 QDATA_SET_ADJINO(qdata);
462
463         QDATA_DEBUG(qdata, "alloc/release qunit in dqacq_handler\n");
464         if (lqs)
465                 lqs_putref(lqs);
466
467         return rc;
468 }
469
470 int mds_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
471                      const unsigned int qpids[], int rc, int opc)
472 {
473         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
474         int rc2 = 0;
475         ENTRY;
476
477         if (rc && rc != -EDQUOT && rc != ENOLCK)
478                 RETURN(0);
479
480         switch (opc) {
481         case FSFILT_OP_SETATTR:
482                 /* release file quota on original owner */
483                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 0, 0, NULL);
484                 /* release block quota on original owner */
485                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
486                 /* acquire file quota on current owner */
487                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
488                 /* acquire block quota on current owner */
489                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
490                 break;
491         case FSFILT_OP_UNLINK_PARTIAL_CHILD:
492                 /* release file quota on child */
493                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
494                 /* rlease block quota on child */
495                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
496                 break;
497         case FSFILT_OP_CREATE_PARTIAL_CHILD:
498                 /* acquire file quota on child */
499                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
500                 /* acquire block quota on child */
501                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
502                 break;
503         case FSFILT_OP_LINK:
504                 /* acquire block quota on parent */
505                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
506                 break;
507         case FSFILT_OP_UNLINK:
508                 /* release block quota on parent */
509                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
510                 /* release file quota on child */
511                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
512                 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
513                         /* release block quota on child */
514                         rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
515                                                   NULL);
516                 break;
517         case FSFILT_OP_UNLINK_PARTIAL_PARENT:
518                 /* release block quota on parent */
519                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
520                 break;
521         case FSFILT_OP_CREATE:
522                 /* acquire block quota on parent */
523                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
524                 /* acquire file quota on child */
525                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 0, 0, NULL);
526                 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
527                         /* acquire block quota on child */
528                         rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0,
529                                                   NULL);
530                 break;
531         default:
532                 LBUG();
533                 break;
534         }
535
536         if (rc2)
537                 CDEBUG(D_QUOTA,
538                        "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
539                        rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
540         RETURN(0);
541 }
542
543 int filter_quota_adjust(struct obd_device *obd, const unsigned int qcids[],
544                         const unsigned int qpids[], int rc, int opc)
545 {
546         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
547         int rc2 = 0;
548         ENTRY;
549
550         if (rc && rc != -EDQUOT)
551                 RETURN(0);
552
553         switch (opc) {
554         case FSFILT_OP_SETATTR:
555                 /* acquire/release block quota on original & current owner */
556                 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
557                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids, 1, 0, NULL);
558                 break;
559         case FSFILT_OP_UNLINK:
560                 /* release block quota on this owner */
561         case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
562                 /* acquire block quota on this owner */
563                 rc = qctxt_adjust_qunit(obd, qctxt, qcids, 1, 0, NULL);
564                 break;
565         default:
566                 LBUG();
567                 break;
568         }
569
570         if (rc || rc2) {
571                 if (!rc)
572                         rc = rc2;
573                 CDEBUG(D_QUOTA,
574                        "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
575                        rc == QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
576         }
577
578         RETURN(0);
579 }
580
581 static const char prefix[] = "OBJECTS/";
582
583 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
584 {
585         struct mds_obd *mds = &obd->u.mds;
586         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
587         struct obd_device_target *obt = &obd->u.obt;
588         int rc = 0, i, rc1 = 0;
589         char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
590         char name[64];
591         struct lvfs_run_ctxt saved;
592         ENTRY;
593
594         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
595
596         if (oqctl->qc_type != USRQUOTA &&
597             oqctl->qc_type != GRPQUOTA &&
598             oqctl->qc_type != UGQUOTA)
599                 RETURN(-EINVAL);
600
601         cfs_down(&obt->obt_quotachecking);
602         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
603         cfs_down_write(&mds->mds_qonoff_sem);
604
605         for (i = 0; i < MAXQUOTAS; i++) {
606                 struct file *fp;
607
608                 if (!Q_TYPESET(oqctl, i))
609                         continue;
610
611                 /* quota file has been opened ? */
612                 if (qinfo->qi_files[i]) {
613                         CWARN("quota[%d] is on yet\n", i);
614                         rc1 = -EBUSY;
615                         continue;
616                 }
617
618                 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
619                 sprintf(name, "%s%s", prefix, quotafile[i]);
620
621                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
622                 if (IS_ERR(fp)) {
623                         rc = PTR_ERR(fp);
624                         CERROR("%s: error invalidating admin quotafile %s (rc:%d)\n",
625                                obd->obd_name, name, rc);
626                 }
627                 else
628                         filp_close(fp, 0);
629         }
630
631         cfs_up_write(&mds->mds_qonoff_sem);
632         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
633         cfs_up(&obt->obt_quotachecking);
634         RETURN(rc ? : rc1);
635 }
636
637 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
638 {
639         struct mds_obd *mds = &obd->u.mds;
640         struct obd_device_target *obt = &obd->u.obt;
641         int rc;
642         struct lvfs_run_ctxt saved;
643         ENTRY;
644
645         if (oqctl->qc_type != USRQUOTA &&
646             oqctl->qc_type != GRPQUOTA &&
647             oqctl->qc_type != UGQUOTA)
648                 RETURN(-EINVAL);
649
650         cfs_down(&obt->obt_quotachecking);
651         if (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))
652                 GOTO(out, rc = -EBUSY);
653         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
654         cfs_down_write(&mds->mds_qonoff_sem);
655
656         oqctl->qc_cmd = Q_FINVALIDATE;
657         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
658         if (!rc)
659                 rc = obd_quotactl(mds->mds_lov_exp, oqctl);
660
661         cfs_up_write(&mds->mds_qonoff_sem);
662         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
663 out:
664         cfs_up(&obt->obt_quotachecking);
665         RETURN(rc);
666 }
667
668 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
669 {
670         struct mds_obd *mds = &obd->u.mds;
671         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
672         const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
673         struct lvfs_run_ctxt saved;
674         char name[64];
675         int i, rc = 0;
676         ENTRY;
677
678         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
679
680         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
681         cfs_down_write(&mds->mds_qonoff_sem);
682
683         for (i = 0; i < MAXQUOTAS && !rc; i++) {
684                 struct file *fp;
685
686                 if (!Q_TYPESET(oqctl, i))
687                         continue;
688
689                 /* quota file has been opened ? */
690                 if (qinfo->qi_files[i]) {
691                         CWARN("init %s admin quotafile while quota on.\n",
692                               i == USRQUOTA ? "user" : "group");
693                         continue;
694                 }
695
696                 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
697                 sprintf(name, "%s%s", prefix, quotafile[i]);
698
699                 /* check if quota file exists and is correct */
700                 fp = filp_open(name, O_RDONLY, 0);
701                 if (!IS_ERR(fp)) {
702                         /* irregular file is not the right place for quota */
703                         if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
704                                 CERROR("admin quota file %s is not "
705                                        "regular!", name);
706                                 filp_close(fp, 0);
707                                 rc = -EINVAL;
708                                 break;
709                         }
710                         qinfo->qi_files[i] = fp;
711                         rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
712                         qinfo->qi_files[i] = NULL;
713                         filp_close(fp, 0);
714                 }
715                 else
716                         rc = PTR_ERR(fp);
717
718                 if (!rc)
719                         continue;
720
721                 /* -EINVAL may be returned by quotainfo for bad quota file */
722                 if (rc != -ENOENT && rc != -EINVAL) {
723                         CERROR("%s: error opening old quota file %s (%d)\n",
724                                obd->obd_name, name, rc);
725                         break;
726                 }
727
728                 CDEBUG(D_INFO, "%s new quota file %s\n", name,
729                        rc == -ENOENT ? "creating" : "overwriting");
730
731                 /* create quota file overwriting old if needed */
732                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
733                 if (IS_ERR(fp)) {
734                         rc = PTR_ERR(fp);
735                         CERROR("%s: error creating admin quotafile %s (rc:%d)\n",
736                                obd->obd_name, name, rc);
737                         break;
738                 }
739
740                 qinfo->qi_files[i] = fp;
741
742                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
743                 if (rc)
744                         CERROR("error init %s admin quotafile! (rc:%d)\n",
745                                i == USRQUOTA ? "user" : "group", rc);
746
747                 filp_close(fp, 0);
748                 qinfo->qi_files[i] = NULL;
749         }
750
751         cfs_up_write(&mds->mds_qonoff_sem);
752         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
753         RETURN(rc);
754 }
755
756 static int close_quota_files(struct obd_quotactl *oqctl,
757                              struct lustre_quota_info *qinfo)
758 {
759         int i, rc = 0;
760         ENTRY;
761
762         for (i = 0; i < MAXQUOTAS; i++) {
763                 if (!Q_TYPESET(oqctl, i))
764                         continue;
765                 if (qinfo->qi_files[i] == NULL) {
766                         CWARN("quota[%d] is off already\n", i);
767                         rc = -EALREADY;
768                         continue;
769                 }
770                 filp_close(qinfo->qi_files[i], 0);
771                 qinfo->qi_files[i] = NULL;
772         }
773         RETURN(rc);
774 }
775
776 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
777 {
778         struct mds_obd *mds = &obd->u.mds;
779         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
780         const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
781         char name[64];
782         int i, rc = 0, rc1 = 0;
783         ENTRY;
784
785         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
786
787         /* open admin quota files and read quotafile info */
788         for (i = 0; i < MAXQUOTAS; i++) {
789                 struct file *fp;
790
791                 if (!Q_TYPESET(oqctl, i) || qinfo->qi_files[i] != NULL)
792                         continue;
793
794                 LASSERT(strlen(quotafile[i])
795                         + sizeof(prefix) <= sizeof(name));
796                 sprintf(name, "%s%s", prefix, quotafile[i]);
797                 fp = filp_open(name, O_RDWR, 0);
798                 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
799                         rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
800                         CERROR("error open/create %s! (rc:%d)\n", name, rc);
801                         break;
802                 }
803                 qinfo->qi_files[i] = fp;
804
805                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
806                 if (rc) {
807                         CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
808                         break;
809                 }
810
811                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
812                 if (rc) {
813                         CERROR("error read quotainfo of %s! (rc:%d)\n", name,
814                                rc);
815                         break;
816                 }
817         }
818
819         if (rc && rc1 != -EALREADY)
820                 close_quota_files(oqctl, qinfo);
821
822         RETURN(rc ? : rc1);
823 }
824
825 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
826 {
827         int rc;
828         ENTRY;
829
830         if (oqctl->qc_type != USRQUOTA &&
831             oqctl->qc_type != GRPQUOTA &&
832             oqctl->qc_type != UGQUOTA)
833                 RETURN(-EINVAL);
834
835         rc = generic_quota_on(obd, oqctl, 1);
836
837         RETURN(rc);
838 }
839
840
841 int mds_admin_quota_off(struct obd_device *obd,
842                         struct obd_quotactl *oqctl)
843 {
844         struct mds_obd *mds = &obd->u.mds;
845         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
846         int rc;
847         ENTRY;
848
849         /* close admin quota files */
850         rc = close_quota_files(oqctl, qinfo);
851         RETURN(rc);
852 }
853
854
855 /* with obt->obt_quotachecking held */
856 int do_mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
857 {
858         struct mds_obd *mds = &obd->u.mds;
859         struct obd_device_target *obt = &obd->u.obt;
860         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
861         struct lvfs_run_ctxt saved;
862         int rc = 0, rc1 = 0, rc2 = 0;
863         ENTRY;
864
865         LASSERT_SEM_LOCKED(&obt->obt_quotachecking);
866
867         if (oqctl->qc_type != USRQUOTA &&
868             oqctl->qc_type != GRPQUOTA &&
869             oqctl->qc_type != UGQUOTA)
870                 RETURN(-EINVAL);
871
872         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
873         cfs_down_write(&mds->mds_qonoff_sem);
874         /* close admin quota files */
875         rc2 = mds_admin_quota_off(obd, oqctl);
876         if (rc2 && rc2 != -EALREADY) {
877                 CWARN("mds quota[%d] is failed to be off for %d\n", oqctl->qc_type, rc2);
878                 GOTO(out, rc2);
879         }
880
881         rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
882         if (!rc1) {
883                 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
884         } else if (quota_is_off(qctxt, oqctl)) {
885                 CWARN("mds local quota[%d] is off already\n", oqctl->qc_type);
886                 rc1 = -EALREADY;
887         } else {
888                 if (rc2 != -EALREADY) {
889                         CWARN("mds local quota[%d] is failed to be off for %d\n",
890                               oqctl->qc_type, rc1);
891                         oqctl->qc_cmd = Q_QUOTAON;
892                         mds_admin_quota_on(obd, oqctl);
893                         oqctl->qc_cmd = Q_QUOTAOFF;
894                 }
895                 GOTO(out, rc1);
896         }
897
898         rc = obd_quotactl(mds->mds_lov_exp, oqctl);
899         if (rc && rc != -EALREADY) {
900                 CWARN("mds remote quota[%d] is failed to be off for %d\n",
901                       oqctl->qc_type, rc);
902                 oqctl->qc_cmd = Q_QUOTAON;
903                 if (rc2 != -EALREADY)
904                         mds_admin_quota_on(obd, oqctl);
905                 if (rc1 != -EALREADY) {
906                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
907                         qctxt->lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
908                 }
909                 oqctl->qc_cmd = Q_QUOTAOFF;
910         }
911         EXIT;
912
913 out:
914         cfs_up_write(&mds->mds_qonoff_sem);
915         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
916         return rc ? : (rc1 ? : rc2);
917 }
918
919 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
920 {
921         struct obd_device_target *obt = &obd->u.obt;
922         int rc;
923         ENTRY;
924
925         cfs_down(&obt->obt_quotachecking);
926         rc = do_mds_quota_off(obd, oqctl);
927         cfs_up(&obt->obt_quotachecking);
928         RETURN(rc);
929 }
930
931 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
932 {
933         struct mds_obd *mds = &obd->u.mds;
934         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
935         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
936         int rc;
937         ENTRY;
938
939         if (oqctl->qc_type != USRQUOTA &&
940             oqctl->qc_type != GRPQUOTA)
941                 RETURN(-EINVAL);
942
943         cfs_down_write(&mds->mds_qonoff_sem);
944         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
945                 CWARN("quota[%u] is off\n", oqctl->qc_type);
946                 GOTO(out, rc = -ESRCH);
947         }
948
949         qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
950         qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
951         qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
952
953         rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
954         EXIT;
955
956 out:
957         cfs_up_write(&mds->mds_qonoff_sem);
958         return rc;
959 }
960
961 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
962 {
963         struct mds_obd *mds = &obd->u.mds;
964         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
965         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
966         int rc = 0;
967         ENTRY;
968
969         if (oqctl->qc_type != USRQUOTA &&
970             oqctl->qc_type != GRPQUOTA)
971                 RETURN(-EINVAL);
972
973         cfs_down_read(&mds->mds_qonoff_sem);
974         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
975                 CWARN("quota[%u] is off\n", oqctl->qc_type);
976                 GOTO(out, rc = -ESRCH);
977         }
978
979         dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
980         dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
981         dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
982         EXIT;
983
984 out:
985         cfs_up_read(&mds->mds_qonoff_sem);
986         return rc;
987 }
988
989 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
990                       struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
991                       int type, struct quota_adjust_qunit *oqaq)
992 {
993         __u64 bunit_curr_o, iunit_curr_o;
994         unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
995         unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
996         __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
997                 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
998         __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
999                 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
1000         int rc = 0;
1001         ENTRY;
1002
1003         if (!dquot || !oqaq)
1004                 RETURN(-EINVAL);
1005         LASSERT_SEM_LOCKED(&dquot->dq_sem);
1006         LASSERT(oqaq->qaq_iunit_sz);
1007         LASSERT(oqaq->qaq_bunit_sz);
1008
1009         /* don't change qunit size */
1010         if (!qctxt->lqc_switch_qs)
1011                 RETURN(rc);
1012
1013         bunit_curr_o = oqaq->qaq_bunit_sz;
1014         iunit_curr_o = oqaq->qaq_iunit_sz;
1015
1016         if (dquot->dq_type == GRPQUOTA)
1017                 QAQ_SET_GRP(oqaq);
1018
1019         if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
1020                 __u64 b_limitation =
1021                         oqaq->qaq_bunit_sz * (ost_num + 1) * shrink_qunit_limit;
1022                 /* enlarge block qunit size */
1023                 while (blimit >
1024                        QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
1025                         oqaq->qaq_bunit_sz =
1026                                 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1027                                 << QUOTABLOCK_BITS;
1028                         b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1029                                 shrink_qunit_limit;
1030                 }
1031
1032                 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1033                         oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1034
1035                 /* shrink block qunit size */
1036                 while (blimit <
1037                        QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1038                         do_div(oqaq->qaq_bunit_sz , cqs_factor);
1039                         oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1040                                 QUOTABLOCK_BITS;
1041                         b_limitation = oqaq->qaq_bunit_sz * (ost_num + 1) *
1042                                 shrink_qunit_limit;
1043                         if (oqaq->qaq_bunit_sz <  qctxt->lqc_cqs_least_bunit)
1044                                 break;
1045                 }
1046
1047                 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1048                         oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1049
1050                 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1051                         QAQ_SET_ADJBLK(oqaq);
1052
1053         }
1054
1055         if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1056                 __u64 i_limitation =
1057                         oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1058                 /* enlarge file qunit size */
1059                 while (ilimit > dquot->dq_dqb.dqb_curinodes
1060                        + 2 * i_limitation) {
1061                         oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1062                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1063                                 shrink_qunit_limit;
1064                 }
1065
1066                 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1067                         oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1068
1069                 /* shrink file qunit size */
1070                 while (ilimit < dquot->dq_dqb.dqb_curinodes
1071                        + i_limitation) {
1072                         do_div(oqaq->qaq_iunit_sz, cqs_factor);
1073                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1074                                        shrink_qunit_limit;
1075                         if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1076                                 break;
1077                 }
1078
1079                 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1080                         oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1081
1082                 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1083                         QAQ_SET_ADJINO(oqaq);
1084
1085         }
1086
1087         QAQ_DEBUG(oqaq, "the oqaq computed\n");
1088
1089         RETURN(rc);
1090 }
1091
1092 static int mds_init_slave_ilimits(struct obd_device *obd,
1093                                   struct obd_quotactl *oqctl, int set)
1094 {
1095         /* XXX: for file limits only adjust local now */
1096         struct obd_device_target *obt = &obd->u.obt;
1097         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1098         unsigned int id[MAXQUOTAS] = { 0, 0 };
1099         struct obd_quotactl *ioqc = NULL;
1100         struct lustre_qunit_size *lqs;
1101         int flag;
1102         int rc;
1103         ENTRY;
1104
1105         /* if we are going to set zero limit, needn't init slaves */
1106         if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1107             !set)
1108                 RETURN(0);
1109
1110         OBD_ALLOC_PTR(ioqc);
1111         if (!ioqc)
1112                 RETURN(-ENOMEM);
1113
1114         flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1115                oqctl->qc_dqblk.dqb_isoftlimit || !set;
1116         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1117         ioqc->qc_id = oqctl->qc_id;
1118         ioqc->qc_type = oqctl->qc_type;
1119         ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1120         ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1121
1122         /* build lqs for mds */
1123         lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1124                                qctxt, flag ? 1 : 0);
1125         if (lqs && !IS_ERR(lqs)) {
1126                 if (flag)
1127                         lqs->lqs_flags |= QI_SET;
1128                 else
1129                         lqs->lqs_flags &= ~QI_SET;
1130                 lqs_putref(lqs);
1131         } else {
1132                 CERROR("fail to %s lqs for inode(%s id: %u)!\n",
1133                        flag ? "create" : "search",
1134                        oqctl->qc_type ? "group" : "user",
1135                        oqctl->qc_id);
1136                 GOTO(out, rc = PTR_ERR(lqs));
1137         }
1138
1139         /* set local limit to MIN_QLIMIT */
1140         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1141         if (rc)
1142                 GOTO(out, rc);
1143
1144         /* trigger local qunit pre-acquire */
1145         if (oqctl->qc_type == USRQUOTA)
1146                 id[USRQUOTA] = oqctl->qc_id;
1147         else
1148                 id[GRPQUOTA] = oqctl->qc_id;
1149
1150         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 0, 0, NULL);
1151         if (rc == -EDQUOT || rc == -EBUSY) {
1152                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1153                 rc = 0;
1154         }
1155         if (rc) {
1156                 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1157                        rc);
1158                 GOTO(out, rc);
1159         }
1160         /* FIXME initialize all slaves in CMD */
1161         EXIT;
1162 out:
1163         if (ioqc)
1164                 OBD_FREE_PTR(ioqc);
1165         return rc;
1166 }
1167
1168 static int mds_init_slave_blimits(struct obd_device *obd,
1169                                   struct obd_quotactl *oqctl, int set)
1170 {
1171         struct obd_device_target *obt = &obd->u.obt;
1172         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1173         struct mds_obd *mds = &obd->u.mds;
1174         struct obd_quotactl *ioqc;
1175         struct lustre_qunit_size *lqs;
1176         unsigned int id[MAXQUOTAS] = { 0, 0 };
1177         int rc;
1178         int flag;
1179         ENTRY;
1180
1181         /* if we are going to set zero limit, needn't init slaves */
1182         if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1183             !set)
1184                 RETURN(0);
1185
1186         OBD_ALLOC_PTR(ioqc);
1187         if (!ioqc)
1188                 RETURN(-ENOMEM);
1189
1190         flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1191                oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1192         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1193         ioqc->qc_id = oqctl->qc_id;
1194         ioqc->qc_type = oqctl->qc_type;
1195         ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1196         ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1197
1198         /* build lqs for mds */
1199         lqs = quota_search_lqs(LQS_KEY(oqctl->qc_type, oqctl->qc_id),
1200                                qctxt, flag ? 1 : 0);
1201         if (lqs && !IS_ERR(lqs)) {
1202                 if (flag)
1203                         lqs->lqs_flags |= QB_SET;
1204                 else
1205                         lqs->lqs_flags &= ~QB_SET;
1206                 lqs_putref(lqs);
1207         } else {
1208                 CERROR("fail to %s lqs for block(%s id: %u)!\n",
1209                        flag ? "create" : "search",
1210                        oqctl->qc_type ? "group" : "user",
1211                        oqctl->qc_id);
1212                 GOTO(out, rc = PTR_ERR(lqs));
1213         }
1214
1215         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1216         if (rc)
1217                 GOTO(out, rc);
1218
1219         /* trigger local qunit pre-acquire */
1220         if (oqctl->qc_type == USRQUOTA)
1221                 id[USRQUOTA] = oqctl->qc_id;
1222         else
1223                 id[GRPQUOTA] = oqctl->qc_id;
1224
1225         /* initialize all slave's limit */
1226         rc = obd_quotactl(mds->mds_lov_exp, ioqc);
1227
1228         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, id, 1, 0, NULL);
1229         if (rc == -EDQUOT || rc == -EBUSY) {
1230                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1231                 rc = 0;
1232         }
1233         if (rc) {
1234                 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1235                 GOTO(out, rc);
1236         }
1237
1238         EXIT;
1239 out:
1240         OBD_FREE_PTR(ioqc);
1241         return rc;
1242 }
1243
1244 static void adjust_lqs(struct obd_device *obd, struct quota_adjust_qunit *qaq)
1245 {
1246         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
1247         int rc = 0;
1248
1249         QAQ_SET_CREATE_LQS(qaq);
1250         /* adjust local lqs */
1251         rc = quota_adjust_slave_lqs(qaq, qctxt);
1252         if (rc < 0)
1253                 CERROR("adjust master's qunit size failed!(rc=%d)\n", rc);
1254
1255         /* adjust remote lqs */
1256         if (QAQ_IS_ADJBLK(qaq)) {
1257                 rc = obd_quota_adjust_qunit(obd->u.mds.mds_lov_exp, qaq, qctxt);
1258                 if (rc < 0)
1259                         CERROR("adjust slaves' qunit size failed!(rc=%d)\n", rc);
1260
1261         }
1262 }
1263
1264 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1265 {
1266         struct mds_obd *mds = &obd->u.mds;
1267         struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1268         struct obd_device *lov_obd = class_exp2obd(mds->mds_lov_exp);
1269         struct lov_obd *lov = &lov_obd->u.lov;
1270         struct quota_adjust_qunit *oqaq = NULL;
1271         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1272         __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1273         time_t btime, itime;
1274         struct lustre_dquot *dquot;
1275         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1276         /* orig_set means if quota was set before; now_set means we are
1277          * setting/cancelling quota */
1278         int orig_set, now_set;
1279         int rc, rc2 = 0, flag = 0;
1280         ENTRY;
1281
1282         if (oqctl->qc_type != USRQUOTA &&
1283             oqctl->qc_type != GRPQUOTA)
1284                 RETURN(-EINVAL);
1285
1286         OBD_ALLOC_PTR(oqaq);
1287         if (!oqaq)
1288                 RETURN(-ENOMEM);
1289
1290         cfs_down_write(&mds->mds_qonoff_sem);
1291         init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1292
1293         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1294                 CWARN("quota[%u] is off\n", oqctl->qc_type);
1295                 GOTO(out_sem, rc = -ESRCH);
1296         }
1297
1298         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1299         if (IS_ERR(dquot))
1300                 GOTO(out_sem, rc = PTR_ERR(dquot));
1301         DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1302         QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1303
1304         cfs_down(&dquot->dq_sem);
1305
1306         if (dquot->dq_status) {
1307                 cfs_up(&dquot->dq_sem);
1308                 lustre_dqput(dquot);
1309                 GOTO(out_sem, rc = -EBUSY);
1310         }
1311         dquot->dq_status |= DQ_STATUS_SET;
1312
1313         ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1314         isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1315         bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1316         bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1317         btime = dquot->dq_dqb.dqb_btime;
1318         itime = dquot->dq_dqb.dqb_itime;
1319
1320         if (dqblk->dqb_valid & QIF_BTIME)
1321                 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1322         if (dqblk->dqb_valid & QIF_ITIME)
1323                 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1324
1325         if (dqblk->dqb_valid & QIF_BLIMITS) {
1326                 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1327                 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1328                 /* clear usage (limit pool) */
1329                 if (!dquot->dq_dqb.dqb_bhardlimit &&
1330                     !dquot->dq_dqb.dqb_bsoftlimit)
1331                         dquot->dq_dqb.dqb_curspace = 0;
1332
1333                 /* clear grace time */
1334                 if (!dqblk->dqb_bsoftlimit ||
1335                     toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1336                         dquot->dq_dqb.dqb_btime = 0;
1337                 /* set grace only if user hasn't provided his own */
1338                 else if (!(dqblk->dqb_valid & QIF_BTIME))
1339                         dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1340                                 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1341
1342                 flag |= LQUOTA_FLAGS_ADJBLK;
1343         }
1344
1345         if (dqblk->dqb_valid & QIF_ILIMITS) {
1346                 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1347                 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1348                 /* clear usage (limit pool) */
1349                 if (!dquot->dq_dqb.dqb_ihardlimit &&
1350                     !dquot->dq_dqb.dqb_isoftlimit)
1351                         dquot->dq_dqb.dqb_curinodes = 0;
1352
1353                 if (!dqblk->dqb_isoftlimit ||
1354                     dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1355                         dquot->dq_dqb.dqb_itime = 0;
1356                 else if (!(dqblk->dqb_valid & QIF_ITIME))
1357                         dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1358                                 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1359
1360                 flag |= LQUOTA_FLAGS_ADJINO;
1361         }
1362         QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1363         rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1364                                flag, oqaq);
1365         QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1366         if (rc < 0)
1367                 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1368
1369
1370         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1371
1372         cfs_up(&dquot->dq_sem);
1373
1374         if (rc) {
1375                 CERROR("set limit failed! (rc:%d)\n", rc);
1376                 goto out;
1377         }
1378
1379         cfs_up_write(&mds->mds_qonoff_sem);
1380         adjust_lqs(obd, oqaq);
1381
1382         orig_set = ihardlimit || isoftlimit;
1383         now_set  = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1384         if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1385                 cfs_down(&dquot->dq_sem);
1386                 dquot->dq_dqb.dqb_curinodes = 0;
1387                 cfs_up(&dquot->dq_sem);
1388                 rc = mds_init_slave_ilimits(obd, oqctl, orig_set);
1389                 if (rc) {
1390                         CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1391                         goto revoke_out;
1392                 }
1393         }
1394
1395         orig_set = bhardlimit || bsoftlimit;
1396         now_set  = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1397         if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1398                 cfs_down(&dquot->dq_sem);
1399                 dquot->dq_dqb.dqb_curspace = 0;
1400                 cfs_up(&dquot->dq_sem);
1401                 rc = mds_init_slave_blimits(obd, oqctl, orig_set);
1402                 if (rc) {
1403                         CERROR("init slave blimits failed! (rc:%d)\n", rc);
1404                         goto revoke_out;
1405                 }
1406         }
1407
1408 revoke_out:
1409         cfs_down_write(&mds->mds_qonoff_sem);
1410         cfs_down(&dquot->dq_sem);
1411         if (rc) {
1412                 /* cancel previous setting */
1413                 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1414                 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1415                 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1416                 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1417                 dquot->dq_dqb.dqb_btime = btime;
1418                 dquot->dq_dqb.dqb_itime = itime;
1419         }
1420         rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1421         cfs_up(&dquot->dq_sem);
1422
1423 out:
1424         cfs_down(&dquot->dq_sem);
1425         dquot->dq_status &= ~DQ_STATUS_SET;
1426         cfs_up(&dquot->dq_sem);
1427         lustre_dqput(dquot);
1428         EXIT;
1429 out_sem:
1430         cfs_up_write(&mds->mds_qonoff_sem);
1431
1432         if (oqaq)
1433                 OBD_FREE_PTR(oqaq);
1434
1435         return rc ? rc : rc2;
1436 }
1437
1438 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1439 {
1440         struct obd_quotactl *soqc;
1441         struct lvfs_run_ctxt saved;
1442         int rc, rc1;
1443         ENTRY;
1444
1445         OBD_ALLOC_PTR(soqc);
1446         if (!soqc)
1447                 RETURN(-ENOMEM);
1448
1449         soqc->qc_cmd = Q_GETOQUOTA;
1450         soqc->qc_id = oqctl->qc_id;
1451         soqc->qc_type = oqctl->qc_type;
1452
1453         /* get block usage from OSS */
1454         soqc->qc_dqblk.dqb_curspace = 0;
1455         rc = obd_quotactl(obd->u.mds.mds_lov_exp, soqc);
1456         if (!rc || rc == -EREMOTEIO) {
1457                 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1458                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1459         }
1460
1461         /* get block/inode usage from MDS */
1462         soqc->qc_dqblk.dqb_curspace = 0;
1463         soqc->qc_dqblk.dqb_curinodes = 0;
1464         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1465         rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1466         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1467         if (!rc1) {
1468                 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1469                 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1470                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1471         }
1472
1473         OBD_FREE_PTR(soqc);
1474
1475         RETURN(rc ? : rc1);
1476 }
1477
1478 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1479 {
1480         struct mds_obd *mds = &obd->u.mds;
1481         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1482         struct lustre_dquot *dquot;
1483         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1484         int rc;
1485         ENTRY;
1486
1487         if (oqctl->qc_type != USRQUOTA &&
1488             oqctl->qc_type != GRPQUOTA)
1489                 RETURN(-EINVAL);
1490
1491         cfs_down_read(&mds->mds_qonoff_sem);
1492         dqblk->dqb_valid = 0;
1493         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
1494                 CWARN("quota[%u] is off\n", oqctl->qc_type);
1495                 GOTO(out, rc = -ESRCH);
1496         }
1497
1498         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1499         if (IS_ERR(dquot))
1500                 GOTO(out, rc = PTR_ERR(dquot));
1501
1502         cfs_down(&dquot->dq_sem);
1503         dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1504         dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1505         dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1506         dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1507         dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1508         dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1509         dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1510         cfs_up(&dquot->dq_sem);
1511
1512         lustre_dqput(dquot);
1513         cfs_up_read(&mds->mds_qonoff_sem);
1514
1515         /* the usages in admin quota file is inaccurate */
1516         dqblk->dqb_curinodes = 0;
1517         dqblk->dqb_curspace = 0;
1518         rc = mds_get_space(obd, oqctl);
1519
1520         /*
1521          * Querying of curinodes and/or curspace may have failed, administrative
1522          * quota data are likely to be better approximation to the real usage in
1523          * this case.
1524          */
1525         if (!(dqblk->dqb_valid & QIF_INODES) && dquot->dq_dqb.dqb_curinodes > 0)
1526                 dqblk->dqb_curinodes = dquot->dq_dqb.dqb_curinodes;
1527
1528         if (!(dqblk->dqb_valid & QIF_SPACE) && dquot->dq_dqb.dqb_curspace > 0)
1529                 dqblk->dqb_curspace = dquot->dq_dqb.dqb_curspace;
1530
1531         RETURN(rc);
1532
1533 out:
1534         cfs_up_read(&mds->mds_qonoff_sem);
1535         return rc;
1536 }
1537
1538 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1539 {
1540         struct lvfs_run_ctxt saved;
1541         int rc;
1542         ENTRY;
1543
1544         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1545         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1546         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1547
1548         RETURN(rc);
1549 }
1550
1551
1552 /* FIXME we only recovery block limit by now, need recovery inode
1553  * limits also after CMD involved in */
1554 static int 
1555 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1556 {
1557         struct mds_obd *mds = &obd->u.mds;
1558         struct lustre_quota_info *qinfo= &mds->mds_quota_info;
1559         struct lustre_dquot *dquot;
1560         struct obd_quotactl *qctl;
1561         __u64 total_limits = 0;
1562         int rc;
1563         ENTRY;
1564
1565         OBD_ALLOC_PTR(qctl);
1566         if (qctl == NULL)
1567                 RETURN(-ENOMEM);
1568
1569         dquot = lustre_dqget(obd, qinfo, id, type);
1570         if (IS_ERR(dquot)) {
1571                 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1572                 OBD_FREE_PTR(qctl);
1573                 RETURN(PTR_ERR(dquot));
1574         }
1575
1576         cfs_down(&dquot->dq_sem);
1577
1578         /* don't recovery the dquot without limits or under setting */
1579         if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1580             dquot->dq_status)
1581                 GOTO(skip, rc = 0);
1582         dquot->dq_status |= DQ_STATUS_RECOVERY;
1583
1584         cfs_up(&dquot->dq_sem);
1585
1586         /* release mds_qonoff_sem during obd_quotactl ops here */
1587         cfs_up_write(&mds->mds_qonoff_sem);
1588
1589         /* get real bhardlimit from all slaves. */
1590         qctl->qc_cmd = Q_GETOQUOTA;
1591         qctl->qc_type = type;
1592         qctl->qc_id = id;
1593         qctl->qc_stat = QUOTA_RECOVERING;
1594         rc = obd_quotactl(mds->mds_lov_exp, qctl);
1595         cfs_down_write(&mds->mds_qonoff_sem);
1596         if (rc)
1597                 GOTO(out, rc);
1598         total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1599
1600         /* get real bhardlimit from master */
1601         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1602         if (rc)
1603                 GOTO(out, rc);
1604         total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1605
1606         /* amend the usage of the administrative quotafile */
1607         cfs_down(&dquot->dq_sem);
1608
1609         dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1610
1611         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1612         if (rc)
1613                 CERROR("write dquot failed! (rc:%d)\n", rc);
1614
1615         cfs_up(&dquot->dq_sem);
1616         EXIT;
1617 out:
1618         cfs_down(&dquot->dq_sem);
1619         dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1620 skip:
1621         cfs_up(&dquot->dq_sem);
1622
1623         lustre_dqput(dquot);
1624         OBD_FREE_PTR(qctl);
1625         return rc;
1626 }
1627
1628 struct qmaster_recov_thread_data {
1629         struct obd_device *obd;
1630         cfs_completion_t comp;
1631 };
1632
1633 static int qmaster_recovery_main(void *arg)
1634 {
1635         struct qmaster_recov_thread_data *data = arg;
1636         struct obd_device *obd = data->obd;
1637         struct mds_obd *mds = &obd->u.mds;
1638         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1639         int rc = 0;
1640         unsigned short type;
1641         ENTRY;
1642
1643         cfs_daemonize_ctxt("qmaster_recovd");
1644
1645         /* for mds */
1646         class_incref(obd, "qmaster_recovd_mds", obd);
1647         /* for lov */
1648         class_incref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1649
1650         cfs_complete(&data->comp);
1651
1652         cfs_down_write(&mds->mds_qonoff_sem);
1653         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1654                 cfs_list_t id_list;
1655                 struct dquot_id *dqid, *tmp;
1656
1657                 if (qinfo->qi_files[type] == NULL)
1658                         continue;
1659
1660                 CFS_INIT_LIST_HEAD(&id_list);
1661                 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1662                                  &id_list);
1663                 if (rc)
1664                         CERROR("error get ids from admin quotafile.(%d)\n", rc);
1665
1666                 cfs_list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1667                         cfs_list_del_init(&dqid->di_link);
1668                         if (rc)
1669                                 goto free;
1670
1671                         rc = dquot_recovery(obd, dqid->di_id, type);
1672                         if (rc)
1673                                 CERROR("%s: qmaster recovery failed for %sid %d"
1674                                        " rc:%d)\n", obd->obd_name,
1675                                        type ? "g" : "u", dqid->di_id, rc);
1676 free:
1677                         OBD_FREE_PTR(dqid);
1678                 }
1679         }
1680         cfs_up_write(&mds->mds_qonoff_sem);
1681         class_decref(mds->mds_lov_obd, "qmaster_recovd_lov", mds->mds_lov_obd);
1682         class_decref(obd, "qmaster_recovd_mds", obd);
1683         RETURN(rc);
1684 }
1685
1686 int mds_quota_recovery(struct obd_device *obd)
1687 {
1688         struct mds_obd *mds = &obd->u.mds;
1689         struct qmaster_recov_thread_data data;
1690         int rc = 0;
1691         ENTRY;
1692
1693         if (unlikely(!mds->mds_quota || obd->obd_stopping))
1694                 RETURN(rc);
1695
1696         cfs_mutex_down(&obd->obd_dev_sem);
1697         if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1698                 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1699                       mds->mds_lov_desc.ld_active_tgt_count,
1700                       mds->mds_lov_objid_count);
1701                 cfs_mutex_up(&obd->obd_dev_sem);
1702                 RETURN(rc);
1703         }
1704         cfs_mutex_up(&obd->obd_dev_sem);
1705
1706         data.obd = obd;
1707         cfs_init_completion(&data.comp);
1708
1709         rc = cfs_kernel_thread(qmaster_recovery_main, &data,
1710                                CLONE_VM|CLONE_FILES);
1711         if (rc < 0)
1712                 CERROR("%s: cannot start quota recovery thread: rc %d\n",
1713                        obd->obd_name, rc);
1714
1715         cfs_wait_for_completion(&data.comp);
1716         RETURN(rc);
1717 }
1718
1719 #endif /* HAVE_QUOTA_SUPPORT */