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