Whamcloud - gitweb
ef02f05607564909a8fc8cf7fa715ed9474f3112
[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, 2017, 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 /* This must be longer than the longest string below */
41 #define SYNC_STATES_MAXLEN 16
42 static char *sync_on_cancel_states[] = {"never",
43                                         "blocking",
44                                         "always" };
45
46 /**
47  * Show policy for handling dirty data under a lock being cancelled.
48  *
49  * \param[in] kobj      sysfs kobject
50  * \param[in] attr      sysfs attribute
51  * \param[in] buf       buffer for data
52  *
53  * \retval              0 and buffer filled with data on success
54  * \retval              negative value on error
55  */
56 ssize_t sync_lock_cancel_show(struct kobject *kobj,
57                               struct attribute *attr, char *buf)
58 {
59         struct obd_device *obd = container_of(kobj, struct obd_device,
60                                               obd_kset.kobj);
61         struct lu_target *tgt = obd->u.obt.obt_lut;
62
63         return sprintf(buf, "%s\n",
64                        sync_on_cancel_states[tgt->lut_sync_lock_cancel]);
65 }
66 EXPORT_SYMBOL(sync_lock_cancel_show);
67
68 /**
69  * Change policy for handling dirty data under a lock being cancelled.
70  *
71  * This variable defines what action target takes upon lock cancel
72  * There are three possible modes:
73  * 1) never - never do sync upon lock cancel. This can lead to data
74  *    inconsistencies if both the OST and client crash while writing a file
75  *    that is also concurrently being read by another client. In these cases,
76  *    this may allow the file data to "rewind" to an earlier state.
77  * 2) blocking - do sync only if there is blocking lock, e.g. if another
78  *    client is trying to access this same object
79  * 3) always - do sync always
80  *
81  * \param[in] kobj      kobject
82  * \param[in] attr      attribute to show
83  * \param[in] buf       buffer for data
84  * \param[in] count     buffer size
85  *
86  * \retval              \a count on success
87  * \retval              negative value on error
88  */
89 ssize_t sync_lock_cancel_store(struct kobject *kobj, struct attribute *attr,
90                                const char *buffer, size_t count)
91 {
92         struct obd_device *obd = container_of(kobj, struct obd_device,
93                                               obd_kset.kobj);
94         struct lu_target *tgt = obd->u.obt.obt_lut;
95         int val = -1;
96         int i;
97
98         if (count == 0 || count >= SYNC_STATES_MAXLEN)
99                 return -EINVAL;
100
101         for (i = 0 ; i < NUM_SYNC_ON_CANCEL_STATES; i++) {
102                 if (strcmp(buffer, sync_on_cancel_states[i]) == 0) {
103                         val = i;
104                         break;
105                 }
106         }
107
108         /* Legacy numeric codes */
109         if (val == -1) {
110                 int rc = kstrtoint(buffer, 0, &val);
111                 if (rc)
112                         return rc;
113         }
114
115         if (val < 0 || val > 2)
116                 return -EINVAL;
117
118         spin_lock(&tgt->lut_flags_lock);
119         tgt->lut_sync_lock_cancel = val;
120         spin_unlock(&tgt->lut_flags_lock);
121         return count;
122 }
123 EXPORT_SYMBOL(sync_lock_cancel_store);
124 LUSTRE_RW_ATTR(sync_lock_cancel);
125
126 /**
127  * Show maximum number of Filter Modification Data (FMD) maintained.
128  *
129  * \param[in] kobj      kobject
130  * \param[in] attr      attribute to show
131  * \param[in] buf       buffer for data
132  *
133  * \retval              0 and buffer filled with data on success
134  * \retval              negative value on error
135  */
136 ssize_t tgt_fmd_count_show(struct kobject *kobj, struct attribute *attr,
137                            char *buf)
138 {
139         struct obd_device *obd = container_of(kobj, struct obd_device,
140                                               obd_kset.kobj);
141         struct lu_target *lut = obd->u.obt.obt_lut;
142
143         return sprintf(buf, "%u\n", lut->lut_fmd_max_num);
144 }
145
146 /**
147  * Change number of FMDs maintained by target.
148  *
149  * This defines how large the list of FMDs can be.
150  *
151  * \param[in] kobj      kobject
152  * \param[in] attr      attribute to show
153  * \param[in] buf       buffer for data
154  * \param[in] count     buffer size
155  *
156  * \retval              \a count on success
157  * \retval              negative value on error
158  */
159 ssize_t tgt_fmd_count_store(struct kobject *kobj, struct attribute *attr,
160                             const char *buffer, size_t count)
161 {
162         struct obd_device *obd = container_of(kobj, struct obd_device,
163                                               obd_kset.kobj);
164         struct lu_target *lut = obd->u.obt.obt_lut;
165         int val, rc;
166
167         rc = kstrtoint(buffer, 0, &val);
168         if (rc)
169                 return rc;
170
171         if (val < 1 || val > 65536)
172                 return -EINVAL;
173
174         lut->lut_fmd_max_num = val;
175
176         return count;
177 }
178 LUSTRE_RW_ATTR(tgt_fmd_count);
179
180 /**
181  * Show the maximum age of FMD data in seconds.
182  *
183  * \param[in] kobj      kobject
184  * \param[in] attr      attribute to show
185  * \param[in] buf       buffer for data
186  *
187  * \retval              0 and buffer filled with data on success
188  * \retval              negative value on error
189  */
190 ssize_t tgt_fmd_seconds_show(struct kobject *kobj, struct attribute *attr,
191                              char *buf)
192 {
193         struct obd_device *obd = container_of(kobj, struct obd_device,
194                                               obd_kset.kobj);
195         struct lu_target *lut = obd->u.obt.obt_lut;
196
197         return sprintf(buf, "%lld\n", lut->lut_fmd_max_age);
198 }
199
200 /**
201  * Set the maximum age of FMD data in seconds.
202  *
203  * This defines how long FMD data stays in the FMD list.
204  *
205  * \param[in] kobj      kobject
206  * \param[in] attr      attribute to show
207  * \param[in] buf       buffer for data
208  * \param[in] count     buffer size
209  *
210  * \retval              \a count on success
211  * \retval              negative number on error
212  */
213 ssize_t tgt_fmd_seconds_store(struct kobject *kobj, struct attribute *attr,
214                               const char *buffer, size_t count)
215 {
216         struct obd_device *obd = container_of(kobj, struct obd_device,
217                                               obd_kset.kobj);
218         struct lu_target *lut = obd->u.obt.obt_lut;
219         time64_t val;
220         int rc;
221
222         rc = kstrtoll(buffer, 0, &val);
223         if (rc)
224                 return rc;
225
226         if (val < 1 || val > 65536) /* ~ 18 hour max */
227                 return -EINVAL;
228
229         lut->lut_fmd_max_age = val;
230
231         return count;
232 }
233 LUSTRE_RW_ATTR(tgt_fmd_seconds);
234
235 /* These two aliases are old names and kept for compatibility, they were
236  * changed to 'tgt_fmd_count' and 'tgt_fmd_seconds'.
237  * This change was made in Lustre 2.13, so these aliases can be removed
238  * when back compatibility is not needed with any Lustre version prior 2.13
239  */
240 static struct lustre_attr tgt_fmd_count_compat = __ATTR(client_cache_count,
241                         0644, tgt_fmd_count_show, tgt_fmd_count_store);
242 static struct lustre_attr tgt_fmd_seconds_compat = __ATTR(client_cache_seconds,
243                         0644, tgt_fmd_seconds_show, tgt_fmd_seconds_store);
244
245 static const struct attribute *tgt_attrs[] = {
246         &lustre_attr_sync_lock_cancel.attr,
247         &lustre_attr_tgt_fmd_count.attr,
248         &lustre_attr_tgt_fmd_seconds.attr,
249         &tgt_fmd_count_compat.attr,
250         &tgt_fmd_seconds_compat.attr,
251         NULL,
252 };
253
254 int tgt_tunables_init(struct lu_target *lut)
255 {
256         int rc;
257
258         rc = sysfs_create_files(&lut->lut_obd->obd_kset.kobj, tgt_attrs);
259         if (!rc)
260                 lut->lut_attrs = tgt_attrs;
261         return rc;
262 }
263 EXPORT_SYMBOL(tgt_tunables_init);
264
265 void tgt_tunables_fini(struct lu_target *lut)
266 {
267         if (lut->lut_attrs) {
268                 sysfs_remove_files(&lut->lut_obd->obd_kset.kobj,
269                                    lut->lut_attrs);
270                 lut->lut_attrs = NULL;
271         }
272 }
273 EXPORT_SYMBOL(tgt_tunables_fini);
274
275 /*
276  * Save cross-MDT lock in lut_slc_locks.
277  *
278  * Lock R/W count is not saved, but released in unlock (not canceled remotely),
279  * instead only a refcount is taken, so that the remote MDT where the object
280  * resides can detect conflict with this lock there.
281  *
282  * \param lut target
283  * \param lock cross-MDT lock to save
284  * \param transno when the transaction with this transno is committed, this lock
285  *                can be canceled.
286  */
287 void tgt_save_slc_lock(struct lu_target *lut, struct ldlm_lock *lock,
288                        __u64 transno)
289 {
290         spin_lock(&lut->lut_slc_locks_guard);
291         lock_res_and_lock(lock);
292         if (ldlm_is_cbpending(lock)) {
293                 /* if it was canceld by server, don't save, because remote MDT
294                  * will do Sync-on-Cancel. */
295                 LDLM_LOCK_PUT(lock);
296         } else {
297                 lock->l_transno = transno;
298                 /* if this lock is in the list already, there are two operations
299                  * both use this lock, and save it after use, so for the second
300                  * one, just put the refcount. */
301                 if (list_empty(&lock->l_slc_link))
302                         list_add_tail(&lock->l_slc_link, &lut->lut_slc_locks);
303                 else
304                         LDLM_LOCK_PUT(lock);
305         }
306         unlock_res_and_lock(lock);
307         spin_unlock(&lut->lut_slc_locks_guard);
308 }
309 EXPORT_SYMBOL(tgt_save_slc_lock);
310
311 /*
312  * Discard cross-MDT lock from lut_slc_locks.
313  *
314  * This is called upon BAST, just remove lock from lut_slc_locks and put lock
315  * refcount. The BAST will cancel this lock.
316  *
317  * \param lut target
318  * \param lock cross-MDT lock to discard
319  */
320 void tgt_discard_slc_lock(struct lu_target *lut, struct ldlm_lock *lock)
321 {
322         spin_lock(&lut->lut_slc_locks_guard);
323         lock_res_and_lock(lock);
324         /* may race with tgt_cancel_slc_locks() */
325         if (lock->l_transno != 0) {
326                 LASSERT(!list_empty(&lock->l_slc_link));
327                 LASSERT(ldlm_is_cbpending(lock));
328                 list_del_init(&lock->l_slc_link);
329                 lock->l_transno = 0;
330                 LDLM_LOCK_PUT(lock);
331         }
332         unlock_res_and_lock(lock);
333         spin_unlock(&lut->lut_slc_locks_guard);
334 }
335 EXPORT_SYMBOL(tgt_discard_slc_lock);
336
337 /*
338  * Cancel cross-MDT locks upon transaction commit.
339  *
340  * Remove cross-MDT locks from lut_slc_locks, cancel them and put lock refcount.
341  *
342  * \param lut target
343  * \param transno transaction with this number was committed.
344  */
345 void tgt_cancel_slc_locks(struct lu_target *lut, __u64 transno)
346 {
347         struct ldlm_lock *lock, *next;
348         LIST_HEAD(list);
349         struct lustre_handle lockh;
350
351         spin_lock(&lut->lut_slc_locks_guard);
352         list_for_each_entry_safe(lock, next, &lut->lut_slc_locks,
353                                  l_slc_link) {
354                 lock_res_and_lock(lock);
355                 LASSERT(lock->l_transno != 0);
356                 if (lock->l_transno > transno) {
357                         unlock_res_and_lock(lock);
358                         continue;
359                 }
360                 /* ouch, another operation is using it after it's saved */
361                 if (lock->l_readers != 0 || lock->l_writers != 0) {
362                         unlock_res_and_lock(lock);
363                         continue;
364                 }
365                 /* set CBPENDING so that this lock won't be used again */
366                 ldlm_set_cbpending(lock);
367                 lock->l_transno = 0;
368                 list_move(&lock->l_slc_link, &list);
369                 unlock_res_and_lock(lock);
370         }
371         spin_unlock(&lut->lut_slc_locks_guard);
372
373         list_for_each_entry_safe(lock, next, &list, l_slc_link) {
374                 list_del_init(&lock->l_slc_link);
375                 ldlm_lock2handle(lock, &lockh);
376                 ldlm_cli_cancel(&lockh, LCF_ASYNC);
377                 LDLM_LOCK_PUT(lock);
378         }
379 }
380
381 int tgt_init(const struct lu_env *env, struct lu_target *lut,
382              struct obd_device *obd, struct dt_device *dt,
383              struct tgt_opc_slice *slice, int request_fail_id,
384              int reply_fail_id)
385 {
386         struct dt_object_format  dof;
387         struct lu_attr           attr;
388         struct lu_fid            fid;
389         struct dt_object        *o;
390         struct tg_grants_data   *tgd = &lut->lut_tgd;
391         struct obd_statfs       *osfs;
392         int i, rc = 0;
393
394         ENTRY;
395
396         LASSERT(lut);
397         LASSERT(obd);
398         lut->lut_obd = obd;
399         lut->lut_bottom = dt;
400         lut->lut_last_rcvd = NULL;
401         lut->lut_client_bitmap = NULL;
402         atomic_set(&lut->lut_num_clients, 0);
403         atomic_set(&lut->lut_client_generation, 0);
404         lut->lut_reply_data = NULL;
405         lut->lut_reply_bitmap = NULL;
406         obd->u.obt.obt_lut = lut;
407         obd->u.obt.obt_magic = OBT_MAGIC;
408
409         /* set request handler slice and parameters */
410         lut->lut_slice = slice;
411         lut->lut_reply_fail_id = reply_fail_id;
412         lut->lut_request_fail_id = request_fail_id;
413
414         /* sptlrcp variables init */
415         rwlock_init(&lut->lut_sptlrpc_lock);
416         sptlrpc_rule_set_init(&lut->lut_sptlrpc_rset);
417
418         spin_lock_init(&lut->lut_flags_lock);
419         lut->lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
420
421         spin_lock_init(&lut->lut_slc_locks_guard);
422         INIT_LIST_HEAD(&lut->lut_slc_locks);
423
424         /* last_rcvd initialization is needed by replayable targets only */
425         if (!obd->obd_replayable)
426                 RETURN(0);
427
428         /* initialize grant and statfs data in target */
429         dt_conf_get(env, lut->lut_bottom, &lut->lut_dt_conf);
430
431         /* statfs data */
432         spin_lock_init(&tgd->tgd_osfs_lock);
433         tgd->tgd_osfs_age = ktime_get_seconds() - 1000;
434         tgd->tgd_osfs_unstable = 0;
435         tgd->tgd_statfs_inflight = 0;
436         tgd->tgd_osfs_inflight = 0;
437
438         /* grant data */
439         spin_lock_init(&tgd->tgd_grant_lock);
440         tgd->tgd_tot_dirty = 0;
441         tgd->tgd_tot_granted = 0;
442         tgd->tgd_tot_pending = 0;
443         tgd->tgd_grant_compat_disable = 0;
444
445         /* populate cached statfs data */
446         osfs = &tgt_th_info(env)->tti_u.osfs;
447         rc = tgt_statfs_internal(env, lut, osfs, 0, NULL);
448         if (rc != 0) {
449                 CERROR("%s: can't get statfs data, rc %d\n", tgt_name(lut),
450                         rc);
451                 GOTO(out, rc);
452         }
453         if (!is_power_of_2(osfs->os_bsize)) {
454                 CERROR("%s: blocksize (%d) is not a power of 2\n",
455                         tgt_name(lut), osfs->os_bsize);
456                 GOTO(out, rc = -EPROTO);
457         }
458         tgd->tgd_blockbits = fls(osfs->os_bsize) - 1;
459
460         spin_lock_init(&lut->lut_translock);
461         spin_lock_init(&lut->lut_client_bitmap_lock);
462
463         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
464         if (lut->lut_client_bitmap == NULL)
465                 RETURN(-ENOMEM);
466
467         memset(&attr, 0, sizeof(attr));
468         attr.la_valid = LA_MODE;
469         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
470         dof.dof_type = dt_mode_to_dft(S_IFREG);
471
472         lu_local_obj_fid(&fid, LAST_RECV_OID);
473
474         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
475         if (IS_ERR(o)) {
476                 rc = PTR_ERR(o);
477                 CERROR("%s: cannot open LAST_RCVD: rc = %d\n", tgt_name(lut),
478                        rc);
479                 GOTO(out_put, rc);
480         }
481
482         lut->lut_last_rcvd = o;
483         rc = tgt_server_data_init(env, lut);
484         if (rc < 0)
485                 GOTO(out_put, rc);
486
487         /* prepare transactions callbacks */
488         lut->lut_txn_cb.dtc_txn_start = tgt_txn_start_cb;
489         lut->lut_txn_cb.dtc_txn_stop = tgt_txn_stop_cb;
490         lut->lut_txn_cb.dtc_cookie = lut;
491         lut->lut_txn_cb.dtc_tag = LCT_DT_THREAD | LCT_MD_THREAD;
492         INIT_LIST_HEAD(&lut->lut_txn_cb.dtc_linkage);
493
494         dt_txn_callback_add(lut->lut_bottom, &lut->lut_txn_cb);
495         lut->lut_bottom->dd_lu_dev.ld_site->ls_tgt = lut;
496
497         lut->lut_fmd_max_num = LUT_FMD_MAX_NUM_DEFAULT;
498         lut->lut_fmd_max_age = LUT_FMD_MAX_AGE_DEFAULT;
499
500         atomic_set(&lut->lut_sync_count, 0);
501
502         /* reply_data is supported by MDT targets only for now */
503         if (strncmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) != 0)
504                 RETURN(0);
505
506         OBD_ALLOC(lut->lut_reply_bitmap,
507                   LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
508         if (lut->lut_reply_bitmap == NULL)
509                 GOTO(out, rc = -ENOMEM);
510
511         memset(&attr, 0, sizeof(attr));
512         attr.la_valid = LA_MODE;
513         attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
514         dof.dof_type = dt_mode_to_dft(S_IFREG);
515
516         lu_local_obj_fid(&fid, REPLY_DATA_OID);
517
518         o = dt_find_or_create(env, lut->lut_bottom, &fid, &dof, &attr);
519         if (IS_ERR(o)) {
520                 rc = PTR_ERR(o);
521                 CERROR("%s: cannot open REPLY_DATA: rc = %d\n", tgt_name(lut),
522                        rc);
523                 GOTO(out, rc);
524         }
525         lut->lut_reply_data = o;
526
527         rc = tgt_reply_data_init(env, lut);
528         if (rc < 0)
529                 GOTO(out, rc);
530
531         RETURN(0);
532
533 out:
534         dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
535 out_put:
536         obd->u.obt.obt_magic = 0;
537         obd->u.obt.obt_lut = NULL;
538         if (lut->lut_last_rcvd != NULL) {
539                 dt_object_put(env, lut->lut_last_rcvd);
540                 lut->lut_last_rcvd = NULL;
541         }
542         if (lut->lut_client_bitmap != NULL)
543                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
544         lut->lut_client_bitmap = NULL;
545         if (lut->lut_reply_data != NULL)
546                 dt_object_put(env, lut->lut_reply_data);
547         lut->lut_reply_data = NULL;
548         if (lut->lut_reply_bitmap != NULL) {
549                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
550                         if (lut->lut_reply_bitmap[i] != NULL)
551                                 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
552                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
553                                     sizeof(long));
554                         lut->lut_reply_bitmap[i] = NULL;
555                 }
556                 OBD_FREE(lut->lut_reply_bitmap,
557                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
558         }
559         lut->lut_reply_bitmap = NULL;
560         return rc;
561 }
562 EXPORT_SYMBOL(tgt_init);
563
564 void tgt_fini(const struct lu_env *env, struct lu_target *lut)
565 {
566         int i;
567         int rc;
568         ENTRY;
569
570         if (lut->lut_lsd.lsd_feature_incompat & OBD_INCOMPAT_MULTI_RPCS &&
571             atomic_read(&lut->lut_num_clients) == 0) {
572                 /* Clear MULTI RPCS incompatibility flag that prevents previous
573                  * Lustre versions to mount a target with reply_data file */
574                 lut->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
575                 rc = tgt_server_data_update(env, lut, 1);
576                 if (rc < 0)
577                         CERROR("%s: unable to clear MULTI RPCS "
578                                "incompatibility flag\n",
579                                lut->lut_obd->obd_name);
580         }
581
582         sptlrpc_rule_set_free(&lut->lut_sptlrpc_rset);
583
584         if (lut->lut_reply_data != NULL)
585                 dt_object_put(env, lut->lut_reply_data);
586         lut->lut_reply_data = NULL;
587         if (lut->lut_reply_bitmap != NULL) {
588                 for (i = 0; i < LUT_REPLY_SLOTS_MAX_CHUNKS; i++) {
589                         if (lut->lut_reply_bitmap[i] != NULL)
590                                 OBD_FREE_LARGE(lut->lut_reply_bitmap[i],
591                                     BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
592                                     sizeof(long));
593                         lut->lut_reply_bitmap[i] = NULL;
594                 }
595                 OBD_FREE(lut->lut_reply_bitmap,
596                          LUT_REPLY_SLOTS_MAX_CHUNKS * sizeof(unsigned long *));
597         }
598         lut->lut_reply_bitmap = NULL;
599         if (lut->lut_client_bitmap) {
600                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
601                 lut->lut_client_bitmap = NULL;
602         }
603         if (lut->lut_last_rcvd) {
604                 dt_txn_callback_del(lut->lut_bottom, &lut->lut_txn_cb);
605                 dt_object_put(env, lut->lut_last_rcvd);
606                 lut->lut_last_rcvd = NULL;
607         }
608         EXIT;
609 }
610 EXPORT_SYMBOL(tgt_fini);
611
612 static struct kmem_cache *tgt_thread_kmem;
613 static struct kmem_cache *tgt_session_kmem;
614 struct kmem_cache *tgt_fmd_kmem;
615
616 static struct lu_kmem_descr tgt_caches[] = {
617         {
618                 .ckd_cache = &tgt_thread_kmem,
619                 .ckd_name  = "tgt_thread_kmem",
620                 .ckd_size  = sizeof(struct tgt_thread_info),
621         },
622         {
623                 .ckd_cache = &tgt_session_kmem,
624                 .ckd_name  = "tgt_session_kmem",
625                 .ckd_size  = sizeof(struct tgt_session_info)
626         },
627         {
628                 .ckd_cache = &tgt_fmd_kmem,
629                 .ckd_name  = "tgt_fmd_cache",
630                 .ckd_size  = sizeof(struct tgt_fmd_data)
631         },
632         {
633                 .ckd_cache = NULL
634         }
635 };
636
637
638 /* context key constructor/destructor: tg_key_init, tg_key_fini */
639 static void *tgt_key_init(const struct lu_context *ctx,
640                                   struct lu_context_key *key)
641 {
642         struct tgt_thread_info *thread;
643
644         OBD_SLAB_ALLOC_PTR_GFP(thread, tgt_thread_kmem, GFP_NOFS);
645         if (thread == NULL)
646                 return ERR_PTR(-ENOMEM);
647
648         return thread;
649 }
650
651 static void tgt_key_fini(const struct lu_context *ctx,
652                          struct lu_context_key *key, void *data)
653 {
654         struct tgt_thread_info          *info = data;
655         struct thandle_exec_args        *args = &info->tti_tea;
656         int                             i;
657
658         for (i = 0; i < args->ta_alloc_args; i++) {
659                 if (args->ta_args[i] != NULL)
660                         OBD_FREE_PTR(args->ta_args[i]);
661         }
662
663         if (args->ta_args != NULL)
664                 OBD_FREE(args->ta_args, sizeof(args->ta_args[0]) *
665                                         args->ta_alloc_args);
666         OBD_SLAB_FREE_PTR(info, tgt_thread_kmem);
667 }
668
669 static void tgt_key_exit(const struct lu_context *ctx,
670                          struct lu_context_key *key, void *data)
671 {
672         struct tgt_thread_info *tti = data;
673
674         tti->tti_has_trans = 0;
675         tti->tti_mult_trans = 0;
676 }
677
678 /* context key: tg_thread_key */
679 struct lu_context_key tgt_thread_key = {
680         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD,
681         .lct_init = tgt_key_init,
682         .lct_fini = tgt_key_fini,
683         .lct_exit = tgt_key_exit,
684 };
685
686 LU_KEY_INIT_GENERIC(tgt);
687
688 static void *tgt_ses_key_init(const struct lu_context *ctx,
689                               struct lu_context_key *key)
690 {
691         struct tgt_session_info *session;
692
693         OBD_SLAB_ALLOC_PTR_GFP(session, tgt_session_kmem, GFP_NOFS);
694         if (session == NULL)
695                 return ERR_PTR(-ENOMEM);
696
697         return session;
698 }
699
700 static void tgt_ses_key_fini(const struct lu_context *ctx,
701                              struct lu_context_key *key, void *data)
702 {
703         struct tgt_session_info *session = data;
704
705         OBD_SLAB_FREE_PTR(session, tgt_session_kmem);
706 }
707
708 /* context key: tgt_session_key */
709 struct lu_context_key tgt_session_key = {
710         .lct_tags = LCT_SERVER_SESSION,
711         .lct_init = tgt_ses_key_init,
712         .lct_fini = tgt_ses_key_fini,
713 };
714 EXPORT_SYMBOL(tgt_session_key);
715
716 LU_KEY_INIT_GENERIC(tgt_ses);
717
718 /*
719  * this page is allocated statically when module is initializing
720  * it is used to simulate data corruptions, see ost_checksum_bulk()
721  * for details. as the original pages provided by the layers below
722  * can be remain in the internal cache, we do not want to modify
723  * them.
724  */
725 struct page *tgt_page_to_corrupt;
726
727 int tgt_mod_init(void)
728 {
729         int     result;
730         ENTRY;
731
732         result = lu_kmem_init(tgt_caches);
733         if (result != 0)
734                 RETURN(result);
735
736         tgt_page_to_corrupt = alloc_page(GFP_KERNEL);
737
738         tgt_key_init_generic(&tgt_thread_key, NULL);
739         lu_context_key_register_many(&tgt_thread_key, NULL);
740
741         tgt_ses_key_init_generic(&tgt_session_key, NULL);
742         lu_context_key_register_many(&tgt_session_key, NULL);
743         barrier_init();
744
745         update_info_init();
746
747         RETURN(0);
748 }
749
750 void tgt_mod_exit(void)
751 {
752         barrier_fini();
753         if (tgt_page_to_corrupt != NULL)
754                 put_page(tgt_page_to_corrupt);
755
756         lu_context_key_degister(&tgt_thread_key);
757         lu_context_key_degister(&tgt_session_key);
758         update_info_fini();
759
760         lu_kmem_fini(tgt_caches);
761 }
762