1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Lustre Metadata Server (MDS) filesystem interface code
8 * Copyright (C) 2002 Cluster File Systems, Inc.
10 * This code is issued under the GNU General Public License.
11 * See the file COPYING in this distribution
13 * by Andreas Dilger <adilger@clusterfs.com>
18 #define DEBUG_SUBSYSTEM S_MDS
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/lustre_mds.h>
23 #include <linux/obd_class.h>
24 #include <linux/obd_support.h>
25 #include <linux/lustre_lib.h>
27 LIST_HEAD(mds_fs_types);
30 struct list_head mft_list;
31 struct mds_fs_operations *mft_ops;
35 #define MDS_MAX_CLIENTS 1024
36 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
38 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
40 /* Add client data to the MDS. We use a bitmap to locate a free space
41 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
42 * Otherwise, we have just read the data from the last_rcvd file and
45 int mds_client_add(struct mds_export_data *med, int cl_off)
52 word = last_rcvd_slots;
55 if (word - last_rcvd_slots >= MDS_MAX_CLIENT_WORDS) {
56 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
60 if (test_and_set_bit(bit, word)) {
61 CERROR("found bit %d set for word %d - fix locking\n",
62 bit, word - last_rcvd_slots);
66 cl_off = word - last_rcvd_slots + bit;
68 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
69 CERROR("bit %d already set in bitmap - bad bad\n",
75 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
76 cl_off, med->med_mcd->mcd_uuid);
78 med->med_off = cl_off;
82 int mds_client_free(struct mds_export_data *med)
87 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
88 med->med_off, med->med_mcd->mcd_uuid);
90 word = last_rcvd_slots + med->med_off / sizeof(unsigned long);
91 bit = med->med_off % sizeof(unsigned long);
93 if (!test_and_clear_bit(bit, word)) {
94 CERROR("bit %d already clear in word %d - bad bad\n",
95 bit, word - last_rcvd_slots);
99 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
104 static int mds_server_free_data(struct mds_obd *mds)
106 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
107 mds->mds_server_data = NULL;
112 #define LAST_RCVD "last_rcvd"
114 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
116 struct mds_obd *mds = &obddev->u.mds;
117 struct mds_server_data *msd;
118 struct mds_client_data *mcd = NULL;
119 loff_t fsize = f->f_dentry->d_inode->i_size;
127 OBD_ALLOC(msd, sizeof(*msd));
130 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
132 mds->mds_server_data = msd;
134 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
138 if (rc != sizeof(*msd)) {
139 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
147 * When we do a clean MDS shutdown, we save the last_rcvd into
148 * the header. If we find clients with higher last_rcvd values
149 * then those clients may need recovery done.
151 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
152 mds->mds_last_rcvd = last_rcvd;
153 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
154 (unsigned long long)last_rcvd);
156 last_mount = le64_to_cpu(msd->msd_mount_count);
157 mds->mds_mount_count = last_mount;
158 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
159 (unsigned long long)last_mount);
161 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
162 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
163 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
165 OBD_ALLOC(mcd, sizeof(*mcd));
167 GOTO(err_msd, rc = -ENOMEM);
170 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
171 if (rc != sizeof(*mcd)) {
172 CERROR("error reading MDS %s offset %d: rc = %d\n",
173 LAST_RCVD, cl_off, rc);
179 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
181 /* Do client recovery here (open files, etc) */
182 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
183 < MDS_MOUNT_RECOV)) {
184 struct obd_export *export = class_new_export(obddev);
189 export->exp_mds_data.med_mcd = mcd;
190 mds_client_add(&export->exp_mds_data, cl_off);
195 "ignored client %d, UUID '%s', last_mount %Ld\n",
196 cl_off, mcd->mcd_uuid,
197 (long long)le64_to_cpu(mcd->mcd_mount_count));
200 if (last_rcvd > mds->mds_last_rcvd) {
202 "client at offset %d has last_rcvd = %Lu\n",
203 cl_off, (unsigned long long)last_rcvd);
204 mds->mds_last_rcvd = last_rcvd;
207 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
208 (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
211 OBD_FREE(mcd, sizeof(*mcd));
213 /* After recovery, there can be no local uncommitted transactions */
214 mds->mds_last_committed = mds->mds_last_rcvd;
219 mds_server_free_data(mds);
223 static int mds_fs_prep(struct obd_device *obddev)
225 struct mds_obd *mds = &obddev->u.mds;
226 struct obd_run_ctxt saved;
227 struct dentry *dentry;
231 push_ctxt(&saved, &mds->mds_ctxt);
232 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
233 if (IS_ERR(dentry)) {
234 rc = PTR_ERR(dentry);
235 CERROR("cannot create ROOT directory: rc = %d\n", rc);
239 mds->mds_rootfid.id = dentry->d_inode->i_ino;
240 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
241 mds->mds_rootfid.f_type = S_IFDIR;
245 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
246 if (IS_ERR(dentry)) {
247 rc = PTR_ERR(dentry);
248 CERROR("cannot create FH directory: rc = %d\n", rc);
251 /* XXX probably want to hold on to this later... */
254 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
257 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
258 GOTO(err_pop, rc = PTR_ERR(f));
260 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
261 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
262 f->f_dentry->d_inode->i_mode);
263 GOTO(err_pop, rc = -ENOENT);
266 rc = mds_fs_journal_data(mds, f);
268 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
272 rc = mds_read_last_rcvd(obddev, f);
274 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
275 GOTO(err_client, rc);
277 mds->mds_rcvd_filp = f;
284 class_disconnect_all(obddev);
286 if (filp_close(f, 0))
287 CERROR("can't close %s after error\n", LAST_RCVD);
291 static struct mds_fs_operations *mds_search_fs_type(const char *name)
294 struct mds_fs_type *type;
296 /* lock mds_fs_types list */
297 list_for_each(p, &mds_fs_types) {
298 type = list_entry(p, struct mds_fs_type, mft_list);
299 if (!strcmp(type->mft_name, name)) {
300 /* unlock mds_fs_types list */
301 return type->mft_ops;
304 /* unlock mds_fs_types list */
308 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
310 struct mds_fs_operations *found;
311 struct mds_fs_type *type;
313 if ((found = mds_search_fs_type(name))) {
315 CERROR("different operations for type %s\n", name);
320 OBD_ALLOC(type, sizeof(*type));
324 INIT_LIST_HEAD(&type->mft_list);
326 type->mft_name = strdup(name);
327 if (!type->mft_name) {
328 OBD_FREE(type, sizeof(*type));
332 list_add(&type->mft_list, &mds_fs_types);
337 void mds_unregister_fs_type(const char *name)
341 /* lock mds_fs_types list */
342 list_for_each(p, &mds_fs_types) {
343 struct mds_fs_type *type;
345 type = list_entry(p, struct mds_fs_type, mft_list);
346 if (!strcmp(type->mft_name, name)) {
348 kfree(type->mft_name);
349 OBD_FREE(type, sizeof(*type));
354 /* unlock mds_fs_types list */
357 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
359 struct mds_fs_operations *fs_ops;
361 if (!(fs_ops = mds_search_fs_type(fstype))) {
365 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
366 name[sizeof(name) - 1] = '\0';
368 if ((rc = request_module(name))) {
369 fs_ops = mds_search_fs_type(fstype);
370 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
376 CERROR("Can't find MDS fs interface '%s'\n", name);
380 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
385 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
387 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
390 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
392 struct mds_obd *mds = &obddev->u.mds;
395 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
396 if (IS_ERR(mds->mds_fsops))
397 RETURN(PTR_ERR(mds->mds_fsops));
399 mds->mds_vfsmnt = mnt;
401 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
402 mds->mds_ctxt.pwdmnt = mnt;
403 mds->mds_ctxt.pwd = mnt->mnt_root;
404 mds->mds_ctxt.fs = get_ds();
407 * Replace the client filesystem delete_inode method with our own,
408 * so that we can clear the object ID before the inode is deleted.
409 * The fs_delete_inode method will call cl_delete_inode for us.
410 * We need to do this for the MDS superblock only, hence we install
411 * a modified copy of the original superblock method table.
413 * We still assume that there is only a single MDS client filesystem
414 * type, as we don't have access to the mds struct in delete_inode
415 * and store the client delete_inode method in a global table. This
416 * will only become a problem if/when multiple MDSs are running on a
417 * single host with different underlying filesystems.
419 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
421 GOTO(out_dec, rc = -ENOMEM);
423 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
424 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
425 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
426 mds->mds_sb->s_op = mds->mds_sop;
428 rc = mds_fs_prep(obddev);
436 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
438 mds_fs_put_ops(mds->mds_fsops);
442 void mds_fs_cleanup(struct obd_device *obddev)
444 struct mds_obd *mds = &obddev->u.mds;
446 class_disconnect_all(obddev); /* this cleans up client info too */
447 mds_server_free_data(mds);
449 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
450 mds_fs_put_ops(mds->mds_fsops);
453 EXPORT_SYMBOL(mds_register_fs_type);
454 EXPORT_SYMBOL(mds_unregister_fs_type);