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