Whamcloud - gitweb
0f0c83a458331956b1393664b7067ba1c0f66346
[fs/lustre-release.git] / lustre / mgs / mgs_nids.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) 2011, Whamcloud, Inc.
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/mgs/mgs_nids.c
37  *
38  * NID table management for lustre.
39  *
40  * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MGS
44 #define D_MGS D_CONFIG
45
46 #ifdef __KERNEL__
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
49 #include <linux/fs.h>
50 #endif
51
52 #include <obd.h>
53 #include <obd_lov.h>
54 #include <obd_class.h>
55 #include <lustre_log.h>
56 #include <obd_ost.h>
57 #include <libcfs/list.h>
58 #include <linux/lvfs.h>
59 #include <lustre_fsfilt.h>
60 #include <lustre_disk.h>
61 #include <lustre_param.h>
62 #include "mgs_internal.h"
63
64 static unsigned int ir_timeout;
65
66 static int nidtbl_is_sane(struct mgs_nidtbl *tbl)
67 {
68         struct mgs_nidtbl_target *tgt;
69         int version = 0;
70
71         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
72         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
73                 if (!tgt->mnt_version)
74                         continue;
75
76                 if (version >= tgt->mnt_version)
77                         return 0;
78
79                 version = tgt->mnt_version;
80         }
81         return 1;
82 }
83
84 /**
85  * Fetch nidtbl entries whose version are not less than @version
86  * nidtbl entries will be packed in @pages by @unit_size units - entries
87  * shouldn't cross unit boundaries.
88  */
89 static int mgs_nidtbl_read(struct obd_export *exp, struct mgs_nidtbl *tbl,
90                            struct mgs_config_res *res, cfs_page_t **pages,
91                            int nrpages, int units_total, int unit_size)
92 {
93         struct mgs_nidtbl_target *tgt;
94         struct mgs_nidtbl_entry  *entry;
95         struct mgs_nidtbl_entry  *last_in_unit = NULL;
96         struct mgs_target_info   *mti;
97         __u64 version = res->mcr_offset;
98         bool nobuf = false;
99         void *buf = NULL;
100         int bytes_in_unit = 0;
101         int units_in_page = 0;
102         int index = 0;
103         int rc = 0;
104         ENTRY;
105
106         /* make sure unit_size is power 2 */
107         LASSERT((unit_size & (unit_size - 1)) == 0);
108         LASSERT(nrpages << CFS_PAGE_SHIFT >= units_total * unit_size);
109
110         cfs_mutex_lock(&tbl->mn_lock);
111         LASSERT(nidtbl_is_sane(tbl));
112
113         /* no more entries ? */
114         if (version > tbl->mn_version) {
115                 version = tbl->mn_version;
116                 goto out;
117         }
118
119         /* iterate over all targets to compose a bitmap by the type of llog.
120          * If the llog is for MDTs, llog entries for OSTs will be returned;
121          * otherwise, it's for clients, then llog entries for both OSTs and
122          * MDTs will be returned.
123          */
124         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
125                 int entry_len = sizeof(*entry);
126
127                 if (tgt->mnt_version < version)
128                         continue;
129
130                 /* write target recover information */
131                 mti  = &tgt->mnt_mti;
132                 LASSERT(mti->mti_nid_count < MTI_NIDS_MAX);
133                 entry_len += mti->mti_nid_count * sizeof(lnet_nid_t);
134
135                 if (entry_len > unit_size) {
136                         CWARN("nidtbl: too large entry: entry length %d,"
137                               "unit size: %d\n", entry_len, unit_size);
138                         GOTO(out, rc = -EOVERFLOW);
139                 }
140
141                 if (bytes_in_unit < entry_len) {
142                         if (units_total == 0) {
143                                 nobuf = true;
144                                 break;
145                         }
146
147                         /* check if we need to consume remaining bytes. */
148                         if (last_in_unit != NULL && bytes_in_unit) {
149 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
150                                 /* May need to swab back to update the length.*/
151                                 if (exp->exp_need_mne_swab)
152                                         lustre_swab_mgs_nidtbl_entry(last_in_unit);
153 #endif
154                                 last_in_unit->mne_length += bytes_in_unit;
155 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
156                                 if (exp->exp_need_mne_swab)
157                                         lustre_swab_mgs_nidtbl_entry(last_in_unit);
158 #endif
159                                 rc  += bytes_in_unit;
160                                 buf += bytes_in_unit;
161                                 last_in_unit = NULL;
162                         }
163                         LASSERT((rc & (unit_size - 1)) == 0);
164
165                         if (units_in_page == 0) {
166                                 /* allocate a new page */
167                                 pages[index] = cfs_alloc_page(CFS_ALLOC_STD);
168                                 if (pages[index] == NULL) {
169                                         rc = -ENOMEM;
170                                         break;
171                                 }
172
173                                 /* destroy previous map */
174                                 if (index > 0)
175                                         cfs_kunmap(pages[index - 1]);
176
177                                 /* reassign buffer */
178                                 buf = cfs_kmap(pages[index]);
179                                 ++index;
180
181                                 units_in_page = CFS_PAGE_SIZE / unit_size;
182                                 LASSERT(units_in_page > 0);
183                         }
184
185                         /* allocate an unit */
186                         LASSERT(((long)buf & (unit_size - 1)) == 0);
187                         bytes_in_unit = unit_size;
188                         --units_in_page;
189                         --units_total;
190                 }
191
192                 /* fill in entry. */
193                 entry = (struct mgs_nidtbl_entry *)buf;
194                 entry->mne_version   = tgt->mnt_version;
195                 entry->mne_instance  = mti->mti_instance;
196                 entry->mne_index     = mti->mti_stripe_index;
197                 entry->mne_length    = entry_len;
198                 entry->mne_type      = tgt->mnt_type;
199                 entry->mne_nid_type  = 0;
200                 entry->mne_nid_size  = sizeof(lnet_nid_t);
201                 entry->mne_nid_count = mti->mti_nid_count;
202                 memcpy(entry->u.nids, mti->mti_nids,
203                        mti->mti_nid_count * sizeof(lnet_nid_t));
204
205 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
206                 /* For LU-1644, swab entry for 2.2 clients. */
207                 if (exp->exp_need_mne_swab)
208                         lustre_swab_mgs_nidtbl_entry(entry);
209 #endif
210
211                 version = tgt->mnt_version;
212                 rc     += entry_len;
213                 buf    += entry_len;
214
215                 bytes_in_unit -= entry_len;
216                 last_in_unit   = entry;
217
218                 CDEBUG(D_MGS, "fsname %s, entry size %d, pages %d/%d/%d/%d.\n",
219                        tbl->mn_fsdb->fsdb_name, entry_len,
220                        bytes_in_unit, index, nrpages, units_total);
221         }
222         if (index > 0)
223                 cfs_kunmap(pages[index - 1]);
224 out:
225         LASSERT(version <= tbl->mn_version);
226         res->mcr_size = tbl->mn_version;
227         res->mcr_offset = nobuf ? version : tbl->mn_version;
228         cfs_mutex_unlock(&tbl->mn_lock);
229         LASSERT(ergo(version == 1, rc == 0)); /* get the log first time */
230
231         CDEBUG(D_MGS, "Read IR logs %s return with %d, version %llu\n",
232                tbl->mn_fsdb->fsdb_name, rc, version);
233         RETURN(rc);
234 }
235
236 static int nidtbl_update_version(const struct lu_env *env,
237                                  struct mgs_device *mgs,
238                                  struct mgs_nidtbl *tbl)
239 {
240         struct lvfs_run_ctxt saved;
241         struct file         *file = NULL;
242         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
243         u64                  version;
244         loff_t               off = 0;
245         int                  rc;
246         struct obd_device   *obd = tbl->mn_fsdb->fsdb_obd;
247         ENTRY;
248
249         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
250         LASSERT(sizeof(filename) < 32);
251
252         sprintf(filename, "%s/%s",
253                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
254
255         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
256
257         file = l_filp_open(filename, O_RDWR|O_CREAT, 0660);
258         if (!IS_ERR(file)) {
259                 version = cpu_to_le64(tbl->mn_version);
260                 rc = lustre_fwrite(file, &version, sizeof(version), &off);
261                 if (rc == sizeof(version))
262                         rc = 0;
263                 filp_close(file, 0);
264                 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
265         } else {
266                 rc = PTR_ERR(file);
267         }
268
269         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
270         RETURN(rc);
271 }
272
273 #define MGS_NIDTBL_VERSION_INIT 2
274
275 static int nidtbl_read_version(const struct lu_env *env,
276                                struct mgs_device *mgs, struct mgs_nidtbl *tbl)
277 {
278         struct lvfs_run_ctxt saved;
279         struct file         *file = NULL;
280         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
281         u64                  version;
282         loff_t               off = 0;
283         int                  rc;
284         struct obd_device   *obd = tbl->mn_fsdb->fsdb_obd;
285         ENTRY;
286
287         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
288         LASSERT(sizeof(filename) < 32);
289
290         sprintf(filename, "%s/%s",
291                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
292
293         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
294
295         file = l_filp_open(filename, O_RDONLY, 0);
296         if (!IS_ERR(file)) {
297                 rc = lustre_fread(file, &version, sizeof(version), &off);
298                 if (rc == sizeof(version))
299                         rc = cpu_to_le64(version);
300                 else if (rc == 0)
301                         rc = MGS_NIDTBL_VERSION_INIT;
302                 else
303                         CERROR("read version file %s error %d\n", filename, rc);
304                 filp_close(file, 0);
305         } else {
306                 rc = PTR_ERR(file);
307                 if (rc == -ENOENT)
308                         rc = MGS_NIDTBL_VERSION_INIT;
309         }
310
311         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
312         RETURN(rc);
313 }
314
315 static int mgs_nidtbl_write(const struct lu_env *env, struct fs_db *fsdb,
316                             struct mgs_target_info *mti)
317 {
318         struct mgs_nidtbl        *tbl;
319         struct mgs_nidtbl_target *tgt;
320         bool found = false;
321         int type   = mti->mti_flags & LDD_F_SV_TYPE_MASK;
322         int rc     = 0;
323         ENTRY;
324
325         type &= ~LDD_F_SV_TYPE_MGS;
326         LASSERT(type != 0);
327
328         tbl = &fsdb->fsdb_nidtbl;
329         cfs_mutex_lock(&tbl->mn_lock);
330         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
331                 struct mgs_target_info *info = &tgt->mnt_mti;
332                 if (type == tgt->mnt_type &&
333                     mti->mti_stripe_index == info->mti_stripe_index) {
334                         found = true;
335                         break;
336                 }
337         }
338         if (!found) {
339                 OBD_ALLOC_PTR(tgt);
340                 if (tgt == NULL)
341                         GOTO(out, rc = -ENOMEM);
342
343                 CFS_INIT_LIST_HEAD(&tgt->mnt_list);
344                 tgt->mnt_fs      = tbl;
345                 tgt->mnt_version = 0;       /* 0 means invalid */
346                 tgt->mnt_type    = type;
347
348                 ++tbl->mn_nr_targets;
349         }
350
351         tgt->mnt_version = ++tbl->mn_version;
352         tgt->mnt_mti     = *mti;
353
354         cfs_list_move_tail(&tgt->mnt_list, &tbl->mn_targets);
355
356         rc = nidtbl_update_version(env, fsdb->fsdb_mgs, tbl);
357         EXIT;
358
359 out:
360         cfs_mutex_unlock(&tbl->mn_lock);
361         if (rc)
362                 CERROR("Write NID table version for file system %s error %d\n",
363                        fsdb->fsdb_name, rc);
364         return rc;
365 }
366
367 static void mgs_nidtbl_fini_fs(struct fs_db *fsdb)
368 {
369         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
370         CFS_LIST_HEAD(head);
371
372         cfs_mutex_lock(&tbl->mn_lock);
373         tbl->mn_nr_targets = 0;
374         cfs_list_splice_init(&tbl->mn_targets, &head);
375         cfs_mutex_unlock(&tbl->mn_lock);
376
377         while (!cfs_list_empty(&head)) {
378                 struct mgs_nidtbl_target *tgt;
379                 tgt = list_entry(head.next, struct mgs_nidtbl_target, mnt_list);
380                 cfs_list_del(&tgt->mnt_list);
381                 OBD_FREE_PTR(tgt);
382         }
383 }
384
385 static int mgs_nidtbl_init_fs(const struct lu_env *env, struct fs_db *fsdb)
386 {
387         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
388
389         CFS_INIT_LIST_HEAD(&tbl->mn_targets);
390         cfs_mutex_init(&tbl->mn_lock);
391         tbl->mn_nr_targets = 0;
392         tbl->mn_fsdb = fsdb;
393         cfs_mutex_lock(&tbl->mn_lock);
394         tbl->mn_version = nidtbl_read_version(env, fsdb->fsdb_mgs, tbl);
395         cfs_mutex_unlock(&tbl->mn_lock);
396         CDEBUG(D_MGS, "IR: current version is %llu\n", tbl->mn_version);
397
398         return 0;
399 }
400
401 /* --------- Imperative Recovery relies on nidtbl stuff ------- */
402 void mgs_ir_notify_complete(struct fs_db *fsdb)
403 {
404         struct timeval tv;
405         cfs_duration_t delta;
406
407         cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
408
409         /* do statistic */
410         fsdb->fsdb_notify_count++;
411         delta = cfs_time_sub(cfs_time_current(), fsdb->fsdb_notify_start);
412         fsdb->fsdb_notify_total += delta;
413         if (delta > fsdb->fsdb_notify_max)
414                 fsdb->fsdb_notify_max = delta;
415
416         cfs_duration_usec(delta, &tv);
417         CDEBUG(D_MGS, "Revoke recover lock of %s completed after %ld.%06lds\n",
418                fsdb->fsdb_name, tv.tv_sec, tv.tv_usec);
419 }
420
421 static int mgs_ir_notify(void *arg)
422 {
423         struct fs_db      *fsdb   = arg;
424         struct ldlm_res_id resid;
425
426         char name[sizeof(fsdb->fsdb_name) + 20];
427
428         LASSERTF(sizeof(name) < 32, "name is too large to be in stack.\n");
429         sprintf(name, "mgs_%s_notify", fsdb->fsdb_name);
430         cfs_daemonize(name);
431
432         cfs_complete(&fsdb->fsdb_notify_comp);
433
434         set_user_nice(current, -2);
435
436         mgc_fsname2resid(fsdb->fsdb_name, &resid, CONFIG_T_RECOVER);
437         while (1) {
438                 struct l_wait_info   lwi = { 0 };
439
440                 l_wait_event(fsdb->fsdb_notify_waitq,
441                              fsdb->fsdb_notify_stop ||
442                              cfs_atomic_read(&fsdb->fsdb_notify_phase),
443                              &lwi);
444                 if (fsdb->fsdb_notify_stop)
445                         break;
446
447                 CDEBUG(D_MGS, "%s woken up, phase is %d\n",
448                        name, cfs_atomic_read(&fsdb->fsdb_notify_phase));
449
450                 fsdb->fsdb_notify_start = cfs_time_current();
451                 mgs_revoke_lock(fsdb->fsdb_mgs, fsdb, CONFIG_T_RECOVER);
452         }
453
454         cfs_complete(&fsdb->fsdb_notify_comp);
455         return 0;
456 }
457
458 int mgs_ir_init_fs(const struct lu_env *env, struct mgs_device *mgs,
459                    struct fs_db *fsdb)
460 {
461         int rc;
462
463         if (!ir_timeout)
464                 ir_timeout = OBD_IR_MGS_TIMEOUT;
465
466         fsdb->fsdb_ir_state = IR_FULL;
467         if (cfs_time_before(cfs_time_current_sec(),
468                             mgs->mgs_start_time + ir_timeout))
469                 fsdb->fsdb_ir_state = IR_STARTUP;
470         fsdb->fsdb_nonir_clients = 0;
471         CFS_INIT_LIST_HEAD(&fsdb->fsdb_clients);
472
473         /* start notify thread */
474         fsdb->fsdb_obd = mgs->mgs_obd;
475         fsdb->fsdb_mgs = mgs;
476         cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
477         cfs_waitq_init(&fsdb->fsdb_notify_waitq);
478         cfs_init_completion(&fsdb->fsdb_notify_comp);
479         rc = cfs_create_thread(mgs_ir_notify, fsdb, CFS_DAEMON_FLAGS);
480         if (rc > 0)
481                 cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
482         else
483                 CERROR("Start notify thread error %d\n", rc);
484
485         mgs_nidtbl_init_fs(env, fsdb);
486         return 0;
487 }
488
489 void mgs_ir_fini_fs(struct mgs_device *mgs, struct fs_db *fsdb)
490 {
491         if (cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
492                 return;
493
494         mgs_fsc_cleanup_by_fsdb(fsdb);
495
496         mgs_nidtbl_fini_fs(fsdb);
497
498         LASSERT(cfs_list_empty(&fsdb->fsdb_clients));
499
500         fsdb->fsdb_notify_stop = 1;
501         cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
502         cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
503 }
504
505 /* caller must have held fsdb_mutex */
506 static inline void ir_state_graduate(struct fs_db *fsdb)
507 {
508         if (fsdb->fsdb_ir_state == IR_STARTUP) {
509                 if (cfs_time_before(fsdb->fsdb_mgs->mgs_start_time + ir_timeout,
510                                     cfs_time_current_sec())) {
511                         fsdb->fsdb_ir_state = IR_FULL;
512                         if (fsdb->fsdb_nonir_clients)
513                                 fsdb->fsdb_ir_state = IR_PARTIAL;
514                 }
515         }
516 }
517
518 int mgs_ir_update(const struct lu_env *env, struct mgs_device *mgs,
519                   struct mgs_target_info *mti)
520 {
521         struct fs_db *fsdb;
522         bool notify = true;
523         int rc;
524
525         if (mti->mti_instance == 0)
526                 return -EINVAL;
527
528         rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
529         if (rc)
530                 return rc;
531
532         rc = mgs_nidtbl_write(env, fsdb, mti);
533         if (rc)
534                 return rc;
535
536         /* check ir state */
537         cfs_mutex_lock(&fsdb->fsdb_mutex);
538         ir_state_graduate(fsdb);
539         switch (fsdb->fsdb_ir_state) {
540         case IR_FULL:
541                 mti->mti_flags |= LDD_F_IR_CAPABLE;
542                 break;
543         case IR_DISABLED:
544                 notify = false;
545         case IR_STARTUP:
546         case IR_PARTIAL:
547                 break;
548         default:
549                 LBUG();
550         }
551         cfs_mutex_unlock(&fsdb->fsdb_mutex);
552
553         LASSERT(ergo(mti->mti_flags & LDD_F_IR_CAPABLE, notify));
554         if (notify) {
555                 CDEBUG(D_MGS, "Try to revoke recover lock of %s\n",
556                        fsdb->fsdb_name);
557                 cfs_atomic_inc(&fsdb->fsdb_notify_phase);
558                 cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
559         }
560         return 0;
561 }
562
563 /* NID table can be cached by two entities: Clients and MDTs */
564 enum {
565         IR_CLIENT  = 1,
566         IR_MDT     = 2
567 };
568
569 static int delogname(char *logname, char *fsname, int *typ)
570 {
571         char *ptr;
572         int   type;
573         int   len;
574
575         ptr = strrchr(logname, '-');
576         if (ptr == NULL)
577                 return -EINVAL;
578
579         /* decouple file system name. The llog name may be:
580          * - "prefix-fsname", prefix is "cliir" or "mdtir"
581          */
582         if (strncmp(ptr, "-mdtir", 6) == 0)
583                 type = IR_MDT;
584         else if (strncmp(ptr, "-cliir", 6) == 0)
585                 type = IR_CLIENT;
586         else
587                 return -EINVAL;
588
589         len = ptr - logname;
590         if (len == 0)
591                 return -EINVAL;
592
593         memcpy(fsname, logname, len);
594         fsname[len] = 0;
595         if (typ)
596                 *typ = type;
597         return 0;
598 }
599
600 int mgs_get_ir_logs(struct ptlrpc_request *req)
601 {
602         struct lu_env     *env = req->rq_svc_thread->t_env;
603         struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
604         struct fs_db      *fsdb;
605         struct mgs_config_body  *body;
606         struct mgs_config_res   *res;
607         struct ptlrpc_bulk_desc *desc;
608         struct l_wait_info lwi;
609         char               fsname[16];
610         long               bufsize;
611         int                unit_size;
612         int                type;
613         int                rc = 0;
614         int                i;
615         int                bytes;
616         int                page_count;
617         int                nrpages;
618         cfs_page_t       **pages = NULL;
619         ENTRY;
620
621         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
622         if (body == NULL)
623                 RETURN(-EINVAL);
624
625         if (body->mcb_type != CONFIG_T_RECOVER)
626                 RETURN(-EINVAL);
627
628         rc = delogname(body->mcb_name, fsname, &type);
629         if (rc)
630                 RETURN(rc);
631
632         rc = mgs_find_or_make_fsdb(env, mgs, fsname, &fsdb);
633         if (rc)
634                 RETURN(rc);
635
636         bufsize = body->mcb_units << body->mcb_bits;
637         nrpages = (bufsize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
638         if (nrpages > PTLRPC_MAX_BRW_PAGES)
639                 RETURN(-EINVAL);
640
641         CDEBUG(D_MGS, "Reading IR log %s bufsize %ld.\n",
642                body->mcb_name, bufsize);
643
644         OBD_ALLOC(pages, sizeof(*pages) * nrpages);
645         if (pages == NULL)
646                 RETURN(-ENOMEM);
647
648         rc = req_capsule_server_pack(&req->rq_pill);
649         if (rc)
650                 GOTO(out, rc);
651
652         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
653         if (res == NULL)
654                 GOTO(out, rc = -EINVAL);
655
656         res->mcr_offset = body->mcb_offset;
657         unit_size = min_t(int, 1 << body->mcb_bits, CFS_PAGE_SIZE);
658         bytes = mgs_nidtbl_read(req->rq_export, &fsdb->fsdb_nidtbl, res,
659                                 pages, nrpages, bufsize / unit_size, unit_size);
660         if (bytes < 0)
661                 GOTO(out, rc = bytes);
662
663         /* start bulk transfer */
664         page_count = (bytes + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
665         LASSERT(page_count <= nrpages);
666         desc = ptlrpc_prep_bulk_exp(req, page_count,
667                                     BULK_PUT_SOURCE, MGS_BULK_PORTAL);
668         if (desc == NULL)
669                 GOTO(out, rc = -ENOMEM);
670
671         for (i = 0; i < page_count && bytes > 0; i++) {
672                 ptlrpc_prep_bulk_page(desc, pages[i], 0,
673                                 min_t(int, bytes, CFS_PAGE_SIZE));
674                 bytes -= CFS_PAGE_SIZE;
675         }
676
677         rc = target_bulk_io(req->rq_export, desc, &lwi);
678         ptlrpc_free_bulk(desc);
679
680 out:
681         if (pages) {
682                 for (i = 0; i < nrpages; i++) {
683                         if (pages[i] == NULL)
684                                 break;
685                         cfs_free_page(pages[i]);
686                 }
687                 OBD_FREE(pages, sizeof(*pages) * nrpages);
688         }
689         return rc;
690 }
691
692 static int lprocfs_ir_set_state(struct fs_db *fsdb, const char *buf)
693 {
694         const char *strings[] = IR_STRINGS;
695         int         state = -1;
696         int         i;
697
698         for (i = 0; i < ARRAY_SIZE(strings); i++) {
699                 if (strcmp(strings[i], buf) == 0) {
700                         state = i;
701                         break;
702                 }
703         }
704         if (state < 0)
705                 return -EINVAL;
706
707         CDEBUG(D_MGS, "change fsr state of %s from %s to %s\n",
708                fsdb->fsdb_name, strings[fsdb->fsdb_ir_state], strings[state]);
709         cfs_mutex_lock(&fsdb->fsdb_mutex);
710         if (state == IR_FULL && fsdb->fsdb_nonir_clients)
711                 state = IR_PARTIAL;
712         fsdb->fsdb_ir_state = state;
713         cfs_mutex_unlock(&fsdb->fsdb_mutex);
714
715         return 0;
716 }
717
718 static int lprocfs_ir_set_timeout(struct fs_db *fsdb, const char *buf)
719 {
720         return -EINVAL;
721 }
722
723 static int lprocfs_ir_clear_stats(struct fs_db *fsdb, const char *buf)
724 {
725         if (*buf)
726                 return -EINVAL;
727
728         fsdb->fsdb_notify_total = 0;
729         fsdb->fsdb_notify_max   = 0;
730         fsdb->fsdb_notify_count = 0;
731         return 0;
732 }
733
734 static struct lproc_ir_cmd {
735         char *name;
736         int   namelen;
737         int (*handler)(struct fs_db *, const char *);
738 } ir_cmds[] = {
739         { "state=",   6, lprocfs_ir_set_state },
740         { "timeout=", 8, lprocfs_ir_set_timeout },
741         { "0",        1, lprocfs_ir_clear_stats }
742 };
743
744 int lprocfs_wr_ir_state(struct file *file, const char *buffer,
745                          unsigned long count, void *data)
746 {
747         struct fs_db *fsdb = data;
748         char *kbuf;
749         char *ptr;
750         int rc = 0;
751
752         if (count > CFS_PAGE_SIZE)
753                 return -EINVAL;
754
755         OBD_ALLOC(kbuf, count + 1);
756         if (kbuf == NULL)
757                 return -ENOMEM;
758
759         if (copy_from_user(kbuf, buffer, count)) {
760                 OBD_FREE(kbuf, count);
761                 return -EFAULT;
762         }
763
764         kbuf[count] = 0; /* buffer is supposed to end with 0 */
765         if (kbuf[count - 1] == '\n')
766                 kbuf[count - 1] = 0;
767         ptr = kbuf;
768
769         /* fsname=<file system name> must be the 1st entry */
770         while (ptr != NULL) {
771                 char *tmpptr;
772                 int i;
773
774                 tmpptr = strchr(ptr, ';');
775                 if (tmpptr)
776                         *tmpptr++ = 0;
777
778                 rc = -EINVAL;
779                 for (i = 0; i < ARRAY_SIZE(ir_cmds); i++) {
780                         struct lproc_ir_cmd *cmd;
781                         int cmdlen;
782
783                         cmd    = &ir_cmds[i];
784                         cmdlen = cmd->namelen;
785                         if (strncmp(cmd->name, ptr, cmdlen) == 0) {
786                                 ptr += cmdlen;
787                                 rc = cmd->handler(fsdb, ptr);
788                                 break;
789                         }
790                 }
791                 if (rc)
792                         break;
793
794                 ptr = tmpptr;
795         }
796         if (rc)
797                 CERROR("Unable to process command: %s(%d)\n", ptr, rc);
798         OBD_FREE(kbuf, count + 1);
799         return rc ?: count;
800 }
801
802 int lprocfs_rd_ir_state(struct seq_file *seq, void *data)
803 {
804         struct fs_db      *fsdb = data;
805         struct mgs_nidtbl *tbl  = &fsdb->fsdb_nidtbl;
806         const char        *ir_strings[] = IR_STRINGS;
807         struct timeval     tv_max;
808         struct timeval     tv;
809
810         /* mgs_live_seq_show() already holds fsdb_mutex. */
811         ir_state_graduate(fsdb);
812
813         seq_printf(seq, "\nimperative_recovery_state:\n");
814         seq_printf(seq,
815                    "    state: %s\n"
816                    "    nonir_clients: %d\n"
817                    "    nidtbl_version: %lld\n",
818                    ir_strings[fsdb->fsdb_ir_state], fsdb->fsdb_nonir_clients,
819                    tbl->mn_version);
820
821         cfs_duration_usec(fsdb->fsdb_notify_total, &tv);
822         cfs_duration_usec(fsdb->fsdb_notify_max, &tv_max);
823
824         seq_printf(seq, "    notify_duration_total: %lu.%06lu\n"
825                         "    notify_duation_max: %lu.%06lu\n"
826                         "    notify_count: %u\n",
827                    tv.tv_sec, tv.tv_usec,
828                    tv_max.tv_sec, tv_max.tv_usec,
829                    fsdb->fsdb_notify_count);
830
831         return 0;
832 }
833
834 int lprocfs_rd_ir_timeout(char *page, char **start, off_t off, int count,
835                           int *eof, void *data)
836 {
837         *eof = 1;
838         return snprintf(page, count, "%d\n", ir_timeout);
839 }
840
841 int lprocfs_wr_ir_timeout(struct file *file, const char *buffer,
842                           unsigned long count, void *data)
843 {
844         return lprocfs_wr_uint(file, buffer, count, &ir_timeout);
845 }
846
847 /* --------------- Handle non IR support clients --------------- */
848 /* attach a lustre file system to an export */
849 int mgs_fsc_attach(const struct lu_env *env, struct obd_export *exp,
850                    char *fsname)
851 {
852         struct mgs_export_data *data = &exp->u.eu_mgs_data;
853         struct mgs_device *mgs = exp2mgs_dev(exp);
854         struct fs_db      *fsdb;
855         struct mgs_fsc    *fsc     = NULL;
856         struct mgs_fsc    *new_fsc = NULL;
857         bool               found   = false;
858         int                rc;
859         ENTRY;
860
861         rc = mgs_find_or_make_fsdb(env, mgs, fsname, &fsdb);
862         if (rc)
863                 RETURN(rc);
864
865         /* allocate a new fsc in case we need it in spinlock. */
866         OBD_ALLOC_PTR(new_fsc);
867         if (new_fsc == NULL)
868                 RETURN(-ENOMEM);
869
870         CFS_INIT_LIST_HEAD(&new_fsc->mfc_export_list);
871         CFS_INIT_LIST_HEAD(&new_fsc->mfc_fsdb_list);
872         new_fsc->mfc_fsdb       = fsdb;
873         new_fsc->mfc_export     = class_export_get(exp);
874         new_fsc->mfc_ir_capable =
875                         !!(exp->exp_connect_flags & OBD_CONNECT_IMP_RECOV);
876
877         rc = -EEXIST;
878         cfs_mutex_lock(&fsdb->fsdb_mutex);
879
880         /* tend to find it in export list because this list is shorter. */
881         cfs_spin_lock(&data->med_lock);
882         cfs_list_for_each_entry(fsc, &data->med_clients, mfc_export_list) {
883                 if (strcmp(fsname, fsc->mfc_fsdb->fsdb_name) == 0) {
884                         found = true;
885                         break;
886                 }
887         }
888         if (!found) {
889                 fsc = new_fsc;
890                 new_fsc = NULL;
891
892                 /* add it into export list. */
893                 cfs_list_add(&fsc->mfc_export_list, &data->med_clients);
894
895                 /* add into fsdb list. */
896                 cfs_list_add(&fsc->mfc_fsdb_list, &fsdb->fsdb_clients);
897                 if (!fsc->mfc_ir_capable) {
898                         ++fsdb->fsdb_nonir_clients;
899                         if (fsdb->fsdb_ir_state == IR_FULL)
900                                 fsdb->fsdb_ir_state = IR_PARTIAL;
901                 }
902                 rc = 0;
903         }
904         cfs_spin_unlock(&data->med_lock);
905         cfs_mutex_unlock(&fsdb->fsdb_mutex);
906
907         if (new_fsc) {
908                 class_export_put(new_fsc->mfc_export);
909                 OBD_FREE_PTR(new_fsc);
910         }
911         RETURN(rc);
912 }
913
914 void mgs_fsc_cleanup(struct obd_export *exp)
915 {
916         struct mgs_export_data *data = &exp->u.eu_mgs_data;
917         struct mgs_fsc *fsc, *tmp;
918         CFS_LIST_HEAD(head);
919
920         cfs_spin_lock(&data->med_lock);
921         cfs_list_splice_init(&data->med_clients, &head);
922         cfs_spin_unlock(&data->med_lock);
923
924         cfs_list_for_each_entry_safe(fsc, tmp, &head, mfc_export_list) {
925                 struct fs_db *fsdb = fsc->mfc_fsdb;
926
927                 LASSERT(fsc->mfc_export == exp);
928
929                 cfs_mutex_lock(&fsdb->fsdb_mutex);
930                 cfs_list_del_init(&fsc->mfc_fsdb_list);
931                 if (fsc->mfc_ir_capable == 0) {
932                         --fsdb->fsdb_nonir_clients;
933                         LASSERT(fsdb->fsdb_ir_state != IR_FULL);
934                         if (fsdb->fsdb_nonir_clients == 0 &&
935                             fsdb->fsdb_ir_state == IR_PARTIAL)
936                                 fsdb->fsdb_ir_state = IR_FULL;
937                 }
938                 cfs_mutex_unlock(&fsdb->fsdb_mutex);
939                 cfs_list_del_init(&fsc->mfc_export_list);
940                 class_export_put(fsc->mfc_export);
941                 OBD_FREE_PTR(fsc);
942         }
943 }
944
945 /* must be called with fsdb->fsdb_mutex held */
946 void mgs_fsc_cleanup_by_fsdb(struct fs_db *fsdb)
947 {
948         struct mgs_fsc *fsc, *tmp;
949
950         cfs_list_for_each_entry_safe(fsc, tmp, &fsdb->fsdb_clients,
951                                      mfc_fsdb_list) {
952                 struct mgs_export_data *data = &fsc->mfc_export->u.eu_mgs_data;
953
954                 LASSERT(fsdb == fsc->mfc_fsdb);
955                 cfs_list_del_init(&fsc->mfc_fsdb_list);
956
957                 cfs_spin_lock(&data->med_lock);
958                 cfs_list_del_init(&fsc->mfc_export_list);
959                 cfs_spin_unlock(&data->med_lock);
960                 class_export_put(fsc->mfc_export);
961                 OBD_FREE_PTR(fsc);
962         }
963
964         fsdb->fsdb_nonir_clients = 0;
965         if (fsdb->fsdb_ir_state == IR_PARTIAL)
966                 fsdb->fsdb_ir_state = IR_FULL;
967 }