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