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