Whamcloud - gitweb
78b66753e61e137f03eedc80b618620fa816eca8
[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  2008 Sun Microsystems, Inc. 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 /* Same as mds_fid2dentry */
62 /* Look up an entry by inode number. */
63 /* this function ONLY returns valid dget'd dentries with an initialized inode
64    or errors */
65 static struct dentry *mgs_fid2dentry(struct mgs_obd *mgs,
66                                      __u64 ino, __u32 gen)
67 {
68         char fid_name[32];
69         struct inode *inode;
70         struct dentry *result;
71         ENTRY;
72
73         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
74                (unsigned long)ino, gen, mgs->mgs_sb);
75
76         if (ino == 0)
77                 RETURN(ERR_PTR(-ESTALE));
78         
79         snprintf(fid_name, sizeof(fid_name), "0x%lx", (unsigned long)ino);
80         
81         /* under ext3 this is neither supposed to return bad inodes nor NULL
82            inodes. */
83         result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
84         if (IS_ERR(result))
85                 RETURN(result);
86
87         inode = result->d_inode;
88         if (!inode)
89                 RETURN(ERR_PTR(-ENOENT));
90
91         if (inode->i_generation == 0 || inode->i_nlink == 0) {
92                 LCONSOLE_WARN("Found inode with zero generation or link -- this"
93                               " may indicate disk corruption (inode: %lu, link:"
94                               " %lu, count: %d)\n", inode->i_ino,
95                               (unsigned long)inode->i_nlink,
96                               atomic_read(&inode->i_count));
97                 l_dput(result);
98                 RETURN(ERR_PTR(-ENOENT));
99         }
100
101         if (gen && inode->i_generation != gen) {
102                 /* we didn't find the right inode.. */
103                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
104                        "count: %d, generation %u/%u\n", inode->i_ino,
105                        (unsigned long)inode->i_nlink, atomic_read(&inode->i_count),
106                        inode->i_generation, gen);
107                 l_dput(result);
108                 RETURN(ERR_PTR(-ENOENT));
109         }
110
111         RETURN(result);
112 }
113
114 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen,
115                                           __u64 gr, void *data)
116 {
117         struct obd_device *obd = data;
118         return mgs_fid2dentry(&obd->u.mgs, id, gen);
119 }
120
121 struct lvfs_callback_ops mgs_lvfs_ops = {
122         l_fid2dentry:     mgs_lvfs_fid2dentry,
123 };
124
125 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
126 {
127         struct mgs_obd *mgs = &obd->u.mgs;
128         struct lvfs_run_ctxt saved;
129         struct dentry *dentry;
130         int rc;
131         ENTRY;
132
133         /* FIXME what's this?  Do I need it? */
134         rc = cleanup_group_info();
135         if (rc)
136                 RETURN(rc);
137
138         mgs->mgs_vfsmnt = mnt;
139         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
140
141         fsfilt_setup(obd, mgs->mgs_sb);
142
143         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
144         obd->obd_lvfs_ctxt.pwdmnt = mnt;
145         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
146         obd->obd_lvfs_ctxt.fs = get_ds();
147         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
148
149         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
150
151         /* Setup the configs dir */
152         dentry = simple_mkdir(current->fs->pwd, mnt, MOUNT_CONFIGS_DIR, 0777, 1);
153         if (IS_ERR(dentry)) {
154                 rc = PTR_ERR(dentry);
155                 CERROR("cannot create %s directory: rc = %d\n", 
156                        MOUNT_CONFIGS_DIR, rc);
157                 GOTO(err_pop, rc);
158         }
159         mgs->mgs_configs_dir = dentry;
160
161         /* Need the iopen dir for fid2dentry, required by
162            LLOG_ORIGIN_HANDLE_READ_HEADER */
163         dentry = lookup_one_len("__iopen__", current->fs->pwd,
164                                 strlen("__iopen__"));
165         if (IS_ERR(dentry)) {
166                 rc = PTR_ERR(dentry);
167                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
168                 GOTO(err_configs, rc);
169         }
170         mgs->mgs_fid_de = dentry;
171         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
172                 rc = -ENOENT;
173                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
174                 GOTO(err_fid, rc);
175         }
176
177 err_pop:
178         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
179         return rc;
180 err_fid:
181         dput(mgs->mgs_fid_de);
182 err_configs:
183         dput(mgs->mgs_configs_dir);
184         goto err_pop;
185 }
186
187 int mgs_fs_cleanup(struct obd_device *obd)
188 {
189         struct mgs_obd *mgs = &obd->u.mgs;
190         struct lvfs_run_ctxt saved;
191         int rc = 0;
192
193         class_disconnect_exports(obd); /* cleans up client info too */
194
195         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
196
197         if (mgs->mgs_configs_dir) {
198                 /*CERROR("configs dir dcount=%d\n",
199                        atomic_read(&mgs->mgs_configs_dir->d_count));*/
200                 l_dput(mgs->mgs_configs_dir);
201                 mgs->mgs_configs_dir = NULL;
202         }
203
204         shrink_dcache_parent(mgs->mgs_fid_de);
205         /*CERROR("fid dir dcount=%d\n",
206                atomic_read(&mgs->mgs_fid_de->d_count));*/
207         dput(mgs->mgs_fid_de);
208
209         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
210
211         return rc;
212 }