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