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