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 obd_export *exp)
84 struct mds_export_data *med = &exp->exp_mds_data;
91 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
92 med->med_off, med->med_mcd->mcd_uuid);
94 word = last_rcvd_slots + med->med_off / sizeof(unsigned long);
95 bit = med->med_off % sizeof(unsigned long);
97 if (!test_and_clear_bit(bit, word)) {
98 CERROR("bit %d already clear in word %d - bad bad\n",
99 bit, word - last_rcvd_slots);
103 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
108 static int mds_server_free_data(struct mds_obd *mds)
110 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
111 mds->mds_server_data = NULL;
116 #define LAST_RCVD "last_rcvd"
118 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
120 struct mds_obd *mds = &obddev->u.mds;
121 struct mds_server_data *msd;
122 struct mds_client_data *mcd = NULL;
123 loff_t fsize = f->f_dentry->d_inode->i_size;
131 OBD_ALLOC(msd, sizeof(*msd));
134 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
136 mds->mds_server_data = msd;
138 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
142 if (rc != sizeof(*msd)) {
143 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
151 * When we do a clean MDS shutdown, we save the last_rcvd into
152 * the header. If we find clients with higher last_rcvd values
153 * then those clients may need recovery done.
155 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
156 mds->mds_last_rcvd = last_rcvd;
157 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
158 (unsigned long long)last_rcvd);
160 last_mount = le64_to_cpu(msd->msd_mount_count);
161 mds->mds_mount_count = last_mount;
162 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
163 (unsigned long long)last_mount);
165 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
166 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
167 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
169 OBD_ALLOC(mcd, sizeof(*mcd));
171 GOTO(err_msd, rc = -ENOMEM);
174 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
175 if (rc != sizeof(*mcd)) {
176 CERROR("error reading MDS %s offset %d: rc = %d\n",
177 LAST_RCVD, cl_off, rc);
183 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
185 /* Do client recovery here (open files, etc) */
186 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
187 < MDS_MOUNT_RECOV)) {
188 struct obd_export *export = class_new_export(obddev);
193 export->exp_mds_data.med_mcd = mcd;
194 mds_client_add(&export->exp_mds_data, cl_off);
199 "ignored client %d, UUID '%s', last_mount %Ld\n",
200 cl_off, mcd->mcd_uuid,
201 (long long)le64_to_cpu(mcd->mcd_mount_count));
204 if (last_rcvd > mds->mds_last_rcvd) {
206 "client at offset %d has last_rcvd = %Lu\n",
207 cl_off, (unsigned long long)last_rcvd);
208 mds->mds_last_rcvd = last_rcvd;
211 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
212 (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
215 OBD_FREE(mcd, sizeof(*mcd));
217 /* After recovery, there can be no local uncommitted transactions */
218 mds->mds_last_committed = mds->mds_last_rcvd;
223 mds_server_free_data(mds);
227 static int mds_fs_prep(struct obd_device *obddev)
229 struct mds_obd *mds = &obddev->u.mds;
230 struct obd_run_ctxt saved;
231 struct dentry *dentry;
235 push_ctxt(&saved, &mds->mds_ctxt);
236 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
237 if (IS_ERR(dentry)) {
238 rc = PTR_ERR(dentry);
239 CERROR("cannot create ROOT directory: rc = %d\n", rc);
243 mds->mds_rootfid.id = dentry->d_inode->i_ino;
244 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
245 mds->mds_rootfid.f_type = S_IFDIR;
249 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
250 if (IS_ERR(dentry)) {
251 rc = PTR_ERR(dentry);
252 CERROR("cannot create FH directory: rc = %d\n", rc);
255 /* XXX probably want to hold on to this later... */
258 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
261 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
262 GOTO(err_pop, rc = PTR_ERR(f));
264 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
265 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
266 f->f_dentry->d_inode->i_mode);
267 GOTO(err_pop, rc = -ENOENT);
270 rc = mds_fs_journal_data(mds, f);
272 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
276 rc = mds_read_last_rcvd(obddev, f);
278 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
279 GOTO(err_client, rc);
281 mds->mds_rcvd_filp = f;
288 class_disconnect_all(obddev);
290 if (filp_close(f, 0))
291 CERROR("can't close %s after error\n", LAST_RCVD);
295 static struct mds_fs_operations *mds_search_fs_type(const char *name)
298 struct mds_fs_type *type;
300 /* lock mds_fs_types list */
301 list_for_each(p, &mds_fs_types) {
302 type = list_entry(p, struct mds_fs_type, mft_list);
303 if (!strcmp(type->mft_name, name)) {
304 /* unlock mds_fs_types list */
305 return type->mft_ops;
308 /* unlock mds_fs_types list */
312 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
314 struct mds_fs_operations *found;
315 struct mds_fs_type *type;
317 if ((found = mds_search_fs_type(name))) {
319 CERROR("different operations for type %s\n", name);
324 OBD_ALLOC(type, sizeof(*type));
328 INIT_LIST_HEAD(&type->mft_list);
330 type->mft_name = strdup(name);
331 if (!type->mft_name) {
332 OBD_FREE(type, sizeof(*type));
336 list_add(&type->mft_list, &mds_fs_types);
341 void mds_unregister_fs_type(const char *name)
345 /* lock mds_fs_types list */
346 list_for_each(p, &mds_fs_types) {
347 struct mds_fs_type *type;
349 type = list_entry(p, struct mds_fs_type, mft_list);
350 if (!strcmp(type->mft_name, name)) {
352 kfree(type->mft_name);
353 OBD_FREE(type, sizeof(*type));
358 /* unlock mds_fs_types list */
361 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
363 struct mds_fs_operations *fs_ops;
365 if (!(fs_ops = mds_search_fs_type(fstype))) {
369 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
370 name[sizeof(name) - 1] = '\0';
372 if ((rc = request_module(name))) {
373 fs_ops = mds_search_fs_type(fstype);
374 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
380 CERROR("Can't find MDS fs interface '%s'\n", name);
384 __MOD_INC_USE_COUNT(fs_ops->fs_owner);
389 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
391 __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
394 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
396 struct mds_obd *mds = &obddev->u.mds;
399 mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
400 if (IS_ERR(mds->mds_fsops))
401 RETURN(PTR_ERR(mds->mds_fsops));
403 mds->mds_vfsmnt = mnt;
405 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
406 mds->mds_ctxt.pwdmnt = mnt;
407 mds->mds_ctxt.pwd = mnt->mnt_root;
408 mds->mds_ctxt.fs = get_ds();
411 * Replace the client filesystem delete_inode method with our own,
412 * so that we can clear the object ID before the inode is deleted.
413 * The fs_delete_inode method will call cl_delete_inode for us.
414 * We need to do this for the MDS superblock only, hence we install
415 * a modified copy of the original superblock method table.
417 * We still assume that there is only a single MDS client filesystem
418 * type, as we don't have access to the mds struct in delete_inode
419 * and store the client delete_inode method in a global table. This
420 * will only become a problem if/when multiple MDSs are running on a
421 * single host with different underlying filesystems.
423 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
425 GOTO(out_dec, rc = -ENOMEM);
427 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
428 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
429 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
430 mds->mds_sb->s_op = mds->mds_sop;
432 rc = mds_fs_prep(obddev);
440 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
442 mds_fs_put_ops(mds->mds_fsops);
446 void mds_fs_cleanup(struct obd_device *obddev)
448 struct mds_obd *mds = &obddev->u.mds;
450 class_disconnect_all(obddev); /* this cleans up client info too */
451 mds_server_free_data(mds);
453 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
454 mds_fs_put_ops(mds->mds_fsops);
457 EXPORT_SYMBOL(mds_register_fs_type);
458 EXPORT_SYMBOL(mds_unregister_fs_type);