Whamcloud - gitweb
LU-137 obdclass: add dt_object_put() and use it
[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) 2012, 2016, 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 #include "tgt_internal.h"
38 #include "../ptlrpc/ptlrpc_internal.h"
39
40 /*
41  * Save cross-MDT lock in lut_slc_locks.
42  *
43  * Lock R/W count is not saved, but released in unlock (not canceled remotely),
44  * instead only a refcount is taken, so that the remote MDT where the object
45  * resides can detect conflict with this lock there.
46  *
47  * \param lut target
48  * \param lock cross-MDT lock to save
49  * \param transno when the transaction with this transno is committed, this lock
50  *                can be canceled.
51  */
52 void tgt_save_slc_lock(struct lu_target *lut, struct ldlm_lock *lock,
53                        __u64 transno)
54 {
55         spin_lock(&lut->lut_slc_locks_guard);
56         lock_res_and_lock(lock);
57         if (ldlm_is_cbpending(lock)) {
58                 /* if it was canceld by server, don't save, because remote MDT
59                  * will do Sync-on-Cancel. */
60                 LDLM_LOCK_PUT(lock);
61         } else {
62                 lock->l_transno = transno;
63                 /* if this lock is in the list already, there are two operations
64                  * both use this lock, and save it after use, so for the second
65                  * one, just put the refcount. */
66                 if (list_empty(&lock->l_slc_link))
67                         list_add_tail(&lock->l_slc_link, &lut->lut_slc_locks);
68                 else
69                         LDLM_LOCK_PUT(lock);
70         }
71         unlock_res_and_lock(lock);
72         spin_unlock(&lut->lut_slc_locks_guard);
73 }
74 EXPORT_SYMBOL(tgt_save_slc_lock);
75
76 /*
77  * Discard cross-MDT lock from lut_slc_locks.
78  *
79  * This is called upon BAST, just remove lock from lut_slc_locks and put lock
80  * refcount. The BAST will cancel this lock.
81  *
82  * \param lut target
83  * \param lock cross-MDT lock to discard
84  */
85 void tgt_discard_slc_lock(struct lu_target *lut, struct ldlm_lock *lock)
86 {
87         spin_lock(&lut->lut_slc_locks_guard);
88         lock_res_and_lock(lock);
89         /* may race with tgt_cancel_slc_locks() */
90         if (lock->l_transno != 0) {
91                 LASSERT(!list_empty(&lock->l_slc_link));
92                 LASSERT(ldlm_is_cbpending(lock));
93                 list_del_init(&lock->l_slc_link);
94                 lock->l_transno = 0;
95                 LDLM_LOCK_PUT(lock);
96         }
97         unlock_res_and_lock(lock);
98         spin_unlock(&lut->lut_slc_locks_guard);
99 }
100 EXPORT_SYMBOL(tgt_discard_slc_lock);
101
102 /*
103  * Cancel cross-MDT locks upon transaction commit.
104  *
105  * Remove cross-MDT locks from lut_slc_locks, cancel them and put lock refcount.
106  *
107  * \param lut target
108  * \param transno transaction with this number was committed.
109  */
110 void tgt_cancel_slc_locks(struct lu_target *lut, __u64 transno)
111 {
112         struct ldlm_lock *lock, *next;
113         LIST_HEAD(list);
114         struct lustre_handle lockh;
115
116         spin_lock(&lut->lut_slc_locks_guard);
117         list_for_each_entry_safe(lock, next, &lut->lut_slc_locks,
118                                  l_slc_link) {
119                 lock_res_and_lock(lock);
120                 LASSERT(lock->l_transno != 0);
121                 if (lock->l_transno > transno) {
122                         unlock_res_and_lock(lock);
123                         continue;
124                 }
125                 /* ouch, another operation is using it after it's saved */
126                 if (lock->l_readers != 0 || lock->l_writers != 0) {
127                         unlock_res_and_lock(lock);
128                         continue;
129                 }
130                 /* set CBPENDING so that this lock won't be used again */
131                 ldlm_set_cbpending(lock);
132                 lock->l_transno = 0;
133                 list_move(&lock->l_slc_link, &list);
134                 unlock_res_and_lock(lock);
135         }
136         spin_unlock(&lut->lut_slc_locks_guard);
137
138         list_for_each_entry_safe(lock, next, &list, l_slc_link) {
139                 list_del_init(&lock->l_slc_link);
140                 ldlm_lock2handle(lock, &lockh);
141                 ldlm_cli_cancel(&lockh, LCF_ASYNC);
142                 LDLM_LOCK_PUT(lock);
143         }
144 }
145
146 int tgt_init(const struct lu_env *env, struct lu_target *lut,
147              struct obd_device *obd, struct dt_device *dt,
148              struct tgt_opc_slice *slice, int request_fail_id,
149              int reply_fail_id)
150 {
151         struct dt_object_format  dof;
152         struct lu_attr           attr;
153         struct lu_fid            fid;
154         struct dt_object        *o;
155         int i, rc = 0;
156
157         ENTRY;
158
159         LASSERT(lut);
160         LASSERT(obd);
161         lut->lut_obd = obd;
162         lut->lut_bottom = dt;
163         lut->lut_last_rcvd = NULL;
164         lut->lut_client_bitmap = NULL;
165         atomic_set(&lut->lut_num_clients, 0);
166         atomic_set(&lut->lut_client_generation, 0);
167         lut->lut_reply_data = NULL;
168         lut->lut_reply_bitmap = NULL;
169         obd->u.obt.obt_lut = lut;
170         obd->u.obt.obt_magic = OBT_MAGIC;
171
172         /* set request handler slice and parameters */
173         lut->lut_slice = slice;
174         lut->lut_reply_fail_id = reply_fail_id;
175         lut->lut_request_fail_id = request_fail_id;
176
177         /* sptlrcp variables init */
178         rwlock_init(&lut->lut_sptlrpc_lock);
179         sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
180
181         spin_lock_init(&lut->lut_flags_lock);
182         lut->lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
183
184         spin_lock_init(&lut->lut_slc_locks_guard);
185         INIT_LIST_HEAD(&lut->lut_slc_locks);
186
187         /* last_rcvd initialization is needed by replayable targets only */
188         if (!obd->obd_replayable)
189                 RETURN(0);
190
191         spin_lock_init(&lut->lut_translock);
192         spin_lock_init(&lut->lut_client_bitmap_lock);
193
194         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
195         if (lut->lut_client_bitmap == NULL)
196                 RETURN(-ENOMEM);
197
198         memset(&attr, 0, sizeof(attr));
199         attr.la_valid = LA_MODE;
200         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
201         dof.dof_type = dt_mode_to_dft(S_IFREG);
202
203         lu_local_obj_fid(&fid, LAST_RECV_OID);
204
205         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
206         if (IS_ERR(o)) {
207                 rc = PTR_ERR(o);
208                 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
209                        rc);
210                 GOTO(out_put, rc);
211         }
212
213         lut->lut_last_rcvd = o;
214         rc = tgt_server_data_init(env, lut);
215         if (rc < 0)
216                 GOTO(out_put, rc);
217
218         /* prepare transactions callbacks */
219         lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
220         lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
221         lut->lut_txn_cb.dtc_txn_commit = NULL;
222         lut->lut_txn_cb.dtc_cookie = lut;
223         lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
224         INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
225
226         dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
227         lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
228
229         /* reply_data is supported by MDT targets only for now */
230         if (strncmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) != 0)
231                 RETURN(0);
232
233         OBD_ALLOC(lut->lut_reply_bitmap,
234                   LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
235         if (lut->lut_reply_bitmap == NULL)
236                 GOTO(out, rc = -ENOMEM);
237
238         memset(&attr, 0, sizeof(attr));
239         attr.la_valid = LA_MODE;
240         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
241         dof.dof_type = dt_mode_to_dft(S_IFREG);
242
243         lu_local_obj_fid(&fid, REPLY_DATA_OID);
244
245         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
246         if (IS_ERR(o)) {
247                 rc = PTR_ERR(o);
248                 CERROR("%s: cannot open REPLY_DATA: rc = %d\n", tgt_name(lut),
249                        rc);
250                 GOTO(out, rc);
251         }
252         lut->lut_reply_data = o;
253
254         rc = tgt_reply_data_init(env, lut);
255         if (rc < 0)
256                 GOTO(out, rc);
257
258         atomic_set(&lut->lut_sync_count, 0);
259
260         RETURN(0);
261
262 out:
263         dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
264 out_put:
265         obd->u.obt.obt_magic = 0;
266         obd->u.obt.obt_lut = NULL;
267         if (lut->lut_last_rcvd != NULL) {
268                 dt_object_put(env, lut->lut_last_rcvd);
269                 lut->lut_last_rcvd = NULL;
270         }
271         if (lut->lut_client_bitmap != NULL)
272                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
273         lut->lut_client_bitmap = NULL;
274         if (lut->lut_reply_data != NULL)
275                 dt_object_put(env, lut->lut_reply_data);
276         lut->lut_reply_data = NULL;
277         if (lut->lut_reply_bitmap != NULL) {
278                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
279                         if (lut->lut_reply_bitmap[i] != NULL)
280                                 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
281                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
282                                     sizeof(long));
283                         lut->lut_reply_bitmap[i] = NULL;
284                 }
285                 OBD_FREE(lut->lut_reply_bitmap,
286                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
287         }
288         lut->lut_reply_bitmap = NULL;
289         return rc;
290 }
291 EXPORT_SYMBOL(tgt_init);
292
293 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
294 {
295         int i;
296         int rc;
297         ENTRY;
298
299         if (lut->lut_lsd.lsd_feature_incompat & OBD_INCOMPAT_MULTI_RPCS &&
300             atomic_read(&lut->lut_num_clients) == 0) {
301                 /* Clear MULTI RPCS incompatibility flag that prevents previous
302                  * Lustre versions to mount a target with reply_data file */
303                 lut->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
304                 rc = tgt_server_data_update(env, lut, 1);
305                 if (rc < 0)
306                         CERROR("%s: unable to clear MULTI RPCS "
307                                "incompatibility flag\n",
308                                lut->lut_obd->obd_name);
309         }
310
311         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
312
313         if (lut->lut_reply_data != NULL)
314                 dt_object_put(env, lut->lut_reply_data);
315         lut->lut_reply_data = NULL;
316         if (lut->lut_reply_bitmap != NULL) {
317                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
318                         if (lut->lut_reply_bitmap[i] != NULL)
319                                 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
320                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
321                                     sizeof(long));
322                         lut->lut_reply_bitmap[i] = NULL;
323                 }
324                 OBD_FREE(lut->lut_reply_bitmap,
325                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
326         }
327         lut->lut_reply_bitmap = NULL;
328         if (lut->lut_client_bitmap) {
329                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
330                 lut->lut_client_bitmap = NULL;
331         }
332         if (lut->lut_last_rcvd) {
333                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
334                 dt_object_put(env, lut->lut_last_rcvd);
335                 lut->lut_last_rcvd = NULL;
336         }
337         EXIT;
338 }
339 EXPORT_SYMBOL(tgt_fini);
340
341 /* context key constructor/destructor: tg_key_init, tg_key_fini */
342 LU_KEY_INIT(tgt, struct tgt_thread_info);
343
344 static void tgt_key_fini(const struct lu_context *ctx,
345                          struct lu_context_key *key, void *data)
346 {
347         struct tgt_thread_info          *info = data;
348         struct thandle_exec_args        *args = &info->tti_tea;
349         int                             i;
350
351         for (i = 0; i < args->ta_alloc_args; i++) {
352                 if (args->ta_args[i] != NULL)
353                         OBD_FREE_PTR(args->ta_args[i]);
354         }
355
356         if (args->ta_args != NULL)
357                 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
358                                         args->ta_alloc_args);
359         OBD_FREE_PTR(info);
360 }
361
362 static void tgt_key_exit(const struct lu_context *ctx,
363                          struct lu_context_key *key, void *data)
364 {
365         struct tgt_thread_info *tti = data;
366
367         tti->tti_has_trans = 0;
368         tti->tti_mult_trans = 0;
369 }
370
371 /* context key: tg_thread_key */
372 struct lu_context_key tgt_thread_key = {
373         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
374         .lct_init = tgt_key_init,
375         .lct_fini = tgt_key_fini,
376         .lct_exit = tgt_key_exit,
377 };
378
379 LU_KEY_INIT_GENERIC(tgt);
380
381 /* context key constructor/destructor: tgt_ses_key_init, tgt_ses_key_fini */
382 LU_KEY_INIT_FINI(tgt_ses, struct tgt_session_info);
383
384 /* context key: tgt_session_key */
385 struct lu_context_key tgt_session_key = {
386         .lct_tags = LCT_SERVER_SESSION,
387         .lct_init = tgt_ses_key_init,
388         .lct_fini = tgt_ses_key_fini,
389 };
390 EXPORT_SYMBOL(tgt_session_key);
391
392 LU_KEY_INIT_GENERIC(tgt_ses);
393
394 /*
395  * this page is allocated statically when module is initializing
396  * it is used to simulate data corruptions, see ost_checksum_bulk()
397  * for details. as the original pages provided by the layers below
398  * can be remain in the internal cache, we do not want to modify
399  * them.
400  */
401 struct page *tgt_page_to_corrupt;
402
403 int tgt_mod_init(void)
404 {
405         ENTRY;
406
407         tgt_page_to_corrupt = alloc_page(GFP_KERNEL);
408
409         tgt_key_init_generic(&tgt_thread_key, NULL);
410         lu_context_key_register_many(&tgt_thread_key, NULL);
411
412         tgt_ses_key_init_generic(&tgt_session_key, NULL);
413         lu_context_key_register_many(&tgt_session_key, NULL);
414         barrier_init();
415
416         update_info_init();
417
418         RETURN(0);
419 }
420
421 void tgt_mod_exit(void)
422 {
423         barrier_fini();
424         if (tgt_page_to_corrupt != NULL)
425                 put_page(tgt_page_to_corrupt);
426
427         lu_context_key_degister(&tgt_thread_key);
428         lu_context_key_degister(&tgt_session_key);
429         update_info_fini();
430 }
431