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