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