Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / quota / quota_check.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
38 #ifndef EXPORT_SYMTAB
39 # define EXPORT_SYMTAB
40 #endif
41 #define DEBUG_SUBSYSTEM S_LQUOTA
42
43 #ifdef __KERNEL__
44 # include <linux/version.h>
45 # include <linux/module.h>
46 # include <linux/init.h>
47 # include <linux/fs.h>
48 # include <linux/jbd.h>
49 # include <linux/ext3_fs.h>
50 # include <linux/smp_lock.h>
51 # include <linux/buffer_head.h>
52 # include <linux/workqueue.h>
53 # include <linux/mount.h>
54 #else /* __KERNEL__ */
55 # include <liblustre.h>
56 #endif
57
58 #include <obd_class.h>
59 #include <lustre_mds.h>
60 #include <lustre_dlm.h>
61 #include <lustre_cfg.h>
62 #include <obd_ost.h>
63 #include <lustre_fsfilt.h>
64 #include <lustre_quota.h>
65 #include "quota_internal.h"
66
67 #ifdef HAVE_QUOTA_SUPPORT
68 #ifdef __KERNEL__
69 static int target_quotacheck_callback(struct obd_export *exp,
70                                       struct obd_quotactl *oqctl)
71 {
72         struct ptlrpc_request *req;
73         struct obd_quotactl   *body;
74         int                    rc;
75         ENTRY;
76
77         req = ptlrpc_request_alloc_pack(exp->exp_imp_reverse, &RQF_QC_CALLBACK,
78                                         LUSTRE_OBD_VERSION, OBD_QC_CALLBACK);
79         if (req == NULL)
80                 RETURN(-ENOMEM);
81
82         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
83         *body = *oqctl;
84
85         ptlrpc_request_set_replen(req);
86
87         rc = ptlrpc_queue_wait(req);
88         ptlrpc_req_finished(req);
89
90         RETURN(rc);
91 }
92
93 static int target_quotacheck_thread(void *data)
94 {
95         struct quotacheck_thread_args *qta = data;
96         struct obd_export *exp;
97         struct obd_device *obd;
98         struct obd_quotactl *oqctl;
99         struct lvfs_run_ctxt saved;
100         int rc;
101
102         ptlrpc_daemonize("quotacheck");
103
104         exp = qta->qta_exp;
105         obd = qta->qta_obd;
106         oqctl = &qta->qta_oqctl;
107
108         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
109
110         rc = fsfilt_quotacheck(obd, qta->qta_sb, oqctl);
111         if (rc)
112                 CERROR("%s: fsfilt_quotacheck: %d\n", obd->obd_name, rc);
113
114         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
115
116         rc = target_quotacheck_callback(exp, oqctl);
117         class_export_put(exp);
118
119         atomic_inc(qta->qta_sem);
120
121         OBD_FREE_PTR(qta);
122         return rc;
123 }
124
125 int target_quota_check(struct obd_device *obd, struct obd_export *exp,
126                        struct obd_quotactl *oqctl)
127 {
128         struct obd_device_target *obt = &obd->u.obt;
129         struct quotacheck_thread_args *qta;
130         int rc = 0;
131         ENTRY;
132
133         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
134                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
135                 GOTO(out, rc = -EBUSY);
136         }
137
138         OBD_ALLOC_PTR(qta);
139         if (!qta)
140                 GOTO(out, rc = -ENOMEM);
141
142         qta->qta_exp = exp;
143         qta->qta_obd = obd;
144         qta->qta_oqctl = *oqctl;
145         qta->qta_oqctl.qc_id = obt->obt_qfmt; /* override qfmt version */
146         qta->qta_sb = obt->obt_sb;
147         qta->qta_sem = &obt->obt_quotachecking;
148
149         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
150                 /* quota master */
151                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
152                 if (rc) {
153                         CERROR("init_admin_quotafiles failed: %d\n", rc);
154                         OBD_FREE_PTR(qta);
155                         GOTO(out, rc);
156                 }
157         }
158
159         /* we get ref for exp because target_quotacheck_callback() will use this
160          * export later b=18126 */
161         class_export_get(exp);
162         rc = kernel_thread(target_quotacheck_thread, qta, CLONE_VM|CLONE_FILES);
163         if (rc >= 0) {
164                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
165                        obd->obd_name, rc);
166                 RETURN(0);
167         }
168
169         class_export_put(exp);
170         CERROR("%s: error starting quotacheck_thread: %d\n",
171                obd->obd_name, rc);
172         OBD_FREE_PTR(qta);
173 out:
174         atomic_inc(&obt->obt_quotachecking);
175         RETURN(rc);
176 }
177
178 #endif /* __KERNEL__ */
179 #endif /* HAVE_QUOTA_SUPPORT */
180
181 int client_quota_check(struct obd_device *unused, struct obd_export *exp,
182                        struct obd_quotactl *oqctl)
183 {
184         struct client_obd       *cli = &exp->exp_obd->u.cli;
185         struct ptlrpc_request   *req;
186         struct obd_quotactl     *body;
187         const struct req_format *rf;
188         int                      ver, opc, rc;
189         ENTRY;
190
191         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
192                 rf  = &RQF_MDS_QUOTACHECK;
193                 ver = LUSTRE_MDS_VERSION;
194                 opc = MDS_QUOTACHECK;
195         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
196                 rf  = &RQF_OST_QUOTACHECK;
197                 ver = LUSTRE_OST_VERSION;
198                 opc = OST_QUOTACHECK;
199         } else {
200                 RETURN(-EINVAL);
201         }
202
203         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), rf, ver, opc);
204         if (req == NULL)
205                 RETURN(-ENOMEM);
206
207         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
208         *body = *oqctl;
209
210         ptlrpc_request_set_replen(req);
211
212         /* the next poll will find -ENODATA, that means quotacheck is
213          * going on */
214         cli->cl_qchk_stat = -ENODATA;
215         rc = ptlrpc_queue_wait(req);
216         if (rc)
217                 cli->cl_qchk_stat = rc;
218         ptlrpc_req_finished(req);
219         RETURN(rc);
220 }
221
222 int client_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
223 {
224         struct client_obd *cli = &exp->exp_obd->u.cli;
225         int rc;
226         ENTRY;
227
228         rc = cli->cl_qchk_stat;
229
230         /* the client is not the previous one */
231         if (rc == CL_NOT_QUOTACHECKED)
232                 rc = -EINTR;
233
234         qchk->obd_uuid = cli->cl_target_uuid;
235         /* FIXME change strncmp to strcmp and save the strlen op */
236         if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME,
237                     strlen(LUSTRE_OSC_NAME)) == 0)
238                 memcpy(qchk->obd_type, LUSTRE_OST_NAME,
239                        strlen(LUSTRE_OST_NAME));
240         else if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME,
241                          strlen(LUSTRE_MDC_NAME)) == 0)
242                 memcpy(qchk->obd_type, LUSTRE_MDS_NAME,
243                        strlen(LUSTRE_MDS_NAME));
244
245         RETURN(rc);
246 }
247
248 int lmv_quota_check(struct obd_device *unused, struct obd_export *exp,
249                     struct obd_quotactl *oqctl)
250 {
251         struct obd_device *obd = class_exp2obd(exp);
252         struct lmv_obd *lmv = &obd->u.lmv;
253         struct lmv_tgt_desc *tgt;
254         int i, rc = 0;
255         ENTRY;
256
257         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
258                 int err;
259
260                 if (!tgt->ltd_active) {
261                         CERROR("lmv idx %d inactive\n", i);
262                         RETURN(-EIO);
263                 }
264
265                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
266                 if (err && !rc)
267                         rc = err;
268         }
269
270         RETURN(rc);
271 }
272
273 int lov_quota_check(struct obd_device *unused, struct obd_export *exp,
274                     struct obd_quotactl *oqctl)
275 {
276         struct obd_device *obd = class_exp2obd(exp);
277         struct lov_obd *lov = &obd->u.lov;
278         int i, rc = 0;
279         ENTRY;
280
281         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
282                 int err;
283
284                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
285                         CERROR("lov idx %d inactive\n", i);
286                         RETURN(-EIO);
287                 }
288
289                 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
290                 if (err && !rc)
291                         rc = err;
292         }
293
294         RETURN(rc);
295 }