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)) {
95 int mds_client_free(struct obd_export *exp)
97 struct mds_export_data *med = &exp->exp_mds_data;
98 struct mds_obd *mds = &exp->exp_obd->u.mds;
99 struct mds_client_data zero_mcd;
100 struct obd_run_ctxt saved;
107 CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
108 med->med_off, med->med_mcd->mcd_uuid);
110 if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
111 CERROR("MDS client %d: bit already clear in bitmap!!\n",
118 memset(&zero_mcd, 0, sizeof zero_mcd);
119 push_ctxt(&saved, &mds->mds_ctxt, NULL);
120 written = lustre_fwrite(mds->mds_rcvd_filp, (const char *)&zero_mcd,
121 sizeof zero_mcd, &off);
122 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
124 if (written != sizeof zero_mcd) {
125 CERROR("error zeroing out client %s off %d in %s: %d\n",
126 med->med_mcd->mcd_uuid, med->med_off, LAST_RCVD,
130 CDEBUG(D_INFO, "zeroed out disconnecting client %s at off %d\n",
131 med->med_mcd->mcd_uuid, med->med_off);
134 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
139 static int mds_server_free_data(struct mds_obd *mds)
141 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
142 mds->mds_server_data = NULL;
147 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
149 struct mds_obd *mds = &obddev->u.mds;
150 struct mds_server_data *msd;
151 struct mds_client_data *mcd = NULL;
154 int max_off = f->f_dentry->d_inode->i_size / sizeof(*mcd);
159 OBD_ALLOC(msd, sizeof(*msd));
162 rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
164 mds->mds_server_data = msd;
166 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
170 if (rc != sizeof(*msd)) {
171 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
179 * When we do a clean MDS shutdown, we save the last_rcvd into
180 * the header. If we find clients with higher last_rcvd values
181 * then those clients may need recovery done.
183 last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
184 mds->mds_last_rcvd = last_rcvd;
185 CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
186 (unsigned long long)last_rcvd);
188 last_mount = le64_to_cpu(msd->msd_mount_count);
189 mds->mds_mount_count = last_mount;
190 CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
191 (unsigned long long)last_mount);
193 for (off = MDS_LR_CLIENT, cl_off = 0;
195 off += MDS_LR_SIZE, cl_off++) {
199 OBD_ALLOC(mcd, sizeof(*mcd));
201 GOTO(err_msd, rc = -ENOMEM);
204 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
205 if (rc != sizeof(*mcd)) {
206 CERROR("error reading MDS %s offset %d: rc = %d\n",
207 LAST_RCVD, cl_off, rc);
213 if (mcd->mcd_uuid[0] == '\0') {
214 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
219 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
221 /* The exports are cleaned up by mds_disconnect, so they
222 * need to be set up like real exports also.
224 mount_age = last_mount - le64_to_cpu(mcd->mcd_mount_count);
225 if (last_rcvd && mount_age < MDS_MOUNT_RECOV) {
226 struct obd_export *exp = class_new_export(obddev);
227 struct mds_export_data *med;
234 med = &exp->exp_mds_data;
236 mds_client_add(mds, med, cl_off);
237 /* XXX put this in a helper if it gets more complex */
238 INIT_LIST_HEAD(&med->med_open_head);
239 spin_lock_init(&med->med_open_lock);
242 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 if (last_rcvd > mds->mds_last_rcvd) {
253 "client at offset %d has last_rcvd = %Lu\n",
254 cl_off, (unsigned long long)last_rcvd);
255 mds->mds_last_rcvd = last_rcvd;
259 mds->mds_last_committed = mds->mds_last_rcvd;
260 if (mds->mds_recoverable_clients) {
261 CERROR("need recovery: %d recoverable clients, last_rcvd %Lu\n",
262 mds->mds_recoverable_clients, mds->mds_last_rcvd);
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_pop, 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 void mds_fs_cleanup(struct obd_device *obddev)
360 struct mds_obd *mds = &obddev->u.mds;
362 class_disconnect_all(obddev); /* this cleans up client info too */
363 mds_server_free_data(mds);