Whamcloud - gitweb
b=16098
[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 #ifndef EXPORT_SYMTAB
37 # define EXPORT_SYMTAB
38 #endif
39 #define DEBUG_SUBSYSTEM S_MDS
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/ext3_fs.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 __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;
72         ENTRY;
73
74         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_QC_CALLBACK,
75                                         LUSTRE_OBD_VERSION, OBD_QC_CALLBACK);
76         if (req == NULL)
77                 RETURN(-ENOMEM);
78
79         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
80         *body = *oqctl;
81
82         ptlrpc_request_set_replen(req);
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         ptlrpc_daemonize("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
115         atomic_inc(qta->qta_sem);
116
117         OBD_FREE_PTR(qta);
118         return rc;
119 }
120
121 int target_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
122 {
123         struct obd_device *obd = exp->exp_obd;
124         struct obd_device_target *obt = &obd->u.obt;
125         struct quotacheck_thread_args *qta;
126         int rc = 0;
127         ENTRY;
128
129         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
130                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
131                 GOTO(out, rc = -EBUSY);
132         }
133
134         OBD_ALLOC_PTR(qta);
135         if (!qta)
136                 GOTO(out, rc = -ENOMEM);
137
138         qta->qta_exp = exp;
139         qta->qta_oqctl = *oqctl;
140         qta->qta_sb = obt->obt_sb;
141         qta->qta_sem = &obt->obt_quotachecking;
142
143         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
144                 /* quota master */
145                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
146                 if (rc) {
147                         CERROR("init_admin_quotafiles failed: %d\n", rc);
148                         OBD_FREE_PTR(qta);
149                         GOTO(out, rc);
150                 }
151         }
152
153         rc = kernel_thread(target_quotacheck_thread, qta, CLONE_VM|CLONE_FILES);
154         if (rc >= 0) {
155                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
156                        obd->obd_name, rc);
157                 RETURN(0);
158         }
159
160         CERROR("%s: error starting quotacheck_thread: %d\n",
161                obd->obd_name, rc);
162         OBD_FREE_PTR(qta);
163 out:
164         atomic_inc(&obt->obt_quotachecking);
165         RETURN(rc);
166 }
167
168 #endif /* __KERNEL__ */
169
170 int client_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
171 {
172         struct client_obd     *cli = &exp->exp_obd->u.cli;
173         struct ptlrpc_request *req;
174         struct obd_quotactl   *body;
175         int                    ver, opc, rc;
176         ENTRY;
177
178         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
179                 ver = LUSTRE_MDS_VERSION;
180                 opc = MDS_QUOTACHECK;
181         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
182                 ver = LUSTRE_OST_VERSION;
183                 opc = OST_QUOTACHECK;
184         } else {
185                 RETURN(-EINVAL);
186         }
187
188         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
189                                         &RQF_MDS_QUOTACHECK, ver, opc);
190         if (req == NULL)
191                 RETURN(-ENOMEM);
192
193         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
194         *body = *oqctl;
195
196         ptlrpc_request_set_replen(req);
197
198         /* the next poll will find -ENODATA, that means quotacheck is
199          * going on */
200         cli->cl_qchk_stat = -ENODATA;
201         rc = ptlrpc_queue_wait(req);
202         if (rc)
203                 cli->cl_qchk_stat = rc;
204         ptlrpc_req_finished(req);
205         RETURN(rc);
206 }
207
208 int client_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
209 {
210         struct client_obd *cli = &exp->exp_obd->u.cli;
211         int rc;
212         ENTRY;
213
214         rc = cli->cl_qchk_stat;
215
216         /* the client is not the previous one */
217         if (rc == CL_NOT_QUOTACHECKED)
218                 rc = -EINTR;
219
220         qchk->obd_uuid = cli->cl_target_uuid;
221         /* FIXME change strncmp to strcmp and save the strlen op */
222         if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME,
223             strlen(LUSTRE_OSC_NAME)))
224                 memcpy(qchk->obd_type, LUSTRE_OST_NAME,
225                        strlen(LUSTRE_OST_NAME));
226         else if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME,
227                  strlen(LUSTRE_MDC_NAME)))
228                 memcpy(qchk->obd_type, LUSTRE_MDS_NAME,
229                        strlen(LUSTRE_MDS_NAME));
230
231         RETURN(rc);
232 }
233
234 int lov_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
235 {
236         struct obd_device *obd = class_exp2obd(exp);
237         struct lov_obd *lov = &obd->u.lov;
238         int i, rc = 0;
239         ENTRY;
240
241         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
242                 int err;
243
244                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
245                         CERROR("lov idx %d inactive\n", i);
246                         RETURN(-EIO);
247                 }
248
249                 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
250                 if (err && lov->lov_tgts[i]->ltd_active && !rc)
251                         rc = err;
252         }
253
254         RETURN(rc);
255 }