Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[fs/lustre-release.git] / lustre / target / tgt_main.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2011, 2012, Intel Corporation.
25  */
26 /*
27  * lustre/target/tgt_main.c
28  *
29  * Lustre Unified Target main initialization code
30  *
31  * Author: Mikhail Pershin <mike.pershin@intel.com>
32  */
33
34 #define DEBUG_SUBSYSTEM S_CLASS
35
36 #include <obd.h>
37
38 #include "tgt_internal.h"
39
40 int tgt_init(const struct lu_env *env, struct lu_target *lut,
41              struct obd_device *obd, struct dt_device *dt)
42 {
43         struct dt_object_format dof;
44         struct lu_attr          attr;
45         struct lu_fid           fid;
46         struct dt_object       *o;
47         int                     rc = 0;
48
49         ENTRY;
50
51         LASSERT(lut);
52         LASSERT(obd);
53         lut->lut_obd = obd;
54         lut->lut_bottom = dt;
55         lut->lut_last_rcvd = NULL;
56         obd->u.obt.obt_lut = lut;
57         obd->u.obt.obt_magic = OBT_MAGIC;
58
59         spin_lock_init(&lut->lut_translock);
60
61         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
62         if (lut->lut_client_bitmap == NULL)
63                 RETURN(-ENOMEM);
64
65         memset(&attr, 0, sizeof(attr));
66         attr.la_valid = LA_MODE;
67         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
68         dof.dof_type = dt_mode_to_dft(S_IFREG);
69
70         lu_local_obj_fid(&fid, MDT_LAST_RECV_OID);
71
72         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
73         if (!IS_ERR(o)) {
74                 lut->lut_last_rcvd = o;
75         } else {
76                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
77                 lut->lut_client_bitmap = NULL;
78                 rc = PTR_ERR(o);
79                 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
80         }
81
82         RETURN(rc);
83 }
84 EXPORT_SYMBOL(tgt_init);
85
86 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
87 {
88         ENTRY;
89
90         if (lut->lut_client_bitmap) {
91                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
92                 lut->lut_client_bitmap = NULL;
93         }
94         if (lut->lut_last_rcvd) {
95                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
96                 lut->lut_last_rcvd = NULL;
97         }
98         EXIT;
99 }
100 EXPORT_SYMBOL(tgt_fini);
101
102 /* context key constructor/destructor: tg_key_init, tg_key_fini */
103 LU_KEY_INIT_FINI(tgt, struct tgt_thread_info);
104
105 /* context key: tg_thread_key */
106 LU_CONTEXT_KEY_DEFINE(tgt, LCT_MD_THREAD | LCT_DT_THREAD);
107 EXPORT_SYMBOL(tgt_thread_key);
108
109 LU_KEY_INIT_GENERIC(tg);
110
111 int tgt_mod_init(void)
112 {
113         tg_key_init_generic(&tgt_thread_key, NULL);
114         lu_context_key_register_many(&tgt_thread_key, NULL);
115         return 0;
116 }
117
118 void tgt_mod_exit(void)
119 {
120         lu_context_key_degister(&tgt_thread_key);
121 }
122