Whamcloud - gitweb
- add ucred structure. MDS now sets the fsuid and fsgid to that of
[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                 /* Do client recovery here (open files, etc) */
178                 if (last_rcvd && (last_mount - le64_to_cpu(mcd->mcd_mount_count)
179                                   < MDS_MOUNT_RECOV)) {
180                         struct obd_export *export = class_new_export(obddev);
181                         if (!export) {
182                                 rc = -ENOMEM;
183                                 break;
184                         }
185                         export->exp_mds_data.med_mcd = mcd;
186                         mds_client_add(&export->exp_mds_data, cl_off);
187                         mcd = NULL;
188                         clients++;
189                 } else {
190                         CDEBUG(D_INFO,
191                                "ignored client %d, UUID '%s', last_mount %Ld\n",
192                                cl_off, mcd->mcd_uuid,
193                                (long long)le64_to_cpu(mcd->mcd_mount_count));
194                 }
195
196                 if (last_rcvd > mds->mds_last_rcvd) {
197                         CDEBUG(D_OTHER,
198                                "client at offset %d has last_rcvd = %Lu\n",
199                                cl_off, (unsigned long long)last_rcvd);
200                         mds->mds_last_rcvd = last_rcvd;
201                 }
202         }
203         CDEBUG(D_INODE, "got %Lu for highest last_rcvd value, %d/%d clients\n",
204                (unsigned long long)mds->mds_last_rcvd, clients, cl_off);
205
206         if (mcd)
207                 OBD_FREE(mcd, sizeof(*mcd));
208
209         /* After recovery, there can be no local uncommitted transactions */
210         mds->mds_last_committed = mds->mds_last_rcvd;
211
212         return 0;
213
214 err_msd:
215         mds_server_free_data(mds);
216         return rc;
217 }
218
219 static int mds_fs_prep(struct obd_device *obddev)
220 {
221         struct mds_obd *mds = &obddev->u.mds;
222         struct obd_run_ctxt saved;
223         struct dentry *dentry;
224         struct file *f;
225         int rc;
226
227         push_ctxt(&saved, &mds->mds_ctxt, NULL);
228         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
229         if (IS_ERR(dentry)) {
230                 rc = PTR_ERR(dentry);
231                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
232                 GOTO(err_pop, rc);
233         }
234
235         mds->mds_rootfid.id = dentry->d_inode->i_ino;
236         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
237         mds->mds_rootfid.f_type = S_IFDIR;
238
239         dput(dentry);
240
241         dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
242         if (IS_ERR(dentry)) {
243                 rc = PTR_ERR(dentry);
244                 CERROR("cannot create FH 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
250         f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
251         if (IS_ERR(f)) {
252                 rc = PTR_ERR(f);
253                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
254                 GOTO(err_pop, rc = PTR_ERR(f));
255         }
256         if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
257                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
258                        f->f_dentry->d_inode->i_mode);
259                 GOTO(err_pop, rc = -ENOENT);
260         }
261
262         rc = mds_fs_journal_data(mds, f);
263         if (rc) {
264                 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
265                 GOTO(err_filp, rc);
266         }
267
268         rc = mds_read_last_rcvd(obddev, f);
269         if (rc) {
270                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
271                 GOTO(err_client, rc);
272         }
273         mds->mds_rcvd_filp = f;
274 err_pop:
275         pop_ctxt(&saved);
276
277         return rc;
278
279 err_client:
280         class_disconnect_all(obddev);
281 err_filp:
282         if (filp_close(f, 0))
283                 CERROR("can't close %s after error\n", LAST_RCVD);
284         goto err_pop;
285 }
286
287 static struct mds_fs_operations *mds_search_fs_type(const char *name)
288 {
289         struct list_head *p;
290         struct mds_fs_type *type;
291
292         /* lock mds_fs_types list */
293         list_for_each(p, &mds_fs_types) {
294                 type = list_entry(p, struct mds_fs_type, mft_list);
295                 if (!strcmp(type->mft_name, name)) {
296                         /* unlock mds_fs_types list */
297                         return type->mft_ops;
298                 }
299         }
300         /* unlock mds_fs_types list */
301         return NULL;
302 }
303
304 int mds_register_fs_type(struct mds_fs_operations *ops, const char *name)
305 {
306         struct mds_fs_operations *found;
307         struct mds_fs_type *type;
308
309         if ((found = mds_search_fs_type(name))) {
310                 if (found != ops) {
311                         CERROR("different operations for type %s\n", name);
312                         RETURN(-EEXIST);
313                 }
314                 return 0;
315         }
316         OBD_ALLOC(type, sizeof(*type));
317         if (!type)
318                 RETURN(-ENOMEM);
319
320         INIT_LIST_HEAD(&type->mft_list);
321         type->mft_ops = ops;
322         type->mft_name = strdup(name);
323         if (!type->mft_name) {
324                 OBD_FREE(type, sizeof(*type));
325                 RETURN(-ENOMEM);
326         }
327         MOD_INC_USE_COUNT;
328         list_add(&type->mft_list, &mds_fs_types);
329
330         return 0;
331 }
332
333 void mds_unregister_fs_type(const char *name)
334 {
335         struct list_head *p;
336
337         /* lock mds_fs_types list */
338         list_for_each(p, &mds_fs_types) {
339                 struct mds_fs_type *type;
340
341                 type = list_entry(p, struct mds_fs_type, mft_list);
342                 if (!strcmp(type->mft_name, name)) {
343                         list_del(p);
344                         kfree(type->mft_name);
345                         OBD_FREE(type, sizeof(*type));
346                         MOD_DEC_USE_COUNT;
347                         break;
348                 }
349         }
350         /* unlock mds_fs_types list */
351 }
352
353 struct mds_fs_operations *mds_fs_get_ops(char *fstype)
354 {
355         struct mds_fs_operations *fs_ops;
356
357         if (!(fs_ops = mds_search_fs_type(fstype))) {
358                 char name[32];
359                 int rc;
360
361                 snprintf(name, sizeof(name) - 1, "mds_%s", fstype);
362                 name[sizeof(name) - 1] = '\0';
363
364                 if ((rc = request_module(name))) {
365                         fs_ops = mds_search_fs_type(fstype);
366                         CDEBUG(D_INFO, "Loaded module '%s'\n", name);
367                         if (!fs_ops)
368                                 rc = -ENOENT;
369                 }
370
371                 if (rc) {
372                         CERROR("Can't find MDS fs interface '%s'\n", name);
373                         RETURN(ERR_PTR(rc));
374                 }
375         }
376         __MOD_INC_USE_COUNT(fs_ops->fs_owner);
377
378         return fs_ops;
379 }
380
381 void mds_fs_put_ops(struct mds_fs_operations *fs_ops)
382 {
383         __MOD_DEC_USE_COUNT(fs_ops->fs_owner);
384 }
385
386 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
387 {
388         struct mds_obd *mds = &obddev->u.mds;
389         int rc;
390
391         mds->mds_fsops = mds_fs_get_ops(mds->mds_fstype);
392         if (IS_ERR(mds->mds_fsops))
393                 RETURN(PTR_ERR(mds->mds_fsops));
394
395         mds->mds_vfsmnt = mnt;
396
397         OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
398         mds->mds_ctxt.pwdmnt = mnt;
399         mds->mds_ctxt.pwd = mnt->mnt_root;
400         mds->mds_ctxt.fs = get_ds();
401
402         /*
403          * Replace the client filesystem delete_inode method with our own,
404          * so that we can clear the object ID before the inode is deleted.
405          * The fs_delete_inode method will call cl_delete_inode for us.
406          * We need to do this for the MDS superblock only, hence we install
407          * a modified copy of the original superblock method table.
408          *
409          * We still assume that there is only a single MDS client filesystem
410          * type, as we don't have access to the mds struct in delete_inode
411          * and store the client delete_inode method in a global table.  This
412          * will only become a problem if/when multiple MDSs are running on a
413          * single host with different underlying filesystems.
414          */
415         OBD_ALLOC(mds->mds_sop, sizeof(*mds->mds_sop));
416         if (!mds->mds_sop)
417                 GOTO(out_dec, rc = -ENOMEM);
418
419         memcpy(mds->mds_sop, mds->mds_sb->s_op, sizeof(*mds->mds_sop));
420         mds->mds_fsops->cl_delete_inode = mds->mds_sop->delete_inode;
421         mds->mds_sop->delete_inode = mds->mds_fsops->fs_delete_inode;
422         mds->mds_sb->s_op = mds->mds_sop;
423
424         rc = mds_fs_prep(obddev);
425
426         if (rc)
427                 GOTO(out_free, rc);
428
429         return 0;
430
431 out_free:
432         OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
433 out_dec:
434         mds_fs_put_ops(mds->mds_fsops);
435         return rc;
436 }
437
438 void mds_fs_cleanup(struct obd_device *obddev)
439 {
440         struct mds_obd *mds = &obddev->u.mds;
441
442         class_disconnect_all(obddev); /* this cleans up client info too */
443         mds_server_free_data(mds);
444
445         OBD_FREE(mds->mds_sop, sizeof(*mds->mds_sop));
446         mds_fs_put_ops(mds->mds_fsops);
447 }
448
449 EXPORT_SYMBOL(mds_register_fs_type);
450 EXPORT_SYMBOL(mds_unregister_fs_type);