Whamcloud - gitweb
4b42c7ee54b9eee2370a23b9b56efbdc5ab6e82c
[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  *  mds/mds_fs.c
5  *  Lustre Metadata Server (MDS) filesystem interface code
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <linux/module.h>
35 #include <linux/kmod.h>
36 #include <linux/version.h>
37 #include <linux/sched.h>
38 #include <linux/lustre_quota.h>
39 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
40 #include <linux/mount.h>
41 #endif
42 #include <linux/lustre_mds.h>
43 #include <linux/obd_class.h>
44 #include <linux/obd_support.h>
45 #include <linux/lustre_lib.h>
46 #include <linux/lustre_fsfilt.h>
47 #include <linux/lustre_disk.h>
48 #include <libcfs/list.h>
49
50 #include "mds_internal.h"
51
52 /* This limit is arbitrary (32k clients on x86), but it is convenient to use
53  * 2^n * PAGE_SIZE * 8 for the number of bits that fit an order-n allocation. */
54 #define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
55
56 #define LAST_RCVD "last_rcvd"
57 #define LOV_OBJID "lov_objid"
58 #define HEALTH_CHECK "health_check"
59
60 /* Add client data to the MDS.  We use a bitmap to locate a free space
61  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
62  * Otherwise, we have just read the data from the last_rcvd file and
63  * we know its offset.
64  *
65  * It should not be possible to fail adding an existing client - otherwise
66  * mds_init_server_data() callsite needs to be fixed.
67  */
68 int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
69                    struct mds_export_data *med, int cl_idx)
70 {
71         unsigned long *bitmap = mds->mds_client_bitmap;
72         int new_client = (cl_idx == -1);
73         ENTRY;
74
75         LASSERT(bitmap != NULL);
76         LASSERTF(cl_idx > -2, "%d\n", cl_idx);
77
78         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
79         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
80                 RETURN(0);
81
82         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
83          * there's no need for extra complication here
84          */
85         if (new_client) {
86                 cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
87         repeat:
88                 if (cl_idx >= MDS_MAX_CLIENTS ||
89                     OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_CLIENT_ADD)) {
90                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
91                         return -EOVERFLOW;
92                 }
93                 if (test_and_set_bit(cl_idx, bitmap)) {
94                         cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
95                                                     cl_idx);
96                         goto repeat;
97                 }
98         } else {
99                 if (test_and_set_bit(cl_idx, bitmap)) {
100                         CERROR("MDS client %d: bit already set in bitmap!!\n",
101                                cl_idx);
102                         LBUG();
103                 }
104         }
105
106         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
107                cl_idx, med->med_mcd->mcd_uuid);
108
109         med->med_lr_idx = cl_idx;
110         med->med_lr_off = le32_to_cpu(mds->mds_server_data->lsd_client_start) +
111                 (cl_idx * le16_to_cpu(mds->mds_server_data->lsd_client_size));
112         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
113
114         if (new_client) {
115                 struct lvfs_run_ctxt saved;
116                 loff_t off = med->med_lr_off;
117                 struct file *file = mds->mds_rcvd_filp;
118                 int rc;
119
120                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
121                 rc = fsfilt_write_record(obd, file, med->med_mcd,
122                                          sizeof(*med->med_mcd), &off, 1);
123                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
124
125                 if (rc)
126                         return rc;
127                 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
128                        med->med_lr_idx, med->med_lr_off,
129                        (unsigned int)sizeof(*med->med_mcd));
130         }
131         return 0;
132 }
133
134 int mds_client_free(struct obd_export *exp)
135 {
136         struct mds_export_data *med = &exp->exp_mds_data;
137         struct mds_obd *mds = &exp->exp_obd->u.mds;
138         struct obd_device *obd = exp->exp_obd;
139         struct mds_client_data zero_mcd;
140         struct lvfs_run_ctxt saved;
141         int rc;
142         loff_t off;
143         ENTRY;
144
145         if (!med->med_mcd)
146                 RETURN(0);
147
148         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
149         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
150                 GOTO(free, 0);
151
152         CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
153                med->med_lr_idx, med->med_lr_off, med->med_mcd->mcd_uuid);
154
155         LASSERT(mds->mds_client_bitmap != NULL);
156
157         off = med->med_lr_off;
158
159         /* Don't clear med_lr_idx here as it is likely also unset.  At worst
160          * we leak a client slot that will be cleaned on the next recovery. */
161         if (off <= 0) {
162                 CERROR("%s: client idx %d has offset %lld\n",
163                         obd->obd_name, med->med_lr_idx, off);
164                 GOTO(free, rc = -EINVAL);
165         }
166
167         /* Clear the bit _after_ zeroing out the client so we don't
168            race with mds_client_add and zero out new clients.*/
169         if (!test_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
170                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
171                        med->med_lr_idx);
172                 LBUG();
173         }
174
175         if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
176                 memset(&zero_mcd, 0, sizeof zero_mcd);
177                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
178                 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
179                                          sizeof(zero_mcd), &off, 1);
180                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
181
182                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
183                        "zeroing out client %s idx %u in %s rc %d\n",
184                        med->med_mcd->mcd_uuid, med->med_lr_idx, LAST_RCVD, rc);
185         }
186
187         if (!test_and_clear_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
188                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
189                        med->med_lr_idx);
190                 LBUG();
191         }
192
193
194         /* Make sure the server's last_transno is up to date. Do this
195          * after the client is freed so we know all the client's
196          * transactions have been committed. */
197         mds_update_server_data(exp->exp_obd, 0);
198
199         EXIT;
200  free:
201         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
202         med->med_mcd = NULL;
203
204         return 0;
205 }
206
207 static int mds_server_free_data(struct mds_obd *mds)
208 {
209         OBD_FREE(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
210         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
211         mds->mds_server_data = NULL;
212
213         return 0;
214 }
215
216 static int mds_init_server_data(struct obd_device *obd, struct file *file)
217 {
218         struct mds_obd *mds = &obd->u.mds;
219         struct lr_server_data *lsd;
220         struct mds_client_data *mcd = NULL;
221         loff_t off = 0;
222         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
223         __u64 mount_count;
224         int cl_idx, rc = 0;
225         ENTRY;
226
227         /* ensure padding in the struct is the correct size */
228         LASSERT(offsetof(struct lr_server_data, lsd_padding) +
229                 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
230         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
231                 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
232         LASSERT(MDS_LR_CLIENT_SIZE == LR_CLIENT_SIZE);
233         LASSERT(MDS_LR_CLIENT_START == LR_CLIENT_START);
234
235         OBD_ALLOC_WAIT(lsd, sizeof(*lsd));
236         if (!lsd)
237                 RETURN(-ENOMEM);
238
239         OBD_ALLOC_WAIT(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
240         if (!mds->mds_client_bitmap) {
241                 OBD_FREE(lsd, sizeof(*lsd));
242                 RETURN(-ENOMEM);
243         }
244
245         mds->mds_server_data = lsd;
246
247         if (last_rcvd_size == 0) {
248                 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
249
250                 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(lsd->lsd_uuid));
251                 lsd->lsd_last_transno = 0;
252                 mount_count = lsd->lsd_mount_count = 0;
253                 lsd->lsd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
254                 lsd->lsd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
255                 lsd->lsd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
256                 lsd->lsd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
257         } else {
258                 rc = fsfilt_read_record(obd, file, lsd, sizeof(*lsd), &off);
259                 if (rc) {
260                         CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
261                         GOTO(err_msd, rc);
262                 }
263                 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
264                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
265                                obd->obd_uuid.uuid, lsd->lsd_uuid);
266                         GOTO(err_msd, rc = -EINVAL);
267                 }
268                 mount_count = le64_to_cpu(lsd->lsd_mount_count);
269         }
270         if (lsd->lsd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
271                 CERROR("unsupported incompat feature %x\n",
272                        le32_to_cpu(lsd->lsd_feature_incompat) &
273                        ~MDS_INCOMPAT_SUPP);
274                 GOTO(err_msd, rc = -EINVAL);
275         }
276
277         if (lsd->lsd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
278                 CERROR("unsupported read-only feature %x\n",
279                        le32_to_cpu(lsd->lsd_feature_rocompat) &
280                        ~MDS_ROCOMPAT_SUPP);
281                 /* Do something like remount filesystem read-only */
282                 GOTO(err_msd, rc = -EINVAL);
283         }
284
285         if (lsd->lsd_feature_compat & ~cpu_to_le32(LR_COMPAT_COMMON_LR)) {
286                 CERROR("old last_rcvd format, updating\n");
287                 lsd->lsd_mount_count = lsd->lsd_last_transno; //msd->msd_mount_count
288                 lsd->lsd_last_transno = lsd->lsd_unused; //msd->msd_last_transno;
289                 lsd->lsd_feature_compat |= cpu_to_le32(LR_COMPAT_COMMON_LR);
290                 GOTO(err_msd, rc = -EINVAL);
291         }
292
293         mds->mds_last_transno = le64_to_cpu(lsd->lsd_last_transno);
294
295         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
296                obd->obd_name, mds->mds_last_transno);
297         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
298                obd->obd_name, mount_count + 1);
299         CDEBUG(D_INODE, "%s: server data size: %u\n",
300                obd->obd_name, le32_to_cpu(lsd->lsd_server_size));
301         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
302                obd->obd_name, le32_to_cpu(lsd->lsd_client_start));
303         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
304                obd->obd_name, le32_to_cpu(lsd->lsd_client_size));
305         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
306                obd->obd_name, last_rcvd_size);
307         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
308                last_rcvd_size <= le32_to_cpu(lsd->lsd_client_start) ? 0 :
309                (last_rcvd_size - le32_to_cpu(lsd->lsd_client_start)) /
310                 le16_to_cpu(lsd->lsd_client_size));
311
312         if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
313             !lsd->lsd_client_size) {
314                 CERROR("Bad last_rcvd contents!\n");
315                 GOTO(err_msd, rc = -EINVAL);
316         }
317
318         /* When we do a clean MDS shutdown, we save the last_transno into
319          * the header.  If we find clients with higher last_transno values
320          * then those clients may need recovery done. */
321         for (cl_idx = 0, off = le32_to_cpu(lsd->lsd_client_start);
322              off < last_rcvd_size; cl_idx++) {
323                 __u64 last_transno;
324                 struct obd_export *exp;
325                 struct mds_export_data *med;
326
327                 if (!mcd) {
328                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
329                         if (!mcd)
330                                 GOTO(err_client, rc = -ENOMEM);
331                 }
332
333                 /* Don't assume off is incremented properly by
334                  * fsfilt_read_record(), in case sizeof(*mcd)
335                  * isn't the same as lsd->lsd_client_size.  */
336                 off = le32_to_cpu(lsd->lsd_client_start) +
337                         cl_idx * le16_to_cpu(lsd->lsd_client_size);
338                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
339                 if (rc) {
340                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
341                                LAST_RCVD, cl_idx, off, rc);
342                         break; /* read error shouldn't cause startup to fail */
343                 }
344
345                 if (mcd->mcd_uuid[0] == '\0') {
346                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
347                                cl_idx);
348                         continue;
349                 }
350
351                 last_transno = le64_to_cpu(mcd->mcd_last_transno);
352
353                 /* These exports are cleaned up by mds_disconnect(), so they
354                  * need to be set up like real exports as mds_connect() does.
355                  */
356                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
357                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
358                        last_transno, le64_to_cpu(lsd->lsd_last_transno),
359                        le64_to_cpu(mcd->mcd_last_xid));
360
361                 exp = class_new_export(obd);
362                 if (exp == NULL)
363                         GOTO(err_client, rc = -ENOMEM);
364
365                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
366                        sizeof exp->exp_client_uuid.uuid);
367                 med = &exp->exp_mds_data;
368                 med->med_mcd = mcd;
369                 rc = mds_client_add(obd, mds, med, cl_idx);
370                 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
371
372                 /* create helper if export init gets more complex */
373                 INIT_LIST_HEAD(&med->med_open_head);
374                 spin_lock_init(&med->med_open_lock);
375
376                 mcd = NULL;
377                 exp->exp_replay_needed = 1;
378                 obd->obd_recoverable_clients++;
379                 obd->obd_max_recoverable_clients++;
380                 class_export_put(exp);
381
382                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
383                        cl_idx, last_transno);
384
385                 if (last_transno > mds->mds_last_transno)
386                         mds->mds_last_transno = last_transno;
387         }
388
389         if (mcd)
390                 OBD_FREE(mcd, sizeof(*mcd));
391
392         obd->obd_last_committed = mds->mds_last_transno;
393
394         if (obd->obd_recoverable_clients) {
395                 CWARN("RECOVERY: service %s, %d recoverable clients, "
396                       "last_transno "LPU64"\n", obd->obd_name,
397                       obd->obd_recoverable_clients, mds->mds_last_transno);
398                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
399                 obd->obd_recovering = 1;
400                 obd->obd_recovery_start = CURRENT_SECONDS;
401                 /* Only used for lprocfs_status */
402                 obd->obd_recovery_end = obd->obd_recovery_start +
403                         OBD_RECOVERY_TIMEOUT / HZ;
404         }
405
406         mds->mds_mount_count = mount_count + 1;
407         lsd->lsd_mount_count = cpu_to_le64(mds->mds_mount_count);
408
409         /* save it, so mount count and last_transno is current */
410         rc = mds_update_server_data(obd, 1);
411         if (rc)
412                 GOTO(err_client, rc);
413
414         RETURN(0);
415
416 err_client:
417         class_disconnect_exports(obd);
418 err_msd:
419         mds_server_free_data(mds);
420         RETURN(rc);
421 }
422
423 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
424 {
425         struct mds_obd *mds = &obd->u.mds;
426         struct lvfs_run_ctxt saved;
427         struct dentry *dentry;
428         struct file *file;
429         int rc;
430         ENTRY;
431
432         rc = cleanup_group_info();
433         if (rc)
434                 RETURN(rc);
435
436         mds->mds_vfsmnt = mnt;
437         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
438
439         fsfilt_setup(obd, mds->mds_sb);
440
441         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
442         obd->obd_lvfs_ctxt.pwdmnt = mnt;
443         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
444         obd->obd_lvfs_ctxt.fs = get_ds();
445         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
446
447         /* setup the directory tree */
448         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
449         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
450         if (IS_ERR(dentry)) {
451                 rc = PTR_ERR(dentry);
452                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
453                 GOTO(err_pop, rc);
454         }
455
456         mds->mds_rootfid.id = dentry->d_inode->i_ino;
457         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
458         mds->mds_rootfid.f_type = S_IFDIR;
459
460         dput(dentry);
461
462         dentry = lookup_one_len("__iopen__", current->fs->pwd,
463                                 strlen("__iopen__"));
464         if (IS_ERR(dentry)) {
465                 rc = PTR_ERR(dentry);
466                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
467                 GOTO(err_pop, rc);
468         }
469
470         mds->mds_fid_de = dentry;
471         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
472                 rc = -ENOENT;
473                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
474                 GOTO(err_fid, rc);
475         }
476
477         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
478         if (IS_ERR(dentry)) {
479                 rc = PTR_ERR(dentry);
480                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
481                 GOTO(err_fid, rc);
482         }
483         mds->mds_pending_dir = dentry;
484
485         dentry = simple_mkdir(current->fs->pwd, MOUNT_CONFIGS_DIR, 0777, 1);
486         if (IS_ERR(dentry)) {
487                 rc = PTR_ERR(dentry);
488                 CERROR("cannot create %s directory: rc = %d\n",
489                        MOUNT_CONFIGS_DIR, rc);
490                 GOTO(err_pending, rc);
491         }
492         mds->mds_logs_dir = dentry;
493
494         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
495         if (IS_ERR(dentry)) {
496                 rc = PTR_ERR(dentry);
497                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
498                 GOTO(err_logs, rc);
499         }
500         mds->mds_objects_dir = dentry;
501
502         /* open and test the last rcvd file */
503         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
504         if (IS_ERR(file)) {
505                 rc = PTR_ERR(file);
506                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
507                 GOTO(err_objects, rc = PTR_ERR(file));
508         }
509         mds->mds_rcvd_filp = file;
510         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
511                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
512                        file->f_dentry->d_inode->i_mode);
513                 GOTO(err_last_rcvd, rc = -ENOENT);
514         }
515
516         rc = mds_init_server_data(obd, file);
517         if (rc) {
518                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
519                 GOTO(err_last_rcvd, rc);
520         }
521
522         /* open and test the lov objd file */
523         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
524         if (IS_ERR(file)) {
525                 rc = PTR_ERR(file);
526                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
527                 GOTO(err_client, rc = PTR_ERR(file));
528         }
529         mds->mds_lov_objid_filp = file;
530         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
531                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
532                        file->f_dentry->d_inode->i_mode);
533                 GOTO(err_lov_objid, rc = -ENOENT);
534         }
535
536         /* open and test the check io file junk */
537         file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
538         if (IS_ERR(file)) {
539                 rc = PTR_ERR(file);
540                 CERROR("cannot open/create %s file: rc = %d\n", HEALTH_CHECK, rc);
541                 GOTO(err_lov_objid, rc = PTR_ERR(file));
542         }
543         mds->mds_health_check_filp = file;
544         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
545                 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
546                        file->f_dentry->d_inode->i_mode);
547                 GOTO(err_health_check, rc = -ENOENT);
548         }
549         rc = lvfs_check_io_health(obd, file);
550         if (rc)
551                 GOTO(err_health_check, rc);
552 err_pop:
553         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
554
555         return rc;
556
557 err_health_check:
558         if (mds->mds_health_check_filp && 
559             filp_close(mds->mds_health_check_filp, 0))
560                 CERROR("can't close %s after error\n", HEALTH_CHECK);
561 err_lov_objid:
562         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
563                 CERROR("can't close %s after error\n", LOV_OBJID);
564 err_client:
565         class_disconnect_exports(obd);
566 err_last_rcvd:
567         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
568                 CERROR("can't close %s after error\n", LAST_RCVD);
569 err_objects:
570         dput(mds->mds_objects_dir);
571 err_logs:
572         dput(mds->mds_logs_dir);
573 err_pending:
574         dput(mds->mds_pending_dir);
575 err_fid:
576         dput(mds->mds_fid_de);
577         goto err_pop;
578 }
579
580
581 int mds_fs_cleanup(struct obd_device *obd)
582 {
583         struct mds_obd *mds = &obd->u.mds;
584         struct lvfs_run_ctxt saved;
585         int rc = 0;
586
587         if (obd->obd_fail)
588                 CERROR("%s: shutting down for failover; client state will"
589                        " be preserved.\n", obd->obd_name);
590
591         class_disconnect_exports(obd); /* cleans up client info too */
592         mds_server_free_data(mds);
593
594         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
595         if (mds->mds_rcvd_filp) {
596                 rc = filp_close(mds->mds_rcvd_filp, 0);
597                 mds->mds_rcvd_filp = NULL;
598                 if (rc)
599                         CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
600         }
601         if (mds->mds_lov_objid_filp) {
602                 rc = filp_close(mds->mds_lov_objid_filp, 0);
603                 mds->mds_lov_objid_filp = NULL;
604                 if (rc)
605                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
606         }
607         if (mds->mds_health_check_filp) {
608                 rc = filp_close(mds->mds_health_check_filp, 0);
609                 mds->mds_health_check_filp = NULL;
610                 if (rc)
611                         CERROR("%s file won't close, rc=%d\n", HEALTH_CHECK, rc);
612         }
613         if (mds->mds_objects_dir != NULL) {
614                 l_dput(mds->mds_objects_dir);
615                 mds->mds_objects_dir = NULL;
616         }
617         if (mds->mds_logs_dir) {
618                 l_dput(mds->mds_logs_dir);
619                 mds->mds_logs_dir = NULL;
620         }
621         if (mds->mds_pending_dir) {
622                 l_dput(mds->mds_pending_dir);
623                 mds->mds_pending_dir = NULL;
624         }
625
626         mds_fs_quota_cleanup(mds);
627         
628         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
629         shrink_dcache_parent(mds->mds_fid_de);
630         dput(mds->mds_fid_de);
631         LL_DQUOT_OFF(mds->mds_sb);
632
633         return rc;
634 }
635
636 /* Creates an object with the same name as its fid.  Because this is not at all
637  * performance sensitive, it is accomplished by creating a file, checking the
638  * fid, and renaming it. */
639 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
640                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
641 {
642         struct mds_obd *mds = &exp->exp_obd->u.mds;
643         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
644         unsigned int tmpname = ll_insecure_random_int();
645         struct file *filp;
646         struct dentry *new_child;
647         struct lvfs_run_ctxt saved;
648         char fidname[LL_FID_NAMELEN];
649         void *handle;
650         struct lvfs_ucred ucred = { 0 };
651         int rc = 0, err, namelen;
652         ENTRY;
653
654         /* the owner of object file should always be root */
655         ucred.luc_cap = current->cap_effective | CAP_SYS_RESOURCE;
656
657         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
658
659         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
660         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
661         if (IS_ERR(filp)) {
662                 rc = PTR_ERR(filp);
663                 if (rc == -EEXIST) {
664                         CERROR("impossible object name collision %u\n",
665                                tmpname);
666                         LBUG();
667                 }
668                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
669                 GOTO(out_pop, rc);
670         }
671
672         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
673
674         oa->o_id = filp->f_dentry->d_inode->i_ino;
675         oa->o_generation = filp->f_dentry->d_inode->i_generation;
676         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
677
678         down(&parent_inode->i_sem);
679         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
680
681         if (IS_ERR(new_child)) {
682                 CERROR("getting neg dentry for obj rename: %d\n", rc);
683                 GOTO(out_close, rc = PTR_ERR(new_child));
684         }
685         if (new_child->d_inode != NULL) {
686                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
687                        oa->o_id, oa->o_generation);
688                 LBUG();
689         }
690
691         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
692                               FSFILT_OP_RENAME, NULL);
693         if (IS_ERR(handle))
694                 GOTO(out_dput, rc = PTR_ERR(handle));
695
696         lock_kernel();
697         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
698                         mds->mds_objects_dir->d_inode, new_child);
699         unlock_kernel();
700         if (rc)
701                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
702                        oa->o_id, oa->o_generation, rc);
703
704         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
705                             handle, 0);
706         if (!err)
707                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
708         else if (!rc)
709                 rc = err;
710 out_dput:
711         dput(new_child);
712 out_close:
713         up(&parent_inode->i_sem);
714         err = filp_close(filp, 0);
715         if (err) {
716                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
717                 if (!rc)
718                         rc = err;
719         }
720 out_pop:
721         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
722         RETURN(rc);
723 }
724
725 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
726                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
727 {
728         struct mds_obd *mds = &exp->exp_obd->u.mds;
729         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
730         struct obd_device *obd = exp->exp_obd;
731         struct lvfs_run_ctxt saved;
732         struct lvfs_ucred ucred = { 0 };
733         char fidname[LL_FID_NAMELEN];
734         struct dentry *de;
735         void *handle;
736         int err, namelen, rc = 0;
737         ENTRY;
738
739         ucred.luc_cap = current->cap_effective | CAP_SYS_RESOURCE;
740         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
741
742         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
743
744         down(&parent_inode->i_sem);
745         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
746         if (IS_ERR(de)) {
747                 rc = IS_ERR(de);
748                 de = NULL;
749                 CERROR("error looking up object "LPU64" %s: rc %d\n",
750                        oa->o_id, fidname, rc);
751                 GOTO(out_dput, rc);
752         }
753         if (de->d_inode == NULL) {
754                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
755                        oa->o_id, fidname, rc);
756                 GOTO(out_dput, rc = -ENOENT);
757         }
758
759         /* Stripe count is 1 here since this is some MDS specific stuff
760            that is unlinked, not spanned across multiple OSTs */
761         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
762                                   FSFILT_OP_UNLINK, oti, 1);
763
764         if (IS_ERR(handle))
765                 GOTO(out_dput, rc = PTR_ERR(handle));
766
767         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
768         if (rc)
769                 CERROR("error destroying object "LPU64":%u: rc %d\n",
770                        oa->o_id, oa->o_generation, rc);
771
772         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
773         if (err && !rc)
774                 rc = err;
775 out_dput:
776         if (de != NULL)
777                 l_dput(de);
778         up(&parent_inode->i_sem);
779
780         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
781         RETURN(rc);
782 }