Whamcloud - gitweb
6bab7137181c8e3537b3d59fb8baba271eb8af0f
[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.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, Whamcloud, Inc.
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_fs.c
37  *
38  * Lustre Management Server (MGS) filesystem interface code
39  *
40  * Author: Nathan Rutman <nathan@clusterfs.com>
41  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MGS
45
46 #include <linux/module.h>
47 #include <linux/kmod.h>
48 #include <linux/version.h>
49 #include <linux/sched.h>
50 #include <linux/mount.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_disk.h>
54 #include <lustre_lib.h>
55 #include <lustre_fsfilt.h>
56 #include <libcfs/list.h>
57 #include <lustre_fid.h>
58 #include "mgs_internal.h"
59
60 int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
61                           void *localdata)
62
63 {
64         lnet_nid_t *client_nid = localdata;
65         int rc, newnid;
66         ENTRY;
67
68         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
69         if (rc) {
70                 /* Mask error for already created
71                  * /proc entries */
72                 if (rc == -EALREADY)
73                         rc = 0;
74                 RETURN(rc);
75         }
76         if (newnid) {
77                 struct nid_stat *tmp = exp->exp_nid_stats;
78                 int num_stats = 0;
79
80                 num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
81                             LPROC_MGS_LAST - 1;
82                 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
83                                                      LPROCFS_STATS_FLAG_NOPERCPU);
84                 if (tmp->nid_stats == NULL)
85                         return -ENOMEM;
86                 lprocfs_init_ops_stats(LPROC_MGS_LAST, tmp->nid_stats);
87                 mgs_stats_counter_init(tmp->nid_stats);
88                 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
89                                             tmp->nid_stats);
90                 if (rc)
91                         GOTO(clean, rc);
92
93                 rc = lprocfs_nid_ldlm_stats_init(tmp);
94                 if (rc)
95                         GOTO(clean, rc);
96         }
97         RETURN(0);
98 clean:
99         return rc;
100 }
101
102 /**
103  * Add client export data to the MGS.  This data is currently NOT stored on
104  * disk in the last_rcvd file or anywhere else.  In the event of a MGS
105  * crash all connections are treated as new connections.
106  */
107 int mgs_client_add(struct obd_device *obd, struct obd_export *exp,
108                    void *localdata)
109 {
110         return 0;
111 }
112
113 /* Remove client export data from the MGS */
114 int mgs_client_free(struct obd_export *exp)
115 {
116         return 0;
117 }
118
119 /* Same as mds_lvfs_fid2dentry */
120 /* Look up an entry by inode number. */
121 /* this function ONLY returns valid dget'd dentries with an initialized inode
122    or errors */
123 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen,
124                                           __u64 gr, void *data)
125 {
126         struct fsfilt_fid  fid;
127         struct obd_device *obd = (struct obd_device *)data;
128         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
129         ENTRY;
130
131         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
132                (unsigned long)id, gen, mgs->mgs_sb);
133
134         if (id == 0)
135                 RETURN(ERR_PTR(-ESTALE));
136
137         fid.ino = id;
138         fid.gen = gen;
139
140         RETURN(fsfilt_fid2dentry(obd, mgs->mgs_vfsmnt, &fid, 0));
141 }
142
143 struct lvfs_callback_ops mgs_lvfs_ops = {
144         l_fid2dentry:     mgs_lvfs_fid2dentry,
145 };
146
147 int mgs_fs_setup_old(const struct lu_env *env, struct mgs_device *mgs)
148 {
149         struct obd_device *obd = mgs->mgs_obd;
150         struct dt_device_param p;
151         struct vfsmount *mnt;
152         struct lvfs_run_ctxt saved;
153         struct dentry *dentry;
154         int rc;
155         ENTRY;
156
157         dt_conf_get(env, mgs->mgs_bottom, &p);
158         mnt = p.ddp_mnt;
159         if (mnt == NULL) {
160                 CERROR("%s: no proper support for OSD yet\n", obd->obd_name);
161                 RETURN(-ENODEV);
162         }
163
164         /* FIXME what's this?  Do I need it? */
165         rc = cfs_cleanup_group_info();
166         if (rc)
167                 RETURN(rc);
168
169         mgs->mgs_vfsmnt = mnt;
170         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
171
172         obd->obd_fsops = fsfilt_get_ops(mt_str(p.ddp_mount_type));
173         if (IS_ERR(obd->obd_fsops))
174                 RETURN(PTR_ERR(obd->obd_fsops));
175
176         rc = fsfilt_setup(obd, mgs->mgs_sb);
177         if (rc)
178                 RETURN(rc);
179
180         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
181         obd->obd_lvfs_ctxt.pwdmnt = mnt;
182         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
183         obd->obd_lvfs_ctxt.fs = get_ds();
184         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
185
186         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
187
188         /* Setup the configs dir */
189         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MOUNT_CONFIGS_DIR, 0777, 1);
190         if (IS_ERR(dentry)) {
191                 rc = PTR_ERR(dentry);
192                 CERROR("cannot create %s directory: rc = %d\n",
193                        MOUNT_CONFIGS_DIR, rc);
194                 GOTO(err_pop, rc);
195         }
196         mgs->mgs_configs_dir_old = dentry;
197
198         /* create directory to store nid table versions */
199         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MGS_NIDTBL_DIR,
200                               0777, 1);
201         if (IS_ERR(dentry)) {
202                 rc = PTR_ERR(dentry);
203                 CERROR("cannot create %s directory: rc = %d\n",
204                        MOUNT_CONFIGS_DIR, rc);
205                 GOTO(err_pop, rc);
206         } else {
207                 dput(dentry);
208         }
209
210 err_pop:
211         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
212         return rc;
213 }
214
215 int mgs_fs_cleanup_old(const struct lu_env *env, struct mgs_device *mgs)
216 {
217         struct obd_device *obd = mgs->mgs_obd;
218         struct lvfs_run_ctxt saved;
219         int rc = 0;
220
221         class_disconnect_exports(obd); /* cleans up client info too */
222
223         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
224
225         if (mgs->mgs_configs_dir_old) {
226                 l_dput(mgs->mgs_configs_dir_old);
227                 mgs->mgs_configs_dir_old = NULL;
228         }
229
230         shrink_dcache_sb(mgs->mgs_sb);
231
232         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
233
234         if (obd->obd_fsops)
235                 fsfilt_put_ops(obd->obd_fsops);
236
237         return rc;
238 }
239
240 int mgs_fs_setup(const struct lu_env *env, struct mgs_device *mgs)
241 {
242         struct lu_fid     fid;
243         struct dt_object *o;
244         struct lu_fid     rfid;
245         struct dt_object *root;
246         int rc;
247         ENTRY;
248
249         /* FIXME what's this?  Do I need it? */
250         rc = cfs_cleanup_group_info();
251         if (rc)
252                 RETURN(rc);
253
254         /* XXX: fix when support for N:1 layering is implemented */
255         LASSERT(mgs->mgs_dt_dev.dd_lu_dev.ld_site);
256         mgs->mgs_dt_dev.dd_lu_dev.ld_site->ls_top_dev =
257                 &mgs->mgs_dt_dev.dd_lu_dev;
258
259         /* Setup the configs dir */
260         fid.f_seq = FID_SEQ_LOCAL_NAME;
261         fid.f_oid = 1;
262         fid.f_ver = 0;
263         rc = local_oid_storage_init(env, mgs->mgs_bottom, &fid, &mgs->mgs_los);
264         if (rc)
265                 GOTO(out, rc);
266
267         rc = dt_root_get(env, mgs->mgs_bottom, &rfid);
268         if (rc)
269                 GOTO(out_los, rc);
270
271         root = dt_locate_at(env, mgs->mgs_bottom, &rfid,
272                             &mgs->mgs_dt_dev.dd_lu_dev);
273         if (unlikely(IS_ERR(root)))
274                 GOTO(out_los, PTR_ERR(root));
275
276         o = local_file_find_or_create(env, mgs->mgs_los, root,
277                                       MOUNT_CONFIGS_DIR,
278                                       S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
279         if (IS_ERR(o))
280                 GOTO(out_root, rc = PTR_ERR(o));
281
282         mgs->mgs_configs_dir = o;
283
284         /* create directory to store nid table versions */
285         o = local_file_find_or_create(env, mgs->mgs_los, root, MGS_NIDTBL_DIR,
286                                       S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
287         if (IS_ERR(o)) {
288                 lu_object_put(env, &mgs->mgs_configs_dir->do_lu);
289                 mgs->mgs_configs_dir = NULL;
290                 GOTO(out_root, rc = PTR_ERR(o));
291         }
292
293         mgs->mgs_nidtbl_dir = o;
294
295 out_root:
296         lu_object_put(env, &root->do_lu);
297 out_los:
298         if (rc) {
299                 local_oid_storage_fini(env, mgs->mgs_los);
300                 mgs->mgs_los = NULL;
301         }
302 out:
303         mgs->mgs_dt_dev.dd_lu_dev.ld_site->ls_top_dev = NULL;
304
305         if (rc == 0) {
306                 rc = mgs_fs_setup_old(env, mgs);
307                 if (rc)
308                         mgs_fs_cleanup(env, mgs);
309         }
310
311         return rc;
312 }
313
314 int mgs_fs_cleanup(const struct lu_env *env, struct mgs_device *mgs)
315 {
316         mgs_fs_cleanup_old(env, mgs);
317
318         class_disconnect_exports(mgs->mgs_obd); /* cleans up client info too */
319
320         if (mgs->mgs_configs_dir) {
321                 lu_object_put(env, &mgs->mgs_configs_dir->do_lu);
322                 mgs->mgs_configs_dir = NULL;
323         }
324         if (mgs->mgs_nidtbl_dir) {
325                 lu_object_put(env, &mgs->mgs_nidtbl_dir->do_lu);
326                 mgs->mgs_nidtbl_dir = NULL;
327         }
328         if (mgs->mgs_los) {
329                 local_oid_storage_fini(env, mgs->mgs_los);
330                 mgs->mgs_los = NULL;
331         }
332
333         return 0;
334 }