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