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