Whamcloud - gitweb
land b_md onto HEAD. the highlights:
[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 #include <linux/lustre_fsfilt.h>
27
28 /* This limit is arbitrary, but for now we fit it in 1 page (32k clients) */
29 #define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
30 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
31
32 static unsigned long last_rcvd_slots[MDS_MAX_CLIENT_WORDS];
33
34 #define LAST_RCVD "last_rcvd"
35
36 /* Add client data to the MDS.  We use a bitmap to locate a free space
37  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
38  * Otherwise, we have just read the data from the last_rcvd file and
39  * we know its offset.
40  */
41 int mds_client_add(struct mds_obd *mds, struct mds_export_data *med, int cl_off)
42 {
43         int new_client = (cl_off == -1);
44
45         /* the bitmap operations can handle cl_off > sizeof(long) * 8, so
46          * there's no need for extra complication here
47          */
48         if (new_client) {
49                 cl_off = find_first_zero_bit(last_rcvd_slots, MDS_MAX_CLIENTS);
50         repeat:
51                 if (cl_off >= MDS_MAX_CLIENTS) {
52                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
53                         return -ENOMEM;
54                 }
55                 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
56                         CERROR("MDS client %d: found bit is set in bitmap\n",
57                                cl_off);
58                         cl_off = find_next_zero_bit(last_rcvd_slots,
59                                                     MDS_MAX_CLIENTS, cl_off);
60                         goto repeat;
61                 }
62         } else {
63                 if (test_and_set_bit(cl_off, last_rcvd_slots)) {
64                         CERROR("MDS client %d: bit already set in bitmap!!\n",
65                                cl_off);
66                         LBUG();
67                 }
68         }
69
70         CDEBUG(D_INFO, "client at offset %d with UUID '%s' added\n",
71                cl_off, med->med_mcd->mcd_uuid);
72
73         med->med_off = cl_off;
74
75         if (new_client) {
76                 struct obd_run_ctxt saved;
77                 loff_t off = MDS_LR_CLIENT + (cl_off * MDS_LR_SIZE);
78                 ssize_t written;
79
80                 push_ctxt(&saved, &mds->mds_ctxt, NULL);
81                 written = lustre_fwrite(mds->mds_rcvd_filp,
82                                                 (char *)med->med_mcd,
83                                                 sizeof(*med->med_mcd), &off);
84                 pop_ctxt(&saved, &mds->mds_ctxt, NULL);
85
86                 if (written != sizeof(*med->med_mcd)) {
87                         if (written < 0)
88                                 RETURN(written);
89                         RETURN(-EIO);
90                 }
91         }
92         return 0;
93 }
94
95 int mds_client_free(struct obd_export *exp)
96 {
97         struct mds_export_data *med = &exp->exp_mds_data;
98         struct mds_obd *mds = &exp->exp_obd->u.mds;
99         struct mds_client_data zero_mcd;
100         struct obd_run_ctxt saved;
101         int written;
102         loff_t off;
103
104         if (!med->med_mcd)
105                 RETURN(0);
106
107         CDEBUG(D_INFO, "freeing client at offset %d with UUID '%s'\n",
108                med->med_off, med->med_mcd->mcd_uuid);
109
110         if (!test_and_clear_bit(med->med_off, last_rcvd_slots)) {
111                 CERROR("MDS client %d: bit already clear in bitmap!!\n",
112                        med->med_off);
113                 LBUG();
114         }
115
116         off = med->med_off;
117
118         memset(&zero_mcd, 0, sizeof zero_mcd);
119         push_ctxt(&saved, &mds->mds_ctxt, NULL);
120         written = lustre_fwrite(mds->mds_rcvd_filp, (const char *)&zero_mcd,
121                                 sizeof zero_mcd, &off);
122         pop_ctxt(&saved, &mds->mds_ctxt, NULL);
123
124         if (written != sizeof zero_mcd) {
125                 CERROR("error zeroing out client %s off %d in %s: %d\n",
126                        med->med_mcd->mcd_uuid, med->med_off, LAST_RCVD,
127                        written);
128                 LBUG();
129         } else {
130                 CDEBUG(D_INFO, "zeroed out disconnecting client %s at off %d\n",
131                        med->med_mcd->mcd_uuid, med->med_off);
132         }
133
134         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
135
136         return 0;
137 }
138
139 static int mds_server_free_data(struct mds_obd *mds)
140 {
141         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
142         mds->mds_server_data = NULL;
143
144         return 0;
145 }
146
147 static int mds_read_last_rcvd(struct obd_device *obddev, struct file *f)
148 {
149         struct mds_obd *mds = &obddev->u.mds;
150         struct mds_server_data *msd;
151         struct mds_client_data *mcd = NULL;
152         loff_t off = 0;
153         int cl_off;
154         int max_off = f->f_dentry->d_inode->i_size / sizeof(*mcd);
155         __u64 last_rcvd = 0;
156         __u64 last_mount;
157         int rc = 0;
158
159         OBD_ALLOC(msd, sizeof(*msd));
160         if (!msd)
161                 RETURN(-ENOMEM);
162         rc = lustre_fread(f, (char *)msd, sizeof(*msd), &off);
163
164         mds->mds_server_data = msd;
165         if (rc == 0) {
166                 CERROR("empty MDS %s, new MDS?\n", LAST_RCVD);
167                 RETURN(0);
168         }
169
170         if (rc != sizeof(*msd)) {
171                 CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
172                 if (rc > 0) {
173                         rc = -EIO;
174                 }
175                 GOTO(err_msd, rc);
176         }
177
178         /*
179          * When we do a clean MDS shutdown, we save the last_rcvd into
180          * the header.  If we find clients with higher last_rcvd values
181          * then those clients may need recovery done.
182          */
183         last_rcvd = le64_to_cpu(msd->msd_last_rcvd);
184         mds->mds_last_rcvd = last_rcvd;
185         CDEBUG(D_INODE, "got %Lu for server last_rcvd value\n",
186                (unsigned long long)last_rcvd);
187
188         last_mount = le64_to_cpu(msd->msd_mount_count);
189         mds->mds_mount_count = last_mount;
190         CDEBUG(D_INODE, "got %Lu for server last_mount value\n",
191                (unsigned long long)last_mount);
192
193         for (off = MDS_LR_CLIENT, cl_off = 0;
194              off < max_off;
195              off += MDS_LR_SIZE, cl_off++) {
196                 int mount_age;
197
198                 if (!mcd) {
199                         OBD_ALLOC(mcd, sizeof(*mcd));
200                         if (!mcd)
201                                 GOTO(err_msd, rc = -ENOMEM);
202                 }
203
204                 rc = lustre_fread(f, (char *)mcd, sizeof(*mcd), &off);
205                 if (rc != sizeof(*mcd)) {
206                         CERROR("error reading MDS %s offset %d: rc = %d\n",
207                                LAST_RCVD, cl_off, rc);
208                         if (rc > 0)
209                                 rc = -EIO;
210                         break;
211                 }
212
213                 if (mcd->mcd_uuid[0] == '\0') {
214                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
215                                cl_off);
216                         continue;
217                 }
218
219                 last_rcvd = le64_to_cpu(mcd->mcd_last_rcvd);
220
221                 /* The exports are cleaned up by mds_disconnect, so they
222                  * need to be set up like real exports also.
223                  */
224                 mount_age = last_mount - le64_to_cpu(mcd->mcd_mount_count);
225                 if (last_rcvd && mount_age < MDS_MOUNT_RECOV) {
226                         struct obd_export *exp = class_new_export(obddev);
227                         struct mds_export_data *med;
228
229                         if (!exp) {
230                                 rc = -ENOMEM;
231                                 break;
232                         }
233
234                         med = &exp->exp_mds_data;
235                         med->med_mcd = mcd;
236                         mds_client_add(mds, med, cl_off);
237                         /* XXX put this in a helper if it gets more complex */
238                         INIT_LIST_HEAD(&med->med_open_head);
239                         spin_lock_init(&med->med_open_lock);
240
241                         mcd = NULL;
242                         mds->mds_recoverable_clients++;
243                         MOD_INC_USE_COUNT;
244                 } else {
245                         CDEBUG(D_INFO,
246                                "discarded client %d, UUID '%s', count %Ld\n",
247                                cl_off, mcd->mcd_uuid,
248                                (long long)le64_to_cpu(mcd->mcd_mount_count));
249                 }
250
251                 if (last_rcvd > mds->mds_last_rcvd) {
252                         CDEBUG(D_OTHER,
253                                "client at offset %d has last_rcvd = %Lu\n",
254                                cl_off, (unsigned long long)last_rcvd);
255                         mds->mds_last_rcvd = last_rcvd;
256                 }
257         }
258
259         mds->mds_last_committed = mds->mds_last_rcvd;
260         if (mds->mds_recoverable_clients) {
261                 CERROR("need recovery: %d recoverable clients, last_rcvd %Lu\n",
262                        mds->mds_recoverable_clients, mds->mds_last_rcvd);
263         }
264
265         if (mcd)
266                 OBD_FREE(mcd, sizeof(*mcd));
267
268         return 0;
269
270 err_msd:
271         mds_server_free_data(mds);
272         return rc;
273 }
274
275 static int mds_fs_prep(struct obd_device *obddev)
276 {
277         struct mds_obd *mds = &obddev->u.mds;
278         struct obd_run_ctxt saved;
279         struct dentry *dentry;
280         struct file *f;
281         int rc;
282
283         push_ctxt(&saved, &mds->mds_ctxt, NULL);
284         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755);
285         if (IS_ERR(dentry)) {
286                 rc = PTR_ERR(dentry);
287                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
288                 GOTO(err_pop, rc);
289         }
290
291         mds->mds_rootfid.id = dentry->d_inode->i_ino;
292         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
293         mds->mds_rootfid.f_type = S_IFDIR;
294
295         dput(dentry);
296
297         dentry = simple_mkdir(current->fs->pwd, "FH", 0700);
298         if (IS_ERR(dentry)) {
299                 rc = PTR_ERR(dentry);
300                 CERROR("cannot create FH directory: rc = %d\n", rc);
301                 GOTO(err_pop, rc);
302         }
303         /* XXX probably want to hold on to this later... */
304         dput(dentry);
305
306         f = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
307         if (IS_ERR(f)) {
308                 rc = PTR_ERR(f);
309                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
310                 GOTO(err_pop, rc = PTR_ERR(f));
311         }
312         if (!S_ISREG(f->f_dentry->d_inode->i_mode)) {
313                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
314                        f->f_dentry->d_inode->i_mode);
315                 GOTO(err_pop, rc = -ENOENT);
316         }
317
318         rc = fsfilt_journal_data(obddev, f);
319         if (rc) {
320                 CERROR("cannot journal data on %s: rc = %d\n", LAST_RCVD, rc);
321                 GOTO(err_filp, rc);
322         }
323
324         rc = mds_read_last_rcvd(obddev, f);
325         if (rc) {
326                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
327                 GOTO(err_client, rc);
328         }
329         mds->mds_rcvd_filp = f;
330 err_pop:
331         pop_ctxt(&saved, &mds->mds_ctxt, NULL);
332
333         return rc;
334
335 err_client:
336         class_disconnect_all(obddev);
337 err_filp:
338         if (filp_close(f, 0))
339                 CERROR("can't close %s after error\n", LAST_RCVD);
340         goto err_pop;
341 }
342
343 int mds_fs_setup(struct obd_device *obddev, struct vfsmount *mnt)
344 {
345         struct mds_obd *mds = &obddev->u.mds;
346         ENTRY;
347
348         mds->mds_vfsmnt = mnt;
349
350         OBD_SET_CTXT_MAGIC(&mds->mds_ctxt);
351         mds->mds_ctxt.pwdmnt = mnt;
352         mds->mds_ctxt.pwd = mnt->mnt_root;
353         mds->mds_ctxt.fs = get_ds();
354
355         RETURN(mds_fs_prep(obddev));
356 }
357
358 void mds_fs_cleanup(struct obd_device *obddev)
359 {
360         struct mds_obd *mds = &obddev->u.mds;
361
362         class_disconnect_all(obddev); /* this cleans up client info too */
363         mds_server_free_data(mds);
364 }