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