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