Whamcloud - gitweb
fccf34f80a78c18297cee1d3c9fdc79bb7e8b598
[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  free_and_out:
171         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
172
173         return 0;
174 }
175
176 static int mds_server_free_data(struct mds_obd *mds)
177 {
178         OBD_FREE(mds->mds_client_bitmap,
179                  MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
180         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
181         mds->mds_server_data = NULL;
182
183         return 0;
184 }
185
186 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
187 {
188         struct mds_obd *mds = &obd->u.mds;
189         struct mds_server_data *msd;
190         struct mds_client_data *mcd = NULL;
191         loff_t off = 0;
192         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
193         __u64 mount_count;
194         int cl_idx, rc = 0;
195         ENTRY;
196
197         /* ensure padding in the struct is the correct size */
198         LASSERT(offsetof(struct mds_server_data, msd_padding) +
199                 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
200         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
201                 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
202
203         OBD_ALLOC_WAIT(msd, sizeof(*msd));
204         if (!msd)
205                 RETURN(-ENOMEM);
206
207         OBD_ALLOC_WAIT(mds->mds_client_bitmap,
208                   MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
209         if (!mds->mds_client_bitmap) {
210                 OBD_FREE(msd, sizeof(*msd));
211                 RETURN(-ENOMEM);
212         }
213
214         mds->mds_server_data = msd;
215
216         if (last_rcvd_size == 0) {
217                 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
218
219                 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
220                 msd->msd_last_transno = 0;
221                 mount_count = msd->msd_mount_count = 0;
222                 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
223                 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
224                 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
225                 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
226         } else {
227                 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
228                 if (rc) {
229                         CERROR("error reading MDS %s: rc = %d\n", LAST_RCVD, rc);
230                         GOTO(err_msd, rc);
231                 }
232                 if (strcmp(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
233                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
234                                obd->obd_uuid.uuid, msd->msd_uuid);
235                         GOTO(err_msd, rc = -EINVAL);
236                 }
237                 mount_count = le64_to_cpu(msd->msd_mount_count);
238         }
239         if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
240                 CERROR("unsupported incompat feature %x\n",
241                        le32_to_cpu(msd->msd_feature_incompat) &
242                        ~MDS_INCOMPAT_SUPP);
243                 GOTO(err_msd, rc = -EINVAL);
244         }
245         /* XXX updating existing b_devel fs only, can be removed in future */
246         msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
247         if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
248                 CERROR("unsupported read-only feature %x\n",
249                        le32_to_cpu(msd->msd_feature_rocompat) &
250                        ~MDS_ROCOMPAT_SUPP);
251                 /* Do something like remount filesystem read-only */
252                 GOTO(err_msd, rc = -EINVAL);
253         }
254
255         mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
256
257         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
258                obd->obd_name, mds->mds_last_transno);
259         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
260                obd->obd_name, mount_count + 1);
261         CDEBUG(D_INODE, "%s: server data size: %u\n",
262                obd->obd_name, le32_to_cpu(msd->msd_server_size));
263         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
264                obd->obd_name, le32_to_cpu(msd->msd_client_start));
265         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
266                obd->obd_name, le32_to_cpu(msd->msd_client_size));
267         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
268                obd->obd_name, last_rcvd_size);
269         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
270                last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
271                (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
272                 le16_to_cpu(msd->msd_client_size));
273
274         /* When we do a clean MDS shutdown, we save the last_transno into
275          * the header.  If we find clients with higher last_transno values
276          * then those clients may need recovery done. */
277         for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
278              off < last_rcvd_size; cl_idx++) {
279                 __u64 last_transno;
280                 struct obd_export *exp;
281                 struct mds_export_data *med;
282
283                 if (!mcd) {
284                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
285                         if (!mcd)
286                                 GOTO(err_client, rc = -ENOMEM);
287                 }
288
289                 /* Don't assume off is incremented properly by
290                  * fsfilt_read_record(), in case sizeof(*mcd)
291                  * isn't the same as msd->msd_client_size.  */
292                 off = le32_to_cpu(msd->msd_client_start) +
293                         cl_idx * le16_to_cpu(msd->msd_client_size);
294                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
295                 if (rc) {
296                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
297                                LAST_RCVD, cl_idx, off, rc);
298                         break; /* read error shouldn't cause startup to fail */
299                 }
300
301                 if (mcd->mcd_uuid[0] == '\0') {
302                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
303                                cl_idx);
304                         continue;
305                 }
306
307                 last_transno = le64_to_cpu(mcd->mcd_last_transno);
308
309                 /* These exports are cleaned up by mds_disconnect(), so they
310                  * need to be set up like real exports as mds_connect() does.
311                  */
312                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
313                        " srv lr: "LPU64"\n", mcd->mcd_uuid, cl_idx,
314                        last_transno, le64_to_cpu(msd->msd_last_transno));
315
316                 exp = class_new_export(obd);
317                 if (exp == NULL)
318                         GOTO(err_client, rc = -ENOMEM);
319
320                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
321                        sizeof exp->exp_client_uuid.uuid);
322                 med = &exp->exp_mds_data;
323                 med->med_mcd = mcd;
324                 mds_client_add(obd, mds, med, cl_idx);
325                 /* create helper if export init gets more complex */
326                 INIT_LIST_HEAD(&med->med_open_head);
327                 spin_lock_init(&med->med_open_lock);
328
329                 mcd = NULL;
330                 obd->obd_recoverable_clients++;
331                 obd->obd_max_recoverable_clients++;
332                 class_export_put(exp);
333
334                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
335                        cl_idx, last_transno);
336
337                 if (last_transno > mds->mds_last_transno)
338                        mds->mds_last_transno = last_transno;
339         }
340
341         obd->obd_last_committed = mds->mds_last_transno;
342         if (obd->obd_recoverable_clients) {
343                 CWARN("RECOVERY: service %s, %d recoverable clients, "
344                       "last_transno "LPU64"\n", obd->obd_name,
345                       obd->obd_recoverable_clients, mds->mds_last_transno);
346                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
347                 obd->obd_recovering = 1;
348         }
349
350         if (mcd)
351                 OBD_FREE(mcd, sizeof(*mcd));
352         
353         mds->mds_mount_count = mount_count + 1;
354         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
355
356         /* save it, so mount count and last_transno is current */
357         rc = mds_update_server_data(obd, 1);
358
359         RETURN(rc);
360
361 err_client:
362         class_disconnect_exports(obd, 0);
363 err_msd:
364         mds_server_free_data(mds);
365         RETURN(rc);
366 }
367
368 static int  mds_fs_post_setup(struct obd_device *obd)
369 {
370         struct mds_obd *mds = &obd->u.mds;
371         struct dentry *de = mds_fid2dentry(mds, &mds->mds_rootfid, NULL);
372         int    rc = 0;
373        
374         rc = fsfilt_post_setup(obd);
375         if (rc)
376                 GOTO(out, rc);
377  
378         fsfilt_set_fs_flags(obd, de->d_inode, 
379                               SM_DO_REC | SM_DO_COW);
380         fsfilt_set_fs_flags(obd, mds->mds_pending_dir->d_inode, 
381                               SM_DO_REC | SM_DO_COW);
382         fsfilt_set_mds_flags(obd, mds->mds_sb);
383 out:
384         l_dput(de);
385         return rc; 
386 }
387
388 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
389 {
390         struct mds_obd *mds = &obd->u.mds;
391         struct lvfs_run_ctxt saved;
392         struct dentry *dentry;
393         struct file *file;
394         int rc;
395         ENTRY;
396
397         rc = cleanup_group_info();
398         if (rc)
399                 RETURN(rc);
400
401         mds->mds_vfsmnt = mnt;
402         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
403
404         fsfilt_setup(obd, mds->mds_sb);
405
406         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
407         obd->obd_lvfs_ctxt.pwdmnt = mnt;
408         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
409         obd->obd_lvfs_ctxt.fs = get_ds();
410         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
411
412         /* setup the directory tree */
413         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
414         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
415         if (IS_ERR(dentry)) {
416                 rc = PTR_ERR(dentry);
417                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
418                 GOTO(err_pop, rc);
419         }
420
421         mds->mds_rootfid.id = dentry->d_inode->i_ino;
422         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
423         mds->mds_rootfid.f_type = S_IFDIR;
424
425         dput(dentry);
426
427         dentry = lookup_one_len("__iopen__", current->fs->pwd,
428                                 strlen("__iopen__"));
429         if (IS_ERR(dentry)) {
430                 rc = PTR_ERR(dentry);
431                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
432                 GOTO(err_pop, rc);
433         }
434         if (!dentry->d_inode) {
435                 rc = -ENOENT;
436                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
437                 GOTO(err_fid, rc);
438         }
439         mds->mds_fid_de = dentry;
440
441         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
442         if (IS_ERR(dentry)) {
443                 rc = PTR_ERR(dentry);
444                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
445                 GOTO(err_fid, rc);
446         }
447         mds->mds_pending_dir = dentry;
448       
449         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
450         if (IS_ERR(dentry)) {
451                 rc = PTR_ERR(dentry);
452                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
453                 GOTO(err_pending, rc);
454         }
455         mds->mds_logs_dir = dentry;
456
457         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
458         if (IS_ERR(dentry)) {
459                 rc = PTR_ERR(dentry);
460                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
461                 GOTO(err_logs, rc);
462         }
463         mds->mds_objects_dir = dentry;
464
465         dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
466         if (IS_ERR(dentry)) {
467                 rc = PTR_ERR(dentry);
468                 CERROR("cannot create FIDS directory: rc = %d\n", rc);
469                 GOTO(err_fids, rc);
470         }
471         mds->mds_fids_dir = dentry;
472
473         /* open and test the last rcvd file */
474         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
475         if (IS_ERR(file)) {
476                 rc = PTR_ERR(file);
477                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
478                 GOTO(err_objects, rc = PTR_ERR(file));
479         }
480         mds->mds_rcvd_filp = file;
481         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
482                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
483                        file->f_dentry->d_inode->i_mode);
484                 GOTO(err_last_rcvd, rc = -ENOENT);
485         }
486
487         rc = mds_read_last_rcvd(obd, file);
488         if (rc) {
489                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
490                 GOTO(err_last_rcvd, rc);
491         }
492
493         /* open and test the lov objd file */
494         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
495         if (IS_ERR(file)) {
496                 rc = PTR_ERR(file);
497                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
498                 GOTO(err_client, rc = PTR_ERR(file));
499         }
500         mds->mds_lov_objid_filp = file;
501         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
502                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
503                        file->f_dentry->d_inode->i_mode);
504                 GOTO(err_lov_objid, rc = -ENOENT);
505         }
506 err_pop:
507         if (!rc) {
508                 rc = mds_fs_post_setup(obd);
509                 if (rc)
510                         CERROR("can not post setup fsfilt\n");        
511         }
512         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
513         return rc;
514
515 err_lov_objid:
516         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
517                 CERROR("can't close %s after error\n", LOV_OBJID);
518 err_client:
519         class_disconnect_exports(obd, 0);
520 err_last_rcvd:
521         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
522                 CERROR("can't close %s after error\n", LAST_RCVD);
523 err_fids:
524         dput(mds->mds_fids_dir);
525 err_objects:
526         dput(mds->mds_objects_dir);
527 err_logs:
528         dput(mds->mds_logs_dir);
529 err_pending:
530         dput(mds->mds_pending_dir);
531 err_fid:
532         dput(mds->mds_fid_de);
533         goto err_pop;
534 }
535
536 static int  mds_fs_post_cleanup(struct obd_device *obd)
537 {
538         int    rc = 0;
539         rc = fsfilt_post_cleanup(obd);
540         return rc; 
541 }
542
543 int mds_fs_cleanup(struct obd_device *obd, int flags)
544 {
545         struct mds_obd *mds = &obd->u.mds;
546         struct lvfs_run_ctxt saved;
547         int rc = 0;
548
549         if (flags & OBD_OPT_FAILOVER)
550                 CERROR("%s: shutting down for failover; client state will"
551                        " be preserved.\n", obd->obd_name);
552
553         class_disconnect_exports(obd, flags); /* cleans up client info too */
554         mds_server_free_data(mds);
555
556         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
557         if (mds->mds_rcvd_filp) {
558                 rc = filp_close(mds->mds_rcvd_filp, 0);
559                 mds->mds_rcvd_filp = NULL;
560                 if (rc)
561                         CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
562         }
563         if (mds->mds_lov_objid_filp) {
564                 rc = filp_close(mds->mds_lov_objid_filp, 0);
565                 mds->mds_lov_objid_filp = NULL;
566                 if (rc)
567                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
568         }
569         if (mds->mds_fids_dir != NULL) {
570                 l_dput(mds->mds_fids_dir);
571                 mds->mds_fids_dir = NULL;
572         }
573         if (mds->mds_objects_dir != NULL) {
574                 l_dput(mds->mds_objects_dir);
575                 mds->mds_objects_dir = NULL;
576         }
577         if (mds->mds_logs_dir) {
578                 l_dput(mds->mds_logs_dir);
579                 mds->mds_logs_dir = NULL;
580         }
581         if (mds->mds_pending_dir) {
582                 l_dput(mds->mds_pending_dir);
583                 mds->mds_pending_dir = NULL;
584         }
585         rc = mds_fs_post_cleanup(obd);
586         
587         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
588         shrink_dcache_parent(mds->mds_fid_de);
589         dput(mds->mds_fid_de);
590
591         return rc;
592 }
593
594 /* Creates an object with the same name as its fid.  Because this is not at all
595  * performance sensitive, it is accomplished by creating a file, checking the
596  * fid, and renaming it. */
597 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
598                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
599 {
600         struct mds_obd *mds = &exp->exp_obd->u.mds;
601         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
602         unsigned int tmpname = ll_insecure_random_int();
603         struct file *filp;
604         struct dentry *new_child;
605         struct lvfs_run_ctxt saved;
606         char fidname[LL_FID_NAMELEN];
607         void *handle;
608         int rc = 0, err, namelen;
609         ENTRY;
610
611         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
612         
613         sprintf(fidname, "OBJECTS/%u", tmpname);
614         filp = filp_open(fidname, O_CREAT | O_EXCL, 0644);
615         if (IS_ERR(filp)) {
616                 rc = PTR_ERR(filp);
617                 if (rc == -EEXIST) {
618                         CERROR("impossible object name collision %u\n",
619                                tmpname);
620                         LBUG();
621                 }
622                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
623                 GOTO(out_pop, rc);
624         }
625
626         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
627
628         oa->o_id = filp->f_dentry->d_inode->i_ino;
629         oa->o_generation = filp->f_dentry->d_inode->i_generation;
630         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
631
632         down(&parent_inode->i_sem);
633         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
634
635         if (IS_ERR(new_child)) {
636                 CERROR("getting neg dentry for obj rename: %d\n", rc);
637                 GOTO(out_close, rc = PTR_ERR(new_child));
638         }
639         if (new_child->d_inode != NULL) {
640                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
641                        oa->o_id, oa->o_generation);
642                 LBUG();
643         }
644
645         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
646                               FSFILT_OP_RENAME, NULL);
647         if (IS_ERR(handle))
648                 GOTO(out_dput, rc = PTR_ERR(handle));
649
650         lock_kernel();
651         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
652                         mds->mds_objects_dir->d_inode, new_child);
653         unlock_kernel();
654         if (rc)
655                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
656                        oa->o_id, oa->o_generation, rc);
657
658         err = fsfilt_commit(exp->exp_obd, mds->mds_sb, 
659                             mds->mds_objects_dir->d_inode, handle, 0);
660         if (!err) {
661                 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
662                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
663         }
664         else if (!rc)
665                 rc = err;
666 out_dput:
667         dput(new_child);
668 out_close:
669         up(&parent_inode->i_sem);
670         err = filp_close(filp, 0);
671         if (err) {
672                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
673                 if (!rc)
674                         rc = err;
675         }
676 out_pop:
677         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
678         RETURN(rc);
679 }
680
681 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
682                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
683 {
684         struct mds_obd *mds = &exp->exp_obd->u.mds;
685         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
686         struct obd_device *obd = exp->exp_obd;
687         struct lvfs_run_ctxt saved;
688         char fidname[LL_FID_NAMELEN];
689         struct dentry *de;
690         void *handle;
691         int err, namelen, rc = 0;
692         ENTRY;
693
694         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
695
696         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
697
698         down(&parent_inode->i_sem);
699         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
700         if (de == NULL || de->d_inode == NULL) {
701                 CERROR("destroying non-existent object "LPU64" %s\n", 
702                        oa->o_id, fidname);
703                 GOTO(out_dput, rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT);
704         }
705
706         /* Stripe count is 1 here since this is some MDS specific stuff
707            that is unlinked, not spanned across multiple OSTs */
708         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
709                                   FSFILT_OP_UNLINK, oti, 1);
710
711         if (IS_ERR(handle))
712                 GOTO(out_dput, rc = PTR_ERR(handle));
713         
714         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
715         if (rc) 
716                 CERROR("error destroying object "LPU64":%u: rc %d\n",
717                        oa->o_id, oa->o_generation, rc);
718         
719         err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode, 
720                             handle, 0);
721         if (err && !rc)
722                 rc = err;
723 out_dput:
724         if (de != NULL)
725                 l_dput(de);
726         up(&parent_inode->i_sem);
727         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
728         RETURN(rc);
729 }