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