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) * sizeof(unsigned long) + bit;
68 /* test_and_set_bit can handle cl_off > sizeof(long), so there's
69 * no need to frob it */
70 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
71 CERROR("bit %d already set in bitmap - bad bad\n",
77 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
78 cl_off, med->med_mcd->mcd_uuid);
80 med->med_off = cl_off;
84 int mds_client_free(struct obd_export *exp)
86 struct mds_export_data *med = &exp->exp_mds_data;
93 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
94 med->med_off, med->med_mcd->mcd_uuid);
96 word = last_rcvd_slots + med->med_off / sizeof(unsigned long);
97 bit = med->med_off % sizeof(unsigned long);
99 if (!test_and_clear_bit(bit, word)) {
100 CERROR("bit %d already clear in word %d - bad bad\n",
101 bit, word - last_rcvd_slots);
105 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
110 static int mds_server_free_data(struct mds_obd *mds)
112 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
113 mds->mds_server_data = NULL;
118 #define LAST_RCVD "last_rcvd"
120 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
122 struct mds_obd *mds = &obddev->u.mds;
123 struct mds_server_data *msd;
124 struct mds_client_data *mcd = NULL;
125 loff_t fsize = f->f_dentry->d_inode->i_size;
133 OBD_ALLOC(msd, sizeof(*msd));
136 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
138 mds->mds_server_data = msd;
140 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
144 if (rc != sizeof(*msd)) {
145 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
153 * When we do a clean MDS shutdown, we save the last_rcvd into
154 * the header. If we find clients with higher last_rcvd values
155 * then those clients may need recovery done.
157 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
158 mds->mds_last_rcvd = last_rcvd;
159 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
160 (unsigned long long)last_rcvd);
162 last_mount = le64_to_cpu(msd->msd_mount_count);
163 mds->mds_mount_count = last_mount;
164 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
165 (unsigned long long)last_mount);
167 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
168 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
169 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
171 OBD_ALLOC(mcd, sizeof(*mcd));
173 GOTO(err_msd, rc = -ENOMEM);
176 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
177 if (rc != sizeof(*mcd)) {
178 CERROR("error reading MDS %s offset %d: rc = %d\n",
179 LAST_RCVD, cl_off, rc);
185 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
187 /* Do client recovery here (open files, etc) */
188 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
189 < MDS_MOUNT_RECOV)) {
190 struct obd_export *export = class_new_export(obddev);
195 export->exp_mds_data.med_mcd = mcd;
196 mds_client_add(&export->exp_mds_data, cl_off);
201 "ignored client %d, UUID '%s', last_mount %Ld\n",
202 cl_off, mcd->mcd_uuid,
203 (long long)le64_to_cpu(mcd->mcd_mount_count));
206 if (last_rcvd > mds->mds_last_rcvd) {
208 "client at offset %d has last_rcvd = %Lu\n",
209 cl_off, (unsigned long long)last_rcvd);
210 mds->mds_last_rcvd = last_rcvd;
213 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
214 (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
217 OBD_FREE(mcd, sizeof(*mcd));
219 /* After recovery, there can be no local uncommitted transactions */
220 mds->mds_last_committed = mds->mds_last_rcvd;
225 mds_server_free_data(mds);
229 static int mds_fs_prep(struct obd_device *obddev)
231 struct mds_obd *mds = &obddev->u.mds;
232 struct obd_run_ctxt saved;
233 struct dentry *dentry;
237 push_ctxt(&saved, &mds->mds_ctxt);
238 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
239 if (IS_ERR(dentry)) {
240 rc = PTR_ERR(dentry);
241 CERROR("cannot create ROOT directory: rc = %d\n", rc);
245 mds->mds_rootfid.id = dentry->d_inode->i_ino;
246 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
247 mds->mds_rootfid.f_type = S_IFDIR;
251 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
252 if (IS_ERR(dentry)) {
253 rc = PTR_ERR(dentry);
254 CERROR("cannot create FH directory: rc = %d\n", rc);
257 /* XXX probably want to hold on to this later... */
260 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
263 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
264 GOTO(err_pop, rc = PTR_ERR(f));
266 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
267 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
268 f->f_dentry->d_inode->i_mode);
269 GOTO(err_pop, rc = -ENOENT);
272 rc = mds_fs_journal_data(mds, f);
274 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
278 rc = mds_read_last_rcvd(obddev, f);
280 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
281 GOTO(err_client, rc);
283 mds->mds_rcvd_filp = f;
290 class_disconnect_all(obddev);
292 if (filp_close(f, 0))
293 CERROR("can't close %s after error\n", LAST_RCVD);
297 static struct mds_fs_operations *mds_search_fs_type(const char *name)
300 struct mds_fs_type *type;
302 /* lock mds_fs_types list */
303 list_for_each(p, &mds_fs_types) {
304 type = list_entry(p, struct mds_fs_type, mft_list);
305 if (!strcmp(type->mft_name, name)) {
306 /* unlock mds_fs_types list */
307 return type->mft_ops;
310 /* unlock mds_fs_types list */
314 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
316 struct mds_fs_operations *found;
317 struct mds_fs_type *type;
319 if ((found = mds_search_fs_type(name))) {
321 CERROR("different operations for type %s\n", name);
326 OBD_ALLOC(type, sizeof(*type));
330 INIT_LIST_HEAD(&type->mft_list);
332 type->mft_name = strdup(name);
333 if (!type->mft_name) {
334 OBD_FREE(type, sizeof(*type));
338 list_add(&type->mft_list, &mds_fs_types);
343 void mds_unregister_fs_type(const char *name)
347 /* lock mds_fs_types list */
348 list_for_each(p, &mds_fs_types) {
349 struct mds_fs_type *type;
351 type = list_entry(p, struct mds_fs_type, mft_list);
352 if (!strcmp(type->mft_name, name)) {
354 kfree(type->mft_name);
355 OBD_FREE(type, sizeof(*type));
360 /* unlock mds_fs_types list */
363 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
365 struct mds_fs_operations *fs_ops;
367 if (!(fs_ops = mds_search_fs_type(fstype))) {
371 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
372 name[sizeof(name) - 1] = '\0';
374 if ((rc = request_module(name))) {
375 fs_ops = mds_search_fs_type(fstype);
376 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
382 CERROR("Can't find MDS fs interface '%s'\n", name);
386 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
391 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
393 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
396 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
398 struct mds_obd *mds = &obddev->u.mds;
401 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
402 if (IS_ERR(mds->mds_fsops))
403 RETURN(PTR_ERR(mds->mds_fsops));
405 mds->mds_vfsmnt = mnt;
407 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
408 mds->mds_ctxt.pwdmnt = mnt;
409 mds->mds_ctxt.pwd = mnt->mnt_root;
410 mds->mds_ctxt.fs = get_ds();
413 * Replace the client filesystem delete_inode method with our own,
414 * so that we can clear the object ID before the inode is deleted.
415 * The fs_delete_inode method will call cl_delete_inode for us.
416 * We need to do this for the MDS superblock only, hence we install
417 * a modified copy of the original superblock method table.
419 * We still assume that there is only a single MDS client filesystem
420 * type, as we don't have access to the mds struct in delete_inode
421 * and store the client delete_inode method in a global table. This
422 * will only become a problem if/when multiple MDSs are running on a
423 * single host with different underlying filesystems.
425 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
427 GOTO(out_dec, rc = -ENOMEM);
429 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
430 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
431 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
432 mds->mds_sb->s_op = mds->mds_sop;
434 rc = mds_fs_prep(obddev);
442 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
444 mds_fs_put_ops(mds->mds_fsops);
448 void mds_fs_cleanup(struct obd_device *obddev)
450 struct mds_obd *mds = &obddev->u.mds;
452 class_disconnect_all(obddev); /* this cleans up client info too */
453 mds_server_free_data(mds);
455 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
456 mds_fs_put_ops(mds->mds_fsops);
459 EXPORT_SYMBOL(mds_register_fs_type);
460 EXPORT_SYMBOL(mds_unregister_fs_type);