Whamcloud - gitweb
LU-530 group quotas not enforced
[fs/lustre-release.git] / lustre / quota / quota_interface.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifdef HAVE_QUOTA_SUPPORT
69
70 static cfs_time_t last_print = 0;
71 static cfs_spinlock_t last_print_lock = CFS_SPIN_LOCK_UNLOCKED;
72
73 static int filter_quota_setup(struct obd_device *obd)
74 {
75         int rc = 0;
76         struct obd_device_target *obt = &obd->u.obt;
77         ENTRY;
78
79         cfs_init_rwsem(&obt->obt_rwsem);
80         obt->obt_qfmt = LUSTRE_QUOTA_V2;
81         cfs_sema_init(&obt->obt_quotachecking, 1);
82         rc = qctxt_init(obd, NULL);
83         if (rc)
84                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
85
86         RETURN(rc);
87 }
88
89 static int filter_quota_cleanup(struct obd_device *obd)
90 {
91         ENTRY;
92         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
93         RETURN(0);
94 }
95
96 static int filter_quota_setinfo(struct obd_device *obd, void *data)
97 {
98         struct obd_export *exp = data;
99         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
100         struct obd_import *imp = exp->exp_imp_reverse;
101         ENTRY;
102
103         LASSERT(imp != NULL);
104
105         /* setup the quota context import */
106         cfs_spin_lock(&qctxt->lqc_lock);
107         if (qctxt->lqc_import != NULL) {
108                 cfs_spin_unlock(&qctxt->lqc_lock);
109                 if (qctxt->lqc_import == imp)
110                         CDEBUG(D_WARNING, "%s: lqc_import(%p) of obd(%p) was "
111                                "activated already.\n", obd->obd_name, imp, obd);
112                 else
113                         CERROR("%s: lqc_import(%p:%p) of obd(%p) was "
114                                "activated by others.\n", obd->obd_name,
115                                qctxt->lqc_import, imp, obd);
116         } else {
117                 qctxt->lqc_import = imp;
118                 /* make imp's connect flags equal relative exp's connect flags
119                  * adding it to avoid the scan export list */
120                 imp->imp_connect_data.ocd_connect_flags |=
121                                 (exp->exp_connect_flags &
122                                  (OBD_CONNECT_QUOTA64 | OBD_CONNECT_CHANGE_QS));
123                 cfs_spin_unlock(&qctxt->lqc_lock);
124                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is reactivated "
125                        "now.\n", obd->obd_name, imp, obd);
126
127                 cfs_waitq_signal(&qctxt->lqc_wait_for_qmaster);
128                 /* start quota slave recovery thread. (release high limits) */
129                 qslave_start_recovery(obd, qctxt);
130         }
131         RETURN(0);
132 }
133
134 static int filter_quota_clearinfo(struct obd_export *exp, struct obd_device *obd)
135 {
136         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
137         struct obd_import *imp = exp->exp_imp_reverse;
138         ENTRY;
139
140         /* lquota may be not set up before destroying export, b=14896 */
141         if (!obd->obd_set_up)
142                 RETURN(0);
143
144         if (unlikely(imp == NULL))
145                 RETURN(0);
146
147         /* when exp->exp_imp_reverse is destroyed, the corresponding lqc_import
148          * should be invalid b=12374 */
149         cfs_spin_lock(&qctxt->lqc_lock);
150         if (qctxt->lqc_import == imp) {
151                 qctxt->lqc_import = NULL;
152                 cfs_spin_unlock(&qctxt->lqc_lock);
153                 CDEBUG(D_QUOTA, "%s: lqc_import(%p) of obd(%p) is invalid now.\n",
154                        obd->obd_name, imp, obd);
155                 ptlrpc_cleanup_imp(imp);
156                 dqacq_interrupt(qctxt);
157         } else {
158                 cfs_spin_unlock(&qctxt->lqc_lock);
159         }
160         RETURN(0);
161 }
162
163 static int filter_quota_enforce(struct obd_device *obd, unsigned int ignore)
164 {
165         ENTRY;
166
167         if (!ll_sb_any_quota_active(obd->u.obt.obt_sb))
168                 RETURN(0);
169
170         if (ignore) {
171                 CDEBUG(D_QUOTA, "blocks will be written with ignoring quota.\n");
172                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
173         } else {
174                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
175         }
176
177         RETURN(0);
178 }
179
180 #define GET_OA_ID(flag, oa) (flag == USRQUOTA ? oa->o_uid : oa->o_gid)
181 static int filter_quota_getflag(struct obd_device *obd, struct obdo *oa)
182 {
183         struct obd_device_target *obt = &obd->u.obt;
184         struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
185         int err, cnt, rc = 0;
186         struct obd_quotactl *oqctl;
187         ENTRY;
188
189         if (!ll_sb_any_quota_active(obt->obt_sb))
190                 RETURN(0);
191
192         OBD_ALLOC_PTR(oqctl);
193         if (!oqctl)
194                 RETURN(-ENOMEM);
195
196         /* set over quota flags for a uid/gid */
197         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
198         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
199
200         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
201                 struct lustre_qunit_size *lqs = NULL;
202
203                 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
204                                        qctxt, 0);
205                 if (IS_ERR(lqs)) {
206                         rc = PTR_ERR(lqs);
207                         CDEBUG(D_QUOTA, "search lqs for %s %d failed, "
208                                "(rc = %d)\n",
209                                cnt == USRQUOTA ? "user" : "group",
210                                GET_OA_ID(cnt, oa), rc);
211                         break;
212                 } else if (lqs == NULL) {
213                         /* continue to check group quota if the file's owner
214                          * doesn't have quota limit. LU-530 */
215                         continue;
216                 } else {
217                         cfs_spin_lock(&lqs->lqs_lock);
218                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
219                                 oa->o_flags |= (cnt == USRQUOTA) ?
220                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
221                                 cfs_spin_unlock(&lqs->lqs_lock);
222                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
223                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
224                                        qctxt->lqc_sync_blk);
225                                 /* this is for quota_search_lqs */
226                                 lqs_putref(lqs);
227                                 continue;
228                         }
229                         cfs_spin_unlock(&lqs->lqs_lock);
230                         /* this is for quota_search_lqs */
231                         lqs_putref(lqs);
232                 }
233
234                 memset(oqctl, 0, sizeof(*oqctl));
235
236                 oqctl->qc_cmd = Q_GETQUOTA;
237                 oqctl->qc_type = cnt;
238                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
239                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
240                 if (err) {
241                         if (!rc)
242                                 rc = err;
243                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
244                                                              OBD_MD_FLGRPQUOTA);
245                         CDEBUG(D_QUOTA, "fsfilt getquota for %s %d failed, "
246                                "(rc = %d)\n",
247                                cnt == USRQUOTA ? "user" : "group",
248                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid, err);
249                         continue;
250                 }
251
252                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
253                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
254                     oqctl->qc_dqblk.dqb_bhardlimit)) {
255                         oa->o_flags |= (cnt == USRQUOTA) ?
256                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
257                         CDEBUG(D_QUOTA, "out of quota for %s %d\n",
258                                cnt == USRQUOTA ? "user" : "group",
259                                cnt == USRQUOTA ? oa->o_uid : oa->o_gid);
260                 }
261         }
262         OBD_FREE_PTR(oqctl);
263         RETURN(rc);
264 }
265
266 /**
267  * check whether the left quota of certain uid and gid can satisfy a block_write
268  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
269  */
270 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
271                               int pending[], int count, int cycle, int isblk,
272                               struct inode *inode, int frags)
273 {
274         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
275         int i;
276         struct qunit_data qdata[MAXQUOTAS];
277         int mb = 0;
278         int rc = 0, rc2[2] = { 0, 0 };
279         ENTRY;
280
281         cfs_spin_lock(&qctxt->lqc_lock);
282         if (!qctxt->lqc_valid){
283                 cfs_spin_unlock(&qctxt->lqc_lock);
284                 RETURN(rc);
285         }
286         cfs_spin_unlock(&qctxt->lqc_lock);
287
288         for (i = 0; i < MAXQUOTAS; i++) {
289                 struct lustre_qunit_size *lqs = NULL;
290
291                 qdata[i].qd_id = id[i];
292                 qdata[i].qd_flags = i;
293                 if (isblk)
294                         QDATA_SET_BLK(&qdata[i]);
295                 qdata[i].qd_count = 0;
296
297                 /* ignore root user */
298                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
299                         continue;
300
301                 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
302                 if (lqs == NULL || IS_ERR(lqs))
303                         continue;
304
305                 if (IS_ERR(lqs)) {
306                         CERROR("can not find lqs for check_common: "
307                                "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
308                                id[i], i % 2 ? 'g': 'u', isblk, count,
309                                PTR_ERR(lqs));
310                         RETURN(PTR_ERR(lqs));
311                 }
312
313                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
314                 cfs_spin_lock(&lqs->lqs_lock);
315                 if (!cycle) {
316                         if (isblk) {
317                                 pending[i] = count * CFS_PAGE_SIZE;
318                                 /* in order to complete this write, we need extra
319                                  * meta blocks. This function can get it through
320                                  * data needed to be written b=16542 */
321                                 if (inode) {
322                                         mb = pending[i];
323                                         rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
324                                                              &mb, inode,
325                                                              frags);
326                                         if (rc)
327                                                 CERROR("%s: can't get extra "
328                                                        "meta blocks\n",
329                                                        obd->obd_name);
330                                         else
331                                                 pending[i] += mb;
332                                 }
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 #endif /* HAVE_QUOTA_SUPPORT */
729 #endif /* __KERNEL__ */
730
731 struct osc_quota_info {
732         cfs_list_t              oqi_hash;       /* hash list */
733         struct client_obd      *oqi_cli;        /* osc obd */
734         unsigned int            oqi_id;         /* uid/gid of a file */
735         short                   oqi_type;       /* quota type */
736 };
737
738 cfs_spinlock_t qinfo_list_lock = CFS_SPIN_LOCK_UNLOCKED;
739
740 static cfs_list_t qinfo_hash[NR_DQHASH];
741 /* SLAB cache for client quota context */
742 cfs_mem_cache_t *qinfo_cachep = NULL;
743
744 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
745                          __attribute__((__const__));
746
747 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
748 {
749         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
750         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
751         return tmp;
752 }
753
754 /* caller must hold qinfo_list_lock */
755 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
756 {
757         cfs_list_t *head = qinfo_hash +
758                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
759
760         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
761         cfs_list_add(&oqi->oqi_hash, head);
762 }
763
764 /* caller must hold qinfo_list_lock */
765 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
766 {
767         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
768         cfs_list_del_init(&oqi->oqi_hash);
769 }
770
771 /* caller must hold qinfo_list_lock */
772 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
773                                                 unsigned int id, int type)
774 {
775         unsigned int hashent = hashfn(cli, id, type);
776         struct osc_quota_info *oqi;
777         ENTRY;
778
779         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
780         cfs_list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
781                 if (oqi->oqi_cli == cli &&
782                     oqi->oqi_id == id && oqi->oqi_type == type)
783                         return oqi;
784         }
785         RETURN(NULL);
786 }
787
788 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
789                                           unsigned int id, int type)
790 {
791         struct osc_quota_info *oqi;
792         ENTRY;
793
794         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_IO, sizeof(*oqi));
795         if(!oqi)
796                 RETURN(NULL);
797
798         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
799         oqi->oqi_cli = cli;
800         oqi->oqi_id = id;
801         oqi->oqi_type = type;
802
803         RETURN(oqi);
804 }
805
806 static void free_qinfo(struct osc_quota_info *oqi)
807 {
808         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
809 }
810
811 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
812 {
813         unsigned int id;
814         int cnt, rc = QUOTA_OK;
815         ENTRY;
816
817         cfs_spin_lock(&qinfo_list_lock);
818         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
819                 struct osc_quota_info *oqi = NULL;
820
821                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
822                 oqi = find_qinfo(cli, id, cnt);
823                 if (oqi) {
824                         rc = NO_QUOTA;
825                         break;
826                 }
827         }
828         cfs_spin_unlock(&qinfo_list_lock);
829
830         if (rc == NO_QUOTA)
831                 CDEBUG(D_QUOTA, "chkdq found noquota for %s %d\n",
832                        cnt == USRQUOTA ? "user" : "group", id);
833         RETURN(rc);
834 }
835
836 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
837                     obd_flag valid, obd_flag flags)
838 {
839         unsigned int id;
840         obd_flag noquota;
841         int cnt, rc = 0;
842         ENTRY;
843
844
845         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
846                 struct osc_quota_info *oqi = NULL, *old;
847
848                 if (!(valid & ((cnt == USRQUOTA) ?
849                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
850                         continue;
851
852                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
853                 noquota = (cnt == USRQUOTA) ?
854                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
855
856                 if (noquota) {
857                         oqi = alloc_qinfo(cli, id, cnt);
858                         if (!oqi) {
859                                 rc = -ENOMEM;
860                                 CDEBUG(D_QUOTA, "setdq for %s %d failed, "
861                                        "(rc = %d)\n",
862                                        cnt == USRQUOTA ? "user" : "group",
863                                        id, rc);
864                                 break;
865                         }
866                 }
867
868                 cfs_spin_lock(&qinfo_list_lock);
869                 old = find_qinfo(cli, id, cnt);
870                 if (old && !noquota)
871                         remove_qinfo_hash(old);
872                 else if (!old && noquota)
873                         insert_qinfo_hash(oqi);
874                 cfs_spin_unlock(&qinfo_list_lock);
875
876                 if (old && !noquota)
877                         CDEBUG(D_QUOTA, "setdq to remove for %s %d\n",
878                                cnt == USRQUOTA ? "user" : "group", id);
879                 else if (!old && noquota)
880                         CDEBUG(D_QUOTA, "setdq to insert for %s %d\n",
881                                cnt == USRQUOTA ? "user" : "group", id);
882
883                 if (old) {
884                         if (noquota)
885                                 free_qinfo(oqi);
886                         else
887                                 free_qinfo(old);
888                 }
889         }
890
891         RETURN(rc);
892 }
893
894 int osc_quota_cleanup(struct obd_device *obd)
895 {
896         struct client_obd *cli = &obd->u.cli;
897         struct osc_quota_info *oqi, *n;
898         int i;
899         ENTRY;
900
901         cfs_spin_lock(&qinfo_list_lock);
902         for (i = 0; i < NR_DQHASH; i++) {
903                 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
904                         if (oqi->oqi_cli != cli)
905                                 continue;
906                         remove_qinfo_hash(oqi);
907                         free_qinfo(oqi);
908                 }
909         }
910         cfs_spin_unlock(&qinfo_list_lock);
911
912         RETURN(0);
913 }
914
915 int osc_quota_init(void)
916 {
917         int i;
918         ENTRY;
919
920         LASSERT(qinfo_cachep == NULL);
921         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
922                                             sizeof(struct osc_quota_info),
923                                             0, 0);
924         if (!qinfo_cachep)
925                 RETURN(-ENOMEM);
926
927         for (i = 0; i < NR_DQHASH; i++)
928                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
929
930         RETURN(0);
931 }
932
933 int osc_quota_exit(void)
934 {
935         struct osc_quota_info *oqi, *n;
936         int i, rc;
937         ENTRY;
938
939         cfs_spin_lock(&qinfo_list_lock);
940         for (i = 0; i < NR_DQHASH; i++) {
941                 cfs_list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
942                         remove_qinfo_hash(oqi);
943                         free_qinfo(oqi);
944                 }
945         }
946         cfs_spin_unlock(&qinfo_list_lock);
947
948         rc = cfs_mem_cache_destroy(qinfo_cachep);
949         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
950         qinfo_cachep = NULL;
951
952         RETURN(0);
953 }
954
955 #ifdef __KERNEL__
956 #ifdef HAVE_QUOTA_SUPPORT
957 quota_interface_t mds_quota_interface = {
958         .quota_init     = mds_quota_init,
959         .quota_exit     = mds_quota_exit,
960         .quota_setup    = mds_quota_setup,
961         .quota_cleanup  = mds_quota_cleanup,
962         .quota_check    = target_quota_check,
963         .quota_ctl      = mds_quota_ctl,
964         .quota_setinfo  = mds_quota_setinfo,
965         .quota_fs_cleanup = mds_quota_fs_cleanup,
966         .quota_recovery = mds_quota_recovery,
967         .quota_adjust   = mds_quota_adjust,
968         .quota_chkquota = quota_chk_acq_common,
969         .quota_acquire  = quota_acquire_common,
970         .quota_pending_commit = quota_pending_commit,
971 };
972
973 quota_interface_t filter_quota_interface = {
974         .quota_setup    = filter_quota_setup,
975         .quota_cleanup  = filter_quota_cleanup,
976         .quota_check    = target_quota_check,
977         .quota_ctl      = filter_quota_ctl,
978         .quota_setinfo  = filter_quota_setinfo,
979         .quota_clearinfo = filter_quota_clearinfo,
980         .quota_enforce  = filter_quota_enforce,
981         .quota_getflag  = filter_quota_getflag,
982         .quota_acquire  = quota_acquire_common,
983         .quota_adjust   = filter_quota_adjust,
984         .quota_chkquota = quota_chk_acq_common,
985         .quota_adjust_qunit   = filter_quota_adjust_qunit,
986         .quota_pending_commit = quota_pending_commit,
987 };
988 #endif
989 #endif /* __KERNEL__ */
990
991 quota_interface_t mdc_quota_interface = {
992         .quota_ctl      = client_quota_ctl,
993         .quota_check    = client_quota_check,
994         .quota_poll_check = client_quota_poll_check,
995 };
996
997 quota_interface_t lmv_quota_interface = {
998         .quota_ctl      = lmv_quota_ctl,
999         .quota_check    = lmv_quota_check,
1000 };
1001
1002 quota_interface_t osc_quota_interface = {
1003         .quota_ctl      = client_quota_ctl,
1004         .quota_check    = client_quota_check,
1005         .quota_poll_check = client_quota_poll_check,
1006         .quota_init     = osc_quota_init,
1007         .quota_exit     = osc_quota_exit,
1008         .quota_chkdq    = osc_quota_chkdq,
1009         .quota_setdq    = osc_quota_setdq,
1010         .quota_cleanup  = osc_quota_cleanup,
1011         .quota_adjust_qunit = client_quota_adjust_qunit,
1012 };
1013
1014 quota_interface_t lov_quota_interface = {
1015         .quota_ctl      = lov_quota_ctl,
1016         .quota_check    = lov_quota_check,
1017         .quota_adjust_qunit = lov_quota_adjust_qunit,
1018 };
1019
1020 #ifdef __KERNEL__
1021
1022 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
1023
1024 static int __init init_lustre_quota(void)
1025 {
1026 #ifdef HAVE_QUOTA_SUPPORT
1027         int rc = 0;
1028
1029         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
1030                                                 proc_lustre_root,
1031                                                 NULL, NULL);
1032         if (IS_ERR(lquota_type_proc_dir)) {
1033                 CERROR("LProcFS failed in lquota-init\n");
1034                 rc = PTR_ERR(lquota_type_proc_dir);
1035                 return rc;
1036         }
1037
1038         rc = qunit_cache_init();
1039         if (rc)
1040                 return rc;
1041
1042         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
1043         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
1044 #endif
1045         PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
1046         PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
1047         PORTAL_SYMBOL_REGISTER(osc_quota_interface);
1048         PORTAL_SYMBOL_REGISTER(lov_quota_interface);
1049         return 0;
1050 }
1051
1052 static void /*__exit*/ exit_lustre_quota(void)
1053 {
1054         PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
1055         PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
1056         PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
1057         PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
1058 #ifdef HAVE_QUOTA_SUPPORT
1059         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
1060         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
1061
1062         qunit_cache_cleanup();
1063
1064         if (lquota_type_proc_dir)
1065                 lprocfs_remove(&lquota_type_proc_dir);
1066 #endif
1067 }
1068
1069 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1070 MODULE_DESCRIPTION("Lustre Quota");
1071 MODULE_LICENSE("GPL");
1072
1073 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
1074
1075 #ifdef HAVE_QUOTA_SUPPORT
1076 EXPORT_SYMBOL(mds_quota_interface);
1077 EXPORT_SYMBOL(filter_quota_interface);
1078 #endif
1079 EXPORT_SYMBOL(mdc_quota_interface);
1080 EXPORT_SYMBOL(lmv_quota_interface);
1081 EXPORT_SYMBOL(osc_quota_interface);
1082 EXPORT_SYMBOL(lov_quota_interface);
1083 #endif /* __KERNEL */