1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Lustre Management Server (MGS) filesystem interface code
40 * Author: Nathan Rutman <nathan@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MGS
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"
62 static int mgs_export_stats_init(struct obd_device *obd,
63 struct obd_export *exp,
66 lnet_nid_t *client_nid = localdata;
67 int rc, num_stats, newnid = 0;
69 rc = lprocfs_exp_setup(exp, client_nid, &newnid);
71 /* Mask error for already created
79 num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
81 exp->exp_ops_stats = lprocfs_alloc_stats(num_stats,
82 LPROCFS_STATS_FLAG_NOPERCPU);
83 if (exp->exp_ops_stats == NULL)
85 lprocfs_init_ops_stats(LPROC_MGS_LAST, exp->exp_ops_stats);
86 mgs_stats_counter_init(exp->exp_ops_stats);
87 lprocfs_register_stats(exp->exp_nid_stats->nid_proc, "stats", exp->exp_ops_stats);
89 /* Always add in ldlm_stats */
90 exp->exp_nid_stats->nid_ldlm_stats = lprocfs_alloc_stats(LDLM_LAST_OPC -
92 if (exp->exp_nid_stats->nid_ldlm_stats == NULL)
95 lprocfs_init_ldlm_stats(exp->exp_nid_stats->nid_ldlm_stats);
97 rc = lprocfs_register_stats(exp->exp_nid_stats->nid_proc, "ldlm_stats",
98 exp->exp_nid_stats->nid_ldlm_stats);
104 /* Add client export data to the MGS. This data is currently NOT stored on
105 * disk in the last_rcvd file or anywhere else. In the event of a MGS
106 * crash all connections are treated as new connections.
108 int mgs_client_add(struct obd_device *obd,
109 struct obd_export *exp,
112 return mgs_export_stats_init(obd, exp, localdata);
115 /* Remove client export data from the MGS */
116 int mgs_client_free(struct obd_export *exp)
121 /* Same as mds_fid2dentry */
122 /* Look up an entry by inode number. */
123 /* this function ONLY returns valid dget'd dentries with an initialized inode
125 static struct dentry *mgs_fid2dentry(struct mgs_obd *mgs, struct ll_fid *fid)
128 unsigned long ino = fid->id;
129 __u32 generation = fid->generation;
131 struct dentry *result;
133 CDEBUG(D_DENTRY, "--> mgs_fid2dentry: ino/gen %lu/%u, sb %p\n",
134 ino, generation, mgs->mgs_sb);
137 RETURN(ERR_PTR(-ESTALE));
139 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
141 /* under ext3 this is neither supposed to return bad inodes
143 result = ll_lookup_one_len(fid_name, mgs->mgs_fid_de, strlen(fid_name));
147 inode = result->d_inode;
149 RETURN(ERR_PTR(-ENOENT));
151 if (inode->i_generation == 0 || inode->i_nlink == 0) {
152 LCONSOLE_WARN("Found inode with zero generation or link -- this"
153 " may indicate disk corruption (inode: %lu, link:"
154 " %lu, count: %d)\n", inode->i_ino,
155 (unsigned long)inode->i_nlink,
156 atomic_read(&inode->i_count));
158 RETURN(ERR_PTR(-ENOENT));
161 if (generation && inode->i_generation != generation) {
162 /* we didn't find the right inode.. */
163 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
164 "count: %d, generation %u/%u\n", inode->i_ino,
165 (unsigned long)inode->i_nlink,
166 atomic_read(&inode->i_count), inode->i_generation,
169 RETURN(ERR_PTR(-ENOENT));
175 static struct dentry *mgs_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
178 struct obd_device *obd = data;
181 fid.generation = gen;
182 return mgs_fid2dentry(&obd->u.mgs, &fid);
185 struct lvfs_callback_ops mgs_lvfs_ops = {
186 l_fid2dentry: mgs_lvfs_fid2dentry,
189 int mgs_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
191 struct mgs_obd *mgs = &obd->u.mgs;
192 struct lvfs_run_ctxt saved;
193 struct dentry *dentry;
197 /* FIXME what's this? Do I need it? */
198 rc = cleanup_group_info();
202 mgs->mgs_vfsmnt = mnt;
203 mgs->mgs_sb = mnt->mnt_root->d_inode->i_sb;
205 rc = fsfilt_setup(obd, mgs->mgs_sb);
207 CWARN("fail to set fsfilter options\n");
209 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
210 obd->obd_lvfs_ctxt.pwdmnt = mnt;
211 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
212 obd->obd_lvfs_ctxt.fs = get_ds();
213 obd->obd_lvfs_ctxt.cb_ops = mgs_lvfs_ops;
215 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
217 /* Setup the configs dir */
218 dentry = simple_mkdir(current->fs->pwd, mnt, MOUNT_CONFIGS_DIR, 0777, 1);
219 if (IS_ERR(dentry)) {
220 rc = PTR_ERR(dentry);
221 CERROR("cannot create %s directory: rc = %d\n",
222 MOUNT_CONFIGS_DIR, rc);
225 mgs->mgs_configs_dir = dentry;
227 /* Need the iopen dir for fid2dentry, required by
228 LLOG_ORIGIN_HANDLE_READ_HEADER */
229 dentry = lookup_one_len("__iopen__", current->fs->pwd,
230 strlen("__iopen__"));
231 if (IS_ERR(dentry)) {
232 rc = PTR_ERR(dentry);
233 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
234 GOTO(err_configs, rc);
236 mgs->mgs_fid_de = dentry;
237 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
239 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
244 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
247 dput(mgs->mgs_fid_de);
249 dput(mgs->mgs_configs_dir);
253 int mgs_fs_cleanup(struct obd_device *obd)
255 struct mgs_obd *mgs = &obd->u.mgs;
256 struct lvfs_run_ctxt saved;
259 class_disconnect_exports(obd); /* cleans up client info too */
261 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
263 if (mgs->mgs_configs_dir) {
264 /*CERROR("configs dir dcount=%d\n",
265 atomic_read(&mgs->mgs_configs_dir->d_count));*/
266 l_dput(mgs->mgs_configs_dir);
267 mgs->mgs_configs_dir = NULL;
270 shrink_dcache_parent(mgs->mgs_fid_de);
271 /*CERROR("fid dir dcount=%d\n",
272 atomic_read(&mgs->mgs_fid_de->d_count));*/
273 dput(mgs->mgs_fid_de);
275 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);