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