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