Whamcloud - gitweb
Do proper setup/cleanup of MDS exports and client data.
[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
27 LIST_HEAD(mds_fs_types);
28
29 struct mds_fs_type {
30         struct list_head                 mft_list;
31         struct mds_fs_operations        *mft_ops;
32         char                            *mft_name;
33 };
34
35 #define MDS_MAX_CLIENTS 1024
36 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
37
38 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
39
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
43  * we know its offset.
44  */
45 int mds_client_add(struct mds_export_data *med, int cl_off)
46 {
47         if (cl_off == -1) {
48                 unsigned long *word;
49                 int bit;
50
51         repeat:
52                 word = last_rcvd_slots;
53                 while(*word == ~0UL)
54                         ++word;
55                 if (word - last_rcvd_slots >= MDS_MAX_CLIENT_WORDS) {
56                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
57                         return -ENOMEM;
58                 }
59                 bit = ffz(*word);
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);
63                         LBUG();
64                         goto repeat;
65                 }
66                 cl_off = word - last_rcvd_slots + bit;
67         } else {
68                 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
69                         CERROR("bit %d already set in bitmap - bad bad\n",
70                                cl_off);
71                         LBUG();
72                 }
73         }
74
75         CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
76                cl_off, med->med_mcd->mcd_uuid);
77
78         med->med_off = cl_off;
79         return 0;
80 }
81
82 int mds_client_free(struct mds_export_data *med)
83 {
84         unsigned long *word;
85         int bit;
86
87         CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
88                med->med_off, med->med_mcd->mcd_uuid);
89
90         word = last_rcvd_slots + med->med_off / sizeof(unsigned long);
91         bit = med->med_off % sizeof(unsigned long);
92
93         if (!test_and_clear_bit(bit, word)) {
94                 CERROR("bit %d already clear in word %d - bad bad\n",
95                        bit, word - last_rcvd_slots);
96                 LBUG();
97         }
98
99         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
100
101         return 0;
102 }
103
104 static int mds_server_free_data(struct mds_obd *mds)
105 {
106         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
107         mds->mds_server_data = NULL;
108
109         return 0;
110 }
111
112 #define LAST_RCVD "last_rcvd"
113
114 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
115 {
116         struct mds_obd *mds = &obddev->u.mds;
117         struct mds_server_data *msd;
118         struct mds_client_data *mcd = NULL;
119         loff_t fsize = f->f_dentry->d_inode->i_size;
120         loff_t off = 0;
121         int cl_off;
122         __u64 last_rcvd = 0;
123         __u64 last_mount;
124         int clients = 0;
125         int rc = 0;
126
127         OBD_ALLOC(msd, sizeof(*msd));
128         if (!msd)
129                 RETURN(-ENOMEM);
130         rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
131
132         mds->mds_server_data = msd;
133         if (rc == 0) {
134                 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
135                 RETURN(0);
136         }
137
138         if (rc != sizeof(*msd)) {
139                 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
140                 if (rc > 0) {
141                         rc = -EIO;
142                 }
143                 GOTO(err_msd, rc);
144         }
145
146         /*
147          * When we do a clean MDS shutdown, we save the last_rcvd into
148          * the header.  If we find clients with higher last_rcvd values
149          * then those clients may need recovery done.
150          */
151         last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
152         mds->mds_last_rcvd = last_rcvd;
153         CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
154                (unsigned long long)last_rcvd);
155
156         last_mount = le64_to_cpu(msd->msd_mount_count);
157         mds->mds_mount_count = last_mount;
158         CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
159                (unsigned long long)last_mount);
160
161         for (off = MDS_LR_CLIENT, cl_off = 0, rc = sizeof(*mcd);
162              off <= fsize - sizeof(*mcd) && rc == sizeof(*mcd);
163              off = MDS_LR_CLIENT + ++cl_off * MDS_LR_SIZE) {
164                 if (!mcd) {
165                         OBD_ALLOC(mcd, sizeof(*mcd));
166                         if (!mcd)
167                                 GOTO(err_msd, rc = -ENOMEM);
168                 }
169
170                 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
171                 if (rc != sizeof(*mcd)) {
172                         CERROR("error reading MDS %s offset %d: rc = %d\n",
173                                LAST_RCVD, cl_off, rc);
174                         if (rc > 0)
175                                 rc = -EIO;
176                         break;
177                 }
178
179                 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
180
181                 /* Do client recovery here (open files, etc) */
182                 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
183                                   < MDS_MOUNT_RECOV)) {
184                         struct obd_export *export = class_new_export(obddev);
185                         if (!export) {
186                                 rc = -ENOMEM;
187                                 break;
188                         }
189                         export->exp_mds_data.med_mcd = mcd;
190                         mds_client_add(&export->exp_mds_data, cl_off);
191                         mcd = NULL;
192                         clients++;
193                 } else {
194                         CDEBUG(D_INFO,
195                                "ignored client %d, UUID '%s', last_mount %Ld\n",
196                                cl_off, mcd->mcd_uuid,
197                                (long long)le64_to_cpu(mcd->mcd_mount_count));
198                 }
199
200                 if (last_rcvd > mds->mds_last_rcvd) {
201                         CDEBUG(D_OTHER,
202                                "client at offset %d has last_rcvd = %Lu\n",
203                                cl_off, (unsigned long long)last_rcvd);
204                         mds->mds_last_rcvd = last_rcvd;
205                 }
206         }
207         CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
208                (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
209
210         if (mcd)
211                 OBD_FREE(mcd, sizeof(*mcd));
212
213         /* After recovery, there can be no local uncommitted transactions */
214         mds->mds_last_committed = mds->mds_last_rcvd;
215
216         return 0;
217
218 err_msd:
219         mds_server_free_data(mds);
220         return rc;
221 }
222
223 static int mds_fs_prep(struct obd_device *obddev)
224 {
225         struct mds_obd *mds = &obddev->u.mds;
226         struct obd_run_ctxt saved;
227         struct dentry *dentry;
228         struct file *f;
229         int rc;
230
231         push_ctxt(&saved, &mds->mds_ctxt);
232         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
233         if (IS_ERR(dentry)) {
234                 rc = PTR_ERR(dentry);
235                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
236                 GOTO(err_pop, rc);
237         }
238
239         mds->mds_rootfid.id = dentry->d_inode->i_ino;
240         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
241         mds->mds_rootfid.f_type = S_IFDIR;
242
243         dput(dentry);
244
245         dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
246         if (IS_ERR(dentry)) {
247                 rc = PTR_ERR(dentry);
248                 CERROR("cannot create FH directory: rc = %d\n", rc);
249                 GOTO(err_pop, rc);
250         }
251         /* XXX probably want to hold on to this later... */
252         dput(dentry);
253
254         f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
255         if (IS_ERR(f)) {
256                 rc = PTR_ERR(f);
257                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
258                 GOTO(err_pop, rc = PTR_ERR(f));
259         }
260         if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
261                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
262                        f->f_dentry->d_inode->i_mode);
263                 GOTO(err_pop, rc = -ENOENT);
264         }
265
266         rc = mds_fs_journal_data(mds, f);
267         if (rc) {
268                 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
269                 GOTO(err_filp, rc);
270         }
271
272         rc = mds_read_last_rcvd(obddev, f);
273         if (rc) {
274                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
275                 GOTO(err_client, rc);
276         }
277         mds->mds_rcvd_filp = f;
278 err_pop:
279         pop_ctxt(&saved);
280
281         return rc;
282
283 err_client:
284         class_disconnect_all(obddev);
285 err_filp:
286         if (filp_close(f, 0))
287                 CERROR("can't close %s after error\n", LAST_RCVD);
288         goto err_pop;
289 }
290
291 static struct mds_fs_operations *mds_search_fs_type(const char *name)
292 {
293         struct list_head *p;
294         struct mds_fs_type *type;
295
296         /* lock mds_fs_types list */
297         list_for_each(p, &mds_fs_types) {
298                 type = list_entry(p, struct mds_fs_type, mft_list);
299                 if (!strcmp(type->mft_name, name)) {
300                         /* unlock mds_fs_types list */
301                         return type->mft_ops;
302                 }
303         }
304         /* unlock mds_fs_types list */
305         return NULL;
306 }
307
308 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
309 {
310         struct mds_fs_operations *found;
311         struct mds_fs_type *type;
312
313         if ((found = mds_search_fs_type(name))) {
314                 if (found != ops) {
315                         CERROR("different operations for type %s\n", name);
316                         RETURN(-EEXIST);
317                 }
318                 return 0;
319         }
320         OBD_ALLOC(type, sizeof(*type));
321         if (!type)
322                 RETURN(-ENOMEM);
323
324         INIT_LIST_HEAD(&type->mft_list);
325         type->mft_ops = ops;
326         type->mft_name = strdup(name);
327         if (!type->mft_name) {
328                 OBD_FREE(type, sizeof(*type));
329                 RETURN(-ENOMEM);
330         }
331         MOD_INC_USE_COUNT;
332         list_add(&type->mft_list, &mds_fs_types);
333
334         return 0;
335 }
336
337 void mds_unregister_fs_type(const char *name)
338 {
339         struct list_head *p;
340
341         /* lock mds_fs_types list */
342         list_for_each(p, &mds_fs_types) {
343                 struct mds_fs_type *type;
344
345                 type = list_entry(p, struct mds_fs_type, mft_list);
346                 if (!strcmp(type->mft_name, name)) {
347                         list_del(p);
348                         kfree(type->mft_name);
349                         OBD_FREE(type, sizeof(*type));
350                         MOD_DEC_USE_COUNT;
351                         break;
352                 }
353         }
354         /* unlock mds_fs_types list */
355 }
356
357 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
358 {
359         struct mds_fs_operations *fs_ops;
360
361         if (!(fs_ops = mds_search_fs_type(fstype))) {
362                 char name[32];
363                 int rc;
364
365                 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
366                 name[sizeof(name) - 1] = '\0';
367
368                 if ((rc = request_module(name))) {
369                         fs_ops = mds_search_fs_type(fstype);
370                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
371                         if (!fs_ops)
372                                 rc = -ENOENT;
373                 }
374
375                 if (rc) {
376                         CERROR("Can't find MDS fs interface '%s'\n", name);
377                         RETURN(ERR_PTR(rc));
378                 }
379         }
380         __MOD_INC_USE_COUNT(fs_ops->fs_owner);
381
382         return fs_ops;
383 }
384
385 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
386 {
387         __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
388 }
389
390 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
391 {
392         struct mds_obd *mds = &obddev->u.mds;
393         int rc;
394
395         mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
396         if (IS_ERR(mds->mds_fsops))
397                 RETURN(PTR_ERR(mds->mds_fsops));
398
399         mds->mds_vfsmnt = mnt;
400
401         OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
402         mds->mds_ctxt.pwdmnt = mnt;
403         mds->mds_ctxt.pwd = mnt->mnt_root;
404         mds->mds_ctxt.fs = get_ds();
405
406         /*
407          * Replace the client filesystem delete_inode method with our own,
408          * so that we can clear the object ID before the inode is deleted.
409          * The fs_delete_inode method will call cl_delete_inode for us.
410          * We need to do this for the MDS superblock only, hence we install
411          * a modified copy of the original superblock method table.
412          *
413          * We still assume that there is only a single MDS client filesystem
414          * type, as we don't have access to the mds struct in delete_inode
415          * and store the client delete_inode method in a global table.  This
416          * will only become a problem if/when multiple MDSs are running on a
417          * single host with different underlying filesystems.
418          */
419         OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
420         if (!mds->mds_sop)
421                 GOTO(out_dec, rc = -ENOMEM);
422
423         memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
424         mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
425         mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
426         mds->mds_sb->s_op = mds->mds_sop;
427
428         rc = mds_fs_prep(obddev);
429
430         if (rc)
431                 GOTO(out_free, rc);
432
433         return 0;
434
435 out_free:
436         OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
437 out_dec:
438         mds_fs_put_ops(mds->mds_fsops);
439         return rc;
440 }
441
442 void mds_fs_cleanup(struct obd_device *obddev)
443 {
444         struct mds_obd *mds = &obddev->u.mds;
445
446         class_disconnect_all(obddev); /* this cleans up client info too */
447         mds_server_free_data(mds);
448
449         OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
450         mds_fs_put_ops(mds->mds_fsops);
451 }
452
453 EXPORT_SYMBOL(mds_register_fs_type);
454 EXPORT_SYMBOL(mds_unregister_fs_type);