Whamcloud - gitweb
LU-1303 mds: integration lod/osp into the stack
[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 /* Update opd_last_used_id along with checking for gap in objid sequence */
96 void osp_update_last_id(struct osp_device *d, obd_id objid)
97 {
98         /*
99          * we might have lost precreated objects due to VBR and precreate
100          * orphans, the gap in objid can be calculated properly only here
101          */
102         if (objid > le64_to_cpu(d->opd_last_used_id)) {
103                 if (objid - le64_to_cpu(d->opd_last_used_id) > 1) {
104                         d->opd_gap_start = le64_to_cpu(d->opd_last_used_id) + 1;
105                         d->opd_gap_count = objid - d->opd_gap_start;
106                         CDEBUG(D_HA, "Gap in objids: %d, start = %llu\n",
107                                d->opd_gap_count, d->opd_gap_start);
108                 }
109                 d->opd_last_used_id = cpu_to_le64(objid);
110         }
111 }
112
113 static int osp_last_used_init(const struct lu_env *env, struct osp_device *m)
114 {
115         struct osp_thread_info  *osi = osp_env_info(env);
116         struct dt_object_format  dof = { 0 };
117         struct dt_object        *o;
118         int                      rc;
119
120         ENTRY;
121
122         osi->osi_attr.la_valid = LA_MODE;
123         osi->osi_attr.la_mode = S_IFREG | 0644;
124         lu_local_obj_fid(&osi->osi_fid, MDD_LOV_OBJ_OID);
125         dof.dof_type = DFT_REGULAR;
126         o = dt_find_or_create(env, m->opd_storage, &osi->osi_fid, &dof,
127                               &osi->osi_attr);
128         if (IS_ERR(o))
129                 RETURN(PTR_ERR(o));
130
131         rc = dt_attr_get(env, o, &osi->osi_attr, NULL);
132         if (rc)
133                 GOTO(out, rc);
134
135         /* object will be released in device cleanup path */
136         m->opd_last_used_file = o;
137
138         if (osi->osi_attr.la_size >= sizeof(osi->osi_id) *
139                                      (m->opd_index + 1)) {
140                 osp_objid_buf_prep(osi, m, m->opd_index);
141                 rc = dt_record_read(env, o, &osi->osi_lb, &osi->osi_off);
142                 if (rc != 0)
143                         GOTO(out, rc);
144         } else {
145                 /* reset value to 0, just to make sure and change file's size */
146                 struct thandle *th;
147
148                 m->opd_last_used_id = 0;
149                 osp_objid_buf_prep(osi, m, m->opd_index);
150
151                 th = dt_trans_create(env, m->opd_storage);
152                 if (IS_ERR(th))
153                         GOTO(out, rc = PTR_ERR(th));
154
155                 rc = dt_declare_record_write(env, m->opd_last_used_file,
156                                              osi->osi_lb.lb_len, osi->osi_off,
157                                              th);
158                 if (rc) {
159                         dt_trans_stop(env, m->opd_storage, th);
160                         GOTO(out, rc);
161                 }
162
163                 rc = dt_trans_start_local(env, m->opd_storage, th);
164                 if (rc) {
165                         dt_trans_stop(env, m->opd_storage, th);
166                         GOTO(out, rc);
167                 }
168
169                 rc = dt_record_write(env, m->opd_last_used_file, &osi->osi_lb,
170                                      &osi->osi_off, th);
171                 dt_trans_stop(env, m->opd_storage, th);
172                 if (rc)
173                         GOTO(out, rc);
174         }
175         RETURN(0);
176 out:
177         CERROR("%s: can't initialize lov_objid: %d\n",
178                m->opd_obd->obd_name, rc);
179         lu_object_put(env, &o->do_lu);
180         m->opd_last_used_file = NULL;
181         return rc;
182 }
183
184 static void osp_last_used_fini(const struct lu_env *env, struct osp_device *d)
185 {
186         lu_object_put(env, &d->opd_last_used_file->do_lu);
187         d->opd_last_used_file = NULL;
188 }
189
190 static int osp_shutdown(const struct lu_env *env, struct osp_device *d)
191 {
192         struct obd_import       *imp;
193         int                      rc = 0;
194         ENTRY;
195
196         /* release last_used file */
197         osp_last_used_fini(env, d);
198
199         imp = d->opd_obd->u.cli.cl_import;
200
201         /* Mark import deactivated now, so we don't try to reconnect if any
202          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
203          * fully deactivate the import, or that would drop all requests. */
204         cfs_spin_lock(&imp->imp_lock);
205         imp->imp_deactive = 1;
206         cfs_spin_unlock(&imp->imp_lock);
207
208         ptlrpc_deactivate_import(imp);
209
210         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
211          * delete it regardless.  (It's safe to delete an import that was
212          * never added.) */
213         (void)ptlrpc_pinger_del_import(imp);
214
215         rc = ptlrpc_disconnect_import(imp, 0);
216         if (rc)
217                 CERROR("%s: can't disconnect: rc = %d\n",
218                        d->opd_obd->obd_name, rc);
219
220         ptlrpc_invalidate_import(imp);
221
222         /* stop precreate thread */
223         osp_precreate_fini(d);
224
225         /* stop sync thread */
226         osp_sync_fini(d);
227
228         RETURN(rc);
229 }
230
231 static int osp_process_config(const struct lu_env *env,
232                               struct lu_device *dev, struct lustre_cfg *lcfg)
233 {
234         struct osp_device               *d = lu2osp_dev(dev);
235         struct lprocfs_static_vars       lvars = { 0 };
236         int                              rc;
237
238         ENTRY;
239
240         switch (lcfg->lcfg_command) {
241         case LCFG_CLEANUP:
242                 lu_dev_del_linkage(dev->ld_site, dev);
243                 rc = osp_shutdown(env, d);
244                 break;
245         case LCFG_PARAM:
246                 lprocfs_osp_init_vars(&lvars);
247
248                 LASSERT(d->opd_obd);
249                 rc = class_process_proc_param(PARAM_OSC, lvars.obd_vars,
250                                               lcfg, d->opd_obd);
251                 if (rc > 0)
252                         rc = 0;
253                 if (rc == -ENOSYS) {
254                         /* class_process_proc_param() haven't found matching
255                          * parameter and returned ENOSYS so that layer(s)
256                          * below could use that. But OSP is the bottom, so
257                          * just ignore it */
258                         CERROR("%s: unknown param %s\n",
259                                (char *)lustre_cfg_string(lcfg, 0),
260                                (char *)lustre_cfg_string(lcfg, 1));
261                         rc = 0;
262                 }
263                 break;
264         default:
265                 CERROR("%s: unknown command %u\n",
266                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
267                 rc = 0;
268                 break;
269         }
270
271         RETURN(rc);
272 }
273
274 static int osp_recovery_complete(const struct lu_env *env,
275                                  struct lu_device *dev)
276 {
277         struct osp_device       *osp = lu2osp_dev(dev);
278         int                      rc = 0;
279
280         ENTRY;
281         osp->opd_recovery_completed = 1;
282         cfs_waitq_signal(&osp->opd_pre_waitq);
283         RETURN(rc);
284 }
285
286 const struct lu_device_operations osp_lu_ops = {
287         .ldo_object_alloc       = osp_object_alloc,
288         .ldo_process_config     = osp_process_config,
289         .ldo_recovery_complete  = osp_recovery_complete,
290 };
291
292 /**
293  * provides with statfs from corresponded OST
294  *
295  */
296 static int osp_statfs(const struct lu_env *env, struct dt_device *dev,
297                       struct obd_statfs *sfs)
298 {
299         struct osp_device *d = dt2osp_dev(dev);
300
301         ENTRY;
302
303         if (unlikely(d->opd_imp_active == 0)) {
304                 /*
305                  * in case of inactive OST we return nulls
306                  * so that caller can understand this device
307                  * is unusable for new objects
308                  *
309                  * XXX: shouldn't we take normal statfs and fill
310                  * just few specific fields with zeroes?
311                  */
312                 memset(sfs, 0, sizeof(*sfs));
313                 sfs->os_bsize = 4096;
314                 RETURN(0);
315         }
316
317         /* return recently updated data */
318         *sfs = d->opd_statfs;
319
320         /*
321          * layer above osp (usually lod) can use ffree to estimate
322          * how many objects are available for immediate creation
323          */
324         cfs_spin_lock(&d->opd_pre_lock);
325         sfs->os_ffree = d->opd_pre_last_created - d->opd_pre_next;
326         cfs_spin_unlock(&d->opd_pre_lock);
327
328         CDEBUG(D_OTHER, "%s: "LPU64" blocks, "LPU64" free, "LPU64" avail, "
329                LPU64" files, "LPU64" free files\n", d->opd_obd->obd_name,
330                sfs->os_blocks, sfs->os_bfree, sfs->os_bavail,
331                sfs->os_files, sfs->os_ffree);
332         RETURN(0);
333 }
334
335 static int osp_sync(const struct lu_env *env, struct dt_device *dev)
336 {
337         ENTRY;
338
339         /*
340          * XXX: wake up sync thread, command it to start flushing asap?
341          */
342
343         RETURN(0);
344 }
345
346 static const struct dt_device_operations osp_dt_ops = {
347         .dt_statfs      = osp_statfs,
348         .dt_sync        = osp_sync,
349 };
350
351 static int osp_connect_to_osd(const struct lu_env *env, struct osp_device *m,
352                               const char *nextdev)
353 {
354         struct obd_connect_data *data = NULL;
355         struct obd_device       *obd;
356         int                      rc;
357
358         ENTRY;
359
360         LASSERT(m->opd_storage_exp == NULL);
361
362         OBD_ALLOC_PTR(data);
363         if (data == NULL)
364                 RETURN(-ENOMEM);
365
366         obd = class_name2obd(nextdev);
367         if (obd == NULL) {
368                 CERROR("%s: can't locate next device: %s\n",
369                        m->opd_obd->obd_name, nextdev);
370                 GOTO(out, rc = -ENOTCONN);
371         }
372
373         rc = obd_connect(env, &m->opd_storage_exp, obd, &obd->obd_uuid, data,
374                          NULL);
375         if (rc) {
376                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
377                        m->opd_obd->obd_name, nextdev, rc);
378                 GOTO(out, rc);
379         }
380
381         m->opd_dt_dev.dd_lu_dev.ld_site =
382                 m->opd_storage_exp->exp_obd->obd_lu_dev->ld_site;
383         LASSERT(m->opd_dt_dev.dd_lu_dev.ld_site);
384         m->opd_storage = lu2dt_dev(m->opd_storage_exp->exp_obd->obd_lu_dev);
385
386 out:
387         OBD_FREE_PTR(data);
388         RETURN(rc);
389 }
390
391 static int osp_init0(const struct lu_env *env, struct osp_device *m,
392                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
393 {
394         struct lprocfs_static_vars       lvars = { 0 };
395         struct proc_dir_entry           *osc_proc_dir;
396         struct obd_import               *imp;
397         class_uuid_t                     uuid;
398         char                            *src, *ost, *mdt, *osdname = NULL;
399         int                              rc, idx;
400
401         ENTRY;
402
403         m->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
404         if (m->opd_obd == NULL) {
405                 CERROR("Cannot find obd with name %s\n",
406                        lustre_cfg_string(cfg, 0));
407                 RETURN(-ENODEV);
408         }
409
410         /* There is no record in the MDT configuration for the local disk
411          * device, so we have to extract this from elsewhere in the profile.
412          * The only information we get at setup is from the OSC records:
413          * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
414          * Note that 1.8 generated configs are missing the -MDTxxxx part.
415          * We need to reconstruct the name of the underlying OSD from this:
416          * {fsname}-{svname}-osd, for example "lustre-MDT0000-osd".  We
417          * also need to determine the OST index from this - will be used
418          * to calculate the offset in shared lov_objids file later */
419
420         src = lustre_cfg_string(cfg, 0);
421         if (src == NULL)
422                 RETURN(-EINVAL);
423
424         ost = strstr(src, "-OST");
425         if (ost == NULL)
426                 RETURN(-EINVAL);
427
428         idx = simple_strtol(ost + 4, &mdt, 16);
429         if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
430                 CERROR("%s: invalid OST index in '%s'\n",
431                        m->opd_obd->obd_name, src);
432                 GOTO(out_fini, rc = -EINVAL);
433         }
434         m->opd_index = idx;
435
436         idx = ost - src;
437         /* check the fsname length, and after this everything else will fit */
438         if (idx > MTI_NAME_MAXLEN) {
439                 CERROR("%s: fsname too long in '%s'\n",
440                        m->opd_obd->obd_name, src);
441                 GOTO(out_fini, rc = -EINVAL);
442         }
443
444         OBD_ALLOC(osdname, MAX_OBD_NAME);
445         if (osdname == NULL)
446                 GOTO(out_fini, rc = -ENOMEM);
447
448         memcpy(osdname, src, idx); /* copy just the fsname part */
449         osdname[idx] = '\0';
450
451         mdt = strstr(mdt, "-MDT");
452         if (mdt == NULL) /* 1.8 configs don't have "-MDT0000" at the end */
453                 strcat(osdname, "-MDT0000");
454         else
455                 strcat(osdname, mdt);
456         strcat(osdname, "-osd");
457         CDEBUG(D_HA, "%s: connect to %s (%s)\n",
458                m->opd_obd->obd_name, osdname, src);
459
460         m->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
461         m->opd_dt_dev.dd_ops = &osp_dt_ops;
462         m->opd_obd->obd_lu_dev = &m->opd_dt_dev.dd_lu_dev;
463
464         rc = osp_connect_to_osd(env, m, osdname);
465         if (rc)
466                 GOTO(out_fini, rc);
467
468         rc = ptlrpcd_addref();
469         if (rc)
470                 GOTO(out_disconnect, rc);
471
472         rc = client_obd_setup(m->opd_obd, cfg);
473         if (rc) {
474                 CERROR("%s: can't setup obd: %d\n", m->opd_obd->obd_name, rc);
475                 GOTO(out_ref, rc);
476         }
477
478         lprocfs_osp_init_vars(&lvars);
479         if (lprocfs_obd_setup(m->opd_obd, lvars.obd_vars) == 0)
480                 ptlrpc_lprocfs_register_obd(m->opd_obd);
481
482         /* for compatibility we link old procfs's OSC entries to osp ones */
483         osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
484         if (osc_proc_dir) {
485                 cfs_proc_dir_entry_t    *symlink = NULL;
486                 char                    *name;
487
488                 OBD_ALLOC(name, strlen(m->opd_obd->obd_name) + 1);
489                 if (name == NULL)
490                         GOTO(out, rc = -ENOMEM);
491
492                 strcpy(name, m->opd_obd->obd_name);
493                 if (strstr(name, "osc"))
494                         symlink = lprocfs_add_symlink(name, osc_proc_dir,
495                                                       "../osp/%s",
496                                                       m->opd_obd->obd_name);
497                 OBD_FREE(name, strlen(m->opd_obd->obd_name) + 1);
498                 m->opd_symlink = symlink;
499         }
500
501         /*
502          * Initialize last id from the storage - will be used in orphan cleanup
503          */
504         rc = osp_last_used_init(env, m);
505         if (rc)
506                 GOTO(out_proc, rc);
507
508         /*
509          * Initialize precreation thread, it handles new connections as well
510          */
511         rc = osp_init_precreate(m);
512         if (rc)
513                 GOTO(out_last_used, rc);
514
515         /*
516          * Initialize synhronization mechanism taking care of propogating
517          * changes to OST in near transactional manner
518          */
519         rc = osp_sync_init(env, m);
520         if (rc)
521                 GOTO(out_precreat, rc);
522
523         /*
524          * Initiate connect to OST
525          */
526         ll_generate_random_uuid(uuid);
527         class_uuid_unparse(uuid, &m->opd_cluuid);
528
529         imp = m->opd_obd->u.cli.cl_import;
530
531         rc = ptlrpc_init_import(imp);
532         if (rc)
533                 GOTO(out, rc);
534         if (osdname)
535                 OBD_FREE(osdname, MAX_OBD_NAME);
536         RETURN(0);
537
538 out:
539         /* stop sync thread */
540         osp_sync_fini(m);
541 out_precreat:
542         /* stop precreate thread */
543         osp_precreate_fini(m);
544 out_last_used:
545         osp_last_used_fini(env, m);
546 out_proc:
547         ptlrpc_lprocfs_unregister_obd(m->opd_obd);
548         lprocfs_obd_cleanup(m->opd_obd);
549         class_destroy_import(m->opd_obd->u.cli.cl_import);
550         client_obd_cleanup(m->opd_obd);
551 out_ref:
552         ptlrpcd_decref();
553 out_disconnect:
554         obd_disconnect(m->opd_storage_exp);
555 out_fini:
556         if (osdname)
557                 OBD_FREE(osdname, MAX_OBD_NAME);
558         RETURN(rc);
559 }
560
561 static struct lu_device *osp_device_free(const struct lu_env *env,
562                                          struct lu_device *lu)
563 {
564         struct osp_device *m = lu2osp_dev(lu);
565
566         ENTRY;
567
568         dt_device_fini(&m->opd_dt_dev);
569         OBD_FREE_PTR(m);
570         RETURN(NULL);
571 }
572
573 static struct lu_device *osp_device_alloc(const struct lu_env *env,
574                                           struct lu_device_type *t,
575                                           struct lustre_cfg *lcfg)
576 {
577         struct osp_device *m;
578         struct lu_device  *l;
579
580         OBD_ALLOC_PTR(m);
581         if (m == NULL) {
582                 l = ERR_PTR(-ENOMEM);
583         } else {
584                 int rc;
585
586                 l = osp2lu_dev(m);
587                 dt_device_init(&m->opd_dt_dev, t);
588                 rc = osp_init0(env, m, t, lcfg);
589                 if (rc != 0) {
590                         osp_device_free(env, l);
591                         l = ERR_PTR(rc);
592                 }
593         }
594         return l;
595 }
596
597 static struct lu_device *osp_device_fini(const struct lu_env *env,
598                                          struct lu_device *d)
599 {
600         struct osp_device *m = lu2osp_dev(d);
601         struct obd_import *imp;
602         int                rc;
603
604         ENTRY;
605
606         LASSERT(m->opd_storage_exp);
607         obd_disconnect(m->opd_storage_exp);
608
609         imp = m->opd_obd->u.cli.cl_import;
610
611         if (imp->imp_rq_pool) {
612                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
613                 imp->imp_rq_pool = NULL;
614         }
615
616         obd_cleanup_client_import(m->opd_obd);
617
618         if (m->opd_symlink)
619                 lprocfs_remove(&m->opd_symlink);
620
621         LASSERT(m->opd_obd);
622         ptlrpc_lprocfs_unregister_obd(m->opd_obd);
623         lprocfs_obd_cleanup(m->opd_obd);
624
625         rc = client_obd_cleanup(m->opd_obd);
626         LASSERTF(rc == 0, "error %d\n", rc);
627
628         ptlrpcd_decref();
629
630         RETURN(NULL);
631 }
632
633 static int osp_reconnect(const struct lu_env *env,
634                          struct obd_export *exp, struct obd_device *obd,
635                          struct obd_uuid *cluuid,
636                          struct obd_connect_data *data,
637                          void *localdata)
638 {
639         return 0;
640 }
641
642 /*
643  * we use exports to track all LOD users
644  */
645 static int osp_obd_connect(const struct lu_env *env, struct obd_export **exp,
646                            struct obd_device *obd, struct obd_uuid *cluuid,
647                            struct obd_connect_data *data, void *localdata)
648 {
649         struct osp_device       *osp = lu2osp_dev(obd->obd_lu_dev);
650         struct obd_connect_data *ocd;
651         struct obd_import       *imp;
652         struct lustre_handle     conn;
653         int                      rc;
654
655         ENTRY;
656
657         CDEBUG(D_CONFIG, "connect #%d\n", osp->opd_connects);
658
659         rc = class_connect(&conn, obd, cluuid);
660         if (rc)
661                 RETURN(rc);
662
663         *exp = class_conn2export(&conn);
664
665         /* Why should there ever be more than 1 connect? */
666         osp->opd_connects++;
667         LASSERT(osp->opd_connects == 1);
668
669         imp = osp->opd_obd->u.cli.cl_import;
670         imp->imp_dlm_handle = conn;
671
672         ocd = &imp->imp_connect_data;
673         ocd->ocd_connect_flags = OBD_CONNECT_AT |
674                                  OBD_CONNECT_FULL20 |
675                                  OBD_CONNECT_INDEX |
676 #ifdef HAVE_LRU_RESIZE_SUPPORT
677                                  OBD_CONNECT_LRU_RESIZE |
678 #endif
679                                  OBD_CONNECT_MDS |
680                                  OBD_CONNECT_OSS_CAPA |
681                                  OBD_CONNECT_REQPORTAL |
682                                  OBD_CONNECT_SKIP_ORPHAN |
683                                  OBD_CONNECT_VERSION;
684         ocd->ocd_version = LUSTRE_VERSION_CODE;
685         LASSERT(data->ocd_connect_flags & OBD_CONNECT_INDEX);
686         ocd->ocd_index = data->ocd_index;
687         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
688
689         rc = ptlrpc_connect_import(imp);
690         if (rc) {
691                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
692                 GOTO(out, rc);
693         }
694
695         ptlrpc_pinger_add_import(imp);
696
697 out:
698         RETURN(rc);
699 }
700
701 /*
702  * once last export (we don't count self-export) disappeared
703  * osp can be released
704  */
705 static int osp_obd_disconnect(struct obd_export *exp)
706 {
707         struct obd_device *obd = exp->exp_obd;
708         struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
709         int                rc;
710
711         ENTRY;
712
713         /* Only disconnect the underlying layers on the final disconnect. */
714         LASSERT(osp->opd_connects == 1);
715         osp->opd_connects--;
716
717         rc = class_disconnect(exp);
718
719         /* destroy the device */
720         if (rc == 0)
721                 class_manual_cleanup(obd);
722
723         RETURN(rc);
724 }
725
726 /*
727  * lprocfs helpers still use OBD API, let's keep obd_statfs() support
728  */
729 static int osp_obd_statfs(const struct lu_env *env, struct obd_export *exp,
730                           struct obd_statfs *osfs, __u64 max_age, __u32 flags)
731 {
732         struct obd_statfs       *msfs;
733         struct ptlrpc_request   *req;
734         struct obd_import       *imp = NULL;
735         int                      rc;
736
737         ENTRY;
738
739         /* Since the request might also come from lprocfs, so we need
740          * sync this with client_disconnect_export Bug15684 */
741         cfs_down_read(&exp->exp_obd->u.cli.cl_sem);
742         if (exp->exp_obd->u.cli.cl_import)
743                 imp = class_import_get(exp->exp_obd->u.cli.cl_import);
744         cfs_up_read(&exp->exp_obd->u.cli.cl_sem);
745         if (!imp)
746                 RETURN(-ENODEV);
747
748         /* We could possibly pass max_age in the request (as an absolute
749          * timestamp or a "seconds.usec ago") so the target can avoid doing
750          * extra calls into the filesystem if that isn't necessary (e.g.
751          * during mount that would help a bit).  Having relative timestamps
752          * is not so great if request processing is slow, while absolute
753          * timestamps are not ideal because they need time synchronization. */
754         req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
755
756         class_import_put(imp);
757
758         if (req == NULL)
759                 RETURN(-ENOMEM);
760
761         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
762         if (rc) {
763                 ptlrpc_request_free(req);
764                 RETURN(rc);
765         }
766         ptlrpc_request_set_replen(req);
767         req->rq_request_portal = OST_CREATE_PORTAL;
768         ptlrpc_at_set_req_timeout(req);
769
770         if (flags & OBD_STATFS_NODELAY) {
771                 /* procfs requests not want stat in wait for avoid deadlock */
772                 req->rq_no_resend = 1;
773                 req->rq_no_delay = 1;
774         }
775
776         rc = ptlrpc_queue_wait(req);
777         if (rc)
778                 GOTO(out, rc);
779
780         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
781         if (msfs == NULL)
782                 GOTO(out, rc = -EPROTO);
783
784         *osfs = *msfs;
785
786         EXIT;
787 out:
788         ptlrpc_req_finished(req);
789         return rc;
790 }
791
792 static int osp_import_event(struct obd_device *obd, struct obd_import *imp,
793                             enum obd_import_event event)
794 {
795         struct osp_device *d = lu2osp_dev(obd->obd_lu_dev);
796
797         switch (event) {
798         case IMP_EVENT_DISCON:
799                 d->opd_got_disconnected = 1;
800                 d->opd_imp_connected = 0;
801                 osp_pre_update_status(d, -ENODEV);
802                 cfs_waitq_signal(&d->opd_pre_waitq);
803                 CDEBUG(D_HA, "got disconnected\n");
804                 break;
805         case IMP_EVENT_INACTIVE:
806                 d->opd_imp_active = 0;
807                 osp_pre_update_status(d, -ENODEV);
808                 cfs_waitq_signal(&d->opd_pre_waitq);
809                 CDEBUG(D_HA, "got inactive\n");
810                 break;
811         case IMP_EVENT_ACTIVE:
812                 d->opd_imp_active = 1;
813                 if (d->opd_got_disconnected)
814                         d->opd_new_connection = 1;
815                 d->opd_imp_connected = 1;
816                 d->opd_imp_seen_connected = 1;
817                 cfs_waitq_signal(&d->opd_pre_waitq);
818                 __osp_sync_check_for_work(d);
819                 CDEBUG(D_HA, "got connected\n");
820                 break;
821         default:
822                 CERROR("%s: unsupported import event: %#x\n",
823                        obd->obd_name, event);
824         }
825         return 0;
826 }
827
828 static int osp_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
829                          void *karg, void *uarg)
830 {
831         struct obd_device       *obd = exp->exp_obd;
832         struct osp_device       *d;
833         struct obd_ioctl_data   *data = karg;
834         int                      rc = 0;
835
836         ENTRY;
837
838         LASSERT(obd->obd_lu_dev);
839         d = lu2osp_dev(obd->obd_lu_dev);
840         LASSERT(d->opd_dt_dev.dd_ops == &osp_dt_ops);
841
842         if (!cfs_try_module_get(THIS_MODULE)) {
843                 CERROR("%s: can't get module. Is it alive?", obd->obd_name);
844                 return -EINVAL;
845         }
846
847         switch (cmd) {
848         case OBD_IOC_CLIENT_RECOVER:
849                 rc = ptlrpc_recover_import(obd->u.cli.cl_import,
850                                            data->ioc_inlbuf1, 0);
851                 if (rc > 0)
852                         rc = 0;
853                 break;
854         case IOC_OSC_SET_ACTIVE:
855                 rc = ptlrpc_set_import_active(obd->u.cli.cl_import,
856                                               data->ioc_offset);
857                 break;
858         case OBD_IOC_PING_TARGET:
859                 rc = ptlrpc_obd_ping(obd);
860                 break;
861         default:
862                 CERROR("%s: unrecognized ioctl %#x by %s\n", obd->obd_name,
863                        cmd, cfs_curproc_comm());
864                 rc = -ENOTTY;
865         }
866         cfs_module_put(THIS_MODULE);
867         return rc;
868 }
869
870 static int osp_obd_health_check(const struct lu_env *env,
871                                 struct obd_device *obd)
872 {
873         struct osp_device *d = lu2osp_dev(obd->obd_lu_dev);
874
875         ENTRY;
876
877         /*
878          * 1.8/2.0 behaviour is that OST being connected once at least
879          * is considired "healthy". and one "healty" OST is enough to
880          * allow lustre clients to connect to MDS
881          */
882         LASSERT(d);
883         RETURN(!d->opd_imp_seen_connected);
884 }
885
886 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
887 LU_KEY_INIT_FINI(osp, struct osp_thread_info);
888 static void osp_key_exit(const struct lu_context *ctx,
889                          struct lu_context_key *key, void *data)
890 {
891         struct osp_thread_info *info = data;
892
893         info->osi_attr.la_valid = 0;
894 }
895
896 struct lu_context_key osp_thread_key = {
897         .lct_tags = LCT_MD_THREAD,
898         .lct_init = osp_key_init,
899         .lct_fini = osp_key_fini,
900         .lct_exit = osp_key_exit
901 };
902
903 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
904 LU_KEY_INIT_FINI(osp_txn, struct osp_txn_info);
905
906 struct lu_context_key osp_txn_key = {
907         .lct_tags = LCT_OSP_THREAD,
908         .lct_init = osp_txn_key_init,
909         .lct_fini = osp_txn_key_fini
910 };
911 LU_TYPE_INIT_FINI(osp, &osp_thread_key, &osp_txn_key);
912
913 static struct lu_device_type_operations osp_device_type_ops = {
914         .ldto_init           = osp_type_init,
915         .ldto_fini           = osp_type_fini,
916
917         .ldto_start          = osp_type_start,
918         .ldto_stop           = osp_type_stop,
919
920         .ldto_device_alloc   = osp_device_alloc,
921         .ldto_device_free    = osp_device_free,
922
923         .ldto_device_fini    = osp_device_fini
924 };
925
926 static struct lu_device_type osp_device_type = {
927         .ldt_tags     = LU_DEVICE_DT,
928         .ldt_name     = LUSTRE_OSP_NAME,
929         .ldt_ops      = &osp_device_type_ops,
930         .ldt_ctx_tags = LCT_MD_THREAD
931 };
932
933 static struct obd_ops osp_obd_device_ops = {
934         .o_owner        = THIS_MODULE,
935         .o_add_conn     = client_import_add_conn,
936         .o_del_conn     = client_import_del_conn,
937         .o_reconnect    = osp_reconnect,
938         .o_connect      = osp_obd_connect,
939         .o_disconnect   = osp_obd_disconnect,
940         .o_health_check = osp_obd_health_check,
941         .o_import_event = osp_import_event,
942         .o_iocontrol    = osp_iocontrol,
943         .o_statfs       = osp_obd_statfs,
944 };
945
946 static int __init osp_mod_init(void)
947 {
948         struct lprocfs_static_vars       lvars;
949         cfs_proc_dir_entry_t            *osc_proc_dir;
950         int                              rc;
951
952         rc = lu_kmem_init(osp_caches);
953         if (rc)
954                 return rc;
955
956         lprocfs_osp_init_vars(&lvars);
957
958         rc = class_register_type(&osp_obd_device_ops, NULL, lvars.module_vars,
959                                  LUSTRE_OSP_NAME, &osp_device_type);
960
961         /* create "osc" entry in procfs for compatibility purposes */
962         if (rc != 0) {
963                 lu_kmem_fini(osp_caches);
964                 return rc;
965         }
966
967         osc_proc_dir = lprocfs_srch(proc_lustre_root, "osc");
968         if (osc_proc_dir == NULL) {
969                 osc_proc_dir = lprocfs_register("osc", proc_lustre_root, NULL,
970                                                 NULL);
971                 if (IS_ERR(osc_proc_dir))
972                         CERROR("osp: can't create compat entry \"osc\": %d\n",
973                                (int) PTR_ERR(osc_proc_dir));
974         }
975         return rc;
976 }
977
978 static void __exit osp_mod_exit(void)
979 {
980         lprocfs_try_remove_proc_entry("osc", proc_lustre_root);
981
982         class_unregister_type(LUSTRE_OSP_NAME);
983         lu_kmem_fini(osp_caches);
984 }
985
986 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
987 MODULE_DESCRIPTION("Lustre OST Proxy Device ("LUSTRE_OSP_NAME")");
988 MODULE_LICENSE("GPL");
989
990 cfs_module(osp, LUSTRE_VERSION_STRING, osp_mod_init, osp_mod_exit);
991