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