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