Whamcloud - gitweb
LU-3536 osp: move update packing into out_lib.c
[fs/lustre-release.git] / lustre / osp / osp_sync.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, 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/osp/osp_sync.c
37  *
38  * Lustre OST Proxy Device
39  *
40  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <lustre_log.h>
47 #include <lustre_update.h>
48 #include "osp_internal.h"
49
50 static int osp_sync_id_traction_init(struct osp_device *d);
51 static void osp_sync_id_traction_fini(struct osp_device *d);
52 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id);
53 static void osp_sync_remove_from_tracker(struct osp_device *d);
54
55 /*
56  * this is a components of OSP implementing synchronization between MDS and OST
57  * it llogs all interesting changes (currently it's uig/gid change and object
58  * destroy) atomically, then makes sure changes hit OST storage
59  *
60  * we have 4 queues of work:
61  *
62  * the first queue is llog itself, once read a change is stored in 2nd queue
63  * in form of RPC (but RPC isn't fired yet).
64  *
65  * the second queue (opd_syn_waiting_for_commit) holds changes awaiting local
66  * commit. once change is committed locally it migrates onto 3rd queue.
67  *
68  * the third queue (opd_syn_committed_here) holds changes committed locally,
69  * but not sent to OST (as the pipe can be full). once pipe becomes non-full
70  * we take a change from the queue and fire corresponded RPC.
71  *
72  * once RPC is reported committed by OST (using regular last_committed mech.)
73  * the change jumps into 4th queue (opd_syn_committed_there), now we can
74  * cancel corresponded llog record and release RPC
75  *
76  * opd_syn_changes is a number of unread llog records (to be processed).
77  * notice this number doesn't include llog records from previous boots.
78  * with OSP_SYN_THRESHOLD we try to batch processing a bit (TO BE IMPLEMENTED)
79  *
80  * opd_syn_rpc_in_progress is a number of requests in 2-4 queues.
81  * we control this with OSP_MAX_IN_PROGRESS so that OSP don't consume
82  * too much memory -- how to deal with 1000th OSTs ? batching could help?
83  *
84  * opd_syn_rpc_in_flight is a number of RPC in flight.
85  * we control this with OSP_MAX_IN_FLIGHT
86  */
87
88 /* XXX: do math to learn reasonable threshold
89  * should it be ~ number of changes fitting bulk? */
90
91 #define OSP_SYN_THRESHOLD       10
92 #define OSP_MAX_IN_FLIGHT       8
93 #define OSP_MAX_IN_PROGRESS     4096
94
95 #define OSP_JOB_MAGIC           0x26112005
96
97 static inline int osp_sync_running(struct osp_device *d)
98 {
99         return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
100 }
101
102 static inline int osp_sync_stopped(struct osp_device *d)
103 {
104         return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
105 }
106
107 static inline int osp_sync_has_new_job(struct osp_device *d)
108 {
109         return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
110                 (d->opd_syn_last_processed_id < d->opd_syn_last_committed_id))
111                 || (d->opd_syn_prev_done == 0);
112 }
113
114 static inline int osp_sync_low_in_progress(struct osp_device *d)
115 {
116         return d->opd_syn_rpc_in_progress < d->opd_syn_max_rpc_in_progress;
117 }
118
119 static inline int osp_sync_low_in_flight(struct osp_device *d)
120 {
121         return d->opd_syn_rpc_in_flight < d->opd_syn_max_rpc_in_flight;
122 }
123
124 static inline int osp_sync_has_work(struct osp_device *d)
125 {
126         /* has new/old changes and low in-progress? */
127         if (osp_sync_has_new_job(d) && osp_sync_low_in_progress(d) &&
128             osp_sync_low_in_flight(d) && d->opd_imp_connected)
129                 return 1;
130
131         /* has remotely committed? */
132         if (!list_empty(&d->opd_syn_committed_there))
133                 return 1;
134
135         return 0;
136 }
137
138 #define osp_sync_check_for_work(d)                      \
139 {                                                       \
140         if (osp_sync_has_work(d)) {                     \
141                 wake_up(&d->opd_syn_waitq);    \
142         }                                               \
143 }
144
145 void __osp_sync_check_for_work(struct osp_device *d)
146 {
147         osp_sync_check_for_work(d);
148 }
149
150 static inline int osp_sync_can_process_new(struct osp_device *d,
151                                            struct llog_rec_hdr *rec)
152 {
153         LASSERT(d);
154
155         if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
156                 return 0;
157         if (!osp_sync_low_in_progress(d))
158                 return 0;
159         if (!osp_sync_low_in_flight(d))
160                 return 0;
161         if (!d->opd_imp_connected)
162                 return 0;
163         if (d->opd_syn_prev_done == 0)
164                 return 1;
165         if (d->opd_syn_changes == 0)
166                 return 0;
167         if (rec == NULL || rec->lrh_id <= d->opd_syn_last_committed_id)
168                 return 1;
169         return 0;
170 }
171
172 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
173                          llog_op_type type, struct thandle *th)
174 {
175         struct osp_thread_info  *osi = osp_env_info(env);
176         struct osp_device       *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
177         struct llog_ctxt        *ctxt;
178         int                      rc;
179
180         ENTRY;
181
182         /* it's a layering violation, to access internals of th,
183          * but we can do this as a sanity check, for a while */
184         LASSERT(th->th_dev == d->opd_storage);
185
186         switch (type) {
187         case MDS_UNLINK64_REC:
188                 osi->osi_hdr.lrh_len = sizeof(struct llog_unlink64_rec);
189                 break;
190         case MDS_SETATTR64_REC:
191                 osi->osi_hdr.lrh_len = sizeof(struct llog_setattr64_rec);
192                 break;
193         default:
194                 LBUG();
195         }
196
197         /* we want ->dt_trans_start() to allocate per-thandle structure */
198         th->th_tags |= LCT_OSP_THREAD;
199
200         ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
201         LASSERT(ctxt);
202
203         rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr, th);
204         llog_ctxt_put(ctxt);
205
206         RETURN(rc);
207 }
208
209 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
210                             const struct lu_fid *fid, llog_op_type type,
211                             int count, struct thandle *th,
212                             const struct lu_attr *attr)
213 {
214         struct osp_thread_info  *osi = osp_env_info(env);
215         struct llog_ctxt        *ctxt;
216         struct osp_txn_info     *txn;
217         int                      rc;
218
219         ENTRY;
220
221         /* it's a layering violation, to access internals of th,
222          * but we can do this as a sanity check, for a while */
223         LASSERT(th->th_dev == d->opd_storage);
224
225         switch (type) {
226         case MDS_UNLINK64_REC:
227                 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
228                 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
229                 osi->osi_unlink.lur_fid  = *fid;
230                 osi->osi_unlink.lur_count = count;
231                 break;
232         case MDS_SETATTR64_REC:
233                 rc = fid_to_ostid(fid, &osi->osi_oi);
234                 LASSERT(rc == 0);
235                 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
236                 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
237                 osi->osi_setattr.lsr_oi  = osi->osi_oi;
238                 LASSERT(attr);
239                 osi->osi_setattr.lsr_uid = attr->la_uid;
240                 osi->osi_setattr.lsr_gid = attr->la_gid;
241                 osi->osi_setattr.lsr_valid =
242                         ((attr->la_valid & LA_UID) ? OBD_MD_FLUID : 0) |
243                         ((attr->la_valid & LA_GID) ? OBD_MD_FLGID : 0);
244                 break;
245         default:
246                 LBUG();
247         }
248
249         txn = osp_txn_info(&th->th_ctx);
250         LASSERT(txn);
251
252         txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
253         osi->osi_hdr.lrh_id = txn->oti_current_id;
254
255         ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
256         if (ctxt == NULL)
257                 RETURN(-ENOMEM);
258         rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
259                       th);
260         llog_ctxt_put(ctxt);
261
262         CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
263                d->opd_obd->obd_name, POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
264                (unsigned long) osi->osi_cookie.lgc_lgl.lgl_ogen,
265                (unsigned long) osi->osi_cookie.lgc_index, rc);
266
267         if (rc > 0)
268                 rc = 0;
269
270         if (likely(rc == 0)) {
271                 spin_lock(&d->opd_syn_lock);
272                 d->opd_syn_changes++;
273                 spin_unlock(&d->opd_syn_lock);
274         }
275
276         RETURN(rc);
277 }
278
279 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
280                  llog_op_type type, struct thandle *th,
281                  const struct lu_attr *attr)
282 {
283         return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
284                                 lu_object_fid(&o->opo_obj.do_lu), type, 1,
285                                 th, attr);
286 }
287
288 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
289                  struct lu_fid *fid, int lost, struct thandle *th)
290 {
291         return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
292 }
293
294 /*
295  * it's quite obvious we can't maintain all the structures in the memory:
296  * while OST is down, MDS can be processing thousands and thousands of unlinks
297  * filling persistent llogs and in-core respresentation
298  *
299  * this doesn't scale at all. so we need basically the following:
300  * a) destroy/setattr append llog records
301  * b) once llog has grown to X records, we process first Y committed records
302  *
303  *  once record R is found via llog_process(), it becomes committed after any
304  *  subsequent commit callback (at the most)
305  */
306
307 /*
308  * called for each atomic on-disk change (not once per transaction batch)
309  * and goes over the list
310  * XXX: should be optimized?
311  */
312
313 /**
314  * called for each RPC reported committed
315  */
316 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
317 {
318         struct osp_device *d = req->rq_cb_data;
319
320         CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
321
322         if (unlikely(req->rq_transno == 0))
323                 return;
324
325         /* do not do any opd_dyn_rpc_* accounting here
326          * it's done in osp_sync_interpret sooner or later */
327
328         LASSERT(d);
329         LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
330         LASSERT(list_empty(&req->rq_exp_list));
331
332         ptlrpc_request_addref(req);
333
334         spin_lock(&d->opd_syn_lock);
335         list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
336         spin_unlock(&d->opd_syn_lock);
337
338         /* XXX: some batching wouldn't hurt */
339         wake_up(&d->opd_syn_waitq);
340 }
341
342 static int osp_sync_interpret(const struct lu_env *env,
343                               struct ptlrpc_request *req, void *aa, int rc)
344 {
345         struct osp_device *d = req->rq_cb_data;
346
347         if (req->rq_svc_thread != (void *) OSP_JOB_MAGIC)
348                 DEBUG_REQ(D_ERROR, req, "bad magic %p\n", req->rq_svc_thread);
349         LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
350         LASSERT(d);
351
352         CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
353                atomic_read(&req->rq_refcount),
354                rc, (unsigned) req->rq_transno);
355         LASSERT(rc || req->rq_transno);
356
357         if (rc == -ENOENT) {
358                 /*
359                  * we tried to destroy object or update attributes,
360                  * but object doesn't exist anymore - cancell llog record
361                  */
362                 LASSERT(req->rq_transno == 0);
363                 LASSERT(list_empty(&req->rq_exp_list));
364
365                 ptlrpc_request_addref(req);
366
367                 spin_lock(&d->opd_syn_lock);
368                 list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
369                 spin_unlock(&d->opd_syn_lock);
370
371                 wake_up(&d->opd_syn_waitq);
372         } else if (rc) {
373                 struct obd_import *imp = req->rq_import;
374                 /*
375                  * error happened, we'll try to repeat on next boot ?
376                  */
377                 LASSERTF(req->rq_transno == 0 ||
378                          req->rq_import_generation < imp->imp_generation,
379                          "transno "LPU64", rc %d, gen: req %d, imp %d\n",
380                          req->rq_transno, rc, req->rq_import_generation,
381                          imp->imp_generation);
382                 if (req->rq_transno == 0) {
383                         /* this is the last time we see the request
384                          * if transno is not zero, then commit cb
385                          * will be called at some point */
386                         LASSERT(d->opd_syn_rpc_in_progress > 0);
387                         spin_lock(&d->opd_syn_lock);
388                         d->opd_syn_rpc_in_progress--;
389                         spin_unlock(&d->opd_syn_lock);
390                 }
391
392                 wake_up(&d->opd_syn_waitq);
393         } else if (d->opd_pre != NULL &&
394                    unlikely(d->opd_pre_status == -ENOSPC)) {
395                 /*
396                  * if current status is -ENOSPC (lack of free space on OST)
397                  * then we should poll OST immediately once object destroy
398                  * is replied
399                  */
400                 osp_statfs_need_now(d);
401         }
402
403         LASSERT(d->opd_syn_rpc_in_flight > 0);
404         spin_lock(&d->opd_syn_lock);
405         d->opd_syn_rpc_in_flight--;
406         spin_unlock(&d->opd_syn_lock);
407         if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
408                 wake_up(&d->opd_syn_barrier_waitq);
409         CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
410                d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
411                d->opd_syn_rpc_in_progress);
412
413         osp_sync_check_for_work(d);
414
415         return 0;
416 }
417
418 /*
419  * the function walks through list of committed locally changes
420  * and send them to RPC until the pipe is full
421  */
422 static void osp_sync_send_new_rpc(struct osp_device *d,
423                                   struct ptlrpc_request *req)
424 {
425         LASSERT(d->opd_syn_rpc_in_flight <= d->opd_syn_max_rpc_in_flight);
426         LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
427
428         ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
429 }
430
431 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
432                                                struct llog_handle *llh,
433                                                struct llog_rec_hdr *h,
434                                                ost_cmd_t op,
435                                                const struct req_format *format)
436 {
437         struct ptlrpc_request   *req;
438         struct ost_body         *body;
439         struct obd_import       *imp;
440         int                      rc;
441
442         /* Prepare the request */
443         imp = d->opd_obd->u.cli.cl_import;
444         LASSERT(imp);
445         req = ptlrpc_request_alloc(imp, format);
446         if (req == NULL)
447                 RETURN(ERR_PTR(-ENOMEM));
448
449         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
450         if (rc) {
451                 ptlrpc_req_finished(req);
452                 return ERR_PTR(rc);
453         }
454
455         /*
456          * this is a trick: to save on memory allocations we put cookie
457          * into the request, but don't set corresponded flag in o_valid
458          * so that OST doesn't interpret this cookie. once the request
459          * is committed on OST we take cookie from the request and cancel
460          */
461         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
462         LASSERT(body);
463         body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
464         body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
465         body->oa.o_lcookie.lgc_index = h->lrh_index;
466         INIT_LIST_HEAD(&req->rq_exp_list);
467         req->rq_svc_thread = (void *) OSP_JOB_MAGIC;
468
469         req->rq_interpret_reply = osp_sync_interpret;
470         req->rq_commit_cb = osp_sync_request_commit_cb;
471         req->rq_cb_data = d;
472
473         ptlrpc_request_set_replen(req);
474
475         return req;
476 }
477
478 static int osp_sync_new_setattr_job(struct osp_device *d,
479                                     struct llog_handle *llh,
480                                     struct llog_rec_hdr *h)
481 {
482         struct llog_setattr64_rec       *rec = (struct llog_setattr64_rec *)h;
483         struct ptlrpc_request           *req;
484         struct ost_body                 *body;
485
486         ENTRY;
487         LASSERT(h->lrh_type == MDS_SETATTR64_REC);
488
489         /* lsr_valid can only be 0 or have OBD_MD_{FLUID,FLGID} set,
490          * so no bits other than these should be set. */
491         if ((rec->lsr_valid & ~(OBD_MD_FLUID | OBD_MD_FLGID)) != 0) {
492                 CERROR("%s: invalid setattr record, lsr_valid:"LPU64"\n",
493                        d->opd_obd->obd_name, rec->lsr_valid);
494                 /* return 0 so that sync thread can continue processing
495                  * other records. */
496                 RETURN(0);
497         }
498
499         req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
500         if (IS_ERR(req))
501                 RETURN(PTR_ERR(req));
502
503         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
504         LASSERT(body);
505         body->oa.o_oi = rec->lsr_oi;
506         body->oa.o_uid = rec->lsr_uid;
507         body->oa.o_gid = rec->lsr_gid;
508         body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
509         /* old setattr record (prior 2.6.0) doesn't have 'valid' stored,
510          * we assume that both UID and GID are valid in that case. */
511         if (rec->lsr_valid == 0)
512                 body->oa.o_valid |= (OBD_MD_FLUID | OBD_MD_FLGID);
513         else
514                 body->oa.o_valid |= rec->lsr_valid;
515
516         osp_sync_send_new_rpc(d, req);
517         RETURN(1);
518 }
519
520 static int osp_sync_new_unlink_job(struct osp_device *d,
521                                    struct llog_handle *llh,
522                                    struct llog_rec_hdr *h)
523 {
524         struct llog_unlink_rec  *rec = (struct llog_unlink_rec *)h;
525         struct ptlrpc_request   *req;
526         struct ost_body         *body;
527
528         ENTRY;
529         LASSERT(h->lrh_type == MDS_UNLINK_REC);
530
531         req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
532         if (IS_ERR(req))
533                 RETURN(PTR_ERR(req));
534
535         body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
536         LASSERT(body);
537         ostid_set_seq(&body->oa.o_oi, rec->lur_oseq);
538         ostid_set_id(&body->oa.o_oi, rec->lur_oid);
539         body->oa.o_misc = rec->lur_count;
540         body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
541         if (rec->lur_count)
542                 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
543
544         osp_sync_send_new_rpc(d, req);
545         RETURN(1);
546 }
547
548 static int osp_prep_unlink_update_req(const struct lu_env *env,
549                                       struct osp_device *osp,
550                                       struct llog_handle *llh,
551                                       struct llog_rec_hdr *h,
552                                       struct ptlrpc_request **reqp)
553 {
554         struct llog_unlink64_rec        *rec = (struct llog_unlink64_rec *)h;
555         struct dt_update_request        *update = NULL;
556         struct ptlrpc_request           *req;
557         struct llog_cookie              lcookie;
558         const void                      *buf;
559         __u16                           size;
560         int                             rc;
561         ENTRY;
562
563         update = dt_update_request_create(&osp->opd_dt_dev);
564         if (IS_ERR(update))
565                 RETURN(PTR_ERR(update));
566
567         /* This can only happens for unlink slave directory, so decrease
568          * ref for ".." and "." */
569         rc = out_update_pack(env, &update->dur_buf, OUT_REF_DEL, &rec->lur_fid,
570                              0, NULL, NULL, 0);
571         if (rc != 0)
572                 GOTO(out, rc);
573
574         rc = out_update_pack(env, &update->dur_buf, OUT_REF_DEL, &rec->lur_fid,
575                              0, NULL, NULL, 0);
576         if (rc != 0)
577                 GOTO(out, rc);
578
579         lcookie.lgc_lgl = llh->lgh_id;
580         lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
581         lcookie.lgc_index = h->lrh_index;
582         size = sizeof(lcookie);
583         buf = &lcookie;
584
585         rc = out_update_pack(env, &update->dur_buf, OUT_DESTROY, &rec->lur_fid,
586                              1, &size, &buf, 0);
587         if (rc != 0)
588                 GOTO(out, rc);
589
590         rc = out_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
591                                  update->dur_buf.ub_req, &req);
592         if (rc != 0)
593                 GOTO(out, rc);
594
595         INIT_LIST_HEAD(&req->rq_exp_list);
596         req->rq_svc_thread = (void *)OSP_JOB_MAGIC;
597
598         req->rq_interpret_reply = osp_sync_interpret;
599         req->rq_commit_cb = osp_sync_request_commit_cb;
600         req->rq_cb_data = osp;
601
602         ptlrpc_request_set_replen(req);
603         *reqp = req;
604 out:
605         if (update != NULL)
606                 dt_update_request_destroy(update);
607
608         RETURN(rc);
609 }
610
611 static int osp_sync_new_unlink64_job(const struct lu_env *env,
612                                      struct osp_device *d,
613                                      struct llog_handle *llh,
614                                      struct llog_rec_hdr *h)
615 {
616         struct llog_unlink64_rec        *rec = (struct llog_unlink64_rec *)h;
617         struct ptlrpc_request           *req = NULL;
618         struct ost_body                 *body;
619         int                              rc;
620
621         ENTRY;
622         LASSERT(h->lrh_type == MDS_UNLINK64_REC);
623
624         if (d->opd_connect_mdt) {
625                 rc = osp_prep_unlink_update_req(env, d, llh, h, &req);
626                 if (rc != 0)
627                         RETURN(rc);
628         } else {
629                 req = osp_sync_new_job(d, llh, h, OST_DESTROY,
630                                        &RQF_OST_DESTROY);
631                 if (IS_ERR(req))
632                         RETURN(PTR_ERR(req));
633
634                 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
635                 if (body == NULL)
636                         RETURN(-EFAULT);
637                 rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
638                 if (rc < 0)
639                         RETURN(rc);
640                 body->oa.o_misc = rec->lur_count;
641                 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
642                                    OBD_MD_FLOBJCOUNT;
643         }
644         osp_sync_send_new_rpc(d, req);
645         RETURN(1);
646 }
647
648 static int osp_sync_process_record(const struct lu_env *env,
649                                    struct osp_device *d,
650                                    struct llog_handle *llh,
651                                    struct llog_rec_hdr *rec)
652 {
653         struct llog_cookie       cookie;
654         int                      rc = 0;
655
656         cookie.lgc_lgl = llh->lgh_id;
657         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
658         cookie.lgc_index = rec->lrh_index;
659
660         if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
661                 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
662
663                 /* we're waiting for the record generated by this instance */
664                 LASSERT(d->opd_syn_prev_done == 0);
665                 if (!memcmp(&d->opd_syn_generation, &gen->lgr_gen,
666                             sizeof(gen->lgr_gen))) {
667                         CDEBUG(D_HA, "processed all old entries\n");
668                         d->opd_syn_prev_done = 1;
669                 }
670
671                 /* cancel any generation record */
672                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
673                                              1, &cookie);
674
675                 return rc;
676         }
677
678         /*
679          * now we prepare and fill requests to OST, put them on the queue
680          * and fire after next commit callback
681          */
682
683         /* notice we increment counters before sending RPC, to be consistent
684          * in RPC interpret callback which may happen very quickly */
685         spin_lock(&d->opd_syn_lock);
686         d->opd_syn_rpc_in_flight++;
687         d->opd_syn_rpc_in_progress++;
688         spin_unlock(&d->opd_syn_lock);
689
690         switch (rec->lrh_type) {
691         /* case MDS_UNLINK_REC is kept for compatibility */
692         case MDS_UNLINK_REC:
693                 rc = osp_sync_new_unlink_job(d, llh, rec);
694                 break;
695         case MDS_UNLINK64_REC:
696                 rc = osp_sync_new_unlink64_job(env, d, llh, rec);
697                 break;
698         case MDS_SETATTR64_REC:
699                 rc = osp_sync_new_setattr_job(d, llh, rec);
700                 break;
701         default:
702                 CERROR("%s: unknown record type: %x\n", d->opd_obd->obd_name,
703                        rec->lrh_type);
704                 /* we should continue processing */
705         }
706
707         /* rc > 0 means sync RPC being added to the queue */
708         if (likely(rc > 0)) {
709                 spin_lock(&d->opd_syn_lock);
710                 if (d->opd_syn_prev_done) {
711                         LASSERT(d->opd_syn_changes > 0);
712                         LASSERT(rec->lrh_id <= d->opd_syn_last_committed_id);
713                         /*
714                          * NOTE: it's possible to meet same id if
715                          * OST stores few stripes of same file
716                          */
717                         if (rec->lrh_id > d->opd_syn_last_processed_id) {
718                                 d->opd_syn_last_processed_id = rec->lrh_id;
719                                 wake_up(&d->opd_syn_barrier_waitq);
720                         }
721
722                         d->opd_syn_changes--;
723                 }
724                 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
725                        d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
726                        d->opd_syn_rpc_in_progress);
727                 spin_unlock(&d->opd_syn_lock);
728                 rc = 0;
729         } else {
730                 spin_lock(&d->opd_syn_lock);
731                 d->opd_syn_rpc_in_flight--;
732                 d->opd_syn_rpc_in_progress--;
733                 spin_unlock(&d->opd_syn_lock);
734         }
735
736         CDEBUG(D_HA, "found record %x, %d, idx %u, id %u: %d\n",
737                rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id, rc);
738         return rc;
739 }
740
741 static void osp_sync_process_committed(const struct lu_env *env,
742                                        struct osp_device *d)
743 {
744         struct obd_device       *obd = d->opd_obd;
745         struct obd_import       *imp = obd->u.cli.cl_import;
746         struct ost_body         *body;
747         struct ptlrpc_request   *req, *tmp;
748         struct llog_ctxt        *ctxt;
749         struct llog_handle      *llh;
750         struct list_head         list;
751         int                      rc, done = 0;
752
753         ENTRY;
754
755         if (list_empty(&d->opd_syn_committed_there))
756                 return;
757
758         /*
759          * if current status is -ENOSPC (lack of free space on OST)
760          * then we should poll OST immediately once object destroy
761          * is committed.
762          * notice: we do this upon commit as well because some backends
763          * (like DMU) do not release space right away.
764          */
765         if (d->opd_pre != NULL && unlikely(d->opd_pre_status == -ENOSPC))
766                 osp_statfs_need_now(d);
767
768         /*
769          * now cancel them all
770          * XXX: can we improve this using some batching?
771          *      with batch RPC that'll happen automatically?
772          * XXX: can we store ctxt in lod_device and save few cycles ?
773          */
774         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
775         LASSERT(ctxt);
776
777         llh = ctxt->loc_handle;
778         LASSERT(llh);
779
780         INIT_LIST_HEAD(&list);
781         spin_lock(&d->opd_syn_lock);
782         list_splice(&d->opd_syn_committed_there, &list);
783         INIT_LIST_HEAD(&d->opd_syn_committed_there);
784         spin_unlock(&d->opd_syn_lock);
785
786         list_for_each_entry_safe(req, tmp, &list, rq_exp_list) {
787                 struct llog_cookie *lcookie = NULL;
788
789                 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
790                 list_del_init(&req->rq_exp_list);
791
792                 if (d->opd_connect_mdt) {
793                         struct object_update_request *ureq;
794                         struct object_update *update;
795                         ureq = req_capsule_client_get(&req->rq_pill,
796                                                       &RMF_OUT_UPDATE);
797                         LASSERT(ureq != NULL &&
798                                 ureq->ourq_magic == UPDATE_REQUEST_MAGIC);
799
800                         /* 1st/2nd is for decref . and .., 3rd one is for
801                          * destroy, where the log cookie is stored.
802                          * See osp_prep_unlink_update_req */
803                         update = object_update_request_get(ureq, 2, NULL);
804                         LASSERT(update != NULL);
805                         lcookie = object_update_param_get(update, 0, NULL);
806                         LASSERT(lcookie != NULL);
807                 } else {
808                         body = req_capsule_client_get(&req->rq_pill,
809                                                       &RMF_OST_BODY);
810                         LASSERT(body);
811                         lcookie = &body->oa.o_lcookie;
812                 }
813                 /* import can be closing, thus all commit cb's are
814                  * called we can check committness directly */
815                 if (req->rq_transno <= imp->imp_peer_committed_transno) {
816                         rc = llog_cat_cancel_records(env, llh, 1, lcookie);
817                         if (rc)
818                                 CERROR("%s: can't cancel record: %d\n",
819                                        obd->obd_name, rc);
820                 } else {
821                         DEBUG_REQ(D_HA, req, "not committed");
822                 }
823
824                 ptlrpc_req_finished(req);
825                 done++;
826         }
827
828         llog_ctxt_put(ctxt);
829
830         LASSERT(d->opd_syn_rpc_in_progress >= done);
831         spin_lock(&d->opd_syn_lock);
832         d->opd_syn_rpc_in_progress -= done;
833         spin_unlock(&d->opd_syn_lock);
834         CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
835                d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
836                d->opd_syn_rpc_in_progress);
837
838         osp_sync_check_for_work(d);
839
840         /* wake up the thread if requested to stop:
841          * it might be waiting for in-progress to complete */
842         if (unlikely(osp_sync_running(d) == 0))
843                 wake_up(&d->opd_syn_waitq);
844
845         EXIT;
846 }
847
848 /*
849  * this is where most of queues processing happens
850  */
851 static int osp_sync_process_queues(const struct lu_env *env,
852                                    struct llog_handle *llh,
853                                    struct llog_rec_hdr *rec,
854                                    void *data)
855 {
856         struct osp_device       *d = data;
857         int                      rc;
858
859         do {
860                 struct l_wait_info lwi = { 0 };
861
862                 if (!osp_sync_running(d)) {
863                         CDEBUG(D_HA, "stop llog processing\n");
864                         return LLOG_PROC_BREAK;
865                 }
866
867                 /* process requests committed by OST */
868                 osp_sync_process_committed(env, d);
869
870                 /* if we there are changes to be processed and we have
871                  * resources for this ... do now */
872                 if (osp_sync_can_process_new(d, rec)) {
873                         if (llh == NULL) {
874                                 /* ask llog for another record */
875                                 CDEBUG(D_HA, "%lu changes, %u in progress, %u in flight\n",
876                                        d->opd_syn_changes,
877                                        d->opd_syn_rpc_in_progress,
878                                        d->opd_syn_rpc_in_flight);
879                                 return 0;
880                         }
881
882                         /*
883                          * try to send, in case of disconnection, suspend
884                          * processing till we can send this request
885                          */
886                         do {
887                                 rc = osp_sync_process_record(env, d, llh, rec);
888                                 /*
889                                  * XXX: probably different handling is needed
890                                  * for some bugs, like immediate exit or if
891                                  * OSP gets inactive
892                                  */
893                                 if (rc) {
894                                         CERROR("can't send: %d\n", rc);
895                                         l_wait_event(d->opd_syn_waitq,
896                                                      !osp_sync_running(d) ||
897                                                      osp_sync_has_work(d),
898                                                      &lwi);
899                                 }
900                         } while (rc != 0 && osp_sync_running(d));
901
902                         llh = NULL;
903                         rec = NULL;
904                 }
905
906                 if (d->opd_syn_last_processed_id == d->opd_syn_last_used_id)
907                         osp_sync_remove_from_tracker(d);
908
909                 l_wait_event(d->opd_syn_waitq,
910                              !osp_sync_running(d) ||
911                              osp_sync_can_process_new(d, rec) ||
912                              !list_empty(&d->opd_syn_committed_there),
913                              &lwi);
914         } while (1);
915 }
916
917 /*
918  * this thread runs llog_cat_process() scanner calling our callback
919  * to process llog records. in the callback we implement tricky
920  * state machine as we don't want to start scanning of the llog again
921  * and again, also we don't want to process too many records and send
922  * too many RPCs a time. so, depending on current load (num of changes
923  * being synced to OST) the callback can suspend awaiting for some
924  * new conditions, like syncs completed.
925  *
926  * in order to process llog records left by previous boots and to allow
927  * llog_process_thread() to find something (otherwise it'd just exit
928  * immediately) we add a special GENERATATION record on each boot.
929  */
930 static int osp_sync_thread(void *_arg)
931 {
932         struct osp_device       *d = _arg;
933         struct ptlrpc_thread    *thread = &d->opd_syn_thread;
934         struct l_wait_info       lwi = { 0 };
935         struct llog_ctxt        *ctxt;
936         struct obd_device       *obd = d->opd_obd;
937         struct llog_handle      *llh;
938         struct lu_env            env;
939         int                      rc, count;
940
941         ENTRY;
942
943         rc = lu_env_init(&env, LCT_LOCAL);
944         if (rc) {
945                 CERROR("%s: can't initialize env: rc = %d\n",
946                        obd->obd_name, rc);
947                 RETURN(rc);
948         }
949
950         spin_lock(&d->opd_syn_lock);
951         thread->t_flags = SVC_RUNNING;
952         spin_unlock(&d->opd_syn_lock);
953         wake_up(&thread->t_ctl_waitq);
954
955         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
956         if (ctxt == NULL) {
957                 CERROR("can't get appropriate context\n");
958                 GOTO(out, rc = -EINVAL);
959         }
960
961         llh = ctxt->loc_handle;
962         if (llh == NULL) {
963                 CERROR("can't get llh\n");
964                 llog_ctxt_put(ctxt);
965                 GOTO(out, rc = -EINVAL);
966         }
967
968         rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
969         LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
970                  "%lu changes, %u in progress, %u in flight: %d\n",
971                  d->opd_syn_changes, d->opd_syn_rpc_in_progress,
972                  d->opd_syn_rpc_in_flight, rc);
973
974         /* we don't expect llog_process_thread() to exit till umount */
975         LASSERTF(thread->t_flags != SVC_RUNNING,
976                  "%lu changes, %u in progress, %u in flight\n",
977                  d->opd_syn_changes, d->opd_syn_rpc_in_progress,
978                  d->opd_syn_rpc_in_flight);
979
980         /* wait till all the requests are completed */
981         count = 0;
982         while (d->opd_syn_rpc_in_progress > 0) {
983                 osp_sync_process_committed(&env, d);
984
985                 lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
986                 rc = l_wait_event(d->opd_syn_waitq,
987                                   d->opd_syn_rpc_in_progress == 0,
988                                   &lwi);
989                 if (rc == -ETIMEDOUT)
990                         count++;
991                 LASSERTF(count < 10, "%s: %d %d %sempty\n",
992                          d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
993                          d->opd_syn_rpc_in_flight,
994                          list_empty(&d->opd_syn_committed_there) ? "" : "!");
995
996         }
997
998         llog_cat_close(&env, llh);
999         rc = llog_cleanup(&env, ctxt);
1000         if (rc)
1001                 CERROR("can't cleanup llog: %d\n", rc);
1002 out:
1003         LASSERTF(d->opd_syn_rpc_in_progress == 0,
1004                  "%s: %d %d %sempty\n",
1005                  d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
1006                  d->opd_syn_rpc_in_flight,
1007                  list_empty(&d->opd_syn_committed_there) ? "" : "!");
1008
1009         thread->t_flags = SVC_STOPPED;
1010
1011         wake_up(&thread->t_ctl_waitq);
1012
1013         lu_env_fini(&env);
1014
1015         RETURN(0);
1016 }
1017
1018 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
1019 {
1020         struct osp_thread_info  *osi = osp_env_info(env);
1021         struct lu_fid           *fid = &osi->osi_fid;
1022         struct llog_handle      *lgh = NULL;
1023         struct obd_device       *obd = d->opd_obd;
1024         struct llog_ctxt        *ctxt;
1025         int                     rc;
1026
1027         ENTRY;
1028
1029         LASSERT(obd);
1030
1031         /*
1032          * open llog corresponding to our OST
1033          */
1034         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1035         obd->obd_lvfs_ctxt.dt = d->opd_storage;
1036
1037         if (d->opd_connect_mdt)
1038                 lu_local_obj_fid(fid, SLAVE_LLOG_CATALOGS_OID);
1039         else
1040                 lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
1041
1042         rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
1043                                    &osi->osi_cid, fid);
1044         if (rc) {
1045                 CERROR("%s: can't get id from catalogs: rc = %d\n",
1046                        obd->obd_name, rc);
1047                 RETURN(rc);
1048         }
1049
1050         CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
1051                obd->obd_name, d->opd_index,
1052                POSTID(&osi->osi_cid.lci_logid.lgl_oi),
1053                osi->osi_cid.lci_logid.lgl_ogen);
1054
1055         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, obd,
1056                         &osp_mds_ost_orig_logops);
1057         if (rc)
1058                 RETURN(rc);
1059
1060         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1061         LASSERT(ctxt);
1062
1063         if (likely(logid_id(&osi->osi_cid.lci_logid) != 0)) {
1064                 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
1065                                LLOG_OPEN_EXISTS);
1066                 /* re-create llog if it is missing */
1067                 if (rc == -ENOENT)
1068                         logid_set_id(&osi->osi_cid.lci_logid, 0);
1069                 else if (rc < 0)
1070                         GOTO(out_cleanup, rc);
1071         }
1072
1073         if (unlikely(logid_id(&osi->osi_cid.lci_logid) == 0)) {
1074                 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
1075                 if (rc < 0)
1076                         GOTO(out_cleanup, rc);
1077                 osi->osi_cid.lci_logid = lgh->lgh_id;
1078         }
1079
1080         LASSERT(lgh != NULL);
1081         ctxt->loc_handle = lgh;
1082
1083         rc = llog_cat_init_and_process(env, lgh);
1084         if (rc)
1085                 GOTO(out_close, rc);
1086
1087         rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
1088                                    &osi->osi_cid, fid);
1089         if (rc)
1090                 GOTO(out_close, rc);
1091
1092         /*
1093          * put a mark in the llog till which we'll be processing
1094          * old records restless
1095          */
1096         d->opd_syn_generation.mnt_cnt = cfs_time_current();
1097         d->opd_syn_generation.conn_cnt = cfs_time_current();
1098
1099         osi->osi_hdr.lrh_type = LLOG_GEN_REC;
1100         osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
1101
1102         memcpy(&osi->osi_gen.lgr_gen, &d->opd_syn_generation,
1103                sizeof(osi->osi_gen.lgr_gen));
1104
1105         rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie);
1106         if (rc < 0)
1107                 GOTO(out_close, rc);
1108         llog_ctxt_put(ctxt);
1109         RETURN(0);
1110 out_close:
1111         llog_cat_close(env, lgh);
1112 out_cleanup:
1113         llog_cleanup(env, ctxt);
1114         RETURN(rc);
1115 }
1116
1117 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
1118 {
1119         struct llog_ctxt *ctxt;
1120
1121         ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
1122         if (ctxt != NULL)
1123                 llog_cat_close(env, ctxt->loc_handle);
1124         llog_cleanup(env, ctxt);
1125 }
1126
1127 /*
1128  * initializes sync component of OSP
1129  */
1130 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1131 {
1132         struct l_wait_info       lwi = { 0 };
1133         struct task_struct      *task;
1134         int                      rc;
1135
1136         ENTRY;
1137
1138         rc = osp_sync_id_traction_init(d);
1139         if (rc)
1140                 RETURN(rc);
1141
1142         /*
1143          * initialize llog storing changes
1144          */
1145         rc = osp_sync_llog_init(env, d);
1146         if (rc) {
1147                 CERROR("%s: can't initialize llog: rc = %d\n",
1148                        d->opd_obd->obd_name, rc);
1149                 GOTO(err_id, rc);
1150         }
1151
1152         /*
1153          * Start synchronization thread
1154          */
1155         d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
1156         d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
1157         spin_lock_init(&d->opd_syn_lock);
1158         init_waitqueue_head(&d->opd_syn_waitq);
1159         init_waitqueue_head(&d->opd_syn_barrier_waitq);
1160         init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
1161         INIT_LIST_HEAD(&d->opd_syn_committed_there);
1162
1163         task = kthread_run(osp_sync_thread, d, "osp-syn-%u-%u",
1164                            d->opd_index, d->opd_group);
1165         if (IS_ERR(task)) {
1166                 rc = PTR_ERR(task);
1167                 CERROR("%s: cannot start sync thread: rc = %d\n",
1168                        d->opd_obd->obd_name, rc);
1169                 GOTO(err_llog, rc);
1170         }
1171
1172         l_wait_event(d->opd_syn_thread.t_ctl_waitq,
1173                      osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1174
1175         RETURN(0);
1176 err_llog:
1177         osp_sync_llog_fini(env, d);
1178 err_id:
1179         osp_sync_id_traction_fini(d);
1180         return rc;
1181 }
1182
1183 int osp_sync_fini(struct osp_device *d)
1184 {
1185         struct ptlrpc_thread *thread = &d->opd_syn_thread;
1186
1187         ENTRY;
1188
1189         thread->t_flags = SVC_STOPPING;
1190         wake_up(&d->opd_syn_waitq);
1191         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1192
1193         /*
1194          * unregister transaction callbacks only when sync thread
1195          * has finished operations with llog
1196          */
1197         osp_sync_id_traction_fini(d);
1198
1199         RETURN(0);
1200 }
1201
1202 static DEFINE_MUTEX(osp_id_tracker_sem);
1203 static struct list_head osp_id_tracker_list =
1204                 LIST_HEAD_INIT(osp_id_tracker_list);
1205
1206 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
1207 {
1208         struct osp_id_tracker   *tr = cookie;
1209         struct osp_device       *d;
1210         struct osp_txn_info     *txn;
1211
1212         LASSERT(tr);
1213
1214         txn = osp_txn_info(&th->th_ctx);
1215         if (txn == NULL || txn->oti_current_id < tr->otr_committed_id)
1216                 return;
1217
1218         spin_lock(&tr->otr_lock);
1219         if (likely(txn->oti_current_id > tr->otr_committed_id)) {
1220                 CDEBUG(D_OTHER, "committed: %u -> %u\n",
1221                        tr->otr_committed_id, txn->oti_current_id);
1222                 tr->otr_committed_id = txn->oti_current_id;
1223
1224                 list_for_each_entry(d, &tr->otr_wakeup_list,
1225                                     opd_syn_ontrack) {
1226                         d->opd_syn_last_committed_id = tr->otr_committed_id;
1227                         wake_up(&d->opd_syn_waitq);
1228                 }
1229         }
1230         spin_unlock(&tr->otr_lock);
1231 }
1232
1233 static int osp_sync_id_traction_init(struct osp_device *d)
1234 {
1235         struct osp_id_tracker   *tr, *found = NULL;
1236         int                      rc = 0;
1237
1238         LASSERT(d);
1239         LASSERT(d->opd_storage);
1240         LASSERT(d->opd_syn_tracker == NULL);
1241         INIT_LIST_HEAD(&d->opd_syn_ontrack);
1242
1243         mutex_lock(&osp_id_tracker_sem);
1244         list_for_each_entry(tr, &osp_id_tracker_list, otr_list) {
1245                 if (tr->otr_dev == d->opd_storage) {
1246                         LASSERT(atomic_read(&tr->otr_refcount));
1247                         atomic_inc(&tr->otr_refcount);
1248                         d->opd_syn_tracker = tr;
1249                         found = tr;
1250                         break;
1251                 }
1252         }
1253
1254         if (found == NULL) {
1255                 rc = -ENOMEM;
1256                 OBD_ALLOC_PTR(tr);
1257                 if (tr) {
1258                         d->opd_syn_tracker = tr;
1259                         spin_lock_init(&tr->otr_lock);
1260                         tr->otr_dev = d->opd_storage;
1261                         tr->otr_next_id = 1;
1262                         tr->otr_committed_id = 0;
1263                         atomic_set(&tr->otr_refcount, 1);
1264                         INIT_LIST_HEAD(&tr->otr_wakeup_list);
1265                         list_add(&tr->otr_list, &osp_id_tracker_list);
1266                         tr->otr_tx_cb.dtc_txn_commit =
1267                                                 osp_sync_tracker_commit_cb;
1268                         tr->otr_tx_cb.dtc_cookie = tr;
1269                         tr->otr_tx_cb.dtc_tag = LCT_MD_THREAD;
1270                         dt_txn_callback_add(d->opd_storage, &tr->otr_tx_cb);
1271                         rc = 0;
1272                 }
1273         }
1274         mutex_unlock(&osp_id_tracker_sem);
1275
1276         return rc;
1277 }
1278
1279 static void osp_sync_id_traction_fini(struct osp_device *d)
1280 {
1281         struct osp_id_tracker *tr;
1282
1283         ENTRY;
1284
1285         LASSERT(d);
1286         tr = d->opd_syn_tracker;
1287         if (tr == NULL) {
1288                 EXIT;
1289                 return;
1290         }
1291
1292         osp_sync_remove_from_tracker(d);
1293
1294         mutex_lock(&osp_id_tracker_sem);
1295         if (atomic_dec_and_test(&tr->otr_refcount)) {
1296                 dt_txn_callback_del(d->opd_storage, &tr->otr_tx_cb);
1297                 LASSERT(list_empty(&tr->otr_wakeup_list));
1298                 list_del(&tr->otr_list);
1299                 OBD_FREE_PTR(tr);
1300                 d->opd_syn_tracker = NULL;
1301         }
1302         mutex_unlock(&osp_id_tracker_sem);
1303
1304         EXIT;
1305 }
1306
1307 /*
1308  * generates id for the tracker
1309  */
1310 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
1311 {
1312         struct osp_id_tracker *tr;
1313
1314         tr = d->opd_syn_tracker;
1315         LASSERT(tr);
1316
1317         /* XXX: we can improve this introducing per-cpu preallocated ids? */
1318         spin_lock(&tr->otr_lock);
1319         if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
1320                 spin_unlock(&tr->otr_lock);
1321                 CERROR("%s: next %u, last synced %lu\n",
1322                        d->opd_obd->obd_name, tr->otr_next_id,
1323                        d->opd_syn_last_used_id);
1324                 LBUG();
1325         }
1326
1327         if (id == 0)
1328                 id = tr->otr_next_id++;
1329         if (id > d->opd_syn_last_used_id)
1330                 d->opd_syn_last_used_id = id;
1331         if (list_empty(&d->opd_syn_ontrack))
1332                 list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
1333         spin_unlock(&tr->otr_lock);
1334         CDEBUG(D_OTHER, "new id %u\n", (unsigned) id);
1335
1336         return id;
1337 }
1338
1339 static void osp_sync_remove_from_tracker(struct osp_device *d)
1340 {
1341         struct osp_id_tracker *tr;
1342
1343         tr = d->opd_syn_tracker;
1344         LASSERT(tr);
1345
1346         if (list_empty(&d->opd_syn_ontrack))
1347                 return;
1348
1349         spin_lock(&tr->otr_lock);
1350         list_del_init(&d->opd_syn_ontrack);
1351         spin_unlock(&tr->otr_lock);
1352 }
1353