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