4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mgs/mgs_nids.c
38 * NID table management for lustre.
40 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_MGS
44 #define D_MGS D_CONFIG
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
54 #include <obd_class.h>
55 #include <lustre_log.h>
57 #include <libcfs/list.h>
58 #include <linux/lvfs.h>
59 #include <lustre_fsfilt.h>
60 #include <lustre_disk.h>
61 #include <lustre_param.h>
62 #include "mgs_internal.h"
64 static unsigned int ir_timeout;
66 static int nidtbl_is_sane(struct mgs_nidtbl *tbl)
68 struct mgs_nidtbl_target *tgt;
71 LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
72 cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
73 if (!tgt->mnt_version)
76 if (version >= tgt->mnt_version)
79 version = tgt->mnt_version;
85 * Fetch nidtbl entries whose version are not less than @version
86 * nidtbl entries will be packed in @pages by @unit_size units - entries
87 * shouldn't cross unit boundaries.
89 static int mgs_nidtbl_read(struct obd_export *exp, struct mgs_nidtbl *tbl,
90 struct mgs_config_res *res, cfs_page_t **pages,
91 int nrpages, int units_total, int unit_size)
93 struct mgs_nidtbl_target *tgt;
94 struct mgs_nidtbl_entry *entry;
95 struct mgs_nidtbl_entry *last_in_unit = NULL;
96 struct mgs_target_info *mti;
97 __u64 version = res->mcr_offset;
100 int bytes_in_unit = 0;
101 int units_in_page = 0;
106 /* make sure unit_size is power 2 */
107 LASSERT((unit_size & (unit_size - 1)) == 0);
108 LASSERT(nrpages << CFS_PAGE_SHIFT >= units_total * unit_size);
110 cfs_mutex_lock(&tbl->mn_lock);
111 LASSERT(nidtbl_is_sane(tbl));
113 /* no more entries ? */
114 if (version > tbl->mn_version) {
115 version = tbl->mn_version;
119 /* iterate over all targets to compose a bitmap by the type of llog.
120 * If the llog is for MDTs, llog entries for OSTs will be returned;
121 * otherwise, it's for clients, then llog entries for both OSTs and
122 * MDTs will be returned.
124 cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
125 int entry_len = sizeof(*entry);
127 if (tgt->mnt_version < version)
130 /* write target recover information */
132 LASSERT(mti->mti_nid_count < MTI_NIDS_MAX);
133 entry_len += mti->mti_nid_count * sizeof(lnet_nid_t);
135 if (entry_len > unit_size) {
136 CWARN("nidtbl: too large entry: entry length %d,"
137 "unit size: %d\n", entry_len, unit_size);
138 GOTO(out, rc = -EOVERFLOW);
141 if (bytes_in_unit < entry_len) {
142 if (units_total == 0) {
147 /* check if we need to consume remaining bytes. */
148 if (last_in_unit != NULL && bytes_in_unit) {
150 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
151 /* May need to swab back to update the length.*/
152 if (exp->exp_need_mne_swab)
153 lustre_swab_mgs_nidtbl_entry(last_in_unit);
155 last_in_unit->mne_length += bytes_in_unit;
156 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
157 if (exp->exp_need_mne_swab)
158 lustre_swab_mgs_nidtbl_entry(last_in_unit);
161 buf += bytes_in_unit;
164 LASSERT((rc & (unit_size - 1)) == 0);
166 if (units_in_page == 0) {
167 /* allocate a new page */
168 pages[index] = cfs_alloc_page(CFS_ALLOC_STD);
169 if (pages[index] == NULL) {
174 /* destroy previous map */
176 cfs_kunmap(pages[index - 1]);
178 /* reassign buffer */
179 buf = cfs_kmap(pages[index]);
182 units_in_page = CFS_PAGE_SIZE / unit_size;
183 LASSERT(units_in_page > 0);
186 /* allocate an unit */
187 LASSERT(((long)buf & (unit_size - 1)) == 0);
188 bytes_in_unit = unit_size;
194 entry = (struct mgs_nidtbl_entry *)buf;
195 entry->mne_version = tgt->mnt_version;
196 entry->mne_instance = mti->mti_instance;
197 entry->mne_index = mti->mti_stripe_index;
198 entry->mne_length = entry_len;
199 entry->mne_type = tgt->mnt_type;
200 entry->mne_nid_type = 0;
201 entry->mne_nid_size = sizeof(lnet_nid_t);
202 entry->mne_nid_count = mti->mti_nid_count;
203 memcpy(entry->u.nids, mti->mti_nids,
204 mti->mti_nid_count * sizeof(lnet_nid_t));
206 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
207 /* For LU-1644, swab entry for 2.2 clients. */
208 if (exp->exp_need_mne_swab)
209 lustre_swab_mgs_nidtbl_entry(entry);
212 version = tgt->mnt_version;
216 bytes_in_unit -= entry_len;
217 last_in_unit = entry;
219 CDEBUG(D_MGS, "fsname %s, entry size %d, pages %d/%d/%d/%d.\n",
220 tbl->mn_fsdb->fsdb_name, entry_len,
221 bytes_in_unit, index, nrpages, units_total);
224 cfs_kunmap(pages[index - 1]);
226 LASSERT(version <= tbl->mn_version);
227 res->mcr_size = tbl->mn_version;
228 res->mcr_offset = nobuf ? version : tbl->mn_version;
229 cfs_mutex_unlock(&tbl->mn_lock);
230 LASSERT(ergo(version == 1, rc == 0)); /* get the log first time */
232 CDEBUG(D_MGS, "Read IR logs %s return with %d, version %llu\n",
233 tbl->mn_fsdb->fsdb_name, rc, version);
237 static int nidtbl_update_version(struct obd_device *obd, struct mgs_nidtbl *tbl)
239 struct lvfs_run_ctxt saved;
240 struct file *file = NULL;
241 char filename[sizeof(MGS_NIDTBL_DIR) + 9];
247 LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
248 LASSERT(sizeof(filename) < 32);
250 sprintf(filename, "%s/%s",
251 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
253 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
255 file = l_filp_open(filename, O_RDWR|O_CREAT, 0660);
257 version = cpu_to_le64(tbl->mn_version);
258 rc = lustre_fwrite(file, &version, sizeof(version), &off);
259 if (rc == sizeof(version))
262 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
267 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
271 #define MGS_NIDTBL_VERSION_INIT 2
273 static int nidtbl_read_version(struct obd_device *obd, struct mgs_nidtbl *tbl)
275 struct lvfs_run_ctxt saved;
276 struct file *file = NULL;
277 char filename[sizeof(MGS_NIDTBL_DIR) + 9];
283 LASSERT(cfs_mutex_is_locked(&tbl->mn_lock));
284 LASSERT(sizeof(filename) < 32);
286 sprintf(filename, "%s/%s",
287 MGS_NIDTBL_DIR, tbl->mn_fsdb->fsdb_name);
289 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
291 file = l_filp_open(filename, O_RDONLY, 0);
293 rc = lustre_fread(file, &version, sizeof(version), &off);
294 if (rc == sizeof(version))
295 rc = cpu_to_le64(version);
297 rc = MGS_NIDTBL_VERSION_INIT;
299 CERROR("read version file %s error %d\n", filename, rc);
304 rc = MGS_NIDTBL_VERSION_INIT;
307 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
311 static int mgs_nidtbl_write(struct fs_db *fsdb, struct mgs_target_info *mti)
313 struct mgs_nidtbl *tbl;
314 struct mgs_nidtbl_target *tgt;
316 int type = mti->mti_flags & LDD_F_SV_TYPE_MASK;
320 type &= ~LDD_F_SV_TYPE_MGS;
323 tbl = &fsdb->fsdb_nidtbl;
324 cfs_mutex_lock(&tbl->mn_lock);
325 cfs_list_for_each_entry(tgt, &tbl->mn_targets, mnt_list) {
326 struct mgs_target_info *info = &tgt->mnt_mti;
327 if (type == tgt->mnt_type &&
328 mti->mti_stripe_index == info->mti_stripe_index) {
336 GOTO(out, rc = -ENOMEM);
338 CFS_INIT_LIST_HEAD(&tgt->mnt_list);
340 tgt->mnt_version = 0; /* 0 means invalid */
341 tgt->mnt_type = type;
343 ++tbl->mn_nr_targets;
346 tgt->mnt_version = ++tbl->mn_version;
349 cfs_list_move_tail(&tgt->mnt_list, &tbl->mn_targets);
351 rc = nidtbl_update_version(fsdb->fsdb_obd, tbl);
355 cfs_mutex_unlock(&tbl->mn_lock);
357 CERROR("Write NID table version for file system %s error %d\n",
358 fsdb->fsdb_name, rc);
362 static void mgs_nidtbl_fini_fs(struct fs_db *fsdb)
364 struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
367 cfs_mutex_lock(&tbl->mn_lock);
368 tbl->mn_nr_targets = 0;
369 cfs_list_splice_init(&tbl->mn_targets, &head);
370 cfs_mutex_unlock(&tbl->mn_lock);
372 while (!cfs_list_empty(&head)) {
373 struct mgs_nidtbl_target *tgt;
374 tgt = list_entry(head.next, struct mgs_nidtbl_target, mnt_list);
375 cfs_list_del(&tgt->mnt_list);
380 static int mgs_nidtbl_init_fs(struct fs_db *fsdb)
382 struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
384 CFS_INIT_LIST_HEAD(&tbl->mn_targets);
385 cfs_mutex_init(&tbl->mn_lock);
386 tbl->mn_nr_targets = 0;
388 cfs_mutex_lock(&tbl->mn_lock);
389 tbl->mn_version = nidtbl_read_version(fsdb->fsdb_obd, tbl);
390 cfs_mutex_unlock(&tbl->mn_lock);
391 CDEBUG(D_MGS, "IR: current version is %llu\n", tbl->mn_version);
396 /* --------- Imperative Recovery relies on nidtbl stuff ------- */
397 void mgs_ir_notify_complete(struct fs_db *fsdb)
400 cfs_duration_t delta;
402 cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
405 fsdb->fsdb_notify_count++;
406 delta = cfs_time_sub(cfs_time_current(), fsdb->fsdb_notify_start);
407 fsdb->fsdb_notify_total += delta;
408 if (delta > fsdb->fsdb_notify_max)
409 fsdb->fsdb_notify_max = delta;
411 cfs_duration_usec(delta, &tv);
412 CDEBUG(D_MGS, "Revoke recover lock of %s completed after %ld.%06lds\n",
413 fsdb->fsdb_name, tv.tv_sec, tv.tv_usec);
416 static int mgs_ir_notify(void *arg)
418 struct fs_db *fsdb = arg;
419 struct ldlm_res_id resid;
421 char name[sizeof(fsdb->fsdb_name) + 20];
423 LASSERTF(sizeof(name) < 32, "name is too large to be in stack.\n");
424 sprintf(name, "mgs_%s_notify", fsdb->fsdb_name);
427 cfs_complete(&fsdb->fsdb_notify_comp);
429 set_user_nice(current, -2);
431 mgc_fsname2resid(fsdb->fsdb_name, &resid, CONFIG_T_RECOVER);
433 struct l_wait_info lwi = { 0 };
435 l_wait_event(fsdb->fsdb_notify_waitq,
436 fsdb->fsdb_notify_stop ||
437 cfs_atomic_read(&fsdb->fsdb_notify_phase),
439 if (fsdb->fsdb_notify_stop)
442 CDEBUG(D_MGS, "%s woken up, phase is %d\n",
443 name, cfs_atomic_read(&fsdb->fsdb_notify_phase));
445 fsdb->fsdb_notify_start = cfs_time_current();
446 mgs_revoke_lock(fsdb->fsdb_obd, fsdb, CONFIG_T_RECOVER);
449 cfs_complete(&fsdb->fsdb_notify_comp);
453 int mgs_ir_init_fs(struct obd_device *obd, struct fs_db *fsdb)
455 struct mgs_obd *mgs = &obd->u.mgs;
459 ir_timeout = OBD_IR_MGS_TIMEOUT;
461 fsdb->fsdb_ir_state = IR_FULL;
462 if (cfs_time_before(cfs_time_current_sec(),
463 mgs->mgs_start_time + ir_timeout))
464 fsdb->fsdb_ir_state = IR_STARTUP;
465 fsdb->fsdb_nonir_clients = 0;
466 CFS_INIT_LIST_HEAD(&fsdb->fsdb_clients);
468 /* start notify thread */
469 fsdb->fsdb_obd = obd;
470 cfs_atomic_set(&fsdb->fsdb_notify_phase, 0);
471 cfs_waitq_init(&fsdb->fsdb_notify_waitq);
472 cfs_init_completion(&fsdb->fsdb_notify_comp);
473 rc = cfs_create_thread(mgs_ir_notify, fsdb, CFS_DAEMON_FLAGS);
475 cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
477 CERROR("Start notify thread error %d\n", rc);
479 mgs_nidtbl_init_fs(fsdb);
483 void mgs_ir_fini_fs(struct obd_device *obd, struct fs_db *fsdb)
485 if (cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
488 mgs_fsc_cleanup_by_fsdb(fsdb);
490 mgs_nidtbl_fini_fs(fsdb);
492 LASSERT(cfs_list_empty(&fsdb->fsdb_clients));
494 fsdb->fsdb_notify_stop = 1;
495 cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
496 cfs_wait_for_completion(&fsdb->fsdb_notify_comp);
499 /* caller must have held fsdb_mutex */
500 static inline void ir_state_graduate(struct fs_db *fsdb)
502 struct mgs_obd *mgs = &fsdb->fsdb_obd->u.mgs;
504 if (fsdb->fsdb_ir_state == IR_STARTUP) {
505 if (cfs_time_before(mgs->mgs_start_time + ir_timeout,
506 cfs_time_current_sec())) {
507 fsdb->fsdb_ir_state = IR_FULL;
508 if (fsdb->fsdb_nonir_clients)
509 fsdb->fsdb_ir_state = IR_PARTIAL;
514 int mgs_ir_update(struct obd_device *obd, struct mgs_target_info *mti)
520 if (mti->mti_instance == 0)
523 rc = mgs_find_or_make_fsdb(obd, mti->mti_fsname, &fsdb);
527 rc = mgs_nidtbl_write(fsdb, mti);
532 cfs_mutex_lock(&fsdb->fsdb_mutex);
533 ir_state_graduate(fsdb);
534 switch (fsdb->fsdb_ir_state) {
536 mti->mti_flags |= LDD_F_IR_CAPABLE;
546 cfs_mutex_unlock(&fsdb->fsdb_mutex);
548 LASSERT(ergo(mti->mti_flags & LDD_F_IR_CAPABLE, notify));
550 CDEBUG(D_MGS, "Try to revoke recover lock of %s\n",
552 cfs_atomic_inc(&fsdb->fsdb_notify_phase);
553 cfs_waitq_signal(&fsdb->fsdb_notify_waitq);
558 /* NID table can be cached by two entities: Clients and MDTs */
564 static int delogname(char *logname, char *fsname, int *typ)
570 ptr = strrchr(logname, '-');
574 /* decouple file system name. The llog name may be:
575 * - "prefix-fsname", prefix is "cliir" or "mdtir"
577 if (strncmp(ptr, "-mdtir", 6) == 0)
579 else if (strncmp(ptr, "-cliir", 6) == 0)
588 memcpy(fsname, logname, len);
595 int mgs_get_ir_logs(struct ptlrpc_request *req)
597 struct obd_device *obd = req->rq_export->exp_obd;
599 struct mgs_config_body *body;
600 struct mgs_config_res *res;
601 struct ptlrpc_bulk_desc *desc;
602 struct l_wait_info lwi;
613 cfs_page_t **pages = NULL;
616 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
620 if (body->mcb_type != CONFIG_T_RECOVER)
623 rc = delogname(body->mcb_name, fsname, &type);
627 rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
631 bufsize = body->mcb_units << body->mcb_bits;
632 nrpages = (bufsize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
633 if (nrpages > PTLRPC_MAX_BRW_PAGES)
636 CDEBUG(D_MGS, "Reading IR log %s bufsize %ld.\n",
637 body->mcb_name, bufsize);
639 OBD_ALLOC(pages, sizeof(*pages) * nrpages);
643 rc = req_capsule_server_pack(&req->rq_pill);
647 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
649 GOTO(out, rc = -EINVAL);
651 res->mcr_offset = body->mcb_offset;
652 unit_size = min_t(int, 1 << body->mcb_bits, CFS_PAGE_SIZE);
653 bytes = mgs_nidtbl_read(req->rq_export, &fsdb->fsdb_nidtbl, res,
654 pages, nrpages, bufsize / unit_size, unit_size);
656 GOTO(out, rc = bytes);
658 /* start bulk transfer */
659 page_count = (bytes + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
660 LASSERT(page_count <= nrpages);
661 desc = ptlrpc_prep_bulk_exp(req, page_count,
662 BULK_PUT_SOURCE, MGS_BULK_PORTAL);
664 GOTO(out, rc = -ENOMEM);
666 for (i = 0; i < page_count && bytes > 0; i++) {
667 ptlrpc_prep_bulk_page(desc, pages[i], 0,
668 min_t(int, bytes, CFS_PAGE_SIZE));
669 bytes -= CFS_PAGE_SIZE;
672 rc = target_bulk_io(req->rq_export, desc, &lwi);
673 ptlrpc_free_bulk(desc);
677 for (i = 0; i < nrpages; i++) {
678 if (pages[i] == NULL)
680 cfs_free_page(pages[i]);
682 OBD_FREE(pages, sizeof(*pages) * nrpages);
687 static int lprocfs_ir_set_state(struct fs_db *fsdb, const char *buf)
689 const char *strings[] = IR_STRINGS;
693 for (i = 0; i < ARRAY_SIZE(strings); i++) {
694 if (strcmp(strings[i], buf) == 0) {
702 CDEBUG(D_MGS, "change fsr state of %s from %s to %s\n",
703 fsdb->fsdb_name, strings[fsdb->fsdb_ir_state], strings[state]);
704 cfs_mutex_lock(&fsdb->fsdb_mutex);
705 if (state == IR_FULL && fsdb->fsdb_nonir_clients)
707 fsdb->fsdb_ir_state = state;
708 cfs_mutex_unlock(&fsdb->fsdb_mutex);
713 static int lprocfs_ir_set_timeout(struct fs_db *fsdb, const char *buf)
718 static int lprocfs_ir_clear_stats(struct fs_db *fsdb, const char *buf)
723 fsdb->fsdb_notify_total = 0;
724 fsdb->fsdb_notify_max = 0;
725 fsdb->fsdb_notify_count = 0;
729 static struct lproc_ir_cmd {
732 int (*handler)(struct fs_db *, const char *);
734 { "state=", 6, lprocfs_ir_set_state },
735 { "timeout=", 8, lprocfs_ir_set_timeout },
736 { "0", 1, lprocfs_ir_clear_stats }
739 int lprocfs_wr_ir_state(struct file *file, const char *buffer,
740 unsigned long count, void *data)
742 struct fs_db *fsdb = data;
747 if (count > CFS_PAGE_SIZE)
750 OBD_ALLOC(kbuf, count + 1);
754 if (copy_from_user(kbuf, buffer, count)) {
755 OBD_FREE(kbuf, count);
759 kbuf[count] = 0; /* buffer is supposed to end with 0 */
760 if (kbuf[count - 1] == '\n')
764 /* fsname=<file system name> must be the 1st entry */
765 while (ptr != NULL) {
769 tmpptr = strchr(ptr, ';');
774 for (i = 0; i < ARRAY_SIZE(ir_cmds); i++) {
775 struct lproc_ir_cmd *cmd;
779 cmdlen = cmd->namelen;
780 if (strncmp(cmd->name, ptr, cmdlen) == 0) {
782 rc = cmd->handler(fsdb, ptr);
792 CERROR("Unable to process command: %s(%d)\n", ptr, rc);
793 OBD_FREE(kbuf, count + 1);
797 int lprocfs_rd_ir_state(struct seq_file *seq, void *data)
799 struct fs_db *fsdb = data;
800 struct mgs_nidtbl *tbl = &fsdb->fsdb_nidtbl;
801 const char *ir_strings[] = IR_STRINGS;
802 struct timeval tv_max;
805 /* mgs_live_seq_show() already holds fsdb_mutex. */
806 ir_state_graduate(fsdb);
808 seq_printf(seq, "\nimperative_recovery_state:\n");
811 " nonir_clients: %d\n"
812 " nidtbl_version: %lld\n",
813 ir_strings[fsdb->fsdb_ir_state], fsdb->fsdb_nonir_clients,
816 cfs_duration_usec(fsdb->fsdb_notify_total, &tv);
817 cfs_duration_usec(fsdb->fsdb_notify_max, &tv_max);
819 seq_printf(seq, " notify_duration_total: %lu.%06lu\n"
820 " notify_duation_max: %lu.%06lu\n"
821 " notify_count: %u\n",
822 tv.tv_sec, tv.tv_usec,
823 tv_max.tv_sec, tv_max.tv_usec,
824 fsdb->fsdb_notify_count);
829 int lprocfs_rd_ir_timeout(char *page, char **start, off_t off, int count,
830 int *eof, void *data)
833 return snprintf(page, count, "%d\n", ir_timeout);
836 int lprocfs_wr_ir_timeout(struct file *file, const char *buffer,
837 unsigned long count, void *data)
839 return lprocfs_wr_uint(file, buffer, count, &ir_timeout);
842 /* --------------- Handle non IR support clients --------------- */
843 /* attach a lustre file system to an export */
844 int mgs_fsc_attach(struct obd_export *exp, char *fsname)
846 struct mgs_export_data *data = &exp->u.eu_mgs_data;
847 struct obd_device *obd = exp->exp_obd;
849 struct mgs_fsc *fsc = NULL;
850 struct mgs_fsc *new_fsc = NULL;
855 rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
859 /* allocate a new fsc in case we need it in spinlock. */
860 OBD_ALLOC_PTR(new_fsc);
864 CFS_INIT_LIST_HEAD(&new_fsc->mfc_export_list);
865 CFS_INIT_LIST_HEAD(&new_fsc->mfc_fsdb_list);
866 new_fsc->mfc_fsdb = fsdb;
867 new_fsc->mfc_export = class_export_get(exp);
868 new_fsc->mfc_ir_capable =
869 !!(exp->exp_connect_flags & OBD_CONNECT_IMP_RECOV);
872 cfs_mutex_lock(&fsdb->fsdb_mutex);
874 /* tend to find it in export list because this list is shorter. */
875 cfs_spin_lock(&data->med_lock);
876 cfs_list_for_each_entry(fsc, &data->med_clients, mfc_export_list) {
877 if (strcmp(fsname, fsc->mfc_fsdb->fsdb_name) == 0) {
886 /* add it into export list. */
887 cfs_list_add(&fsc->mfc_export_list, &data->med_clients);
889 /* add into fsdb list. */
890 cfs_list_add(&fsc->mfc_fsdb_list, &fsdb->fsdb_clients);
891 if (!fsc->mfc_ir_capable) {
892 ++fsdb->fsdb_nonir_clients;
893 if (fsdb->fsdb_ir_state == IR_FULL)
894 fsdb->fsdb_ir_state = IR_PARTIAL;
898 cfs_spin_unlock(&data->med_lock);
899 cfs_mutex_unlock(&fsdb->fsdb_mutex);
902 class_export_put(new_fsc->mfc_export);
903 OBD_FREE_PTR(new_fsc);
908 void mgs_fsc_cleanup(struct obd_export *exp)
910 struct mgs_export_data *data = &exp->u.eu_mgs_data;
911 struct mgs_fsc *fsc, *tmp;
914 cfs_spin_lock(&data->med_lock);
915 cfs_list_splice_init(&data->med_clients, &head);
916 cfs_spin_unlock(&data->med_lock);
918 cfs_list_for_each_entry_safe(fsc, tmp, &head, mfc_export_list) {
919 struct fs_db *fsdb = fsc->mfc_fsdb;
921 LASSERT(fsc->mfc_export == exp);
923 cfs_mutex_lock(&fsdb->fsdb_mutex);
924 cfs_list_del_init(&fsc->mfc_fsdb_list);
925 if (fsc->mfc_ir_capable == 0) {
926 --fsdb->fsdb_nonir_clients;
927 LASSERT(fsdb->fsdb_ir_state != IR_FULL);
928 if (fsdb->fsdb_nonir_clients == 0 &&
929 fsdb->fsdb_ir_state == IR_PARTIAL)
930 fsdb->fsdb_ir_state = IR_FULL;
932 cfs_mutex_unlock(&fsdb->fsdb_mutex);
933 cfs_list_del_init(&fsc->mfc_export_list);
934 class_export_put(fsc->mfc_export);
939 /* must be called with fsdb->fsdb_mutex held */
940 void mgs_fsc_cleanup_by_fsdb(struct fs_db *fsdb)
942 struct mgs_fsc *fsc, *tmp;
944 cfs_list_for_each_entry_safe(fsc, tmp, &fsdb->fsdb_clients,
946 struct mgs_export_data *data = &fsc->mfc_export->u.eu_mgs_data;
948 LASSERT(fsdb == fsc->mfc_fsdb);
949 cfs_list_del_init(&fsc->mfc_fsdb_list);
951 cfs_spin_lock(&data->med_lock);
952 cfs_list_del_init(&fsc->mfc_export_list);
953 cfs_spin_unlock(&data->med_lock);
954 class_export_put(fsc->mfc_export);
958 fsdb->fsdb_nonir_clients = 0;
959 if (fsdb->fsdb_ir_state == IR_PARTIAL)
960 fsdb->fsdb_ir_state = IR_FULL;