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_obd *mds, struct mds_export_data *med, int cl_off)
47 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
48 cl_off, med->med_mcd->mcd_uuid);
55 word = last_rcvd_slots;
58 if (word - last_rcvd_slots >= MDS_MAX_CLIENT_WORDS) {
59 CERROR("no room in client MDS bitmap - fix code\n");
63 if (test_and_set_bit(bit, word)) {
64 CERROR("found bit %d set for word %d - fix code\n",
65 bit, word - last_rcvd_slots);
68 cl_off = word - last_rcvd_slots + bit;
70 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
71 CERROR("bit %d already set in bitmap - bad bad\n",
77 med->med_off = cl_off;
78 mds->mds_client_count++;
83 static int mds_client_free_all(struct obd_device *obddev)
85 struct mds_obd *mds = &obddev->u.mds;
86 struct list_head *p, *n;
88 list_for_each_safe(p, n, &obddev->obd_exports) {
89 struct obd_export *exp;
90 struct mds_export_data *med;
94 exp = list_entry(p, struct obd_export, exp_chain);
95 med = &exp->exp_mds_data;
97 word = last_rcvd_slots + med->med_off / sizeof(unsigned long);
98 bit = med->med_off % sizeof(unsigned long);
100 if (!test_and_clear_bit(bit, word)) {
101 CERROR("bit %d already clear in word %d - bad bad\n",
102 bit, word - last_rcvd_slots);
106 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
107 --mds->mds_client_count;
110 if (mds->mds_client_count) {
111 CERROR("%d mds clients remaining after cleanup\n",
112 mds->mds_client_count);
119 static int mds_server_free_data(struct mds_obd *mds)
121 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
122 mds->mds_server_data = NULL;
127 #define LAST_RCVD "last_rcvd"
129 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
131 struct mds_obd *mds = &obddev->u.mds;
132 struct mds_server_data *msd;
133 struct mds_client_data *mcd = NULL;
134 loff_t fsize = f->f_dentry->d_inode->i_size;
141 OBD_ALLOC(msd, sizeof(*msd));
144 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
146 mds->mds_server_data = msd;
148 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
152 if (rc != sizeof(*msd)) {
153 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
161 * When we do a clean MDS shutdown, we save the last_rcvd into
162 * the header. If we find clients with higher last_rcvd values
163 * then those clients may need recovery done.
165 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
166 mds->mds_last_rcvd = last_rcvd;
167 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
168 (unsigned long long)last_rcvd);
170 last_mount = le64_to_cpu(msd->msd_mount_count);
171 mds->mds_mount_count = last_mount;
172 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
173 (unsigned long long)last_mount);
175 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
176 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
177 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
179 OBD_ALLOC(mcd, sizeof(*mcd));
181 GOTO(err_msd, rc = -ENOMEM);
183 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
184 if (rc != sizeof(*mcd)) {
185 CERROR("error reading MDS %s offset %d: rc = %d\n",
186 LAST_RCVD, cl_off, rc);
192 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
194 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
195 < MDS_MOUNT_RECOV)) {
196 struct obd_export *export = class_new_export(obddev);
201 export->exp_mds_data.med_mcd = mcd;
205 "client at offset %d with UUID '%s' ignored\n",
206 cl_off, mcd->mcd_uuid);
209 if (last_rcvd > mds->mds_last_rcvd) {
211 "client at offset %d has last_rcvd = %Lu\n",
212 cl_off, (unsigned long long)last_rcvd);
213 mds->mds_last_rcvd = last_rcvd;
216 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d clients\n",
217 (unsigned long long)mds->mds_last_rcvd, mds->mds_client_count);
220 OBD_FREE(mcd, sizeof(*mcd));
222 /* After recovery, there can be no local uncommitted transactions */
223 mds->mds_last_committed = mds->mds_last_rcvd;
228 mds_server_free_data(mds);
232 static int mds_fs_prep(struct obd_device *obddev)
234 struct mds_obd *mds = &obddev->u.mds;
235 struct obd_run_ctxt saved;
236 struct dentry *dentry;
240 push_ctxt(&saved, &mds->mds_ctxt);
241 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
242 if (IS_ERR(dentry)) {
243 rc = PTR_ERR(dentry);
244 CERROR("cannot create ROOT directory: rc = %d\n", rc);
247 /* XXX probably want to hold on to this later... */
249 f = filp_open("ROOT", O_RDONLY, 0);
252 CERROR("cannot open ROOT: rc = %d\n", rc);
257 mds->mds_rootfid.id = f->f_dentry->d_inode->i_ino;
258 mds->mds_rootfid.generation = f->f_dentry->d_inode->i_generation;
259 mds->mds_rootfid.f_type = S_IFDIR;
261 rc = filp_close(f, 0);
263 CERROR("cannot close ROOT: rc = %d\n", rc);
267 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
268 if (IS_ERR(dentry)) {
269 rc = PTR_ERR(dentry);
270 CERROR("cannot create FH directory: rc = %d\n", rc);
273 /* XXX probably want to hold on to this later... */
276 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
279 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
280 GOTO(err_pop, rc = PTR_ERR(f));
282 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
283 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
284 f->f_dentry->d_inode->i_mode);
285 GOTO(err_pop, rc = -ENOENT);
288 rc = mds_fs_journal_data(mds, f);
290 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
294 rc = mds_read_last_rcvd(obddev, f);
296 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
297 GOTO(err_client, rc);
299 mds->mds_rcvd_filp = f;
305 mds_client_free_all(obddev);
307 if (filp_close(f, 0))
308 CERROR("can't close %s after error\n", LAST_RCVD);
315 static struct mds_fs_operations *mds_search_fs_type(const char *name)
318 struct mds_fs_type *type;
320 /* lock mds_fs_types list */
321 list_for_each(p, &mds_fs_types) {
322 type = list_entry(p, struct mds_fs_type, mft_list);
323 if (!strcmp(type->mft_name, name)) {
324 /* unlock mds_fs_types list */
325 return type->mft_ops;
328 /* unlock mds_fs_types list */
332 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
334 struct mds_fs_operations *found;
335 struct mds_fs_type *type;
337 if ((found = mds_search_fs_type(name))) {
339 CERROR("different operations for type %s\n", name);
344 OBD_ALLOC(type, sizeof(*type));
348 INIT_LIST_HEAD(&type->mft_list);
350 type->mft_name = strdup(name);
351 if (!type->mft_name) {
352 OBD_FREE(type, sizeof(*type));
356 list_add(&type->mft_list, &mds_fs_types);
361 void mds_unregister_fs_type(const char *name)
365 /* lock mds_fs_types list */
366 list_for_each(p, &mds_fs_types) {
367 struct mds_fs_type *type;
369 type = list_entry(p, struct mds_fs_type, mft_list);
370 if (!strcmp(type->mft_name, name)) {
372 kfree(type->mft_name);
373 OBD_FREE(type, sizeof(*type));
378 /* unlock mds_fs_types list */
381 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
383 struct mds_fs_operations *fs_ops;
385 if (!(fs_ops = mds_search_fs_type(fstype))) {
389 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
390 name[sizeof(name) - 1] = '\0';
392 if ((rc = request_module(name))) {
393 fs_ops = mds_search_fs_type(fstype);
394 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
400 CERROR("Can't find MDS fs interface '%s'\n", name);
404 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
409 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
411 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
414 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
416 struct mds_obd *mds = &obddev->u.mds;
419 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
420 if (IS_ERR(mds->mds_fsops))
421 RETURN(PTR_ERR(mds->mds_fsops));
423 mds->mds_vfsmnt = mnt;
425 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
426 mds->mds_ctxt.pwdmnt = mnt;
427 mds->mds_ctxt.pwd = mnt->mnt_root;
428 mds->mds_ctxt.fs = get_ds();
431 * Replace the client filesystem delete_inode method with our own,
432 * so that we can clear the object ID before the inode is deleted.
433 * The fs_delete_inode method will call cl_delete_inode for us.
434 * We need to do this for the MDS superblock only, hence we install
435 * a modified copy of the original superblock method table.
437 * We still assume that there is only a single MDS client filesystem
438 * type, as we don't have access to the mds struct in delete_inode
439 * and store the client delete_inode method in a global table. This
440 * will only become a problem if/when multiple MDSs are running on a
441 * single host with different underlying filesystems.
443 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
445 GOTO(out_dec, rc = -ENOMEM);
447 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
448 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
449 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
450 mds->mds_sb->s_op = mds->mds_sop;
452 rc = mds_fs_prep(obddev);
460 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
462 mds_fs_put_ops(mds->mds_fsops);
466 void mds_fs_cleanup(struct obd_device *obddev)
468 struct mds_obd *mds = &obddev->u.mds;
469 mds_client_free_all(obddev);
470 mds_server_free_data(mds);
472 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
473 mds_fs_put_ops(mds->mds_fsops);
476 EXPORT_SYMBOL(mds_register_fs_type);
477 EXPORT_SYMBOL(mds_unregister_fs_type);