Whamcloud - gitweb
b=24255 move seq_client_alloc_seq out of lcs_sem
[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 (c) 2007, 2010, Oracle and/or its affiliates. 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,
67                           struct lu_seq_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_seq_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         range_init(in);
89
90         ptlrpc_request_set_replen(req);
91
92        if (seq->lcs_type == LUSTRE_SEQ_METADATA) {
93                 req->rq_request_portal = SEQ_METADATA_PORTAL;
94                 in->lsr_flags = LU_SEQ_RANGE_MDT;
95         } else {
96                 LASSERTF(seq->lcs_type == LUSTRE_SEQ_DATA,
97                          "unknown lcs_type %u\n", seq->lcs_type);
98                 req->rq_request_portal = SEQ_DATA_PORTAL;
99                 in->lsr_flags = LU_SEQ_RANGE_OST;
100         }
101
102         if (opc == SEQ_ALLOC_SUPER) {
103                 /* Update index field of *in, it is required for
104                  * FLD update on super sequence allocator node. */
105                 in->lsr_index = seq->lcs_space.lsr_index;
106                 req->rq_request_portal = SEQ_CONTROLLER_PORTAL;
107         } else {
108                 LASSERTF(opc == SEQ_ALLOC_META,
109                          "unknown opcode %u\n, opc", opc);
110         }
111
112         ptlrpc_at_set_req_timeout(req);
113
114         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
115         rc = ptlrpc_queue_wait(req);
116         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
117
118         if (rc)
119                 GOTO(out_req, rc);
120
121         out = req_capsule_server_get(&req->rq_pill, &RMF_SEQ_RANGE);
122         *output = *out;
123
124         if (!range_is_sane(output)) {
125                 CERROR("%s: Invalid range received from server: "
126                        DRANGE"\n", seq->lcs_name, PRANGE(output));
127                 GOTO(out_req, rc = -EINVAL);
128         }
129
130         if (range_is_exhausted(output)) {
131                 CERROR("%s: Range received from server is exhausted: "
132                        DRANGE"]\n", seq->lcs_name, PRANGE(output));
133                 GOTO(out_req, rc = -EINVAL);
134         }
135
136         CDEBUG(D_INFO, "%s: Allocated %s-sequence "DRANGE"]\n",
137                seq->lcs_name, opcname, PRANGE(output));
138
139         EXIT;
140 out_req:
141         ptlrpc_req_finished(req);
142         return rc;
143 }
144
145 /* Request sequence-controller node to allocate new super-sequence. */
146 int seq_client_alloc_super(struct lu_client_seq *seq,
147                            const struct lu_env *env)
148 {
149         int rc;
150         ENTRY;
151
152         cfs_down(&seq->lcs_sem);
153
154 #ifdef __KERNEL__
155         if (seq->lcs_srv) {
156                 LASSERT(env != NULL);
157                 rc = seq_server_alloc_super(seq->lcs_srv, &seq->lcs_space,
158                                             env);
159         } else {
160 #endif
161                 rc = seq_client_rpc(seq, &seq->lcs_space,
162                                     SEQ_ALLOC_SUPER, "super");
163 #ifdef __KERNEL__
164         }
165 #endif
166         cfs_up(&seq->lcs_sem);
167         RETURN(rc);
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, &seq->lcs_space, env);
181         } else {
182 #endif
183                 rc = seq_client_rpc(seq, &seq->lcs_space,
184                                     SEQ_ALLOC_META, "meta");
185 #ifdef __KERNEL__
186         }
187 #endif
188         RETURN(rc);
189 }
190
191 /* Allocate new sequence for client. */
192 static int seq_client_alloc_seq(struct lu_client_seq *seq, seqno_t *seqnr)
193 {
194         int rc;
195         ENTRY;
196
197         LASSERT(range_is_sane(&seq->lcs_space));
198
199         if (range_is_exhausted(&seq->lcs_space)) {
200                 rc = seq_client_alloc_meta(seq, NULL);
201                 if (rc) {
202                         CERROR("%s: Can't allocate new meta-sequence, "
203                                "rc %d\n", seq->lcs_name, rc);
204                         RETURN(rc);
205                 } else {
206                         CDEBUG(D_INFO, "%s: New range - "DRANGE"\n",
207                                seq->lcs_name, PRANGE(&seq->lcs_space));
208                 }
209         } else {
210                 rc = 0;
211         }
212
213         LASSERT(!range_is_exhausted(&seq->lcs_space));
214         *seqnr = seq->lcs_space.lsr_start;
215         seq->lcs_space.lsr_start += 1;
216
217         CDEBUG(D_INFO, "%s: Allocated sequence ["LPX64"]\n", seq->lcs_name,
218                *seqnr);
219
220         RETURN(rc);
221 }
222
223 static int seq_fid_alloc_prep(struct lu_client_seq *seq,
224                               cfs_waitlink_t *link)
225 {
226
227         cfs_waitq_add(&seq->lcs_waitq, link);
228         if (seq->lcs_update) {
229                 cfs_up(&seq->lcs_sem);
230                 cfs_set_current_state(CFS_TASK_UNINT);
231                 cfs_waitq_wait(link, CFS_TASK_UNINT);
232                 cfs_set_current_state(CFS_TASK_RUNNING);
233                 cfs_down(&seq->lcs_sem);
234                 return -EAGAIN;
235         }
236         ++seq->lcs_update;
237         cfs_up(&seq->lcs_sem);
238         return 0;
239 }
240
241 static void seq_fid_alloc_fini(struct lu_client_seq *seq,
242                                cfs_waitlink_t *link)
243 {
244         LASSERT(seq->lcs_update == 1);
245         cfs_down(&seq->lcs_sem);
246         --seq->lcs_update;
247         cfs_waitq_del(&seq->lcs_waitq, link);
248         cfs_waitq_signal(&seq->lcs_waitq);
249 }
250
251 /* Allocate new fid on passed client @seq and save it to @fid. */
252 int seq_client_alloc_fid(struct lu_client_seq *seq, struct lu_fid *fid)
253 {
254         cfs_waitlink_t link;
255         int rc;
256         ENTRY;
257
258         LASSERT(seq != NULL);
259         LASSERT(fid != NULL);
260
261         cfs_waitlink_init(&link);
262         cfs_down(&seq->lcs_sem);
263
264         while (1) {
265                 seqno_t seqnr;
266
267                 if (!fid_is_zero(&seq->lcs_fid) &&
268                     fid_oid(&seq->lcs_fid) < seq->lcs_width) {
269                         /* Just bump last allocated fid and return to caller. */
270                         seq->lcs_fid.f_oid += 1;
271                         rc = 0;
272                         break;
273                 }
274
275                 rc = seq_fid_alloc_prep(seq, &link);
276                 if (rc)
277                         continue;
278
279                 rc = seq_client_alloc_seq(seq, &seqnr);
280                 if (rc) {
281                         CERROR("%s: Can't allocate new sequence, "
282                                "rc %d\n", seq->lcs_name, rc);
283                         cfs_up(&seq->lcs_sem);
284                         RETURN(rc);
285                 }
286
287                 CDEBUG(D_INFO, "%s: Switch to sequence "
288                        "[0x%16.16"LPF64"x]\n", seq->lcs_name, seqnr);
289
290                 seq->lcs_fid.f_oid = LUSTRE_FID_INIT_OID;
291                 seq->lcs_fid.f_seq = seqnr;
292                 seq->lcs_fid.f_ver = 0;
293
294                 /*
295                  * Inform caller that sequence switch is performed to allow it
296                  * to setup FLD for it.
297                  */
298                 rc = 1;
299
300                 seq_fid_alloc_fini(seq, &link);
301                 break;
302         }
303
304         *fid = seq->lcs_fid;
305         cfs_up(&seq->lcs_sem);
306
307         CDEBUG(D_INFO, "%s: Allocated FID "DFID"\n", seq->lcs_name,  PFID(fid));
308         RETURN(rc);
309 }
310 EXPORT_SYMBOL(seq_client_alloc_fid);
311
312 /*
313  * Finish the current sequence due to disconnect.
314  * See mdc_import_event()
315  */
316 void seq_client_flush(struct lu_client_seq *seq)
317 {
318         LASSERT(seq != NULL);
319         cfs_down(&seq->lcs_sem);
320         fid_zero(&seq->lcs_fid);
321         /**
322          * this id shld not be used for seq range allocation.
323          * set to -1 for dgb check.
324          */
325
326         seq->lcs_space.lsr_index = -1;
327
328         range_init(&seq->lcs_space);
329         cfs_up(&seq->lcs_sem);
330 }
331 EXPORT_SYMBOL(seq_client_flush);
332
333 static void seq_client_proc_fini(struct lu_client_seq *seq);
334
335 #ifdef LPROCFS
336 static int seq_client_proc_init(struct lu_client_seq *seq)
337 {
338         int rc;
339         ENTRY;
340
341         seq->lcs_proc_dir = lprocfs_register(seq->lcs_name,
342                                              seq_type_proc_dir,
343                                              NULL, NULL);
344
345         if (IS_ERR(seq->lcs_proc_dir)) {
346                 CERROR("%s: LProcFS failed in seq-init\n",
347                        seq->lcs_name);
348                 rc = PTR_ERR(seq->lcs_proc_dir);
349                 RETURN(rc);
350         }
351
352         rc = lprocfs_add_vars(seq->lcs_proc_dir,
353                               seq_client_proc_list, seq);
354         if (rc) {
355                 CERROR("%s: Can't init sequence manager "
356                        "proc, rc %d\n", seq->lcs_name, rc);
357                 GOTO(out_cleanup, rc);
358         }
359
360         RETURN(0);
361
362 out_cleanup:
363         seq_client_proc_fini(seq);
364         return rc;
365 }
366
367 static void seq_client_proc_fini(struct lu_client_seq *seq)
368 {
369         ENTRY;
370         if (seq->lcs_proc_dir) {
371                 if (!IS_ERR(seq->lcs_proc_dir))
372                         lprocfs_remove(&seq->lcs_proc_dir);
373                 seq->lcs_proc_dir = NULL;
374         }
375         EXIT;
376 }
377 #else
378 static int seq_client_proc_init(struct lu_client_seq *seq)
379 {
380         return 0;
381 }
382
383 static void seq_client_proc_fini(struct lu_client_seq *seq)
384 {
385         return;
386 }
387 #endif
388
389 int seq_client_init(struct lu_client_seq *seq,
390                     struct obd_export *exp,
391                     enum lu_cli_type type,
392                     const char *prefix,
393                     struct lu_server_seq *srv)
394 {
395         int rc;
396         ENTRY;
397
398         LASSERT(seq != NULL);
399         LASSERT(prefix != NULL);
400
401         seq->lcs_exp = exp;
402         seq->lcs_srv = srv;
403         seq->lcs_type = type;
404         cfs_sema_init(&seq->lcs_sem, 1);
405         seq->lcs_width = LUSTRE_SEQ_MAX_WIDTH;
406         cfs_waitq_init(&seq->lcs_waitq);
407
408         /* Make sure that things are clear before work is started. */
409         seq_client_flush(seq);
410
411         if (exp == NULL) {
412                 LASSERT(seq->lcs_srv != NULL);
413         } else {
414                 LASSERT(seq->lcs_exp != NULL);
415                 seq->lcs_exp = class_export_get(seq->lcs_exp);
416         }
417
418         snprintf(seq->lcs_name, sizeof(seq->lcs_name),
419                  "cli-%s", prefix);
420
421         rc = seq_client_proc_init(seq);
422         if (rc)
423                 seq_client_fini(seq);
424         RETURN(rc);
425 }
426 EXPORT_SYMBOL(seq_client_init);
427
428 void seq_client_fini(struct lu_client_seq *seq)
429 {
430         ENTRY;
431
432         seq_client_proc_fini(seq);
433
434         if (seq->lcs_exp != NULL) {
435                 class_export_put(seq->lcs_exp);
436                 seq->lcs_exp = NULL;
437         }
438
439         seq->lcs_srv = NULL;
440         EXIT;
441 }
442 EXPORT_SYMBOL(seq_client_fini);