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