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