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         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 uid = 0, gid = 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                 gid = dquot->dq_id;
309         else
310                 uid = dquot->dq_id;
311
312         up(&dquot->dq_sem);
313
314         rc = qctxt_adjust_qunit(obd, qctxt, uid, gid, 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, unsigned int qcids[],
475                      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], qpids[1], 0, 0,
488                                           NULL);
489                 /* release block quota on original owner */
490                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
491                                           NULL);
492                 /* acquire file quota on current owner */
493                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
494                                           NULL);
495                 /* acquire block quota on current owner */
496                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
497                                           NULL);
498                 break;
499         case FSFILT_OP_UNLINK_PARTIAL_CHILD:
500                 /* release file quota on child */
501                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
502                                           NULL);
503                 /* rlease block quota on child */
504                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
505                                           NULL);
506                 break;
507         case FSFILT_OP_CREATE_PARTIAL_CHILD:
508                 /* acquire file quota on child */
509                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
510                                           NULL);
511                 /* acquire block quota on child */
512                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
513                                           NULL);
514                 break;
515         case FSFILT_OP_LINK:
516                 /* acquire block quota on parent */
517                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
518                                           NULL);
519                 break;
520         case FSFILT_OP_UNLINK:
521                 /* release block quota on parent */
522                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
523                                           NULL);
524                 /* release file quota on child */
525                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
526                                           NULL);
527                 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
528                         /* release block quota on child */
529                         rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0],
530                                                   qcids[1], 1, 0, NULL);
531                 break;
532         case FSFILT_OP_UNLINK_PARTIAL_PARENT:
533                 /* release block quota on parent */
534                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
535                                           NULL);
536                 break;
537         case FSFILT_OP_CREATE:
538                 /* acquire block quota on parent */
539                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
540                                           NULL);
541                 /* acquire file quota on child */
542                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0,
543                                           NULL);
544                 if (qpids[0] != qcids[0] || qpids[1] != qcids[1])
545                         /* acquire block quota on child */
546                         rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0],
547                                                   qcids[1], 1, 0, NULL);
548                 break;
549         default:
550                 LBUG();
551                 break;
552         }
553
554         if (rc2)
555                 CDEBUG(rc2 == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
556                        "mds adjust qunit %ssuccessfully! (opc:%d rc:%d)\n",
557                        rc2 == QUOTA_REQ_RETURNED ? "" : "un", opc, rc2);
558         RETURN(0);
559 }
560
561 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
562                         unsigned int qpids[], int rc, int opc)
563 {
564         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
565         int rc2 = 0;
566         ENTRY;
567
568         if (rc && rc != -EDQUOT)
569                 RETURN(0);
570
571         switch (opc) {
572         case FSFILT_OP_SETATTR:
573                 /* acquire/release block quota on original & current owner */
574                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
575                                         NULL);
576                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0,
577                                          NULL);
578                 break;
579         case FSFILT_OP_UNLINK:
580                 /* release block quota on this owner */
581         case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
582                 /* acquire block quota on this owner */
583                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0,
584                                         NULL);
585                 break;
586         default:
587                 LBUG();
588                 break;
589         }
590
591         if (rc || rc2) {
592                 if (!rc)
593                         rc = rc2;
594                 CDEBUG(rc == QUOTA_REQ_RETURNED ? D_QUOTA: D_ERROR,
595                        "filter adjust qunit %ssuccessfully! (opc:%d rc%d)\n",
596                        QUOTA_REQ_RETURNED ? "" : "un", opc, rc);
597         }
598
599         RETURN(0);
600 }
601
602 static const char prefix[] = "OBJECTS/";
603
604 int mds_quota_invalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
605 {
606         struct mds_obd *mds = &obd->u.mds;
607         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
608         int rc = 0, i;
609         char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
610         char name[64];
611         struct lvfs_run_ctxt saved;
612
613         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
614
615         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
616
617         down(&mds->mds_qonoff_sem);
618
619         for (i = 0; i < MAXQUOTAS; i++) {
620                 struct file *fp;
621
622                 if (!Q_TYPESET(oqctl, i))
623                         continue;
624
625                 /* quota file has been opened ? */
626                 if (qinfo->qi_files[i]) {
627                         rc = -EBUSY;
628                         goto out;
629                 }
630
631                 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
632                 sprintf(name, "%s%s", prefix, quotafile[i]);
633
634                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
635                 if (IS_ERR(fp)) {
636                         rc = PTR_ERR(fp);
637                         CERROR("error invalidating admin quotafile %s (rc:%d)\n",
638                                name, rc);
639                 }
640                 else
641                         filp_close(fp, 0);
642         }
643
644 out:
645         up(&mds->mds_qonoff_sem);
646
647         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
648
649         return rc;
650 }
651
652 int mds_quota_finvalidate(struct obd_device *obd, struct obd_quotactl *oqctl)
653 {
654         struct mds_obd *mds = &obd->u.mds;
655         int rc;
656         struct lvfs_run_ctxt saved;
657
658         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
659         down(&mds->mds_qonoff_sem);
660
661         oqctl->qc_cmd = Q_FINVALIDATE;
662         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
663         if (!rc)
664                 rc = obd_quotactl(mds->mds_osc_exp, oqctl);
665
666         up(&mds->mds_qonoff_sem);
667         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
668
669         return rc;
670 }
671
672 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
673 {
674         struct mds_obd *mds = &obd->u.mds;
675         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
676         const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
677         struct lvfs_run_ctxt saved;
678         char name[64];
679         int i, rc = 0;
680         ENTRY;
681
682         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
683
684         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
685
686         down(&mds->mds_qonoff_sem);
687
688         for (i = 0; i < MAXQUOTAS && !rc; i++) {
689                 struct file *fp;
690
691                 if (!Q_TYPESET(oqctl, i))
692                         continue;
693
694                 /* quota file has been opened ? */
695                 if (qinfo->qi_files[i]) {
696                         CWARN("init %s admin quotafile while quota on.\n",
697                               i == USRQUOTA ? "user" : "group");
698                         continue;
699                 }
700
701                 LASSERT(strlen(quotafile[i]) + sizeof(prefix) <= sizeof(name));
702                 sprintf(name, "%s%s", prefix, quotafile[i]);
703
704                 /* check if quota file exists and is correct */
705                 fp = filp_open(name, O_RDONLY, 0);
706                 if (!IS_ERR(fp)) {
707                         /* irregular file is not the right place for quota */
708                         if (!S_ISREG(fp->f_dentry->d_inode->i_mode)) {
709                                 CERROR("admin quota file %s is not "
710                                        "regular!", name);
711                                 filp_close(fp, 0);
712                                 rc = -EINVAL;
713                                 break;
714                         }
715                         qinfo->qi_files[i] = fp;
716                         rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
717                         qinfo->qi_files[i] = 0;
718                         filp_close(fp, 0);
719                 }
720                 else
721                         rc = PTR_ERR(fp);
722
723                 if (!rc)
724                         continue;
725
726                 /* -EINVAL may be returned by quotainfo for bad quota file */
727                 if (rc != -ENOENT && rc != -EINVAL) {
728                         CERROR("error opening old quota file %s (%d)\n",
729                                name, rc);
730                         break;
731                 }
732
733                 CDEBUG(D_INFO, "%s new quota file %s\n", name,
734                        rc == -ENOENT ? "creating" : "overwriting");
735
736                 /* create quota file overwriting old if needed */
737                 fp = filp_open(name, O_CREAT | O_TRUNC | O_RDWR, 0644);
738                 if (IS_ERR(fp)) {
739                         rc = PTR_ERR(fp);
740                         CERROR("error creating admin quotafile %s (rc:%d)\n",
741                                name, rc);
742                         break;
743                 }
744
745                 qinfo->qi_files[i] = fp;
746
747                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
748                 if (rc)
749                         CERROR("error init %s admin quotafile! (rc:%d)\n",
750                                i == USRQUOTA ? "user" : "group", rc);
751
752                 filp_close(fp, 0);
753                 qinfo->qi_files[i] = NULL;
754         }
755         up(&mds->mds_qonoff_sem);
756
757         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
758         RETURN(rc);
759 }
760
761 static int close_quota_files(struct obd_quotactl *oqctl,
762                              struct lustre_quota_info *qinfo)
763 {
764         int i, rc = 0;
765         ENTRY;
766
767         for (i = 0; i < MAXQUOTAS; i++) {
768                 if (!Q_TYPESET(oqctl, i))
769                         continue;
770                 if (qinfo->qi_files[i] == NULL) {
771                         rc = -ESRCH;
772                         continue;
773                 }
774                 filp_close(qinfo->qi_files[i], 0);
775                 qinfo->qi_files[i] = NULL;
776         }
777         RETURN(rc);
778 }
779
780 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
781 {
782         struct mds_obd *mds = &obd->u.mds;
783         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
784         const char *quotafile[] = LUSTRE_ADMIN_QUOTAFILES_V2;
785         char name[64];
786         int i, rc = 0;
787         ENTRY;
788
789         LASSERT(qinfo->qi_version == LUSTRE_QUOTA_V2);
790
791         /* open admin quota files and read quotafile info */
792         for (i = 0; i < MAXQUOTAS; i++) {
793                 struct file *fp;
794
795                 if (!Q_TYPESET(oqctl, i))
796                         continue;
797
798                 LASSERT(strlen(quotafile[i])
799                         + sizeof(prefix) <= sizeof(name));
800                 sprintf(name, "%s%s", prefix, quotafile[i]);
801
802                 if (qinfo->qi_files[i] != NULL) {
803                         rc = -EBUSY;
804                         break;
805                 }
806
807                 fp = filp_open(name, O_RDWR, 0);
808                 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
809                         rc = IS_ERR(fp) ? PTR_ERR(fp) : -EINVAL;
810                         CERROR("error open/create %s! (rc:%d)\n", name, rc);
811                         break;
812                 }
813                 qinfo->qi_files[i] = fp;
814
815                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_CHK);
816                 if (rc) {
817                         CERROR("invalid quota file %s! (rc:%d)\n", name, rc);
818                         break;
819                 }
820
821                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
822                 if (rc) {
823                         CERROR("error read quotainfo of %s! (rc:%d)\n", name,
824                                rc);
825                         break;
826                 }
827         }
828
829         if (rc && rc != -EBUSY)
830                 close_quota_files(oqctl, qinfo);
831
832         RETURN(rc);
833 }
834
835 int mds_admin_quota_off(struct obd_device *obd,
836                         struct obd_quotactl *oqctl)
837 {
838         struct mds_obd *mds = &obd->u.mds;
839         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
840         int rc;
841         ENTRY;
842
843         /* close admin quota files */
844         rc = close_quota_files(oqctl, qinfo);
845         RETURN(rc);
846 }
847
848 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
849 {
850         struct mds_obd *mds = &obd->u.mds;
851         struct obd_device_target *obt = &obd->u.obt;
852         struct lvfs_run_ctxt saved;
853         int rc;
854         ENTRY;
855
856         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
857                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
858                 atomic_inc(&obt->obt_quotachecking);
859                 RETURN(-EBUSY);
860         }
861
862         down(&mds->mds_qonoff_sem);
863         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
864         rc = mds_admin_quota_on(obd, oqctl);
865         if (rc)
866                 GOTO(out, rc);
867
868         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
869         if (!rc)
870                 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
871         else
872                 GOTO(out, rc);
873
874         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
875
876 out:
877         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
878         up(&mds->mds_qonoff_sem);
879         atomic_inc(&obt->obt_quotachecking);
880         RETURN(rc);
881 }
882
883 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
884 {
885         struct mds_obd *mds = &obd->u.mds;
886         struct obd_device_target *obt = &obd->u.obt;
887         struct lvfs_run_ctxt saved;
888         int rc, rc2;
889         ENTRY;
890
891         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
892                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
893                 atomic_inc(&obt->obt_quotachecking);
894                 RETURN(-EBUSY);
895         }
896
897         down(&mds->mds_qonoff_sem);
898         /* close admin quota files */
899         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
900         mds_admin_quota_off(obd, oqctl);
901
902         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
903         rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
904         if (!rc2)
905                 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
906
907         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
908         up(&mds->mds_qonoff_sem);
909         atomic_inc(&obt->obt_quotachecking);
910
911         RETURN(rc ?: rc2);
912 }
913
914 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
915 {
916         struct mds_obd *mds = &obd->u.mds;
917         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
918         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
919         int rc;
920         ENTRY;
921
922         down(&mds->mds_qonoff_sem);
923         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
924                 rc = -ESRCH;
925                 goto out;
926         }
927
928         qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
929         qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
930         qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
931
932         rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
933
934 out:
935         up(&mds->mds_qonoff_sem);
936         RETURN(rc);
937 }
938
939 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
940 {
941         struct mds_obd *mds = &obd->u.mds;
942         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
943         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
944         int rc = 0;
945         ENTRY;
946
947         down(&mds->mds_qonoff_sem);
948         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
949                 rc = -ESRCH;
950                 goto out;
951         }
952
953         dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
954         dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
955         dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
956
957 out:
958         up(&mds->mds_qonoff_sem);
959         RETURN(rc);
960 }
961
962 int dquot_create_oqaq(struct lustre_quota_ctxt *qctxt,
963                       struct lustre_dquot *dquot, __u32 ost_num, __u32 mdt_num,
964                       int type, struct quota_adjust_qunit *oqaq)
965 {
966         __u64 bunit_curr_o, iunit_curr_o;
967         unsigned long shrink_qunit_limit = qctxt->lqc_cqs_boundary_factor;
968         unsigned long cqs_factor = qctxt->lqc_cqs_qs_factor;
969         __u64 blimit = dquot->dq_dqb.dqb_bhardlimit ?
970                 dquot->dq_dqb.dqb_bhardlimit : dquot->dq_dqb.dqb_bsoftlimit;
971         __u64 ilimit = dquot->dq_dqb.dqb_ihardlimit ?
972                 dquot->dq_dqb.dqb_ihardlimit : dquot->dq_dqb.dqb_isoftlimit;
973         int rc = 0;
974         ENTRY;
975
976         if (!dquot || !oqaq)
977                 RETURN(-EINVAL);
978         LASSERT_SEM_LOCKED(&dquot->dq_sem);
979         LASSERT(oqaq->qaq_iunit_sz);
980         LASSERT(oqaq->qaq_bunit_sz);
981
982         /* don't change qunit size */
983         if (!qctxt->lqc_switch_qs)
984                 RETURN(rc);
985
986         bunit_curr_o = oqaq->qaq_bunit_sz;
987         iunit_curr_o = oqaq->qaq_iunit_sz;
988
989         if (dquot->dq_type == GRPQUOTA)
990                 QAQ_SET_GRP(oqaq);
991
992         if ((type & LQUOTA_FLAGS_ADJBLK) && blimit) {
993                 __u64 b_limitation =
994                         oqaq->qaq_bunit_sz * ost_num * shrink_qunit_limit;
995                 /* enlarge block qunit size */
996                 while (blimit >
997                        QUSG(dquot->dq_dqb.dqb_curspace + 2 * b_limitation, 1)) {
998                         oqaq->qaq_bunit_sz =
999                                 QUSG(oqaq->qaq_bunit_sz * cqs_factor, 1)
1000                                 << QUOTABLOCK_BITS;
1001                         b_limitation = oqaq->qaq_bunit_sz * ost_num *
1002                                 shrink_qunit_limit;
1003                 }
1004
1005                 if (oqaq->qaq_bunit_sz > qctxt->lqc_bunit_sz)
1006                         oqaq->qaq_bunit_sz = qctxt->lqc_bunit_sz;
1007
1008                 /* shrink block qunit size */
1009                 while (blimit <
1010                        QUSG(dquot->dq_dqb.dqb_curspace + b_limitation, 1)) {
1011                         do_div(oqaq->qaq_bunit_sz , cqs_factor);
1012                         oqaq->qaq_bunit_sz = QUSG(oqaq->qaq_bunit_sz, 1) <<
1013                                 QUOTABLOCK_BITS;
1014                         b_limitation = oqaq->qaq_bunit_sz * ost_num *
1015                                 shrink_qunit_limit;
1016                         if (oqaq->qaq_bunit_sz <  qctxt->lqc_cqs_least_bunit)
1017                                 break;
1018                 }
1019
1020                 if (oqaq->qaq_bunit_sz < qctxt->lqc_cqs_least_bunit)
1021                         oqaq->qaq_bunit_sz = qctxt->lqc_cqs_least_bunit;
1022
1023                 if (bunit_curr_o != oqaq->qaq_bunit_sz)
1024                         QAQ_SET_ADJBLK(oqaq);
1025
1026         }
1027
1028         if ((type & LQUOTA_FLAGS_ADJINO) && ilimit) {
1029                 __u64 i_limitation =
1030                         oqaq->qaq_iunit_sz * mdt_num * shrink_qunit_limit;
1031                 /* enlarge file qunit size */
1032                 while (ilimit > dquot->dq_dqb.dqb_curinodes
1033                        + 2 * i_limitation) {
1034                         oqaq->qaq_iunit_sz = oqaq->qaq_iunit_sz * cqs_factor;
1035                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1036                                 shrink_qunit_limit;
1037                 }
1038
1039                 if (oqaq->qaq_iunit_sz > qctxt->lqc_iunit_sz)
1040                         oqaq->qaq_iunit_sz = qctxt->lqc_iunit_sz;
1041
1042                 /* shrink file qunit size */
1043                 while (ilimit < dquot->dq_dqb.dqb_curinodes
1044                        + i_limitation) {
1045                         do_div(oqaq->qaq_iunit_sz, cqs_factor);
1046                         i_limitation = oqaq->qaq_iunit_sz * mdt_num *
1047                                        shrink_qunit_limit;
1048                         if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1049                                 break;
1050                 }
1051
1052                 if (oqaq->qaq_iunit_sz < qctxt->lqc_cqs_least_iunit)
1053                         oqaq->qaq_iunit_sz = qctxt->lqc_cqs_least_iunit;
1054
1055                 if (iunit_curr_o != oqaq->qaq_iunit_sz)
1056                         QAQ_SET_ADJINO(oqaq);
1057
1058         }
1059
1060         if (!dquot->dq_dqb.dqb_bhardlimit && !dquot->dq_dqb.dqb_bsoftlimit &&
1061             !dquot->dq_dqb.dqb_ihardlimit && !dquot->dq_dqb.dqb_isoftlimit) {
1062                 oqaq->qaq_bunit_sz = 0;
1063                 oqaq->qaq_iunit_sz = 0;
1064                 QAQ_SET_ADJBLK(oqaq);
1065                 QAQ_SET_ADJINO(oqaq);
1066         }
1067
1068         QAQ_DEBUG(oqaq, "the oqaq computed\n");
1069
1070         RETURN(rc);
1071 }
1072
1073 static int mds_init_slave_ilimits(struct obd_device *obd,
1074                                   struct obd_quotactl *oqctl, int set,
1075                                   struct quota_adjust_qunit *oqaq)
1076 {
1077         /* XXX: for file limits only adjust local now */
1078         struct obd_device_target *obt = &obd->u.obt;
1079         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1080         unsigned int uid = 0, gid = 0;
1081         struct obd_quotactl *ioqc = NULL;
1082         int flag;
1083         int rc;
1084         ENTRY;
1085
1086         /* if we are going to set zero limit, needn't init slaves */
1087         if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
1088             !set)
1089                 RETURN(0);
1090
1091         OBD_ALLOC_PTR(ioqc);
1092         if (!ioqc)
1093                 RETURN(-ENOMEM);
1094
1095         flag = oqctl->qc_dqblk.dqb_ihardlimit ||
1096                oqctl->qc_dqblk.dqb_isoftlimit || !set;
1097         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1098         ioqc->qc_id = oqctl->qc_id;
1099         ioqc->qc_type = oqctl->qc_type;
1100         ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
1101         ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
1102
1103         if (QAQ_IS_ADJINO(oqaq)) {
1104                 /* adjust the mds slave's inode qunit size */
1105                 rc = quota_adjust_slave_lqs(oqaq, qctxt);
1106                 if (rc < 0)
1107                         CDEBUG(D_ERROR, "adjust mds slave's inode qunit size \
1108                                failed! (rc:%d)\n", rc);
1109         }
1110
1111         /* set local limit to MIN_QLIMIT */
1112         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1113         if (rc)
1114                 GOTO(out, rc);
1115
1116         /* trigger local qunit pre-acquire */
1117         if (oqctl->qc_type == USRQUOTA)
1118                 uid = oqctl->qc_id;
1119         else
1120                 gid = oqctl->qc_id;
1121
1122         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0,
1123                                 NULL);
1124         if (rc == -EDQUOT || rc == -EBUSY) {
1125                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1126                 rc = 0;
1127         }
1128         if (rc) {
1129                 CDEBUG(D_QUOTA,"error mds adjust local file quota! (rc:%d)\n",
1130                        rc);
1131                 GOTO(out, rc);
1132         }
1133         /* FIXME initialize all slaves in CMD */
1134         EXIT;
1135 out:
1136         if (ioqc)
1137                 OBD_FREE_PTR(ioqc);
1138         return rc;
1139 }
1140
1141 static int mds_init_slave_blimits(struct obd_device *obd,
1142                                   struct obd_quotactl *oqctl, int set,
1143                                   struct quota_adjust_qunit *oqaq)
1144 {
1145         struct obd_device_target *obt = &obd->u.obt;
1146         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
1147         struct mds_obd *mds = &obd->u.mds;
1148         struct obd_quotactl *ioqc;
1149         unsigned int uid = 0, gid = 0;
1150         int rc, rc1 = 0;
1151         int flag;
1152         ENTRY;
1153
1154         /* if we are going to set zero limit, needn't init slaves */
1155         if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
1156             !set)
1157                 RETURN(0);
1158
1159         OBD_ALLOC_PTR(ioqc);
1160         if (!ioqc)
1161                 RETURN(-ENOMEM);
1162
1163         flag = oqctl->qc_dqblk.dqb_bhardlimit ||
1164                oqctl->qc_dqblk.dqb_bsoftlimit || !set;
1165         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
1166         ioqc->qc_id = oqctl->qc_id;
1167         ioqc->qc_type = oqctl->qc_type;
1168         ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
1169         ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
1170         if (QAQ_IS_ADJBLK(oqaq)) {
1171                 /* adjust the mds slave's block qunit size */
1172                 rc1 = quota_adjust_slave_lqs(oqaq, qctxt);
1173                 if (rc1 < 0)
1174                         CERROR("adjust mds slave's block qunit size failed!"
1175                                "(rc:%d)\n", rc1);
1176         }
1177
1178         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
1179         if (rc)
1180                 GOTO(out, rc);
1181
1182         /* trigger local qunit pre-acquire */
1183         if (oqctl->qc_type == USRQUOTA)
1184                 uid = oqctl->qc_id;
1185         else
1186                 gid = oqctl->qc_id;
1187
1188         /* initialize all slave's limit */
1189         rc = obd_quotactl(mds->mds_osc_exp, ioqc);
1190
1191         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0,
1192                                 NULL);
1193         if (rc == -EDQUOT || rc == -EBUSY) {
1194                 CDEBUG(D_QUOTA, "rc: %d.\n", rc);
1195                 rc = 0;
1196         }
1197         if (rc) {
1198                 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
1199                 GOTO(out, rc);
1200         }
1201
1202         /* adjust all slave's qunit size when setting quota
1203          * this is will create a lqs for every ost, which will present
1204          * certain uid/gid is set quota or not */
1205         QAQ_SET_ADJBLK(oqaq);
1206         rc = obd_quota_adjust_qunit(mds->mds_osc_exp, oqaq, qctxt);
1207
1208         EXIT;
1209 out:
1210         OBD_FREE_PTR(ioqc);
1211         return rc;
1212 }
1213
1214 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1215 {
1216         struct mds_obd *mds = &obd->u.mds;
1217         struct lustre_quota_ctxt *qctxt = &mds->mds_obt.obt_qctxt;
1218         struct obd_device *lov_obd = class_exp2obd(mds->mds_osc_exp);
1219         struct lov_obd *lov = &lov_obd->u.lov;
1220         struct quota_adjust_qunit *oqaq = NULL;
1221         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1222         __u64 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
1223         time_t btime, itime;
1224         struct lustre_dquot *dquot;
1225         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1226         /* orig_set means if quota was set before; now_set means we are
1227          * setting/cancelling quota */
1228         int orig_set, now_set;
1229         int rc, rc2 = 0, flag = 0;
1230         ENTRY;
1231
1232         OBD_ALLOC_PTR(oqaq);
1233         if (!oqaq)
1234                 RETURN(-ENOMEM);
1235         down(&mds->mds_qonoff_sem);
1236         init_oqaq(oqaq, qctxt, oqctl->qc_id, oqctl->qc_type);
1237
1238         if (qinfo->qi_files[oqctl->qc_type] == NULL)
1239                 GOTO(out_sem, rc = -ESRCH);
1240
1241         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1242         if (IS_ERR(dquot))
1243                 GOTO(out_sem, rc = PTR_ERR(dquot));
1244         DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
1245         QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
1246
1247         down(&dquot->dq_sem);
1248
1249         if (dquot->dq_status) {
1250                 up(&dquot->dq_sem);
1251                 lustre_dqput(dquot);
1252                 GOTO(out_sem, rc = -EBUSY);
1253         }
1254         dquot->dq_status |= DQ_STATUS_SET;
1255
1256         ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1257         isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1258         bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1259         bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1260         btime = dquot->dq_dqb.dqb_btime;
1261         itime = dquot->dq_dqb.dqb_itime;
1262
1263         if (dqblk->dqb_valid & QIF_BTIME)
1264                 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
1265         if (dqblk->dqb_valid & QIF_ITIME)
1266                 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
1267
1268         if (dqblk->dqb_valid & QIF_BLIMITS) {
1269                 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
1270                 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
1271                 /* clear usage (limit pool) */
1272                 if (!dquot->dq_dqb.dqb_bhardlimit &&
1273                     !dquot->dq_dqb.dqb_bsoftlimit)
1274                         dquot->dq_dqb.dqb_curspace = 0;
1275
1276                 /* clear grace time */
1277                 if (!dqblk->dqb_bsoftlimit ||
1278                     toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
1279                         dquot->dq_dqb.dqb_btime = 0;
1280                 /* set grace only if user hasn't provided his own */
1281                 else if (!(dqblk->dqb_valid & QIF_BTIME))
1282                         dquot->dq_dqb.dqb_btime = cfs_time_current_sec() +
1283                                 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
1284
1285                 flag |= LQUOTA_FLAGS_ADJBLK;
1286         }
1287
1288         if (dqblk->dqb_valid & QIF_ILIMITS) {
1289                 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
1290                 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
1291                 /* clear usage (limit pool) */
1292                 if (!dquot->dq_dqb.dqb_ihardlimit &&
1293                     !dquot->dq_dqb.dqb_isoftlimit)
1294                         dquot->dq_dqb.dqb_curinodes = 0;
1295
1296                 if (!dqblk->dqb_isoftlimit ||
1297                     dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
1298                         dquot->dq_dqb.dqb_itime = 0;
1299                 else if (!(dqblk->dqb_valid & QIF_ITIME))
1300                         dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
1301                                 qinfo->qi_info[dquot->dq_type].dqi_igrace;
1302
1303                 flag |= LQUOTA_FLAGS_ADJINO;
1304         }
1305         QAQ_DEBUG(oqaq, "before dquot_create_oqaq\n");
1306         rc = dquot_create_oqaq(qctxt, dquot, lov->desc.ld_tgt_count, 1,
1307                                flag, oqaq);
1308         QAQ_DEBUG(oqaq, "after dquot_create_oqaq\n");
1309         if (rc < 0)
1310                 CDEBUG(D_QUOTA, "adjust qunit size failed! (rc:%d)\n", rc);
1311
1312
1313         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1314
1315         up(&dquot->dq_sem);
1316
1317         if (rc) {
1318                 CERROR("set limit failed! (rc:%d)\n", rc);
1319                 goto out;
1320         }
1321
1322         up(&mds->mds_qonoff_sem);
1323         orig_set = ihardlimit || isoftlimit;
1324         now_set  = dqblk->dqb_ihardlimit || dqblk->dqb_isoftlimit;
1325         if (dqblk->dqb_valid & QIF_ILIMITS && orig_set != now_set) {
1326                 down(&dquot->dq_sem);
1327                 dquot->dq_dqb.dqb_curinodes = 0;
1328                 up(&dquot->dq_sem);
1329                 rc = mds_init_slave_ilimits(obd, oqctl, orig_set, oqaq);
1330                 if (rc) {
1331                         CERROR("init slave ilimits failed! (rc:%d)\n", rc);
1332                         goto revoke_out;
1333                 }
1334         }
1335
1336         orig_set = bhardlimit || bsoftlimit;
1337         now_set  = dqblk->dqb_bhardlimit || dqblk->dqb_bsoftlimit;
1338         if (dqblk->dqb_valid & QIF_BLIMITS && orig_set != now_set) {
1339                 down(&dquot->dq_sem);
1340                 dquot->dq_dqb.dqb_curspace = 0;
1341                 up(&dquot->dq_sem);
1342                 rc = mds_init_slave_blimits(obd, oqctl, orig_set, oqaq);
1343                 if (rc) {
1344                         CERROR("init slave blimits failed! (rc:%d)\n", rc);
1345                         goto revoke_out;
1346                 }
1347         }
1348
1349 revoke_out:
1350         down(&mds->mds_qonoff_sem);
1351         down(&dquot->dq_sem);
1352         if (rc) {
1353                 /* cancel previous setting */
1354                 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
1355                 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
1356                 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
1357                 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
1358                 dquot->dq_dqb.dqb_btime = btime;
1359                 dquot->dq_dqb.dqb_itime = itime;
1360         }
1361         rc2 = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1362         up(&dquot->dq_sem);
1363
1364 out:
1365         down(&dquot->dq_sem);
1366         dquot->dq_status &= ~DQ_STATUS_SET;
1367         up(&dquot->dq_sem);
1368         lustre_dqput(dquot);
1369         EXIT;
1370 out_sem:
1371         up(&mds->mds_qonoff_sem);
1372
1373         if (oqaq)
1374                 OBD_FREE_PTR(oqaq);
1375
1376         return rc ? rc : rc2;
1377 }
1378
1379 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
1380 {
1381         struct obd_quotactl *soqc;
1382         struct lvfs_run_ctxt saved;
1383         int rc, rc1;
1384         ENTRY;
1385
1386         OBD_ALLOC_PTR(soqc);
1387         if (!soqc)
1388                 RETURN(-ENOMEM);
1389
1390         soqc->qc_cmd = Q_GETOQUOTA;
1391         soqc->qc_id = oqctl->qc_id;
1392         soqc->qc_type = oqctl->qc_type;
1393
1394         /* get block usage from OSS */
1395         soqc->qc_dqblk.dqb_curspace = 0;
1396         rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
1397         if (!rc) {
1398                 oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
1399                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
1400         }
1401
1402         /* get block/inode usage from MDS */
1403         soqc->qc_dqblk.dqb_curspace = 0;
1404         soqc->qc_dqblk.dqb_curinodes = 0;
1405         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1406         rc1 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
1407         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1408         if (!rc1) {
1409                 oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
1410                 oqctl->qc_dqblk.dqb_curinodes = soqc->qc_dqblk.dqb_curinodes;
1411                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
1412         }
1413
1414         OBD_FREE_PTR(soqc);
1415
1416         RETURN(rc ? : rc1);
1417 }
1418
1419 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
1420 {
1421         struct mds_obd *mds = &obd->u.mds;
1422         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1423         struct lustre_dquot *dquot;
1424         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
1425         int rc;
1426         ENTRY;
1427
1428         down(&mds->mds_qonoff_sem);
1429         dqblk->dqb_valid = 0;
1430         if (qinfo->qi_files[oqctl->qc_type] == NULL)
1431                 GOTO(out, rc = -ESRCH);
1432
1433         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
1434         if (IS_ERR(dquot))
1435                 GOTO(out, rc = PTR_ERR(dquot));
1436
1437         down(&dquot->dq_sem);
1438         dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
1439         dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
1440         dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
1441         dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
1442         dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
1443         dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
1444         dqblk->dqb_valid |= QIF_LIMITS | QIF_TIMES;
1445         up(&dquot->dq_sem);
1446
1447         lustre_dqput(dquot);
1448
1449         /* the usages in admin quota file is inaccurate */
1450         dqblk->dqb_curinodes = 0;
1451         dqblk->dqb_curspace = 0;
1452         rc = mds_get_space(obd, oqctl);
1453         EXIT;
1454 out:
1455         up(&mds->mds_qonoff_sem);
1456         return rc;
1457 }
1458
1459 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
1460 {
1461         struct lvfs_run_ctxt saved;
1462         int rc;
1463         ENTRY;
1464
1465         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1466         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
1467         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1468
1469         RETURN(rc);
1470 }
1471
1472
1473 /* FIXME we only recovery block limit by now, need recovery inode
1474  * limits also after CMD involved in */
1475 static int 
1476 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
1477 {
1478         struct mds_obd *mds = &obd->u.mds;
1479         struct lustre_quota_info *qinfo= &mds->mds_quota_info;
1480         struct lustre_dquot *dquot;
1481         struct obd_quotactl *qctl;
1482         __u64 total_limits = 0;
1483         int rc;
1484         ENTRY;
1485
1486         OBD_ALLOC_PTR(qctl);
1487         if (qctl == NULL)
1488                 RETURN(-ENOMEM);
1489
1490         dquot = lustre_dqget(obd, qinfo, id, type);
1491         if (IS_ERR(dquot)) {
1492                 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
1493                 OBD_FREE_PTR(qctl);
1494                 RETURN(PTR_ERR(dquot));
1495         }
1496
1497         down(&dquot->dq_sem);
1498
1499         /* don't recovery the dquot without limits or under setting */
1500         if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
1501             dquot->dq_status)
1502                 GOTO(skip, rc = 0);
1503         dquot->dq_status |= DQ_STATUS_RECOVERY;
1504
1505         up(&dquot->dq_sem);
1506
1507         /* get real bhardlimit from all slaves. */
1508         qctl->qc_cmd = Q_GETOQUOTA;
1509         qctl->qc_type = type;
1510         qctl->qc_id = id;
1511         qctl->qc_stat = QUOTA_RECOVERING;
1512         rc = obd_quotactl(mds->mds_osc_exp, qctl);
1513         if (rc)
1514                 GOTO(out, rc);
1515         total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1516
1517         /* get real bhardlimit from master */
1518         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1519         if (rc)
1520                 GOTO(out, rc);
1521         total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1522
1523         /* amend the usage of the administrative quotafile */
1524         down(&mds->mds_qonoff_sem);
1525         down(&dquot->dq_sem);
1526
1527         dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1528
1529         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1530         if (rc)
1531                 CERROR("write dquot failed! (rc:%d)\n", rc);
1532
1533         up(&dquot->dq_sem);
1534         up(&mds->mds_qonoff_sem);
1535         EXIT;
1536 out:
1537         down(&dquot->dq_sem);
1538         dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1539 skip:
1540         up(&dquot->dq_sem);
1541
1542         lustre_dqput(dquot);
1543         OBD_FREE_PTR(qctl);
1544         return rc;
1545 }
1546
1547 struct qmaster_recov_thread_data {
1548         struct obd_device *obd;
1549         struct completion comp;
1550 };
1551
1552 static int qmaster_recovery_main(void *arg)
1553 {
1554         struct qmaster_recov_thread_data *data = arg;
1555         struct obd_device *obd = data->obd;
1556         int rc = 0;
1557         unsigned short type;
1558         ENTRY;
1559
1560         ptlrpc_daemonize("qmaster_recovd");
1561
1562         complete(&data->comp);
1563
1564         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1565                 struct mds_obd *mds = &obd->u.mds;
1566                 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1567                 struct list_head id_list;
1568                 struct dquot_id *dqid, *tmp;
1569
1570                 down(&mds->mds_qonoff_sem);
1571                 if (qinfo->qi_files[type] == NULL) {
1572                         up(&mds->mds_qonoff_sem);
1573                         continue;
1574                 }
1575                 CFS_INIT_LIST_HEAD(&id_list);
1576                 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type,
1577                                  &id_list);
1578                 up(&mds->mds_qonoff_sem);
1579
1580                 if (rc)
1581                         CERROR("error get ids from admin quotafile.(%d)\n", rc);
1582
1583                 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1584                         list_del_init(&dqid->di_link);
1585                         if (rc)
1586                                 goto free;
1587
1588                         rc = dquot_recovery(obd, dqid->di_id, type);
1589                         if (rc)
1590                                 CERROR("qmaster recovery failed! (id:%d type:%d"
1591                                        " rc:%d)\n", dqid->di_id, type, rc);
1592 free:
1593                         kfree(dqid);
1594                 }
1595         }
1596         RETURN(rc);
1597 }
1598
1599 int mds_quota_recovery(struct obd_device *obd)
1600 {
1601         struct mds_obd *mds = &obd->u.mds;
1602         struct qmaster_recov_thread_data data;
1603         int rc = 0;
1604         ENTRY;
1605
1606         if (unlikely(!mds->mds_quota))
1607                 RETURN(rc);
1608
1609         mutex_down(&obd->obd_dev_sem);
1610         if (mds->mds_lov_desc.ld_active_tgt_count != mds->mds_lov_objid_count) {
1611                 CWARN("Only %u/%u OSTs are active, abort quota recovery\n",
1612                       mds->mds_lov_desc.ld_active_tgt_count,
1613                       mds->mds_lov_objid_count);
1614                 mutex_up(&obd->obd_dev_sem);
1615                 RETURN(rc);
1616         }
1617         mutex_up(&obd->obd_dev_sem);
1618
1619         data.obd = obd;
1620         init_completion(&data.comp);
1621
1622         rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1623         if (rc < 0)
1624                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1625
1626         wait_for_completion(&data.comp);
1627         RETURN(rc);
1628 }
1629
1630 #endif /* HAVE_QUOTA_SUPPORT */