Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / fld / fld_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  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/fld/fld_request.c
39  *
40  * FLD (Fids Location Database)
41  *
42  * Author: Yury Umanets <umka@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_FLD
49
50 #ifdef __KERNEL__
51 # include <libcfs/libcfs.h>
52 # include <linux/module.h>
53 # include <linux/jbd.h>
54 # include <asm/div64.h>
55 #else /* __KERNEL__ */
56 # include <liblustre.h>
57 # include <libcfs/list.h>
58 #endif
59
60 #include <obd.h>
61 #include <obd_class.h>
62 #include <lustre_ver.h>
63 #include <obd_support.h>
64 #include <lprocfs_status.h>
65
66 #include <dt_object.h>
67 #include <md_object.h>
68 #include <lustre_req_layout.h>
69 #include <lustre_fld.h>
70 #include <lustre_mdc.h>
71 #include "fld_internal.h"
72
73 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
74  * It should be common thing. The same about mdc RPC lock */
75 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
76 {
77         int rc;
78         ENTRY;
79         client_obd_list_lock(&cli->cl_loi_list_lock);
80         rc = cfs_list_empty(&mcw->mcw_entry);
81         client_obd_list_unlock(&cli->cl_loi_list_lock);
82         RETURN(rc);
83 };
84
85 static void fld_enter_request(struct client_obd *cli)
86 {
87         struct mdc_cache_waiter mcw;
88         struct l_wait_info lwi = { 0 };
89
90         client_obd_list_lock(&cli->cl_loi_list_lock);
91         if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
92                 cfs_list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
93                 cfs_waitq_init(&mcw.mcw_waitq);
94                 client_obd_list_unlock(&cli->cl_loi_list_lock);
95                 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
96         } else {
97                 cli->cl_r_in_flight++;
98                 client_obd_list_unlock(&cli->cl_loi_list_lock);
99         }
100 }
101
102 static void fld_exit_request(struct client_obd *cli)
103 {
104         cfs_list_t *l, *tmp;
105         struct mdc_cache_waiter *mcw;
106
107         client_obd_list_lock(&cli->cl_loi_list_lock);
108         cli->cl_r_in_flight--;
109         cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
110
111                 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
112                         /* No free request slots anymore */
113                         break;
114                 }
115
116                 mcw = cfs_list_entry(l, struct mdc_cache_waiter, mcw_entry);
117                 cfs_list_del_init(&mcw->mcw_entry);
118                 cli->cl_r_in_flight++;
119                 cfs_waitq_signal(&mcw->mcw_waitq);
120         }
121         client_obd_list_unlock(&cli->cl_loi_list_lock);
122 }
123
124 static int fld_rrb_hash(struct lu_client_fld *fld,
125                         seqno_t seq)
126 {
127         LASSERT(fld->lcf_count > 0);
128         return do_div(seq, fld->lcf_count);
129 }
130
131 static struct lu_fld_target *
132 fld_rrb_scan(struct lu_client_fld *fld, seqno_t seq)
133 {
134         struct lu_fld_target *target;
135         int hash;
136         ENTRY;
137
138         hash = fld_rrb_hash(fld, seq);
139
140         cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
141                 if (target->ft_idx == hash)
142                         RETURN(target);
143         }
144
145         CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
146                "Targets (%d):\n", fld->lcf_name, hash, seq,
147                fld->lcf_count);
148
149         cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
150                 const char *srv_name = target->ft_srv != NULL  ?
151                         target->ft_srv->lsf_name : "<null>";
152                 const char *exp_name = target->ft_exp != NULL ?
153                         (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
154                         "<null>";
155
156                 CERROR("  exp: 0x%p (%s), srv: 0x%p (%s), idx: "LPU64"\n",
157                        target->ft_exp, exp_name, target->ft_srv,
158                        srv_name, target->ft_idx);
159         }
160
161         /*
162          * If target is not found, there is logical error anyway, so here is
163          * LBUG() to catch this situation.
164          */
165         LBUG();
166         RETURN(NULL);
167 }
168
169 struct lu_fld_hash fld_hash[] = {
170         {
171                 .fh_name = "RRB",
172                 .fh_hash_func = fld_rrb_hash,
173                 .fh_scan_func = fld_rrb_scan
174         },
175         {
176                 0,
177         }
178 };
179
180 static struct lu_fld_target *
181 fld_client_get_target(struct lu_client_fld *fld,
182                       seqno_t seq)
183 {
184         struct lu_fld_target *target;
185         ENTRY;
186
187         LASSERT(fld->lcf_hash != NULL);
188
189         cfs_spin_lock(&fld->lcf_lock);
190         target = fld->lcf_hash->fh_scan_func(fld, seq);
191         cfs_spin_unlock(&fld->lcf_lock);
192
193         if (target != NULL) {
194                 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
195                        ") by seq "LPX64"\n", fld->lcf_name,
196                        target->ft_idx, seq);
197         }
198
199         RETURN(target);
200 }
201
202 /*
203  * Add export to FLD. This is usually done by CMM and LMV as they are main users
204  * of FLD module.
205  */
206 int fld_client_add_target(struct lu_client_fld *fld,
207                           struct lu_fld_target *tar)
208 {
209         const char *name = fld_target_name(tar);
210         struct lu_fld_target *target, *tmp;
211         ENTRY;
212
213         LASSERT(tar != NULL);
214         LASSERT(name != NULL);
215         LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
216
217         if (fld->lcf_flags != LUSTRE_FLD_INIT) {
218                 CERROR("%s: Attempt to add target %s (idx "LPU64") "
219                        "on fly - skip it\n", fld->lcf_name, name,
220                        tar->ft_idx);
221                 RETURN(0);
222         } else {
223                 CDEBUG(D_INFO, "%s: Adding target %s (idx "
224                        LPU64")\n", fld->lcf_name, name, tar->ft_idx);
225         }
226
227         OBD_ALLOC_PTR(target);
228         if (target == NULL)
229                 RETURN(-ENOMEM);
230
231         cfs_spin_lock(&fld->lcf_lock);
232         cfs_list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
233                 if (tmp->ft_idx == tar->ft_idx) {
234                         cfs_spin_unlock(&fld->lcf_lock);
235                         OBD_FREE_PTR(target);
236                         CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
237                                name, fld_target_name(tmp), tmp->ft_idx);
238                         RETURN(-EEXIST);
239                 }
240         }
241
242         target->ft_exp = tar->ft_exp;
243         if (target->ft_exp != NULL)
244                 class_export_get(target->ft_exp);
245         target->ft_srv = tar->ft_srv;
246         target->ft_idx = tar->ft_idx;
247
248         cfs_list_add_tail(&target->ft_chain,
249                           &fld->lcf_targets);
250
251         fld->lcf_count++;
252         cfs_spin_unlock(&fld->lcf_lock);
253
254         RETURN(0);
255 }
256 EXPORT_SYMBOL(fld_client_add_target);
257
258 /* Remove export from FLD */
259 int fld_client_del_target(struct lu_client_fld *fld,
260                           __u64 idx)
261 {
262         struct lu_fld_target *target, *tmp;
263         ENTRY;
264
265         cfs_spin_lock(&fld->lcf_lock);
266         cfs_list_for_each_entry_safe(target, tmp,
267                                      &fld->lcf_targets, ft_chain) {
268                 if (target->ft_idx == idx) {
269                         fld->lcf_count--;
270                         cfs_list_del(&target->ft_chain);
271                         cfs_spin_unlock(&fld->lcf_lock);
272
273                         if (target->ft_exp != NULL)
274                                 class_export_put(target->ft_exp);
275
276                         OBD_FREE_PTR(target);
277                         RETURN(0);
278                 }
279         }
280         cfs_spin_unlock(&fld->lcf_lock);
281         RETURN(-ENOENT);
282 }
283 EXPORT_SYMBOL(fld_client_del_target);
284
285 #ifdef LPROCFS
286 static int fld_client_proc_init(struct lu_client_fld *fld)
287 {
288         int rc;
289         ENTRY;
290
291         fld->lcf_proc_dir = lprocfs_register(fld->lcf_name,
292                                              fld_type_proc_dir,
293                                              NULL, NULL);
294
295         if (IS_ERR(fld->lcf_proc_dir)) {
296                 CERROR("%s: LProcFS failed in fld-init\n",
297                        fld->lcf_name);
298                 rc = PTR_ERR(fld->lcf_proc_dir);
299                 RETURN(rc);
300         }
301
302         rc = lprocfs_add_vars(fld->lcf_proc_dir,
303                               fld_client_proc_list, fld);
304         if (rc) {
305                 CERROR("%s: Can't init FLD proc, rc %d\n",
306                        fld->lcf_name, rc);
307                 GOTO(out_cleanup, rc);
308         }
309
310         RETURN(0);
311
312 out_cleanup:
313         fld_client_proc_fini(fld);
314         return rc;
315 }
316
317 void fld_client_proc_fini(struct lu_client_fld *fld)
318 {
319         ENTRY;
320         if (fld->lcf_proc_dir) {
321                 if (!IS_ERR(fld->lcf_proc_dir))
322                         lprocfs_remove(&fld->lcf_proc_dir);
323                 fld->lcf_proc_dir = NULL;
324         }
325         EXIT;
326 }
327 #else
328 static int fld_client_proc_init(struct lu_client_fld *fld)
329 {
330         return 0;
331 }
332
333 void fld_client_proc_fini(struct lu_client_fld *fld)
334 {
335         return;
336 }
337 #endif
338
339 EXPORT_SYMBOL(fld_client_proc_fini);
340
341 static inline int hash_is_sane(int hash)
342 {
343         return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
344 }
345
346 int fld_client_init(struct lu_client_fld *fld,
347                     const char *prefix, int hash)
348 {
349         int cache_size, cache_threshold;
350         int rc;
351         ENTRY;
352
353         LASSERT(fld != NULL);
354
355         snprintf(fld->lcf_name, sizeof(fld->lcf_name),
356                  "cli-%s", prefix);
357
358         if (!hash_is_sane(hash)) {
359                 CERROR("%s: Wrong hash function %#x\n",
360                        fld->lcf_name, hash);
361                 RETURN(-EINVAL);
362         }
363
364         fld->lcf_count = 0;
365         cfs_spin_lock_init(&fld->lcf_lock);
366         fld->lcf_hash = &fld_hash[hash];
367         fld->lcf_flags = LUSTRE_FLD_INIT;
368         CFS_INIT_LIST_HEAD(&fld->lcf_targets);
369
370         cache_size = FLD_CLIENT_CACHE_SIZE /
371                 sizeof(struct fld_cache_entry);
372
373         cache_threshold = cache_size *
374                 FLD_CLIENT_CACHE_THRESHOLD / 100;
375
376         fld->lcf_cache = fld_cache_init(fld->lcf_name,
377                                         cache_size, cache_threshold);
378         if (IS_ERR(fld->lcf_cache)) {
379                 rc = PTR_ERR(fld->lcf_cache);
380                 fld->lcf_cache = NULL;
381                 GOTO(out, rc);
382         }
383
384         rc = fld_client_proc_init(fld);
385         if (rc)
386                 GOTO(out, rc);
387         EXIT;
388 out:
389         if (rc)
390                 fld_client_fini(fld);
391         else
392                 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
393                        fld->lcf_name, fld->lcf_hash->fh_name);
394         return rc;
395 }
396 EXPORT_SYMBOL(fld_client_init);
397
398 void fld_client_fini(struct lu_client_fld *fld)
399 {
400         struct lu_fld_target *target, *tmp;
401         ENTRY;
402
403         cfs_spin_lock(&fld->lcf_lock);
404         cfs_list_for_each_entry_safe(target, tmp,
405                                      &fld->lcf_targets, ft_chain) {
406                 fld->lcf_count--;
407                 cfs_list_del(&target->ft_chain);
408                 if (target->ft_exp != NULL)
409                         class_export_put(target->ft_exp);
410                 OBD_FREE_PTR(target);
411         }
412         cfs_spin_unlock(&fld->lcf_lock);
413
414         if (fld->lcf_cache != NULL) {
415                 if (!IS_ERR(fld->lcf_cache))
416                         fld_cache_fini(fld->lcf_cache);
417                 fld->lcf_cache = NULL;
418         }
419
420         EXIT;
421 }
422 EXPORT_SYMBOL(fld_client_fini);
423
424 int fld_client_rpc(struct obd_export *exp,
425                    struct lu_seq_range *range, __u32 fld_op)
426 {
427         struct ptlrpc_request *req;
428         struct lu_seq_range      *prange;
429         __u32                 *op;
430         int                    rc;
431         ENTRY;
432
433         LASSERT(exp != NULL);
434
435         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_FLD_QUERY,
436                                         LUSTRE_MDS_VERSION, FLD_QUERY);
437         if (req == NULL)
438                 RETURN(-ENOMEM);
439
440         op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
441         *op = fld_op;
442
443         prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
444         *prange = *range;
445
446         ptlrpc_request_set_replen(req);
447         req->rq_request_portal = FLD_REQUEST_PORTAL;
448         ptlrpc_at_set_req_timeout(req);
449
450         if (fld_op != FLD_LOOKUP)
451                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
452         fld_enter_request(&exp->exp_obd->u.cli);
453         rc = ptlrpc_queue_wait(req);
454         fld_exit_request(&exp->exp_obd->u.cli);
455         if (fld_op != FLD_LOOKUP)
456                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
457         if (rc)
458                 GOTO(out_req, rc);
459
460         prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
461         if (prange == NULL)
462                 GOTO(out_req, rc = -EFAULT);
463         *range = *prange;
464         EXIT;
465 out_req:
466         ptlrpc_req_finished(req);
467         return rc;
468 }
469
470 int fld_client_lookup(struct lu_client_fld *fld, seqno_t seq, mdsno_t *mds,
471                       __u32 flags, const struct lu_env *env)
472 {
473         struct lu_seq_range res;
474         struct lu_fld_target *target;
475         int rc;
476         ENTRY;
477
478         fld->lcf_flags |= LUSTRE_FLD_RUN;
479
480         rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
481         if (rc == 0) {
482                 *mds = res.lsr_index;
483                 RETURN(0);
484         }
485
486         /* Can not find it in the cache */
487         target = fld_client_get_target(fld, seq);
488         LASSERT(target != NULL);
489
490         CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
491                "target %s (idx "LPU64")\n", fld->lcf_name, seq,
492                fld_target_name(target), target->ft_idx);
493
494         res.lsr_start = seq;
495         res.lsr_flags = flags;
496 #ifdef __KERNEL__
497         if (target->ft_srv != NULL) {
498                 LASSERT(env != NULL);
499                 rc = fld_server_lookup(target->ft_srv,
500                                        env, seq, &res);
501         } else {
502 #endif
503                 rc = fld_client_rpc(target->ft_exp,
504                                     &res, FLD_LOOKUP);
505 #ifdef __KERNEL__
506         }
507 #endif
508
509         if (rc == 0) {
510                 *mds = res.lsr_index;
511
512                 fld_cache_insert(fld->lcf_cache, &res);
513         }
514         RETURN(rc);
515 }
516 EXPORT_SYMBOL(fld_client_lookup);
517
518 void fld_client_flush(struct lu_client_fld *fld)
519 {
520         fld_cache_flush(fld->lcf_cache);
521 }
522 EXPORT_SYMBOL(fld_client_flush);