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