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