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>
24 LIST_HEAD(mds_fs_types);
27 struct list_head mft_list;
28 struct mds_fs_operations *mft_ops;
32 /* This will be a hash table at some point. */
33 static int mds_init_client_data(struct mds_obd *mds)
35 INIT_LIST_HEAD(&mds->mds_client_info);
39 #define MDS_MAX_CLIENTS 1024
40 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
42 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
44 /* Add client data to the MDS. The in-memory storage will be a hash at some
45 * point. We use a bitmap to locate a free space in the last_rcvd file if
46 * cl_off is -1 (i.e. a new client). Otherwise, we have just read the data
47 * from the last_rcvd file and we know its offset.
49 int mds_client_add(struct mds_obd *mds, struct mds_client_data *mcd, int cl_off)
51 struct mds_client_info *mci;
53 OBD_ALLOC(mci, sizeof(*mci));
55 CERROR("no memory for MDS client info\n");
58 INIT_LIST_HEAD(&mci->mci_open_head);
60 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
61 cl_off, mcd->mcd_uuid);
68 word = last_rcvd_slots;
71 if (word - last_rcvd_slots >= MDS_MAX_CLIENT_WORDS) {
72 CERROR("no room in client MDS bitmap - fix code\n");
76 if (test_and_set_bit(bit, word)) {
77 CERROR("found bit %d set for word %d - fix code\n",
78 bit, word - last_rcvd_slots);
81 cl_off = word - last_rcvd_slots + bit;
83 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
84 CERROR("bit %d already set in bitmap - bad bad\n",
91 mci->mci_off = cl_off;
93 /* For now we just put the clients in a list, not a hashed list */
94 list_add_tail(&mci->mci_list, &mds->mds_client_info);
96 mds->mds_client_count++;
101 void mds_client_del(struct mds_obd *mds, struct mds_client_info *mci)
106 word = last_rcvd_slots + mci->mci_off / sizeof(unsigned long);
107 bit = mci->mci_off % sizeof(unsigned long);
109 if (!test_and_clear_bit(bit, word)) {
110 CERROR("bit %d already clear in word %d - bad bad\n",
111 bit, word - last_rcvd_slots);
115 --mds->mds_client_count;
116 list_del(&mci->mci_list);
117 OBD_FREE(mci->mci_mcd, sizeof(*mci->mci_mcd));
118 OBD_FREE(mci, sizeof (*mci));
121 static int mds_client_free_all(struct mds_obd *mds)
123 struct list_head *p, *n;
125 list_for_each_safe(p, n, &mds->mds_client_info) {
126 struct mds_client_info *mci;
128 mci = list_entry(p, struct mds_client_info, mci_list);
129 mds_client_del(mds, mci);
135 static int mds_server_free_data(struct mds_obd *mds)
137 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
138 mds->mds_server_data = NULL;
143 #define LAST_RCVD "last_rcvd"
145 static int mds_read_last_rcvd(struct mds_obd *mds, struct file *f)
147 struct mds_server_data *msd;
148 struct mds_client_data *mcd = NULL;
149 loff_t fsize = f->f_dentry->d_inode->i_size;
156 OBD_ALLOC(msd, sizeof(*msd));
159 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
161 mds->mds_server_data = msd;
163 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
165 } else if (rc != sizeof(*msd)) {
166 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
174 * When we do a clean MDS shutdown, we save the last_rcvd into
175 * the header. If we find clients with higher last_rcvd values
176 * then those clients may need recovery done.
178 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
179 mds->mds_last_rcvd = last_rcvd;
180 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
181 (unsigned long long)last_rcvd);
183 last_mount = le64_to_cpu(msd->msd_mount_count);
184 mds->mds_mount_count = last_mount;
185 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
186 (unsigned long long)last_mount);
188 for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
189 off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
190 off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
192 OBD_ALLOC(mcd, sizeof(*mcd));
194 GOTO(err_msd, rc = -ENOMEM);
196 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
197 if (rc != sizeof(*mcd)) {
198 CERROR("error reading MDS %s offset %d: rc = %d\n",
199 LAST_RCVD, cl_off, rc);
205 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
206 last_mount = le64_to_cpu(mcd->mcd_mount_count);
209 last_mount - mcd->mcd_mount_count < MDS_MOUNT_RECOV) {
210 rc = mds_client_add(mds, mcd, cl_off);
218 "client at offset %d with UUID '%s' ignored\n",
219 cl_off, mcd->mcd_uuid);
222 if (last_rcvd > mds->mds_last_rcvd) {
224 "client at offset %d has last_rcvd = %Lu\n",
225 cl_off, (unsigned long long)last_rcvd);
226 mds->mds_last_rcvd = last_rcvd;
229 CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d clients\n",
230 (unsigned long long)mds->mds_last_rcvd, mds->mds_client_count);
232 /* After recovery, there can be no local uncommitted transactions */
233 mds->mds_last_committed = mds->mds_last_rcvd;
238 mds_server_free_data(mds);
242 static int mds_fs_prep(struct mds_obd *mds)
244 struct obd_run_ctxt saved;
245 struct dentry *dentry;
249 push_ctxt(&saved, &mds->mds_ctxt);
250 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0700);
251 if (IS_ERR(dentry)) {
252 rc = PTR_ERR(dentry);
253 CERROR("cannot create ROOT directory: rc = %d\n", rc);
256 /* XXX probably want to hold on to this later... */
258 f = filp_open("ROOT", O_RDONLY, 0);
261 CERROR("cannot open ROOT: rc = %d\n", rc);
266 mds->mds_rootfid.id = f->f_dentry->d_inode->i_ino;
267 mds->mds_rootfid.generation = f->f_dentry->d_inode->i_generation;
268 mds->mds_rootfid.f_type = S_IFDIR;
270 rc = filp_close(f, 0);
272 CERROR("cannot close ROOT: rc = %d\n", rc);
276 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
277 if (IS_ERR(dentry)) {
278 rc = PTR_ERR(dentry);
279 CERROR("cannot create FH directory: rc = %d\n", rc);
282 /* XXX probably want to hold on to this later... */
285 rc = mds_init_client_data(mds);
289 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
292 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
293 GOTO(err_pop, rc = PTR_ERR(f));
295 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
296 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
297 f->f_dentry->d_inode->i_mode);
298 GOTO(err_pop, rc = -ENOENT);
301 rc = mds_fs_journal_data(mds, f);
303 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
307 rc = mds_read_last_rcvd(mds, f);
309 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
310 GOTO(err_client, rc);
312 mds->mds_rcvd_filp = f;
318 mds_client_free_all(mds);
320 if (filp_close(f, 0))
321 CERROR("can't close %s after error\n", LAST_RCVD);
328 static struct mds_fs_operations *mds_search_fs_type(const char *name)
331 struct mds_fs_type *type;
333 /* lock mds_fs_types list */
334 list_for_each(p, &mds_fs_types) {
335 type = list_entry(p, struct mds_fs_type, mft_list);
336 if (!strcmp(type->mft_name, name)) {
337 /* unlock mds_fs_types list */
338 return type->mft_ops;
341 /* unlock mds_fs_types list */
345 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
347 struct mds_fs_operations *found;
348 struct mds_fs_type *type;
350 if ((found = mds_search_fs_type(name))) {
352 CERROR("different operations for type %s\n", name);
357 OBD_ALLOC(type, sizeof(*type));
361 INIT_LIST_HEAD(&type->mft_list);
363 type->mft_name = strdup(name);
364 if (!type->mft_name) {
365 OBD_FREE(type, sizeof(*type));
369 list_add(&type->mft_list, &mds_fs_types);
374 void mds_unregister_fs_type(const char *name)
378 /* lock mds_fs_types list */
379 list_for_each(p, &mds_fs_types) {
380 struct mds_fs_type *type;
382 type = list_entry(p, struct mds_fs_type, mft_list);
383 if (!strcmp(type->mft_name, name)) {
385 kfree(type->mft_name);
386 OBD_FREE(type, sizeof(*type));
391 /* unlock mds_fs_types list */
394 int mds_fs_setup(struct mds_obd *mds, struct vfsmount *mnt)
396 struct mds_fs_operations *fs_ops;
399 if (!(fs_ops = mds_search_fs_type(mds->mds_fstype))) {
402 snprintf(name, sizeof(name) - 1, "mds_%s", mds->mds_fstype);
403 name[sizeof(name) - 1] = '\0';
405 if ((rc = request_module(name))) {
406 fs_ops = mds_search_fs_type(mds->mds_fstype);
407 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
413 CERROR("Can't find MDS fs interface '%s'\n", name);
418 mds->mds_fsops = fs_ops;
419 mds->mds_vfsmnt = mnt;
420 mds->mds_ctxt.pwdmnt = mnt;
421 mds->mds_ctxt.pwd = mnt->mnt_root;
422 mds->mds_ctxt.fs = KERNEL_DS;
425 * Replace the client filesystem delete_inode method with our own,
426 * so that we can clear the object ID before the inode is deleted.
427 * The fs_delete_inode method will call cl_delete_inode for us.
428 * We need to do this for the MDS superblock only, hence we install
429 * a modified copy of the original superblock method table.
431 * We still assume that there is only a single MDS client filesystem
432 * type, as we don't have access to the mds struct in delete_inode
433 * and store the client delete_inode method in a global table. This
434 * will only become a problem if/when multiple MDSs are running on a
435 * single host with different underlying filesystems.
437 OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
441 memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
442 mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
443 mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
444 mds->mds_sb->s_op = mds->mds_sop;
446 rc = mds_fs_prep(mds);
449 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
454 void mds_fs_cleanup(struct mds_obd *mds)
456 mds_client_free_all(mds);
457 mds_server_free_data(mds);
459 OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
462 EXPORT_SYMBOL(mds_register_fs_type);
463 EXPORT_SYMBOL(mds_unregister_fs_type);