Whamcloud - gitweb
- lost #define changes from 1_6
[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  *  lustre/quota/quota_master.c
5  *  Lustre Quota Master request handler
6  *
7  *  Copyright (c) 2001-2005 Cluster File Systems, Inc.
8  *   Author: Niu YaWei <niu@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   No redistribution or use is permitted outside of Cluster File Systems, Inc.
13  *
14  */
15 #ifndef EXPORT_SYMTAB
16 # define EXPORT_SYMTAB
17 #endif
18
19 #define DEBUG_SUBSYSTEM S_MDS
20
21 #include <linux/version.h>
22 #include <linux/fs.h>
23 #include <asm/unistd.h>
24 #include <linux/slab.h>
25 #include <linux/quotaops.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/quota.h>
29
30 #include <obd_class.h>
31 #include <lustre_quota.h>
32 #include <lustre_fsfilt.h>
33 #include <lustre_mds.h>
34
35 #include "quota_internal.h"
36
37 /* lock ordering: 
38  * mds->mds_qonoff_sem > dquot->dq_sem */
39 static struct list_head lustre_dquot_hash[NR_DQHASH];
40 static spinlock_t dquot_hash_lock = SPIN_LOCK_UNLOCKED;
41
42 cfs_mem_cache_t *lustre_dquot_cachep;
43
44 int lustre_dquot_init(void)
45 {
46         int i;
47         ENTRY;
48
49         LASSERT(lustre_dquot_cachep == NULL);
50         lustre_dquot_cachep = cfs_mem_cache_create("lustre_dquot_cache",
51                                                    sizeof(struct lustre_dquot),
52                                                    0, 0);
53         if (!lustre_dquot_cachep)
54                 return (-ENOMEM);
55
56         for (i = 0; i < NR_DQHASH; i++) {
57                 INIT_LIST_HEAD(lustre_dquot_hash + i);
58         }
59         RETURN(0);
60 }
61
62 void lustre_dquot_exit(void)
63 {
64         int i;
65         ENTRY;
66         /* FIXME cleanup work ?? */
67
68         for (i = 0; i < NR_DQHASH; i++) {
69                 LASSERT(list_empty(lustre_dquot_hash + i));
70         }
71         if (lustre_dquot_cachep) {
72                 int rc;
73                 rc = cfs_mem_cache_destroy(lustre_dquot_cachep);
74                 LASSERTF(rc == 0,"couldn't destroy lustre_dquot_cachep slab\n");
75                 lustre_dquot_cachep = NULL;
76         }
77         EXIT;
78 }
79
80 static inline int
81 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
82              __attribute__((__const__));
83
84 static inline int
85 dquot_hashfn(struct lustre_quota_info *info, unsigned int id, int type)
86 {
87         unsigned long tmp = ((unsigned long)info >> L1_CACHE_SHIFT) ^ id;
88         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
89         return tmp;
90 }
91
92 /* caller must hold dquot_hash_lock */
93 static struct lustre_dquot *find_dquot(int hashent,
94                                        struct lustre_quota_info *lqi, qid_t id,
95                                        int type)
96 {
97         struct lustre_dquot *dquot;
98         ENTRY;
99
100         LASSERT_SPIN_LOCKED(&dquot_hash_lock);
101         list_for_each_entry(dquot, &lustre_dquot_hash[hashent], dq_hash) {
102                 if (dquot->dq_info == lqi &&
103                     dquot->dq_id == id && dquot->dq_type == type)
104                         RETURN(dquot);
105         }
106         RETURN(NULL);
107 }
108
109 static struct lustre_dquot *alloc_dquot(struct lustre_quota_info *lqi,
110                                         qid_t id, int type)
111 {
112         struct lustre_dquot *dquot = NULL;
113         ENTRY;
114
115         OBD_SLAB_ALLOC(dquot, lustre_dquot_cachep, CFS_ALLOC_IO, sizeof(*dquot));
116         if (dquot == NULL)
117                 RETURN(NULL);
118
119         INIT_LIST_HEAD(&dquot->dq_hash);
120         init_mutex_locked(&dquot->dq_sem);
121         dquot->dq_refcnt = 1;
122         dquot->dq_info = lqi;
123         dquot->dq_id = id;
124         dquot->dq_type = type;
125         dquot->dq_status = DQ_STATUS_AVAIL;
126
127         RETURN(dquot);
128 }
129
130 static void free_dquot(struct lustre_dquot *dquot)
131 {
132         OBD_SLAB_FREE(dquot, lustre_dquot_cachep, sizeof(*dquot));
133 }
134
135 static void insert_dquot_nolock(struct lustre_dquot *dquot)
136 {
137         struct list_head *head = lustre_dquot_hash +
138             dquot_hashfn(dquot->dq_info, dquot->dq_id, dquot->dq_type);
139         LASSERT(list_empty(&dquot->dq_hash));
140         list_add(&dquot->dq_hash, head);
141 }
142
143 static void remove_dquot_nolock(struct lustre_dquot *dquot)
144 {
145         LASSERT(!list_empty(&dquot->dq_hash));
146         list_del_init(&dquot->dq_hash);
147 }
148
149 static void lustre_dqput(struct lustre_dquot *dquot)
150 {
151         ENTRY;
152         spin_lock(&dquot_hash_lock);
153         LASSERT(dquot->dq_refcnt);
154         dquot->dq_refcnt--;
155         if (!dquot->dq_refcnt) {
156                 remove_dquot_nolock(dquot);
157                 free_dquot(dquot);
158         }
159         spin_unlock(&dquot_hash_lock);
160         EXIT;
161 }
162
163 static struct lustre_dquot *lustre_dqget(struct obd_device *obd,
164                                          struct lustre_quota_info *lqi,
165                                          qid_t id, int type)
166 {
167         unsigned int hashent = dquot_hashfn(lqi, id, type);
168         struct lustre_dquot *dquot, *empty;
169         ENTRY;
170
171         if ((empty = alloc_dquot(lqi, id, type)) == NULL)
172                 RETURN(ERR_PTR(-ENOMEM));
173         
174         spin_lock(&dquot_hash_lock);
175         if ((dquot = find_dquot(hashent, lqi, id, type)) != NULL) {
176                 dquot->dq_refcnt++;
177                 spin_unlock(&dquot_hash_lock);
178                 free_dquot(empty);
179         } else {
180                 int rc;
181
182                 dquot = empty;
183                 insert_dquot_nolock(dquot);
184                 spin_unlock(&dquot_hash_lock);
185
186                 rc = fsfilt_dquot(obd, dquot, QFILE_RD_DQUOT);
187                 up(&dquot->dq_sem);
188                 if (rc) {
189                         CERROR("can't read dquot from admin quotafile! "
190                                "(rc:%d)\n", rc);
191                         lustre_dqput(dquot);
192                         RETURN(ERR_PTR(rc));
193                 }
194
195         }
196
197         LASSERT(dquot);
198         RETURN(dquot);
199 }
200
201 int dqacq_handler(struct obd_device *obd, struct qunit_data *qdata, int opc)
202 {
203         struct mds_obd *mds = &obd->u.mds;
204         struct lustre_quota_info *info = &mds->mds_quota_info;
205         struct lustre_dquot *dquot = NULL;
206         __u64 *usage = NULL;
207         __u32 hlimit = 0, slimit = 0;
208         __u32 qdata_type = qdata->qd_flags & QUOTA_IS_GRP;
209         __u32 is_blk = (qdata->qd_flags & QUOTA_IS_BLOCK) >> 1;
210         time_t *time = NULL;
211         unsigned int grace = 0;
212         int rc = 0;
213         ENTRY;
214
215         OBD_FAIL_RETURN(OBD_FAIL_OBD_DQACQ, -EIO);
216
217         dquot = lustre_dqget(obd, info, qdata->qd_id, qdata_type);
218         if (IS_ERR(dquot))
219                 RETURN(PTR_ERR(dquot));
220
221         DQUOT_DEBUG(dquot, "get dquot in dqacq_handler\n");
222         QINFO_DEBUG(dquot->dq_info, "get dquot in dqadq_handler\n");
223
224         down(&mds->mds_qonoff_sem);
225         down(&dquot->dq_sem);
226
227         if (dquot->dq_status & DQ_STATUS_RECOVERY) {
228                 DQUOT_DEBUG(dquot, "this dquot is under recovering.\n");
229                 GOTO(out, rc = -EBUSY);
230         }
231
232         if (is_blk) {
233                 grace = info->qi_info[qdata_type].dqi_bgrace;
234                 usage = &dquot->dq_dqb.dqb_curspace;
235                 hlimit = dquot->dq_dqb.dqb_bhardlimit;
236                 slimit = dquot->dq_dqb.dqb_bsoftlimit;
237                 time = &dquot->dq_dqb.dqb_btime;
238         } else {
239                 grace = info->qi_info[qdata_type].dqi_igrace;
240                 usage = (__u64 *) & dquot->dq_dqb.dqb_curinodes;
241                 hlimit = dquot->dq_dqb.dqb_ihardlimit;
242                 slimit = dquot->dq_dqb.dqb_isoftlimit;
243                 time = &dquot->dq_dqb.dqb_itime;
244         }
245
246         /* if the quota limit in admin quotafile is zero, we just inform
247          * slave to clear quota limit with zero qd_count */
248         if (hlimit == 0 && slimit == 0) {
249                 qdata->qd_count = 0;
250                 GOTO(out, rc);
251         }
252
253         switch (opc) {
254         case QUOTA_DQACQ:
255                 if (hlimit && 
256                     QUSG(*usage + qdata->qd_count, is_blk) > hlimit)
257                         GOTO(out, rc = -EDQUOT);
258
259                 if (slimit &&
260                     QUSG(*usage + qdata->qd_count, is_blk) > slimit) {
261                         if (*time && cfs_time_current_sec() >= *time)
262                                 GOTO(out, rc = -EDQUOT);
263                         else if (!*time)
264                                 *time = cfs_time_current_sec() + grace;
265                 }
266
267                 *usage += qdata->qd_count;
268                 break;
269         case QUOTA_DQREL:
270                 /* The usage in administrative file might be incorrect before
271                  * recovery done */
272                 if (*usage - qdata->qd_count < 0)
273                         *usage = 0;
274                 else
275                         *usage -= qdata->qd_count;
276
277                 /* (usage <= soft limit) but not (usage < soft limit) */
278                 if (!slimit || QUSG(*usage, is_blk) <= slimit)
279                         *time = 0;
280                 break;
281         default:
282                 LBUG();
283         }
284
285         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
286         EXIT;
287 out:
288         up(&dquot->dq_sem);
289         up(&mds->mds_qonoff_sem);
290         lustre_dqput(dquot);
291         return rc;
292 }
293
294 int mds_quota_adjust(struct obd_device *obd, unsigned int qcids[],
295                      unsigned int qpids[], int rc, int opc)
296 {
297         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
298         int rc2 = 0;
299         ENTRY;
300
301         if (rc && rc != -EDQUOT)
302                 RETURN(0);
303
304         switch (opc) {
305         case FSFILT_OP_RENAME:
306                 /* acquire/release block quota on owner of original parent */
307                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[2], qpids[3], 1, 0);
308                 /* fall-through */
309         case FSFILT_OP_SETATTR:
310                 /* acquire/release file quota on original owner */
311                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 0, 0);
312                 /* fall-through */
313         case FSFILT_OP_CREATE:
314         case FSFILT_OP_UNLINK:
315                 /* acquire/release file/block quota on owner of child (or current owner) */
316                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 0, 0);
317                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
318                 /* acquire/release block quota on owner of parent (or original owner) */
319                 rc2 |= qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
320                 break;
321         default:
322                 LBUG();
323                 break;
324         }
325
326         if (rc2)
327                 CERROR("mds adjust qunit failed! (opc:%d rc:%d)\n", opc, rc2);
328         RETURN(0);
329 }
330
331 int filter_quota_adjust(struct obd_device *obd, unsigned int qcids[],
332                         unsigned int qpids[], int rc, int opc)
333 {
334         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
335         int rc2 = 0;
336         ENTRY;
337
338         if (rc && rc != -EDQUOT)
339                 RETURN(0);
340
341         switch (opc) {
342         case FSFILT_OP_SETATTR:
343                 /* acquire/release block quota on original & current owner */
344                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
345                 rc2 = qctxt_adjust_qunit(obd, qctxt, qpids[0], qpids[1], 1, 0);
346                 break;
347         case FSFILT_OP_UNLINK:
348                 /* release block quota on this owner */
349         case FSFILT_OP_CREATE: /* XXX for write operation on obdfilter */
350                 /* acquire block quota on this owner */
351                 rc = qctxt_adjust_qunit(obd, qctxt, qcids[0], qcids[1], 1, 0);
352                 break;
353         default:
354                 LBUG();
355                 break;
356         }
357
358         if (rc || rc2)
359                 CERROR("filter adjust qunit failed! (opc:%d rc%d)\n",
360                        opc, rc ?: rc2);
361         RETURN(0);
362 }
363
364 #define LUSTRE_ADMIN_QUOTAFILES {\
365         "admin_quotafile.usr",  /* user admin quotafile */\
366         "admin_quotafile.grp"   /* group admin quotafile */\
367 }
368 static const char prefix[] = "OBJECTS/";
369
370 int init_admin_quotafiles(struct obd_device *obd, struct obd_quotactl *oqctl)
371 {
372         struct mds_obd *mds = &obd->u.mds;
373         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
374         const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
375         struct lvfs_run_ctxt saved;
376         char name[64];
377         int i, rc = 0;
378         struct dentry *dparent = mds->mds_objects_dir;
379         struct inode *iparent = dparent->d_inode;
380         ENTRY;
381
382         LASSERT(iparent);
383         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
384
385         down(&mds->mds_qonoff_sem);
386         for (i = 0; i < MAXQUOTAS; i++) {
387                 struct dentry *de;
388                 struct file *fp;
389
390                 if (!Q_TYPESET(oqctl, i))
391                         continue;
392
393                 /* quota file has been opened ? */
394                 if (qinfo->qi_files[i]) {
395                         CWARN("init %s admin quotafile while quota on.\n",
396                               i == USRQUOTA ? "user" : "group");
397                         continue;
398                 }
399
400                 /* lookup quota file */
401                 rc = 0;
402                 LOCK_INODE_MUTEX(iparent);
403                 de = lookup_one_len(quotafiles[i], dparent,
404                                     strlen(quotafiles[i]));
405                 UNLOCK_INODE_MUTEX(iparent);
406                 if (IS_ERR(de) || de->d_inode == NULL || 
407                     !S_ISREG(de->d_inode->i_mode))
408                         rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
409                 if (!IS_ERR(de))
410                         dput(de);
411
412                 if (rc && rc != -ENOENT) {
413                         CERROR("error lookup quotafile %s! (rc:%d)\n",
414                                name, rc);
415                         break;
416                 } else if (!rc) {
417                         continue;
418                 }
419
420                 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
421                 sprintf(name, "%s%s", prefix, quotafiles[i]);
422
423                 LASSERT(rc == -ENOENT);
424                 /* create quota file */
425                 fp = filp_open(name, O_CREAT | O_EXCL, 0644);
426                 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
427                         rc = PTR_ERR(fp);
428                         CERROR("error creating admin quotafile %s (rc:%d)\n",
429                                name, rc);
430                         break;
431                 }
432
433                 qinfo->qi_files[i] = fp;
434                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_INIT_INFO);
435                 filp_close(fp, 0);
436                 qinfo->qi_files[i] = NULL;
437
438                 if (rc) {
439                         CERROR("error init %s admin quotafile! (rc:%d)\n",
440                                i == USRQUOTA ? "user" : "group", rc);
441                         break;
442                 }
443         }
444         up(&mds->mds_qonoff_sem);
445
446         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
447         RETURN(rc);
448 }
449
450 static int close_quota_files(struct obd_quotactl *oqctl, 
451                              struct lustre_quota_info *qinfo)
452 {
453         int i, rc = 0;
454         ENTRY;
455
456         for (i = 0; i < MAXQUOTAS; i++) {
457                 if (!Q_TYPESET(oqctl, i))
458                         continue;
459                 if (qinfo->qi_files[i] == NULL) {
460                         rc = -ESRCH;
461                         continue;
462                 }
463                 filp_close(qinfo->qi_files[i], 0);
464                 qinfo->qi_files[i] = NULL;
465         }
466         RETURN(rc);
467 }
468
469 int mds_admin_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
470 {
471         struct mds_obd *mds = &obd->u.mds;
472         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
473         const char *quotafiles[] = LUSTRE_ADMIN_QUOTAFILES;
474         char name[64];
475         int i, rc = 0;
476         struct inode *iparent = mds->mds_objects_dir->d_inode;
477         ENTRY;
478
479         LASSERT(iparent);
480
481         /* open admin quota files and read quotafile info */
482         for (i = 0; i < MAXQUOTAS; i++) {
483                 struct file *fp;
484
485                 if (!Q_TYPESET(oqctl, i))
486                         continue;
487
488                 LASSERT(strlen(quotafiles[i]) + sizeof(prefix) <= sizeof(name));
489                 sprintf(name, "%s%s", prefix, quotafiles[i]);
490
491                 if (qinfo->qi_files[i] != NULL) {
492                         rc = -EBUSY;
493                         break;
494                 }
495
496                 fp = filp_open(name, O_RDWR | O_EXCL, 0644);
497                 if (IS_ERR(fp) || !S_ISREG(fp->f_dentry->d_inode->i_mode)) {
498                         rc = PTR_ERR(fp);
499                         CDEBUG(rc == -ENOENT ? D_QUOTA : D_ERROR,
500                                "open %s failed! (rc:%d)\n", name, rc);
501                         break;
502                 }
503                 qinfo->qi_files[i] = fp;
504
505                 rc = fsfilt_quotainfo(obd, qinfo, i, QFILE_RD_INFO);
506                 if (rc) {
507                         CERROR("error read quotainfo of %s! (rc:%d)\n",
508                                name, rc);
509                         break;
510                 }
511         }
512
513         if (rc && rc != -EBUSY)
514                 close_quota_files(oqctl, qinfo);
515
516         RETURN(rc);
517 }
518
519 static int mds_admin_quota_off(struct obd_device *obd, 
520                                struct obd_quotactl *oqctl)
521 {
522         struct mds_obd *mds = &obd->u.mds;
523         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
524         int rc;
525         ENTRY;
526
527         /* close admin quota files */
528         rc = close_quota_files(oqctl, qinfo);
529         RETURN(rc);
530 }
531
532 int mds_quota_on(struct obd_device *obd, struct obd_quotactl *oqctl)
533 {
534         struct mds_obd *mds = &obd->u.mds;
535         struct obd_device_target *obt = &obd->u.obt;
536         struct lvfs_run_ctxt saved;
537         int rc;
538         ENTRY;
539
540         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
541                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
542                 atomic_inc(&obt->obt_quotachecking);
543                 RETURN(-EBUSY);
544         }
545
546         down(&mds->mds_qonoff_sem);
547         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
548         rc = mds_admin_quota_on(obd, oqctl);
549         if (rc)
550                 goto out;
551
552         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
553         if (rc)
554                 goto out;
555
556         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
557         if (!rc)
558                 obt->obt_qctxt.lqc_status = 1;
559 out:
560         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
561         up(&mds->mds_qonoff_sem);
562         atomic_inc(&obt->obt_quotachecking);
563         RETURN(rc);
564 }
565
566 int mds_quota_off(struct obd_device *obd, struct obd_quotactl *oqctl)
567 {
568         struct mds_obd *mds = &obd->u.mds;
569         struct obd_device_target *obt = &obd->u.obt;
570         struct lvfs_run_ctxt saved;
571         int rc, rc2;
572         ENTRY;
573
574         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
575                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
576                 atomic_inc(&obt->obt_quotachecking);
577                 RETURN(-EBUSY);
578         }
579
580         down(&mds->mds_qonoff_sem);
581         /* close admin quota files */
582         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
583         mds_admin_quota_off(obd, oqctl);
584
585         rc = obd_quotactl(mds->mds_osc_exp, oqctl);
586         rc2 = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
587         if (!rc2)
588                 obt->obt_qctxt.lqc_status = 0;
589
590         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
591         up(&mds->mds_qonoff_sem);
592         atomic_inc(&obt->obt_quotachecking);
593
594         RETURN(rc ?: rc2);
595 }
596
597 int mds_set_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
598 {
599         struct mds_obd *mds = &obd->u.mds;
600         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
601         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
602         int rc;
603         ENTRY;
604
605         down(&mds->mds_qonoff_sem);
606         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
607                 rc = -ESRCH;
608                 goto out;
609         }
610
611         qinfo->qi_info[oqctl->qc_type].dqi_bgrace = dqinfo->dqi_bgrace;
612         qinfo->qi_info[oqctl->qc_type].dqi_igrace = dqinfo->dqi_igrace;
613         qinfo->qi_info[oqctl->qc_type].dqi_flags = dqinfo->dqi_flags;
614
615         rc = fsfilt_quotainfo(obd, qinfo, oqctl->qc_type, QFILE_WR_INFO);
616
617 out:
618         up(&mds->mds_qonoff_sem);
619         RETURN(rc);
620 }
621
622 int mds_get_dqinfo(struct obd_device *obd, struct obd_quotactl *oqctl)
623 {
624         struct mds_obd *mds = &obd->u.mds;
625         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
626         struct obd_dqinfo *dqinfo = &oqctl->qc_dqinfo;
627         int rc = 0;
628         ENTRY;
629
630         down(&mds->mds_qonoff_sem);
631         if (qinfo->qi_files[oqctl->qc_type] == NULL) {
632                 rc = -ESRCH;
633                 goto out;
634         }
635
636         dqinfo->dqi_bgrace = qinfo->qi_info[oqctl->qc_type].dqi_bgrace;
637         dqinfo->dqi_igrace = qinfo->qi_info[oqctl->qc_type].dqi_igrace;
638         dqinfo->dqi_flags = qinfo->qi_info[oqctl->qc_type].dqi_flags;
639
640 out:
641         up(&mds->mds_qonoff_sem);
642         RETURN(rc);
643 }
644
645 static int mds_init_slave_ilimits(struct obd_device *obd,
646                                   struct obd_quotactl *oqctl, int set)
647 {
648         /* XXX: for file limits only adjust local now */
649         unsigned int uid = 0, gid = 0;
650         struct obd_quotactl *ioqc = NULL;
651         int flag;
652         int rc;
653         ENTRY;
654
655         /* if we are going to set zero limit, needn't init slaves */
656         if (!oqctl->qc_dqblk.dqb_ihardlimit && !oqctl->qc_dqblk.dqb_isoftlimit &&
657             set)
658                 RETURN(0);
659
660         OBD_ALLOC_PTR(ioqc);
661         if (!ioqc)
662                 RETURN(-ENOMEM);
663         
664         flag = oqctl->qc_dqblk.dqb_ihardlimit || 
665                oqctl->qc_dqblk.dqb_isoftlimit || set;
666         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
667         ioqc->qc_id = oqctl->qc_id;
668         ioqc->qc_type = oqctl->qc_type;
669         ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
670         ioqc->qc_dqblk.dqb_ihardlimit = flag ? MIN_QLIMIT : 0;
671
672         /* set local limit to MIN_QLIMIT */
673         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
674         if (rc)
675                 GOTO(out, rc);
676
677         /* trigger local qunit pre-acquire */
678         if (oqctl->qc_type == USRQUOTA)
679                 uid = oqctl->qc_id;
680         else
681                 gid = oqctl->qc_id;
682
683         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0);
684         if (rc) {
685                 CERROR("error mds adjust local file quota! (rc:%d)\n", rc);
686                 GOTO(out, rc);
687         }
688         /* FIXME initialize all slaves in CMD */
689         EXIT;
690 out:
691         if (ioqc)
692                 OBD_FREE_PTR(ioqc);
693         return rc;
694 }
695
696 static int mds_init_slave_blimits(struct obd_device *obd,
697                                   struct obd_quotactl *oqctl, int set)
698 {
699         struct mds_obd *mds = &obd->u.mds;
700         struct obd_quotactl *ioqc;
701         unsigned int uid = 0, gid = 0;
702         int flag;
703         int rc;
704         ENTRY;
705
706         /* if we are going to set zero limit, needn't init slaves */
707         if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit &&
708             set)
709                 RETURN(0);
710
711         OBD_ALLOC_PTR(ioqc);
712         if (!ioqc)
713                 RETURN(-ENOMEM);
714
715         flag = oqctl->qc_dqblk.dqb_bhardlimit || 
716                oqctl->qc_dqblk.dqb_bsoftlimit || set;
717         ioqc->qc_cmd = flag ? Q_INITQUOTA : Q_SETQUOTA;
718         ioqc->qc_id = oqctl->qc_id;
719         ioqc->qc_type = oqctl->qc_type;
720         ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
721         ioqc->qc_dqblk.dqb_bhardlimit = flag ? MIN_QLIMIT : 0;
722
723         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
724         if (rc)
725                 GOTO(out, rc);
726
727         /* trigger local qunit pre-acquire */
728         if (oqctl->qc_type == USRQUOTA)
729                 uid = oqctl->qc_id;
730         else
731                 gid = oqctl->qc_id;
732
733         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0);
734         if (rc) {
735                 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
736                 GOTO(out, rc);
737         }
738
739         /* initialize all slave's limit */
740         rc = obd_quotactl(mds->mds_osc_exp, ioqc);
741         EXIT;
742 out:
743         OBD_FREE_PTR(ioqc);
744         return rc;
745 }
746
747 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
748 {
749         struct mds_obd *mds = &obd->u.mds;
750         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
751         __u32 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
752         time_t btime, itime;
753         struct lustre_dquot *dquot;
754         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
755         int set, rc;
756         ENTRY;
757
758         down(&mds->mds_qonoff_sem);
759         if (qinfo->qi_files[oqctl->qc_type] == NULL)
760                 GOTO(out_sem, rc = -ESRCH);
761
762         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
763         if (IS_ERR(dquot))
764                 GOTO(out_sem, rc = PTR_ERR(dquot));
765         DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
766         QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
767
768         down(&dquot->dq_sem);
769
770         if (dquot->dq_status) {
771                 up(&dquot->dq_sem);
772                 lustre_dqput(dquot);
773                 GOTO(out_sem, rc = -EBUSY);
774         }
775         dquot->dq_status |= DQ_STATUS_SET;
776
777         ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
778         isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
779         bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
780         bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
781         btime = dquot->dq_dqb.dqb_btime;
782         itime = dquot->dq_dqb.dqb_itime;
783
784         if (dqblk->dqb_valid & QIF_BTIME)
785                 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
786         if (dqblk->dqb_valid & QIF_ITIME)
787                 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
788
789         if (dqblk->dqb_valid & QIF_BLIMITS) {
790                 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
791                 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
792                 /* clear usage (limit pool) */
793                 if (!dquot->dq_dqb.dqb_bhardlimit && 
794                     !dquot->dq_dqb.dqb_bsoftlimit)
795                         dquot->dq_dqb.dqb_curspace = 0;
796
797                 /* clear grace time */
798                 if (!dqblk->dqb_bsoftlimit || 
799                     toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
800                         dquot->dq_dqb.dqb_btime = 0;
801                 /* set grace only if user hasn't provided his own */
802                 else if (!(dqblk->dqb_valid & QIF_BTIME))
803                         dquot->dq_dqb.dqb_btime = cfs_time_current_sec() + 
804                                 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
805         }
806
807         if (dqblk->dqb_valid & QIF_ILIMITS) {
808                 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
809                 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
810                 /* clear usage (limit pool) */
811                 if (!dquot->dq_dqb.dqb_ihardlimit &&
812                     !dquot->dq_dqb.dqb_isoftlimit)
813                         dquot->dq_dqb.dqb_curinodes = 0;
814
815                 if (!dqblk->dqb_isoftlimit ||
816                     dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
817                         dquot->dq_dqb.dqb_itime = 0;
818                 else if (!(dqblk->dqb_valid & QIF_ITIME))
819                         dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
820                                 qinfo->qi_info[dquot->dq_type].dqi_igrace;
821         }
822
823         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
824
825         up(&dquot->dq_sem);
826
827         if (rc) {
828                 CERROR("set limit failed! (rc:%d)\n", rc);
829                 goto out;
830         }
831
832         up(&mds->mds_qonoff_sem);
833         if (dqblk->dqb_valid & QIF_ILIMITS) {
834                 set = !(ihardlimit || isoftlimit);
835                 rc = mds_init_slave_ilimits(obd, oqctl, set);
836                 if (rc) {
837                         CERROR("init slave ilimits failed! (rc:%d)\n", rc);
838                         goto revoke_out;
839                 }
840         }
841
842         if (dqblk->dqb_valid & QIF_BLIMITS) {
843                 set = !(bhardlimit || bsoftlimit);
844                 rc = mds_init_slave_blimits(obd, oqctl, set);
845                 if (rc) {
846                         CERROR("init slave blimits failed! (rc:%d)\n", rc);
847                         goto revoke_out;
848                 }
849         }
850         down(&mds->mds_qonoff_sem);
851
852 revoke_out:
853         if (rc) {
854                 /* cancel previous setting */
855                 down(&dquot->dq_sem);
856                 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
857                 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
858                 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
859                 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
860                 dquot->dq_dqb.dqb_btime = btime;
861                 dquot->dq_dqb.dqb_itime = itime;
862                 fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
863                 up(&dquot->dq_sem);
864         }
865 out:
866         down(&dquot->dq_sem);
867         dquot->dq_status &= ~DQ_STATUS_SET;
868         up(&dquot->dq_sem);
869         lustre_dqput(dquot);
870         EXIT;
871 out_sem:
872         up(&mds->mds_qonoff_sem);
873         return rc;
874 }
875
876 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
877 {
878         struct obd_quotactl *soqc;
879         struct lvfs_run_ctxt saved;
880         int rc;
881         ENTRY;
882
883         OBD_ALLOC_PTR(soqc);
884         if (!soqc)
885                 RETURN(-ENOMEM);
886
887         soqc->qc_cmd = Q_GETOQUOTA;
888         soqc->qc_id = oqctl->qc_id;
889         soqc->qc_type = oqctl->qc_type;
890
891         rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
892         if (rc)
893                GOTO(out, rc);
894
895         oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
896
897         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
898         soqc->qc_dqblk.dqb_curspace = 0;
899         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
900         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
901
902         if (rc)
903                 GOTO(out, rc);
904
905         oqctl->qc_dqblk.dqb_curinodes += soqc->qc_dqblk.dqb_curinodes;
906         oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
907         EXIT;
908 out:
909         OBD_FREE_PTR(soqc);
910         return rc;
911 }
912
913 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
914 {
915         struct mds_obd *mds = &obd->u.mds;
916         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
917         struct lustre_dquot *dquot;
918         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
919         int rc;
920         ENTRY;
921
922         down(&mds->mds_qonoff_sem);
923         if (qinfo->qi_files[oqctl->qc_type] == NULL)
924                 GOTO(out, rc = -ESRCH);
925
926         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
927         if (IS_ERR(dquot))
928                 GOTO(out, rc = PTR_ERR(dquot));
929
930         down(&dquot->dq_sem);
931         dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
932         dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
933         dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
934         dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
935         dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
936         dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
937         up(&dquot->dq_sem);
938
939         lustre_dqput(dquot);
940
941         /* the usages in admin quota file is inaccurate */
942         dqblk->dqb_curinodes = 0;
943         dqblk->dqb_curspace = 0;
944         rc = mds_get_space(obd, oqctl);
945         EXIT;
946 out:
947         up(&mds->mds_qonoff_sem);
948         return rc;
949 }
950
951 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
952 {
953         struct lvfs_run_ctxt saved;
954         int rc;
955         ENTRY;
956
957         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
958         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
959         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
960
961         RETURN(rc);
962 }
963
964
965 /* FIXME we only recovery block limit by now, need recovery inode
966  * limits also after CMD involved in */
967 static int 
968 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
969 {
970         struct mds_obd *mds = &obd->u.mds;
971         struct lustre_quota_info *qinfo= &obd->u.mds.mds_quota_info;
972         struct lustre_dquot *dquot;
973         struct obd_quotactl *qctl;
974         __u64 total_limits = 0;
975         int rc;
976         ENTRY;
977
978         OBD_ALLOC_PTR(qctl);
979         if (qctl == NULL)
980                 RETURN(-ENOMEM);
981
982         dquot = lustre_dqget(obd, qinfo, id, type);
983         if (IS_ERR(dquot)) {
984                 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
985                 OBD_FREE_PTR(qctl);
986                 RETURN(PTR_ERR(dquot));
987         }
988
989         down(&dquot->dq_sem);
990
991         /* don't recovery the dquot without limits or under setting */
992         if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
993             dquot->dq_status)
994                 GOTO(skip, rc = 0);
995         dquot->dq_status |= DQ_STATUS_RECOVERY;
996
997         up(&dquot->dq_sem);
998
999         /* get real bhardlimit from all slaves. */
1000         qctl->qc_cmd = Q_GETOQUOTA;
1001         qctl->qc_type = type;
1002         qctl->qc_id = id;
1003         qctl->qc_stat = QUOTA_RECOVERING;
1004         rc = obd_quotactl(obd->u.mds.mds_osc_exp, qctl);
1005         if (rc)
1006                 GOTO(out, rc);
1007         total_limits = qctl->qc_dqblk.dqb_bhardlimit;
1008
1009         /* get real bhardlimit from master */
1010         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
1011         if (rc)
1012                 GOTO(out, rc);
1013         total_limits += qctl->qc_dqblk.dqb_bhardlimit;
1014
1015         /* amend the usage of the administrative quotafile */
1016         down(&mds->mds_qonoff_sem);
1017         down(&dquot->dq_sem);
1018
1019         dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
1020
1021         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
1022         if (rc)
1023                 CERROR("write dquot failed! (rc:%d)\n", rc);
1024
1025         up(&dquot->dq_sem);
1026         up(&mds->mds_qonoff_sem);
1027         EXIT;
1028 out:
1029         down(&dquot->dq_sem);
1030         dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1031 skip:
1032         up(&dquot->dq_sem);
1033
1034         lustre_dqput(dquot);
1035         OBD_FREE_PTR(qctl);
1036         return rc;
1037 }
1038
1039 struct qmaster_recov_thread_data {
1040         struct obd_device *obd;
1041         struct completion comp;
1042 };
1043
1044 static int qmaster_recovery_main(void *arg)
1045 {
1046         struct qmaster_recov_thread_data *data = arg;
1047         struct obd_device *obd = data->obd;
1048         int rc = 0;
1049         unsigned short type;
1050         ENTRY;
1051
1052         ptlrpc_daemonize("qmaster_recovd");
1053
1054         complete(&data->comp);
1055
1056         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1057                 struct mds_obd *mds = &obd->u.mds;
1058                 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1059                 struct list_head id_list;
1060                 struct dquot_id *dqid, *tmp;
1061
1062                 down(&mds->mds_qonoff_sem);
1063                 if (qinfo->qi_files[type] == NULL) {
1064                         up(&mds->mds_qonoff_sem);
1065                         continue;
1066                 }
1067                 INIT_LIST_HEAD(&id_list);
1068                 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type, 
1069                                  &id_list);
1070                 up(&mds->mds_qonoff_sem);
1071
1072                 if (rc)
1073                         CERROR("error get ids from admin quotafile.(%d)\n", rc);
1074
1075                 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1076                         list_del_init(&dqid->di_link);
1077                         if (rc)
1078                                 goto free;
1079
1080                         rc = dquot_recovery(obd, dqid->di_id, type);
1081                         if (rc)
1082                                 CERROR("qmaster recovery failed! (id:%d type:%d"
1083                                        " rc:%d)\n", dqid->di_id, type, rc);
1084 free:
1085                         kfree(dqid);
1086                 }
1087         }
1088         RETURN(rc);
1089 }
1090
1091 int mds_quota_recovery(struct obd_device *obd)
1092 {
1093         struct lov_obd *lov = &obd->u.mds.mds_osc_obd->u.lov;
1094         struct qmaster_recov_thread_data data;
1095         int rc = 0;
1096         ENTRY;
1097
1098         mutex_down(&lov->lov_lock);
1099         if (lov->desc.ld_tgt_count != lov->desc.ld_active_tgt_count) {
1100                 CWARN("Not all osts are active, abort quota recovery\n");
1101                 mutex_up(&lov->lov_lock);
1102                 RETURN(rc);
1103         }
1104         mutex_up(&lov->lov_lock);
1105
1106         data.obd = obd;
1107         init_completion(&data.comp);
1108
1109         rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1110         if (rc < 0)
1111                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1112
1113         wait_for_completion(&data.comp);
1114         RETURN(rc);
1115 }