Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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                                 /* entry has been swapped. */
150                                 __swab32s(&last_in_unit->mne_length);
151                                 last_in_unit->mne_length += bytes_in_unit;
152                                 __swab32s(&last_in_unit->mne_length);
153                                 rc  += bytes_in_unit;
154                                 buf += bytes_in_unit;
155                                 last_in_unit = NULL;
156                         }
157                         LASSERT((rc & (unit_size - 1)) == 0);
158
159                         if (units_in_page == 0) {
160                                 /* allocate a new page */
161                                 pages[index] = cfs_alloc_page(CFS_ALLOC_STD);
162                                 if (pages[index] == NULL) {
163                                         rc = -ENOMEM;
164                                         break;
165                                 }
166
167                                 /* destroy previous map */
168                                 if (index > 0)
169                                         cfs_kunmap(pages[index - 1]);
170
171                                 /* reassign buffer */
172                                 buf = cfs_kmap(pages[index]);
173                                 ++index;
174
175                                 units_in_page = CFS_PAGE_SIZE / unit_size;
176                                 LASSERT(units_in_page > 0);
177                         }
178
179                         /* allocate an unit */
180                         LASSERT(((long)buf & (unit_size - 1)) == 0);
181                         bytes_in_unit = unit_size;
182                         --units_in_page;
183                         --units_total;
184                 }
185
186                 /* fill in entry. */
187                 entry = (struct mgs_nidtbl_entry *)buf;
188                 entry->mne_version   = tgt->mnt_version;
189                 entry->mne_instance  = mti->mti_instance;
190                 entry->mne_index     = mti->mti_stripe_index;
191                 entry->mne_length    = entry_len;
192                 entry->mne_type      = tgt->mnt_type;
193                 entry->mne_nid_type  = 0;
194                 entry->mne_nid_size  = sizeof(lnet_nid_t);
195                 entry->mne_nid_count = mti->mti_nid_count;
196                 memcpy(entry->u.nids, mti->mti_nids,
197                        mti->mti_nid_count * sizeof(lnet_nid_t));
198                 lustre_swab_mgs_nidtbl_entry(entry);
199
200                 version = tgt->mnt_version;
201                 rc     += entry_len;
202                 buf    += entry_len;
203
204                 bytes_in_unit -= entry_len;
205                 last_in_unit   = entry;
206
207                 CDEBUG(D_MGS, "fsname %s, entry size %d, pages %d/%d/%d/%d.\n",
208                        tbl->mn_fsdb->fsdb_name, entry_len,
209                        bytes_in_unit, index, nrpages, units_total);
210         }
211         if (index > 0)
212                 cfs_kunmap(pages[index - 1]);
213 out:
214         LASSERT(version <= tbl->mn_version);
215         res->mcr_size = tbl->mn_version;
216         res->mcr_offset = nobuf ? version : tbl->mn_version;
217         cfs_mutex_unlock(&tbl->mn_lock);
218         LASSERT(ergo(version == 1, rc == 0)); /* get the log first time */
219
220         CDEBUG(D_MGS, "Read IR logs %s return with %d, version %llu\n",
221                tbl->mn_fsdb->fsdb_name, rc, version);
222         RETURN(rc);
223 }
224
225 static int nidtbl_update_version(struct obd_device *obd, struct mgs_nidtbl *tbl)
226 {
227         struct lvfs_run_ctxt saved;
228         struct file         *file = NULL;
229         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
230         u64                  version;
231         loff_t               off = 0;
232         int                  rc;
233         ENTRY;
234
235         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
236         LASSERT(sizeof(filename) < 32);
237
238         sprintf(filename, "%s/%s",
239                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
240
241         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
242
243         file = l_filp_open(filename, O_RDWR|O_CREAT, 0660);
244         if (!IS_ERR(file)) {
245                 version = cpu_to_le64(tbl->mn_version);
246                 rc = lustre_fwrite(file, &version, sizeof(version), &off);
247                 if (rc == sizeof(version))
248                         rc = 0;
249                 filp_close(file, 0);
250                 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
251         } else {
252                 rc = PTR_ERR(file);
253         }
254
255         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
256         RETURN(rc);
257 }
258
259 #define MGS_NIDTBL_VERSION_INIT 2
260
261 static int nidtbl_read_version(struct obd_device *obd, struct mgs_nidtbl *tbl)
262 {
263         struct lvfs_run_ctxt saved;
264         struct file         *file = NULL;
265         char                 filename[sizeof(MGS_NIDTBL_DIR) + 9];
266         u64                  version;
267         loff_t               off = 0;
268         int                  rc;
269         ENTRY;
270
271         LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
272         LASSERT(sizeof(filename) < 32);
273
274         sprintf(filename, "%s/%s",
275                 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
276
277         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
278
279         file = l_filp_open(filename, O_RDONLY, 0);
280         if (!IS_ERR(file)) {
281                 rc = lustre_fread(file, &version, sizeof(version), &off);
282                 if (rc == sizeof(version))
283                         rc = cpu_to_le64(version);
284                 else if (rc == 0)
285                         rc = MGS_NIDTBL_VERSION_INIT;
286                 else
287                         CERROR("read version file %s error %d\n", filename, rc);
288                 filp_close(file, 0);
289         } else {
290                 rc = PTR_ERR(file);
291                 if (rc == -ENOENT)
292                         rc = MGS_NIDTBL_VERSION_INIT;
293         }
294
295         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
296         RETURN(rc);
297 }
298
299 static int mgs_nidtbl_write(struct fs_db *fsdb, struct mgs_target_info *mti)
300 {
301         struct mgs_nidtbl        *tbl;
302         struct mgs_nidtbl_target *tgt;
303         bool found = false;
304         int type   = mti->mti_flags & LDD_F_SV_TYPE_MASK;
305         int rc     = 0;
306         ENTRY;
307
308         type &= ~LDD_F_SV_TYPE_MGS;
309         LASSERT(type != 0);
310
311         tbl = &fsdb->fsdb_nidtbl;
312         cfs_mutex_lock(&tbl->mn_lock);
313         cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
314                 struct mgs_target_info *info = &tgt->mnt_mti;
315                 if (type == tgt->mnt_type &&
316                     mti->mti_stripe_index == info->mti_stripe_index) {
317                         found = true;
318                         break;
319                 }
320         }
321         if (!found) {
322                 OBD_ALLOC_PTR(tgt);
323                 if (tgt == NULL)
324                         GOTO(out, rc = -ENOMEM);
325
326                 CFS_INIT_LIST_HEAD(&tgt->mnt_list);
327                 tgt->mnt_fs      = tbl;
328                 tgt->mnt_version = 0;       /* 0 means invalid */
329                 tgt->mnt_type    = type;
330
331                 ++tbl->mn_nr_targets;
332         }
333
334         tgt->mnt_version = ++tbl->mn_version;
335         tgt->mnt_mti     = *mti;
336
337         cfs_list_move_tail(&tgt->mnt_list, &tbl->mn_targets);
338
339         rc = nidtbl_update_version(fsdb->fsdb_obd, tbl);
340         EXIT;
341
342 out:
343         cfs_mutex_unlock(&tbl->mn_lock);
344         if (rc)
345                 CERROR("Write NID table version for file system %s error %d\n",
346                        fsdb->fsdb_name, rc);
347         return rc;
348 }
349
350 static void mgs_nidtbl_fini_fs(struct fs_db *fsdb)
351 {
352         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
353         CFS_LIST_HEAD(head);
354
355         cfs_mutex_lock(&tbl->mn_lock);
356         tbl->mn_nr_targets = 0;
357         cfs_list_splice_init(&tbl->mn_targets, &head);
358         cfs_mutex_unlock(&tbl->mn_lock);
359
360         while (!cfs_list_empty(&head)) {
361                 struct mgs_nidtbl_target *tgt;
362                 tgt = list_entry(head.next, struct mgs_nidtbl_target, mnt_list);
363                 cfs_list_del(&tgt->mnt_list);
364                 OBD_FREE_PTR(tgt);
365         }
366 }
367
368 static int mgs_nidtbl_init_fs(struct fs_db *fsdb)
369 {
370         struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
371
372         CFS_INIT_LIST_HEAD(&tbl->mn_targets);
373         cfs_mutex_init(&tbl->mn_lock);
374         tbl->mn_nr_targets = 0;
375         tbl->mn_fsdb = fsdb;
376         cfs_mutex_lock(&tbl->mn_lock);
377         tbl->mn_version = nidtbl_read_version(fsdb->fsdb_obd, tbl);
378         cfs_mutex_unlock(&tbl->mn_lock);
379         CDEBUG(D_MGS, "IR: current version is %llu\n", tbl->mn_version);
380
381         return 0;
382 }
383
384 /* --------- Imperative Recovery relies on nidtbl stuff ------- */
385 void mgs_ir_notify_complete(struct fs_db *fsdb)
386 {
387         struct timeval tv;
388         cfs_duration_t delta;
389
390         cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
391
392         /* do statistic */
393         fsdb->fsdb_notify_count++;
394         delta = cfs_time_sub(cfs_time_current(), fsdb->fsdb_notify_start);
395         fsdb->fsdb_notify_total += delta;
396         if (delta > fsdb->fsdb_notify_max)
397                 fsdb->fsdb_notify_max = delta;
398
399         cfs_duration_usec(delta, &tv);
400         CDEBUG(D_MGS, "Revoke recover lock of %s completed after %ld.%06lds\n",
401                fsdb->fsdb_name, tv.tv_sec, tv.tv_usec);
402 }
403
404 static int mgs_ir_notify(void *arg)
405 {
406         struct fs_db      *fsdb   = arg;
407         struct ldlm_res_id resid;
408
409         char name[sizeof(fsdb->fsdb_name) + 20];
410
411         LASSERTF(sizeof(name) < 32, "name is too large to be in stack.\n");
412         sprintf(name, "mgs_%s_notify", fsdb->fsdb_name);
413         cfs_daemonize(name);
414
415         cfs_complete(&fsdb->fsdb_notify_comp);
416
417         set_user_nice(current, -2);
418
419         mgc_fsname2resid(fsdb->fsdb_name, &resid, CONFIG_T_RECOVER);
420         while (1) {
421                 struct l_wait_info   lwi = { 0 };
422
423                 l_wait_event(fsdb->fsdb_notify_waitq,
424                              fsdb->fsdb_notify_stop ||
425                              cfs_atomic_read(&fsdb->fsdb_notify_phase),
426                              &lwi);
427                 if (fsdb->fsdb_notify_stop)
428                         break;
429
430                 CDEBUG(D_MGS, "%s woken up, phase is %d\n",
431                        name, cfs_atomic_read(&fsdb->fsdb_notify_phase));
432
433                 fsdb->fsdb_notify_start = cfs_time_current();
434                 mgs_revoke_lock(fsdb->fsdb_obd, fsdb, CONFIG_T_RECOVER);
435         }
436
437         cfs_complete(&fsdb->fsdb_notify_comp);
438         return 0;
439 }
440
441 int mgs_ir_init_fs(struct obd_device *obd, struct fs_db *fsdb)
442 {
443         struct mgs_obd *mgs = &obd->u.mgs;
444         int rc;
445
446         if (!ir_timeout)
447                 ir_timeout = OBD_IR_MGS_TIMEOUT;
448
449         fsdb->fsdb_ir_state = IR_FULL;
450         if (cfs_time_before(cfs_time_current_sec(),
451                             mgs->mgs_start_time + ir_timeout))
452                 fsdb->fsdb_ir_state = IR_STARTUP;
453         fsdb->fsdb_nonir_clients = 0;
454         CFS_INIT_LIST_HEAD(&fsdb->fsdb_clients);
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_fsc_cleanup_by_fsdb(fsdb);
477
478         mgs_nidtbl_fini_fs(fsdb);
479
480         LASSERT(cfs_list_empty(&fsdb->fsdb_clients));
481
482         fsdb->fsdb_notify_stop = 1;
483         cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
484         cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
485 }
486
487 /* caller must have held fsdb_mutex */
488 static inline void ir_state_graduate(struct fs_db *fsdb)
489 {
490         struct mgs_obd *mgs = &fsdb->fsdb_obd->u.mgs;
491
492         if (fsdb->fsdb_ir_state == IR_STARTUP) {
493                 if (cfs_time_before(mgs->mgs_start_time + ir_timeout,
494                                     cfs_time_current_sec())) {
495                         fsdb->fsdb_ir_state = IR_FULL;
496                         if (fsdb->fsdb_nonir_clients)
497                                 fsdb->fsdb_ir_state = IR_PARTIAL;
498                 }
499         }
500 }
501
502 int mgs_ir_update(struct obd_device *obd, struct mgs_target_info *mti)
503 {
504         struct fs_db *fsdb;
505         bool notify = true;
506         int rc;
507
508         if (mti->mti_instance == 0)
509                 return -EINVAL;
510
511         rc = mgs_find_or_make_fsdb(obd, mti->mti_fsname, &fsdb);
512         if (rc)
513                 return rc;
514
515         rc = mgs_nidtbl_write(fsdb, mti);
516         if (rc)
517                 return rc;
518
519         /* check ir state */
520         cfs_mutex_lock(&fsdb->fsdb_mutex);
521         ir_state_graduate(fsdb);
522         switch (fsdb->fsdb_ir_state) {
523         case IR_FULL:
524                 mti->mti_flags |= LDD_F_IR_CAPABLE;
525                 break;
526         case IR_DISABLED:
527                 notify = false;
528         case IR_STARTUP:
529         case IR_PARTIAL:
530                 break;
531         default:
532                 LBUG();
533         }
534         cfs_mutex_unlock(&fsdb->fsdb_mutex);
535
536         LASSERT(ergo(mti->mti_flags & LDD_F_IR_CAPABLE, notify));
537         if (notify) {
538                 CDEBUG(D_MGS, "Try to revoke recover lock of %s\n",
539                        fsdb->fsdb_name);
540                 cfs_atomic_inc(&fsdb->fsdb_notify_phase);
541                 cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
542         }
543         return 0;
544 }
545
546 /* NID table can be cached by two entities: Clients and MDTs */
547 enum {
548         IR_CLIENT  = 1,
549         IR_MDT     = 2
550 };
551
552 static int delogname(char *logname, char *fsname, int *typ)
553 {
554         char *ptr;
555         int   type;
556         int   len;
557
558         ptr = strrchr(logname, '-');
559         if (ptr == NULL)
560                 return -EINVAL;
561
562         /* decouple file system name. The llog name may be:
563          * - "prefix-fsname", prefix is "cliir" or "mdtir"
564          */
565         if (strncmp(ptr, "-mdtir", 6) == 0)
566                 type = IR_MDT;
567         else if (strncmp(ptr, "-cliir", 6) == 0)
568                 type = IR_CLIENT;
569         else
570                 return -EINVAL;
571
572         len = ptr - logname;
573         if (len == 0)
574                 return -EINVAL;
575
576         memcpy(fsname, logname, len);
577         fsname[len] = 0;
578         if (typ)
579                 *typ = type;
580         return 0;
581 }
582
583 int mgs_get_ir_logs(struct ptlrpc_request *req)
584 {
585         struct obd_device *obd = req->rq_export->exp_obd;
586         struct fs_db      *fsdb;
587         struct mgs_config_body  *body;
588         struct mgs_config_res   *res;
589         struct ptlrpc_bulk_desc *desc;
590         struct l_wait_info lwi;
591         char               fsname[16];
592         long               bufsize;
593         int                unit_size;
594
595         int                type;
596         int                rc = 0;
597         int                i;
598         int                bytes;
599         int                page_count;
600         int                nrpages;
601         cfs_page_t       **pages = NULL;
602         ENTRY;
603
604         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
605         if (body == NULL)
606                 RETURN(-EINVAL);
607
608         if (body->mcb_type != CONFIG_T_RECOVER)
609                 RETURN(-EINVAL);
610
611         rc = delogname(body->mcb_name, fsname, &type);
612         if (rc)
613                 RETURN(rc);
614
615         rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
616         if (rc)
617                 GOTO(out, rc);
618
619         bufsize = body->mcb_units << body->mcb_bits;
620         nrpages = (bufsize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
621         if (nrpages > PTLRPC_MAX_BRW_PAGES)
622                 RETURN(-EINVAL);
623
624         CDEBUG(D_MGS, "Reading IR log %s bufsize %ld.\n",
625                body->mcb_name, bufsize);
626
627         OBD_ALLOC(pages, sizeof(*pages) * nrpages);
628         if (pages == NULL)
629                 RETURN(-ENOMEM);
630
631         rc = req_capsule_server_pack(&req->rq_pill);
632         if (rc)
633                 GOTO(out, rc);
634
635         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
636         if (res == NULL)
637                 GOTO(out, rc = -EINVAL);
638
639         res->mcr_offset = body->mcb_offset;
640         unit_size = min_t(int, 1 << body->mcb_bits, CFS_PAGE_SIZE);
641         bytes = mgs_nidtbl_read(obd, &fsdb->fsdb_nidtbl, res, pages, nrpages,
642                                 bufsize / unit_size, unit_size);
643         if (bytes < 0)
644                 GOTO(out, rc = bytes);
645
646         /* start bulk transfer */
647         page_count = (bytes + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
648         LASSERT(page_count <= nrpages);
649         desc = ptlrpc_prep_bulk_exp(req, page_count,
650                                     BULK_PUT_SOURCE, MGS_BULK_PORTAL);
651         if (desc == NULL)
652                 GOTO(out, rc = -ENOMEM);
653
654         for (i = 0; i < page_count && bytes > 0; i++) {
655                 ptlrpc_prep_bulk_page(desc, pages[i], 0,
656                                 min_t(int, bytes, CFS_PAGE_SIZE));
657                 bytes -= CFS_PAGE_SIZE;
658         }
659
660         rc = target_bulk_io(req->rq_export, desc, &lwi);
661         ptlrpc_free_bulk(desc);
662
663 out:
664         if (pages) {
665                 for (i = 0; i < nrpages; i++) {
666                         if (pages[i] == NULL)
667                                 break;
668                         cfs_free_page(pages[i]);
669                 }
670                 OBD_FREE(pages, sizeof(*pages) * nrpages);
671         }
672         return rc;
673 }
674
675 static int lprocfs_ir_set_state(struct fs_db *fsdb, const char *buf)
676 {
677         const char *strings[] = IR_STRINGS;
678         int         state = -1;
679         int         i;
680
681         for (i = 0; i < ARRAY_SIZE(strings); i++) {
682                 if (strcmp(strings[i], buf) == 0) {
683                         state = i;
684                         break;
685                 }
686         }
687         if (state < 0)
688                 return -EINVAL;
689
690         CDEBUG(D_MGS, "change fsr state of %s from %s to %s\n",
691                fsdb->fsdb_name, strings[fsdb->fsdb_ir_state], strings[state]);
692         cfs_mutex_lock(&fsdb->fsdb_mutex);
693         if (state == IR_FULL && fsdb->fsdb_nonir_clients)
694                 state = IR_PARTIAL;
695         fsdb->fsdb_ir_state = state;
696         cfs_mutex_unlock(&fsdb->fsdb_mutex);
697
698         return 0;
699 }
700
701 static int lprocfs_ir_set_timeout(struct fs_db *fsdb, const char *buf)
702 {
703         return -EINVAL;
704 }
705
706 static int lprocfs_ir_clear_stats(struct fs_db *fsdb, const char *buf)
707 {
708         if (*buf)
709                 return -EINVAL;
710
711         fsdb->fsdb_notify_total = 0;
712         fsdb->fsdb_notify_max   = 0;
713         fsdb->fsdb_notify_count = 0;
714         return 0;
715 }
716
717 static struct lproc_ir_cmd {
718         char *name;
719         int   namelen;
720         int (*handler)(struct fs_db *, const char *);
721 } ir_cmds[] = {
722         { "state=",   6, lprocfs_ir_set_state },
723         { "timeout=", 8, lprocfs_ir_set_timeout },
724         { "0",        1, lprocfs_ir_clear_stats }
725 };
726
727 int lprocfs_wr_ir_state(struct file *file, const char *buffer,
728                          unsigned long count, void *data)
729 {
730         struct fs_db *fsdb = data;
731         char *kbuf;
732         char *ptr;
733         int rc = 0;
734
735         if (count > CFS_PAGE_SIZE)
736                 return -EINVAL;
737
738         OBD_ALLOC(kbuf, count + 1);
739         if (kbuf == NULL)
740                 return -ENOMEM;
741
742         if (copy_from_user(kbuf, buffer, count)) {
743                 OBD_FREE(kbuf, count);
744                 return -EFAULT;
745         }
746
747         kbuf[count] = 0; /* buffer is supposed to end with 0 */
748         if (kbuf[count - 1] == '\n')
749                 kbuf[count - 1] = 0;
750         ptr = kbuf;
751
752         /* fsname=<file system name> must be the 1st entry */
753         while (ptr != NULL) {
754                 char *tmpptr;
755                 int i;
756
757                 tmpptr = strchr(ptr, ';');
758                 if (tmpptr)
759                         *tmpptr++ = 0;
760
761                 rc = -EINVAL;
762                 for (i = 0; i < ARRAY_SIZE(ir_cmds); i++) {
763                         struct lproc_ir_cmd *cmd;
764                         int cmdlen;
765
766                         cmd    = &ir_cmds[i];
767                         cmdlen = cmd->namelen;
768                         if (strncmp(cmd->name, ptr, cmdlen) == 0) {
769                                 ptr += cmdlen;
770                                 rc = cmd->handler(fsdb, ptr);
771                                 break;
772                         }
773                 }
774                 if (rc)
775                         break;
776
777                 ptr = tmpptr;
778         }
779         if (rc)
780                 CERROR("Unable to process command: %s(%d)\n", ptr, rc);
781         OBD_FREE(kbuf, count + 1);
782         return rc ?: count;
783 }
784
785 int lprocfs_rd_ir_state(struct seq_file *seq, void *data)
786 {
787         struct fs_db      *fsdb = data;
788         struct mgs_nidtbl *tbl  = &fsdb->fsdb_nidtbl;
789         const char        *ir_strings[] = IR_STRINGS;
790         struct timeval     tv_max;
791         struct timeval     tv;
792
793         /* mgs_live_seq_show() already holds fsdb_mutex. */
794         ir_state_graduate(fsdb);
795
796         seq_printf(seq, "\nimperative_recovery_state:\n");
797         seq_printf(seq,
798                    "    state: %s\n"
799                    "    nonir_clients: %d\n"
800                    "    nidtbl_version: %lld\n",
801                    ir_strings[fsdb->fsdb_ir_state], fsdb->fsdb_nonir_clients,
802                    tbl->mn_version);
803
804         cfs_duration_usec(fsdb->fsdb_notify_total, &tv);
805         cfs_duration_usec(fsdb->fsdb_notify_max, &tv_max);
806
807         seq_printf(seq, "    notify_duration_total: %lu.%06lu\n"
808                         "    notify_duation_max: %lu.%06lu\n"
809                         "    notify_count: %u\n",
810                    tv.tv_sec, tv.tv_usec,
811                    tv_max.tv_sec, tv_max.tv_usec,
812                    fsdb->fsdb_notify_count);
813
814         return 0;
815 }
816
817 int lprocfs_rd_ir_timeout(char *page, char **start, off_t off, int count,
818                           int *eof, void *data)
819 {
820         *eof = 1;
821         return snprintf(page, count, "%d\n", ir_timeout);
822 }
823
824 int lprocfs_wr_ir_timeout(struct file *file, const char *buffer,
825                           unsigned long count, void *data)
826 {
827         return lprocfs_wr_uint(file, buffer, count, &ir_timeout);
828 }
829
830 /* --------------- Handle non IR support clients --------------- */
831 /* attach a lustre file system to an export */
832 int mgs_fsc_attach(struct obd_export *exp, char *fsname)
833 {
834         struct mgs_export_data *data = &exp->u.eu_mgs_data;
835         struct obd_device *obd       = exp->exp_obd;
836         struct fs_db      *fsdb;
837         struct mgs_fsc    *fsc     = NULL;
838         struct mgs_fsc    *new_fsc = NULL;
839         bool               found   = false;
840         int                rc;
841         ENTRY;
842
843         rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
844         if (rc)
845                 RETURN(rc);
846
847         /* allocate a new fsc in case we need it in spinlock. */
848         OBD_ALLOC_PTR(new_fsc);
849         if (new_fsc == NULL)
850                 RETURN(-ENOMEM);
851
852         CFS_INIT_LIST_HEAD(&new_fsc->mfc_export_list);
853         CFS_INIT_LIST_HEAD(&new_fsc->mfc_fsdb_list);
854         new_fsc->mfc_fsdb       = fsdb;
855         new_fsc->mfc_export     = class_export_get(exp);
856         new_fsc->mfc_ir_capable =
857                         !!(exp->exp_connect_flags & OBD_CONNECT_IMP_RECOV);
858
859         rc = -EEXIST;
860         cfs_mutex_lock(&fsdb->fsdb_mutex);
861
862         /* tend to find it in export list because this list is shorter. */
863         cfs_spin_lock(&data->med_lock);
864         cfs_list_for_each_entry(fsc, &data->med_clients, mfc_export_list) {
865                 if (strcmp(fsname, fsc->mfc_fsdb->fsdb_name) == 0) {
866                         found = true;
867                         break;
868                 }
869         }
870         if (!found) {
871                 fsc = new_fsc;
872                 new_fsc = NULL;
873
874                 /* add it into export list. */
875                 cfs_list_add(&fsc->mfc_export_list, &data->med_clients);
876
877                 /* add into fsdb list. */
878                 cfs_list_add(&fsc->mfc_fsdb_list, &fsdb->fsdb_clients);
879                 if (!fsc->mfc_ir_capable) {
880                         ++fsdb->fsdb_nonir_clients;
881                         if (fsdb->fsdb_ir_state == IR_FULL)
882                                 fsdb->fsdb_ir_state = IR_PARTIAL;
883                 }
884                 rc = 0;
885         }
886         cfs_spin_unlock(&data->med_lock);
887         cfs_mutex_unlock(&fsdb->fsdb_mutex);
888
889         if (new_fsc) {
890                 class_export_put(new_fsc->mfc_export);
891                 OBD_FREE_PTR(new_fsc);
892         }
893         RETURN(rc);
894 }
895
896 void mgs_fsc_cleanup(struct obd_export *exp)
897 {
898         struct mgs_export_data *data = &exp->u.eu_mgs_data;
899         struct mgs_fsc *fsc, *tmp;
900         CFS_LIST_HEAD(head);
901
902         cfs_spin_lock(&data->med_lock);
903         cfs_list_splice_init(&data->med_clients, &head);
904         cfs_spin_unlock(&data->med_lock);
905
906         cfs_list_for_each_entry_safe(fsc, tmp, &head, mfc_export_list) {
907                 struct fs_db *fsdb = fsc->mfc_fsdb;
908
909                 LASSERT(fsc->mfc_export == exp);
910
911                 cfs_mutex_lock(&fsdb->fsdb_mutex);
912                 cfs_list_del_init(&fsc->mfc_fsdb_list);
913                 if (fsc->mfc_ir_capable == 0) {
914                         --fsdb->fsdb_nonir_clients;
915                         LASSERT(fsdb->fsdb_ir_state != IR_FULL);
916                         if (fsdb->fsdb_nonir_clients == 0 &&
917                             fsdb->fsdb_ir_state == IR_PARTIAL)
918                                 fsdb->fsdb_ir_state = IR_FULL;
919                 }
920                 cfs_mutex_unlock(&fsdb->fsdb_mutex);
921                 cfs_list_del_init(&fsc->mfc_export_list);
922                 class_export_put(fsc->mfc_export);
923                 OBD_FREE_PTR(fsc);
924         }
925 }
926
927 /* must be called with fsdb->fsdb_mutex held */
928 void mgs_fsc_cleanup_by_fsdb(struct fs_db *fsdb)
929 {
930         struct mgs_fsc *fsc, *tmp;
931
932         cfs_list_for_each_entry_safe(fsc, tmp, &fsdb->fsdb_clients,
933                                      mfc_fsdb_list) {
934                 struct mgs_export_data *data = &fsc->mfc_export->u.eu_mgs_data;
935
936                 LASSERT(fsdb == fsc->mfc_fsdb);
937                 cfs_list_del_init(&fsc->mfc_fsdb_list);
938
939                 cfs_spin_lock(&data->med_lock);
940                 cfs_list_del_init(&fsc->mfc_export_list);
941                 cfs_spin_unlock(&data->med_lock);
942                 class_export_put(fsc->mfc_export);
943                 OBD_FREE_PTR(fsc);
944         }
945
946         fsdb->fsdb_nonir_clients = 0;
947         if (fsdb->fsdb_ir_state == IR_PARTIAL)
948                 fsdb->fsdb_ir_state = IR_FULL;
949 }