1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Management Server (MGS) filesystem interface code
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Nathan Rutman <nathan@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_MGS
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>
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"
46 static int mgs_export_stats_init(struct obd_device *obd, struct obd_export *exp)
48 int rc, num_stats, newnid = 0;
50 rc = lprocfs_exp_setup(exp, NULL, &newnid);
55 num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
57 exp->exp_ops_stats = lprocfs_alloc_stats(num_stats,
58 LPROCFS_STATS_FLAG_NOPERCPU);
59 if (exp->exp_ops_stats == NULL)
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);
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.
72 int mgs_client_add(struct obd_device *obd, struct obd_export *exp)
74 return mgs_export_stats_init(obd, exp);
77 /* Remove client export data from the MGS */
78 int mgs_client_free(struct obd_export *exp)
80 return lprocfs_exp_cleanup(exp);
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
87 static struct dentry *mgs_fid2dentry(struct mgs_obd *mgs, struct ll_fid *fid)
90 unsigned long ino = fid->id;
91 __u32 generation = fid->generation;
93 struct dentry *result;
95 CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
96 ino, generation, mgs->mgs_sb);
99 RETURN(ERR_PTR(-ESTALE));
101 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
103 /* under ext3 this is neither supposed to return bad inodes
105 result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
109 inode = result->d_inode;
111 RETURN(ERR_PTR(-ENOENT));
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));
120 RETURN(ERR_PTR(-ENOENT));
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,
131 RETURN(ERR_PTR(-ENOENT));
137 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
140 struct obd_device *obd = data;
143 fid.generation = gen;
144 return mgs_fid2dentry(&obd->u.mgs, &fid);
147 struct lvfs_callback_ops mgs_lvfs_ops = {
148 l_fid2dentry: mgs_lvfs_fid2dentry,
151 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
153 struct mgs_obd *mgs = &obd->u.mgs;
154 struct lvfs_run_ctxt saved;
155 struct dentry *dentry;
159 /* FIXME what's this? Do I need it? */
160 rc = cleanup_group_info();
164 mgs->mgs_vfsmnt = mnt;
165 mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
167 rc = fsfilt_setup(obd, mgs->mgs_sb);
169 CWARN("fail to set fsfilter options\n");
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;
177 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
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);
187 mgs->mgs_configs_dir = dentry;
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);
198 mgs->mgs_fid_de = dentry;
199 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
201 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
206 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
209 dput(mgs->mgs_fid_de);
211 dput(mgs->mgs_configs_dir);
215 int mgs_fs_cleanup(struct obd_device *obd)
217 struct mgs_obd *mgs = &obd->u.mgs;
218 struct lvfs_run_ctxt saved;
221 class_disconnect_exports(obd); /* cleans up client info too */
223 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
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;
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);
237 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);