Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / quota / quota_ctl.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 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_LQUOTA
40
41 #ifdef __KERNEL__
42 # include <linux/version.h>
43 # include <linux/module.h>
44 # include <linux/init.h>
45 # include <linux/fs.h>
46 # include <linux/jbd.h>
47 # include <linux/quota.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 "quota_internal.h"
64
65 #ifdef HAVE_QUOTA_SUPPORT
66 #ifdef __KERNEL__
67 int mds_quota_ctl(struct obd_device *obd, struct obd_export *unused,
68                   struct obd_quotactl *oqctl)
69 {
70         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
71         struct timeval work_start;
72         struct timeval work_end;
73         long timediff;
74         int rc = 0;
75         ENTRY;
76
77         do_gettimeofday(&work_start);
78         switch (oqctl->qc_cmd) {
79         case Q_QUOTAON:
80                 rc = mds_quota_on(obd, oqctl);
81                 break;
82         case Q_QUOTAOFF:
83                 rc = mds_quota_off(obd, oqctl);
84                 break;
85         case Q_SETINFO:
86                 rc = mds_set_dqinfo(obd, oqctl);
87                 break;
88         case Q_GETINFO:
89                 rc = mds_get_dqinfo(obd, oqctl);
90                 break;
91         case Q_SETQUOTA:
92                 rc = mds_set_dqblk(obd, oqctl);
93                 break;
94         case Q_GETQUOTA:
95                 rc = mds_get_dqblk(obd, oqctl);
96                 break;
97         case Q_GETOINFO:
98         case Q_GETOQUOTA:
99                 rc = mds_get_obd_quota(obd, oqctl);
100                 break;
101         case LUSTRE_Q_INVALIDATE:
102                 rc = mds_quota_invalidate(obd, oqctl);
103                 break;
104         case LUSTRE_Q_FINVALIDATE:
105                 rc = mds_quota_finvalidate(obd, oqctl);
106                 break;
107         default:
108                 CERROR("%s: unsupported mds_quotactl command: %d\n",
109                        obd->obd_name, oqctl->qc_cmd);
110                 RETURN(-EFAULT);
111         }
112
113         if (rc)
114                 CDEBUG(D_INFO, "mds_quotactl admin quota command %d, id %u, "
115                                "type %d, failed: rc = %d\n",
116                        oqctl->qc_cmd, oqctl->qc_id, oqctl->qc_type, rc);
117         do_gettimeofday(&work_end);
118         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
119         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
120
121         RETURN(rc);
122 }
123
124 int filter_quota_ctl(struct obd_device *unused, struct obd_export *exp,
125                      struct obd_quotactl *oqctl)
126 {
127         struct obd_device *obd = exp->exp_obd;
128         struct obd_device_target *obt = &obd->u.obt;
129         struct lvfs_run_ctxt saved;
130         struct lustre_quota_ctxt *qctxt = &obd->u.obt.obt_qctxt;
131         struct timeval work_start;
132         struct timeval work_end;
133         long timediff;
134         int rc = 0;
135         ENTRY;
136
137         do_gettimeofday(&work_start);
138         switch (oqctl->qc_cmd) {
139         case Q_FINVALIDATE:
140         case Q_QUOTAON:
141         case Q_QUOTAOFF:
142                 if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
143                         CDEBUG(D_INFO, "other people are doing quotacheck\n");
144                         atomic_inc(&obt->obt_quotachecking);
145                         rc = -EBUSY;
146                         break;
147                 }
148                 if (oqctl->qc_cmd == Q_FINVALIDATE &&
149                     (obt->obt_qctxt.lqc_flags & UGQUOTA2LQC(oqctl->qc_type))) {
150                         rc = -EBUSY;
151                         break;
152                 }
153                 oqctl->qc_id = obt->obt_qfmt; /* override qfmt version */
154         case Q_GETOINFO:
155         case Q_GETOQUOTA:
156         case Q_GETQUOTA:
157                 /* In recovery scenario, this pending dqacq/dqrel might have
158                  * been processed by master successfully before it's dquot
159                  * on master enter recovery mode. We must wait for this 
160                  * dqacq/dqrel done then return the correct limits to master */
161                 if (oqctl->qc_stat == QUOTA_RECOVERING)
162                         qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
163                                                  oqctl->qc_id, oqctl->qc_type, 
164                                                  1);
165
166                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
167                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
168                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
169
170                 if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF ||
171                     oqctl->qc_cmd == Q_FINVALIDATE) {
172                         if (!rc && oqctl->qc_cmd == Q_QUOTAON)
173                                 obt->obt_qctxt.lqc_flags |= UGQUOTA2LQC(oqctl->qc_type);
174                         if (!rc && oqctl->qc_cmd == Q_QUOTAOFF)
175                                 obt->obt_qctxt.lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
176                         atomic_inc(&obt->obt_quotachecking);
177                 }
178                 break;
179         case Q_SETQUOTA:
180                 /* currently, it is only used for nullifying the quota */
181                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
182                                          oqctl->qc_id, oqctl->qc_type, 1);
183
184                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
185                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
186
187                 if (!rc) {
188                         oqctl->qc_cmd = Q_SYNC;
189                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
190                         oqctl->qc_cmd = Q_SETQUOTA;
191                 }
192                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
193                 break;
194         case Q_INITQUOTA:
195                 {
196                 unsigned int uid = 0, gid = 0;
197
198                 /* Initialize quota limit to MIN_QLIMIT */
199                 LASSERT(oqctl->qc_dqblk.dqb_valid == QIF_BLIMITS);
200                 LASSERT(oqctl->qc_dqblk.dqb_bsoftlimit == 0);
201
202                 /* There might be a pending dqacq/dqrel (which is going to
203                  * clear stale limits on slave). we should wait for it's
204                  * completion then initialize limits */
205                 qctxt_wait_pending_dqacq(&obd->u.obt.obt_qctxt,
206                                          oqctl->qc_id, oqctl->qc_type, 1);
207
208                 if (!oqctl->qc_dqblk.dqb_bhardlimit)
209                         goto adjust;
210
211                 LASSERT(oqctl->qc_dqblk.dqb_bhardlimit == MIN_QLIMIT);
212                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
213                 rc = fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
214
215                 /* Update on-disk quota, in case of lose the changed limits
216                  * (MIN_QLIMIT) on crash, which cannot be recovered.*/
217                 if (!rc) {
218                         oqctl->qc_cmd = Q_SYNC;
219                         fsfilt_quotactl(obd, obd->u.obt.obt_sb, oqctl);
220                         oqctl->qc_cmd = Q_INITQUOTA;
221                 }
222                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
223
224                 if (rc)
225                         RETURN(rc);
226 adjust:
227                 /* Trigger qunit pre-acquire */
228                 if (oqctl->qc_type == USRQUOTA)
229                         uid = oqctl->qc_id;
230                 else
231                         gid = oqctl->qc_id;
232
233                 rc = qctxt_adjust_qunit(obd, &obd->u.obt.obt_qctxt,
234                                         uid, gid, 1, 0, NULL);
235                 if (rc == -EDQUOT || rc == -EBUSY) {
236                         CDEBUG(D_QUOTA, "rc: %d.\n", rc);
237                         rc = 0;
238                 }
239
240                 break;
241                 }
242         default:
243                 CERROR("%s: unsupported filter_quotactl command: %d\n",
244                        obd->obd_name, oqctl->qc_cmd);
245                 RETURN(-EFAULT);
246         }
247         do_gettimeofday(&work_end);
248         timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
249         lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_QUOTA_CTL, timediff);
250
251         RETURN(rc);
252 }
253 #endif /* __KERNEL__ */
254 #endif
255
256 int client_quota_ctl(struct obd_device *unused, struct obd_export *exp,
257                      struct obd_quotactl *oqctl)
258 {
259         struct ptlrpc_request   *req;
260         struct obd_quotactl     *oqc;
261         const struct req_format *rf;
262         int                      ver, opc, rc;
263         ENTRY;
264
265         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
266                 rf  = &RQF_MDS_QUOTACTL;
267                 ver = LUSTRE_MDS_VERSION,
268                 opc = MDS_QUOTACTL;
269         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
270                 rf  = &RQF_OST_QUOTACTL;
271                 ver = LUSTRE_OST_VERSION,
272                 opc = OST_QUOTACTL;
273         } else {
274                 RETURN(-EINVAL);
275         }
276
277         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
278         if (req == NULL)
279                 RETURN(-ENOMEM);
280
281         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
282         *oqc = *oqctl;
283
284         ptlrpc_request_set_replen(req);
285
286         rc = ptlrpc_queue_wait(req);
287         if (rc) {
288                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
289                 GOTO(out, rc);
290         }
291
292         oqc = NULL;
293         if (req->rq_repmsg)
294                 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
295
296         if (oqc == NULL) {
297                 CERROR ("Can't unpack obd_quotactl\n");
298                 GOTO(out, rc = -EPROTO);
299         }
300
301         *oqctl = *oqc;
302         EXIT;
303 out:
304         ptlrpc_req_finished(req);
305         return rc;
306 }
307
308 /**
309  * For lmv, only need to send request to master MDT, and the master MDT will
310  * process with other slave MDTs.
311  */
312 int lmv_quota_ctl(struct obd_device *unused, struct obd_export *exp,
313                   struct obd_quotactl *oqctl)
314 {
315         struct obd_device *obd = class_exp2obd(exp);
316         struct lmv_obd *lmv = &obd->u.lmv;
317         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
318         int rc;
319         ENTRY;
320
321         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
322                 CERROR("master lmv inactive\n");
323                 RETURN(-EIO);
324         }
325
326         rc = obd_quotactl(tgt->ltd_exp, oqctl);
327         RETURN(rc);
328 }
329
330 int lov_quota_ctl(struct obd_device *unused, struct obd_export *exp,
331                   struct obd_quotactl *oqctl)
332 {
333         struct obd_device *obd = class_exp2obd(exp);
334         struct lov_obd *lov = &obd->u.lov;
335         __u64 curspace = 0;
336         __u64 bhardlimit = 0;
337         int i, rc = 0;
338         ENTRY;
339
340         if (oqctl->qc_cmd != LUSTRE_Q_QUOTAON &&
341             oqctl->qc_cmd != LUSTRE_Q_QUOTAOFF &&
342             oqctl->qc_cmd != Q_GETOQUOTA &&
343             oqctl->qc_cmd != Q_INITQUOTA &&
344             oqctl->qc_cmd != LUSTRE_Q_SETQUOTA &&
345             oqctl->qc_cmd != Q_FINVALIDATE) {
346                 CERROR("bad quota opc %x for lov obd", oqctl->qc_cmd);
347                 RETURN(-EFAULT);
348         }
349
350         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
351                 int err;
352
353                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
354                         if (oqctl->qc_cmd == Q_GETOQUOTA) {
355                                 CERROR("ost %d is inactive\n", i);
356                                 rc = -EIO;
357                         } else {
358                                 CDEBUG(D_HA, "ost %d is inactive\n", i);
359                         }
360                         continue;
361                 }
362
363                 err = obd_quotactl(lov->lov_tgts[i]->ltd_exp, oqctl);
364                 if (err) {
365                         if (lov->lov_tgts[i]->ltd_active && !rc)
366                                 rc = err;
367                         continue;
368                 }
369
370                 if (oqctl->qc_cmd == Q_GETOQUOTA) {
371                         curspace += oqctl->qc_dqblk.dqb_curspace;
372                         bhardlimit += oqctl->qc_dqblk.dqb_bhardlimit;
373                 }
374         }
375
376         if (oqctl->qc_cmd == Q_GETOQUOTA) {
377                 oqctl->qc_dqblk.dqb_curspace = curspace;
378                 oqctl->qc_dqblk.dqb_bhardlimit = bhardlimit;
379         }
380         RETURN(rc);
381 }