Whamcloud - gitweb
- update from parent
[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                 LOCK_INODE_MUTEX(iparent);
400                 de = lookup_one_len(quotafiles[i], dparent,
401                                     strlen(quotafiles[i]));
402                 UNLOCK_INODE_MUTEX(iparent);
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, int set)
622 {
623         /* XXX: for file limits only adjust local now */
624         unsigned int uid = 0, gid = 0;
625         struct obd_quotactl *ioqc = NULL;
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         if (!set)
634                 goto acquire;
635
636         OBD_ALLOC_PTR(ioqc);
637         if (!ioqc)
638                 RETURN(-ENOMEM);
639
640         ioqc->qc_cmd = Q_INITQUOTA;
641         ioqc->qc_id = oqctl->qc_id;
642         ioqc->qc_type = oqctl->qc_type;
643         ioqc->qc_dqblk.dqb_valid = QIF_ILIMITS;
644         ioqc->qc_dqblk.dqb_ihardlimit = MIN_QLIMIT;
645
646         /* set local limit to MIN_QLIMIT */
647         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
648         if (rc)
649                 GOTO(out, rc);
650 acquire:
651         /* trigger local qunit pre-acquire */
652         if (oqctl->qc_type == USRQUOTA)
653                 uid = oqctl->qc_id;
654         else
655                 gid = oqctl->qc_id;
656
657         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 0, 0);
658         if (rc) {
659                 CERROR("error mds adjust local file quota! (rc:%d)\n", rc);
660                 GOTO(out, rc);
661         }
662         /* FIXME initialize all slaves in CMD */
663         EXIT;
664 out:
665         if (ioqc)
666                 OBD_FREE_PTR(ioqc);
667         return rc;
668 }
669
670 static int mds_init_slave_blimits(struct obd_device *obd,
671                                   struct obd_quotactl *oqctl, int set)
672 {
673         struct mds_obd *mds = &obd->u.mds;
674         struct obd_quotactl *ioqc;
675         unsigned int uid = 0, gid = 0;
676         int rc;
677         ENTRY;
678
679         /* if we are going to set zero limit, needn't init slaves */
680         if (!oqctl->qc_dqblk.dqb_bhardlimit && !oqctl->qc_dqblk.dqb_bsoftlimit)
681                 RETURN(0);
682
683         OBD_ALLOC_PTR(ioqc);
684         if (!ioqc)
685                 RETURN(-ENOMEM);
686
687         ioqc->qc_cmd = Q_INITQUOTA;
688         ioqc->qc_id = oqctl->qc_id;
689         ioqc->qc_type = oqctl->qc_type;
690         ioqc->qc_dqblk.dqb_valid = QIF_BLIMITS;
691         ioqc->qc_dqblk.dqb_bhardlimit = set ? MIN_QLIMIT : 0;
692
693         /* set local limit to MIN_QLIMIT */
694         if (set) {
695                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, ioqc);
696                 if (rc)
697                         GOTO(out, rc);
698         }
699
700         /* trigger local qunit pre-acquire */
701         if (oqctl->qc_type == USRQUOTA)
702                 uid = oqctl->qc_id;
703         else
704                 gid = oqctl->qc_id;
705
706         rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt, uid, gid, 1, 0);
707         if (rc) {
708                 CERROR("error mds adjust local block quota! (rc:%d)\n", rc);
709                 GOTO(out, rc);
710         }
711
712         /* initialize all slave's limit */
713         rc = obd_quotactl(mds->mds_osc_exp, ioqc);
714         EXIT;
715 out:
716         OBD_FREE_PTR(ioqc);
717         return rc;
718 }
719
720 int mds_set_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
721 {
722         struct mds_obd *mds = &obd->u.mds;
723         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
724         __u32 ihardlimit, isoftlimit, bhardlimit, bsoftlimit;
725         time_t btime, itime;
726         struct lustre_dquot *dquot;
727         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
728         int set, rc;
729         ENTRY;
730
731         down(&mds->mds_qonoff_sem);
732         if (qinfo->qi_files[oqctl->qc_type] == NULL)
733                 GOTO(out_sem, rc = -ESRCH);
734
735         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
736         if (IS_ERR(dquot))
737                 GOTO(out_sem, rc = PTR_ERR(dquot));
738         DQUOT_DEBUG(dquot, "get dquot in mds_set_blk\n");
739         QINFO_DEBUG(dquot->dq_info, "get dquot in mds_set_blk\n");
740
741         down(&dquot->dq_sem);
742
743         if (dquot->dq_status) {
744                 up(&dquot->dq_sem);
745                 lustre_dqput(dquot);
746                 GOTO(out_sem, rc = -EBUSY);
747         }
748         dquot->dq_status |= DQ_STATUS_SET;
749
750         ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
751         isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
752         bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
753         bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
754         btime = dquot->dq_dqb.dqb_btime;
755         itime = dquot->dq_dqb.dqb_itime;
756
757         if (dqblk->dqb_valid & QIF_BTIME)
758                 dquot->dq_dqb.dqb_btime = dqblk->dqb_btime;
759         if (dqblk->dqb_valid & QIF_ITIME)
760                 dquot->dq_dqb.dqb_itime = dqblk->dqb_itime;
761
762         if (dqblk->dqb_valid & QIF_BLIMITS) {
763                 dquot->dq_dqb.dqb_bhardlimit = dqblk->dqb_bhardlimit;
764                 dquot->dq_dqb.dqb_bsoftlimit = dqblk->dqb_bsoftlimit;
765                 /* clear usage (limit pool) */
766                 if (!dquot->dq_dqb.dqb_bhardlimit && 
767                     !dquot->dq_dqb.dqb_bsoftlimit)
768                         dquot->dq_dqb.dqb_curspace = 0;
769
770                 /* clear grace time */
771                 if (!dqblk->dqb_bsoftlimit || 
772                     toqb(dquot->dq_dqb.dqb_curspace) <= dqblk->dqb_bsoftlimit)
773                         dquot->dq_dqb.dqb_btime = 0;
774                 /* set grace only if user hasn't provided his own */
775                 else if (!(dqblk->dqb_valid & QIF_BTIME))
776                         dquot->dq_dqb.dqb_btime = cfs_time_current_sec() + 
777                                 qinfo->qi_info[dquot->dq_type].dqi_bgrace;
778         }
779
780         if (dqblk->dqb_valid & QIF_ILIMITS) {
781                 dquot->dq_dqb.dqb_ihardlimit = dqblk->dqb_ihardlimit;
782                 dquot->dq_dqb.dqb_isoftlimit = dqblk->dqb_isoftlimit;
783                 /* clear usage (limit pool) */
784                 if (!dquot->dq_dqb.dqb_ihardlimit &&
785                     !dquot->dq_dqb.dqb_isoftlimit)
786                         dquot->dq_dqb.dqb_curinodes = 0;
787
788                 if (!dqblk->dqb_isoftlimit ||
789                     dquot->dq_dqb.dqb_curinodes <= dqblk->dqb_isoftlimit)
790                         dquot->dq_dqb.dqb_itime = 0;
791                 else if (!(dqblk->dqb_valid & QIF_ITIME))
792                         dquot->dq_dqb.dqb_itime = cfs_time_current_sec() +
793                                 qinfo->qi_info[dquot->dq_type].dqi_igrace;
794         }
795
796         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
797
798         up(&dquot->dq_sem);
799
800         if (rc) {
801                 CERROR("set limit failed! (rc:%d)\n", rc);
802                 goto out;
803         }
804
805         up(&mds->mds_qonoff_sem);
806         if (dqblk->dqb_valid & QIF_ILIMITS) {
807                 set = !(ihardlimit || isoftlimit);
808                 rc = mds_init_slave_ilimits(obd, oqctl, set);
809                 if (rc) {
810                         CERROR("init slave ilimits failed! (rc:%d)\n", rc);
811                         goto revoke_out;
812                 }
813         }
814
815         if (dqblk->dqb_valid & QIF_BLIMITS) {
816                 set = !(bhardlimit || bsoftlimit);
817                 rc = mds_init_slave_blimits(obd, oqctl, set);
818                 if (rc) {
819                         CERROR("init slave blimits failed! (rc:%d)\n", rc);
820                         goto revoke_out;
821                 }
822         }
823         down(&mds->mds_qonoff_sem);
824
825 revoke_out:
826         if (rc) {
827                 /* cancel previous setting */
828                 down(&dquot->dq_sem);
829                 dquot->dq_dqb.dqb_ihardlimit = ihardlimit;
830                 dquot->dq_dqb.dqb_isoftlimit = isoftlimit;
831                 dquot->dq_dqb.dqb_bhardlimit = bhardlimit;
832                 dquot->dq_dqb.dqb_bsoftlimit = bsoftlimit;
833                 dquot->dq_dqb.dqb_btime = btime;
834                 dquot->dq_dqb.dqb_itime = itime;
835                 fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
836                 up(&dquot->dq_sem);
837         }
838 out:
839         down(&dquot->dq_sem);
840         dquot->dq_status &= ~DQ_STATUS_SET;
841         up(&dquot->dq_sem);
842         lustre_dqput(dquot);
843         EXIT;
844 out_sem:
845         up(&mds->mds_qonoff_sem);
846         return rc;
847 }
848
849 static int mds_get_space(struct obd_device *obd, struct obd_quotactl *oqctl)
850 {
851         struct obd_quotactl *soqc;
852         struct lvfs_run_ctxt saved;
853         int rc;
854         ENTRY;
855
856         OBD_ALLOC_PTR(soqc);
857         if (!soqc)
858                 RETURN(-ENOMEM);
859
860         soqc->qc_cmd = Q_GETOQUOTA;
861         soqc->qc_id = oqctl->qc_id;
862         soqc->qc_type = oqctl->qc_type;
863
864         rc = obd_quotactl(obd->u.mds.mds_osc_exp, soqc);
865         if (rc)
866                GOTO(out, rc);
867
868         oqctl->qc_dqblk.dqb_curspace = soqc->qc_dqblk.dqb_curspace;
869
870         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
871         soqc->qc_dqblk.dqb_curspace = 0;
872         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, soqc);
873         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
874
875         if (rc)
876                 GOTO(out, rc);
877
878         oqctl->qc_dqblk.dqb_curinodes += soqc->qc_dqblk.dqb_curinodes;
879         oqctl->qc_dqblk.dqb_curspace += soqc->qc_dqblk.dqb_curspace;
880         EXIT;
881 out:
882         OBD_FREE_PTR(soqc);
883         return rc;
884 }
885
886 int mds_get_dqblk(struct obd_device *obd, struct obd_quotactl *oqctl)
887 {
888         struct mds_obd *mds = &obd->u.mds;
889         struct lustre_quota_info *qinfo = &mds->mds_quota_info;
890         struct lustre_dquot *dquot;
891         struct obd_dqblk *dqblk = &oqctl->qc_dqblk;
892         int rc;
893         ENTRY;
894
895         down(&mds->mds_qonoff_sem);
896         if (qinfo->qi_files[oqctl->qc_type] == NULL)
897                 GOTO(out, rc = -ESRCH);
898
899         dquot = lustre_dqget(obd, qinfo, oqctl->qc_id, oqctl->qc_type);
900         if (IS_ERR(dquot))
901                 GOTO(out, rc = PTR_ERR(dquot));
902
903         down(&dquot->dq_sem);
904         dqblk->dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
905         dqblk->dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
906         dqblk->dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
907         dqblk->dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
908         dqblk->dqb_btime = dquot->dq_dqb.dqb_btime;
909         dqblk->dqb_itime = dquot->dq_dqb.dqb_itime;
910         up(&dquot->dq_sem);
911
912         lustre_dqput(dquot);
913
914         /* the usages in admin quota file is inaccurate */
915         dqblk->dqb_curinodes = 0;
916         dqblk->dqb_curspace = 0;
917         rc = mds_get_space(obd, oqctl);
918         EXIT;
919 out:
920         up(&mds->mds_qonoff_sem);
921         return rc;
922 }
923
924 int mds_get_obd_quota(struct obd_device *obd, struct obd_quotactl *oqctl)
925 {
926         struct lvfs_run_ctxt saved;
927         int rc;
928         ENTRY;
929
930         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
931         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
932         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
933
934         RETURN(rc);
935 }
936
937
938 /* FIXME we only recovery block limit by now, need recovery inode
939  * limits also after CMD involved in */
940 static int 
941 dquot_recovery(struct obd_device *obd, unsigned int id, unsigned short type)
942 {
943         struct mds_obd *mds = &obd->u.mds;
944         struct lustre_quota_info *qinfo= &obd->u.mds.mds_quota_info;
945         struct lustre_dquot *dquot;
946         struct obd_quotactl *qctl;
947         __u64 total_limits = 0;
948         int rc;
949         ENTRY;
950
951         OBD_ALLOC_PTR(qctl);
952         if (qctl == NULL)
953                 RETURN(-ENOMEM);
954
955         dquot = lustre_dqget(obd, qinfo, id, type);
956         if (IS_ERR(dquot)) {
957                 CERROR("Get dquot failed. (rc:%ld)\n", PTR_ERR(dquot));
958                 OBD_FREE_PTR(qctl);
959                 RETURN(PTR_ERR(dquot));
960         }
961
962         down(&dquot->dq_sem);
963
964         /* don't recovery the dquot without limits or under setting */
965         if (!(dquot->dq_dqb.dqb_bhardlimit || dquot->dq_dqb.dqb_bsoftlimit) ||
966             dquot->dq_status)
967                 GOTO(skip, rc = 0);
968         dquot->dq_status |= DQ_STATUS_RECOVERY;
969
970         up(&dquot->dq_sem);
971
972         /* get real bhardlimit from all slaves. */
973         qctl->qc_cmd = Q_GETOQUOTA;
974         qctl->qc_type = type;
975         qctl->qc_id = id;
976         qctl->qc_stat = QUOTA_RECOVERING;
977         rc = obd_quotactl(obd->u.mds.mds_osc_exp, qctl);
978         if (rc)
979                 GOTO(out, rc);
980         total_limits = qctl->qc_dqblk.dqb_bhardlimit;
981
982         /* get real bhardlimit from master */
983         rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, qctl);
984         if (rc)
985                 GOTO(out, rc);
986         total_limits += qctl->qc_dqblk.dqb_bhardlimit;
987
988         /* amend the usage of the administrative quotafile */
989         down(&mds->mds_qonoff_sem);
990         down(&dquot->dq_sem);
991
992         dquot->dq_dqb.dqb_curspace = total_limits << QUOTABLOCK_BITS;
993
994         rc = fsfilt_dquot(obd, dquot, QFILE_WR_DQUOT);
995         if (rc)
996                 CERROR("write dquot failed! (rc:%d)\n", rc);
997
998         up(&dquot->dq_sem);
999         up(&mds->mds_qonoff_sem);
1000         EXIT;
1001 out:
1002         down(&dquot->dq_sem);
1003         dquot->dq_status &= ~DQ_STATUS_RECOVERY;
1004 skip:
1005         up(&dquot->dq_sem);
1006
1007         lustre_dqput(dquot);
1008         OBD_FREE_PTR(qctl);
1009         return rc;
1010 }
1011
1012 struct qmaster_recov_thread_data {
1013         struct obd_device *obd;
1014         struct completion comp;
1015 };
1016
1017 static int qmaster_recovery_main(void *arg)
1018 {
1019         struct qmaster_recov_thread_data *data = arg;
1020         struct obd_device *obd = data->obd;
1021         int rc = 0;
1022         unsigned short type;
1023         ENTRY;
1024
1025         ptlrpc_daemonize("qmaster_recovd");
1026
1027         complete(&data->comp);
1028
1029         for (type = USRQUOTA; type < MAXQUOTAS; type++) {
1030                 struct mds_obd *mds = &obd->u.mds;
1031                 struct lustre_quota_info *qinfo = &mds->mds_quota_info;
1032                 struct list_head id_list;
1033                 struct dquot_id *dqid, *tmp;
1034
1035                 down(&mds->mds_qonoff_sem);
1036                 if (qinfo->qi_files[type] == NULL) {
1037                         up(&mds->mds_qonoff_sem);
1038                         continue;
1039                 }
1040                 INIT_LIST_HEAD(&id_list);
1041                 rc = fsfilt_qids(obd, qinfo->qi_files[type], NULL, type, 
1042                                  &id_list);
1043                 up(&mds->mds_qonoff_sem);
1044
1045                 if (rc)
1046                         CERROR("error get ids from admin quotafile.(%d)\n", rc);
1047
1048                 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
1049                         list_del_init(&dqid->di_link);
1050                         if (rc)
1051                                 goto free;
1052
1053                         rc = dquot_recovery(obd, dqid->di_id, type);
1054                         if (rc)
1055                                 CERROR("qmaster recovery failed! (id:%d type:%d"
1056                                        " rc:%d)\n", dqid->di_id, type, rc);
1057 free:
1058                         kfree(dqid);
1059                 }
1060         }
1061         RETURN(rc);
1062 }
1063
1064 int mds_quota_recovery(struct obd_device *obd)
1065 {
1066         struct lov_obd *lov = &obd->u.mds.mds_osc_obd->u.lov;
1067         struct qmaster_recov_thread_data data;
1068         int rc = 0;
1069         ENTRY;
1070
1071         down(&lov->lov_lock);
1072         if (lov->desc.ld_tgt_count != lov->desc.ld_active_tgt_count) {
1073                 CWARN("Not all osts are active, abort quota recovery\n");
1074                 up(&lov->lov_lock);
1075                 RETURN(rc);
1076         }
1077         up(&lov->lov_lock);
1078
1079         data.obd = obd;
1080         init_completion(&data.comp);
1081
1082         rc = kernel_thread(qmaster_recovery_main, &data, CLONE_VM|CLONE_FILES);
1083         if (rc < 0)
1084                 CERROR("Cannot start quota recovery thread: rc %d\n", rc);
1085
1086         wait_for_completion(&data.comp);
1087         RETURN(rc);
1088 }