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