Whamcloud - gitweb
b=16461
[fs/lustre-release.git] / lustre / fid / fid_request.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  * lustre/fid/fid_request.c
37  *
38  * Lustre Sequence Manager
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_FID
47
48 #ifdef __KERNEL__
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 #else /* __KERNEL__ */
52 # include <liblustre.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <dt_object.h>
58 #include <md_object.h>
59 #include <obd_support.h>
60 #include <lustre_req_layout.h>
61 #include <lustre_fid.h>
62 /* mdc RPC locks */
63 #include <lustre_mdc.h>
64 #include "fid_internal.h"
65
66 static int seq_client_rpc(struct lu_client_seq *seq, struct lu_range *input,
67                           struct lu_range *output, __u32 opc,
68                           const char *opcname)
69 {
70         struct obd_export     *exp = seq->lcs_exp;
71         struct ptlrpc_request *req;
72         struct lu_range       *out, *in;
73         __u32                 *op;
74         int                    rc;
75         ENTRY;
76
77         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_SEQ_QUERY,
78                                         LUSTRE_MDS_VERSION, SEQ_QUERY);
79         if (req == NULL)
80                 RETURN(-ENOMEM);
81
82         /* Init operation code */
83         op = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_OPC);
84         *op = opc;
85
86         /* Zero out input range, this is not recovery yet. */
87         in = req_capsule_client_get(&req->rq_pill, &RMF_SEQ_RANGE);
88         if (input != NULL)
89                 *in = *input;
90         else
91                 range_init(in);
92
93         ptlrpc_request_set_replen(req);
94
95         if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
96                 req->rq_request_portal = (opc == SEQ_ALLOC_SUPER) ?
97                         SEQ_CONTROLLER_PORTAL : SEQ_METADATA_PORTAL;
98         } else {
99                 req->rq_request_portal = (opc == SEQ_ALLOC_SUPER) ?
100                         SEQ_CONTROLLER_PORTAL : SEQ_DATA_PORTAL;
101         }
102         ptlrpc_at_set_req_timeout(req);
103
104         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
105         rc = ptlrpc_queue_wait(req);
106         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
107
108         if (rc)
109                 GOTO(out_req, rc);
110
111         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
112         *output = *out;
113
114         if (!range_is_sane(output)) {
115                 CERROR("%s: Invalid range received from server: "
116                        DRANGE"\n", seq->lcs_name, PRANGE(output));
117                 GOTO(out_req, rc = -EINVAL);
118         }
119
120         if (range_is_exhausted(output)) {
121                 CERROR("%s: Range received from server is exhausted: "
122                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
123                 GOTO(out_req, rc = -EINVAL);
124         }
125         *in = *out;
126
127         CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
128                seq->lcs_name, opcname, PRANGE(output));
129
130         EXIT;
131 out_req:
132         ptlrpc_req_finished(req);
133         return rc;
134 }
135
136 /* Request sequence-controller node to allocate new super-sequence. */
137 int seq_client_replay_super(struct lu_client_seq *seq,
138                             struct lu_range *range,
139                             const struct lu_env *env)
140 {
141         int rc;
142         ENTRY;
143
144         down(&seq->lcs_sem);
145
146 #ifdef __KERNEL__
147         if (seq->lcs_srv) {
148                 LASSERT(env != NULL);
149                 rc = seq_server_alloc_super(seq->lcs_srv, range,
150                                             &seq->lcs_space, env);
151         } else {
152 #endif
153                 rc = seq_client_rpc(seq, range, &seq->lcs_space,
154                                     SEQ_ALLOC_SUPER, "super");
155 #ifdef __KERNEL__
156         }
157 #endif
158         up(&seq->lcs_sem);
159         RETURN(rc);
160 }
161
162 /* Request sequence-controller node to allocate new super-sequence. */
163 int seq_client_alloc_super(struct lu_client_seq *seq,
164                            const struct lu_env *env)
165 {
166         ENTRY;
167         RETURN(seq_client_replay_super(seq, NULL, env));
168 }
169
170 /* Request sequence-controller node to allocate new meta-sequence. */
171 static int seq_client_alloc_meta(struct lu_client_seq *seq,
172                                  const struct lu_env *env)
173 {
174         int rc;
175         ENTRY;
176
177 #ifdef __KERNEL__
178         if (seq->lcs_srv) {
179                 LASSERT(env != NULL);
180                 rc = seq_server_alloc_meta(seq->lcs_srv, NULL,
181                                            &seq->lcs_space, env);
182         } else {
183 #endif
184                 rc = seq_client_rpc(seq, NULL, &seq->lcs_space,
185                                     SEQ_ALLOC_META, "meta");
186 #ifdef __KERNEL__
187         }
188 #endif
189         RETURN(rc);
190 }
191
192 /* Allocate new sequence for client. */
193 static int seq_client_alloc_seq(struct lu_client_seq *seq, seqno_t *seqnr)
194 {
195         int rc;
196         ENTRY;
197
198         LASSERT(range_is_sane(&seq->lcs_space));
199
200         if (range_is_exhausted(&seq->lcs_space)) {
201                 rc = seq_client_alloc_meta(seq, NULL);
202                 if (rc) {
203                         CERROR("%s: Can't allocate new meta-sequence, "
204                                "rc %d\n", seq->lcs_name, rc);
205                         RETURN(rc);
206                 } else {
207                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
208                                seq->lcs_name, PRANGE(&seq->lcs_space));
209                 }
210         } else {
211                 rc = 0;
212         }
213
214         LASSERT(!range_is_exhausted(&seq->lcs_space));
215         *seqnr = seq->lcs_space.lr_start;
216         seq->lcs_space.lr_start += 1;
217
218         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
219                *seqnr);
220
221         RETURN(rc);
222 }
223
224 /* Allocate new fid on passed client @seq and save it to @fid. */
225 int seq_client_alloc_fid(struct lu_client_seq *seq, struct lu_fid *fid)
226 {
227         int rc;
228         ENTRY;
229
230         LASSERT(seq != NULL);
231         LASSERT(fid != NULL);
232
233         down(&seq->lcs_sem);
234
235         if (fid_is_zero(&seq->lcs_fid) ||
236             fid_oid(&seq->lcs_fid) >= seq->lcs_width)
237         {
238                 seqno_t seqnr;
239
240                 rc = seq_client_alloc_seq(seq, &seqnr);
241                 if (rc) {
242                         CERROR("%s: Can't allocate new sequence, "
243                                "rc %d\n", seq->lcs_name, rc);
244                         up(&seq->lcs_sem);
245                         RETURN(rc);
246                 }
247
248                 CDEBUG(D_INFO, "%s: Switch to sequence "
249                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
250
251                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
252                 seq->lcs_fid.f_seq = seqnr;
253                 seq->lcs_fid.f_ver = 0;
254
255                 /*
256                  * Inform caller that sequence switch is performed to allow it
257                  * to setup FLD for it.
258                  */
259                 rc = 1;
260         } else {
261                 /* Just bump last allocated fid and return to caller. */
262                 seq->lcs_fid.f_oid += 1;
263                 rc = 0;
264         }
265
266         *fid = seq->lcs_fid;
267         up(&seq->lcs_sem);
268
269         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
270         RETURN(rc);
271 }
272 EXPORT_SYMBOL(seq_client_alloc_fid);
273
274 /*
275  * Finish the current sequence due to disconnect.
276  * See mdc_import_event()
277  */
278 void seq_client_flush(struct lu_client_seq *seq)
279 {
280         LASSERT(seq != NULL);
281         down(&seq->lcs_sem);
282         fid_zero(&seq->lcs_fid);
283         range_init(&seq->lcs_space);
284         up(&seq->lcs_sem);
285 }
286 EXPORT_SYMBOL(seq_client_flush);
287
288 static void seq_client_proc_fini(struct lu_client_seq *seq);
289
290 #ifdef LPROCFS
291 static int seq_client_proc_init(struct lu_client_seq *seq)
292 {
293         int rc;
294         ENTRY;
295
296         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
297                                              seq_type_proc_dir,
298                                              NULL, NULL);
299
300         if (IS_ERR(seq->lcs_proc_dir)) {
301                 CERROR("%s: LProcFS failed in seq-init\n",
302                        seq->lcs_name);
303                 rc = PTR_ERR(seq->lcs_proc_dir);
304                 RETURN(rc);
305         }
306
307         rc = lprocfs_add_vars(seq->lcs_proc_dir,
308                               seq_client_proc_list, seq);
309         if (rc) {
310                 CERROR("%s: Can't init sequence manager "
311                        "proc, rc %d\n", seq->lcs_name, rc);
312                 GOTO(out_cleanup, rc);
313         }
314
315         RETURN(0);
316
317 out_cleanup:
318         seq_client_proc_fini(seq);
319         return rc;
320 }
321
322 static void seq_client_proc_fini(struct lu_client_seq *seq)
323 {
324         ENTRY;
325         if (seq->lcs_proc_dir) {
326                 if (!IS_ERR(seq->lcs_proc_dir))
327                         lprocfs_remove(&seq->lcs_proc_dir);
328                 seq->lcs_proc_dir = NULL;
329         }
330         EXIT;
331 }
332 #else
333 static int seq_client_proc_init(struct lu_client_seq *seq)
334 {
335         return 0;
336 }
337
338 static void seq_client_proc_fini(struct lu_client_seq *seq)
339 {
340         return;
341 }
342 #endif
343
344 int seq_client_init(struct lu_client_seq *seq,
345                     struct obd_export *exp,
346                     enum lu_cli_type type,
347                     const char *prefix,
348                     struct lu_server_seq *srv)
349 {
350         int rc;
351         ENTRY;
352
353         LASSERT(seq != NULL);
354         LASSERT(prefix != NULL);
355
356         seq->lcs_exp = exp;
357         seq->lcs_srv = srv;
358         seq->lcs_type = type;
359         sema_init(&seq->lcs_sem, 1);
360         seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
361
362         /* Make sure that things are clear before work is started. */
363         seq_client_flush(seq);
364
365         if (exp == NULL) {
366                 LASSERT(seq->lcs_srv != NULL);
367         } else {
368                 LASSERT(seq->lcs_exp != NULL);
369                 seq->lcs_exp = class_export_get(seq->lcs_exp);
370         }
371
372         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
373                  "cli-%s", prefix);
374
375         rc = seq_client_proc_init(seq);
376         if (rc)
377                 seq_client_fini(seq);
378         RETURN(rc);
379 }
380 EXPORT_SYMBOL(seq_client_init);
381
382 void seq_client_fini(struct lu_client_seq *seq)
383 {
384         ENTRY;
385
386         seq_client_proc_fini(seq);
387
388         if (seq->lcs_exp != NULL) {
389                 class_export_put(seq->lcs_exp);
390                 seq->lcs_exp = NULL;
391         }
392
393         seq->lcs_srv = NULL;
394         EXIT;
395 }
396 EXPORT_SYMBOL(seq_client_fini);