Whamcloud - gitweb
LU-812 kernel: remove smp_lock.h
[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 #define DEBUG_SUBSYSTEM S_LQUOTA
38
39 #ifdef __KERNEL__
40 # include <linux/version.h>
41 # include <linux/module.h>
42 # include <linux/init.h>
43 # include <linux/fs.h>
44 # include <linux/jbd.h>
45 # include <linux/ext3_fs.h>
46 # include <linux/buffer_head.h>
47 # include <linux/workqueue.h>
48 # include <linux/mount.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 #endif
52
53 #include <obd_class.h>
54 #include <lustre_mds.h>
55 #include <lustre_dlm.h>
56 #include <lustre_cfg.h>
57 #include <obd_ost.h>
58 #include <lustre_fsfilt.h>
59 #include <lustre_quota.h>
60 #include "quota_internal.h"
61
62 #ifdef __KERNEL__
63 static int target_quotacheck_callback(struct obd_export *exp,
64                                       struct obd_quotactl *oqctl)
65 {
66         struct ptlrpc_request *req;
67         struct obd_quotactl   *body;
68         int                    rc;
69         ENTRY;
70
71         req = ptlrpc_request_alloc_pack(exp->exp_imp_reverse, &RQF_QC_CALLBACK,
72                                         LUSTRE_OBD_VERSION, OBD_QC_CALLBACK);
73         if (req == NULL)
74                 RETURN(-ENOMEM);
75
76         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
77         *body = *oqctl;
78
79         ptlrpc_request_set_replen(req);
80
81         rc = ptlrpc_queue_wait(req);
82         ptlrpc_req_finished(req);
83
84         RETURN(rc);
85 }
86
87 static int target_quotacheck_thread(void *data)
88 {
89         struct quotacheck_thread_args *qta = data;
90         struct obd_export *exp;
91         struct obd_device *obd;
92         struct obd_quotactl *oqctl;
93         struct lvfs_run_ctxt saved;
94         int rc;
95
96         cfs_daemonize_ctxt("quotacheck");
97
98         exp = qta->qta_exp;
99         obd = qta->qta_obd;
100         oqctl = &qta->qta_oqctl;
101
102         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
103
104         rc = fsfilt_quotacheck(obd, qta->qta_sb, oqctl);
105         if (rc)
106                 CERROR("%s: fsfilt_quotacheck: %d\n", obd->obd_name, rc);
107
108         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
109
110         rc = target_quotacheck_callback(exp, oqctl);
111         class_export_put(exp);
112         cfs_up(qta->qta_sem);
113         OBD_FREE_PTR(qta);
114         return rc;
115 }
116
117 int target_quota_check(struct obd_device *obd, struct obd_export *exp,
118                        struct obd_quotactl *oqctl)
119 {
120         struct obd_device_target *obt = &obd->u.obt;
121         struct quotacheck_thread_args *qta;
122         int rc = 0;
123         ENTRY;
124
125         OBD_ALLOC_PTR(qta);
126         if (!qta)
127                 RETURN(ENOMEM);
128
129         cfs_down(&obt->obt_quotachecking);
130
131         qta->qta_exp = exp;
132         qta->qta_obd = obd;
133         qta->qta_oqctl = *oqctl;
134         qta->qta_oqctl.qc_id = obt->obt_qfmt; /* override qfmt version */
135         qta->qta_sb = obt->obt_sb;
136         qta->qta_sem = &obt->obt_quotachecking;
137
138         /* quotaoff firstly */
139         oqctl->qc_cmd = Q_QUOTAOFF;
140         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
141                 rc = do_mds_quota_off(obd, oqctl);
142                 if (rc && rc != -EALREADY) {
143                         CERROR("off quota on MDS failed: %d\n", rc);
144                         GOTO(out, rc);
145                 }
146
147                 /* quota master */
148                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
149                 if (rc) {
150                         CERROR("init_admin_quotafiles failed: %d\n", rc);
151                         GOTO(out, rc);
152                 }
153         } else {
154                 struct lvfs_run_ctxt saved;
155                 struct lustre_quota_ctxt *qctxt = &obt->obt_qctxt;
156
157                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
158                 rc = fsfilt_quotactl(obd, obt->obt_sb, oqctl);
159                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
160                 if (!rc) {
161                         qctxt->lqc_flags &= ~UGQUOTA2LQC(oqctl->qc_type);
162                 } else if (!quota_is_off(qctxt, oqctl)) {
163                         CERROR("off quota on OSS failed: %d\n", rc);
164                         GOTO(out, rc);
165                 }
166         }
167
168         /* we get ref for exp because target_quotacheck_callback() will use this
169          * export later b=18126 */
170         class_export_get(exp);
171         rc = cfs_create_thread(target_quotacheck_thread, qta,
172                                CFS_DAEMON_FLAGS);
173         if (rc >= 0) {
174                 /* target_quotacheck_thread will drop the ref on exp and release
175                  * obt_quotachecking */
176                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
177                        obd->obd_name, rc);
178                 RETURN(0);
179         } else {
180                 CERROR("%s: error starting quotacheck_thread: %d\n",
181                        obd->obd_name, rc);
182                 class_export_put(exp);
183                 EXIT;
184         }
185
186 out:
187         cfs_up(&obt->obt_quotachecking);
188         OBD_FREE_PTR(qta);
189         return rc;
190 }
191
192 #endif /* __KERNEL__ */