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