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