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