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