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