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