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