Whamcloud - gitweb
LU-13252 mgs: Handle possible failure from name_create_mdt
[fs/lustre-release.git] / lustre / mgs / mgs_fs.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_fs.c
33  *
34  * Lustre Management Server (MGS) filesystem interface code
35  *
36  * Author: Nathan Rutman <nathan@clusterfs.com>
37  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_MGS
41
42 #include <lustre_fid.h>
43 #include "mgs_internal.h"
44
45 /**
46  * Initialize MGS per-export statistics.
47  *
48  * This function sets up procfs entries for various MGS export counters. These
49  * counters are for per-client statistics tracked on the server.
50  *
51  * \param[in] obd       OBD device
52  * \param[in] exp       OBD export
53  * \param[in] localdata NID of client
54  *
55  * \retval              0 if successful
56  * \retval              negative value on error
57  */
58 int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
59                           void *localdata)
60 {
61         lnet_nid_t *client_nid = localdata;
62         struct nid_stat *stats;
63         int rc;
64
65         ENTRY;
66
67         rc = lprocfs_exp_setup(exp, client_nid);
68         if (rc != 0)
69                 /* Mask error for already created /proc entries */
70                 RETURN(rc == -EALREADY ? 0 : rc);
71
72         stats = exp->exp_nid_stats;
73         stats->nid_stats = lprocfs_alloc_stats(LPROC_MGS_LAST,
74                                                LPROCFS_STATS_FLAG_NOPERCPU);
75         if (stats->nid_stats == NULL)
76                 RETURN(-ENOMEM);
77
78         mgs_stats_counter_init(stats->nid_stats);
79
80         rc = lprocfs_register_stats(stats->nid_proc, "stats", stats->nid_stats);
81         if (rc != 0) {
82                 lprocfs_free_stats(&stats->nid_stats);
83                 GOTO(out, rc);
84         }
85
86         rc = lprocfs_nid_ldlm_stats_init(stats);
87         if (rc != 0)
88                 GOTO(out, rc);
89
90 out:
91         RETURN(rc);
92 }
93
94 /**
95  * Add client export data to the MGS.  This data is currently NOT stored on
96  * disk in the last_rcvd file or anywhere else.  In the event of a MGS
97  * crash all connections are treated as new connections.
98  */
99 int mgs_client_add(struct obd_device *obd, struct obd_export *exp,
100                    void *localdata)
101 {
102         return 0;
103 }
104
105 /* Remove client export data from the MGS */
106 int mgs_client_free(struct obd_export *exp)
107 {
108         return 0;
109 }
110
111 int mgs_fs_setup(const struct lu_env *env, struct mgs_device *mgs)
112 {
113         struct lu_fid fid;
114         struct dt_object *o;
115         struct lu_fid rfid;
116         struct dt_object *root;
117         struct dt_object *nm_config_file_obj;
118         struct nm_config_file *nm_config_file;
119         int rc;
120
121         ENTRY;
122
123         OBD_SET_CTXT_MAGIC(&mgs->mgs_obd->obd_lvfs_ctxt);
124         mgs->mgs_obd->obd_lvfs_ctxt.dt = mgs->mgs_bottom;
125
126         /* XXX: fix when support for N:1 layering is implemented */
127         LASSERT(mgs->mgs_dt_dev.dd_lu_dev.ld_site);
128         mgs->mgs_dt_dev.dd_lu_dev.ld_site->ls_top_dev =
129                 &mgs->mgs_dt_dev.dd_lu_dev;
130
131         /* Setup the configs dir */
132         fid.f_seq = FID_SEQ_LOCAL_NAME;
133         fid.f_oid = 1;
134         fid.f_ver = 0;
135         rc = local_oid_storage_init(env, mgs->mgs_bottom, &fid, &mgs->mgs_los);
136         if (rc)
137                 GOTO(out, rc);
138
139         rc = dt_root_get(env, mgs->mgs_bottom, &rfid);
140         if (rc)
141                 GOTO(out_los, rc);
142
143         root = dt_locate_at(env, mgs->mgs_bottom, &rfid,
144                             &mgs->mgs_dt_dev.dd_lu_dev, NULL);
145         if (unlikely(IS_ERR(root)))
146                 GOTO(out_los, rc = PTR_ERR(root));
147
148         o = local_file_find_or_create(env, mgs->mgs_los, root,
149                                       MOUNT_CONFIGS_DIR,
150                                       S_IFDIR | 0755);
151         if (IS_ERR(o))
152                 GOTO(out_root, rc = PTR_ERR(o));
153
154         if (!dt_try_as_dir(env, o)) {
155                 dt_object_put(env, o);
156                 GOTO(out_root, rc = -ENOTDIR);
157         }
158
159         mgs->mgs_configs_dir = o;
160
161         /* colocated MDT will cache config in target root dir */
162         nm_config_file_obj = local_index_find_or_create(env, mgs->mgs_los,
163                                                         mgs->mgs_configs_dir,
164                                                         LUSTRE_NODEMAP_NAME,
165                                                         S_IFREG | 0644,
166                                                         &dt_nodemap_features);
167         if (IS_ERR(nm_config_file_obj))
168                 GOTO(out_configs, rc = PTR_ERR(nm_config_file_obj));
169
170         if (nm_config_file_obj->do_index_ops == NULL) {
171                 rc = nm_config_file_obj->do_ops->do_index_try(env,
172                                                         nm_config_file_obj,
173                                                         &dt_nodemap_features);
174                 if (rc < 0) {
175                         dt_object_put(env, nm_config_file_obj);
176                         GOTO(out_configs, rc);
177                 }
178         }
179         nm_config_file = nm_config_file_register_mgs(env, nm_config_file_obj,
180                                                      mgs->mgs_los);
181         dt_object_put(env, nm_config_file_obj);
182         if (IS_ERR(nm_config_file)) {
183                 CERROR("%s: error loading nodemap config file, file must be "
184                        "removed via ldiskfs: rc = %ld\n",
185                        mgs->mgs_obd->obd_name, PTR_ERR(nm_config_file));
186                 GOTO(out_configs, rc = PTR_ERR(nm_config_file));
187         }
188         mgs->mgs_obd->u.obt.obt_nodemap_config_file = nm_config_file;
189
190         /* create directory to store nid table versions */
191         o = local_file_find_or_create(env, mgs->mgs_los, root, MGS_NIDTBL_DIR,
192                                       S_IFDIR | 0755);
193         if (IS_ERR(o))
194                 GOTO(out_nm, rc = PTR_ERR(o));
195
196         mgs->mgs_nidtbl_dir = o;
197
198 out_nm:
199         if (rc < 0) {
200                 nm_config_file_deregister_mgs(env, nm_config_file);
201                 mgs->mgs_obd->u.obt.obt_nodemap_config_file = NULL;
202         }
203 out_configs:
204         if (rc < 0) {
205                 dt_object_put(env, mgs->mgs_configs_dir);
206                 mgs->mgs_configs_dir = NULL;
207         }
208 out_root:
209         dt_object_put(env, root);
210 out_los:
211         if (rc) {
212                 local_oid_storage_fini(env, mgs->mgs_los);
213                 mgs->mgs_los = NULL;
214         }
215 out:
216         mgs->mgs_dt_dev.dd_lu_dev.ld_site->ls_top_dev = NULL;
217
218         return rc;
219 }
220
221 int mgs_fs_cleanup(const struct lu_env *env, struct mgs_device *mgs)
222 {
223         struct lustre_cfg_bufs bufs;
224         struct lustre_cfg *lcfg;
225
226         /*
227          * For the MGS on independent device from MDT, it notifies the lower
228          * layer OSD to backup index before the umount via LCFG_PRE_CLEANUP.
229          */
230         lustre_cfg_bufs_reset(&bufs, mgs->mgs_obd->obd_name);
231         lustre_cfg_bufs_set_string(&bufs, 1, NULL);
232         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
233         if (!lcfg) {
234                 CERROR("%s: failed to trigger LCFG_PRE_CLEANUP\n",
235                        mgs->mgs_obd->obd_name);
236         } else {
237                 struct lu_device *l = &mgs->mgs_bottom->dd_lu_dev;
238
239                 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, &bufs);
240                 l->ld_ops->ldo_process_config(env, l, lcfg);
241                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
242                                               lcfg->lcfg_buflens));
243         }
244
245         if (mgs->mgs_configs_dir) {
246                 dt_object_put(env, mgs->mgs_configs_dir);
247                 mgs->mgs_configs_dir = NULL;
248         }
249         if (mgs->mgs_nidtbl_dir) {
250                 dt_object_put(env, mgs->mgs_nidtbl_dir);
251                 mgs->mgs_nidtbl_dir = NULL;
252         }
253         if (mgs->mgs_obd->u.obt.obt_nodemap_config_file != NULL) {
254                 struct nm_config_file *ncf;
255
256                 ncf = mgs->mgs_obd->u.obt.obt_nodemap_config_file;
257                 nm_config_file_deregister_mgs(env, ncf);
258                 mgs->mgs_obd->u.obt.obt_nodemap_config_file = NULL;
259         }
260
261         if (mgs->mgs_los) {
262                 local_oid_storage_fini(env, mgs->mgs_los);
263                 mgs->mgs_los = NULL;
264         }
265
266         return 0;
267 }