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