Whamcloud - gitweb
945596336ba8a2a0592f154e59481f8c6f53deba
[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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MGS
47
48 #include <linux/module.h>
49 #include <linux/kmod.h>
50 #include <linux/version.h>
51 #include <linux/sched.h>
52 #include <linux/mount.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_disk.h>
56 #include <lustre_lib.h>
57 #include <lustre_fsfilt.h>
58 #include <libcfs/list.h>
59 #include "mgs_internal.h"
60
61
62 int mgs_export_stats_init(struct obd_device *obd,
63                                  struct obd_export *exp,
64                                  int reconnect,
65                                  void *localdata)
66 {
67         lnet_nid_t *client_nid = localdata;
68         int rc, num_stats, newnid = 0;
69
70         rc = lprocfs_exp_setup(exp, client_nid, reconnect, &newnid);
71         if (rc) {
72                 /* Mask error for already created
73                  * /proc entries */
74                 if (rc == -EALREADY)
75                         rc = 0;
76                 return rc;
77         }
78
79         if (newnid) {
80                 struct nid_stat *tmp = exp->exp_nid_stats;
81
82                 num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
83                              LPROC_MGS_LAST - 1;
84                 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
85                                                 LPROCFS_STATS_FLAG_NOPERCPU);
86                 if (tmp->nid_stats == NULL)
87                         return -ENOMEM;
88
89                 lprocfs_init_ops_stats(LPROC_MGS_LAST, tmp->nid_stats);
90                 mgs_stats_counter_init(tmp->nid_stats);
91
92                 rc = lprocfs_nid_ldlm_stats_init(tmp);
93                 if (rc)
94                         return rc;
95         }
96
97         return 0;
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,
105                    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_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_fid2dentry(struct mgs_obd *mgs, struct ll_fid *fid)
122 {
123         char fid_name[32];
124         unsigned long ino = fid->id;
125         __u32 generation = fid->generation;
126         struct inode *inode;
127         struct dentry *result;
128
129         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
130                ino, generation, mgs->mgs_sb);
131
132         if (ino == 0)
133                 RETURN(ERR_PTR(-ESTALE));
134
135         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
136
137         /* under ext3 this is neither supposed to return bad inodes
138            nor NULL inodes. */
139         result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
140         if (IS_ERR(result))
141                 RETURN(result);
142
143         inode = result->d_inode;
144         if (!inode)
145                 RETURN(ERR_PTR(-ENOENT));
146
147         if (inode->i_generation == 0 || inode->i_nlink == 0) {
148                 LCONSOLE_WARN("Found inode with zero generation or link -- this"
149                               " may indicate disk corruption (inode: %lu, link:"
150                               " %lu, count: %d)\n", inode->i_ino,
151                               (unsigned long)inode->i_nlink,
152                               atomic_read(&inode->i_count));
153                 l_dput(result);
154                 RETURN(ERR_PTR(-ENOENT));
155         }
156
157         if (generation && inode->i_generation != generation) {
158                 /* we didn't find the right inode.. */
159                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
160                        "count: %d, generation %u/%u\n", inode->i_ino,
161                        (unsigned long)inode->i_nlink,
162                        atomic_read(&inode->i_count), inode->i_generation,
163                        generation);
164                 l_dput(result);
165                 RETURN(ERR_PTR(-ENOENT));
166         }
167
168         RETURN(result);
169 }
170
171 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
172                                           void *data)
173 {
174         struct obd_device *obd = data;
175         struct ll_fid fid;
176         fid.id = id;
177         fid.generation = gen;
178         return mgs_fid2dentry(&obd->u.mgs, &fid);
179 }
180
181 struct lvfs_callback_ops mgs_lvfs_ops = {
182         l_fid2dentry:     mgs_lvfs_fid2dentry,
183 };
184
185 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
186 {
187         struct mgs_obd *mgs = &obd->u.mgs;
188         struct lvfs_run_ctxt saved;
189         struct dentry *dentry;
190         int rc;
191         ENTRY;
192
193         /* FIXME what's this?  Do I need it? */
194         rc = cleanup_group_info();
195         if (rc)
196                 RETURN(rc);
197
198         mgs->mgs_vfsmnt = mnt;
199         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
200
201         rc = fsfilt_setup(obd, mgs->mgs_sb);
202         if (rc)
203                 CWARN("fail to set fsfilter options\n");
204
205         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
206         obd->obd_lvfs_ctxt.pwdmnt = mnt;
207         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
208         obd->obd_lvfs_ctxt.fs = get_ds();
209         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
210
211         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
212
213         /* Setup the configs dir */
214         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, MOUNT_CONFIGS_DIR, 0777, 1);
215         if (IS_ERR(dentry)) {
216                 rc = PTR_ERR(dentry);
217                 CERROR("cannot create %s directory: rc = %d\n",
218                        MOUNT_CONFIGS_DIR, rc);
219                 GOTO(err_pop, rc);
220         }
221         mgs->mgs_configs_dir = dentry;
222
223         /* Need the iopen dir for fid2dentry, required by
224            LLOG_ORIGIN_HANDLE_READ_HEADER */
225         dentry = lookup_one_len("__iopen__", cfs_fs_pwd(current->fs),
226                                 strlen("__iopen__"));
227         if (IS_ERR(dentry)) {
228                 rc = PTR_ERR(dentry);
229                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
230                 GOTO(err_configs, rc);
231         }
232         mgs->mgs_fid_de = dentry;
233         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
234                 rc = -ENOENT;
235                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
236                 GOTO(err_fid, rc);
237         }
238
239 err_pop:
240         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
241         return rc;
242 err_fid:
243         dput(mgs->mgs_fid_de);
244 err_configs:
245         dput(mgs->mgs_configs_dir);
246         goto err_pop;
247 }
248
249 int mgs_fs_cleanup(struct obd_device *obd)
250 {
251         struct mgs_obd *mgs = &obd->u.mgs;
252         struct lvfs_run_ctxt saved;
253         int rc = 0;
254
255         class_disconnect_exports(obd); /* cleans up client info too */
256
257         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
258
259         if (mgs->mgs_configs_dir) {
260                 l_dput(mgs->mgs_configs_dir);
261                 mgs->mgs_configs_dir = NULL;
262         }
263
264         dput(mgs->mgs_fid_de);
265         shrink_dcache_sb(mgs->mgs_sb);
266
267         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
268
269         return rc;
270 }