Whamcloud - gitweb
LU-1214 quota: don't compile quota module for client
[fs/lustre-release.git] / lustre / quota / quota_interface.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LQUOTA
41
42 #ifdef __KERNEL__
43 # include <linux/version.h>
44 # include <linux/module.h>
45 # include <linux/init.h>
46 # include <linux/fs.h>
47 # include <linux/jbd.h>
48 # include <linux/smp_lock.h>
49 # include <linux/buffer_head.h>
50 # include <linux/workqueue.h>
51 # include <linux/mount.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 #endif
55
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lustre_dlm.h>
59 #include <lustre_cfg.h>
60 #include <obd_ost.h>
61 #include <lustre_fsfilt.h>
62 #include <lustre_quota.h>
63 #include <lprocfs_status.h>
64 #include "quota_internal.h"
65
66 #ifdef __KERNEL__
67
68 static cfs_time_t last_print = 0;
69 static cfs_spinlock_t last_print_lock = CFS_SPIN_LOCK_UNLOCKED;
70
71 static int filter_quota_setup(struct obd_device *obd)
72 {
73         int rc = 0;
74         struct obd_device_target *obt = &obd->u.obt;
75         ENTRY;
76
77         cfs_init_rwsem(&obt->obt_rwsem);
78         obt->obt_qfmt = LUSTRE_QUOTA_V2;
79         cfs_sema_init(&obt->obt_quotachecking, 1);
80         rc = qctxt_init(obd, NULL);
81         if (rc)
82                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
83
84         RETURN(rc);
85 }
86
87 static int filter_quota_cleanup(struct obd_device *obd)
88 {
89         ENTRY;
90         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
91         RETURN(0);
92 }
93
94 static int filter_quota_setinfo(struct obd_device *obd, void *data)
95 {
96         struct obd_export *exp = data;
97         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
98         struct obd_import *imp = exp->exp_imp_reverse;
99         ENTRY;
100
101         LASSERT(imp != NULL);
102
103         /* setup the quota context import */
104         cfs_spin_lock(&qctxt->lqc_lock);
105         if (qctxt->lqc_import != NULL) {
106                 cfs_spin_unlock(&qctxt->lqc_lock);
107                 if (qctxt->lqc_import == imp)
108                         CDEBUG(D_WARNING, "%s: lqc_import(%p) of obd(%p) was "
109                                "activated already.\n", obd->obd_name, imp, obd);
110                 else
111                         CERROR("%s: lqc_import(%p:%p) of obd(%p) was "
112                                "activated by others.\n", obd->obd_name,
113                                qctxt->lqc_import, imp, obd);
114         } else {
115                 qctxt->lqc_import = imp;
116                 /* make imp's connect flags equal relative exp's connect flags
117                  * adding it to avoid the scan export list */
118                 imp->imp_connect_data.ocd_connect_flags |=
119                                 (exp->exp_connect_flags &
120                                  (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
121                 cfs_spin_unlock(&qctxt->lqc_lock);
122                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated "
123                        "now.\n", obd->obd_name, imp, obd);
124
125                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
126                 /* start quota slave recovery thread. (release high limits) */
127                 qslave_start_recovery(obd, qctxt);
128         }
129         RETURN(0);
130 }
131
132 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
133 {
134         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
135         struct obd_import *imp = exp->exp_imp_reverse;
136         ENTRY;
137
138         /* lquota may be not set up before destroying export, b=14896 */
139         if (!obd->obd_set_up)
140                 RETURN(0);
141
142         if (unlikely(imp == NULL))
143                 RETURN(0);
144
145         /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
146          * should be invalid b=12374 */
147         cfs_spin_lock(&qctxt->lqc_lock);
148         if (qctxt->lqc_import == imp) {
149                 qctxt->lqc_import = NULL;
150                 cfs_spin_unlock(&qctxt->lqc_lock);
151                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is invalid now.\n",
152                        obd->obd_name, imp, obd);
153                 ptlrpc_cleanup_imp(imp);
154                 dqacq_interrupt(qctxt);
155         } else {
156                 cfs_spin_unlock(&qctxt->lqc_lock);
157         }
158         RETURN(0);
159 }
160
161 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
162 {
163         ENTRY;
164
165         if (!ll_sb_any_quota_active(obd->u.obt.obt_sb))
166                 RETURN(0);
167
168         if (ignore) {
169                 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
170                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
171         } else {
172                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
173         }
174
175         RETURN(0);
176 }
177
178 #define GET_OA_ID(flag, oa) (flag == USRQUOTA ? oa->o_uid : oa->o_gid)
179 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
180 {
181         struct obd_device_target *obt = &obd->u.obt;
182         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
183         int err, cnt, rc = 0;
184         struct obd_quotactl *oqctl;
185         ENTRY;
186
187         if (!ll_sb_any_quota_active(obt->obt_sb))
188                 RETURN(0);
189
190         OBD_ALLOC_PTR(oqctl);
191         if (!oqctl)
192                 RETURN(-ENOMEM);
193
194         /* set over quota flags for a uid/gid */
195         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
196         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
197
198         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
199                 struct lustre_qunit_size *lqs = NULL;
200
201                 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
202                                        qctxt, 0);
203                 if (IS_ERR(lqs)) {
204                         rc = PTR_ERR(lqs);
205                         CDEBUG(D_QUOTA, "search lqs for %s %d failed, "
206                                "(rc = %d)\n",
207                                cnt == USRQUOTA ? "user" : "group",
208                                GET_OA_ID(cnt, oa), rc);
209                         break;
210                 } else if (lqs == NULL) {
211                         /* continue to check group quota if the file's owner
212                          * doesn't have quota limit. LU-530 */
213                         continue;
214                 } else {
215                         cfs_spin_lock(&lqs->lqs_lock);
216                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
217                                 oa->o_flags |= (cnt == USRQUOTA) ?
218                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
219                                 cfs_spin_unlock(&lqs->lqs_lock);
220                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
221                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
222                                        qctxt->lqc_sync_blk);
223                                 /* this is for quota_search_lqs */
224                                 lqs_putref(lqs);
225                                 continue;
226                         }
227                         cfs_spin_unlock(&lqs->lqs_lock);
228                         /* this is for quota_search_lqs */
229                         lqs_putref(lqs);
230                 }
231
232                 memset(oqctl, 0, sizeof(*oqctl));
233
234                 oqctl->qc_cmd = Q_GETQUOTA;
235                 oqctl->qc_type = cnt;
236                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
237                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
238                 if (err) {
239                         if (!rc)
240                                 rc = err;
241                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
242                                                              OBD_MD_FLGRPQUOTA);
243                         CDEBUG(D_QUOTA, "fsfilt getquota for %s %d failed, "
244                                "(rc = %d)\n",
245                                cnt == USRQUOTA ? "user" : "group",
246                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid, err);
247                         continue;
248                 }
249
250                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
251                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
252                     oqctl->qc_dqblk.dqb_bhardlimit)) {
253                         oa->o_flags |= (cnt == USRQUOTA) ?
254                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
255                         CDEBUG(D_QUOTA, "out of quota for %s %d\n",
256                                cnt == USRQUOTA ? "user" : "group",
257                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid);
258                 }
259         }
260         OBD_FREE_PTR(oqctl);
261         RETURN(rc);
262 }
263
264 /**
265  * check whether the left quota of certain uid and gid can satisfy a block_write
266  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
267  */
268 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
269                               int pending[], int count, int cycle, int isblk,
270                               struct inode *inode, int frags)
271 {
272         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
273         int i;
274         struct qunit_data qdata[MAXQUOTAS];
275         int mb = 0;
276         int rc = 0, rc2[2] = { 0, 0 };
277         ENTRY;
278
279         cfs_spin_lock(&qctxt->lqc_lock);
280         if (!qctxt->lqc_valid){
281                 cfs_spin_unlock(&qctxt->lqc_lock);
282                 RETURN(rc);
283         }
284         cfs_spin_unlock(&qctxt->lqc_lock);
285
286         for (i = 0; i < MAXQUOTAS; i++) {
287                 struct lustre_qunit_size *lqs = NULL;
288
289                 qdata[i].qd_id = id[i];
290                 qdata[i].qd_flags = i;
291                 if (isblk)
292                         QDATA_SET_BLK(&qdata[i]);
293                 qdata[i].qd_count = 0;
294
295                 /* ignore root user */
296                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
297                         continue;
298
299                 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
300                 if (lqs == NULL || IS_ERR(lqs))
301                         continue;
302
303                 if (IS_ERR(lqs)) {
304                         CERROR("can not find lqs for check_common: "
305                                "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
306                                id[i], i % 2 ? 'g': 'u', isblk, count,
307                                PTR_ERR(lqs));
308                         RETURN(PTR_ERR(lqs));
309                 }
310
311                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
312                 cfs_spin_lock(&lqs->lqs_lock);
313                 if (!cycle) {
314                         if (isblk) {
315                                 pending[i] = count * CFS_PAGE_SIZE;
316                                 /* in order to complete this write, we need extra
317                                  * meta blocks. This function can get it through
318                                  * data needed to be written b=16542 */
319                                 if (inode) {
320                                         mb = pending[i];
321                                         rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
322                                                              &mb, inode,
323                                                              frags);
324                                         if (rc)
325                                                 CERROR("%s: can't get extra "
326                                                        "meta blocks\n",
327                                                        obd->obd_name);
328                                         else
329                                                 pending[i] += mb;
330                                 }
331                                 LASSERTF(pending[i] >= 0, "pending is not valid"
332                                          ", count=%d, mb=%d\n", count, mb);
333                                 lqs->lqs_bwrite_pending += pending[i];
334                         } else {
335                                 pending[i] = count;
336                                 lqs->lqs_iwrite_pending += pending[i];
337                         }
338                 }
339
340                 /* if xx_rec < 0, that means quota are releasing,
341                  * and it may return before we use quota. So if
342                  * we find this situation, we assuming it has
343                  * returned b=18491 */
344                 if (isblk && lqs->lqs_blk_rec < 0) {
345                         if (qdata[i].qd_count < -lqs->lqs_blk_rec)
346                                 qdata[i].qd_count = 0;
347                         else
348                                 qdata[i].qd_count += lqs->lqs_blk_rec;
349                 }
350                 if (!isblk && lqs->lqs_ino_rec < 0) {
351                         if (qdata[i].qd_count < -lqs->lqs_ino_rec)
352                                 qdata[i].qd_count = 0;
353                         else
354                                 qdata[i].qd_count += lqs->lqs_ino_rec;
355                 }
356
357                 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
358                        " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
359                        " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
360                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
361                        qdata[i].qd_count, mb, pending[i]);
362                 if (rc2[i] == QUOTA_RET_OK) {
363                         if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
364                                 rc2[i] = QUOTA_RET_ACQUOTA;
365                         if (!isblk && qdata[i].qd_count <
366                             lqs->lqs_iwrite_pending)
367                                 rc2[i] = QUOTA_RET_ACQUOTA;
368                 }
369
370                 cfs_spin_unlock(&lqs->lqs_lock);
371
372                 if (lqs->lqs_blk_rec  < 0 &&
373                     qdata[i].qd_count <
374                     lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
375                         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
376
377                 /* When cycle is zero, lqs_*_pending will be changed. We will
378                  * get reference of the lqs here and put reference of lqs in
379                  * quota_pending_commit b=14784 */
380                 if (!cycle)
381                         lqs_getref(lqs);
382
383                 /* this is for quota_search_lqs */
384                 lqs_putref(lqs);
385         }
386
387         if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
388                 RETURN(QUOTA_RET_ACQUOTA);
389         else
390                 RETURN(rc);
391 }
392
393 int quota_is_set(struct obd_device *obd, const unsigned int id[], int flag)
394 {
395         struct lustre_qunit_size *lqs;
396         int i, q_set = 0;
397
398         if (!ll_sb_any_quota_active(obd->u.obt.obt_qctxt.lqc_sb))
399                 RETURN(0);
400
401         for (i = 0; i < MAXQUOTAS; i++) {
402                 lqs = quota_search_lqs(LQS_KEY(i, id[i]),
403                                        &obd->u.obt.obt_qctxt, 0);
404                 if (lqs && !IS_ERR(lqs)) {
405                         if (lqs->lqs_flags & flag)
406                                 q_set = 1;
407                         lqs_putref(lqs);
408                 }
409         }
410
411         return q_set;
412 }
413
414 static int quota_chk_acq_common(struct obd_device *obd, struct obd_export *exp,
415                                 const unsigned int id[], int pending[],
416                                 int count, quota_acquire acquire,
417                                 struct obd_trans_info *oti, int isblk,
418                                 struct inode *inode, int frags)
419 {
420         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
421         struct timeval work_start;
422         struct timeval work_end;
423         long timediff;
424         struct l_wait_info lwi = { 0 };
425         int rc = 0, cycle = 0, count_err = 1;
426         ENTRY;
427
428         if (!quota_is_set(obd, id, isblk ? QB_SET : QI_SET))
429                 RETURN(0);
430
431         if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
432                 /* If the client has been evicted or if it
433                  * timed out and tried to reconnect already,
434                  * abort the request immediately */
435                 RETURN(-ENOTCONN);
436
437         CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
438         pending[USRQUOTA] = pending[GRPQUOTA] = 0;
439         /* Unfortunately, if quota master is too busy to handle the
440          * pre-dqacq in time and quota hash on ost is used up, we
441          * have to wait for the completion of in flight dqacq/dqrel,
442          * in order to get enough quota for write b=12588 */
443         cfs_gettimeofday(&work_start);
444         while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
445                                         inode, frags)) &
446                QUOTA_RET_ACQUOTA) {
447
448                 cfs_spin_lock(&qctxt->lqc_lock);
449                 if (!qctxt->lqc_import && oti) {
450                         cfs_spin_unlock(&qctxt->lqc_lock);
451                         LASSERT(oti->oti_thread);
452                         /* The recovery thread doesn't have watchdog
453                          * attached. LU-369 */
454                         if (oti->oti_thread->t_watchdog)
455                                 lc_watchdog_disable(oti->oti_thread->\
456                                                 t_watchdog);
457                         CDEBUG(D_QUOTA, "sleep for quota master\n");
458                         l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
459                                      &lwi);
460                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
461                         if (oti->oti_thread->t_watchdog)
462                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
463                                        CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
464                 } else {
465                         cfs_spin_unlock(&qctxt->lqc_lock);
466                 }
467
468                 cycle++;
469                 if (isblk)
470                         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
471                 /* after acquire(), we should run quota_check_common again
472                  * so that we confirm there are enough quota to finish write */
473                 rc = acquire(obd, id, oti, isblk);
474
475                 /* please reference to dqacq_completion for the below */
476                 /* a new request is finished, try again */
477                 if (rc == QUOTA_REQ_RETURNED) {
478                         CDEBUG(D_QUOTA, "finish a quota req, try again\n");
479                         continue;
480                 }
481
482                 /* it is out of quota already */
483                 if (rc == -EDQUOT) {
484                         CDEBUG(D_QUOTA, "out of quota,  return -EDQUOT\n");
485                         break;
486                 }
487
488                 /* Related quota has been disabled by master, but enabled by
489                  * slave, do not try again. */
490                 if (unlikely(rc == -ESRCH)) {
491                         CERROR("mismatched quota configuration, stop try.\n");
492                         break;
493                 }
494
495                 if (isblk && (exp->exp_failed || exp->exp_abort_active_req))
496                         /* The client has been evicted or tried to
497                          * to reconnect already, abort the request */
498                         RETURN(-ENOTCONN);
499
500                 /* -EBUSY and others, wait a second and try again */
501                 if (rc < 0) {
502                         cfs_waitq_t        waitq;
503                         struct l_wait_info lwi;
504
505                         if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
506                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
507                                        CFS_GET_TIMEOUT(oti->oti_thread->t_svc));
508                         CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
509                                count_err++);
510
511                         cfs_waitq_init(&waitq);
512                         lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
513                                           NULL);
514                         l_wait_event(waitq, 0, &lwi);
515                 }
516
517                 if (rc < 0 || cycle % 10 == 0) {
518                         cfs_spin_lock(&last_print_lock);
519                         if (last_print == 0 ||
520                             cfs_time_before((last_print + cfs_time_seconds(30)),
521                                             cfs_time_current())) {
522                                 last_print = cfs_time_current();
523                                 cfs_spin_unlock(&last_print_lock);
524                                 CWARN("still haven't managed to acquire quota "
525                                       "space from the quota master after %d "
526                                       "retries (err=%d, rc=%d)\n",
527                                       cycle, count_err - 1, rc);
528                         } else {
529                                 cfs_spin_unlock(&last_print_lock);
530                         }
531                 }
532
533                 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
534                        cycle);
535         }
536         cfs_gettimeofday(&work_end);
537         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
538         lprocfs_counter_add(qctxt->lqc_stats,
539                             isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
540                                     LQUOTA_WAIT_FOR_CHK_INO,
541                             timediff);
542
543         if (rc > 0)
544                 rc = 0;
545         RETURN(rc);
546 }
547
548 /**
549  * when a block_write or inode_create rpc is finished, adjust the record for
550  * pending blocks and inodes
551  */
552 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
553                                 int pending[], int isblk)
554 {
555         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
556         struct timeval work_start;
557         struct timeval work_end;
558         long timediff;
559         int i;
560         struct qunit_data qdata[MAXQUOTAS];
561         ENTRY;
562
563         CDEBUG(D_QUOTA, "commit pending quota for  %s\n", obd->obd_name);
564         CLASSERT(MAXQUOTAS < 4);
565         if (!ll_sb_any_quota_active(qctxt->lqc_sb))
566                 RETURN(0);
567
568         cfs_gettimeofday(&work_start);
569         for (i = 0; i < MAXQUOTAS; i++) {
570                 struct lustre_qunit_size *lqs = NULL;
571
572                 LASSERT(pending[i] >= 0);
573                 if (pending[i] == 0)
574                         continue;
575
576                 qdata[i].qd_id = id[i];
577                 qdata[i].qd_flags = i;
578                 if (isblk)
579                         QDATA_SET_BLK(&qdata[i]);
580                 qdata[i].qd_count = 0;
581
582                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
583                         continue;
584
585                 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
586                 if (lqs == NULL || IS_ERR(lqs)) {
587                         CERROR("can not find lqs for pending_commit: "
588                                "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
589                                "maybe cause unexpected lqs refcount error!\n",
590                                id[i], i ? 'g': 'u', pending[i], isblk,
591                                lqs ? PTR_ERR(lqs) : -1);
592                         continue;
593                 }
594
595                 cfs_spin_lock(&lqs->lqs_lock);
596                 if (isblk) {
597                         LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
598                                  "there are too many blocks! [id %u] [%c] "
599                                  "[bwrite_pending %lu] [pending %u]\n",
600                                  id[i], i % 2 ? 'g' : 'u',
601                                  lqs->lqs_bwrite_pending, pending[i]);
602
603                         lqs->lqs_bwrite_pending -= pending[i];
604                 } else {
605                         LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
606                                 "there are too many files! [id %u] [%c] "
607                                 "[iwrite_pending %lu] [pending %u]\n",
608                                 id[i], i % 2 ? 'g' : 'u',
609                                 lqs->lqs_iwrite_pending, pending[i]);
610
611                         lqs->lqs_iwrite_pending -= pending[i];
612                 }
613                 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
614                        obd->obd_name,
615                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
616                        i, pending[i], isblk);
617                 cfs_spin_unlock(&lqs->lqs_lock);
618
619                 /* for quota_search_lqs in pending_commit */
620                 lqs_putref(lqs);
621                 /* for quota_search_lqs in quota_check */
622                 lqs_putref(lqs);
623         }
624         cfs_gettimeofday(&work_end);
625         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
626         lprocfs_counter_add(qctxt->lqc_stats,
627                             isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
628                                     LQUOTA_WAIT_FOR_COMMIT_INO,
629                             timediff);
630
631         RETURN(0);
632 }
633
634 static int mds_quota_init(void)
635 {
636         return lustre_dquot_init();
637 }
638
639 static int mds_quota_exit(void)
640 {
641         lustre_dquot_exit();
642         return 0;
643 }
644
645 static int mds_quota_setup(struct obd_device *obd)
646 {
647         struct obd_device_target *obt = &obd->u.obt;
648         struct mds_obd *mds = &obd->u.mds;
649         int rc;
650         ENTRY;
651
652         if (unlikely(mds->mds_quota)) {
653                 CWARN("try to reinitialize quota context!\n");
654                 RETURN(0);
655         }
656
657         cfs_init_rwsem(&obt->obt_rwsem);
658         obt->obt_qfmt = LUSTRE_QUOTA_V2;
659         mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
660         cfs_sema_init(&obt->obt_quotachecking, 1);
661         /* initialize quota master and quota context */
662         cfs_init_rwsem(&mds->mds_qonoff_sem);
663         rc = qctxt_init(obd, dqacq_handler);
664         if (rc) {
665                 CERROR("%s: initialize quota context failed! (rc:%d)\n",
666                        obd->obd_name, rc);
667                 RETURN(rc);
668         }
669         mds->mds_quota = 1;
670         RETURN(rc);
671 }
672
673 static int mds_quota_cleanup(struct obd_device *obd)
674 {
675         ENTRY;
676         if (unlikely(!obd->u.mds.mds_quota))
677                 RETURN(0);
678
679         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
680         RETURN(0);
681 }
682
683 static int mds_quota_setinfo(struct obd_device *obd, void *data)
684 {
685         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
686         ENTRY;
687
688         if (unlikely(!obd->u.mds.mds_quota))
689                 RETURN(0);
690
691         if (data != NULL)
692                 QUOTA_MASTER_READY(qctxt);
693         else
694                 QUOTA_MASTER_UNREADY(qctxt);
695         RETURN(0);
696 }
697
698 static int mds_quota_fs_cleanup(struct obd_device *obd)
699 {
700         struct mds_obd *mds = &obd->u.mds;
701         struct obd_quotactl oqctl;
702         ENTRY;
703
704         if (unlikely(!mds->mds_quota))
705                 RETURN(0);
706
707         mds->mds_quota = 0;
708         memset(&oqctl, 0, sizeof(oqctl));
709         oqctl.qc_type = UGQUOTA;
710
711         cfs_down_write(&mds->mds_qonoff_sem);
712         mds_admin_quota_off(obd, &oqctl);
713         cfs_up_write(&mds->mds_qonoff_sem);
714         RETURN(0);
715 }
716
717 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
718                                 struct obd_trans_info *oti, int isblk)
719 {
720         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
721         int rc;
722         ENTRY;
723
724         rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
725         RETURN(rc);
726 }
727
728 quota_interface_t mds_quota_interface = {
729         .quota_init     = mds_quota_init,
730         .quota_exit     = mds_quota_exit,
731         .quota_setup    = mds_quota_setup,
732         .quota_cleanup  = mds_quota_cleanup,
733         .quota_check    = target_quota_check,
734         .quota_ctl      = mds_quota_ctl,
735         .quota_setinfo  = mds_quota_setinfo,
736         .quota_fs_cleanup = mds_quota_fs_cleanup,
737         .quota_recovery = mds_quota_recovery,
738         .quota_adjust   = mds_quota_adjust,
739         .quota_chkquota = quota_chk_acq_common,
740         .quota_acquire  = quota_acquire_common,
741         .quota_pending_commit = quota_pending_commit,
742 };
743
744 quota_interface_t filter_quota_interface = {
745         .quota_setup    = filter_quota_setup,
746         .quota_cleanup  = filter_quota_cleanup,
747         .quota_check    = target_quota_check,
748         .quota_ctl      = filter_quota_ctl,
749         .quota_setinfo  = filter_quota_setinfo,
750         .quota_clearinfo = filter_quota_clearinfo,
751         .quota_enforce  = filter_quota_enforce,
752         .quota_getflag  = filter_quota_getflag,
753         .quota_acquire  = quota_acquire_common,
754         .quota_adjust   = filter_quota_adjust,
755         .quota_chkquota = quota_chk_acq_common,
756         .quota_adjust_qunit   = filter_quota_adjust_qunit,
757         .quota_pending_commit = quota_pending_commit,
758 };
759
760 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
761
762 static int __init init_lustre_quota(void)
763 {
764         int rc = 0;
765
766         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
767                                                 proc_lustre_root,
768                                                 NULL, NULL);
769         if (IS_ERR(lquota_type_proc_dir)) {
770                 CERROR("LProcFS failed in lquota-init\n");
771                 rc = PTR_ERR(lquota_type_proc_dir);
772                 return rc;
773         }
774
775         rc = qunit_cache_init();
776         if (rc)
777                 return rc;
778
779         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
780         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
781
782         return 0;
783 }
784
785 static void /*__exit*/ exit_lustre_quota(void)
786 {
787         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
788         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
789
790         qunit_cache_cleanup();
791
792         if (lquota_type_proc_dir)
793                 lprocfs_remove(&lquota_type_proc_dir);
794 }
795
796 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
797 MODULE_DESCRIPTION("Lustre Quota");
798 MODULE_LICENSE("GPL");
799
800 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
801
802 EXPORT_SYMBOL(mds_quota_interface);
803 EXPORT_SYMBOL(filter_quota_interface);
804 #endif /* __KERNEL */