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