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