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>
26 #include <linux/lustre_fsfilt.h>
28 /* This limit is arbitrary, but for now we fit it in 1 page (32k clients) */
29 #define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
30 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
32 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
34 #define LAST_RCVD "last_rcvd"
36 /* Add client data to the MDS. We use a bitmap to locate a free space
37 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
38 * Otherwise, we have just read the data from the last_rcvd file and
41 int mds_client_add(struct mds_obd *mds, struct mds_export_data *med, int cl_off)
43 int new_client = (cl_off == -1);
45 /* the bitmap operations can handle cl_off > sizeof(long) * 8, so
46 * there's no need for extra complication here
49 cl_off = find_first_zero_bit(last_rcvd_slots, MDS_MAX_CLIENTS);
51 if (cl_off >= MDS_MAX_CLIENTS) {
52 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
55 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
56 CERROR("MDS client %d: found bit is set in bitmap\n",
58 cl_off = find_next_zero_bit(last_rcvd_slots,
59 MDS_MAX_CLIENTS, cl_off);
63 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
64 CERROR("MDS client %d: bit already set in bitmap!!\n",
70 CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
71 cl_off, med->med_mcd->mcd_uuid);
73 med->med_off = cl_off;
76 struct obd_run_ctxt saved;
77 loff_t off = MDS_LR_CLIENT + (cl_off * MDS_LR_SIZE);
80 push_ctxt(&saved, &mds->mds_ctxt, NULL);
81 written = lustre_fwrite(mds->mds_rcvd_filp,
83 sizeof(*med->med_mcd), &off);
84 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
86 if (written != sizeof(*med->med_mcd)) {
91 CDEBUG(D_INFO, "wrote client mcd at off %u (len %u)\n",
92 MDS_LR_CLIENT + (cl_off * MDS_LR_SIZE),
93 (unsigned int)sizeof(*med->med_mcd));
98 int mds_client_free(struct obd_export *exp)
100 struct mds_export_data *med = &exp->exp_mds_data;
101 struct mds_obd *mds = &exp->exp_obd->u.mds;
102 struct mds_client_data zero_mcd;
103 struct obd_run_ctxt saved;
110 off = MDS_LR_CLIENT + (med->med_off * MDS_LR_SIZE);
112 CDEBUG(D_INFO, "freeing client at offset %u (%lld)with UUID '%s'\n",
113 med->med_off, off, med->med_mcd->mcd_uuid);
115 if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
116 CERROR("MDS client %u: bit already clear in bitmap!!\n",
121 memset(&zero_mcd, 0, sizeof zero_mcd);
122 push_ctxt(&saved, &mds->mds_ctxt, NULL);
123 written = lustre_fwrite(mds->mds_rcvd_filp, (const char *)&zero_mcd,
124 sizeof(zero_mcd), &off);
125 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
127 if (written != sizeof(zero_mcd)) {
128 CERROR("error zeroing out client %s off %d in %s: %d\n",
129 med->med_mcd->mcd_uuid, med->med_off, LAST_RCVD,
132 CDEBUG(D_INFO, "zeroed out disconnecting client %s at off %d\n",
133 med->med_mcd->mcd_uuid, med->med_off);
136 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
141 static int mds_server_free_data(struct mds_obd *mds)
143 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
144 mds->mds_server_data = NULL;
149 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
151 struct mds_obd *mds = &obddev->u.mds;
152 struct mds_server_data *msd;
153 struct mds_client_data *mcd = NULL;
156 unsigned long last_rcvd_size = f->f_dentry->d_inode->i_size;
161 OBD_ALLOC(msd, sizeof(*msd));
164 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
166 mds->mds_server_data = msd;
168 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
172 if (rc != sizeof(*msd)) {
173 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
179 CDEBUG(D_INODE, "last_rcvd has size %lu (msd + %lu clients)\n",
180 last_rcvd_size, (last_rcvd_size - sizeof *msd) / sizeof *mcd);
183 * When we do a clean MDS shutdown, we save the last_rcvd into
184 * the header. If we find clients with higher last_rcvd values
185 * then those clients may need recovery done.
187 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
188 mds->mds_last_rcvd = last_rcvd;
189 CDEBUG(D_INODE, "got "LPU64" for server last_rcvd value\n", last_rcvd);
191 last_mount = le64_to_cpu(msd->msd_mount_count);
192 mds->mds_mount_count = last_mount;
193 CDEBUG(D_INODE, "got "LPU64" for server last_mount value\n",last_mount);
195 /* off is adjusted by lustre_fread, so we don't adjust it in the loop */
196 for (off = MDS_LR_CLIENT, cl_off = 0; off < last_rcvd_size; cl_off++) {
200 OBD_ALLOC(mcd, sizeof(*mcd));
202 GOTO(err_msd, rc = -ENOMEM);
205 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
206 if (rc != sizeof(*mcd)) {
207 CERROR("error reading MDS %s offset %d: rc = %d\n",
208 LAST_RCVD, cl_off, rc);
209 if (rc > 0) /* XXX fatal error or just abort reading? */
214 if (mcd->mcd_uuid[0] == '\0') {
215 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
220 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
222 /* These exports are cleaned up by mds_disconnect(), so they
223 * need to be set up like real exports as mds_connect() does.
225 mount_age = last_mount - le64_to_cpu(mcd->mcd_mount_count);
226 if (mount_age < MDS_MOUNT_RECOV) {
227 struct obd_export *exp = class_new_export(obddev);
228 struct mds_export_data *med;
235 med = &exp->exp_mds_data;
237 mds_client_add(mds, med, cl_off);
238 /* create helper if export init gets more complex */
239 INIT_LIST_HEAD(&med->med_open_head);
240 spin_lock_init(&med->med_open_lock);
243 mds->mds_recoverable_clients++;
246 "discarded client %d, UUID '%s', count %Ld\n",
247 cl_off, mcd->mcd_uuid,
248 (long long)le64_to_cpu(mcd->mcd_mount_count));
251 CDEBUG(D_OTHER, "client at offset %d has last_rcvd = %Lu\n",
252 cl_off, (unsigned long long)last_rcvd);
254 if (last_rcvd > mds->mds_last_rcvd)
255 mds->mds_last_rcvd = last_rcvd;
258 mds->mds_last_committed = mds->mds_last_rcvd;
259 if (mds->mds_recoverable_clients) {
260 CERROR("RECOVERY: %d recoverable clients, last_rcvd "LPU64"\n",
261 mds->mds_recoverable_clients, mds->mds_last_rcvd);
262 mds->mds_next_recovery_transno = mds->mds_last_committed + 1;
266 OBD_FREE(mcd, sizeof(*mcd));
271 mds_server_free_data(mds);
275 static int mds_fs_prep(struct obd_device *obddev)
277 struct mds_obd *mds = &obddev->u.mds;
278 struct obd_run_ctxt saved;
279 struct dentry *dentry;
283 push_ctxt(&saved, &mds->mds_ctxt, NULL);
284 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
285 if (IS_ERR(dentry)) {
286 rc = PTR_ERR(dentry);
287 CERROR("cannot create ROOT directory: rc = %d\n", rc);
291 mds->mds_rootfid.id = dentry->d_inode->i_ino;
292 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
293 mds->mds_rootfid.f_type = S_IFDIR;
297 dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
298 if (IS_ERR(dentry)) {
299 rc = PTR_ERR(dentry);
300 CERROR("cannot create FH directory: rc = %d\n", rc);
303 /* XXX probably want to hold on to this later... */
306 f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
309 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
310 GOTO(err_pop, rc = PTR_ERR(f));
312 if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
313 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
314 f->f_dentry->d_inode->i_mode);
315 GOTO(err_filp, rc = -ENOENT);
318 rc = fsfilt_journal_data(obddev, f);
320 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
324 rc = mds_read_last_rcvd(obddev, f);
326 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
327 GOTO(err_client, rc);
329 mds->mds_rcvd_filp = f;
331 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
336 class_disconnect_all(obddev);
338 if (filp_close(f, 0))
339 CERROR("can't close %s after error\n", LAST_RCVD);
343 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
345 struct mds_obd *mds = &obddev->u.mds;
348 mds->mds_vfsmnt = mnt;
350 OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
351 mds->mds_ctxt.pwdmnt = mnt;
352 mds->mds_ctxt.pwd = mnt->mnt_root;
353 mds->mds_ctxt.fs = get_ds();
355 RETURN(mds_fs_prep(obddev));
358 int mds_fs_cleanup(struct obd_device *obddev)
360 struct mds_obd *mds = &obddev->u.mds;
361 struct obd_run_ctxt saved;
364 class_disconnect_all(obddev); /* this cleans up client info too */
365 mds_server_free_data(mds);
367 push_ctxt(&saved, &mds->mds_ctxt, NULL);
368 if (mds->mds_rcvd_filp) {
369 rc = filp_close(mds->mds_rcvd_filp, 0);
370 mds->mds_rcvd_filp = NULL;
373 CERROR("last_rcvd file won't close, rc=%d\n", rc);
375 pop_ctxt(&saved, &mds->mds_ctxt, NULL);