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