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