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