Whamcloud - gitweb
land b1_5 onto HEAD
[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  *  lustre/mgs/mgs_fs.c
5  *  Lustre Management Server (MGS) filesystem interface code
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Nathan Rutman <nathan@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MGS
29
30 #include <linux/module.h>
31 #include <linux/kmod.h>
32 #include <linux/version.h>
33 #include <linux/sched.h>
34 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
35 #include <linux/mount.h>
36 #endif
37 #include <obd_class.h>
38 #include <obd_support.h>
39 #include <lustre_disk.h>
40 #include <lustre_lib.h>
41 #include <lustre_fsfilt.h>
42 #include <libcfs/list.h>
43 #include "mgs_internal.h"
44
45 /* Same as mds_fid2dentry */
46 /* Look up an entry by inode number. */
47 /* this function ONLY returns valid dget'd dentries with an initialized inode
48    or errors */
49 static struct dentry *mgs_fid2dentry(struct mgs_obd *mgs, struct ll_fid *fid)
50 {
51         char fid_name[32];
52         unsigned long ino = fid->id;
53         __u32 generation = fid->generation;
54         struct inode *inode;
55         struct dentry *result;
56
57         CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
58                ino, generation, mgs->mgs_sb);
59
60         if (ino == 0)
61                 RETURN(ERR_PTR(-ESTALE));
62         
63         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
64         
65         /* under ext3 this is neither supposed to return bad inodes
66            nor NULL inodes. */
67         result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
68         if (IS_ERR(result))
69                 RETURN(result);
70
71         inode = result->d_inode;
72         if (!inode)
73                 RETURN(ERR_PTR(-ENOENT));
74
75         if (inode->i_generation == 0 || inode->i_nlink == 0) {
76                 LCONSOLE_WARN("Found inode with zero generation or link -- this"
77                               " may indicate disk corruption (inode: %lu, link:"
78                               " %lu, count: %d)\n", inode->i_ino,
79                               (unsigned long)inode->i_nlink,
80                               atomic_read(&inode->i_count));
81                 l_dput(result);
82                 RETURN(ERR_PTR(-ENOENT));
83         }
84
85         if (generation && inode->i_generation != generation) {
86                 /* we didn't find the right inode.. */
87                 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
88                        "count: %d, generation %u/%u\n", inode->i_ino,
89                        (unsigned long)inode->i_nlink,
90                        atomic_read(&inode->i_count), inode->i_generation,
91                        generation);
92                 l_dput(result);
93                 RETURN(ERR_PTR(-ENOENT));
94         }
95
96         RETURN(result);
97 }
98
99 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
100                                           void *data)
101 {
102         struct obd_device *obd = data;
103         struct ll_fid fid;
104         fid.id = id;
105         fid.generation = gen;
106         return mgs_fid2dentry(&obd->u.mgs, &fid);
107 }
108
109 struct lvfs_callback_ops mgs_lvfs_ops = {
110         l_fid2dentry:     mgs_lvfs_fid2dentry,
111 };
112
113 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
114 {
115         struct mgs_obd *mgs = &obd->u.mgs;
116         struct lvfs_run_ctxt saved;
117         struct dentry *dentry;
118         int rc;
119         ENTRY;
120
121         /* FIXME what's this?  Do I need it? */
122         rc = cleanup_group_info();
123         if (rc)
124                 RETURN(rc);
125
126         mgs->mgs_vfsmnt = mnt;
127         mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
128
129         fsfilt_setup(obd, mgs->mgs_sb);
130
131         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
132         obd->obd_lvfs_ctxt.pwdmnt = mnt;
133         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
134         obd->obd_lvfs_ctxt.fs = get_ds();
135         obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
136
137         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
138
139         /* Setup the configs dir */
140         dentry = simple_mkdir(current->fs->pwd, MOUNT_CONFIGS_DIR, 0777, 1);
141         if (IS_ERR(dentry)) {
142                 rc = PTR_ERR(dentry);
143                 CERROR("cannot create %s directory: rc = %d\n", 
144                        MOUNT_CONFIGS_DIR, rc);
145                 GOTO(err_pop, rc);
146         }
147         mgs->mgs_configs_dir = dentry;
148
149         /* Need the iopen dir for fid2dentry, required by
150            LLOG_ORIGIN_HANDLE_READ_HEADER */
151         dentry = lookup_one_len("__iopen__", current->fs->pwd,
152                                 strlen("__iopen__"));
153         if (IS_ERR(dentry)) {
154                 rc = PTR_ERR(dentry);
155                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
156                 GOTO(err_configs, rc);
157         }
158         mgs->mgs_fid_de = dentry;
159         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
160                 rc = -ENOENT;
161                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
162                 GOTO(err_fid, rc);
163         }
164
165 err_pop:
166         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
167         return rc;
168 err_fid:
169         dput(mgs->mgs_fid_de);
170 err_configs:
171         dput(mgs->mgs_configs_dir);
172         goto err_pop;
173 }
174
175 int mgs_fs_cleanup(struct obd_device *obd)
176 {
177         struct mgs_obd *mgs = &obd->u.mgs;
178         struct lvfs_run_ctxt saved;
179         int rc = 0;
180
181         class_disconnect_exports(obd); /* cleans up client info too */
182
183         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
184
185         if (mgs->mgs_configs_dir) {
186                 /*CERROR("configs dir dcount=%d\n",
187                        atomic_read(&mgs->mgs_configs_dir->d_count));*/
188                 l_dput(mgs->mgs_configs_dir);
189                 mgs->mgs_configs_dir = NULL;
190         }
191
192         shrink_dcache_parent(mgs->mgs_fid_de);
193         /*CERROR("fid dir dcount=%d\n",
194                atomic_read(&mgs->mgs_fid_de->d_count));*/
195         dput(mgs->mgs_fid_de);
196
197         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
198
199         return rc;
200 }