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 #define LAST_RCVD "last_rcvd"
42 /* Add client data to the MDS. We use a bitmap to locate a free space
43 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
44 * Otherwise, we have just read the data from the last_rcvd file and
47 int mds_client_add(struct mds_obd *mds, struct mds_export_data *med, int cl_off)
49 int new_client = (cl_off == -1);
51 /* the bitmap operations can handle cl_off > sizeof(long) * 8, so
52 * there's no need for extra complication here
55 cl_off = find_first_zero_bit(last_rcvd_slots, MDS_MAX_CLIENTS);
57 if (cl_off >= MDS_MAX_CLIENTS) {
58 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
61 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
62 CERROR("MDS client %d: found bit is set in bitmap\n",
64 cl_off = find_next_zero_bit(last_rcvd_slots,
65 MDS_MAX_CLIENTS, cl_off);
69 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
70 CERROR("MDS client %d: bit already set in bitmap!!\n",
76 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
77 cl_off, med->med_mcd->mcd_uuid);
79 med->med_off = cl_off;
82 struct obd_run_ctxt saved;
83 loff_t off = MDS_LR_CLIENT + (cl_off * MDS_LR_SIZE);
86 push_ctxt(&saved, &mds->mds_ctxt, NULL);
87 written = lustre_fwrite(mds->mds_rcvd_filp,
89 sizeof(*med->med_mcd), &off);
90 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
92 if (written != sizeof(*med->med_mcd)) {
101 int mds_client_free(struct obd_export *exp)
103 struct mds_export_data *med = &exp->exp_mds_data;
104 struct mds_obd *mds = &exp->exp_obd->u.mds;
105 struct mds_client_data zero_mcd;
106 struct obd_run_ctxt saved;
113 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
114 med->med_off, med->med_mcd->mcd_uuid);
116 if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
117 CERROR("MDS client %d: bit already clear in bitmap!!\n",
124 memset(&zero_mcd, 0, sizeof zero_mcd);
125 push_ctxt(&saved, &mds->mds_ctxt, NULL);
126 written = lustre_fwrite(mds->mds_rcvd_filp, (const char *)&zero_mcd,
127 sizeof zero_mcd, &off);
128 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
130 if (written != sizeof zero_mcd) {
131 CERROR("error zeroing out client %s off %d in %s: %d\n",
132 med->med_mcd->mcd_uuid, med->med_off, LAST_RCVD,
136 CDEBUG(D_INFO, "zeroed out disconnecting client %s at off %d\n",
137 med->med_mcd->mcd_uuid, med->med_off);
140 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
145 static int mds_server_free_data(struct mds_obd *mds)
147 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
148 mds->mds_server_data = NULL;
153 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
155 struct mds_obd *mds = &obddev->u.mds;
156 struct mds_server_data *msd;
157 struct mds_client_data *mcd = NULL;
160 int max_off = f->f_dentry->d_inode->i_size / sizeof(*mcd);
165 OBD_ALLOC(msd, sizeof(*msd));
168 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
170 mds->mds_server_data = msd;
172 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
176 if (rc != sizeof(*msd)) {
177 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
185 * When we do a clean MDS shutdown, we save the last_rcvd into
186 * the header. If we find clients with higher last_rcvd values
187 * then those clients may need recovery done.
189 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
190 mds->mds_last_rcvd = last_rcvd;
191 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
192 (unsigned long long)last_rcvd);
194 last_mount = le64_to_cpu(msd->msd_mount_count);
195 mds->mds_mount_count = last_mount;
196 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
197 (unsigned long long)last_mount);
199 for (off = MDS_LR_CLIENT, cl_off = 0;
201 off += MDS_LR_SIZE, cl_off++) {
205 OBD_ALLOC(mcd, sizeof(*mcd));
207 GOTO(err_msd, rc = -ENOMEM);
210 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
211 if (rc != sizeof(*mcd)) {
212 CERROR("error reading MDS %s offset %d: rc = %d\n",
213 LAST_RCVD, cl_off, rc);
219 if (mcd->mcd_uuid[0] == '\0') {
220 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
225 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
227 /* The exports are cleaned up by mds_disconnect, so they
228 * need to be set up like real exports also.
230 mount_age = last_mount - le64_to_cpu(mcd->mcd_mount_count);
231 if (last_rcvd && mount_age < MDS_MOUNT_RECOV) {
232 struct obd_export *exp = class_new_export(obddev);
233 struct mds_export_data *med;
240 med = &exp->exp_mds_data;
242 mds_client_add(mds, med, cl_off);
243 /* XXX put this in a helper if it gets more complex */
244 INIT_LIST_HEAD(&med->med_open_head);
245 spin_lock_init(&med->med_open_lock);
248 mds->mds_recoverable_clients++;
252 "discarded client %d, UUID '%s', count %Ld\n",
253 cl_off, mcd->mcd_uuid,
254 (long long)le64_to_cpu(mcd->mcd_mount_count));
257 if (last_rcvd > mds->mds_last_rcvd) {
259 "client at offset %d has last_rcvd = %Lu\n",
260 cl_off, (unsigned long long)last_rcvd);
261 mds->mds_last_rcvd = last_rcvd;
265 mds->mds_last_committed = mds->mds_last_rcvd;
266 if (mds->mds_recoverable_clients) {
267 CERROR("need recovery: %d recoverable clients, last_rcvd %Lu\n",
268 mds->mds_recoverable_clients, mds->mds_last_rcvd);
272 OBD_FREE(mcd, sizeof(*mcd));
277 mds_server_free_data(mds);
281 static int mds_fs_prep(struct obd_device *obddev)
283 struct mds_obd *mds = &obddev->u.mds;
284 struct obd_run_ctxt saved;
285 struct dentry *dentry;
289 push_ctxt(&saved, &mds->mds_ctxt, NULL);
290 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
291 if (IS_ERR(dentry)) {
292 rc = PTR_ERR(dentry);
293 CERROR("cannot create ROOT directory: rc = %d\n", rc);
297 mds->mds_rootfid.id = dentry->d_inode->i_ino;
298 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
299 mds->mds_rootfid.f_type = S_IFDIR;
303 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
304 if (IS_ERR(dentry)) {
305 rc = PTR_ERR(dentry);
306 CERROR("cannot create FH directory: rc = %d\n", rc);
309 /* XXX probably want to hold on to this later... */
312 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
315 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
316 GOTO(err_pop, rc = PTR_ERR(f));
318 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
319 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
320 f->f_dentry->d_inode->i_mode);
321 GOTO(err_pop, rc = -ENOENT);
324 rc = mds_fs_journal_data(mds, f);
326 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
330 rc = mds_read_last_rcvd(obddev, f);
332 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
333 GOTO(err_client, rc);
335 mds->mds_rcvd_filp = f;
337 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
342 class_disconnect_all(obddev);
344 if (filp_close(f, 0))
345 CERROR("can't close %s after error\n", LAST_RCVD);
349 static struct mds_fs_operations *mds_search_fs_type(const char *name)
352 struct mds_fs_type *type;
354 /* lock mds_fs_types list */
355 list_for_each(p, &mds_fs_types) {
356 type = list_entry(p, struct mds_fs_type, mft_list);
357 if (!strcmp(type->mft_name, name)) {
358 /* unlock mds_fs_types list */
359 return type->mft_ops;
362 /* unlock mds_fs_types list */
366 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
368 struct mds_fs_operations *found;
369 struct mds_fs_type *type;
371 if ((found = mds_search_fs_type(name))) {
373 CERROR("different operations for type %s\n", name);
378 OBD_ALLOC(type, sizeof(*type));
382 INIT_LIST_HEAD(&type->mft_list);
384 type->mft_name = strdup(name);
385 if (!type->mft_name) {
386 OBD_FREE(type, sizeof(*type));
390 list_add(&type->mft_list, &mds_fs_types);
395 void mds_unregister_fs_type(const char *name)
399 /* lock mds_fs_types list */
400 list_for_each(p, &mds_fs_types) {
401 struct mds_fs_type *type;
403 type = list_entry(p, struct mds_fs_type, mft_list);
404 if (!strcmp(type->mft_name, name)) {
406 kfree(type->mft_name);
407 OBD_FREE(type, sizeof(*type));
412 /* unlock mds_fs_types list */
415 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
417 struct mds_fs_operations *fs_ops;
419 if (!(fs_ops = mds_search_fs_type(fstype))) {
423 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
424 name[sizeof(name) - 1] = '\0';
426 if ((rc = request_module(name))) {
427 fs_ops = mds_search_fs_type(fstype);
428 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
434 CERROR("Can't find MDS fs interface '%s'\n", name);
438 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
443 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
445 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
448 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
450 struct mds_obd *mds = &obddev->u.mds;
453 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
454 if (IS_ERR(mds->mds_fsops))
455 RETURN(PTR_ERR(mds->mds_fsops));
457 mds->mds_vfsmnt = mnt;
459 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
460 mds->mds_ctxt.pwdmnt = mnt;
461 mds->mds_ctxt.pwd = mnt->mnt_root;
462 mds->mds_ctxt.fs = get_ds();
465 * Replace the client filesystem delete_inode method with our own,
466 * so that we can clear the object ID before the inode is deleted.
467 * The fs_delete_inode method will call cl_delete_inode for us.
468 * We need to do this for the MDS superblock only, hence we install
469 * a modified copy of the original superblock method table.
471 * We still assume that there is only a single MDS client filesystem
472 * type, as we don't have access to the mds struct in delete_inode
473 * and store the client delete_inode method in a global table. This
474 * will only become a problem if/when multiple MDSs are running on a
475 * single host with different underlying filesystems.
477 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
479 GOTO(out_dec, rc = -ENOMEM);
481 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
482 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
483 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
484 mds->mds_sb->s_op = mds->mds_sop;
486 rc = mds_fs_prep(obddev);
494 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
496 mds_fs_put_ops(mds->mds_fsops);
500 void mds_fs_cleanup(struct obd_device *obddev)
502 struct mds_obd *mds = &obddev->u.mds;
504 class_disconnect_all(obddev); /* this cleans up client info too */
505 mds_server_free_data(mds);
507 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
508 mds_fs_put_ops(mds->mds_fsops);
511 EXPORT_SYMBOL(mds_register_fs_type);
512 EXPORT_SYMBOL(mds_unregister_fs_type);