Whamcloud - gitweb
fe8c1e411699888a26516be612238bb2477489f8
[fs/lustre-release.git] / lustre / osp / osp_dev.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, 2012, Intel, 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/osp/osp_dev.c
37  *
38  * Lustre OST Proxy Device
39  *
40  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include <obd_class.h>
50 #include <lustre_param.h>
51
52 #include "osp_internal.h"
53
54 /* Slab for OSP object allocation */
55 cfs_mem_cache_t *osp_object_kmem;
56
57 static struct lu_kmem_descr osp_caches[] = {
58         {
59                 .ckd_cache = &osp_object_kmem,
60                 .ckd_name  = "osp_obj",
61                 .ckd_size  = sizeof(struct osp_object)
62         },
63         {
64                 .ckd_cache = NULL
65         }
66 };
67
68 struct lu_object *osp_object_alloc(const struct lu_env *env,
69                                    const struct lu_object_header *hdr,
70                                    struct lu_device *d)
71 {
72         struct lu_object_header *h;
73         struct osp_object       *o;
74         struct lu_object        *l;
75
76         LASSERT(hdr == NULL);
77
78         OBD_SLAB_ALLOC_PTR_GFP(o, osp_object_kmem, CFS_ALLOC_IO);
79         if (o != NULL) {
80                 l = &o->opo_obj.do_lu;
81                 h = &o->opo_header;
82
83                 lu_object_header_init(h);
84                 dt_object_init(&o->opo_obj, h, d);
85                 lu_object_add_top(h, l);
86
87                 l->lo_ops = &osp_lu_obj_ops;
88
89                 return l;
90         } else {
91                 return NULL;
92         }
93 }
94
95 static int osp_shutdown(const struct lu_env *env, struct osp_device *d)
96 {
97         struct obd_import       *imp;
98         int                      rc = 0;
99
100         ENTRY;
101
102         imp = d->opd_obd->u.cli.cl_import;
103
104         /* Mark import deactivated now, so we don't try to reconnect if any
105          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
106          * fully deactivate the import, or that would drop all requests. */
107         cfs_spin_lock(&imp->imp_lock);
108         imp->imp_deactive = 1;
109         cfs_spin_unlock(&imp->imp_lock);
110
111         ptlrpc_deactivate_import(imp);
112
113         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
114          * delete it regardless.  (It's safe to delete an import that was
115          * never added.) */
116         (void)ptlrpc_pinger_del_import(imp);
117
118         rc = ptlrpc_disconnect_import(imp, 0);
119         if (rc)
120                 CERROR("%s: can't disconnect: rc = %d\n",
121                        d->opd_obd->obd_name, rc);
122
123         ptlrpc_invalidate_import(imp);
124
125         RETURN(rc);
126 }
127
128 static int osp_process_config(const struct lu_env *env,
129                               struct lu_device *dev, struct lustre_cfg *lcfg)
130 {
131         struct osp_device               *d = lu2osp_dev(dev);
132         struct lprocfs_static_vars       lvars = { 0 };
133         int                              rc;
134
135         ENTRY;
136
137         switch (lcfg->lcfg_command) {
138         case LCFG_CLEANUP:
139                 lu_dev_del_linkage(dev->ld_site, dev);
140                 rc = osp_shutdown(env, d);
141                 break;
142         case LCFG_PARAM:
143                 lprocfs_osp_init_vars(&lvars);
144
145                 LASSERT(d->opd_obd);
146                 rc = class_process_proc_param(PARAM_OSP, lvars.obd_vars,
147                                               lcfg, d->opd_obd);
148                 if (rc > 0)
149                         rc = 0;
150                 if (rc == -ENOSYS) {
151                         /* class_process_proc_param() haven't found matching
152                          * parameter and returned ENOSYS so that layer(s)
153                          * below could use that. But OSP is the bottom, so
154                          * just ignore it */
155                         CERROR("%s: unknown param %s\n",
156                                (char *)lustre_cfg_string(lcfg, 0),
157                                (char *)lustre_cfg_string(lcfg, 1));
158                         rc = 0;
159                 }
160                 break;
161         default:
162                 CERROR("%s: unknown command %u\n",
163                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
164                 rc = 0;
165                 break;
166         }
167
168         RETURN(rc);
169 }
170
171 static int osp_recovery_complete(const struct lu_env *env,
172                                  struct lu_device *dev)
173 {
174         struct osp_device       *osp = lu2osp_dev(dev);
175         int                      rc = 0;
176
177         ENTRY;
178         osp->opd_recovery_completed = 1;
179         RETURN(rc);
180 }
181
182 const struct lu_device_operations osp_lu_ops = {
183         .ldo_object_alloc       = osp_object_alloc,
184         .ldo_process_config     = osp_process_config,
185         .ldo_recovery_complete  = osp_recovery_complete,
186 };
187
188 static int osp_sync(const struct lu_env *env, struct dt_device *dev)
189 {
190         ENTRY;
191
192         /*
193          * XXX: wake up sync thread, command it to start flushing asap?
194          */
195
196         RETURN(0);
197 }
198
199 static const struct dt_device_operations osp_dt_ops = {
200         .dt_sync        = osp_sync,
201 };
202
203 static int osp_connect_to_osd(const struct lu_env *env, struct osp_device *m,
204                               const char *nextdev)
205 {
206         struct obd_connect_data *data = NULL;
207         struct obd_device       *obd;
208         int                      rc;
209
210         ENTRY;
211
212         LASSERT(m->opd_storage_exp == NULL);
213
214         OBD_ALLOC_PTR(data);
215         if (data == NULL)
216                 RETURN(-ENOMEM);
217
218         obd = class_name2obd(nextdev);
219         if (obd == NULL) {
220                 CERROR("%s: can't locate next device: %s\n",
221                        m->opd_obd->obd_name, nextdev);
222                 GOTO(out, rc = -ENOTCONN);
223         }
224
225         rc = obd_connect(env, &m->opd_storage_exp, obd, &obd->obd_uuid, data,
226                          NULL);
227         if (rc) {
228                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
229                        m->opd_obd->obd_name, nextdev, rc);
230                 GOTO(out, rc);
231         }
232
233         m->opd_dt_dev.dd_lu_dev.ld_site =
234                 m->opd_storage_exp->exp_obd->obd_lu_dev->ld_site;
235         LASSERT(m->opd_dt_dev.dd_lu_dev.ld_site);
236         m->opd_storage = lu2dt_dev(m->opd_storage_exp->exp_obd->obd_lu_dev);
237
238 out:
239         OBD_FREE_PTR(data);
240         RETURN(rc);
241 }
242
243 static int osp_init0(const struct lu_env *env, struct osp_device *m,
244                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
245 {
246         struct lprocfs_static_vars       lvars = { 0 };
247         struct proc_dir_entry           *osc_proc_dir;
248         struct obd_import               *imp;
249         class_uuid_t                     uuid;
250         char                            *src, *ost, *mdt, *osdname = NULL;
251         int                              rc, idx;
252
253         ENTRY;
254
255         m->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
256         if (m->opd_obd == NULL) {
257                 CERROR("Cannot find obd with name %s\n",
258                        lustre_cfg_string(cfg, 0));
259                 RETURN(-ENODEV);
260         }
261
262         /* There is no record in the MDT configuration for the local disk
263          * device, so we have to extract this from elsewhere in the profile.
264          * The only information we get at setup is from the OSC records:
265          * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
266          * Note that 1.8 generated configs are missing the -MDTxxxx part.
267          * We need to reconstruct the name of the underlying OSD from this:
268          * {fsname}-{svname}-osd, for example "lustre-MDT0000-osd".  We
269          * also need to determine the OST index from this - will be used
270          * to calculate the offset in shared lov_objids file later */
271
272         src = lustre_cfg_string(cfg, 0);
273         if (src == NULL)
274                 RETURN(-EINVAL);
275
276         ost = strstr(src, "-OST");
277         if (ost == NULL)
278                 RETURN(-EINVAL);
279
280         idx = simple_strtol(ost + 4, &mdt, 16);
281         if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
282                 CERROR("%s: invalid OST index in '%s'\n",
283                        m->opd_obd->obd_name, src);
284                 GOTO(out_fini, rc = -EINVAL);
285         }
286         m->opd_index = idx;
287
288         idx = ost - src;
289         /* check the fsname length, and after this everything else will fit */
290         if (idx > MTI_NAME_MAXLEN) {
291                 CERROR("%s: fsname too long in '%s'\n",
292                        m->opd_obd->obd_name, src);
293                 GOTO(out_fini, rc = -EINVAL);
294         }
295
296         OBD_ALLOC(osdname, MAX_OBD_NAME);
297         if (osdname == NULL)
298                 GOTO(out_fini, rc = -ENOMEM);
299
300         memcpy(osdname, src, idx); /* copy just the fsname part */
301         osdname[idx] = '\0';
302
303         mdt = strstr(mdt, "-MDT");
304         if (mdt == NULL) /* 1.8 configs don't have "-MDT0000" at the end */
305                 strcat(osdname, "-MDT0000");
306         else
307                 strcat(osdname, mdt);
308         strcat(osdname, "-osd");
309         CDEBUG(D_HA, "%s: connect to %s (%s)\n",
310                m->opd_obd->obd_name, osdname, src);
311
312         m->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
313         m->opd_dt_dev.dd_ops = &osp_dt_ops;
314         m->opd_obd->obd_lu_dev = &m->opd_dt_dev.dd_lu_dev;
315
316         rc = osp_connect_to_osd(env, m, osdname);
317         if (rc)
318                 GOTO(out_fini, rc);
319
320         rc = ptlrpcd_addref();
321         if (rc)
322                 GOTO(out_disconnect, rc);
323
324         rc = client_obd_setup(m->opd_obd, cfg);
325         if (rc) {
326                 CERROR("%s: can't setup obd: %d\n", m->opd_obd->obd_name, rc);
327                 GOTO(out_ref, rc);
328         }
329
330         lprocfs_osp_init_vars(&lvars);
331         if (lprocfs_obd_setup(m->opd_obd, lvars.obd_vars) == 0)
332                 ptlrpc_lprocfs_register_obd(m->opd_obd);
333
334         /* for compatibility we link old procfs's OSC entries to osp ones */
335         osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
336         if (osc_proc_dir) {
337                 cfs_proc_dir_entry_t    *symlink = NULL;
338                 char                    *name;
339
340                 OBD_ALLOC(name, strlen(m->opd_obd->obd_name) + 1);
341                 if (name == NULL)
342                         GOTO(out, rc = -ENOMEM);
343
344                 strcpy(name, m->opd_obd->obd_name);
345                 if (strstr(name, "osc"))
346                         symlink = lprocfs_add_symlink(name, osc_proc_dir,
347                                                       "../osp/%s",
348                                                       m->opd_obd->obd_name);
349                 OBD_FREE(name, strlen(m->opd_obd->obd_name) + 1);
350                 m->opd_symlink = symlink;
351         }
352
353         /*
354          * Initiate connect to OST
355          */
356         ll_generate_random_uuid(uuid);
357         class_uuid_unparse(uuid, &m->opd_cluuid);
358
359         imp = m->opd_obd->u.cli.cl_import;
360
361         rc = ptlrpc_init_import(imp);
362         if (rc)
363                 GOTO(out, rc);
364         if (osdname)
365                 OBD_FREE(osdname, MAX_OBD_NAME);
366         RETURN(0);
367
368 out:
369         ptlrpc_lprocfs_unregister_obd(m->opd_obd);
370         lprocfs_obd_cleanup(m->opd_obd);
371         class_destroy_import(m->opd_obd->u.cli.cl_import);
372         client_obd_cleanup(m->opd_obd);
373 out_ref:
374         ptlrpcd_decref();
375 out_disconnect:
376         obd_disconnect(m->opd_storage_exp);
377 out_fini:
378         if (osdname)
379                 OBD_FREE(osdname, MAX_OBD_NAME);
380         RETURN(rc);
381 }
382
383 static struct lu_device *osp_device_free(const struct lu_env *env,
384                                          struct lu_device *lu)
385 {
386         struct osp_device *m = lu2osp_dev(lu);
387
388         ENTRY;
389
390         dt_device_fini(&m->opd_dt_dev);
391         OBD_FREE_PTR(m);
392         RETURN(NULL);
393 }
394
395 static struct lu_device *osp_device_alloc(const struct lu_env *env,
396                                           struct lu_device_type *t,
397                                           struct lustre_cfg *lcfg)
398 {
399         struct osp_device *m;
400         struct lu_device  *l;
401
402         OBD_ALLOC_PTR(m);
403         if (m == NULL) {
404                 l = ERR_PTR(-ENOMEM);
405         } else {
406                 int rc;
407
408                 l = osp2lu_dev(m);
409                 dt_device_init(&m->opd_dt_dev, t);
410                 rc = osp_init0(env, m, t, lcfg);
411                 if (rc != 0) {
412                         osp_device_free(env, l);
413                         l = ERR_PTR(rc);
414                 }
415         }
416         return l;
417 }
418
419 static struct lu_device *osp_device_fini(const struct lu_env *env,
420                                          struct lu_device *d)
421 {
422         struct osp_device *m = lu2osp_dev(d);
423         struct obd_import *imp;
424         int                rc;
425
426         ENTRY;
427
428         LASSERT(m->opd_storage_exp);
429         obd_disconnect(m->opd_storage_exp);
430
431         imp = m->opd_obd->u.cli.cl_import;
432
433         if (imp->imp_rq_pool) {
434                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
435                 imp->imp_rq_pool = NULL;
436         }
437
438         obd_cleanup_client_import(m->opd_obd);
439
440         if (m->opd_symlink)
441                 lprocfs_remove(&m->opd_symlink);
442
443         LASSERT(m->opd_obd);
444         ptlrpc_lprocfs_unregister_obd(m->opd_obd);
445         lprocfs_obd_cleanup(m->opd_obd);
446
447         rc = client_obd_cleanup(m->opd_obd);
448         LASSERTF(rc == 0, "error %d\n", rc);
449
450         ptlrpcd_decref();
451
452         RETURN(NULL);
453 }
454
455 static int osp_reconnect(const struct lu_env *env,
456                          struct obd_export *exp, struct obd_device *obd,
457                          struct obd_uuid *cluuid,
458                          struct obd_connect_data *data,
459                          void *localdata)
460 {
461         return 0;
462 }
463
464 /*
465  * we use exports to track all LOD users
466  */
467 static int osp_obd_connect(const struct lu_env *env, struct obd_export **exp,
468                            struct obd_device *obd, struct obd_uuid *cluuid,
469                            struct obd_connect_data *data, void *localdata)
470 {
471         struct osp_device       *osp = lu2osp_dev(obd->obd_lu_dev);
472         struct obd_connect_data *ocd;
473         struct obd_import       *imp;
474         struct lustre_handle     conn;
475         int                      rc;
476
477         ENTRY;
478
479         CDEBUG(D_CONFIG, "connect #%d\n", osp->opd_connects);
480
481         rc = class_connect(&conn, obd, cluuid);
482         if (rc)
483                 RETURN(rc);
484
485         *exp = class_conn2export(&conn);
486
487         /* Why should there ever be more than 1 connect? */
488         osp->opd_connects++;
489         LASSERT(osp->opd_connects == 1);
490
491         imp = osp->opd_obd->u.cli.cl_import;
492         imp->imp_dlm_handle = conn;
493
494         ocd = &imp->imp_connect_data;
495         ocd->ocd_connect_flags = OBD_CONNECT_AT |
496                                  OBD_CONNECT_FULL20 |
497                                  OBD_CONNECT_INDEX |
498 #ifdef HAVE_LRU_RESIZE_SUPPORT
499                                  OBD_CONNECT_LRU_RESIZE |
500 #endif
501                                  OBD_CONNECT_MDS |
502                                  OBD_CONNECT_OSS_CAPA |
503                                  OBD_CONNECT_REQPORTAL |
504                                  OBD_CONNECT_SKIP_ORPHAN |
505                                  OBD_CONNECT_VERSION;
506         ocd->ocd_version = LUSTRE_VERSION_CODE;
507         LASSERT(data->ocd_connect_flags & OBD_CONNECT_INDEX);
508         ocd->ocd_index = data->ocd_index;
509         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
510
511         rc = ptlrpc_connect_import(imp);
512         if (rc) {
513                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
514                 GOTO(out, rc);
515         }
516
517         ptlrpc_pinger_add_import(imp);
518
519 out:
520         RETURN(rc);
521 }
522
523 /*
524  * once last export (we don't count self-export) disappeared
525  * osp can be released
526  */
527 static int osp_obd_disconnect(struct obd_export *exp)
528 {
529         struct obd_device *obd = exp->exp_obd;
530         struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
531         int                rc;
532
533         ENTRY;
534
535         /* Only disconnect the underlying layers on the final disconnect. */
536         LASSERT(osp->opd_connects == 1);
537         osp->opd_connects--;
538
539         rc = class_disconnect(exp);
540
541         /* destroy the device */
542         if (rc == 0)
543                 class_manual_cleanup(obd);
544
545         RETURN(rc);
546 }
547
548 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
549 LU_KEY_INIT_FINI(osp, struct osp_thread_info);
550 static void osp_key_exit(const struct lu_context *ctx,
551                          struct lu_context_key *key, void *data)
552 {
553         struct osp_thread_info *info = data;
554
555         info->osi_attr.la_valid = 0;
556 }
557
558 struct lu_context_key osp_thread_key = {
559         .lct_tags = LCT_MD_THREAD,
560         .lct_init = osp_key_init,
561         .lct_fini = osp_key_fini,
562         .lct_exit = osp_key_exit
563 };
564
565 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
566 LU_KEY_INIT_FINI(osp_txn, struct osp_txn_info);
567
568 struct lu_context_key osp_txn_key = {
569         .lct_tags = LCT_OSP_THREAD,
570         .lct_init = osp_txn_key_init,
571         .lct_fini = osp_txn_key_fini
572 };
573 LU_TYPE_INIT_FINI(osp, &osp_thread_key, &osp_txn_key);
574
575 static struct lu_device_type_operations osp_device_type_ops = {
576         .ldto_init           = osp_type_init,
577         .ldto_fini           = osp_type_fini,
578
579         .ldto_start          = osp_type_start,
580         .ldto_stop           = osp_type_stop,
581
582         .ldto_device_alloc   = osp_device_alloc,
583         .ldto_device_free    = osp_device_free,
584
585         .ldto_device_fini    = osp_device_fini
586 };
587
588 static struct lu_device_type osp_device_type = {
589         .ldt_tags     = LU_DEVICE_DT,
590         .ldt_name     = LUSTRE_OSP_NAME,
591         .ldt_ops      = &osp_device_type_ops,
592         .ldt_ctx_tags = LCT_MD_THREAD
593 };
594
595 static struct obd_ops osp_obd_device_ops = {
596         .o_owner        = THIS_MODULE,
597         .o_add_conn     = client_import_add_conn,
598         .o_del_conn     = client_import_del_conn,
599         .o_reconnect    = osp_reconnect,
600         .o_connect      = osp_obd_connect,
601         .o_disconnect   = osp_obd_disconnect,
602 };
603
604 static int __init osp_mod_init(void)
605 {
606         struct lprocfs_static_vars       lvars;
607         cfs_proc_dir_entry_t            *osc_proc_dir;
608         int                              rc;
609
610         rc = lu_kmem_init(osp_caches);
611         if (rc)
612                 return rc;
613
614         lprocfs_osp_init_vars(&lvars);
615
616         rc = class_register_type(&osp_obd_device_ops, NULL, lvars.module_vars,
617                                  LUSTRE_OSP_NAME, &osp_device_type);
618
619         /* create "osc" entry in procfs for compatibility purposes */
620         if (rc != 0) {
621                 lu_kmem_fini(osp_caches);
622                 return rc;
623         }
624
625         osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
626         if (osc_proc_dir == NULL) {
627                 osc_proc_dir = lprocfs_register("osc", proc_lustre_root, NULL,
628                                                 NULL);
629                 if (IS_ERR(osc_proc_dir))
630                         CERROR("osp: can't create compat entry \"osc\": %d\n",
631                                (int) PTR_ERR(osc_proc_dir));
632         }
633         return rc;
634 }
635
636 static void __exit osp_mod_exit(void)
637 {
638         lprocfs_try_remove_proc_entry("osc", proc_lustre_root);
639
640         class_unregister_type(LUSTRE_OSP_NAME);
641         lu_kmem_fini(osp_caches);
642 }
643
644 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
645 MODULE_DESCRIPTION("Lustre OST Proxy Device ("LUSTRE_OSP_NAME")");
646 MODULE_LICENSE("GPL");
647
648 cfs_module(osp, LUSTRE_VERSION_STRING, osp_mod_init, osp_mod_exit);
649