Whamcloud - gitweb
f4140695a13f22657927f4c3620361817e24cb0c
[fs/lustre-release.git] / lustre / mgs / mgs_fs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mgs/mgs_fs.c
39  *
40  * Lustre Management Server (MGS) filesystem interface code
41  *
42  * Author: Nathan Rutman <nathan@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_MGS
49
50 #include <linux/module.h>
51 #include <linux/kmod.h>
52 #include <linux/version.h>
53 #include <linux/sched.h>
54 #include <linux/mount.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lustre_disk.h>
58 #include <lustre_lib.h>
59 #include <lustre_fsfilt.h>
60 #include <libcfs/list.h>
61 #include "mgs_internal.h"
62
63 int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
64                           void *localdata)
65
66 {
67         lnet_nid_t *client_nid = localdata;
68         int rc, newnid;
69         ENTRY;
70
71         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
72         if (rc) {
73                 /* Mask error for already created
74                  * /proc entries */
75                 if (rc == -EALREADY)
76                         rc = 0;
77                 RETURN(rc);
78         }
79         if (newnid) {
80                 struct nid_stat *tmp = exp->exp_nid_stats;
81                 int num_stats = 0;
82
83                 num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
84                             LPROC_MGS_LAST - 1;
85                 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
86                                                      LPROCFS_STATS_FLAG_NOPERCPU);
87                 if (tmp->nid_stats == NULL)
88                         return -ENOMEM;
89                 lprocfs_init_ops_stats(LPROC_MGS_LAST, tmp->nid_stats);
90                 mgs_stats_counter_init(tmp->nid_stats);
91                 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
92                                             tmp->nid_stats);
93                 if (rc)
94                         GOTO(clean, rc);
95
96                 rc = lprocfs_nid_ldlm_stats_init(tmp);
97                 if (rc)
98                         GOTO(clean, rc);
99         }
100         RETURN(0);
101 clean:
102         return rc;
103 }
104
105 /**
106  * Add client export data to the MGS.  This data is currently NOT stored on
107  * disk in the last_rcvd file or anywhere else.  In the event of a MGS
108  * crash all connections are treated as new connections.
109  */
110 int mgs_client_add(struct obd_device *obd, struct obd_export *exp,
111                    void *localdata)
112 {
113         return 0;
114 }
115
116 /* Remove client export data from the MGS */
117 int mgs_client_free(struct obd_export *exp)
118 {
119         return 0;
120 }
121
122 /* Same as mds_lvfs_fid2dentry */
123 /* Look up an entry by inode number. */
124 /* this function ONLY returns valid dget'd dentries with an initialized inode
125    or errors */
126 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen,
127                                           __u64 gr, void *data)
128 {
129         struct fsfilt_fid  fid;
130         struct obd_device *obd = (struct obd_device *)data;
131         ENTRY;
132
133         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
134                (unsigned long)id, gen, obd->u.mgs.mgs_sb);
135
136         if (id == 0)
137                 RETURN(ERR_PTR(-ESTALE));
138
139         fid.ino = id;
140         fid.gen = gen;
141
142         RETURN(fsfilt_fid2dentry(obd, obd->u.mgs.mgs_vfsmnt, &fid, 0));
143 }
144
145 struct lvfs_callback_ops mgs_lvfs_ops = {
146         l_fid2dentry:     mgs_lvfs_fid2dentry,
147 };
148
149 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
150 {
151         struct mgs_obd *mgs = &obd->u.mgs;
152         struct lvfs_run_ctxt saved;
153         struct dentry *dentry;
154         int rc;
155         ENTRY;
156
157         /* FIXME what's this?  Do I need it? */
158         rc = cfs_cleanup_group_info();
159         if (rc)
160                 RETURN(rc);
161
162         mgs->mgs_vfsmnt = mnt;
163         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
164
165         fsfilt_setup(obd, mgs->mgs_sb);
166
167         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
168         obd->obd_lvfs_ctxt.pwdmnt = mnt;
169         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
170         obd->obd_lvfs_ctxt.fs = get_ds();
171         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
172
173         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
174
175         /* Setup the configs dir */
176         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MOUNT_CONFIGS_DIR, 0777, 1);
177         if (IS_ERR(dentry)) {
178                 rc = PTR_ERR(dentry);
179                 CERROR("cannot create %s directory: rc = %d\n",
180                        MOUNT_CONFIGS_DIR, rc);
181                 GOTO(err_pop, rc);
182         }
183         mgs->mgs_configs_dir = dentry;
184
185         /* create directory to store nid table versions */
186         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MGS_NIDTBL_DIR,
187                               0777, 1);
188         if (IS_ERR(dentry)) {
189                 rc = PTR_ERR(dentry);
190                 CERROR("cannot create %s directory: rc = %d\n",
191                        MOUNT_CONFIGS_DIR, rc);
192                 GOTO(err_pop, rc);
193         } else {
194                 dput(dentry);
195         }
196
197 err_pop:
198         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
199         return rc;
200 }
201
202 int mgs_fs_cleanup(struct obd_device *obd)
203 {
204         struct mgs_obd *mgs = &obd->u.mgs;
205         struct lvfs_run_ctxt saved;
206         int rc = 0;
207
208         class_disconnect_exports(obd); /* cleans up client info too */
209
210         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
211
212         if (mgs->mgs_configs_dir) {
213                 l_dput(mgs->mgs_configs_dir);
214                 mgs->mgs_configs_dir = NULL;
215         }
216
217         shrink_dcache_sb(mgs->mgs_sb);
218
219         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
220
221         return rc;
222 }