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