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