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