Whamcloud - gitweb
- landing of b_hd_cleanup_merge to 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 Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #ifndef EXPORT_SYMTAB
27 # define EXPORT_SYMTAB
28 #endif
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/kmod.h>
33 #include <linux/version.h>
34 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
35 #include <linux/mount.h>
36 #endif
37 #include <linux/lustre_mds.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_fsfilt.h>
42 #include <portals/list.h>
43
44 #include <linux/lustre_smfs.h>
45 #include "mds_internal.h"
46
47 /* This limit is arbitrary, but for now we fit it in 1 page (32k clients) */
48 #define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
49 #define MDS_MAX_CLIENT_WORDS (MDS_MAX_CLIENTS / sizeof(unsigned long))
50
51 #define LAST_RCVD "last_rcvd"
52 #define LOV_OBJID "lov_objid"
53
54 /* Add client data to the MDS.  We use a bitmap to locate a free space
55  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
56  * Otherwise, we have just read the data from the last_rcvd file and
57  * we know its offset.
58  */
59 int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
60                    struct mds_export_data *med, int cl_idx)
61 {
62         unsigned long *bitmap = mds->mds_client_bitmap;
63         int new_client = (cl_idx == -1);
64         ENTRY;
65
66         LASSERT(bitmap != NULL);
67
68         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
69         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
70                 RETURN(0);
71
72         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
73          * there's no need for extra complication here
74          */
75         if (new_client) {
76                 cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
77         repeat:
78                 if (cl_idx >= MDS_MAX_CLIENTS) {
79                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
80                         return -ENOMEM;
81                 }
82                 if (test_and_set_bit(cl_idx, bitmap)) {
83                         cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
84                                                     cl_idx);
85                         goto repeat;
86                 }
87         } else {
88                 if (test_and_set_bit(cl_idx, bitmap)) {
89                         CERROR("MDS client %d: bit already set in bitmap!!\n",
90                                cl_idx);
91                         LBUG();
92                 }
93         }
94
95         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
96                cl_idx, med->med_mcd->mcd_uuid);
97
98         med->med_idx = cl_idx;
99         med->med_off = le32_to_cpu(mds->mds_server_data->msd_client_start) +
100                 (cl_idx * le16_to_cpu(mds->mds_server_data->msd_client_size));
101
102         if (new_client) {
103                 struct lvfs_run_ctxt saved;
104                 loff_t off = med->med_off;
105                 struct file *file = mds->mds_rcvd_filp;
106                 int rc;
107
108                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
109                 rc = fsfilt_write_record(obd, file, med->med_mcd,
110                                          sizeof(*med->med_mcd), &off, 1);
111                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
112
113                 if (rc)
114                         return rc;
115                 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
116                        med->med_idx, med->med_off,
117                        (unsigned int)sizeof(*med->med_mcd));
118         }
119         return 0;
120 }
121
122 int mds_client_free(struct obd_export *exp, int clear_client)
123 {
124         struct mds_export_data *med = &exp->exp_mds_data;
125         struct mds_obd *mds = &exp->exp_obd->u.mds;
126         struct obd_device *obd = exp->exp_obd;
127         struct mds_client_data zero_mcd;
128         struct lvfs_run_ctxt saved;
129         int rc;
130         unsigned long *bitmap = mds->mds_client_bitmap;
131
132         if (!med->med_mcd)
133                 RETURN(0);
134
135         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
136         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
137                 GOTO(free_and_out, 0);
138
139         CDEBUG(D_INFO, "freeing client at idx %u (%lld)with UUID '%s'\n",
140                med->med_idx, med->med_off, med->med_mcd->mcd_uuid);
141
142         LASSERT(bitmap);
143
144         /* Clear the bit _after_ zeroing out the client so we don't
145            race with mds_client_add and zero out new clients.*/
146         if (!test_bit(med->med_idx, bitmap)) {
147                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
148                        med->med_idx);
149                 LBUG();
150         }
151
152         if (clear_client) {
153                 memset(&zero_mcd, 0, sizeof zero_mcd);
154                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
155                 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
156                                          sizeof(zero_mcd), &med->med_off, 1);
157                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
158
159                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
160                        "zeroing out client %s idx %u in %s rc %d\n",
161                        med->med_mcd->mcd_uuid, med->med_idx, LAST_RCVD, rc);
162         }
163
164         if (!test_and_clear_bit(med->med_idx, bitmap)) {
165                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
166                        med->med_idx);
167                 LBUG();
168         }
169
170
171         /* Make sure the server's last_transno is up to date. Do this
172          * after the client is freed so we know all the client's
173          * transactions have been committed. */
174         mds_update_server_data(exp->exp_obd, 1);
175
176  free_and_out:
177         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
178
179         return 0;
180 }
181
182 static int mds_server_free_data(struct mds_obd *mds)
183 {
184         OBD_FREE(mds->mds_client_bitmap,
185                  MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
186         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
187         mds->mds_server_data = NULL;
188
189         return 0;
190 }
191
192 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
193 {
194         struct mds_obd *mds = &obd->u.mds;
195         struct mds_server_data *msd;
196         struct mds_client_data *mcd = NULL;
197         loff_t off = 0;
198         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
199         __u64 mount_count;
200         int cl_idx, rc = 0;
201         ENTRY;
202
203         /* ensure padding in the struct is the correct size */
204         LASSERT(offsetof(struct mds_server_data, msd_padding) +
205                 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
206         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
207                 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
208
209         OBD_ALLOC_WAIT(msd, sizeof(*msd));
210         if (!msd)
211                 RETURN(-ENOMEM);
212
213         OBD_ALLOC_WAIT(mds->mds_client_bitmap,
214                   MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
215         if (!mds->mds_client_bitmap) {
216                 OBD_FREE(msd, sizeof(*msd));
217                 RETURN(-ENOMEM);
218         }
219
220         mds->mds_server_data = msd;
221
222         if (last_rcvd_size == 0) {
223                 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
224
225                 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
226                 msd->msd_last_transno = 0;
227                 mount_count = msd->msd_mount_count = 0;
228                 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
229                 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
230                 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
231                 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
232         } else {
233                 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
234                 if (rc) {
235                         CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
236                         GOTO(err_msd, rc);
237                 }
238                 if (strcmp(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
239                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
240                                obd->obd_uuid.uuid, msd->msd_uuid);
241                         GOTO(err_msd, rc = -EINVAL);
242                 }
243                 mount_count = le64_to_cpu(msd->msd_mount_count);
244         }
245         if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
246                 CERROR("unsupported incompat feature %x\n",
247                        le32_to_cpu(msd->msd_feature_incompat) &
248                        ~MDS_INCOMPAT_SUPP);
249                 GOTO(err_msd, rc = -EINVAL);
250         }
251         /* XXX updating existing b_devel fs only, can be removed in future */
252         msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
253         if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
254                 CERROR("unsupported read-only feature %x\n",
255                        le32_to_cpu(msd->msd_feature_rocompat) &
256                        ~MDS_ROCOMPAT_SUPP);
257                 /* Do something like remount filesystem read-only */
258                 GOTO(err_msd, rc = -EINVAL);
259         }
260
261         mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
262
263         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
264                obd->obd_name, mds->mds_last_transno);
265         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
266                obd->obd_name, mount_count + 1);
267         CDEBUG(D_INODE, "%s: server data size: %u\n",
268                obd->obd_name, le32_to_cpu(msd->msd_server_size));
269         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
270                obd->obd_name, le32_to_cpu(msd->msd_client_start));
271         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
272                obd->obd_name, le32_to_cpu(msd->msd_client_size));
273         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
274                obd->obd_name, last_rcvd_size);
275         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
276                last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
277                (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
278                 le16_to_cpu(msd->msd_client_size));
279
280         /* When we do a clean MDS shutdown, we save the last_transno into
281          * the header.  If we find clients with higher last_transno values
282          * then those clients may need recovery done. */
283         for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
284              off < last_rcvd_size; cl_idx++) {
285                 __u64 last_transno;
286                 struct obd_export *exp;
287                 struct mds_export_data *med;
288
289                 if (!mcd) {
290                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
291                         if (!mcd)
292                                 GOTO(err_client, rc = -ENOMEM);
293                 }
294
295                 /* Don't assume off is incremented properly by
296                  * fsfilt_read_record(), in case sizeof(*mcd)
297                  * isn't the same as msd->msd_client_size.  */
298                 off = le32_to_cpu(msd->msd_client_start) +
299                         cl_idx * le16_to_cpu(msd->msd_client_size);
300                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
301                 if (rc) {
302                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
303                                LAST_RCVD, cl_idx, off, rc);
304                         break; /* read error shouldn't cause startup to fail */
305                 }
306
307                 if (mcd->mcd_uuid[0] == '\0') {
308                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
309                                cl_idx);
310                         continue;
311                 }
312
313                 last_transno = le64_to_cpu(mcd->mcd_last_transno);
314
315                 /* These exports are cleaned up by mds_disconnect(), so they
316                  * need to be set up like real exports as mds_connect() does.
317                  */
318                 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
319                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
320                        last_transno, le64_to_cpu(msd->msd_last_transno),
321                        mcd->mcd_last_xid);
322
323                 exp = class_new_export(obd);
324                 if (exp == NULL)
325                         GOTO(err_client, rc = -ENOMEM);
326
327                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
328                        sizeof exp->exp_client_uuid.uuid);
329                 med = &exp->exp_mds_data;
330                 med->med_mcd = mcd;
331                 mds_client_add(obd, mds, med, cl_idx);
332                 /* create helper if export init gets more complex */
333                 INIT_LIST_HEAD(&med->med_open_head);
334                 spin_lock_init(&med->med_open_lock);
335
336                 mcd = NULL;
337                 exp->exp_replay_needed = 1;
338                 obd->obd_recoverable_clients++;
339                 obd->obd_max_recoverable_clients++;
340                 class_export_put(exp);
341
342                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
343                        cl_idx, last_transno);
344
345                 if (last_transno > mds->mds_last_transno)
346                        mds->mds_last_transno = last_transno;
347         }
348         if (mcd)
349                 OBD_FREE(mcd, sizeof(*mcd));
350         obd->obd_last_committed = mds->mds_last_transno;
351         if (obd->obd_recoverable_clients) {
352                 CWARN("RECOVERY: service %s, %d recoverable clients, "
353                       "last_transno "LPU64"\n", obd->obd_name,
354                       obd->obd_recoverable_clients, mds->mds_last_transno);
355                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
356                 target_start_recovery_thread(obd, mds_handle);
357                 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
358         }
359         
360         mds->mds_mount_count = mount_count + 1;
361         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
362
363         /* save it, so mount count and last_transno is current */
364         rc = mds_update_server_data(obd, 1);
365         if (rc)
366                 GOTO(err_client, rc);
367
368         RETURN(0);
369
370 err_client:
371         class_disconnect_exports(obd, 0);
372 err_msd:
373         mds_server_free_data(mds);
374         RETURN(rc);
375 }
376
377 static int  mds_fs_post_setup(struct obd_device *obd)
378 {
379         struct mds_obd *mds = &obd->u.mds;
380         struct dentry *de = mds_fid2dentry(mds, &mds->mds_rootfid, NULL);
381         int    rc = 0;
382        
383         rc = fsfilt_post_setup(obd, de);
384         if (rc)
385                 GOTO(out, rc);
386  
387         fsfilt_set_fs_flags(obd, de->d_inode, 
388                               SM_DO_REC | SM_DO_COW);
389         fsfilt_set_fs_flags(obd, mds->mds_pending_dir->d_inode, 
390                               SM_DO_REC | SM_DO_COW);
391         fsfilt_set_mds_flags(obd, mds->mds_sb);
392 out:
393         l_dput(de);
394         return rc; 
395 }
396
397 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
398 {
399         struct mds_obd *mds = &obd->u.mds;
400         struct lvfs_run_ctxt saved;
401         struct dentry *dentry;
402         struct file *file;
403         int rc;
404         ENTRY;
405
406         rc = cleanup_group_info();
407         if (rc)
408                 RETURN(rc);
409
410         mds->mds_vfsmnt = mnt;
411         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
412
413         fsfilt_setup(obd, mds->mds_sb);
414
415         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
416         obd->obd_lvfs_ctxt.pwdmnt = mnt;
417         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
418         obd->obd_lvfs_ctxt.fs = get_ds();
419         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
420
421         /* setup the directory tree */
422         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
423         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
424         if (IS_ERR(dentry)) {
425                 rc = PTR_ERR(dentry);
426                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
427                 GOTO(err_pop, rc);
428         }
429
430         mds->mds_rootfid.id = dentry->d_inode->i_ino;
431         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
432         mds->mds_rootfid.f_type = S_IFDIR;
433
434         dput(dentry);
435
436         dentry = lookup_one_len("__iopen__", current->fs->pwd,
437                                 strlen("__iopen__"));
438         if (IS_ERR(dentry)) {
439                 rc = PTR_ERR(dentry);
440                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
441                 GOTO(err_pop, rc);
442         }
443         if (!dentry->d_inode) {
444                 rc = -ENOENT;
445                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
446                 GOTO(err_fid, rc);
447         }
448         mds->mds_fid_de = dentry;
449
450         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
451         if (IS_ERR(dentry)) {
452                 rc = PTR_ERR(dentry);
453                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
454                 GOTO(err_fid, rc);
455         }
456         mds->mds_pending_dir = dentry;
457       
458         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
459         if (IS_ERR(dentry)) {
460                 rc = PTR_ERR(dentry);
461                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
462                 GOTO(err_pending, rc);
463         }
464         mds->mds_logs_dir = dentry;
465
466         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
467         if (IS_ERR(dentry)) {
468                 rc = PTR_ERR(dentry);
469                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
470                 GOTO(err_logs, rc);
471         }
472         mds->mds_objects_dir = dentry;
473
474         dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
475         if (IS_ERR(dentry)) {
476                 rc = PTR_ERR(dentry);
477                 CERROR("cannot create FIDS directory: rc = %d\n", rc);
478                 GOTO(err_fids, rc);
479         }
480         mds->mds_fids_dir = dentry;
481
482         dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
483         if (IS_ERR(dentry)) {
484                 rc = PTR_ERR(dentry);
485                 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
486                 GOTO(err_unnamed, rc);
487         }
488         mds->mds_unnamed_dir = dentry;
489
490         /* open and test the last rcvd file */
491         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
492         if (IS_ERR(file)) {
493                 rc = PTR_ERR(file);
494                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
495                 GOTO(err_objects, rc = PTR_ERR(file));
496         }
497         mds->mds_rcvd_filp = file;
498         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
499                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
500                        file->f_dentry->d_inode->i_mode);
501                 GOTO(err_last_rcvd, rc = -ENOENT);
502         }
503
504         rc = mds_read_last_rcvd(obd, file);
505         if (rc) {
506                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
507                 GOTO(err_last_rcvd, rc);
508         }
509
510         /* open and test the lov objd file */
511         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
512         if (IS_ERR(file)) {
513                 rc = PTR_ERR(file);
514                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
515                 GOTO(err_client, rc = PTR_ERR(file));
516         }
517         mds->mds_lov_objid_filp = file;
518         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
519                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
520                        file->f_dentry->d_inode->i_mode);
521                 GOTO(err_lov_objid, rc = -ENOENT);
522         }
523 err_pop:
524         if (!rc) {
525                 rc = mds_fs_post_setup(obd);
526                 if (rc)
527                         CERROR("can not post setup fsfilt\n");        
528         }
529         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
530         return rc;
531
532 err_lov_objid:
533         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
534                 CERROR("can't close %s after error\n", LOV_OBJID);
535 err_client:
536         class_disconnect_exports(obd, 0);
537 err_last_rcvd:
538         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
539                 CERROR("can't close %s after error\n", LAST_RCVD);
540 err_unnamed:
541         dput(mds->mds_unnamed_dir);
542 err_fids:
543         dput(mds->mds_fids_dir);
544 err_objects:
545         dput(mds->mds_objects_dir);
546 err_logs:
547         dput(mds->mds_logs_dir);
548 err_pending:
549         dput(mds->mds_pending_dir);
550 err_fid:
551         dput(mds->mds_fid_de);
552         goto err_pop;
553 }
554
555 static int  mds_fs_post_cleanup(struct obd_device *obd)
556 {
557         int    rc = 0;
558         rc = fsfilt_post_cleanup(obd);
559         return rc; 
560 }
561
562 int mds_fs_cleanup(struct obd_device *obd, int flags)
563 {
564         struct mds_obd *mds = &obd->u.mds;
565         struct lvfs_run_ctxt saved;
566         int rc = 0;
567
568         if (flags & OBD_OPT_FAILOVER)
569                 CERROR("%s: shutting down for failover; client state will"
570                        " be preserved.\n", obd->obd_name);
571
572         class_disconnect_exports(obd, flags); /* cleans up client info too */
573         target_cleanup_recovery(obd);
574         mds_server_free_data(mds);
575
576         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
577         if (mds->mds_rcvd_filp) {
578                 rc = filp_close(mds->mds_rcvd_filp, 0);
579                 mds->mds_rcvd_filp = NULL;
580                 if (rc)
581                         CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
582         }
583         if (mds->mds_lov_objid_filp) {
584                 rc = filp_close(mds->mds_lov_objid_filp, 0);
585                 mds->mds_lov_objid_filp = NULL;
586                 if (rc)
587                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
588         }
589         if (mds->mds_unnamed_dir != NULL) {
590                 l_dput(mds->mds_unnamed_dir);
591                 mds->mds_unnamed_dir = NULL;
592         }
593         if (mds->mds_fids_dir != NULL) {
594                 l_dput(mds->mds_fids_dir);
595                 mds->mds_fids_dir = NULL;
596         }
597         if (mds->mds_objects_dir != NULL) {
598                 l_dput(mds->mds_objects_dir);
599                 mds->mds_objects_dir = NULL;
600         }
601         if (mds->mds_logs_dir) {
602                 l_dput(mds->mds_logs_dir);
603                 mds->mds_logs_dir = NULL;
604         }
605         if (mds->mds_pending_dir) {
606                 l_dput(mds->mds_pending_dir);
607                 mds->mds_pending_dir = NULL;
608         }
609         rc = mds_fs_post_cleanup(obd);
610         
611         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
612         shrink_dcache_parent(mds->mds_fid_de);
613         dput(mds->mds_fid_de);
614
615         return rc;
616 }
617
618 /* Creates an object with the same name as its fid.  Because this is not at all
619  * performance sensitive, it is accomplished by creating a file, checking the
620  * fid, and renaming it. */
621 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
622                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
623 {
624         struct mds_obd *mds = &exp->exp_obd->u.mds;
625         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
626         struct file *filp;
627         struct dentry *dchild;
628         struct lvfs_run_ctxt saved;
629         char fidname[LL_FID_NAMELEN];
630         void *handle;
631         int rc = 0, err, namelen;
632         ENTRY;
633
634         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
635         down(&parent_inode->i_sem);
636         if (oa->o_id) {
637                 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
638
639                 dchild = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
640                 if (IS_ERR(dchild))
641                         GOTO(out_pop, rc = PTR_ERR(dchild));
642
643                 if (dchild->d_inode == NULL) {
644                         struct dentry_params dp;
645                         struct inode *inode;
646
647                         CWARN("creating log with ID "LPU64"\n", oa->o_id);
648                         
649                         dchild->d_fsdata = (void *) &dp;
650                         dp.p_ptr = NULL;
651                         dp.p_inum = oa->o_id;
652                         rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
653                         if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
654                                 dchild->d_fsdata = NULL;
655                         if (rc) {
656                                 CDEBUG(D_INODE, "err during create: %d\n", rc);
657                                 dput(dchild);
658                                 GOTO(out_pop, rc);
659                         }
660                         inode = dchild->d_inode;
661                         LASSERT(inode->i_ino == oa->o_id);
662                         inode->i_generation = oa->o_generation;
663                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
664                                inode->i_ino, inode->i_generation);
665                         mark_inode_dirty(inode);
666                 } else {
667                         CWARN("it should be here!\n");
668                 }
669                 GOTO(out_pop, rc);
670         }
671
672         sprintf(fidname, "OBJECTS/%u.%u",ll_insecure_random_int(),current->pid);
673         filp = filp_open(fidname, O_CREAT | O_EXCL, 0644);
674         if (IS_ERR(filp)) {
675                 rc = PTR_ERR(filp);
676                 if (rc == -EEXIST) {
677                         CERROR("impossible object name collision %s\n",
678                                fidname);
679                         LBUG();
680                 }
681                 CERROR("error creating tmp object %s: rc %d\n", fidname, rc);
682                 GOTO(out_pop, rc);
683         }
684
685         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
686
687         oa->o_id = filp->f_dentry->d_inode->i_ino;
688         oa->o_generation = filp->f_dentry->d_inode->i_generation;
689         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
690         CWARN("created log anonymous "LPU64"/%u\n", oa->o_id, oa->o_generation);
691
692         dchild = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
693
694         if (IS_ERR(dchild)) {
695                 CERROR("getting neg dentry for obj rename: %d\n", rc);
696                 GOTO(out_close, rc = PTR_ERR(dchild));
697         }
698         if (dchild->d_inode != NULL) {
699                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
700                        oa->o_id, oa->o_generation);
701                 LBUG();
702         }
703
704         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
705                               FSFILT_OP_RENAME, NULL);
706         if (IS_ERR(handle))
707                 GOTO(out_dput, rc = PTR_ERR(handle));
708
709         lock_kernel();
710         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
711                         mds->mds_objects_dir->d_inode, dchild);
712         unlock_kernel();
713         if (rc)
714                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
715                        oa->o_id, oa->o_generation, rc);
716
717         err = fsfilt_commit(exp->exp_obd, mds->mds_sb, 
718                             mds->mds_objects_dir->d_inode, handle, 0);
719         if (!err) {
720                 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
721                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
722         } else if (!rc)
723                 rc = err;
724 out_dput:
725         dput(dchild);
726 out_close:
727         err = filp_close(filp, 0);
728         if (err) {
729                 CERROR("closing tmpfile %s: rc %d\n", fidname, rc);
730                 if (!rc)
731                         rc = err;
732         }
733 out_pop:
734         up(&parent_inode->i_sem);
735         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
736         RETURN(rc);
737 }
738
739 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
740                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
741 {
742         struct mds_obd *mds = &exp->exp_obd->u.mds;
743         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
744         struct obd_device *obd = exp->exp_obd;
745         struct lvfs_run_ctxt saved;
746         char fidname[LL_FID_NAMELEN];
747         struct dentry *de;
748         void *handle;
749         int err, namelen, rc = 0;
750         ENTRY;
751
752         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
753
754         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
755
756         down(&parent_inode->i_sem);
757         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
758         if (IS_ERR(de) || de->d_inode == NULL) {
759                 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
760                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
761                        oa->o_id, fidname, rc);
762                 GOTO(out_dput, rc);
763         }
764         /* Stripe count is 1 here since this is some MDS specific stuff
765            that is unlinked, not spanned across multiple OSTs */
766         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
767                                   FSFILT_OP_UNLINK, oti, 1);
768
769         if (IS_ERR(handle))
770                 GOTO(out_dput, rc = PTR_ERR(handle));
771         
772         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
773         if (rc) 
774                 CERROR("error destroying object "LPU64":%u: rc %d\n",
775                        oa->o_id, oa->o_generation, rc);
776         
777         err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode, 
778                             handle, 0);
779         if (err && !rc)
780                 rc = err;
781 out_dput:
782         if (de != NULL)
783                 l_dput(de);
784         up(&parent_inode->i_sem);
785         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
786         RETURN(rc);
787 }