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