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