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