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