Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / mds / mds_fs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_fs.c
5  *
6  *  Lustre Metadata Server (MDS) filesystem interface code
7  *
8  *  Copyright (C) 2002 Cluster File Systems, Inc.
9  *
10  *  This code is issued under the GNU General Public License.
11  *  See the file COPYING in this distribution
12  *
13  *  by Andreas Dilger <adilger@clusterfs.com>
14  *
15  */
16
17 #define EXPORT_SYMTAB
18 #define DEBUG_SUBSYSTEM S_MDS
19
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>
27
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))
31
32 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
33
34 #define LAST_RCVD "last_rcvd"
35
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
39  * we know its offset.
40  */
41 int mds_client_add(struct mds_obd *mds, struct mds_export_data *med, int cl_off)
42 {
43         int new_client = (cl_off == -1);
44
45         /* the bitmap operations can handle cl_off > sizeof(long) * 8, so
46          * there's no need for extra complication here
47          */
48         if (new_client) {
49                 cl_off = find_first_zero_bit(last_rcvd_slots, MDS_MAX_CLIENTS);
50         repeat:
51                 if (cl_off >= MDS_MAX_CLIENTS) {
52                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
53                         return -ENOMEM;
54                 }
55                 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
56                         CERROR("MDS client %d: found bit is set in bitmap\n",
57                                cl_off);
58                         cl_off = find_next_zero_bit(last_rcvd_slots,
59                                                     MDS_MAX_CLIENTS, cl_off);
60                         goto repeat;
61                 }
62         } else {
63                 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
64                         CERROR("MDS client %d: bit already set in bitmap!!\n",
65                                cl_off);
66                         LBUG();
67                 }
68         }
69
70         CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
71                cl_off, med->med_mcd->mcd_uuid);
72
73         med->med_off = cl_off;
74
75         if (new_client) {
76                 struct obd_run_ctxt saved;
77                 loff_t off = MDS_LR_CLIENT + (cl_off * MDS_LR_SIZE);
78                 ssize_t written;
79
80                 push_ctxt(&saved, &mds->mds_ctxt, NULL);
81                 written = lustre_fwrite(mds->mds_rcvd_filp,
82                                                 (char *)med->med_mcd,
83                                                 sizeof(*med->med_mcd), &off);
84                 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
85
86                 if (written != sizeof(*med->med_mcd)) {
87                         if (written < 0)
88                                 RETURN(written);
89                         RETURN(-EIO);
90                 }
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));
94         }
95         return 0;
96 }
97
98 int mds_client_free(struct obd_export *exp)
99 {
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;
104         int written;
105         loff_t off;
106
107         if (!med->med_mcd)
108                 RETURN(0);
109
110         off = MDS_LR_CLIENT + (med->med_off * MDS_LR_SIZE);
111
112         CDEBUG(D_INFO, "freeing client at offset %u (%lld)with UUID '%s'\n",
113                med->med_off, off, med->med_mcd->mcd_uuid);
114
115         if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
116                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
117                        med->med_off);
118                 LBUG();
119         }
120
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);
126
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,
130                        written);
131         } else {
132                 CDEBUG(D_INFO, "zeroed out disconnecting client %s at off %d\n",
133                        med->med_mcd->mcd_uuid, med->med_off);
134         }
135
136         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
137
138         return 0;
139 }
140
141 static int mds_server_free_data(struct mds_obd *mds)
142 {
143         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
144         mds->mds_server_data = NULL;
145
146         return 0;
147 }
148
149 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
150 {
151         struct mds_obd *mds = &obddev->u.mds;
152         struct mds_server_data *msd;
153         struct mds_client_data *mcd = NULL;
154         loff_t off = 0;
155         int cl_off;
156         unsigned long last_rcvd_size = f->f_dentry->d_inode->i_size;
157         __u64 last_rcvd = 0;
158         __u64 last_mount;
159         int rc = 0;
160
161         OBD_ALLOC(msd, sizeof(*msd));
162         if (!msd)
163                 RETURN(-ENOMEM);
164         rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
165
166         mds->mds_server_data = msd;
167         if (rc == 0) {
168                 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
169                 RETURN(0);
170         }
171
172         if (rc != sizeof(*msd)) {
173                 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
174                 if (rc > 0)
175                         rc = -EIO;
176                 GOTO(err_msd, rc);
177         }
178
179         CDEBUG(D_INODE, "last_rcvd has size %lu (msd + %lu clients)\n",
180                last_rcvd_size, (last_rcvd_size - sizeof *msd) / sizeof *mcd);
181
182         /*
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.
186          */
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);
190
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);
194
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++) {
197                 int mount_age;
198
199                 if (!mcd) {
200                         OBD_ALLOC(mcd, sizeof(*mcd));
201                         if (!mcd)
202                                 GOTO(err_msd, rc = -ENOMEM);
203                 }
204
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? */
210                                 rc = -EIO;
211                         break;
212                 }
213
214                 if (mcd->mcd_uuid[0] == '\0') {
215                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
216                                cl_off);
217                         continue;
218                 }
219
220                 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
221
222                 /* These exports are cleaned up by mds_disconnect(), so they
223                  * need to be set up like real exports as mds_connect() does.
224                  */
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;
229
230                         if (!exp) {
231                                 rc = -ENOMEM;
232                                 break;
233                         }
234
235                         med = &exp->exp_mds_data;
236                         med->med_mcd = mcd;
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);
241
242                         mcd = NULL;
243                         mds->mds_recoverable_clients++;
244                 } else {
245                         CDEBUG(D_INFO,
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));
249                 }
250
251                 CDEBUG(D_OTHER, "client at offset %d has last_rcvd = %Lu\n",
252                        cl_off, (unsigned long long)last_rcvd);
253
254                 if (last_rcvd > mds->mds_last_rcvd)
255                         mds->mds_last_rcvd = last_rcvd;
256         }
257
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;
263         }
264
265         if (mcd)
266                 OBD_FREE(mcd, sizeof(*mcd));
267
268         return 0;
269
270 err_msd:
271         mds_server_free_data(mds);
272         return rc;
273 }
274
275 static int mds_fs_prep(struct obd_device *obddev)
276 {
277         struct mds_obd *mds = &obddev->u.mds;
278         struct obd_run_ctxt saved;
279         struct dentry *dentry;
280         struct file *f;
281         int rc;
282
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);
288                 GOTO(err_pop, rc);
289         }
290
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;
294
295         dput(dentry);
296
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);
301                 GOTO(err_pop, rc);
302         }
303         /* XXX probably want to hold on to this later... */
304         dput(dentry);
305
306         f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
307         if (IS_ERR(f)) {
308                 rc = PTR_ERR(f);
309                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
310                 GOTO(err_pop, rc = PTR_ERR(f));
311         }
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);
316         }
317
318         rc = fsfilt_journal_data(obddev, f);
319         if (rc) {
320                 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
321                 GOTO(err_filp, rc);
322         }
323
324         rc = mds_read_last_rcvd(obddev, f);
325         if (rc) {
326                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
327                 GOTO(err_client, rc);
328         }
329         mds->mds_rcvd_filp = f;
330 err_pop:
331         pop_ctxt(&saved, &mds->mds_ctxt, NULL);
332
333         return rc;
334
335 err_client:
336         class_disconnect_all(obddev);
337 err_filp:
338         if (filp_close(f, 0))
339                 CERROR("can't close %s after error\n", LAST_RCVD);
340         goto err_pop;
341 }
342
343 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
344 {
345         struct mds_obd *mds = &obddev->u.mds;
346         ENTRY;
347
348         mds->mds_vfsmnt = mnt;
349
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();
354
355         RETURN(mds_fs_prep(obddev));
356 }
357
358 int mds_fs_cleanup(struct obd_device *obddev)
359 {
360         struct mds_obd *mds = &obddev->u.mds;
361         struct obd_run_ctxt saved;
362         int rc = 0;
363
364         class_disconnect_all(obddev); /* this cleans up client info too */
365         mds_server_free_data(mds);
366
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;
371
372                 if (rc)
373                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
374         }
375         pop_ctxt(&saved, &mds->mds_ctxt, NULL);
376
377         return rc;
378 }