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