Whamcloud - gitweb
2b2d5223256b786022b2584a502fd26abed03b7b
[fs/lustre-release.git] / lustre / ptlrpc / target.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 Common Target
37  * These are common function for MDT and OST recovery-related functionality
38  *
39  *   Author: Mikhail Pershin <tappro@sun.com>
40  */
41
42 #include <obd.h>
43 #include <lustre_fsfilt.h>
44
45 /**
46  * Update client data in last_rcvd file. An obd API
47  */
48 static int obt_client_data_update(struct obd_export *exp)
49 {
50         struct lu_export_data *led = &exp->exp_target_data;
51         struct obd_device_target *obt = &exp->exp_obd->u.obt;
52         struct lu_target *lut = class_exp2tgt(exp);
53         loff_t off = led->led_lr_off;
54         int rc = 0;
55
56         rc = fsfilt_write_record(exp->exp_obd, obt->obt_rcvd_filp,
57                                  led->led_lcd, sizeof(*led->led_lcd), &off, 0);
58
59         CDEBUG(D_INFO, "update client idx %u last_epoch %#x (%#x)\n",
60                led->led_lr_idx, le32_to_cpu(led->led_lcd->lcd_last_epoch),
61                le32_to_cpu(lut->lut_lsd.lsd_start_epoch));
62
63         return rc;
64 }
65
66 /**
67  * Update server data in last_rcvd file. An obd API
68  */
69 int obt_server_data_update(struct lu_target *lut, int force_sync)
70 {
71         struct obd_device_target *obt = &lut->lut_obd->u.obt;
72         loff_t off = 0;
73         int rc;
74         ENTRY;
75
76         CDEBUG(D_SUPER,
77                "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
78                lut->lut_lsd.lsd_uuid,
79                le64_to_cpu(lut->lut_lsd.lsd_mount_count),
80                le64_to_cpu(lut->lut_lsd.lsd_last_transno));
81
82         rc = fsfilt_write_record(lut->lut_obd, obt->obt_rcvd_filp,
83                                  &lut->lut_lsd, sizeof(lut->lut_lsd),
84                                  &off, force_sync);
85         if (rc)
86                 CERROR("error writing lr_server_data: rc = %d\n", rc);
87
88         RETURN(rc);
89 }
90
91 /**
92  * Update client epoch with server's one
93  */
94 void obt_client_epoch_update(struct obd_export *exp)
95 {
96         struct lsd_client_data *lcd = exp->exp_target_data.led_lcd;
97         struct lu_target *lut = class_exp2tgt(exp);
98
99         /** VBR: set client last_epoch to current epoch */
100         if (le32_to_cpu(lcd->lcd_last_epoch) >=
101             le32_to_cpu(lut->lut_lsd.lsd_start_epoch))
102                 return;
103         lcd->lcd_last_epoch = lut->lut_lsd.lsd_start_epoch;
104         obt_client_data_update(exp);
105 }
106
107 /**
108  * Increment server epoch. An obd API
109  */
110 static void obt_boot_epoch_update(struct lu_target *lut)
111 {
112         struct obd_device *obd = lut->lut_obd;
113         __u32 start_epoch;
114         struct ptlrpc_request *req;
115         cfs_list_t client_list;
116
117         cfs_spin_lock(&lut->lut_translock);
118         start_epoch = lr_epoch(le64_to_cpu(lut->lut_last_transno)) + 1;
119         lut->lut_last_transno = cpu_to_le64((__u64)start_epoch <<
120                                             LR_EPOCH_BITS);
121         lut->lut_lsd.lsd_start_epoch = cpu_to_le32(start_epoch);
122         cfs_spin_unlock(&lut->lut_translock);
123
124         CFS_INIT_LIST_HEAD(&client_list);
125         cfs_spin_lock_bh(&obd->obd_processing_task_lock);
126         cfs_list_splice_init(&obd->obd_final_req_queue, &client_list);
127         cfs_spin_unlock_bh(&obd->obd_processing_task_lock);
128
129         /**
130          * go through list of exports participated in recovery and
131          * set new epoch for them
132          */
133         cfs_list_for_each_entry(req, &client_list, rq_list) {
134                 LASSERT(!req->rq_export->exp_delayed);
135                 obt_client_epoch_update(req->rq_export);
136         }
137         /** return list back at once */
138         cfs_spin_lock_bh(&obd->obd_processing_task_lock);
139         cfs_list_splice_init(&client_list, &obd->obd_final_req_queue);
140         cfs_spin_unlock_bh(&obd->obd_processing_task_lock);
141         obt_server_data_update(lut, 1);
142 }
143
144 /**
145  * write data in last_rcvd file.
146  */
147 static int lut_last_rcvd_write(const struct lu_env *env, struct lu_target *lut,
148                                const struct lu_buf *buf, loff_t *off, int sync)
149 {
150         struct thandle *th;
151         struct txn_param p;
152         int rc, credits;
153         ENTRY;
154
155         credits = lut->lut_bottom->dd_ops->dt_credit_get(env, lut->lut_bottom,
156                                                          DTO_WRITE_BLOCK);
157         txn_param_init(&p, credits);
158
159         th = dt_trans_start(env, lut->lut_bottom, &p);
160         if (IS_ERR(th))
161                 RETURN(PTR_ERR(th));
162
163         rc = dt_record_write(env, lut->lut_last_rcvd, buf, off, th);
164         dt_trans_stop(env, lut->lut_bottom, th);
165
166         CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
167                "uuid = %s\nlast_transno = "LPU64"\n",
168                rc, lut->lut_lsd.lsd_uuid, lut->lut_lsd.lsd_last_transno);
169
170         RETURN(rc);
171 }
172
173 /**
174  * Free in-memory data for client slot related to export.
175  */
176 void lut_client_free(struct obd_export *exp)
177 {
178         struct lu_export_data *led = &exp->exp_target_data;
179         struct lu_target *lut = class_exp2tgt(exp);
180
181         OBD_FREE_PTR(led->led_lcd);
182         led->led_lcd = NULL;
183         /* Clear bit when lcd is freed */
184         spin_lock(&lut->lut_client_bitmap_lock);
185         if (!test_and_clear_bit(led->led_lr_idx, lut->lut_client_bitmap)) {
186                 CERROR("%s: client %u bit already clear in bitmap\n",
187                        exp->exp_obd->obd_name, led->led_lr_idx);
188                 LBUG();
189         }
190         spin_unlock(&lut->lut_client_bitmap_lock);
191 }
192 EXPORT_SYMBOL(lut_client_free);
193
194 /**
195  * Update client data in last_rcvd
196  */
197 int lut_client_data_update(const struct lu_env *env, struct obd_export *exp)
198 {
199         struct lu_export_data *led = &exp->exp_target_data;
200         struct lu_target *lut = class_exp2tgt(exp);
201         struct lsd_client_data tmp_lcd;
202         loff_t tmp_off = led->led_lr_off;
203         struct lu_buf tmp_buf = {
204                                         .lb_buf = &tmp_lcd,
205                                         .lb_len = sizeof(tmp_lcd)
206                                 };
207         int rc = 0;
208
209         lcd_cpu_to_le(led->led_lcd, &tmp_lcd);
210         LASSERT(lut->lut_last_rcvd);
211         rc = lut_last_rcvd_write(env, lut, &tmp_buf, &tmp_off, 0);
212
213         return rc;
214 }
215
216 /**
217  * Update server data in last_rcvd
218  */
219 static int lut_server_data_update(const struct lu_env *env,
220                                   struct lu_target *lut, int sync)
221 {
222         struct lr_server_data tmp_lsd;
223         loff_t tmp_off = 0;
224         struct lu_buf tmp_buf = {
225                                         .lb_buf = &tmp_lsd,
226                                         .lb_len = sizeof(tmp_lsd)
227                                 };
228         int rc = 0;
229         ENTRY;
230
231         CDEBUG(D_SUPER,
232                "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
233                lut->lut_lsd.lsd_uuid, lut->lut_mount_count,
234                lut->lut_last_transno);
235
236         cfs_spin_lock(&lut->lut_translock);
237         lut->lut_lsd.lsd_last_transno = lut->lut_last_transno;
238         cfs_spin_unlock(&lut->lut_translock);
239
240         lsd_cpu_to_le(&lut->lut_lsd, &tmp_lsd);
241         if (lut->lut_last_rcvd != NULL)
242                 rc = lut_last_rcvd_write(env, lut, &tmp_buf, &tmp_off, sync);
243         RETURN(rc);
244 }
245
246 void lut_client_epoch_update(const struct lu_env *env, struct obd_export *exp)
247 {
248         struct lsd_client_data *lcd = exp->exp_target_data.led_lcd;
249         struct lu_target *lut = class_exp2tgt(exp);
250
251         LASSERT(lut->lut_bottom);
252         /** VBR: set client last_epoch to current epoch */
253         if (lcd->lcd_last_epoch >= lut->lut_lsd.lsd_start_epoch)
254                 return;
255         lcd->lcd_last_epoch = lut->lut_lsd.lsd_start_epoch;
256         lut_client_data_update(env, exp);
257 }
258
259 /**
260  * Update boot epoch when recovery ends
261  */
262 void lut_boot_epoch_update(struct lu_target *lut)
263 {
264         struct lu_env env;
265         struct ptlrpc_request *req;
266         __u32 start_epoch;
267         cfs_list_t client_list;
268         int rc;
269
270         if (lut->lut_obd->obd_stopping)
271                 return;
272         /** Increase server epoch after recovery */
273         if (lut->lut_bottom == NULL)
274                 return obt_boot_epoch_update(lut);
275
276         rc = lu_env_init(&env, LCT_DT_THREAD);
277         if (rc) {
278                 CERROR("Can't initialize environment rc=%i\n", rc);
279                 return;
280         }
281
282         cfs_spin_lock(&lut->lut_translock);
283         start_epoch = lr_epoch(lut->lut_last_transno) + 1;
284         lut->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
285         lut->lut_lsd.lsd_start_epoch = start_epoch;
286         cfs_spin_unlock(&lut->lut_translock);
287
288         CFS_INIT_LIST_HEAD(&client_list);
289         /**
290          * The recovery is not yet finished and final queue can still be updated
291          * with resend requests. Move final list to separate one for processing
292          */
293         cfs_spin_lock_bh(&lut->lut_obd->obd_processing_task_lock);
294         cfs_list_splice_init(&lut->lut_obd->obd_final_req_queue, &client_list);
295         cfs_spin_unlock_bh(&lut->lut_obd->obd_processing_task_lock);
296
297         /**
298          * go through list of exports participated in recovery and
299          * set new epoch for them
300          */
301         cfs_list_for_each_entry(req, &client_list, rq_list) {
302                 LASSERT(!req->rq_export->exp_delayed);
303                 if (!req->rq_export->exp_vbr_failed)
304                         lut_client_epoch_update(&env, req->rq_export);
305         }
306         /** return list back at once */
307         cfs_spin_lock_bh(&lut->lut_obd->obd_processing_task_lock);
308         cfs_list_splice_init(&client_list, &lut->lut_obd->obd_final_req_queue);
309         cfs_spin_unlock_bh(&lut->lut_obd->obd_processing_task_lock);
310         /** update server epoch */
311         lut_server_data_update(&env, lut, 1);
312         lu_env_fini(&env);
313 }
314 EXPORT_SYMBOL(lut_boot_epoch_update);
315
316 /**
317  * commit callback, need to update last_commited value
318  */
319 void lut_cb_last_committed(struct lu_target *lut, __u64 transno,
320                            void *data, int err)
321 {
322         struct obd_export *exp = data;
323         LASSERT(exp->exp_obd == lut->lut_obd);
324         cfs_spin_lock(&lut->lut_translock);
325         if (transno > lut->lut_obd->obd_last_committed)
326                 lut->lut_obd->obd_last_committed = transno;
327
328         LASSERT(exp);
329         if (transno > exp->exp_last_committed) {
330                 exp->exp_last_committed = transno;
331                 cfs_spin_unlock(&lut->lut_translock);
332                 ptlrpc_commit_replies(exp);
333         } else {
334                 cfs_spin_unlock(&lut->lut_translock);
335         }
336         class_export_cb_put(exp);
337         if (transno)
338                 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
339                        lut->lut_obd->obd_name, transno);
340 }
341 EXPORT_SYMBOL(lut_cb_last_committed);
342
343 void lut_cb_client(struct lu_target *lut, __u64 transno,
344                        void *data, int err)
345 {
346         LASSERT(lut->lut_obd);
347         target_client_add_cb(lut->lut_obd, transno, data, err);
348 }
349 EXPORT_SYMBOL(lut_cb_client);
350
351 int lut_init(const struct lu_env *env, struct lu_target *lut,
352              struct obd_device *obd, struct dt_device *dt)
353 {
354         struct lu_fid fid;
355         struct dt_object *o;
356         int rc = 0;
357         ENTRY;
358
359         LASSERT(lut);
360         LASSERT(obd);
361         lut->lut_obd = obd;
362         lut->lut_bottom = dt;
363         lut->lut_last_rcvd = NULL;
364         obd->u.obt.obt_lut = lut;
365
366         cfs_spin_lock_init(&lut->lut_translock);
367         cfs_spin_lock_init(&lut->lut_client_bitmap_lock);
368
369         OBD_ALLOC(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
370         if (lut->lut_client_bitmap == NULL)
371                 RETURN(-ENOMEM);
372
373         /** obdfilter has no lu_device stack yet */
374         if (dt == NULL)
375                 RETURN(rc);
376         o = dt_store_open(env, lut->lut_bottom, "", LAST_RCVD, &fid);
377         if (!IS_ERR(o)) {
378                 lut->lut_last_rcvd = o;
379         } else {
380                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
381                 lut->lut_client_bitmap = NULL;
382                 rc = PTR_ERR(o);
383                 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
384         }
385
386         RETURN(rc);
387 }
388 EXPORT_SYMBOL(lut_init);
389
390 void lut_fini(const struct lu_env *env, struct lu_target *lut)
391 {
392         ENTRY;
393
394         if (lut->lut_client_bitmap) {
395                 OBD_FREE(lut->lut_client_bitmap, LR_MAX_CLIENTS >> 3);
396                 lut->lut_client_bitmap = NULL;
397         }
398         if (lut->lut_last_rcvd) {
399                 lu_object_put(env, &lut->lut_last_rcvd->do_lu);
400                 lut->lut_last_rcvd = NULL;
401         }
402         EXIT;
403 }
404 EXPORT_SYMBOL(lut_fini);