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