Whamcloud - gitweb
land b1_5 onto 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  *  lustre/quota/quota_check.c
5  *
6  *  Copyright (c) 2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   No redistribution or use is permitted outside of Cluster File Systems, Inc.
11  *
12  */
13 #ifndef EXPORT_SYMTAB
14 # define EXPORT_SYMTAB
15 #endif
16 #define DEBUG_SUBSYSTEM S_MDS
17
18 #ifdef __KERNEL__
19 # include <linux/version.h>
20 # include <linux/module.h>
21 # include <linux/init.h>
22 # include <linux/fs.h>
23 # include <linux/jbd.h>
24 # include <linux/ext3_fs.h>
25 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
26 #  include <linux/smp_lock.h>
27 #  include <linux/buffer_head.h>
28 #  include <linux/workqueue.h>
29 #  include <linux/mount.h>
30 # else
31 #  include <linux/locks.h>
32 # endif
33 #else /* __KERNEL__ */
34 # include <liblustre.h>
35 #endif
36
37 #include <obd_class.h>
38 #include <lustre_mds.h>
39 #include <lustre_dlm.h>
40 #include <lustre_cfg.h>
41 #include <obd_ost.h>
42 #include <lustre_fsfilt.h>
43 #include <lustre_quota.h>
44 #include "quota_internal.h"
45
46 #ifdef __KERNEL__
47 static int target_quotacheck_callback(struct obd_export *exp,
48                                       struct obd_quotactl *oqctl)
49 {
50         struct ptlrpc_request *req;
51         struct obd_quotactl *body;
52         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*oqctl) };
53         ENTRY;
54
55         req = ptlrpc_prep_req(exp->exp_imp_reverse, LUSTRE_OBD_VERSION,
56                               OBD_QC_CALLBACK, 2, size, NULL);
57         if (!req)
58                 RETURN(-ENOMEM);
59
60         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
61         *body = *oqctl;
62
63         ptlrpc_req_set_repsize(req, 1, NULL);
64
65         rc = ptlrpc_queue_wait(req);
66         ptlrpc_req_finished(req);
67
68         RETURN(rc);
69 }
70
71 static int target_quotacheck_thread(void *data)
72 {
73         struct quotacheck_thread_args *qta = data;
74         struct obd_export *exp;
75         struct obd_device *obd;
76         struct obd_quotactl *oqctl;
77         struct lvfs_run_ctxt saved;
78         int rc;
79
80         ptlrpc_daemonize("quotacheck");
81
82         exp = qta->qta_exp;
83         obd = exp->exp_obd;
84         oqctl = &qta->qta_oqctl;
85
86         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
87
88         rc = fsfilt_quotacheck(obd, qta->qta_sb, oqctl);
89         if (rc)
90                 CERROR("%s: fsfilt_quotacheck: %d\n", obd->obd_name, rc);
91
92         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
93
94         rc = target_quotacheck_callback(exp, oqctl);
95
96         atomic_inc(qta->qta_sem);
97
98         OBD_FREE_PTR(qta);
99         return rc;
100 }
101
102 int target_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
103 {
104         struct obd_device *obd = exp->exp_obd;
105         struct obd_device_target *obt = &obd->u.obt;
106         struct quotacheck_thread_args *qta;
107         int rc = 0;
108         ENTRY;
109
110         if (!atomic_dec_and_test(&obt->obt_quotachecking)) {
111                 CDEBUG(D_INFO, "other people are doing quotacheck\n");
112                 GOTO(out, rc = -EBUSY);
113         }
114
115         OBD_ALLOC_PTR(qta);
116         if (!qta)
117                 GOTO(out, rc = -ENOMEM);
118
119         qta->qta_exp = exp;
120         qta->qta_oqctl = *oqctl;
121         qta->qta_sb = obt->obt_sb;
122         qta->qta_sem = &obt->obt_quotachecking;
123
124         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
125                 /* quota master */
126                 rc = init_admin_quotafiles(obd, &qta->qta_oqctl);
127                 if (rc) {
128                         CERROR("init_admin_quotafiles failed: %d\n", rc);
129                         OBD_FREE_PTR(qta);
130                         GOTO(out, rc);
131                 }
132         }
133
134         rc = kernel_thread(target_quotacheck_thread, qta, CLONE_VM|CLONE_FILES);
135         if (rc >= 0) {
136                 CDEBUG(D_INFO, "%s: target_quotacheck_thread: %d\n",
137                        obd->obd_name, rc);
138                 RETURN(0);
139         }
140
141         CERROR("%s: error starting quotacheck_thread: %d\n",
142                obd->obd_name, rc);
143         OBD_FREE_PTR(qta);
144 out:
145         atomic_inc(&obt->obt_quotachecking);
146         RETURN(rc);
147 }
148
149 #endif /* __KERNEL__ */
150
151 int client_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
152 {
153         struct client_obd *cli = &exp->exp_obd->u.cli;
154         struct ptlrpc_request *req;
155         struct obd_quotactl *body;
156         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
157         int ver, opc, rc;
158         ENTRY;
159
160         if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME)) {
161                 ver = LUSTRE_MDS_VERSION;
162                 opc = MDS_QUOTACHECK;
163         } else if (!strcmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
164                 ver = LUSTRE_OST_VERSION;
165                 opc = OST_QUOTACHECK;
166         } else {
167                 RETURN(-EINVAL);
168         }
169
170         req = ptlrpc_prep_req(class_exp2cliimp(exp), ver, opc, 2, size, NULL);
171         if (!req)
172                 GOTO(out, rc = -ENOMEM);
173
174         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
175         *body = *oqctl;
176
177         ptlrpc_req_set_repsize(req, 1, NULL);
178
179         /* the next poll will find -ENODATA, that means quotacheck is
180          * going on */
181         cli->cl_qchk_stat = -ENODATA;
182         rc = ptlrpc_queue_wait(req);
183         if (rc)
184                 cli->cl_qchk_stat = rc;
185 out:
186         ptlrpc_req_finished(req);
187         RETURN(rc);
188 }
189
190 int client_quota_poll_check(struct obd_export *exp, struct if_quotacheck *qchk)
191 {
192         struct client_obd *cli = &exp->exp_obd->u.cli;
193         int rc;
194         ENTRY;
195
196         rc = cli->cl_qchk_stat;
197
198         /* the client is not the previous one */
199         if (rc == CL_NOT_QUOTACHECKED)
200                 rc = -EINTR;
201
202         qchk->obd_uuid = cli->cl_target_uuid;
203         /* FIXME change strncmp to strcmp and save the strlen op */
204         if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_OSC_NAME,
205             strlen(LUSTRE_OSC_NAME)))
206                 memcpy(qchk->obd_type, LUSTRE_OST_NAME,
207                        strlen(LUSTRE_OST_NAME));
208         else if (strncmp(exp->exp_obd->obd_type->typ_name, LUSTRE_MDC_NAME,
209                  strlen(LUSTRE_MDC_NAME)))
210                 memcpy(qchk->obd_type, LUSTRE_MDS_NAME,
211                        strlen(LUSTRE_MDS_NAME));
212
213         RETURN(rc);
214 }
215
216 int lov_quota_check(struct obd_export *exp, struct obd_quotactl *oqctl)
217 {
218         struct obd_device *obd = class_exp2obd(exp);
219         struct lov_obd *lov = &obd->u.lov;
220         int i, rc = 0;
221         ENTRY;
222
223         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
224                 int err;
225
226                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active) {
227                         CERROR("lov idx %d inactive\n", i);
228                         RETURN(-EIO);
229                 }
230
231                 err = obd_quotacheck(lov->lov_tgts[i]->ltd_exp, oqctl);
232                 if (err && lov->lov_tgts[i]->ltd_active && !rc)
233                         rc = err;
234         }
235
236         RETURN(rc);
237 }