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)
47 /* the bitmap operations can handle cl_off > sizeof(long) * 8, so
48 * there's no need for extra complication here
51 cl_off = find_first_zero_bit(last_rcvd_slots, MDS_MAX_CLIENTS);
53 if (cl_off >= MDS_MAX_CLIENTS) {
54 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
57 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
58 CERROR("MDS client %d: found bit is set in bitmap\n",
60 cl_off = find_next_zero_bit(last_rcvd_slots,
61 MDS_MAX_CLIENTS, cl_off);
65 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
66 CERROR("MDS client %d: bit already set in bitmap!!\n",
72 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
73 cl_off, med->med_mcd->mcd_uuid);
75 med->med_off = cl_off;
79 int mds_client_free(struct obd_export *exp)
81 struct mds_export_data *med = &exp->exp_mds_data;
86 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
87 med->med_off, med->med_mcd->mcd_uuid);
89 if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
90 CERROR("MDS client %d: bit already clear in bitmap!!\n",
95 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
100 static int mds_server_free_data(struct mds_obd *mds)
102 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
103 mds->mds_server_data = NULL;
108 #define LAST_RCVD "last_rcvd"
110 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
112 struct mds_obd *mds = &obddev->u.mds;
113 struct mds_server_data *msd;
114 struct mds_client_data *mcd = NULL;
115 loff_t fsize = f->f_dentry->d_inode->i_size;
123 OBD_ALLOC(msd, sizeof(*msd));
126 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
128 mds->mds_server_data = msd;
130 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
134 if (rc != sizeof(*msd)) {
135 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
143 * When we do a clean MDS shutdown, we save the last_rcvd into
144 * the header. If we find clients with higher last_rcvd values
145 * then those clients may need recovery done.
147 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
148 mds->mds_last_rcvd = last_rcvd;
149 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
150 (unsigned long long)last_rcvd);
152 last_mount = le64_to_cpu(msd->msd_mount_count);
153 mds->mds_mount_count = last_mount;
154 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
155 (unsigned long long)last_mount);
157 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
158 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
159 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
161 OBD_ALLOC(mcd, sizeof(*mcd));
163 GOTO(err_msd, rc = -ENOMEM);
166 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
167 if (rc != sizeof(*mcd)) {
168 CERROR("error reading MDS %s offset %d: rc = %d\n",
169 LAST_RCVD, cl_off, rc);
175 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
177 /* The exports are cleaned up by mds_disconnect, so they
178 * need to be set up like real exports also.
180 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
181 < MDS_MOUNT_RECOV)) {
182 struct obd_export *exp = class_new_export(obddev);
183 struct mds_export_data *med;
190 med = &exp->exp_mds_data;
192 mds_client_add(med, cl_off);
193 /* XXX put this in a helper if it gets more complex */
194 INIT_LIST_HEAD(&med->med_open_head);
195 spin_lock_init(&med->med_open_lock);
202 "ignored client %d, UUID '%s', last_mount %Ld\n",
203 cl_off, mcd->mcd_uuid,
204 (long long)le64_to_cpu(mcd->mcd_mount_count));
207 if (last_rcvd > mds->mds_last_rcvd) {
209 "client at offset %d has last_rcvd = %Lu\n",
210 cl_off, (unsigned long long)last_rcvd);
211 mds->mds_last_rcvd = last_rcvd;
214 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
215 (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
218 OBD_FREE(mcd, sizeof(*mcd));
220 /* After recovery, there can be no local uncommitted transactions */
221 mds->mds_last_committed = mds->mds_last_rcvd;
226 mds_server_free_data(mds);
230 static int mds_fs_prep(struct obd_device *obddev)
232 struct mds_obd *mds = &obddev->u.mds;
233 struct obd_run_ctxt saved;
234 struct dentry *dentry;
238 push_ctxt(&saved, &mds->mds_ctxt, NULL);
239 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
240 if (IS_ERR(dentry)) {
241 rc = PTR_ERR(dentry);
242 CERROR("cannot create ROOT directory: rc = %d\n", rc);
246 mds->mds_rootfid.id = dentry->d_inode->i_ino;
247 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
248 mds->mds_rootfid.f_type = S_IFDIR;
252 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
253 if (IS_ERR(dentry)) {
254 rc = PTR_ERR(dentry);
255 CERROR("cannot create FH directory: rc = %d\n", rc);
258 /* XXX probably want to hold on to this later... */
261 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
264 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
265 GOTO(err_pop, rc = PTR_ERR(f));
267 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
268 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
269 f->f_dentry->d_inode->i_mode);
270 GOTO(err_pop, rc = -ENOENT);
273 rc = mds_fs_journal_data(mds, f);
275 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
279 rc = mds_read_last_rcvd(obddev, f);
281 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
282 GOTO(err_client, rc);
284 mds->mds_rcvd_filp = f;
291 class_disconnect_all(obddev);
293 if (filp_close(f, 0))
294 CERROR("can't close %s after error\n", LAST_RCVD);
298 static struct mds_fs_operations *mds_search_fs_type(const char *name)
301 struct mds_fs_type *type;
303 /* lock mds_fs_types list */
304 list_for_each(p, &mds_fs_types) {
305 type = list_entry(p, struct mds_fs_type, mft_list);
306 if (!strcmp(type->mft_name, name)) {
307 /* unlock mds_fs_types list */
308 return type->mft_ops;
311 /* unlock mds_fs_types list */
315 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
317 struct mds_fs_operations *found;
318 struct mds_fs_type *type;
320 if ((found = mds_search_fs_type(name))) {
322 CERROR("different operations for type %s\n", name);
327 OBD_ALLOC(type, sizeof(*type));
331 INIT_LIST_HEAD(&type->mft_list);
333 type->mft_name = strdup(name);
334 if (!type->mft_name) {
335 OBD_FREE(type, sizeof(*type));
339 list_add(&type->mft_list, &mds_fs_types);
344 void mds_unregister_fs_type(const char *name)
348 /* lock mds_fs_types list */
349 list_for_each(p, &mds_fs_types) {
350 struct mds_fs_type *type;
352 type = list_entry(p, struct mds_fs_type, mft_list);
353 if (!strcmp(type->mft_name, name)) {
355 kfree(type->mft_name);
356 OBD_FREE(type, sizeof(*type));
361 /* unlock mds_fs_types list */
364 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
366 struct mds_fs_operations *fs_ops;
368 if (!(fs_ops = mds_search_fs_type(fstype))) {
372 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
373 name[sizeof(name) - 1] = '\0';
375 if ((rc = request_module(name))) {
376 fs_ops = mds_search_fs_type(fstype);
377 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
383 CERROR("Can't find MDS fs interface '%s'\n", name);
387 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
392 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
394 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
397 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
399 struct mds_obd *mds = &obddev->u.mds;
402 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
403 if (IS_ERR(mds->mds_fsops))
404 RETURN(PTR_ERR(mds->mds_fsops));
406 mds->mds_vfsmnt = mnt;
408 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
409 mds->mds_ctxt.pwdmnt = mnt;
410 mds->mds_ctxt.pwd = mnt->mnt_root;
411 mds->mds_ctxt.fs = get_ds();
414 * Replace the client filesystem delete_inode method with our own,
415 * so that we can clear the object ID before the inode is deleted.
416 * The fs_delete_inode method will call cl_delete_inode for us.
417 * We need to do this for the MDS superblock only, hence we install
418 * a modified copy of the original superblock method table.
420 * We still assume that there is only a single MDS client filesystem
421 * type, as we don't have access to the mds struct in delete_inode
422 * and store the client delete_inode method in a global table. This
423 * will only become a problem if/when multiple MDSs are running on a
424 * single host with different underlying filesystems.
426 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
428 GOTO(out_dec, rc = -ENOMEM);
430 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
431 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
432 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
433 mds->mds_sb->s_op = mds->mds_sop;
435 rc = mds_fs_prep(obddev);
443 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
445 mds_fs_put_ops(mds->mds_fsops);
449 void mds_fs_cleanup(struct obd_device *obddev)
451 struct mds_obd *mds = &obddev->u.mds;
453 class_disconnect_all(obddev); /* this cleans up client info too */
454 mds_server_free_data(mds);
456 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
457 mds_fs_put_ops(mds->mds_fsops);
460 EXPORT_SYMBOL(mds_register_fs_type);
461 EXPORT_SYMBOL(mds_unregister_fs_type);