Whamcloud - gitweb
Branch HEAD
[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  2008 Sun Microsystems, Inc. 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 spinlock_t last_print_lock = 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         init_rwsem(&obt->obt_rwsem);
80         obt->obt_qfmt = LUSTRE_QUOTA_V2;
81         atomic_set(&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         spin_lock(&qctxt->lqc_lock);
107         if (qctxt->lqc_import != NULL) {
108                 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                         CDEBUG(D_ERROR, "%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                 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         spin_lock(&qctxt->lqc_lock);
150         if (qctxt->lqc_import == imp) {
151                 qctxt->lqc_import = NULL;
152                 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                 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 (!sb_any_quota_enabled(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 (!sb_any_quota_enabled(obt->obt_sb))
190                 RETURN(0);
191
192         OBD_ALLOC_PTR(oqctl);
193         if (!oqctl) {
194                 CERROR("Not enough memory!");
195                 RETURN(-ENOMEM);
196         }
197
198         /* set over quota flags for a uid/gid */
199         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
200         oa->o_flags &= ~(OBD_FL_NO_USRQUOTA | OBD_FL_NO_GRPQUOTA);
201
202         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
203                 struct lustre_qunit_size *lqs = NULL;
204
205                 lqs = quota_search_lqs(LQS_KEY(cnt, GET_OA_ID(cnt, oa)),
206                                        qctxt, 0);
207                 if (lqs == NULL || IS_ERR(lqs)) {
208                         rc = PTR_ERR(lqs);
209                         break;
210                 } else {
211                         spin_lock(&lqs->lqs_lock);
212                         if (lqs->lqs_bunit_sz <= qctxt->lqc_sync_blk) {
213                                 oa->o_flags |= (cnt == USRQUOTA) ?
214                                         OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
215                                 spin_unlock(&lqs->lqs_lock);
216                                 CDEBUG(D_QUOTA, "set sync flag: bunit(%lu), "
217                                        "sync_blk(%d)\n", lqs->lqs_bunit_sz,
218                                        qctxt->lqc_sync_blk);
219                                 /* this is for quota_search_lqs */
220                                 lqs_putref(lqs);
221                                 continue;
222                         }
223                         spin_unlock(&lqs->lqs_lock);
224                         /* this is for quota_search_lqs */
225                         lqs_putref(lqs);
226                 }
227
228                 memset(oqctl, 0, sizeof(*oqctl));
229
230                 oqctl->qc_cmd = Q_GETQUOTA;
231                 oqctl->qc_type = cnt;
232                 oqctl->qc_id = (cnt == USRQUOTA) ? oa->o_uid : oa->o_gid;
233                 err = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
234                 if (err) {
235                         if (!rc)
236                                 rc = err;
237                         oa->o_valid &= ~((cnt == USRQUOTA) ? OBD_MD_FLUSRQUOTA :
238                                                              OBD_MD_FLGRPQUOTA);
239                         continue;
240                 }
241
242                 if (oqctl->qc_dqblk.dqb_bhardlimit &&
243                    (toqb(oqctl->qc_dqblk.dqb_curspace) >=
244                     oqctl->qc_dqblk.dqb_bhardlimit))
245                         oa->o_flags |= (cnt == USRQUOTA) ?
246                                 OBD_FL_NO_USRQUOTA : OBD_FL_NO_GRPQUOTA;
247         }
248         OBD_FREE_PTR(oqctl);
249         RETURN(rc);
250 }
251
252 /**
253  * check whether the left quota of certain uid and gid can satisfy a block_write
254  * or inode_create rpc. When need to acquire quota, return QUOTA_RET_ACQUOTA
255  */
256 static int quota_check_common(struct obd_device *obd, const unsigned int id[],
257                               int pending[], int count, int cycle, int isblk,
258                               struct inode *inode, int frags)
259 {
260         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
261         int i;
262         struct qunit_data qdata[MAXQUOTAS];
263         int mb = 0;
264         int rc = 0, rc2[2] = { 0, 0 };
265         ENTRY;
266
267         CLASSERT(MAXQUOTAS < 4);
268         if (!sb_any_quota_enabled(qctxt->lqc_sb))
269                 RETURN(rc);
270
271         spin_lock(&qctxt->lqc_lock);
272         if (!qctxt->lqc_valid){
273                 spin_unlock(&qctxt->lqc_lock);
274                 RETURN(rc);
275         }
276         spin_unlock(&qctxt->lqc_lock);
277
278         for (i = 0; i < MAXQUOTAS; i++) {
279                 struct lustre_qunit_size *lqs = NULL;
280
281                 qdata[i].qd_id = id[i];
282                 qdata[i].qd_flags = i;
283                 if (isblk)
284                         QDATA_SET_BLK(&qdata[i]);
285                 qdata[i].qd_count = 0;
286
287                 /* ignore root user */
288                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
289                         continue;
290
291                 lqs = quota_search_lqs(LQS_KEY(i, id[i]), qctxt, 0);
292                 if (lqs == NULL || IS_ERR(lqs))
293                         continue;
294
295                 if (IS_ERR(lqs)) {
296                         CERROR("can not find lqs for check_common: "
297                                "[id %u] [%c] [isblk %d] [count %d] [rc %ld]\n",
298                                id[i], i % 2 ? 'g': 'u', isblk, count,
299                                PTR_ERR(lqs));
300                         RETURN(PTR_ERR(lqs));
301                 }
302
303                 rc2[i] = compute_remquota(obd, qctxt, &qdata[i], isblk);
304                 spin_lock(&lqs->lqs_lock);
305                 if (!cycle) {
306                         if (isblk) {
307                                 pending[i] = count * CFS_PAGE_SIZE;
308                                 /* in order to complete this write, we need extra
309                                  * meta blocks. This function can get it through
310                                  * data needed to be written b=16542 */
311                                 if (inode) {
312                                         mb = pending[i];
313                                         rc = fsfilt_get_mblk(obd, qctxt->lqc_sb,
314                                                              &mb, inode,frags);
315                                         if (rc)
316                                                 CDEBUG(D_ERROR,
317                                                        "can't get extra "
318                                                        "meta blocks.\n");
319                                         else
320                                                 pending[i] += mb;
321                                 }
322                                 lqs->lqs_bwrite_pending += pending[i];
323                         } else {
324                                 pending[i] = count;
325                                 lqs->lqs_iwrite_pending += pending[i];
326                         }
327                 }
328
329                 /* if xx_rec < 0, that means quota are releasing,
330                  * and it may return before we use quota. So if
331                  * we find this situation, we assuming it has
332                  * returned b=18491 */
333                 if (isblk && lqs->lqs_blk_rec < 0) {
334                         if (qdata[i].qd_count < -lqs->lqs_blk_rec)
335                                 qdata[i].qd_count = 0;
336                         else
337                                 qdata[i].qd_count += lqs->lqs_blk_rec;
338                 }
339                 if (!isblk && lqs->lqs_ino_rec < 0) {
340                         if (qdata[i].qd_count < -lqs->lqs_ino_rec)
341                                 qdata[i].qd_count = 0;
342                         else
343                                 qdata[i].qd_count += lqs->lqs_ino_rec;
344                 }
345
346                 CDEBUG(D_QUOTA, "[id %u] [%c] [isblk %d] [count %d]"
347                        " [lqs pending: %lu] [qd_count: "LPU64"] [metablocks: %d]"
348                        " [pending: %d]\n", id[i], i % 2 ? 'g': 'u', isblk, count,
349                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
350                        qdata[i].qd_count, mb, pending[i]);
351                 if (rc2[i] == QUOTA_RET_OK) {
352                         if (isblk && qdata[i].qd_count < lqs->lqs_bwrite_pending)
353                                 rc2[i] = QUOTA_RET_ACQUOTA;
354                         if (!isblk && qdata[i].qd_count <
355                             lqs->lqs_iwrite_pending)
356                                 rc2[i] = QUOTA_RET_ACQUOTA;
357                 }
358
359                 spin_unlock(&lqs->lqs_lock);
360
361                 if (lqs->lqs_blk_rec  < 0 &&
362                     qdata[i].qd_count <
363                     lqs->lqs_bwrite_pending - lqs->lqs_blk_rec - mb)
364                         OBD_FAIL_TIMEOUT(OBD_FAIL_QUOTA_DELAY_REL, 5);
365
366                 /* When cycle is zero, lqs_*_pending will be changed. We will
367                  * get reference of the lqs here and put reference of lqs in
368                  * quota_pending_commit b=14784 */
369                 if (!cycle)
370                         lqs_getref(lqs);
371
372                 /* this is for quota_search_lqs */
373                 lqs_putref(lqs);
374         }
375
376         if (rc2[0] == QUOTA_RET_ACQUOTA || rc2[1] == QUOTA_RET_ACQUOTA)
377                 RETURN(QUOTA_RET_ACQUOTA);
378         else
379                 RETURN(rc);
380 }
381
382 static int quota_chk_acq_common(struct obd_device *obd, const unsigned int id[],
383                                 int pending[], int count, quota_acquire acquire,
384                                 struct obd_trans_info *oti, int isblk,
385                                 struct inode *inode, int frags)
386 {
387         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
388         struct timeval work_start;
389         struct timeval work_end;
390         long timediff;
391         struct l_wait_info lwi = { 0 };
392         int rc = 0, cycle = 0, count_err = 1;
393         ENTRY;
394
395         CDEBUG(D_QUOTA, "check quota for %s\n", obd->obd_name);
396         pending[USRQUOTA] = pending[GRPQUOTA] = 0;
397         /* Unfortunately, if quota master is too busy to handle the
398          * pre-dqacq in time and quota hash on ost is used up, we
399          * have to wait for the completion of in flight dqacq/dqrel,
400          * in order to get enough quota for write b=12588 */
401         do_gettimeofday(&work_start);
402         while ((rc = quota_check_common(obd, id, pending, count, cycle, isblk,
403                                         inode, frags)) &
404                QUOTA_RET_ACQUOTA) {
405
406                 spin_lock(&qctxt->lqc_lock);
407                 if (!qctxt->lqc_import && oti) {
408                         spin_unlock(&qctxt->lqc_lock);
409
410                         LASSERT(oti && oti->oti_thread &&
411                                 oti->oti_thread->t_watchdog);
412
413                         lc_watchdog_disable(oti->oti_thread->t_watchdog);
414                         CDEBUG(D_QUOTA, "sleep for quota master\n");
415                         l_wait_event(qctxt->lqc_wait_for_qmaster, check_qm(qctxt),
416                                      &lwi);
417                         CDEBUG(D_QUOTA, "wake up when quota master is back\n");
418                         lc_watchdog_touch(oti->oti_thread->t_watchdog,
419                                  GET_TIMEOUT(oti->oti_thread->t_svc));
420                 } else {
421                         spin_unlock(&qctxt->lqc_lock);
422                 }
423
424                 cycle++;
425                 if (isblk)
426                         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
427                 /* after acquire(), we should run quota_check_common again
428                  * so that we confirm there are enough quota to finish write */
429                 rc = acquire(obd, id, oti, isblk);
430
431                 /* please reference to dqacq_completion for the below */
432                 /* a new request is finished, try again */
433                 if (rc == QUOTA_REQ_RETURNED) {
434                         CDEBUG(D_QUOTA, "finish a quota req, try again\n");
435                         continue;
436                 }
437
438                 /* it is out of quota already */
439                 if (rc == -EDQUOT) {
440                         CDEBUG(D_QUOTA, "out of quota,  return -EDQUOT\n");
441                         break;
442                 }
443
444                 /* -EBUSY and others, wait a second and try again */
445                 if (rc < 0) {
446                         cfs_waitq_t        waitq;
447                         struct l_wait_info lwi;
448
449                         if (oti && oti->oti_thread && oti->oti_thread->t_watchdog)
450                                 lc_watchdog_touch(oti->oti_thread->t_watchdog,
451                                          GET_TIMEOUT(oti->oti_thread->t_svc));
452                         CDEBUG(D_QUOTA, "rc: %d, count_err: %d\n", rc,
453                                count_err++);
454
455                         init_waitqueue_head(&waitq);
456                         lwi = LWI_TIMEOUT(cfs_time_seconds(min(cycle, 10)), NULL,
457                                           NULL);
458                         l_wait_event(waitq, 0, &lwi);
459                 }
460
461                 if (rc < 0 || cycle % 10 == 2) {
462                         spin_lock(&last_print_lock);
463                         if (last_print == 0 ||
464                             cfs_time_before((last_print + cfs_time_seconds(30)),
465                                             cfs_time_current())) {
466                                 last_print = cfs_time_current();
467                                 spin_unlock(&last_print_lock);
468                                 CWARN("still haven't managed to acquire quota "
469                                       "space from the quota master after %d "
470                                       "retries (err=%d, rc=%d)\n",
471                                       cycle, count_err - 1, rc);
472                         } else {
473                                 spin_unlock(&last_print_lock);
474                         }
475                 }
476
477                 CDEBUG(D_QUOTA, "recheck quota with rc: %d, cycle: %d\n", rc,
478                        cycle);
479         }
480         do_gettimeofday(&work_end);
481         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
482         lprocfs_counter_add(qctxt->lqc_stats,
483                             isblk ? LQUOTA_WAIT_FOR_CHK_BLK :
484                                     LQUOTA_WAIT_FOR_CHK_INO,
485                             timediff);
486
487         RETURN(rc);
488 }
489
490 /**
491  * when a block_write or inode_create rpc is finished, adjust the record for
492  * pending blocks and inodes
493  */
494 static int quota_pending_commit(struct obd_device *obd, const unsigned int id[],
495                                 int pending[], int isblk)
496 {
497         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
498         struct timeval work_start;
499         struct timeval work_end;
500         long timediff;
501         int i;
502         struct qunit_data qdata[MAXQUOTAS];
503         ENTRY;
504
505         CDEBUG(D_QUOTA, "commit pending quota for  %s\n", obd->obd_name);
506         CLASSERT(MAXQUOTAS < 4);
507         if (!sb_any_quota_enabled(qctxt->lqc_sb))
508                 RETURN(0);
509
510         do_gettimeofday(&work_start);
511         for (i = 0; i < MAXQUOTAS; i++) {
512                 struct lustre_qunit_size *lqs = NULL;
513
514                 LASSERT(pending[i] >= 0);
515                 if (pending[i] == 0)
516                         continue;
517
518                 qdata[i].qd_id = id[i];
519                 qdata[i].qd_flags = i;
520                 if (isblk)
521                         QDATA_SET_BLK(&qdata[i]);
522                 qdata[i].qd_count = 0;
523
524                 if (qdata[i].qd_id == 0 && !QDATA_IS_GRP(&qdata[i]))
525                         continue;
526
527                 lqs = quota_search_lqs(LQS_KEY(i, qdata[i].qd_id), qctxt, 0);
528                 if (lqs == NULL || IS_ERR(lqs)) {
529                         CERROR("can not find lqs for pending_commit: "
530                                "[id %u] [%c] [pending %u] [isblk %d] (rc %ld), "
531                                "maybe cause unexpected lqs refcount error!\n",
532                                id[i], i ? 'g': 'u', pending[i], isblk,
533                                lqs ? PTR_ERR(lqs) : -1);
534                         continue;
535                 }
536
537                 spin_lock(&lqs->lqs_lock);
538                 if (isblk) {
539                         LASSERTF(lqs->lqs_bwrite_pending >= pending[i],
540                                  "there are too many blocks! [id %u] [%c] "
541                                  "[bwrite_pending %lu] [pending %u]\n",
542                                  id[i], i % 2 ? 'g' : 'u',
543                                  lqs->lqs_bwrite_pending, pending[i]);
544
545                         lqs->lqs_bwrite_pending -= pending[i];
546                 } else {
547                         LASSERTF(lqs->lqs_iwrite_pending >= pending[i],
548                                 "there are too many files! [id %u] [%c] "
549                                 "[iwrite_pending %lu] [pending %u]\n",
550                                 id[i], i % 2 ? 'g' : 'u',
551                                 lqs->lqs_iwrite_pending, pending[i]);
552
553                         lqs->lqs_iwrite_pending -= pending[i];
554                 }
555                 CDEBUG(D_QUOTA, "%s: lqs_pending=%lu pending[%d]=%d isblk=%d\n",
556                        obd->obd_name,
557                        isblk ? lqs->lqs_bwrite_pending : lqs->lqs_iwrite_pending,
558                        i, pending[i], isblk);
559                 spin_unlock(&lqs->lqs_lock);
560
561                 /* for quota_search_lqs in pending_commit */
562                 lqs_putref(lqs);
563                 /* for quota_search_lqs in quota_check */
564                 lqs_putref(lqs);
565         }
566         do_gettimeofday(&work_end);
567         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
568         lprocfs_counter_add(qctxt->lqc_stats,
569                             isblk ? LQUOTA_WAIT_FOR_COMMIT_BLK :
570                                     LQUOTA_WAIT_FOR_COMMIT_INO,
571                             timediff);
572
573         RETURN(0);
574 }
575
576 static int mds_quota_init(void)
577 {
578         return lustre_dquot_init();
579 }
580
581 static int mds_quota_exit(void)
582 {
583         lustre_dquot_exit();
584         return 0;
585 }
586
587 static int mds_quota_setup(struct obd_device *obd)
588 {
589         struct obd_device_target *obt = &obd->u.obt;
590         struct mds_obd *mds = &obd->u.mds;
591         int rc;
592         ENTRY;
593
594         if (unlikely(mds->mds_quota)) {
595                 CWARN("try to reinitialize quota context!\n");
596                 RETURN(0);
597         }
598
599         init_rwsem(&obt->obt_rwsem);
600         obt->obt_qfmt = LUSTRE_QUOTA_V2;
601         mds->mds_quota_info.qi_version = LUSTRE_QUOTA_V2;
602         atomic_set(&obt->obt_quotachecking, 1);
603         /* initialize quota master and quota context */
604         sema_init(&mds->mds_qonoff_sem, 1);
605         rc = qctxt_init(obd, dqacq_handler);
606         if (rc) {
607                 CERROR("initialize quota context failed! (rc:%d)\n", rc);
608                 RETURN(rc);
609         }
610         mds->mds_quota = 1;
611         RETURN(rc);
612 }
613
614 static int mds_quota_cleanup(struct obd_device *obd)
615 {
616         ENTRY;
617         if (unlikely(!obd->u.mds.mds_quota))
618                 RETURN(0);
619
620         qctxt_cleanup(&obd->u.obt.obt_qctxt, 0);
621         RETURN(0);
622 }
623
624 static int mds_quota_setinfo(struct obd_device *obd, void *data)
625 {
626         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
627         ENTRY;
628
629         if (unlikely(!obd->u.mds.mds_quota))
630                 RETURN(0);
631
632         if (data != NULL)
633                 QUOTA_MASTER_READY(qctxt);
634         else
635                 QUOTA_MASTER_UNREADY(qctxt);
636         RETURN(0);
637 }
638
639 static int mds_quota_fs_cleanup(struct obd_device *obd)
640 {
641         struct mds_obd *mds = &obd->u.mds;
642         struct obd_quotactl oqctl;
643         ENTRY;
644
645         if (unlikely(!mds->mds_quota))
646                 RETURN(0);
647
648         mds->mds_quota = 0;
649         memset(&oqctl, 0, sizeof(oqctl));
650         oqctl.qc_type = UGQUOTA;
651
652         down(&mds->mds_qonoff_sem);
653         mds_admin_quota_off(obd, &oqctl);
654         up(&mds->mds_qonoff_sem);
655         RETURN(0);
656 }
657
658 static int quota_acquire_common(struct obd_device *obd, const unsigned int id[],
659                                 struct obd_trans_info *oti, int isblk)
660 {
661         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
662         int rc;
663         ENTRY;
664
665         rc = qctxt_adjust_qunit(obd, qctxt, id, isblk, 1, oti);
666         RETURN(rc);
667 }
668
669 #endif /* HAVE_QUOTA_SUPPORT */
670 #endif /* __KERNEL__ */
671
672 struct osc_quota_info {
673         struct list_head        oqi_hash;       /* hash list */
674         struct client_obd      *oqi_cli;        /* osc obd */
675         unsigned int            oqi_id;         /* uid/gid of a file */
676         short                   oqi_type;       /* quota type */
677 };
678
679 spinlock_t qinfo_list_lock = SPIN_LOCK_UNLOCKED;
680
681 static struct list_head qinfo_hash[NR_DQHASH];
682 /* SLAB cache for client quota context */
683 cfs_mem_cache_t *qinfo_cachep = NULL;
684
685 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
686                          __attribute__((__const__));
687
688 static inline int hashfn(struct client_obd *cli, unsigned long id, int type)
689 {
690         unsigned long tmp = ((unsigned long)cli>>6) ^ id;
691         tmp = (tmp * (MAXQUOTAS - type)) % NR_DQHASH;
692         return tmp;
693 }
694
695 /* caller must hold qinfo_list_lock */
696 static inline void insert_qinfo_hash(struct osc_quota_info *oqi)
697 {
698         struct list_head *head = qinfo_hash +
699                 hashfn(oqi->oqi_cli, oqi->oqi_id, oqi->oqi_type);
700
701         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
702         list_add(&oqi->oqi_hash, head);
703 }
704
705 /* caller must hold qinfo_list_lock */
706 static inline void remove_qinfo_hash(struct osc_quota_info *oqi)
707 {
708         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
709         list_del_init(&oqi->oqi_hash);
710 }
711
712 /* caller must hold qinfo_list_lock */
713 static inline struct osc_quota_info *find_qinfo(struct client_obd *cli,
714                                                 unsigned int id, int type)
715 {
716         unsigned int hashent = hashfn(cli, id, type);
717         struct osc_quota_info *oqi;
718         ENTRY;
719
720         LASSERT_SPIN_LOCKED(&qinfo_list_lock);
721         list_for_each_entry(oqi, &qinfo_hash[hashent], oqi_hash) {
722                 if (oqi->oqi_cli == cli &&
723                     oqi->oqi_id == id && oqi->oqi_type == type)
724                         return oqi;
725         }
726         RETURN(NULL);
727 }
728
729 static struct osc_quota_info *alloc_qinfo(struct client_obd *cli,
730                                           unsigned int id, int type)
731 {
732         struct osc_quota_info *oqi;
733         ENTRY;
734
735         OBD_SLAB_ALLOC(oqi, qinfo_cachep, CFS_ALLOC_STD, sizeof(*oqi));
736         if(!oqi)
737                 RETURN(NULL);
738
739         CFS_INIT_LIST_HEAD(&oqi->oqi_hash);
740         oqi->oqi_cli = cli;
741         oqi->oqi_id = id;
742         oqi->oqi_type = type;
743
744         RETURN(oqi);
745 }
746
747 static void free_qinfo(struct osc_quota_info *oqi)
748 {
749         OBD_SLAB_FREE(oqi, qinfo_cachep, sizeof(*oqi));
750 }
751
752 int osc_quota_chkdq(struct client_obd *cli, const unsigned int qid[])
753 {
754         unsigned int id;
755         int cnt, rc = QUOTA_OK;
756         ENTRY;
757
758         spin_lock(&qinfo_list_lock);
759         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
760                 struct osc_quota_info *oqi = NULL;
761
762                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
763                 oqi = find_qinfo(cli, id, cnt);
764                 if (oqi) {
765                         rc = NO_QUOTA;
766                         break;
767                 }
768         }
769         spin_unlock(&qinfo_list_lock);
770
771         RETURN(rc);
772 }
773
774 int osc_quota_setdq(struct client_obd *cli, const unsigned int qid[],
775                     obd_flag valid, obd_flag flags)
776 {
777         unsigned int id;
778         obd_flag noquota;
779         int cnt, rc = 0;
780         ENTRY;
781
782
783         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
784                 struct osc_quota_info *oqi, *old;
785
786                 if (!(valid & ((cnt == USRQUOTA) ?
787                     OBD_MD_FLUSRQUOTA : OBD_MD_FLGRPQUOTA)))
788                         continue;
789
790                 id = (cnt == USRQUOTA) ? qid[USRQUOTA] : qid[GRPQUOTA];
791                 noquota = (cnt == USRQUOTA) ?
792                     (flags & OBD_FL_NO_USRQUOTA) : (flags & OBD_FL_NO_GRPQUOTA);
793
794                 oqi = alloc_qinfo(cli, id, cnt);
795                 if (oqi) {
796                         spin_lock(&qinfo_list_lock);
797
798                         old = find_qinfo(cli, id, cnt);
799                         if (old && !noquota)
800                                 remove_qinfo_hash(old);
801                         else if (!old && noquota)
802                                 insert_qinfo_hash(oqi);
803
804                         spin_unlock(&qinfo_list_lock);
805
806                         if (old || !noquota)
807                                 free_qinfo(oqi);
808                         if (old && !noquota)
809                                 free_qinfo(old);
810                 } else {
811                         CERROR("not enough mem!\n");
812                         rc = -ENOMEM;
813                         break;
814                 }
815         }
816
817         RETURN(rc);
818 }
819
820 int osc_quota_cleanup(struct obd_device *obd)
821 {
822         struct client_obd *cli = &obd->u.cli;
823         struct osc_quota_info *oqi, *n;
824         int i;
825         ENTRY;
826
827         spin_lock(&qinfo_list_lock);
828         for (i = 0; i < NR_DQHASH; i++) {
829                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
830                         if (oqi->oqi_cli != cli)
831                                 continue;
832                         remove_qinfo_hash(oqi);
833                         free_qinfo(oqi);
834                 }
835         }
836         spin_unlock(&qinfo_list_lock);
837
838         RETURN(0);
839 }
840
841 int osc_quota_init(void)
842 {
843         int i;
844         ENTRY;
845
846         LASSERT(qinfo_cachep == NULL);
847         qinfo_cachep = cfs_mem_cache_create("osc_quota_info",
848                                             sizeof(struct osc_quota_info),
849                                             0, 0);
850         if (!qinfo_cachep)
851                 RETURN(-ENOMEM);
852
853         for (i = 0; i < NR_DQHASH; i++)
854                 CFS_INIT_LIST_HEAD(qinfo_hash + i);
855
856         RETURN(0);
857 }
858
859 int osc_quota_exit(void)
860 {
861         struct osc_quota_info *oqi, *n;
862         int i, rc;
863         ENTRY;
864
865         spin_lock(&qinfo_list_lock);
866         for (i = 0; i < NR_DQHASH; i++) {
867                 list_for_each_entry_safe(oqi, n, &qinfo_hash[i], oqi_hash) {
868                         remove_qinfo_hash(oqi);
869                         free_qinfo(oqi);
870                 }
871         }
872         spin_unlock(&qinfo_list_lock);
873
874         rc = cfs_mem_cache_destroy(qinfo_cachep);
875         LASSERTF(rc == 0, "couldn't destory qinfo_cachep slab\n");
876         qinfo_cachep = NULL;
877
878         RETURN(0);
879 }
880
881 #ifdef __KERNEL__
882 #ifdef HAVE_QUOTA_SUPPORT
883 quota_interface_t mds_quota_interface = {
884         .quota_init     = mds_quota_init,
885         .quota_exit     = mds_quota_exit,
886         .quota_setup    = mds_quota_setup,
887         .quota_cleanup  = mds_quota_cleanup,
888         .quota_check    = target_quota_check,
889         .quota_ctl      = mds_quota_ctl,
890         .quota_setinfo  = mds_quota_setinfo,
891         .quota_fs_cleanup = mds_quota_fs_cleanup,
892         .quota_recovery = mds_quota_recovery,
893         .quota_adjust   = mds_quota_adjust,
894         .quota_chkquota = quota_chk_acq_common,
895         .quota_acquire  = quota_acquire_common,
896         .quota_pending_commit = quota_pending_commit,
897 };
898
899 quota_interface_t filter_quota_interface = {
900         .quota_setup    = filter_quota_setup,
901         .quota_cleanup  = filter_quota_cleanup,
902         .quota_check    = target_quota_check,
903         .quota_ctl      = filter_quota_ctl,
904         .quota_setinfo  = filter_quota_setinfo,
905         .quota_clearinfo = filter_quota_clearinfo,
906         .quota_enforce  = filter_quota_enforce,
907         .quota_getflag  = filter_quota_getflag,
908         .quota_acquire  = quota_acquire_common,
909         .quota_adjust   = filter_quota_adjust,
910         .quota_chkquota = quota_chk_acq_common,
911         .quota_adjust_qunit   = filter_quota_adjust_qunit,
912         .quota_pending_commit = quota_pending_commit,
913 };
914 #endif
915 #endif /* __KERNEL__ */
916
917 quota_interface_t mdc_quota_interface = {
918         .quota_ctl      = client_quota_ctl,
919         .quota_check    = client_quota_check,
920         .quota_poll_check = client_quota_poll_check,
921 };
922
923 quota_interface_t lmv_quota_interface = {
924         .quota_ctl      = lmv_quota_ctl,
925         .quota_check    = lmv_quota_check,
926 };
927
928 quota_interface_t osc_quota_interface = {
929         .quota_ctl      = client_quota_ctl,
930         .quota_check    = client_quota_check,
931         .quota_poll_check = client_quota_poll_check,
932         .quota_init     = osc_quota_init,
933         .quota_exit     = osc_quota_exit,
934         .quota_chkdq    = osc_quota_chkdq,
935         .quota_setdq    = osc_quota_setdq,
936         .quota_cleanup  = osc_quota_cleanup,
937         .quota_adjust_qunit = client_quota_adjust_qunit,
938 };
939
940 quota_interface_t lov_quota_interface = {
941         .quota_ctl      = lov_quota_ctl,
942         .quota_check    = lov_quota_check,
943         .quota_adjust_qunit = lov_quota_adjust_qunit,
944 };
945
946 #ifdef __KERNEL__
947
948 cfs_proc_dir_entry_t *lquota_type_proc_dir = NULL;
949
950 static int __init init_lustre_quota(void)
951 {
952 #ifdef HAVE_QUOTA_SUPPORT
953         int rc = 0;
954
955         lquota_type_proc_dir = lprocfs_register(OBD_LQUOTA_DEVICENAME,
956                                                 proc_lustre_root,
957                                                 NULL, NULL);
958         if (IS_ERR(lquota_type_proc_dir)) {
959                 CERROR("LProcFS failed in lquota-init\n");
960                 rc = PTR_ERR(lquota_type_proc_dir);
961                 return rc;
962         }
963
964         rc = qunit_cache_init();
965         if (rc)
966                 return rc;
967
968         PORTAL_SYMBOL_REGISTER(filter_quota_interface);
969         PORTAL_SYMBOL_REGISTER(mds_quota_interface);
970 #endif
971         PORTAL_SYMBOL_REGISTER(mdc_quota_interface);
972         PORTAL_SYMBOL_REGISTER(lmv_quota_interface);
973         PORTAL_SYMBOL_REGISTER(osc_quota_interface);
974         PORTAL_SYMBOL_REGISTER(lov_quota_interface);
975         return 0;
976 }
977
978 static void /*__exit*/ exit_lustre_quota(void)
979 {
980         PORTAL_SYMBOL_UNREGISTER(mdc_quota_interface);
981         PORTAL_SYMBOL_UNREGISTER(lmv_quota_interface);
982         PORTAL_SYMBOL_UNREGISTER(osc_quota_interface);
983         PORTAL_SYMBOL_UNREGISTER(lov_quota_interface);
984 #ifdef HAVE_QUOTA_SUPPORT
985         PORTAL_SYMBOL_UNREGISTER(filter_quota_interface);
986         PORTAL_SYMBOL_UNREGISTER(mds_quota_interface);
987
988         qunit_cache_cleanup();
989
990         if (lquota_type_proc_dir)
991                 lprocfs_remove(&lquota_type_proc_dir);
992 #endif
993 }
994
995 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
996 MODULE_DESCRIPTION("Lustre Quota");
997 MODULE_LICENSE("GPL");
998
999 cfs_module(lquota, "1.0.0", init_lustre_quota, exit_lustre_quota);
1000
1001 #ifdef HAVE_QUOTA_SUPPORT
1002 EXPORT_SYMBOL(mds_quota_interface);
1003 EXPORT_SYMBOL(filter_quota_interface);
1004 #endif
1005 EXPORT_SYMBOL(mdc_quota_interface);
1006 EXPORT_SYMBOL(lmv_quota_interface);
1007 EXPORT_SYMBOL(osc_quota_interface);
1008 EXPORT_SYMBOL(lov_quota_interface);
1009 #endif /* __KERNEL */