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