Whamcloud - gitweb
3b57c671d87ad9cd6924fd12d84c971763c101e1
[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 "mgs_internal.h"
58
59 int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
60                           void *localdata)
61
62 {
63         lnet_nid_t *client_nid = localdata;
64         int rc, newnid;
65         ENTRY;
66
67         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
68         if (rc) {
69                 /* Mask error for already created
70                  * /proc entries */
71                 if (rc == -EALREADY)
72                         rc = 0;
73                 RETURN(rc);
74         }
75         if (newnid) {
76                 struct nid_stat *tmp = exp->exp_nid_stats;
77                 int num_stats = 0;
78
79                 num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
80                             LPROC_MGS_LAST - 1;
81                 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
82                                                      LPROCFS_STATS_FLAG_NOPERCPU);
83                 if (tmp->nid_stats == NULL)
84                         return -ENOMEM;
85                 lprocfs_init_ops_stats(LPROC_MGS_LAST, tmp->nid_stats);
86                 mgs_stats_counter_init(tmp->nid_stats);
87                 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
88                                             tmp->nid_stats);
89                 if (rc)
90                         GOTO(clean, rc);
91
92                 rc = lprocfs_nid_ldlm_stats_init(tmp);
93                 if (rc)
94                         GOTO(clean, rc);
95         }
96         RETURN(0);
97 clean:
98         return rc;
99 }
100
101 /**
102  * Add client export data to the MGS.  This data is currently NOT stored on
103  * disk in the last_rcvd file or anywhere else.  In the event of a MGS
104  * crash all connections are treated as new connections.
105  */
106 int mgs_client_add(struct obd_device *obd, struct obd_export *exp,
107                    void *localdata)
108 {
109         return 0;
110 }
111
112 /* Remove client export data from the MGS */
113 int mgs_client_free(struct obd_export *exp)
114 {
115         return 0;
116 }
117
118 /* Same as mds_lvfs_fid2dentry */
119 /* Look up an entry by inode number. */
120 /* this function ONLY returns valid dget'd dentries with an initialized inode
121    or errors */
122 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen,
123                                           __u64 gr, void *data)
124 {
125         struct fsfilt_fid  fid;
126         struct obd_device *obd = (struct obd_device *)data;
127         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
128         ENTRY;
129
130         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
131                (unsigned long)id, gen, mgs->mgs_sb);
132
133         if (id == 0)
134                 RETURN(ERR_PTR(-ESTALE));
135
136         fid.ino = id;
137         fid.gen = gen;
138
139         RETURN(fsfilt_fid2dentry(obd, mgs->mgs_vfsmnt, &fid, 0));
140 }
141
142 struct lvfs_callback_ops mgs_lvfs_ops = {
143         l_fid2dentry:     mgs_lvfs_fid2dentry,
144 };
145
146 int mgs_fs_setup(const struct lu_env *env, struct mgs_device *mgs)
147 {
148         struct obd_device *obd = mgs->mgs_obd;
149         struct dt_device_param p;
150         struct vfsmount *mnt;
151         struct lvfs_run_ctxt saved;
152         struct dentry *dentry;
153         int rc;
154         ENTRY;
155
156         dt_conf_get(env, mgs->mgs_bottom, &p);
157         mnt = p.ddp_mnt;
158         if (mnt == NULL) {
159                 CERROR("%s: no proper support for OSD yet\n", obd->obd_name);
160                 RETURN(-ENODEV);
161         }
162
163         /* FIXME what's this?  Do I need it? */
164         rc = cfs_cleanup_group_info();
165         if (rc)
166                 RETURN(rc);
167
168         mgs->mgs_vfsmnt = mnt;
169         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
170
171         obd->obd_fsops = fsfilt_get_ops(mt_str(p.ddp_mount_type));
172         if (IS_ERR(obd->obd_fsops))
173                 RETURN(PTR_ERR(obd->obd_fsops));
174
175         rc = fsfilt_setup(obd, mgs->mgs_sb);
176         if (rc)
177                 RETURN(rc);
178
179         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
180         obd->obd_lvfs_ctxt.pwdmnt = mnt;
181         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
182         obd->obd_lvfs_ctxt.fs = get_ds();
183         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
184
185         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
186
187         /* Setup the configs dir */
188         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MOUNT_CONFIGS_DIR, 0777, 1);
189         if (IS_ERR(dentry)) {
190                 rc = PTR_ERR(dentry);
191                 CERROR("cannot create %s directory: rc = %d\n",
192                        MOUNT_CONFIGS_DIR, rc);
193                 GOTO(err_pop, rc);
194         }
195         mgs->mgs_configs_dir = dentry;
196
197         /* create directory to store nid table versions */
198         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MGS_NIDTBL_DIR,
199                               0777, 1);
200         if (IS_ERR(dentry)) {
201                 rc = PTR_ERR(dentry);
202                 CERROR("cannot create %s directory: rc = %d\n",
203                        MOUNT_CONFIGS_DIR, rc);
204                 GOTO(err_pop, rc);
205         } else {
206                 dput(dentry);
207         }
208
209 err_pop:
210         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
211         return rc;
212 }
213
214 int mgs_fs_cleanup(const struct lu_env *env, struct mgs_device *mgs)
215 {
216         struct obd_device *obd = mgs->mgs_obd;
217         struct lvfs_run_ctxt saved;
218         int rc = 0;
219
220         class_disconnect_exports(obd); /* cleans up client info too */
221
222         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
223
224         if (mgs->mgs_configs_dir) {
225                 l_dput(mgs->mgs_configs_dir);
226                 mgs->mgs_configs_dir = NULL;
227         }
228
229         shrink_dcache_sb(mgs->mgs_sb);
230
231         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
232
233         if (obd->obd_fsops)
234                 fsfilt_put_ops(obd->obd_fsops);
235
236         return rc;
237 }