Whamcloud - gitweb
Update copyrights on source files changed since 2010-02-15.
[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 /*
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 static int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp,
62                                  void *localdata)
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
77         if (newnid) {
78                 rc = lprocfs_nid_ldlm_stats_init(exp->exp_nid_stats);
79                 if (rc)
80                         GOTO(clean, rc);
81         }
82         RETURN(0);
83 clean:
84         lprocfs_exp_cleanup(exp);
85         return rc;
86 }
87
88 /**
89  * Add client export data to the MGS.  This data is currently NOT stored on
90  * disk in the last_rcvd file or anywhere else.  In the event of a MGS
91  * crash all connections are treated as new connections.
92  */
93 int mgs_client_add(struct obd_device *obd, struct obd_export *exp,
94                    void *localdata)
95 {
96         return mgs_export_stats_init(obd, exp, localdata);
97 }
98
99 /* Remove client export data from the MGS */
100 int mgs_client_free(struct obd_export *exp)
101 {
102         return 0;
103 }
104
105 /* Same as mds_fid2dentry */
106 /* Look up an entry by inode number. */
107 /* this function ONLY returns valid dget'd dentries with an initialized inode
108    or errors */
109 static struct dentry *mgs_fid2dentry(struct mgs_obd *mgs,
110                                      __u64 ino, __u32 gen)
111 {
112         char fid_name[32];
113         struct inode *inode;
114         struct dentry *result;
115         ENTRY;
116
117         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
118                (unsigned long)ino, gen, mgs->mgs_sb);
119
120         if (ino == 0)
121                 RETURN(ERR_PTR(-ESTALE));
122
123         snprintf(fid_name, sizeof(fid_name), "0x%lx", (unsigned long)ino);
124
125         /* under ext3 this is neither supposed to return bad inodes nor NULL
126            inodes. */
127         result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
128         if (IS_ERR(result))
129                 RETURN(result);
130
131         inode = result->d_inode;
132         if (!inode)
133                 RETURN(ERR_PTR(-ENOENT));
134
135         if (inode->i_generation == 0 || inode->i_nlink == 0) {
136                 LCONSOLE_WARN("Found inode with zero generation or link -- this"
137                               " may indicate disk corruption (inode: %lu, link:"
138                               " %lu, count: %d)\n", inode->i_ino,
139                               (unsigned long)inode->i_nlink,
140                               atomic_read(&inode->i_count));
141                 l_dput(result);
142                 RETURN(ERR_PTR(-ENOENT));
143         }
144
145         if (gen && inode->i_generation != gen) {
146                 /* we didn't find the right inode.. */
147                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
148                        "count: %d, generation %u/%u\n", inode->i_ino,
149                        (unsigned long)inode->i_nlink,
150                        atomic_read(&inode->i_count),
151                        inode->i_generation, gen);
152                 l_dput(result);
153                 RETURN(ERR_PTR(-ENOENT));
154         }
155
156         RETURN(result);
157 }
158
159 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen,
160                                           __u64 gr, void *data)
161 {
162         struct obd_device *obd = data;
163         return mgs_fid2dentry(&obd->u.mgs, id, gen);
164 }
165
166 struct lvfs_callback_ops mgs_lvfs_ops = {
167         l_fid2dentry:     mgs_lvfs_fid2dentry,
168 };
169
170 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
171 {
172         struct mgs_obd *mgs = &obd->u.mgs;
173         struct lvfs_run_ctxt saved;
174         struct dentry *dentry;
175         int rc;
176         ENTRY;
177
178         /* FIXME what's this?  Do I need it? */
179         rc = cfs_cleanup_group_info();
180         if (rc)
181                 RETURN(rc);
182
183         mgs->mgs_vfsmnt = mnt;
184         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
185
186         fsfilt_setup(obd, mgs->mgs_sb);
187
188         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
189         obd->obd_lvfs_ctxt.pwdmnt = mnt;
190         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
191         obd->obd_lvfs_ctxt.fs = get_ds();
192         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
193
194         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
195
196         /* Setup the configs dir */
197         dentry = simple_mkdir(current->fs->pwd, mnt, MOUNT_CONFIGS_DIR, 0777, 1);
198         if (IS_ERR(dentry)) {
199                 rc = PTR_ERR(dentry);
200                 CERROR("cannot create %s directory: rc = %d\n",
201                        MOUNT_CONFIGS_DIR, rc);
202                 GOTO(err_pop, rc);
203         }
204         mgs->mgs_configs_dir = dentry;
205
206         /* Need the iopen dir for fid2dentry, required by
207            LLOG_ORIGIN_HANDLE_READ_HEADER */
208         dentry = lookup_one_len("__iopen__", current->fs->pwd,
209                                 strlen("__iopen__"));
210         if (IS_ERR(dentry)) {
211                 rc = PTR_ERR(dentry);
212                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
213                 GOTO(err_configs, rc);
214         }
215         mgs->mgs_fid_de = dentry;
216         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
217                 rc = -ENOENT;
218                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
219                 GOTO(err_fid, rc);
220         }
221
222 err_pop:
223         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
224         return rc;
225 err_fid:
226         dput(mgs->mgs_fid_de);
227 err_configs:
228         dput(mgs->mgs_configs_dir);
229         goto err_pop;
230 }
231
232 int mgs_fs_cleanup(struct obd_device *obd)
233 {
234         struct mgs_obd *mgs = &obd->u.mgs;
235         struct lvfs_run_ctxt saved;
236         int rc = 0;
237
238         class_disconnect_exports(obd); /* cleans up client info too */
239
240         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
241
242         if (mgs->mgs_configs_dir) {
243                 l_dput(mgs->mgs_configs_dir);
244                 mgs->mgs_configs_dir = NULL;
245         }
246
247         dput(mgs->mgs_fid_de);
248         shrink_dcache_sb(mgs->mgs_sb);
249
250         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
251
252         return rc;
253 }