Whamcloud - gitweb
LU-2679 grant: OFD grant as client requested upon reconnect
[fs/lustre-release.git] / lustre / ofd / ofd_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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 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/ofd/ofd.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include <obd_class.h>
46 #include <lustre_param.h>
47 #include <lustre_fid.h>
48
49 #include "ofd_internal.h"
50
51 /* Slab for OFD object allocation */
52 static cfs_mem_cache_t *ofd_object_kmem;
53
54 static struct lu_kmem_descr ofd_caches[] = {
55         {
56                 .ckd_cache = &ofd_object_kmem,
57                 .ckd_name  = "ofd_obj",
58                 .ckd_size  = sizeof(struct ofd_object)
59         },
60         {
61                 .ckd_cache = NULL
62         }
63 };
64
65 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
66                                const char *next, struct obd_export **exp)
67 {
68         struct obd_connect_data *data = NULL;
69         struct obd_device       *obd;
70         int                      rc;
71         ENTRY;
72
73         OBD_ALLOC_PTR(data);
74         if (data == NULL)
75                 GOTO(out, rc = -ENOMEM);
76
77         obd = class_name2obd(next);
78         if (obd == NULL) {
79                 CERROR("%s: can't locate next device: %s\n",
80                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next);
81                 GOTO(out, rc = -ENOTCONN);
82         }
83
84         data->ocd_connect_flags = OBD_CONNECT_VERSION;
85         data->ocd_version = LUSTRE_VERSION_CODE;
86
87         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
88         if (rc) {
89                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
90                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next, rc);
91                 GOTO(out, rc);
92         }
93
94         m->ofd_dt_dev.dd_lu_dev.ld_site =
95                 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
96         LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
97         m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
98         m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
99
100 out:
101         if (data)
102                 OBD_FREE_PTR(data);
103         RETURN(rc);
104 }
105
106 static int ofd_stack_init(const struct lu_env *env,
107                           struct ofd_device *m, struct lustre_cfg *cfg)
108 {
109         const char              *dev = lustre_cfg_string(cfg, 0);
110         struct lu_device        *d;
111         struct ofd_thread_info  *info = ofd_info(env);
112         struct lustre_mount_info *lmi;
113         int                      rc;
114         char                    *osdname;
115
116         ENTRY;
117
118         lmi = server_get_mount(dev);
119         if (lmi == NULL) {
120                 CERROR("Cannot get mount info for %s!\n", dev);
121                 RETURN(-ENODEV);
122         }
123
124         /* find bottom osd */
125         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
126         if (osdname == NULL)
127                 RETURN(-ENOMEM);
128
129         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
130         rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
131         OBD_FREE(osdname, MTI_NAME_MAXLEN);
132         if (rc)
133                 RETURN(rc);
134
135         d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
136         LASSERT(d);
137         m->ofd_osd = lu2dt_dev(d);
138
139         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
140                  "%s-osd", lustre_cfg_string(cfg, 0));
141
142         RETURN(rc);
143 }
144
145 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
146                            struct lu_device *top)
147 {
148         struct obd_device       *obd = ofd_obd(m);
149         struct lustre_cfg_bufs   bufs;
150         struct lustre_cfg       *lcfg;
151         char                     flags[3] = "";
152
153         ENTRY;
154
155         lu_site_purge(env, top->ld_site, ~0);
156
157         /* process cleanup, pass mdt obd name to get obd umount flags */
158         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
159         if (obd->obd_force)
160                 strcat(flags, "F");
161         if (obd->obd_fail)
162                 strcat(flags, "A");
163         lustre_cfg_bufs_set_string(&bufs, 1, flags);
164         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
165         if (!lcfg) {
166                 CERROR("Cannot alloc lcfg!\n");
167                 RETURN_EXIT;
168         }
169
170         LASSERT(top);
171         top->ld_ops->ldo_process_config(env, top, lcfg);
172         lustre_cfg_free(lcfg);
173
174         lu_site_purge(env, top->ld_site, ~0);
175
176         LASSERT(m->ofd_osd_exp);
177         obd_disconnect(m->ofd_osd_exp);
178         m->ofd_osd = NULL;
179
180         EXIT;
181 }
182
183 /* For interoperability, see mdt_interop_param[]. */
184 static struct cfg_interop_param ofd_interop_param[] = {
185         { "ost.quota_type",     NULL },
186         { NULL }
187 };
188
189 /* used by MGS to process specific configurations */
190 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
191                               struct lustre_cfg *cfg)
192 {
193         struct ofd_device       *m = ofd_dev(d);
194         struct dt_device        *dt_next = m->ofd_osd;
195         struct lu_device        *next = &dt_next->dd_lu_dev;
196         int                      rc;
197
198         ENTRY;
199
200         switch (cfg->lcfg_command) {
201         case LCFG_PARAM: {
202                 struct lprocfs_static_vars lvars;
203
204                 /* For interoperability */
205                 struct cfg_interop_param   *ptr = NULL;
206                 struct lustre_cfg          *old_cfg = NULL;
207                 char                       *param = NULL;
208
209                 param = lustre_cfg_string(cfg, 1);
210                 if (param == NULL) {
211                         CERROR("param is empty\n");
212                         rc = -EINVAL;
213                         break;
214                 }
215
216                 ptr = class_find_old_param(param, ofd_interop_param);
217                 if (ptr != NULL) {
218                         if (ptr->new_param == NULL) {
219                                 rc = 0;
220                                 CWARN("For interoperability, skip this %s."
221                                       " It is obsolete.\n", ptr->old_param);
222                                 break;
223                         }
224
225                         CWARN("Found old param %s, changed it to %s.\n",
226                               ptr->old_param, ptr->new_param);
227
228                         old_cfg = cfg;
229                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
230                         if (IS_ERR(cfg)) {
231                                 rc = PTR_ERR(cfg);
232                                 break;
233                         }
234                 }
235
236                 lprocfs_ofd_init_vars(&lvars);
237                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
238                                               d->ld_obd);
239                 if (rc > 0 || rc == -ENOSYS)
240                         /* we don't understand; pass it on */
241                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
242                 break;
243         }
244         case LCFG_SPTLRPC_CONF: {
245                 rc = -ENOTSUPP;
246                 break;
247         }
248         default:
249                 /* others are passed further */
250                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
251                 break;
252         }
253         RETURN(rc);
254 }
255
256 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
257                            const struct lu_object_conf *conf)
258 {
259         struct ofd_device       *d = ofd_dev(o->lo_dev);
260         struct lu_device        *under;
261         struct lu_object        *below;
262         int                      rc = 0;
263
264         ENTRY;
265
266         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
267                PFID(lu_object_fid(o)));
268
269         under = &d->ofd_osd->dd_lu_dev;
270         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
271         if (below != NULL)
272                 lu_object_add(o, below);
273         else
274                 rc = -ENOMEM;
275
276         RETURN(rc);
277 }
278
279 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
280 {
281         struct ofd_object       *of = ofd_obj(o);
282         struct lu_object_header *h;
283
284         ENTRY;
285
286         h = o->lo_header;
287         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
288                PFID(lu_object_fid(o)));
289
290         lu_object_fini(o);
291         lu_object_header_fini(h);
292         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
293         EXIT;
294 }
295
296 static int ofd_object_print(const struct lu_env *env, void *cookie,
297                             lu_printer_t p, const struct lu_object *o)
298 {
299         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
300 }
301
302 struct lu_object_operations ofd_obj_ops = {
303         .loo_object_init        = ofd_object_init,
304         .loo_object_free        = ofd_object_free,
305         .loo_object_print       = ofd_object_print
306 };
307
308 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
309                                           const struct lu_object_header *hdr,
310                                           struct lu_device *d)
311 {
312         struct ofd_object *of;
313
314         ENTRY;
315
316         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
317         if (of != NULL) {
318                 struct lu_object        *o;
319                 struct lu_object_header *h;
320
321                 o = &of->ofo_obj.do_lu;
322                 h = &of->ofo_header;
323                 lu_object_header_init(h);
324                 lu_object_init(o, h, d);
325                 lu_object_add_top(h, o);
326                 o->lo_ops = &ofd_obj_ops;
327                 RETURN(o);
328         } else {
329                 RETURN(NULL);
330         }
331 }
332
333 extern int ost_handle(struct ptlrpc_request *req);
334
335 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
336                        struct lu_device *dev)
337 {
338         struct ofd_thread_info *info;
339         struct ofd_device       *ofd = ofd_dev(dev);
340         struct obd_device       *obd = ofd_obd(ofd);
341         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
342         int                      rc;
343
344         ENTRY;
345
346         rc = lu_env_refill((struct lu_env *)env);
347         if (rc != 0) {
348                 CERROR("Failure to refill session: '%d'\n", rc);
349                 RETURN(rc);
350         }
351
352         info = ofd_info_init(env, NULL);
353         if (info == NULL)
354                 RETURN(-EFAULT);
355
356         /* initialize lower device */
357         rc = next->ld_ops->ldo_prepare(env, dev, next);
358
359         target_recovery_init(&ofd->ofd_lut, ost_handle);
360         LASSERT(obd->obd_no_conn);
361         spin_lock(&obd->obd_dev_lock);
362         obd->obd_no_conn = 0;
363         spin_unlock(&obd->obd_dev_lock);
364
365         if (obd->obd_recovering == 0)
366                 ofd_postrecov(env, ofd);
367
368         RETURN(rc);
369 }
370
371 static int ofd_recovery_complete(const struct lu_env *env,
372                                  struct lu_device *dev)
373 {
374         struct ofd_device       *ofd = ofd_dev(dev);
375         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
376         int                      rc = 0;
377
378         ENTRY;
379
380         /* Grant space for object precreation on the self export.
381          * This initial reserved space (i.e. 20MB for zfs and 560KB for ldiskfs)
382          * is enough to create 20k objects. It is then adapted based on the
383          * precreate request size (see ofd_grant_create()
384          */
385         ofd_grant_connect(env, dev->ld_obd->obd_self_export,
386                           OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace,
387                           false);
388         rc = next->ld_ops->ldo_recovery_complete(env, next);
389         RETURN(rc);
390 }
391
392 static struct lu_device_operations ofd_lu_ops = {
393         .ldo_object_alloc       = ofd_object_alloc,
394         .ldo_process_config     = ofd_process_config,
395         .ldo_recovery_complete  = ofd_recovery_complete,
396         .ldo_prepare            = ofd_prepare,
397 };
398
399 static int ofd_procfs_init(struct ofd_device *ofd)
400 {
401         struct lprocfs_static_vars       lvars;
402         struct obd_device               *obd = ofd_obd(ofd);
403         cfs_proc_dir_entry_t            *entry;
404         int                              rc = 0;
405
406         ENTRY;
407
408         /* lprocfs must be setup before the ofd so state can be safely added
409          * to /proc incrementally as the ofd is setup */
410         lprocfs_ofd_init_vars(&lvars);
411         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
412         if (rc) {
413                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
414                        obd->obd_name, rc);
415                 RETURN(rc);
416         }
417
418         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
419         if (rc) {
420                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
421                        obd->obd_name, rc);
422                 GOTO(obd_cleanup, rc);
423         }
424
425         /* Init OFD private stats here */
426         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
427                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
428         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
429                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
430
431         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
432         if (IS_ERR(entry)) {
433                 rc = PTR_ERR(entry);
434                 CERROR("%s: error %d setting up lprocfs for %s\n",
435                        obd->obd_name, rc, "exports");
436                 GOTO(obd_cleanup, rc);
437         }
438         obd->obd_proc_exports_entry = entry;
439
440         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
441                                    lprocfs_nid_stats_clear_read,
442                                    lprocfs_nid_stats_clear_write, obd, NULL);
443         if (IS_ERR(entry)) {
444                 rc = PTR_ERR(entry);
445                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
446                        obd->obd_name, rc);
447                 GOTO(obd_cleanup, rc);
448         }
449
450         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
451                                     ofd_stats_counter_init);
452         if (rc)
453                 GOTO(remove_entry_clear, rc);
454         RETURN(0);
455 remove_entry_clear:
456         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
457 obd_cleanup:
458         lprocfs_obd_cleanup(obd);
459         lprocfs_free_obd_stats(obd);
460
461         return rc;
462 }
463
464 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
465 {
466         struct obd_device       *obd = ofd_obd(ofd);
467         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
468         cfs_proc_dir_entry_t    *osd_root = osd_obd->obd_type->typ_procroot;
469         cfs_proc_dir_entry_t    *osd_dir;
470
471         osd_dir = lprocfs_srch(osd_root, obd->obd_name);
472         if (osd_dir == NULL)
473                 return;
474
475         if (lprocfs_srch(osd_dir, "brw_stats") == NULL)
476                 return;
477
478         lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
479                             "../../%s/%s/brw_stats",
480                             osd_root->name, osd_dir->name);
481 }
482
483 static void ofd_procfs_fini(struct ofd_device *ofd)
484 {
485         struct obd_device *obd = ofd_obd(ofd);
486
487         lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
488         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
489         lprocfs_free_per_client_stats(obd);
490         lprocfs_obd_cleanup(obd);
491         lprocfs_free_obd_stats(obd);
492         lprocfs_job_stats_fini(obd);
493 }
494
495 extern int ost_handle(struct ptlrpc_request *req);
496
497 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
498 {
499         return seq_site_fini(env, &ofd->ofd_seq_site);
500 }
501
502 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
503 {
504         struct seq_server_site  *ss = &ofd->ofd_seq_site;
505         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
506         char                    *obd_name = ofd_name(ofd);
507         char                    *name = NULL;
508         int                     rc = 0;
509
510         ss = &ofd->ofd_seq_site;
511         lu->ld_site->ld_seq_site = ss;
512         ss->ss_lu = lu->ld_site;
513         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
514
515         OBD_ALLOC_PTR(ss->ss_server_seq);
516         if (ss->ss_server_seq == NULL)
517                 GOTO(out_free, rc = -ENOMEM);
518
519         OBD_ALLOC(name, strlen(obd_name) + 10);
520         if (!name) {
521                 OBD_FREE_PTR(ss->ss_server_seq);
522                 ss->ss_server_seq = NULL;
523                 GOTO(out_free, rc = -ENOMEM);
524         }
525
526         rc = seq_server_init(ss->ss_server_seq, ofd->ofd_osd, obd_name,
527                              LUSTRE_SEQ_SERVER, ss, env);
528         if (rc) {
529                 CERROR("%s : seq server init error %d\n", obd_name, rc);
530                 GOTO(out_free, rc);
531         }
532         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
533
534         OBD_ALLOC_PTR(ss->ss_client_seq);
535         if (ss->ss_client_seq == NULL)
536                 GOTO(out_free, -ENOMEM);
537
538         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
539         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
540                              name, NULL);
541         if (rc) {
542                 CERROR("%s : seq client init error %d\n", obd_name, rc);
543                 GOTO(out_free, rc);
544         }
545         OBD_FREE(name, strlen(obd_name) + 10);
546         name = NULL;
547
548         rc = seq_server_set_cli(ss->ss_server_seq, ss->ss_client_seq, env);
549
550 out_free:
551         if (rc) {
552                 if (ss->ss_server_seq) {
553                         seq_server_fini(ss->ss_server_seq, env);
554                         OBD_FREE_PTR(ss->ss_server_seq);
555                         ss->ss_server_seq = NULL;
556                 }
557
558                 if (ss->ss_client_seq) {
559                         seq_client_fini(ss->ss_client_seq);
560                         OBD_FREE_PTR(ss->ss_client_seq);
561                         ss->ss_client_seq = NULL;
562                 }
563
564                 if (name) {
565                         OBD_FREE(name, strlen(obd_name) + 10);
566                         name = NULL;
567                 }
568         }
569
570         return rc;
571 }
572
573 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
574                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
575 {
576         const char              *dev = lustre_cfg_string(cfg, 0);
577         struct ofd_thread_info  *info = NULL;
578         struct obd_device       *obd;
579         struct obd_statfs       *osfs;
580         int                      rc;
581
582         ENTRY;
583
584         obd = class_name2obd(dev);
585         if (obd == NULL) {
586                 CERROR("Cannot find obd with name %s\n", dev);
587                 RETURN(-ENODEV);
588         }
589
590         rc = lu_env_refill((struct lu_env *)env);
591         if (rc != 0)
592                 RETURN(rc);
593
594         obd->u.obt.obt_magic = OBT_MAGIC;
595
596         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
597         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
598
599         spin_lock_init(&m->ofd_flags_lock);
600         m->ofd_raid_degraded = 0;
601         m->ofd_syncjournal = 0;
602         ofd_slc_set(m);
603         m->ofd_grant_compat_disable = 0;
604
605         /* statfs data */
606         spin_lock_init(&m->ofd_osfs_lock);
607         m->ofd_osfs_age = cfs_time_shift_64(-1000);
608         m->ofd_osfs_unstable = 0;
609         m->ofd_statfs_inflight = 0;
610         m->ofd_osfs_inflight = 0;
611
612         /* grant data */
613         spin_lock_init(&m->ofd_grant_lock);
614         m->ofd_tot_dirty = 0;
615         m->ofd_tot_granted = 0;
616         m->ofd_tot_pending = 0;
617         m->ofd_seq_count = 0;
618
619         spin_lock_init(&m->ofd_batch_lock);
620         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
621         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
622
623         obd->u.filter.fo_fl_oss_capa = 0;
624         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
625         obd->u.filter.fo_capa_hash = init_capa_hash();
626         if (obd->u.filter.fo_capa_hash == NULL)
627                 RETURN(-ENOMEM);
628
629         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
630         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
631         /* set this lu_device to obd, because error handling need it */
632         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
633
634         rc = ofd_procfs_init(m);
635         if (rc) {
636                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
637                 RETURN(rc);
638         }
639
640         /* No connection accepted until configurations will finish */
641         spin_lock(&obd->obd_dev_lock);
642         obd->obd_no_conn = 1;
643         spin_unlock(&obd->obd_dev_lock);
644         obd->obd_replayable = 1;
645         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
646                 char *str = lustre_cfg_string(cfg, 4);
647
648                 if (strchr(str, 'n')) {
649                         CWARN("%s: recovery disabled\n", obd->obd_name);
650                         obd->obd_replayable = 0;
651                 }
652         }
653
654         info = ofd_info_init(env, NULL);
655         if (info == NULL)
656                 RETURN(-EFAULT);
657
658         rc = ofd_stack_init(env, m, cfg);
659         if (rc) {
660                 CERROR("Can't init device stack, rc %d\n", rc);
661                 GOTO(err_fini_proc, rc);
662         }
663
664         ofd_procfs_add_brw_stats_symlink(m);
665
666         /* populate cached statfs data */
667         osfs = &ofd_info(env)->fti_u.osfs;
668         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
669         if (rc != 0) {
670                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
671                 GOTO(err_fini_stack, rc);
672         }
673         if (!IS_PO2(osfs->os_bsize)) {
674                 CERROR("%s: blocksize (%d) is not a power of 2\n",
675                                 obd->obd_name, osfs->os_bsize);
676                 GOTO(err_fini_stack, rc = -EPROTO);
677         }
678         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
679
680         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
681         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
682                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
683
684         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
685                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
686         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
687                                               LDLM_NAMESPACE_SERVER,
688                                               LDLM_NAMESPACE_GREEDY,
689                                               LDLM_NS_TYPE_OST);
690         if (m->ofd_namespace == NULL)
691                 GOTO(err_fini_stack, rc = -ENOMEM);
692         /* set obd_namespace for compatibility with old code */
693         obd->obd_namespace = m->ofd_namespace;
694         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
695         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
696         m->ofd_namespace->ns_lvbp = m;
697
698         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
699                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
700
701         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
702
703         /* Allow at most ddp_grant_reserved% of the available filesystem space
704          * to be granted to clients, so that any errors in the grant overhead
705          * calculations do not allow granting more space to clients than can be
706          * written. Assumes that in aggregate the grant overhead calculations do
707          * not have more than ddp_grant_reserved% estimation error in them. */
708         m->ofd_grant_ratio =
709                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
710
711         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd);
712         if (rc)
713                 GOTO(err_free_ns, rc);
714
715         rc = ofd_fs_setup(env, m, obd);
716         if (rc)
717                 GOTO(err_fini_lut, rc);
718
719         RETURN(0);
720 err_fini_lut:
721         tgt_fini(env, &m->ofd_lut);
722 err_free_ns:
723         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
724         obd->obd_namespace = m->ofd_namespace = NULL;
725 err_fini_stack:
726         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
727 err_fini_proc:
728         ofd_procfs_fini(m);
729         return rc;
730 }
731
732 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
733 {
734         struct obd_device *obd = ofd_obd(m);
735         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
736
737         target_recovery_fini(obd);
738         obd_exports_barrier(obd);
739         obd_zombie_barrier();
740
741         tgt_fini(env, &m->ofd_lut);
742         ofd_fs_cleanup(env, m);
743
744         ofd_free_capa_keys(m);
745         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
746
747         if (m->ofd_namespace != NULL) {
748                 ldlm_namespace_free(m->ofd_namespace, NULL,
749                                     d->ld_obd->obd_force);
750                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
751         }
752
753         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
754         ofd_procfs_fini(m);
755         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
756         server_put_mount(obd->obd_name, NULL);
757         EXIT;
758 }
759
760 static struct lu_device *ofd_device_fini(const struct lu_env *env,
761                                          struct lu_device *d)
762 {
763         ENTRY;
764         ofd_fini(env, ofd_dev(d));
765         RETURN(NULL);
766 }
767
768 static struct lu_device *ofd_device_free(const struct lu_env *env,
769                                          struct lu_device *d)
770 {
771         struct ofd_device *m = ofd_dev(d);
772
773         dt_device_fini(&m->ofd_dt_dev);
774         OBD_FREE_PTR(m);
775         RETURN(NULL);
776 }
777
778 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
779                                           struct lu_device_type *t,
780                                           struct lustre_cfg *cfg)
781 {
782         struct ofd_device *m;
783         struct lu_device  *l;
784         int                rc;
785
786         OBD_ALLOC_PTR(m);
787         if (m == NULL)
788                 return ERR_PTR(-ENOMEM);
789
790         l = &m->ofd_dt_dev.dd_lu_dev;
791         dt_device_init(&m->ofd_dt_dev, t);
792         rc = ofd_init0(env, m, t, cfg);
793         if (rc != 0) {
794                 ofd_device_free(env, l);
795                 l = ERR_PTR(rc);
796         }
797
798         return l;
799 }
800
801 /* thread context key constructor/destructor */
802 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
803
804 static void ofd_key_exit(const struct lu_context *ctx,
805                          struct lu_context_key *key, void *data)
806 {
807         struct ofd_thread_info *info = data;
808
809         info->fti_env = NULL;
810         info->fti_exp = NULL;
811
812         info->fti_xid = 0;
813         info->fti_transno = 0;
814         info->fti_pre_version = 0;
815         info->fti_obj = NULL;
816         info->fti_has_trans = 0;
817         info->fti_mult_trans = 0;
818         info->fti_used = 0;
819
820         memset(&info->fti_attr, 0, sizeof info->fti_attr);
821 }
822
823 struct lu_context_key ofd_thread_key = {
824         .lct_tags = LCT_DT_THREAD,
825         .lct_init = ofd_key_init,
826         .lct_fini = ofd_key_fini,
827         .lct_exit = ofd_key_exit
828 };
829
830 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
831 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
832
833 static struct lu_device_type_operations ofd_device_type_ops = {
834         .ldto_init              = ofd_type_init,
835         .ldto_fini              = ofd_type_fini,
836
837         .ldto_start             = ofd_type_start,
838         .ldto_stop              = ofd_type_stop,
839
840         .ldto_device_alloc      = ofd_device_alloc,
841         .ldto_device_free       = ofd_device_free,
842         .ldto_device_fini       = ofd_device_fini
843 };
844
845 static struct lu_device_type ofd_device_type = {
846         .ldt_tags       = LU_DEVICE_DT,
847         .ldt_name       = LUSTRE_OST_NAME,
848         .ldt_ops        = &ofd_device_type_ops,
849         .ldt_ctx_tags   = LCT_DT_THREAD
850 };
851
852 int __init ofd_init(void)
853 {
854         struct lprocfs_static_vars      lvars;
855         int                             rc;
856
857         rc = lu_kmem_init(ofd_caches);
858         if (rc)
859                 return rc;
860
861         rc = ofd_fmd_init();
862         if (rc) {
863                 lu_kmem_fini(ofd_caches);
864                 return(rc);
865         }
866
867         lprocfs_ofd_init_vars(&lvars);
868
869         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
870                                  LUSTRE_OST_NAME, &ofd_device_type);
871         return rc;
872 }
873
874 void __exit ofd_exit(void)
875 {
876         ofd_fmd_exit();
877         lu_kmem_fini(ofd_caches);
878         class_unregister_type(LUSTRE_OST_NAME);
879 }
880
881 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
882 MODULE_DESCRIPTION("Lustre Object Filtering Device");
883 MODULE_LICENSE("GPL");
884
885 module_init(ofd_init);
886 module_exit(ofd_exit);