Whamcloud - gitweb
LU-4621 ofd: create same count of objects
[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) 2012, 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/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  * Author: Di Wang <di.wang@intel.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #include <obd_class.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_param.h>
50 #include <lustre_log.h>
51 #include <lustre_mdc.h>
52
53 #include "osp_internal.h"
54
55 /* Slab for OSP object allocation */
56 struct kmem_cache *osp_object_kmem;
57
58 static struct lu_kmem_descr osp_caches[] = {
59         {
60                 .ckd_cache = &osp_object_kmem,
61                 .ckd_name  = "osp_obj",
62                 .ckd_size  = sizeof(struct osp_object)
63         },
64         {
65                 .ckd_cache = NULL
66         }
67 };
68
69 struct lu_object *osp_object_alloc(const struct lu_env *env,
70                                    const struct lu_object_header *hdr,
71                                    struct lu_device *d)
72 {
73         struct lu_object_header *h = NULL;
74         struct osp_object       *o;
75         struct lu_object        *l;
76
77         OBD_SLAB_ALLOC_PTR_GFP(o, osp_object_kmem, GFP_NOFS);
78         if (o != NULL) {
79                 l = &o->opo_obj.do_lu;
80
81                 /* If hdr is NULL, it means the object is not built
82                  * from the top dev(MDT/OST), usually it happens when
83                  * building striped object, like data object on MDT or
84                  * striped object for directory */
85                 if (hdr == NULL) {
86                         h = &o->opo_header;
87                         lu_object_header_init(h);
88                         dt_object_init(&o->opo_obj, h, d);
89                         lu_object_add_top(h, l);
90                 } else {
91                         dt_object_init(&o->opo_obj, h, d);
92                 }
93
94                 l->lo_ops = &osp_lu_obj_ops;
95
96                 return l;
97         } else {
98                 return NULL;
99         }
100 }
101
102 static struct dt_object
103 *osp_find_or_create(const struct lu_env *env, struct osp_device *osp,
104                     struct lu_attr *attr, __u32 reg_id)
105 {
106         struct osp_thread_info *osi = osp_env_info(env);
107         struct dt_object_format dof = { 0 };
108         struct dt_object       *dto;
109         int                  rc;
110         ENTRY;
111
112         lu_local_obj_fid(&osi->osi_fid, reg_id);
113         attr->la_valid = LA_MODE;
114         attr->la_mode = S_IFREG | 0644;
115         dof.dof_type = DFT_REGULAR;
116         dto = dt_find_or_create(env, osp->opd_storage, &osi->osi_fid,
117                                 &dof, attr);
118         if (IS_ERR(dto))
119                 RETURN(dto);
120
121         rc = dt_attr_get(env, dto, attr, NULL);
122         if (rc) {
123                 CERROR("%s: can't be initialized: rc = %d\n",
124                        osp->opd_obd->obd_name, rc);
125                 lu_object_put(env, &dto->do_lu);
126                 RETURN(ERR_PTR(rc));
127         }
128         RETURN(dto);
129 }
130
131 static int osp_write_local_file(const struct lu_env *env,
132                                 struct osp_device *osp,
133                                 struct dt_object *dt_obj,
134                                 struct lu_buf *buf,
135                                 loff_t offset)
136 {
137         struct thandle *th;
138         int rc;
139
140         th = dt_trans_create(env, osp->opd_storage);
141         if (IS_ERR(th))
142                 RETURN(PTR_ERR(th));
143
144         rc = dt_declare_record_write(env, dt_obj, buf, offset, th);
145         if (rc)
146                 GOTO(out, rc);
147         rc = dt_trans_start_local(env, osp->opd_storage, th);
148         if (rc)
149                 GOTO(out, rc);
150
151         rc = dt_record_write(env, dt_obj, buf, &offset, th);
152 out:
153         dt_trans_stop(env, osp->opd_storage, th);
154         RETURN(rc);
155 }
156
157 static int osp_init_last_objid(const struct lu_env *env, struct osp_device *osp)
158 {
159         struct osp_thread_info  *osi = osp_env_info(env);
160         struct lu_fid           *fid = &osp->opd_last_used_fid;
161         struct dt_object        *dto;
162         int                     rc;
163         ENTRY;
164
165         dto = osp_find_or_create(env, osp, &osi->osi_attr, MDD_LOV_OBJ_OID);
166         if (IS_ERR(dto))
167                 RETURN(PTR_ERR(dto));
168         /* object will be released in device cleanup path */
169         if (osi->osi_attr.la_size >=
170             sizeof(osi->osi_id) * (osp->opd_index + 1)) {
171                 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_oid,
172                                    osp->opd_index);
173                 rc = dt_record_read(env, dto, &osi->osi_lb, &osi->osi_off);
174                 if (rc != 0)
175                         GOTO(out, rc);
176         } else {
177                 fid->f_oid = 0;
178                 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_oid,
179                                    osp->opd_index);
180                 rc = osp_write_local_file(env, osp, dto, &osi->osi_lb,
181                                           osi->osi_off);
182         }
183         osp->opd_last_used_oid_file = dto;
184         RETURN(0);
185 out:
186         /* object will be released in device cleanup path */
187         CERROR("%s: can't initialize lov_objid: rc = %d\n",
188                osp->opd_obd->obd_name, rc);
189         lu_object_put(env, &dto->do_lu);
190         osp->opd_last_used_oid_file = NULL;
191         RETURN(rc);
192 }
193
194 static int osp_init_last_seq(const struct lu_env *env, struct osp_device *osp)
195 {
196         struct osp_thread_info  *osi = osp_env_info(env);
197         struct lu_fid           *fid = &osp->opd_last_used_fid;
198         struct dt_object        *dto;
199         int                     rc;
200         ENTRY;
201
202         dto = osp_find_or_create(env, osp, &osi->osi_attr, MDD_LOV_OBJ_OSEQ);
203         if (IS_ERR(dto))
204                 RETURN(PTR_ERR(dto));
205
206         /* object will be released in device cleanup path */
207         if (osi->osi_attr.la_size >=
208             sizeof(osi->osi_id) * (osp->opd_index + 1)) {
209                 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_seq,
210                                    osp->opd_index);
211                 rc = dt_record_read(env, dto, &osi->osi_lb, &osi->osi_off);
212                 if (rc != 0)
213                         GOTO(out, rc);
214         } else {
215                 fid->f_seq = 0;
216                 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off, &fid->f_seq,
217                                     osp->opd_index);
218                 rc = osp_write_local_file(env, osp, dto, &osi->osi_lb,
219                                           osi->osi_off);
220         }
221         osp->opd_last_used_seq_file = dto;
222         RETURN(0);
223 out:
224         /* object will be released in device cleanup path */
225         CERROR("%s: can't initialize lov_seq: rc = %d\n",
226                osp->opd_obd->obd_name, rc);
227         lu_object_put(env, &dto->do_lu);
228         osp->opd_last_used_seq_file = NULL;
229         RETURN(rc);
230 }
231
232 static int osp_last_used_init(const struct lu_env *env, struct osp_device *osp)
233 {
234         struct osp_thread_info *osi = osp_env_info(env);
235         int                  rc;
236         ENTRY;
237
238         fid_zero(&osp->opd_last_used_fid);
239         rc = osp_init_last_objid(env, osp);
240         if (rc < 0) {
241                 CERROR("%s: Can not get ids %d from old objid!\n",
242                        osp->opd_obd->obd_name, rc);
243                 RETURN(rc);
244         }
245
246         rc = osp_init_last_seq(env, osp);
247         if (rc < 0) {
248                 CERROR("%s: Can not get ids %d from old objid!\n",
249                        osp->opd_obd->obd_name, rc);
250                 GOTO(out, rc);
251         }
252
253         if (fid_oid(&osp->opd_last_used_fid) != 0 &&
254             fid_seq(&osp->opd_last_used_fid) == 0) {
255                 /* Just upgrade from the old version,
256                  * set the seq to be IDIF */
257                 osp->opd_last_used_fid.f_seq =
258                    fid_idif_seq(fid_oid(&osp->opd_last_used_fid),
259                                 osp->opd_index);
260                 osp_objseq_buf_prep(&osi->osi_lb, &osi->osi_off,
261                                     &osp->opd_last_used_fid.f_seq,
262                                     osp->opd_index);
263                 rc = osp_write_local_file(env, osp, osp->opd_last_used_seq_file,
264                                           &osi->osi_lb, osi->osi_off);
265                 if (rc) {
266                         CERROR("%s : Can not write seq file: rc = %d\n",
267                                osp->opd_obd->obd_name, rc);
268                         GOTO(out, rc);
269                 }
270         }
271
272         if (!fid_is_zero(&osp->opd_last_used_fid) &&
273                  !fid_is_sane(&osp->opd_last_used_fid)) {
274                 CERROR("%s: Got invalid FID "DFID"\n", osp->opd_obd->obd_name,
275                         PFID(&osp->opd_last_used_fid));
276                 GOTO(out, rc = -EINVAL);
277         }
278
279         CDEBUG(D_INFO, "%s: Init last used fid "DFID"\n",
280                osp->opd_obd->obd_name, PFID(&osp->opd_last_used_fid));
281 out:
282         if (rc != 0) {
283                 if (osp->opd_last_used_oid_file != NULL) {
284                         lu_object_put(env, &osp->opd_last_used_oid_file->do_lu);
285                         osp->opd_last_used_oid_file = NULL;
286                 }
287                 if (osp->opd_last_used_seq_file != NULL) {
288                         lu_object_put(env, &osp->opd_last_used_seq_file->do_lu);
289                         osp->opd_last_used_seq_file = NULL;
290                 }
291         }
292
293         RETURN(rc);
294 }
295
296 static void osp_last_used_fini(const struct lu_env *env, struct osp_device *d)
297 {
298         /* release last_used file */
299         if (d->opd_last_used_oid_file != NULL) {
300                 lu_object_put(env, &d->opd_last_used_oid_file->do_lu);
301                 d->opd_last_used_oid_file = NULL;
302         }
303
304         if (d->opd_last_used_seq_file != NULL) {
305                 lu_object_put(env, &d->opd_last_used_seq_file->do_lu);
306                 d->opd_last_used_seq_file = NULL;
307         }
308 }
309
310 static int osp_disconnect(struct osp_device *d)
311 {
312         struct obd_import *imp;
313         int rc = 0;
314
315         imp = d->opd_obd->u.cli.cl_import;
316
317         /* Mark import deactivated now, so we don't try to reconnect if any
318          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
319          * fully deactivate the import, or that would drop all requests. */
320         LASSERT(imp != NULL);
321         spin_lock(&imp->imp_lock);
322         imp->imp_deactive = 1;
323         spin_unlock(&imp->imp_lock);
324
325         ptlrpc_deactivate_import(imp);
326
327         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
328          * delete it regardless.  (It's safe to delete an import that was
329          * never added.) */
330         (void)ptlrpc_pinger_del_import(imp);
331
332         rc = ptlrpc_disconnect_import(imp, 0);
333         if (rc != 0)
334                 CERROR("%s: can't disconnect: rc = %d\n",
335                        d->opd_obd->obd_name, rc);
336
337         ptlrpc_invalidate_import(imp);
338
339         RETURN(rc);
340 }
341
342 static int osp_shutdown(const struct lu_env *env, struct osp_device *d)
343 {
344         int                      rc = 0;
345         ENTRY;
346
347         LASSERT(env);
348
349         rc = osp_disconnect(d);
350
351         osp_sync_fini(d);
352
353         if (!d->opd_connect_mdt) {
354                 /* stop precreate thread */
355                 osp_precreate_fini(d);
356
357                 /* release last_used file */
358                 osp_last_used_fini(env, d);
359         }
360
361         obd_fid_fini(d->opd_obd);
362
363         RETURN(rc);
364 }
365
366 static int osp_process_config(const struct lu_env *env,
367                               struct lu_device *dev, struct lustre_cfg *lcfg)
368 {
369         struct osp_device               *d = lu2osp_dev(dev);
370         struct obd_device               *obd = d->opd_obd;
371         int                              rc;
372
373         ENTRY;
374
375         switch (lcfg->lcfg_command) {
376         case LCFG_PRE_CLEANUP:
377                 rc = osp_disconnect(d);
378                 break;
379         case LCFG_CLEANUP:
380                 lu_dev_del_linkage(dev->ld_site, dev);
381                 rc = osp_shutdown(env, d);
382                 break;
383         case LCFG_PARAM:
384                 LASSERT(obd);
385                 rc = class_process_proc_seq_param(PARAM_OSC, obd->obd_vars,
386                                                   lcfg, obd);
387                 if (rc > 0)
388                         rc = 0;
389                 if (rc == -ENOSYS) {
390                         /* class_process_proc_param() haven't found matching
391                          * parameter and returned ENOSYS so that layer(s)
392                          * below could use that. But OSP is the bottom, so
393                          * just ignore it */
394                         CERROR("%s: unknown param %s\n",
395                                (char *)lustre_cfg_string(lcfg, 0),
396                                (char *)lustre_cfg_string(lcfg, 1));
397                         rc = 0;
398                 }
399                 break;
400         default:
401                 CERROR("%s: unknown command %u\n",
402                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
403                 rc = 0;
404                 break;
405         }
406
407         RETURN(rc);
408 }
409
410 static int osp_recovery_complete(const struct lu_env *env,
411                                  struct lu_device *dev)
412 {
413         struct osp_device       *osp = lu2osp_dev(dev);
414         int                      rc = 0;
415
416         ENTRY;
417         osp->opd_recovery_completed = 1;
418
419         if (!osp->opd_connect_mdt && osp->opd_pre != NULL)
420                 wake_up(&osp->opd_pre_waitq);
421         RETURN(rc);
422 }
423
424 const struct lu_device_operations osp_lu_ops = {
425         .ldo_object_alloc       = osp_object_alloc,
426         .ldo_process_config     = osp_process_config,
427         .ldo_recovery_complete  = osp_recovery_complete,
428 };
429
430 /**
431  * provides with statfs from corresponded OST
432  *
433  */
434 static int osp_statfs(const struct lu_env *env, struct dt_device *dev,
435                       struct obd_statfs *sfs)
436 {
437         struct osp_device *d = dt2osp_dev(dev);
438
439         ENTRY;
440
441         if (unlikely(d->opd_imp_active == 0))
442                 RETURN(-ENOTCONN);
443
444         if (d->opd_pre == NULL)
445                 RETURN(0);
446
447         /* return recently updated data */
448         *sfs = d->opd_statfs;
449
450         /*
451          * layer above osp (usually lod) can use ffree to estimate
452          * how many objects are available for immediate creation
453          */
454         spin_lock(&d->opd_pre_lock);
455         LASSERTF(fid_seq(&d->opd_pre_last_created_fid) ==
456                  fid_seq(&d->opd_pre_used_fid),
457                  "last_created "DFID", next_fid "DFID"\n",
458                  PFID(&d->opd_pre_last_created_fid),
459                  PFID(&d->opd_pre_used_fid));
460         sfs->os_fprecreated = fid_oid(&d->opd_pre_last_created_fid) -
461                               fid_oid(&d->opd_pre_used_fid);
462         sfs->os_fprecreated -= d->opd_pre_reserved;
463         spin_unlock(&d->opd_pre_lock);
464
465         LASSERT(sfs->os_fprecreated <= OST_MAX_PRECREATE * 2);
466
467         CDEBUG(D_OTHER, "%s: "LPU64" blocks, "LPU64" free, "LPU64" avail, "
468                LPU64" files, "LPU64" free files\n", d->opd_obd->obd_name,
469                sfs->os_blocks, sfs->os_bfree, sfs->os_bavail,
470                sfs->os_files, sfs->os_ffree);
471         RETURN(0);
472 }
473
474 static int osp_sync_timeout(void *data)
475 {
476         return 1;
477 }
478
479 static int osp_sync(const struct lu_env *env, struct dt_device *dev)
480 {
481         struct osp_device *d = dt2osp_dev(dev);
482         cfs_time_t         expire;
483         struct l_wait_info lwi = { 0 };
484         unsigned long      id, old;
485         int                rc = 0;
486         unsigned long      start = cfs_time_current();
487         ENTRY;
488
489         if (unlikely(d->opd_imp_active == 0))
490                 RETURN(-ENOTCONN);
491
492         id = d->opd_syn_last_used_id;
493
494         CDEBUG(D_OTHER, "%s: id: used %lu, processed %lu\n",
495                d->opd_obd->obd_name, id, d->opd_syn_last_processed_id);
496
497         /* wait till all-in-line are processed */
498         while (d->opd_syn_last_processed_id < id) {
499
500                 old = d->opd_syn_last_processed_id;
501
502                 /* make sure the connection is fine */
503                 expire = cfs_time_shift(obd_timeout);
504                 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
505                                   osp_sync_timeout, d);
506                 l_wait_event(d->opd_syn_barrier_waitq,
507                              d->opd_syn_last_processed_id >= id,
508                              &lwi);
509
510                 if (d->opd_syn_last_processed_id >= id)
511                         break;
512
513                 if (d->opd_syn_last_processed_id != old) {
514                         /* some progress have been made,
515                          * keep trying... */
516                         continue;
517                 }
518
519                 /* no changes and expired, something is wrong */
520                 GOTO(out, rc = -ETIMEDOUT);
521         }
522
523         /* block new processing (barrier>0 - few callers are possible */
524         atomic_inc(&d->opd_syn_barrier);
525
526         CDEBUG(D_OTHER, "%s: %u in flight\n", d->opd_obd->obd_name,
527                d->opd_syn_rpc_in_flight);
528
529         /* wait till all-in-flight are replied, so executed by the target */
530         /* XXX: this is used by LFSCK at the moment, which doesn't require
531          *      all the changes to be committed, but in general it'd be
532          *      better to wait till commit */
533         while (d->opd_syn_rpc_in_flight > 0) {
534
535                 old = d->opd_syn_rpc_in_flight;
536
537                 expire = cfs_time_shift(obd_timeout);
538                 lwi = LWI_TIMEOUT(expire - cfs_time_current(),
539                                   osp_sync_timeout, d);
540                 l_wait_event(d->opd_syn_barrier_waitq,
541                                 d->opd_syn_rpc_in_flight == 0, &lwi);
542
543                 if (d->opd_syn_rpc_in_flight == 0)
544                         break;
545
546                 if (d->opd_syn_rpc_in_flight != old) {
547                         /* some progress have been made */
548                         continue;
549                 }
550
551                 /* no changes and expired, something is wrong */
552                 GOTO(out, rc = -ETIMEDOUT);
553         }
554
555         CDEBUG(D_OTHER, "%s: done in %lu\n", d->opd_obd->obd_name,
556                cfs_time_current() - start);
557 out:
558         /* resume normal processing (barrier=0) */
559         atomic_dec(&d->opd_syn_barrier);
560         __osp_sync_check_for_work(d);
561
562         RETURN(rc);
563 }
564
565 const struct dt_device_operations osp_dt_ops = {
566         .dt_statfs       = osp_statfs,
567         .dt_sync         = osp_sync,
568         .dt_trans_create = osp_trans_create,
569         .dt_trans_start  = osp_trans_start,
570         .dt_trans_stop   = osp_trans_stop,
571 };
572
573 static int osp_connect_to_osd(const struct lu_env *env, struct osp_device *m,
574                               const char *nextdev)
575 {
576         struct obd_connect_data *data = NULL;
577         struct obd_device       *obd;
578         int                      rc;
579
580         ENTRY;
581
582         LASSERT(m->opd_storage_exp == NULL);
583
584         OBD_ALLOC_PTR(data);
585         if (data == NULL)
586                 RETURN(-ENOMEM);
587
588         obd = class_name2obd(nextdev);
589         if (obd == NULL) {
590                 CERROR("%s: can't locate next device: %s\n",
591                        m->opd_obd->obd_name, nextdev);
592                 GOTO(out, rc = -ENOTCONN);
593         }
594
595         rc = obd_connect(env, &m->opd_storage_exp, obd, &obd->obd_uuid, data,
596                          NULL);
597         if (rc) {
598                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
599                        m->opd_obd->obd_name, nextdev, rc);
600                 GOTO(out, rc);
601         }
602
603         m->opd_dt_dev.dd_lu_dev.ld_site =
604                 m->opd_storage_exp->exp_obd->obd_lu_dev->ld_site;
605         LASSERT(m->opd_dt_dev.dd_lu_dev.ld_site);
606         m->opd_storage = lu2dt_dev(m->opd_storage_exp->exp_obd->obd_lu_dev);
607
608 out:
609         OBD_FREE_PTR(data);
610         RETURN(rc);
611 }
612
613 static int osp_init0(const struct lu_env *env, struct osp_device *m,
614                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
615 {
616         struct obd_device       *obd;
617         struct obd_import       *imp;
618         class_uuid_t            uuid;
619         char                    *src, *tgt, *mdt, *osdname = NULL;
620         int                     rc;
621         long                    idx;
622
623         ENTRY;
624
625         mutex_init(&m->opd_async_requests_mutex);
626
627         obd = class_name2obd(lustre_cfg_string(cfg, 0));
628         if (obd == NULL) {
629                 CERROR("Cannot find obd with name %s\n",
630                        lustre_cfg_string(cfg, 0));
631                 RETURN(-ENODEV);
632         }
633         m->opd_obd = obd;
634
635         /* There is no record in the MDT configuration for the local disk
636          * device, so we have to extract this from elsewhere in the profile.
637          * The only information we get at setup is from the OSC records:
638          * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
639          * Note that 1.8 generated configs are missing the -MDTxxxx part.
640          * We need to reconstruct the name of the underlying OSD from this:
641          * {fsname}-{svname}-osd, for example "lustre-MDT0000-osd".  We
642          * also need to determine the OST index from this - will be used
643          * to calculate the offset in shared lov_objids file later */
644
645         src = lustre_cfg_string(cfg, 0);
646         if (src == NULL)
647                 RETURN(-EINVAL);
648
649         tgt = strrchr(src, '-');
650         if (tgt == NULL) {
651                 CERROR("%s: invalid target name %s\n",
652                        m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
653                 RETURN(-EINVAL);
654         }
655
656         if (strncmp(tgt, "-osc", 4) == 0) {
657                 /* Old OSC name fsname-OSTXXXX-osc */
658                 for (tgt--; tgt > src && *tgt != '-'; tgt--)
659                         ;
660                 if (tgt == src) {
661                         CERROR("%s: invalid target name %s\n",
662                                m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
663                         RETURN(-EINVAL);
664                 }
665
666                 if (strncmp(tgt, "-OST", 4) != 0) {
667                         CERROR("%s: invalid target name %s\n",
668                                m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
669                         RETURN(-EINVAL);
670                 }
671
672                 idx = simple_strtol(tgt + 4, &mdt, 16);
673                 if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
674                         CERROR("%s: invalid OST index in '%s'\n",
675                                m->opd_obd->obd_name, src);
676                         RETURN(-EINVAL);
677                 }
678                 m->opd_index = idx;
679                 m->opd_group = 0;
680                 idx = tgt - src;
681         } else {
682                 /* New OSC name fsname-OSTXXXX-osc-MDTXXXX */
683                 if (strncmp(tgt, "-MDT", 4) != 0 &&
684                     strncmp(tgt, "-OST", 4) != 0) {
685                         CERROR("%s: invalid target name %s\n",
686                                m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
687                         RETURN(-EINVAL);
688                 }
689
690                 idx = simple_strtol(tgt + 4, &mdt, 16);
691                 if (*mdt != '\0' || idx > INT_MAX || idx < 0) {
692                         CERROR("%s: invalid OST index in '%s'\n",
693                                m->opd_obd->obd_name, src);
694                         RETURN(-EINVAL);
695                 }
696
697                 /* Get MDT index from the name and set it to opd_group,
698                  * which will be used by OSP to connect with OST */
699                 m->opd_group = idx;
700                 if (tgt - src <= 12) {
701                         CERROR("%s: invalid mdt index retrieve from %s\n",
702                                m->opd_obd->obd_name, lustre_cfg_string(cfg, 0));
703                         RETURN(-EINVAL);
704                 }
705
706                 if (strncmp(tgt - 12, "-MDT", 4) == 0)
707                         m->opd_connect_mdt = 1;
708
709                 idx = simple_strtol(tgt - 8, &mdt, 16);
710                 if (mdt[0] != '-' || idx > INT_MAX || idx < 0) {
711                         CERROR("%s: invalid OST index in '%s'\n",
712                                m->opd_obd->obd_name, src);
713                         RETURN(-EINVAL);
714                 }
715
716                 m->opd_index = idx;
717                 idx = tgt - src - 12;
718         }
719         /* check the fsname length, and after this everything else will fit */
720         if (idx > MTI_NAME_MAXLEN) {
721                 CERROR("%s: fsname too long in '%s'\n",
722                        m->opd_obd->obd_name, src);
723                 RETURN(-EINVAL);
724         }
725
726         OBD_ALLOC(osdname, MAX_OBD_NAME);
727         if (osdname == NULL)
728                 RETURN(-ENOMEM);
729
730         memcpy(osdname, src, idx); /* copy just the fsname part */
731         osdname[idx] = '\0';
732
733         mdt = strstr(mdt, "-MDT");
734         if (mdt == NULL) /* 1.8 configs don't have "-MDT0000" at the end */
735                 strcat(osdname, "-MDT0000");
736         else
737                 strcat(osdname, mdt);
738         strcat(osdname, "-osd");
739         CDEBUG(D_HA, "%s: connect to %s (%s)\n", obd->obd_name, osdname, src);
740
741         if (m->opd_connect_mdt) {
742                 struct client_obd *cli = &m->opd_obd->u.cli;
743
744                 OBD_ALLOC(cli->cl_rpc_lock, sizeof(*cli->cl_rpc_lock));
745                 if (!cli->cl_rpc_lock)
746                         GOTO(out_fini, rc = -ENOMEM);
747                 osp_init_rpc_lock(cli->cl_rpc_lock);
748         }
749
750         m->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
751         m->opd_dt_dev.dd_ops = &osp_dt_ops;
752         obd->obd_lu_dev = &m->opd_dt_dev.dd_lu_dev;
753
754         rc = osp_connect_to_osd(env, m, osdname);
755         if (rc)
756                 GOTO(out_fini, rc);
757
758         rc = ptlrpcd_addref();
759         if (rc)
760                 GOTO(out_disconnect, rc);
761
762         rc = client_obd_setup(obd, cfg);
763         if (rc) {
764                 CERROR("%s: can't setup obd: %d\n", m->opd_obd->obd_name, rc);
765                 GOTO(out_ref, rc);
766         }
767
768         osp_lprocfs_init(m);
769
770         rc = obd_fid_init(m->opd_obd, NULL, m->opd_connect_mdt ?
771                           LUSTRE_SEQ_METADATA : LUSTRE_SEQ_DATA);
772         if (rc) {
773                 CERROR("%s: fid init error: rc = %d\n",
774                        m->opd_obd->obd_name, rc);
775                 GOTO(out_proc, rc);
776         }
777
778         if (!m->opd_connect_mdt) {
779                 /* Initialize last id from the storage - will be
780                  * used in orphan cleanup. */
781                 rc = osp_last_used_init(env, m);
782                 if (rc)
783                         GOTO(out_proc, rc);
784
785
786                 /* Initialize precreation thread, it handles new
787                  * connections as well. */
788                 rc = osp_init_precreate(m);
789                 if (rc)
790                         GOTO(out_last_used, rc);
791         }
792
793         /*
794          * Initialize synhronization mechanism taking
795          * care of propogating changes to OST in near
796          * transactional manner.
797          */
798         rc = osp_sync_init(env, m);
799         if (rc)
800                 GOTO(out_precreat, rc);
801
802         /*
803          * Initiate connect to OST
804          */
805         ll_generate_random_uuid(uuid);
806         class_uuid_unparse(uuid, &m->opd_cluuid);
807
808         imp = obd->u.cli.cl_import;
809
810         rc = ptlrpc_init_import(imp);
811         if (rc)
812                 GOTO(out, rc);
813         if (osdname)
814                 OBD_FREE(osdname, MAX_OBD_NAME);
815         RETURN(0);
816
817 out:
818         /* stop sync thread */
819         osp_sync_fini(m);
820 out_precreat:
821         /* stop precreate thread */
822         if (!m->opd_connect_mdt)
823                 osp_precreate_fini(m);
824 out_last_used:
825         if (!m->opd_connect_mdt)
826                 osp_last_used_fini(env, m);
827 out_proc:
828         ptlrpc_lprocfs_unregister_obd(obd);
829         lprocfs_obd_cleanup(obd);
830         if (m->opd_symlink)
831                 lprocfs_remove(&m->opd_symlink);
832         client_obd_cleanup(obd);
833 out_ref:
834         ptlrpcd_decref();
835 out_disconnect:
836         if (m->opd_connect_mdt) {
837                 struct client_obd *cli = &m->opd_obd->u.cli;
838                 if (cli->cl_rpc_lock != NULL) {
839                         OBD_FREE_PTR(cli->cl_rpc_lock);
840                         cli->cl_rpc_lock = NULL;
841                 }
842         }
843         obd_disconnect(m->opd_storage_exp);
844 out_fini:
845         if (osdname)
846                 OBD_FREE(osdname, MAX_OBD_NAME);
847         RETURN(rc);
848 }
849
850 static struct lu_device *osp_device_free(const struct lu_env *env,
851                                          struct lu_device *lu)
852 {
853         struct osp_device *m = lu2osp_dev(lu);
854
855         ENTRY;
856
857         if (atomic_read(&lu->ld_ref) && lu->ld_site) {
858                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
859                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
860         }
861         dt_device_fini(&m->opd_dt_dev);
862         OBD_FREE_PTR(m);
863         RETURN(NULL);
864 }
865
866 static struct lu_device *osp_device_alloc(const struct lu_env *env,
867                                           struct lu_device_type *t,
868                                           struct lustre_cfg *lcfg)
869 {
870         struct osp_device *m;
871         struct lu_device  *l;
872
873         OBD_ALLOC_PTR(m);
874         if (m == NULL) {
875                 l = ERR_PTR(-ENOMEM);
876         } else {
877                 int rc;
878
879                 l = osp2lu_dev(m);
880                 dt_device_init(&m->opd_dt_dev, t);
881                 rc = osp_init0(env, m, t, lcfg);
882                 if (rc != 0) {
883                         osp_device_free(env, l);
884                         l = ERR_PTR(rc);
885                 }
886         }
887         return l;
888 }
889
890 static struct lu_device *osp_device_fini(const struct lu_env *env,
891                                          struct lu_device *d)
892 {
893         struct osp_device *m = lu2osp_dev(d);
894         struct obd_import *imp;
895         int                rc;
896
897         ENTRY;
898
899         if (m->opd_async_requests != NULL) {
900                 out_destroy_update_req(m->opd_async_requests);
901                 m->opd_async_requests = NULL;
902         }
903
904         if (m->opd_storage_exp)
905                 obd_disconnect(m->opd_storage_exp);
906
907         imp = m->opd_obd->u.cli.cl_import;
908
909         if (imp->imp_rq_pool) {
910                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
911                 imp->imp_rq_pool = NULL;
912         }
913
914         if (m->opd_symlink)
915                 lprocfs_remove(&m->opd_symlink);
916
917         LASSERT(m->opd_obd);
918         ptlrpc_lprocfs_unregister_obd(m->opd_obd);
919         lprocfs_obd_cleanup(m->opd_obd);
920
921         if (m->opd_connect_mdt) {
922                 struct client_obd *cli = &m->opd_obd->u.cli;
923                 if (cli->cl_rpc_lock != NULL) {
924                         OBD_FREE_PTR(cli->cl_rpc_lock);
925                         cli->cl_rpc_lock = NULL;
926                 }
927         }
928
929         rc = client_obd_cleanup(m->opd_obd);
930         LASSERTF(rc == 0, "error %d\n", rc);
931
932         ptlrpcd_decref();
933
934         RETURN(NULL);
935 }
936
937 static int osp_reconnect(const struct lu_env *env,
938                          struct obd_export *exp, struct obd_device *obd,
939                          struct obd_uuid *cluuid,
940                          struct obd_connect_data *data,
941                          void *localdata)
942 {
943         return 0;
944 }
945
946 static int osp_prepare_fid_client(struct osp_device *osp)
947 {
948         LASSERT(osp->opd_obd->u.cli.cl_seq != NULL);
949         if (osp->opd_obd->u.cli.cl_seq->lcs_exp != NULL)
950                 return 0;
951
952         LASSERT(osp->opd_exp != NULL);
953         osp->opd_obd->u.cli.cl_seq->lcs_exp =
954                                 class_export_get(osp->opd_exp);
955         return 0;
956 }
957
958 /*
959  * we use exports to track all LOD users
960  */
961 static int osp_obd_connect(const struct lu_env *env, struct obd_export **exp,
962                            struct obd_device *obd, struct obd_uuid *cluuid,
963                            struct obd_connect_data *data, void *localdata)
964 {
965         struct osp_device       *osp = lu2osp_dev(obd->obd_lu_dev);
966         struct obd_connect_data *ocd;
967         struct obd_import       *imp;
968         struct lustre_handle     conn;
969         int                      rc;
970
971         ENTRY;
972
973         CDEBUG(D_CONFIG, "connect #%d\n", osp->opd_connects);
974
975         rc = class_connect(&conn, obd, cluuid);
976         if (rc)
977                 RETURN(rc);
978
979         *exp = class_conn2export(&conn);
980         /* Why should there ever be more than 1 connect? */
981         osp->opd_connects++;
982         LASSERT(osp->opd_connects == 1);
983
984         osp->opd_exp = *exp;
985
986         imp = osp->opd_obd->u.cli.cl_import;
987         imp->imp_dlm_handle = conn;
988
989         LASSERT(data != NULL);
990         LASSERT(data->ocd_connect_flags & OBD_CONNECT_INDEX);
991         ocd = &imp->imp_connect_data;
992         *ocd = *data;
993
994         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
995
996         ocd->ocd_version = LUSTRE_VERSION_CODE;
997         ocd->ocd_index = data->ocd_index;
998         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
999
1000         rc = ptlrpc_connect_import(imp);
1001         if (rc) {
1002                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
1003                 GOTO(out, rc);
1004         }
1005
1006         ptlrpc_pinger_add_import(imp);
1007 out:
1008         RETURN(rc);
1009 }
1010
1011 /*
1012  * once last export (we don't count self-export) disappeared
1013  * osp can be released
1014  */
1015 static int osp_obd_disconnect(struct obd_export *exp)
1016 {
1017         struct obd_device *obd = exp->exp_obd;
1018         struct osp_device *osp = lu2osp_dev(obd->obd_lu_dev);
1019         int                rc;
1020         ENTRY;
1021
1022         /* Only disconnect the underlying layers on the final disconnect. */
1023         LASSERT(osp->opd_connects == 1);
1024         osp->opd_connects--;
1025
1026         rc = class_disconnect(exp);
1027         if (rc) {
1028                 CERROR("%s: class disconnect error: rc = %d\n",
1029                        obd->obd_name, rc);
1030                 RETURN(rc);
1031         }
1032
1033         /* destroy the device */
1034         class_manual_cleanup(obd);
1035
1036         RETURN(rc);
1037 }
1038
1039 /*
1040  * lprocfs helpers still use OBD API, let's keep obd_statfs() support
1041  */
1042 static int osp_obd_statfs(const struct lu_env *env, struct obd_export *exp,
1043                           struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1044 {
1045         struct obd_statfs       *msfs;
1046         struct ptlrpc_request   *req;
1047         struct obd_import       *imp = NULL;
1048         int                      rc;
1049
1050         ENTRY;
1051
1052         /* Since the request might also come from lprocfs, so we need
1053          * sync this with client_disconnect_export Bug15684 */
1054         down_read(&exp->exp_obd->u.cli.cl_sem);
1055         if (exp->exp_obd->u.cli.cl_import)
1056                 imp = class_import_get(exp->exp_obd->u.cli.cl_import);
1057         up_read(&exp->exp_obd->u.cli.cl_sem);
1058         if (!imp)
1059                 RETURN(-ENODEV);
1060
1061         /* We could possibly pass max_age in the request (as an absolute
1062          * timestamp or a "seconds.usec ago") so the target can avoid doing
1063          * extra calls into the filesystem if that isn't necessary (e.g.
1064          * during mount that would help a bit).  Having relative timestamps
1065          * is not so great if request processing is slow, while absolute
1066          * timestamps are not ideal because they need time synchronization. */
1067         req = ptlrpc_request_alloc(imp, &RQF_OST_STATFS);
1068
1069         class_import_put(imp);
1070
1071         if (req == NULL)
1072                 RETURN(-ENOMEM);
1073
1074         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_STATFS);
1075         if (rc) {
1076                 ptlrpc_request_free(req);
1077                 RETURN(rc);
1078         }
1079         ptlrpc_request_set_replen(req);
1080         req->rq_request_portal = OST_CREATE_PORTAL;
1081         ptlrpc_at_set_req_timeout(req);
1082
1083         if (flags & OBD_STATFS_NODELAY) {
1084                 /* procfs requests not want stat in wait for avoid deadlock */
1085                 req->rq_no_resend = 1;
1086                 req->rq_no_delay = 1;
1087         }
1088
1089         rc = ptlrpc_queue_wait(req);
1090         if (rc)
1091                 GOTO(out, rc);
1092
1093         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1094         if (msfs == NULL)
1095                 GOTO(out, rc = -EPROTO);
1096
1097         *osfs = *msfs;
1098
1099         EXIT;
1100 out:
1101         ptlrpc_req_finished(req);
1102         return rc;
1103 }
1104
1105 static int osp_import_event(struct obd_device *obd, struct obd_import *imp,
1106                             enum obd_import_event event)
1107 {
1108         struct osp_device *d = lu2osp_dev(obd->obd_lu_dev);
1109
1110         switch (event) {
1111         case IMP_EVENT_DISCON:
1112                 d->opd_got_disconnected = 1;
1113                 d->opd_imp_connected = 0;
1114                 if (d->opd_connect_mdt)
1115                         break;
1116
1117                 if (d->opd_pre != NULL) {
1118                         osp_pre_update_status(d, -ENODEV);
1119                         wake_up(&d->opd_pre_waitq);
1120                 }
1121
1122                 CDEBUG(D_HA, "got disconnected\n");
1123                 break;
1124         case IMP_EVENT_INACTIVE:
1125                 d->opd_imp_active = 0;
1126                 if (d->opd_connect_mdt)
1127                         break;
1128
1129                 if (d->opd_pre != NULL) {
1130                         osp_pre_update_status(d, -ENODEV);
1131                         wake_up(&d->opd_pre_waitq);
1132                 }
1133
1134                 CDEBUG(D_HA, "got inactive\n");
1135                 break;
1136         case IMP_EVENT_ACTIVE:
1137                 d->opd_imp_active = 1;
1138
1139                 if (osp_prepare_fid_client(d) != 0)
1140                         break;
1141
1142                 if (d->opd_got_disconnected)
1143                         d->opd_new_connection = 1;
1144                 d->opd_imp_connected = 1;
1145                 d->opd_imp_seen_connected = 1;
1146                 if (d->opd_connect_mdt)
1147                         break;
1148
1149                 if (d->opd_pre != NULL)
1150                         wake_up(&d->opd_pre_waitq);
1151
1152                 __osp_sync_check_for_work(d);
1153                 CDEBUG(D_HA, "got connected\n");
1154                 break;
1155         case IMP_EVENT_INVALIDATE:
1156                 if (obd->obd_namespace == NULL)
1157                         break;
1158                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
1159                 break;
1160         case IMP_EVENT_OCD:
1161         case IMP_EVENT_DEACTIVATE:
1162         case IMP_EVENT_ACTIVATE:
1163                 break;
1164         default:
1165                 CERROR("%s: unsupported import event: %#x\n",
1166                        obd->obd_name, event);
1167         }
1168         return 0;
1169 }
1170
1171 static int osp_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1172                          void *karg, void *uarg)
1173 {
1174         struct obd_device       *obd = exp->exp_obd;
1175         struct osp_device       *d;
1176         struct obd_ioctl_data   *data = karg;
1177         int                      rc = 0;
1178
1179         ENTRY;
1180
1181         LASSERT(obd->obd_lu_dev);
1182         d = lu2osp_dev(obd->obd_lu_dev);
1183         LASSERT(d->opd_dt_dev.dd_ops == &osp_dt_ops);
1184
1185         if (!try_module_get(THIS_MODULE)) {
1186                 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
1187                        module_name(THIS_MODULE));
1188                 return -EINVAL;
1189         }
1190
1191         switch (cmd) {
1192         case OBD_IOC_CLIENT_RECOVER:
1193                 rc = ptlrpc_recover_import(obd->u.cli.cl_import,
1194                                            data->ioc_inlbuf1, 0);
1195                 if (rc > 0)
1196                         rc = 0;
1197                 break;
1198         case IOC_OSC_SET_ACTIVE:
1199                 rc = ptlrpc_set_import_active(obd->u.cli.cl_import,
1200                                               data->ioc_offset);
1201                 break;
1202         case OBD_IOC_PING_TARGET:
1203                 rc = ptlrpc_obd_ping(obd);
1204                 break;
1205         default:
1206                 CERROR("%s: unrecognized ioctl %#x by %s\n", obd->obd_name,
1207                        cmd, current_comm());
1208                 rc = -ENOTTY;
1209         }
1210         module_put(THIS_MODULE);
1211         return rc;
1212 }
1213
1214 static int osp_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1215                             __u32 keylen, void *key, __u32 *vallen, void *val,
1216                             struct lov_stripe_md *lsm)
1217 {
1218         int rc = -EINVAL;
1219
1220         if (KEY_IS(KEY_OSP_CONNECTED)) {
1221                 struct obd_device       *obd = exp->exp_obd;
1222                 struct osp_device       *osp;
1223
1224                 if (!obd->obd_set_up || obd->obd_stopping)
1225                         RETURN(-EAGAIN);
1226
1227                 osp = lu2osp_dev(obd->obd_lu_dev);
1228                 LASSERT(osp);
1229                 /*
1230                  * 1.8/2.0 behaviour is that OST being connected once at least
1231                  * is considered "healthy". and one "healthy" OST is enough to
1232                  * allow lustre clients to connect to MDS
1233                  */
1234                 RETURN(!osp->opd_imp_seen_connected);
1235         }
1236
1237         RETURN(rc);
1238 }
1239
1240 int osp_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1241                   struct lu_fid *fid, struct md_op_data *op_data)
1242 {
1243         struct client_obd       *cli = &exp->exp_obd->u.cli;
1244         struct osp_device       *osp = lu2osp_dev(exp->exp_obd->obd_lu_dev);
1245         struct lu_client_seq    *seq = cli->cl_seq;
1246         ENTRY;
1247
1248         LASSERT(osp->opd_obd->u.cli.cl_seq != NULL);
1249         /* Sigh, fid client is not ready yet */
1250         if (osp->opd_obd->u.cli.cl_seq->lcs_exp == NULL)
1251                 RETURN(-ENOTCONN);
1252
1253         RETURN(seq_client_alloc_fid(env, seq, fid));
1254 }
1255
1256 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
1257 LU_KEY_INIT_FINI(osp, struct osp_thread_info);
1258 static void osp_key_exit(const struct lu_context *ctx,
1259                          struct lu_context_key *key, void *data)
1260 {
1261         struct osp_thread_info *info = data;
1262
1263         info->osi_attr.la_valid = 0;
1264 }
1265
1266 struct lu_context_key osp_thread_key = {
1267         .lct_tags = LCT_MD_THREAD,
1268         .lct_init = osp_key_init,
1269         .lct_fini = osp_key_fini,
1270         .lct_exit = osp_key_exit
1271 };
1272
1273 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
1274 LU_KEY_INIT_FINI(osp_txn, struct osp_txn_info);
1275
1276 struct lu_context_key osp_txn_key = {
1277         .lct_tags = LCT_OSP_THREAD | LCT_TX_HANDLE,
1278         .lct_init = osp_txn_key_init,
1279         .lct_fini = osp_txn_key_fini
1280 };
1281 LU_TYPE_INIT_FINI(osp, &osp_thread_key, &osp_txn_key);
1282
1283 static struct lu_device_type_operations osp_device_type_ops = {
1284         .ldto_init           = osp_type_init,
1285         .ldto_fini           = osp_type_fini,
1286
1287         .ldto_start          = osp_type_start,
1288         .ldto_stop           = osp_type_stop,
1289
1290         .ldto_device_alloc   = osp_device_alloc,
1291         .ldto_device_free    = osp_device_free,
1292
1293         .ldto_device_fini    = osp_device_fini
1294 };
1295
1296 static struct lu_device_type osp_device_type = {
1297         .ldt_tags     = LU_DEVICE_DT,
1298         .ldt_name     = LUSTRE_OSP_NAME,
1299         .ldt_ops      = &osp_device_type_ops,
1300         .ldt_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
1301 };
1302
1303 static struct obd_ops osp_obd_device_ops = {
1304         .o_owner        = THIS_MODULE,
1305         .o_add_conn     = client_import_add_conn,
1306         .o_del_conn     = client_import_del_conn,
1307         .o_reconnect    = osp_reconnect,
1308         .o_connect      = osp_obd_connect,
1309         .o_disconnect   = osp_obd_disconnect,
1310         .o_get_info     = osp_obd_get_info,
1311         .o_import_event = osp_import_event,
1312         .o_iocontrol    = osp_iocontrol,
1313         .o_statfs       = osp_obd_statfs,
1314         .o_fid_init     = client_fid_init,
1315         .o_fid_fini     = client_fid_fini,
1316         .o_fid_alloc    = osp_fid_alloc,
1317 };
1318
1319 struct llog_operations osp_mds_ost_orig_logops;
1320
1321 static int __init osp_mod_init(void)
1322 {
1323         struct obd_type *type;
1324         int rc;
1325
1326         rc = lu_kmem_init(osp_caches);
1327         if (rc)
1328                 return rc;
1329
1330
1331         rc = class_register_type(&osp_obd_device_ops, NULL, true, NULL,
1332 #ifndef HAVE_ONLY_PROCFS_SEQ
1333                                  NULL,
1334 #endif
1335                                  LUSTRE_OSP_NAME, &osp_device_type);
1336         if (rc != 0) {
1337                 lu_kmem_fini(osp_caches);
1338                 return rc;
1339         }
1340
1341         rc = class_register_type(&lwp_obd_device_ops, NULL, true, NULL,
1342 #ifndef HAVE_ONLY_PROCFS_SEQ
1343                                  NULL,
1344 #endif
1345                                  LUSTRE_LWP_NAME, &lwp_device_type);
1346         if (rc != 0) {
1347                 class_unregister_type(LUSTRE_OSP_NAME);
1348                 lu_kmem_fini(osp_caches);
1349                 return rc;
1350         }
1351
1352         /* Note: add_rec/delcare_add_rec will be only used by catalogs */
1353         osp_mds_ost_orig_logops = llog_osd_ops;
1354         osp_mds_ost_orig_logops.lop_add = llog_cat_add_rec;
1355         osp_mds_ost_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1356
1357         /* create "osc" entry in procfs for compatibility purposes */
1358         type = class_search_type(LUSTRE_OSC_NAME);
1359         if (type != NULL && type->typ_procroot != NULL)
1360                 return rc;
1361
1362         type = class_search_type(LUSTRE_OSP_NAME);
1363         type->typ_procsym = lprocfs_seq_register("osc", proc_lustre_root,
1364                                                  NULL, NULL);
1365         if (IS_ERR(type->typ_procsym)) {
1366                 CERROR("osp: can't create compat entry \"osc\": %d\n",
1367                        (int) PTR_ERR(type->typ_procsym));
1368                 type->typ_procsym = NULL;
1369         }
1370         return rc;
1371 }
1372
1373 static void __exit osp_mod_exit(void)
1374 {
1375         class_unregister_type(LUSTRE_LWP_NAME);
1376         class_unregister_type(LUSTRE_OSP_NAME);
1377         lu_kmem_fini(osp_caches);
1378 }
1379
1380 MODULE_AUTHOR("Intel, Inc. <http://www.intel.com/>");
1381 MODULE_DESCRIPTION("Lustre OST Proxy Device ("LUSTRE_OSP_NAME")");
1382 MODULE_LICENSE("GPL");
1383
1384 cfs_module(osp, LUSTRE_VERSION_STRING, osp_mod_init, osp_mod_exit);