Whamcloud - gitweb
LU-1214 quota: don't compile quota module for client
[fs/lustre-release.git] / lustre / quota / quota_check.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 __KERNEL__
68 static int target_quotacheck_callback(struct obd_export *exp,
69                                       struct obd_quotactl *oqctl)
70 {
71         struct ptlrpc_request *req;
72         struct obd_quotactl   *body;
73         int                    rc;
74         ENTRY;
75
76         req = ptlrpc_request_alloc_pack(exp->exp_imp_reverse, &RQF_QC_CALLBACK,
77                                         LUSTRE_OBD_VERSION, OBD_QC_CALLBACK);
78         if (req == NULL)
79                 RETURN(-ENOMEM);
80
81         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
82         *body = *oqctl;
83
84         ptlrpc_request_set_replen(req);
85
86         rc = ptlrpc_queue_wait(req);
87         ptlrpc_req_finished(req);
88
89         RETURN(rc);
90 }
91
92 static int target_quotacheck_thread(void *data)
93 {
94         struct quotacheck_thread_args *qta = data;
95         struct obd_export *exp;
96         struct obd_device *obd;
97         struct obd_quotactl *oqctl;
98         struct lvfs_run_ctxt saved;
99         int rc;
100
101         cfs_daemonize_ctxt("quotacheck");
102
103         exp = qta->qta_exp;
104         obd = qta->qta_obd;
105         oqctl = &qta->qta_oqctl;
106
107         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
108
109         rc = fsfilt_quotacheck(obd, qta->qta_sb, oqctl);
110         if (rc)
111                 CERROR("%s: fsfilt_quotacheck: %d\n", obd->obd_name, rc);
112
113         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
114
115         rc = target_quotacheck_callback(exp, oqctl);
116         class_export_put(exp);
117         cfs_up(qta->qta_sem);
118         OBD_FREE_PTR(qta);
119         return rc;
120 }
121
122 int target_quota_check(struct obd_device *obd, struct obd_export *exp,
123                        struct obd_quotactl *oqctl)
124 {
125         struct obd_device_target *obt = &obd->u.obt;
126         struct quotacheck_thread_args *qta;
127         int rc = 0;
128         ENTRY;
129
130         OBD_ALLOC_PTR(qta);
131         if (!qta)
132                 RETURN(ENOMEM);
133
134         cfs_down(&obt->obt_quotachecking);
135
136         qta->qta_exp = exp;
137         qta->qta_obd = obd;
138         qta->qta_oqctl = *oqctl;
139         qta->qta_oqctl.qc_id = obt->obt_qfmt; /* override qfmt version */
140         qta->qta_sb = obt->obt_sb;
141         qta->qta_sem = &obt->obt_quotachecking;
142
143         /* quotaoff firstly */
144         oqctl->qc_cmd = Q_QUOTAOFF;
145         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
146                 rc = do_mds_quota_off(obd, oqctl);
147                 if (rc && rc != -EALREADY) {
148                         CERROR("off quota on MDS failed: %d\n", rc);
149                         GOTO(out, rc);
150                 }
151
152                 /* quota master */
153                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
154                 if (rc) {
155                         CERROR("init_admin_quotafiles failed: %d\n", rc);
156                         GOTO(out, rc);
157                 }
158         } else {
159                 struct lvfs_run_ctxt saved;
160                 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
161
162                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
163                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
164                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
165                 if (!rc) {
166                         qctxt->lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
167                 } else if (!quota_is_off(qctxt, oqctl)) {
168                         CERROR("off quota on OSS failed: %d\n", rc);
169                         GOTO(out, rc);
170                 }
171         }
172
173         /* we get ref for exp because target_quotacheck_callback() will use this
174          * export later b=18126 */
175         class_export_get(exp);
176         rc = cfs_create_thread(target_quotacheck_thread, qta,
177                                CFS_DAEMON_FLAGS);
178         if (rc >= 0) {
179                 /* target_quotacheck_thread will drop the ref on exp and release
180                  * obt_quotachecking */
181                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
182                        obd->obd_name, rc);
183                 RETURN(0);
184         } else {
185                 CERROR("%s: error starting quotacheck_thread: %d\n",
186                        obd->obd_name, rc);
187                 class_export_put(exp);
188                 EXIT;
189         }
190
191 out:
192         cfs_up(&obt->obt_quotachecking);
193         OBD_FREE_PTR(qta);
194         return rc;
195 }
196
197 #endif /* __KERNEL__ */