Whamcloud - gitweb
ORNL-10: Basic IR implementation
[fs/lustre-release.git] / lustre / mgs / mgs_nids.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
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 #ifndef EXPORT_SYMTAB
44 #define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MGS
47 #define D_MGS D_CONFIG
48
49 #ifdef __KERNEL__
50 #include <linux/module.h>
51 #include <linux/pagemap.h>
52 #include <linux/fs.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_lov.h>
57 #include <obd_class.h>
58 #include <lustre_log.h>
59 #include <obd_ost.h>
60 #include <libcfs/list.h>
61 #include <linux/lvfs.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_disk.h>
64 #include <lustre_param.h>
65 #include "mgs_internal.h"
66
67 static unsigned int ir_timeout;
68
69 static int nidtbl_is_sane(struct mgs_nidtbl *tbl)
70 {
71         struct mgs_nidtbl_target *tgt;
72         int version = 0;
73
74         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
75         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
76                 if (!tgt->mnt_version)
77                         continue;
78
79                 if (version >= tgt->mnt_version)
80                         return 0;
81
82                 version = tgt->mnt_version;
83         }
84         return 1;
85 }
86
87 /**
88  * Fetch nidtbl entries whose version are not less than @version
89  * nidtbl entries will be packed in @pages by @unit_size units - entries
90  * shouldn't cross unit boundaries.
91  */
92 static int mgs_nidtbl_read(struct obd_device *unused, struct mgs_nidtbl *tbl,
93                            struct mgs_config_res *res, cfs_page_t **pages,
94                            int nrpages, int units_total, int unit_size)
95 {
96         struct mgs_nidtbl_target *tgt;
97         struct mgs_nidtbl_entry  *entry;
98         struct mgs_nidtbl_entry  *last_in_unit = NULL;
99         struct mgs_target_info   *mti;
100         __u64 version = res->mcr_offset;
101         bool nobuf = false;
102         void *buf = NULL;
103         int bytes_in_unit = 0;
104         int units_in_page = 0;
105         int index = 0;
106         int rc = 0;
107         ENTRY;
108
109         /* make sure unit_size is power 2 */
110         LASSERT((unit_size & (unit_size - 1)) == 0);
111         LASSERT(nrpages << CFS_PAGE_SHIFT >= units_total * unit_size);
112
113         cfs_mutex_lock(&tbl->mn_lock);
114         LASSERT(nidtbl_is_sane(tbl));
115
116         /* no more entries ? */
117         if (version > tbl->mn_version) {
118                 version = tbl->mn_version;
119                 goto out;
120         }
121
122         /* iterate over all targets to compose a bitmap by the type of llog.
123          * If the llog is for MDTs, llog entries for OSTs will be returned;
124          * otherwise, it's for clients, then llog entries for both OSTs and
125          * MDTs will be returned.
126          */
127         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
128                 int entry_len = sizeof(*entry);
129
130                 if (tgt->mnt_version < version)
131                         continue;
132
133                 /* write target recover information */
134                 mti  = &tgt->mnt_mti;
135                 LASSERT(mti->mti_nid_count < MTI_NIDS_MAX);
136                 entry_len += mti->mti_nid_count * sizeof(lnet_nid_t);
137
138                 if (entry_len > unit_size) {
139                         CWARN("nidtbl: too large entry: entry length %d,"
140                               "unit size: %d\n", entry_len, unit_size);
141                         GOTO(out, rc = -EOVERFLOW);
142                 }
143
144                 if (bytes_in_unit < entry_len) {
145                         if (units_total == 0) {
146                                 nobuf = true;
147                                 break;
148                         }
149
150                         /* check if we need to consume remaining bytes. */
151                         if (last_in_unit != NULL && bytes_in_unit) {
152                                 /* entry has been swapped. */
153                                 __swab32s(&last_in_unit->mne_length);
154                                 last_in_unit->mne_length += bytes_in_unit;
155                                 __swab32s(&last_in_unit->mne_length);
156                                 rc  += bytes_in_unit;
157                                 buf += bytes_in_unit;
158                                 last_in_unit = NULL;
159                         }
160                         LASSERT((rc & (unit_size - 1)) == 0);
161
162                         if (units_in_page == 0) {
163                                 /* allocate a new page */
164                                 pages[index] = cfs_alloc_page(CFS_ALLOC_STD);
165                                 if (pages[index] == NULL) {
166                                         rc = -ENOMEM;
167                                         break;
168                                 }
169
170                                 /* destroy previous map */
171                                 if (index > 0)
172                                         cfs_kunmap(pages[index - 1]);
173
174                                 /* reassign buffer */
175                                 buf = cfs_kmap(pages[index]);
176                                 ++index;
177
178                                 units_in_page = CFS_PAGE_SIZE / unit_size;
179                                 LASSERT(units_in_page > 0);
180                         }
181
182                         /* allocate an unit */
183                         LASSERT(((long)buf & (unit_size - 1)) == 0);
184                         bytes_in_unit = unit_size;
185                         --units_in_page;
186                         --units_total;
187                 }
188
189                 /* fill in entry. */
190                 entry = (struct mgs_nidtbl_entry *)buf;
191                 entry->mne_version   = tgt->mnt_version;
192                 entry->mne_instance  = mti->mti_instance;
193                 entry->mne_index     = mti->mti_stripe_index;
194                 entry->mne_length    = entry_len;
195                 entry->mne_type      = tgt->mnt_type;
196                 entry->mne_nid_type  = 0;
197                 entry->mne_nid_size  = sizeof(lnet_nid_t);
198                 entry->mne_nid_count = mti->mti_nid_count;
199                 memcpy(entry->u.nids, mti->mti_nids,
200                        mti->mti_nid_count * sizeof(lnet_nid_t));
201                 lustre_swab_mgs_nidtbl_entry(entry);
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                 cfs_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         cfs_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(struct obd_device *obd, struct mgs_nidtbl *tbl)
229 {
230         struct lvfs_run_ctxt saved;
231         struct file         *file = NULL;
232         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
233         u64                  version;
234         loff_t               off = 0;
235         int                  rc;
236         ENTRY;
237
238         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
239         LASSERT(sizeof(filename) < 32);
240
241         sprintf(filename, "%s/%s",
242                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
243
244         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
245
246         file = l_filp_open(filename, O_RDWR|O_CREAT, 0660);
247         if (!IS_ERR(file)) {
248                 version = cpu_to_le64(tbl->mn_version);
249                 rc = lustre_fwrite(file, &version, sizeof(version), &off);
250                 if (rc == sizeof(version))
251                         rc = 0;
252                 filp_close(file, 0);
253                 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
254         } else {
255                 rc = PTR_ERR(file);
256         }
257
258         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
259         RETURN(rc);
260 }
261
262 #define MGS_NIDTBL_VERSION_INIT 2
263
264 static int nidtbl_read_version(struct obd_device *obd, struct mgs_nidtbl *tbl)
265 {
266         struct lvfs_run_ctxt saved;
267         struct file         *file = NULL;
268         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
269         u64                  version;
270         loff_t               off = 0;
271         int                  rc;
272         ENTRY;
273
274         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
275         LASSERT(sizeof(filename) < 32);
276
277         sprintf(filename, "%s/%s",
278                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
279
280         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
281
282         file = l_filp_open(filename, O_RDONLY, 0);
283         if (!IS_ERR(file)) {
284                 rc = lustre_fread(file, &version, sizeof(version), &off);
285                 if (rc == sizeof(version))
286                         rc = cpu_to_le64(version);
287                 else if (rc == 0)
288                         rc = MGS_NIDTBL_VERSION_INIT;
289                 else
290                         CERROR("read version file %s error %d\n", filename, rc);
291                 filp_close(file, 0);
292         } else {
293                 rc = PTR_ERR(file);
294                 if (rc == -ENOENT)
295                         rc = MGS_NIDTBL_VERSION_INIT;
296         }
297
298         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
299         RETURN(rc);
300 }
301
302 static int mgs_nidtbl_write(struct fs_db *fsdb, struct mgs_target_info *mti)
303 {
304         struct mgs_nidtbl        *tbl;
305         struct mgs_nidtbl_target *tgt;
306         bool found = false;
307         int type   = mti->mti_flags & LDD_F_SV_TYPE_MASK;
308         int rc     = 0;
309         ENTRY;
310
311         type &= ~LDD_F_SV_TYPE_MGS;
312         LASSERT(type != 0);
313
314         tbl = &fsdb->fsdb_nidtbl;
315         cfs_mutex_lock(&tbl->mn_lock);
316         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
317                 struct mgs_target_info *info = &tgt->mnt_mti;
318                 if (type == tgt->mnt_type &&
319                     mti->mti_stripe_index == info->mti_stripe_index) {
320                         found = true;
321                         break;
322                 }
323         }
324         if (!found) {
325                 OBD_ALLOC_PTR(tgt);
326                 if (tgt == NULL)
327                         GOTO(out, rc = -ENOMEM);
328
329                 CFS_INIT_LIST_HEAD(&tgt->mnt_list);
330                 tgt->mnt_fs      = tbl;
331                 tgt->mnt_version = 0;       /* 0 means invalid */
332                 tgt->mnt_type    = type;
333
334                 ++tbl->mn_nr_targets;
335         }
336
337         tgt->mnt_version = ++tbl->mn_version;
338         tgt->mnt_mti     = *mti;
339
340         cfs_list_move_tail(&tgt->mnt_list, &tbl->mn_targets);
341
342         rc = nidtbl_update_version(fsdb->fsdb_obd, tbl);
343         EXIT;
344
345 out:
346         cfs_mutex_unlock(&tbl->mn_lock);
347         if (rc)
348                 CERROR("Write NID table version for file system %s error %d\n",
349                        fsdb->fsdb_name, rc);
350         return rc;
351 }
352
353 static void mgs_nidtbl_fini_fs(struct fs_db *fsdb)
354 {
355         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
356         CFS_LIST_HEAD(head);
357
358         cfs_mutex_lock(&tbl->mn_lock);
359         tbl->mn_nr_targets = 0;
360         cfs_list_splice_init(&tbl->mn_targets, &head);
361         cfs_mutex_unlock(&tbl->mn_lock);
362
363         while (!cfs_list_empty(&head)) {
364                 struct mgs_nidtbl_target *tgt;
365                 tgt = list_entry(head.next, struct mgs_nidtbl_target, mnt_list);
366                 cfs_list_del(&tgt->mnt_list);
367                 OBD_FREE_PTR(tgt);
368         }
369 }
370
371 static int mgs_nidtbl_init_fs(struct fs_db *fsdb)
372 {
373         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
374
375         CFS_INIT_LIST_HEAD(&tbl->mn_targets);
376         cfs_mutex_init(&tbl->mn_lock);
377         tbl->mn_nr_targets = 0;
378         tbl->mn_fsdb = fsdb;
379         cfs_mutex_lock(&tbl->mn_lock);
380         tbl->mn_version = nidtbl_read_version(fsdb->fsdb_obd, tbl);
381         cfs_mutex_unlock(&tbl->mn_lock);
382         CDEBUG(D_MGS, "IR: current version is %llu\n", tbl->mn_version);
383
384         return 0;
385 }
386
387 /* --------- Imperative Recovery relies on nidtbl stuff ------- */
388 static int mgs_ir_notify(void *arg)
389 {
390         struct fs_db      *fsdb   = arg;
391         struct ldlm_res_id resid;
392
393         char name[sizeof(fsdb->fsdb_name) + 20];
394
395         LASSERTF(sizeof(name) < 32, "name is too large to be in stack.\n");
396         sprintf(name, "mgs_%s_notify", fsdb->fsdb_name);
397         cfs_daemonize(name);
398
399         cfs_complete(&fsdb->fsdb_notify_comp);
400
401         mgc_fsname2resid(fsdb->fsdb_name, &resid, CONFIG_T_RECOVER);
402         while (1) {
403                 struct l_wait_info   lwi = { 0 };
404                 struct lustre_handle lockh;
405                 cfs_time_t           curtime;
406                 int                  lockrc;
407                 int                  delta;
408
409                 l_wait_event(fsdb->fsdb_notify_waitq,
410                              fsdb->fsdb_notify_stop ||
411                              cfs_atomic_read(&fsdb->fsdb_notify_phase),
412                              &lwi);
413                 if (fsdb->fsdb_notify_stop)
414                         break;
415
416                 CDEBUG(D_MGS, "%s woken up, phase is %d\n",
417                        name, cfs_atomic_read(&fsdb->fsdb_notify_phase));
418
419                 curtime = cfs_time_current();
420                 lockrc = mgs_get_lock(fsdb->fsdb_obd, &resid, &lockh);
421                 if (lockrc == ELDLM_OK) {
422                         cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
423                         mgs_put_lock(&lockh);
424
425                         /* do statistic */
426                         fsdb->fsdb_notify_count++;
427                         delta = (cfs_time_current() - curtime) / NSEC_PER_USEC;
428                         fsdb->fsdb_notify_total += delta;
429                         if (delta > fsdb->fsdb_notify_max)
430                                 fsdb->fsdb_notify_max = delta;
431                         CDEBUG(D_MGS, "Revoke recover lock of %s spent %dus\n",
432                                fsdb->fsdb_name, delta);
433                 } else {
434                         CERROR("Fatal error %d for fs %s\n",
435                                lockrc, fsdb->fsdb_name);
436                 }
437         }
438
439         cfs_complete(&fsdb->fsdb_notify_comp);
440         return 0;
441 }
442
443 int mgs_ir_init_fs(struct obd_device *obd, struct fs_db *fsdb)
444 {
445         struct mgs_obd *mgs = &obd->u.mgs;
446         int rc;
447
448         if (!ir_timeout)
449                 ir_timeout = OBD_IR_MGS_TIMEOUT;
450
451         fsdb->fsdb_ir_state = IR_FULL;
452         if (cfs_time_before(cfs_time_current_sec(),
453                             mgs->mgs_start_time + ir_timeout))
454                 fsdb->fsdb_ir_state = IR_STARTUP;
455
456         /* start notify thread */
457         fsdb->fsdb_obd = obd;
458         cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
459         cfs_waitq_init(&fsdb->fsdb_notify_waitq);
460         cfs_init_completion(&fsdb->fsdb_notify_comp);
461         rc = cfs_create_thread(mgs_ir_notify, fsdb, CFS_DAEMON_FLAGS);
462         if (rc > 0)
463                 cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
464         else
465                 CERROR("Start notify thread error %d\n", rc);
466
467         mgs_nidtbl_init_fs(fsdb);
468         return 0;
469 }
470
471 void mgs_ir_fini_fs(struct obd_device *obd, struct fs_db *fsdb)
472 {
473         if (cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
474                 return;
475
476         mgs_nidtbl_fini_fs(fsdb);
477
478         fsdb->fsdb_notify_stop = 1;
479         cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
480         cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
481 }
482
483 /* caller must have held fsdb_sem */
484 static inline void ir_state_graduate(struct fs_db *fsdb)
485 {
486         struct mgs_obd *mgs = &fsdb->fsdb_obd->u.mgs;
487
488         if (fsdb->fsdb_ir_state == IR_STARTUP) {
489                 if (cfs_time_before(mgs->mgs_start_time + ir_timeout,
490                                     cfs_time_current_sec())) {
491                         fsdb->fsdb_ir_state = IR_FULL;
492                 }
493         }
494 }
495
496 int mgs_ir_update(struct obd_device *obd, struct mgs_target_info *mti)
497 {
498         struct fs_db *fsdb;
499         bool notify = true;
500         int rc;
501
502         if (mti->mti_instance == 0)
503                 return -EINVAL;
504
505         rc = mgs_find_or_make_fsdb(obd, mti->mti_fsname, &fsdb);
506         if (rc)
507                 return rc;
508
509         rc = mgs_nidtbl_write(fsdb, mti);
510         if (rc)
511                 return rc;
512
513         /* check ir state */
514         cfs_down(&fsdb->fsdb_sem);
515         ir_state_graduate(fsdb);
516         switch (fsdb->fsdb_ir_state) {
517         case IR_FULL:
518                 mti->mti_flags |= LDD_F_IR_CAPABLE;
519                 break;
520         case IR_DISABLED:
521                 notify = false;
522         case IR_STARTUP:
523         case IR_PARTIAL:
524                 break;
525         default:
526                 LBUG();
527         }
528         cfs_up(&fsdb->fsdb_sem);
529
530         LASSERT(ergo(mti->mti_flags & LDD_F_IR_CAPABLE, notify));
531         if (notify) {
532                 CDEBUG(D_MGS, "Try to revoke recover lock of %s\n",
533                        fsdb->fsdb_name);
534                 cfs_atomic_inc(&fsdb->fsdb_notify_phase);
535                 cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
536         }
537         return 0;
538 }
539
540 /* NID table can be cached by two entities: Clients and MDTs */
541 enum {
542         IR_CLIENT  = 1,
543         IR_MDT     = 2
544 };
545
546 static int delogname(char *logname, char *fsname, int *typ)
547 {
548         char *ptr;
549         int   type;
550         int   len;
551
552         ptr = strrchr(logname, '-');
553         if (ptr == NULL)
554                 return -EINVAL;
555
556         /* decouple file system name. The llog name may be:
557          * - "prefix-fsname", prefix is "cliir" or "mdtir"
558          */
559         if (strncmp(ptr, "-mdtir", 6) == 0)
560                 type = IR_MDT;
561         else if (strncmp(ptr, "-cliir", 6) == 0)
562                 type = IR_CLIENT;
563         else
564                 return -EINVAL;
565
566         len = ptr - logname;
567         if (len == 0)
568                 return -EINVAL;
569
570         memcpy(fsname, logname, len);
571         fsname[len] = 0;
572         if (typ)
573                 *typ = type;
574         return 0;
575 }
576
577 int mgs_get_ir_logs(struct ptlrpc_request *req)
578 {
579         struct obd_device *obd = req->rq_export->exp_obd;
580         struct fs_db      *fsdb;
581         struct mgs_config_body  *body;
582         struct mgs_config_res   *res;
583         struct ptlrpc_bulk_desc *desc;
584         struct l_wait_info lwi;
585         char               fsname[16];
586         long               bufsize;
587         int                unit_size;
588
589         int                type;
590         int                rc = 0;
591         int                i;
592         int                bytes;
593         int                page_count;
594         int                nrpages;
595         cfs_page_t       **pages = NULL;
596         ENTRY;
597
598         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
599         if (body == NULL)
600                 RETURN(-EINVAL);
601
602         if (body->mcb_type != CONFIG_T_RECOVER)
603                 RETURN(-EINVAL);
604
605         rc = delogname(body->mcb_name, fsname, &type);
606         if (rc)
607                 RETURN(rc);
608
609         rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
610         if (rc)
611                 GOTO(out, rc);
612
613         bufsize = body->mcb_units << body->mcb_bits;
614         nrpages = (bufsize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
615         if (nrpages > PTLRPC_MAX_BRW_PAGES)
616                 RETURN(-EINVAL);
617
618         CDEBUG(D_MGS, "Reading IR log %s bufsize %ld.\n",
619                body->mcb_name, bufsize);
620
621         OBD_ALLOC(pages, sizeof(*pages) * nrpages);
622         if (pages == NULL)
623                 RETURN(-ENOMEM);
624
625         rc = req_capsule_server_pack(&req->rq_pill);
626         if (rc)
627                 GOTO(out, rc);
628
629         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
630         if (res == NULL)
631                 GOTO(out, rc = -EINVAL);
632
633         res->mcr_offset = body->mcb_offset;
634         unit_size = min_t(int, 1 << body->mcb_bits, CFS_PAGE_SIZE);
635         bytes = mgs_nidtbl_read(obd, &fsdb->fsdb_nidtbl, res, pages, nrpages,
636                                 bufsize / unit_size, unit_size);
637         if (bytes < 0)
638                 GOTO(out, rc = bytes);
639
640         /* start bulk transfer */
641         page_count = (bytes + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
642         LASSERT(page_count <= nrpages);
643         desc = ptlrpc_prep_bulk_exp(req, page_count,
644                                     BULK_PUT_SOURCE, MGS_BULK_PORTAL);
645         if (desc == NULL)
646                 GOTO(out, rc = -ENOMEM);
647
648         for (i = 0; i < page_count && bytes > 0; i++) {
649                 ptlrpc_prep_bulk_page(desc, pages[i], 0,
650                                 min_t(int, bytes, CFS_PAGE_SIZE));
651                 bytes -= CFS_PAGE_SIZE;
652         }
653
654         rc = target_bulk_io(req->rq_export, desc, &lwi);
655         ptlrpc_free_bulk(desc);
656
657 out:
658         if (pages) {
659                 for (i = 0; i < nrpages; i++) {
660                         if (pages[i] == NULL)
661                                 break;
662                         cfs_free_page(pages[i]);
663                 }
664                 OBD_FREE(pages, sizeof(*pages) * nrpages);
665         }
666         return rc;
667 }
668
669 static int lprocfs_ir_set_state(struct fs_db *fsdb, const char *buf)
670 {
671         const char *strings[] = IR_STRINGS;
672         int         state = -1;
673         int         i;
674
675         for (i = 0; i < ARRAY_SIZE(strings); i++) {
676                 if (strcmp(strings[i], buf) == 0) {
677                         state = i;
678                         break;
679                 }
680         }
681         if (state < 0)
682                 return -EINVAL;
683
684         CDEBUG(D_MGS, "change fsr state of %s from %s to %s\n",
685                fsdb->fsdb_name, strings[fsdb->fsdb_ir_state], strings[state]);
686         cfs_down(&fsdb->fsdb_sem);
687         fsdb->fsdb_ir_state = state;
688         cfs_up(&fsdb->fsdb_sem);
689
690         return 0;
691 }
692
693 static int lprocfs_ir_set_timeout(struct fs_db *fsdb, const char *buf)
694 {
695         return -EINVAL;
696 }
697
698 static int lprocfs_ir_clear_stats(struct fs_db *fsdb, const char *buf)
699 {
700         if (*buf)
701                 return -EINVAL;
702
703         fsdb->fsdb_notify_total = 0;
704         fsdb->fsdb_notify_max   = 0;
705         fsdb->fsdb_notify_count = 0;
706         return 0;
707 }
708
709 static struct lproc_ir_cmd {
710         char *name;
711         int   namelen;
712         int (*handler)(struct fs_db *, const char *);
713 } ir_cmds[] = {
714         { "state=",   6, lprocfs_ir_set_state },
715         { "timeout=", 8, lprocfs_ir_set_timeout },
716         { "0",        1, lprocfs_ir_clear_stats }
717 };
718
719 int lprocfs_wr_ir_state(struct file *file, const char *buffer,
720                          unsigned long count, void *data)
721 {
722         struct fs_db *fsdb = data;
723         char *kbuf;
724         char *ptr;
725         int rc = 0;
726
727         if (count > CFS_PAGE_SIZE)
728                 return -EINVAL;
729
730         OBD_ALLOC(kbuf, count + 1);
731         if (kbuf == NULL)
732                 return -ENOMEM;
733
734         if (copy_from_user(kbuf, buffer, count)) {
735                 OBD_FREE(kbuf, count);
736                 return -EFAULT;
737         }
738
739         kbuf[count] = 0; /* buffer is supposed to end with 0 */
740         if (kbuf[count - 1] == '\n')
741                 kbuf[count - 1] = 0;
742         ptr = kbuf;
743
744         /* fsname=<file system name> must be the 1st entry */
745         while (ptr != NULL) {
746                 char *tmpptr;
747                 int i;
748
749                 tmpptr = strchr(ptr, ';');
750                 if (tmpptr)
751                         *tmpptr++ = 0;
752
753                 rc = -EINVAL;
754                 for (i = 0; i < ARRAY_SIZE(ir_cmds); i++) {
755                         struct lproc_ir_cmd *cmd;
756                         int cmdlen;
757
758                         cmd    = &ir_cmds[i];
759                         cmdlen = cmd->namelen;
760                         if (strncmp(cmd->name, ptr, cmdlen) == 0) {
761                                 ptr += cmdlen;
762                                 rc = cmd->handler(fsdb, ptr);
763                                 break;
764                         }
765                 }
766                 if (rc)
767                         break;
768
769                 ptr = tmpptr;
770         }
771         if (rc)
772                 CERROR("Unable to process command: %s(%d)\n", ptr, rc);
773         OBD_FREE(kbuf, count + 1);
774         return rc ?: count;
775 }
776
777 int lprocfs_rd_ir_state(struct seq_file *seq, void *data)
778 {
779         struct fs_db      *fsdb = data;
780         struct mgs_nidtbl *tbl  = &fsdb->fsdb_nidtbl;
781         const char        *ir_strings[] = IR_STRINGS;
782
783         /* mgs_live_seq_show() already holds fsdb_sem. */
784         ir_state_graduate(fsdb);
785
786         seq_printf(seq,
787                    "\tstate: %s, nidtbl version: %lld\n",
788                    ir_strings[fsdb->fsdb_ir_state], tbl->mn_version);
789         seq_printf(seq, "\tnotify total/max/count: %u/%u/%u\n",
790                    fsdb->fsdb_notify_total, fsdb->fsdb_notify_max,
791                    fsdb->fsdb_notify_count);
792         return 0;
793 }
794
795 int lprocfs_rd_ir_timeout(char *page, char **start, off_t off, int count,
796                           int *eof, void *data)
797 {
798         *eof = 1;
799         return snprintf(page, count, "%d\n", ir_timeout);
800 }
801
802 int lprocfs_wr_ir_timeout(struct file *file, const char *buffer,
803                           unsigned long count, void *data)
804 {
805         return lprocfs_wr_uint(file, buffer, count, &ir_timeout);
806 }
807