Whamcloud - gitweb
- OBD_ALLOC() of obdos are replaced by obdo_alloc(), the same about obdo_free().
[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 #define LAST_FID  "last_fid"
54
55 /* Add client data to the MDS.  We use a bitmap to locate a free space
56  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
57  * Otherwise, we have just read the data from the last_rcvd file and
58  * we know its offset.
59  */
60 int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
61                    struct mds_export_data *med, int cl_idx)
62 {
63         unsigned long *bitmap = mds->mds_client_bitmap;
64         int new_client = (cl_idx == -1);
65         ENTRY;
66
67         LASSERT(bitmap != NULL);
68
69         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
70         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
71                 RETURN(0);
72
73         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
74          * there's no need for extra complication here
75          */
76         if (new_client) {
77                 cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
78         repeat:
79                 if (cl_idx >= MDS_MAX_CLIENTS) {
80                         CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
81                         return -ENOMEM;
82                 }
83                 if (test_and_set_bit(cl_idx, bitmap)) {
84                         cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
85                                                     cl_idx);
86                         goto repeat;
87                 }
88         } else {
89                 if (test_and_set_bit(cl_idx, bitmap)) {
90                         CERROR("MDS client %d: bit already set in bitmap!!\n",
91                                cl_idx);
92                         LBUG();
93                 }
94         }
95
96         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
97                cl_idx, med->med_mcd->mcd_uuid);
98
99         med->med_idx = cl_idx;
100         med->med_off = le32_to_cpu(mds->mds_server_data->msd_client_start) +
101                 (cl_idx * le16_to_cpu(mds->mds_server_data->msd_client_size));
102
103         if (new_client) {
104                 struct file *file = mds->mds_rcvd_filp;
105                 struct lvfs_run_ctxt saved;
106                 loff_t off = med->med_off;
107                 int rc;
108
109                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
110                 rc = fsfilt_write_record(obd, file, med->med_mcd,
111                                          sizeof(*med->med_mcd), &off, 1);
112                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
113
114                 if (rc)
115                         return rc;
116                 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
117                        med->med_idx, med->med_off,
118                        (unsigned int)sizeof(*med->med_mcd));
119         }
120         return 0;
121 }
122
123 int mds_client_free(struct obd_export *exp, int clear_client)
124 {
125         struct mds_export_data *med = &exp->exp_mds_data;
126         struct mds_obd *mds = &exp->exp_obd->u.mds;
127         unsigned long *bitmap = mds->mds_client_bitmap;
128         struct obd_device *obd = exp->exp_obd;
129         struct mds_client_data zero_mcd;
130         struct lvfs_run_ctxt saved;
131         int rc;
132
133         if (!med->med_mcd)
134                 RETURN(0);
135
136         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
137         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
138                 GOTO(free_and_out, 0);
139
140         CDEBUG(D_INFO, "freeing client at idx %u (%lld)with UUID '%s'\n",
141                med->med_idx, med->med_off, med->med_mcd->mcd_uuid);
142
143         LASSERT(bitmap);
144
145         /* Clear the bit _after_ zeroing out the client so we don't
146            race with mds_client_add and zero out new clients.*/
147         if (!test_bit(med->med_idx, bitmap)) {
148                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
149                        med->med_idx);
150                 LBUG();
151         }
152
153         if (clear_client) {
154                 memset(&zero_mcd, 0, sizeof zero_mcd);
155                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
156                 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
157                                          sizeof(zero_mcd), &med->med_off, 1);
158                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
159
160                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
161                        "zeroing out client %s idx %u in %s rc %d\n",
162                        med->med_mcd->mcd_uuid, med->med_idx, LAST_RCVD, rc);
163         }
164
165         if (!test_and_clear_bit(med->med_idx, bitmap)) {
166                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
167                        med->med_idx);
168                 LBUG();
169         }
170
171
172         /* Make sure the server's last_transno is up to date. Do this
173          * after the client is freed so we know all the client's
174          * transactions have been committed. */
175         mds_update_server_data(exp->exp_obd, 1);
176
177 free_and_out:
178         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
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_fid(struct obd_device *obd, struct file *file)
193 {
194         int rc = 0;
195         loff_t off = 0;
196         struct mds_obd *mds = &obd->u.mds;
197         unsigned long last_fid_size = file->f_dentry->d_inode->i_size;
198         ENTRY;
199
200         if (last_fid_size == 0) {
201                 CWARN("%s: initializing new %s\n", obd->obd_name,
202                       file->f_dentry->d_name.name);
203
204                 /* 
205                  * as fid is used for forming res_id for locking, it should not
206                  * be zero. This will keep us out of lots possible problems,
207                  * asserts, etc.
208                  */
209                 mds_set_last_fid(obd, 0);
210         } else {
211                 __u64 lastfid;
212                 
213                 rc = fsfilt_read_record(obd, file, &lastfid,
214                                         sizeof(lastfid), &off);
215                 if (rc) {
216                         CERROR("error reading MDS %s: rc = %d\n",
217                                file->f_dentry->d_name.name, rc);
218                         RETURN(rc);
219                 }
220
221                 /* 
222                  * make sure, that fid is up-to-date.
223                  */
224                 mds_set_last_fid(obd, lastfid);
225         }
226
227         CDEBUG(D_INODE, "%s: server last_fid: "LPU64"\n",
228                obd->obd_name, mds->mds_last_fid);
229
230         rc = mds_update_last_fid(obd, NULL, 1);
231         RETURN(rc);
232 }
233
234 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
235 {
236         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
237         struct mds_obd *mds = &obd->u.mds;
238         struct mds_server_data *msd = NULL;
239         struct mds_client_data *mcd = NULL;
240         loff_t off = 0;
241         __u64 mount_count;
242         int cl_idx, rc = 0;
243         ENTRY;
244
245         /* ensure padding in the struct is the correct size */
246         LASSERT(offsetof(struct mds_server_data, msd_padding) +
247                 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
248         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
249                 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
250
251         OBD_ALLOC_WAIT(msd, sizeof(*msd));
252         if (!msd)
253                 RETURN(-ENOMEM);
254
255         OBD_ALLOC_WAIT(mds->mds_client_bitmap,
256                   MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
257         if (!mds->mds_client_bitmap) {
258                 OBD_FREE(msd, sizeof(*msd));
259                 RETURN(-ENOMEM);
260         }
261
262         mds->mds_server_data = msd;
263
264         if (last_rcvd_size == 0) {
265                 CWARN("%s: initializing new %s\n", obd->obd_name,
266                       file->f_dentry->d_name.name);
267
268                 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
269                 msd->msd_last_transno = 0;
270                 mount_count = msd->msd_mount_count = 0;
271                 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
272                 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
273                 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
274                 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
275         } else {
276                 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
277                 if (rc) {
278                         CERROR("error reading MDS %s: rc = %d\n",
279                                file->f_dentry->d_name.name, rc);
280                         GOTO(err_msd, rc);
281                 }
282                 if (strcmp(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
283                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
284                                obd->obd_uuid.uuid, msd->msd_uuid);
285                         GOTO(err_msd, rc = -EINVAL);
286                 }
287                 mount_count = le64_to_cpu(msd->msd_mount_count);
288         }
289         if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
290                 CERROR("unsupported incompat feature %x\n",
291                        le32_to_cpu(msd->msd_feature_incompat) &
292                        ~MDS_INCOMPAT_SUPP);
293                 GOTO(err_msd, rc = -EINVAL);
294         }
295         /* XXX updating existing b_devel fs only, can be removed in future */
296         msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
297         if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
298                 CERROR("unsupported read-only feature %x\n",
299                        le32_to_cpu(msd->msd_feature_rocompat) &
300                        ~MDS_ROCOMPAT_SUPP);
301                 /* Do something like remount filesystem read-only */
302                 GOTO(err_msd, rc = -EINVAL);
303         }
304
305         mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
306
307         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
308                obd->obd_name, mds->mds_last_transno);
309         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
310                obd->obd_name, mount_count + 1);
311         CDEBUG(D_INODE, "%s: server data size: %u\n",
312                obd->obd_name, le32_to_cpu(msd->msd_server_size));
313         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
314                obd->obd_name, le32_to_cpu(msd->msd_client_start));
315         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
316                obd->obd_name, le32_to_cpu(msd->msd_client_size));
317         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
318                obd->obd_name, last_rcvd_size);
319         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
320                last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
321                (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
322                 le16_to_cpu(msd->msd_client_size));
323
324         /* When we do a clean MDS shutdown, we save the last_transno into
325          * the header.  If we find clients with higher last_transno values
326          * then those clients may need recovery done. */
327         for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
328              off < last_rcvd_size; cl_idx++) {
329                 __u64 last_transno;
330                 struct obd_export *exp;
331                 struct mds_export_data *med;
332
333                 if (!mcd) {
334                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
335                         if (!mcd)
336                                 GOTO(err_client, rc = -ENOMEM);
337                 }
338
339                 /* Don't assume off is incremented properly by
340                  * fsfilt_read_record(), in case sizeof(*mcd)
341                  * isn't the same as msd->msd_client_size.  */
342                 off = le32_to_cpu(msd->msd_client_start) +
343                         cl_idx * le16_to_cpu(msd->msd_client_size);
344                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
345                 if (rc) {
346                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
347                                file->f_dentry->d_name.name, cl_idx, off, rc);
348                         break; /* read error shouldn't cause startup to fail */
349                 }
350
351                 if (mcd->mcd_uuid[0] == '\0') {
352                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
353                                cl_idx);
354                         continue;
355                 }
356
357                 last_transno = le64_to_cpu(mcd->mcd_last_transno);
358
359                 /* These exports are cleaned up by mds_disconnect(), so they
360                  * need to be set up like real exports as mds_connect() does.
361                  */
362                 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
363                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
364                        last_transno, le64_to_cpu(msd->msd_last_transno),
365                        mcd->mcd_last_xid);
366
367                 exp = class_new_export(obd);
368                 if (exp == NULL)
369                         GOTO(err_client, rc = -ENOMEM);
370
371                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
372                        sizeof exp->exp_client_uuid.uuid);
373                 med = &exp->exp_mds_data;
374                 med->med_mcd = mcd;
375                 mds_client_add(obd, mds, med, cl_idx);
376                 /* create helper if export init gets more complex */
377                 INIT_LIST_HEAD(&med->med_open_head);
378                 spin_lock_init(&med->med_open_lock);
379
380                 mcd = NULL;
381                 exp->exp_replay_needed = 1;
382                 obd->obd_recoverable_clients++;
383                 obd->obd_max_recoverable_clients++;
384                 class_export_put(exp);
385
386                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
387                        cl_idx, last_transno);
388
389                 if (last_transno > mds->mds_last_transno)
390                        mds->mds_last_transno = last_transno;
391         }
392         if (mcd)
393                 OBD_FREE(mcd, sizeof(*mcd));
394         obd->obd_last_committed = mds->mds_last_transno;
395         if (obd->obd_recoverable_clients) {
396                 CWARN("RECOVERY: service %s, %d recoverable clients, "
397                       "last_transno "LPU64"\n", obd->obd_name,
398                       obd->obd_recoverable_clients, mds->mds_last_transno);
399                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
400                 target_start_recovery_thread(obd, mds_handle);
401                 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
402         }
403         
404         mds->mds_mount_count = mount_count + 1;
405         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
406
407         /* save it, so mount count and last_transno is current */
408         rc = mds_update_server_data(obd, 1);
409         if (rc)
410                 GOTO(err_client, rc);
411
412         RETURN(0);
413
414 err_client:
415         class_disconnect_exports(obd, 0);
416 err_msd:
417         mds_server_free_data(mds);
418         RETURN(rc);
419 }
420
421 static int mds_fs_post_setup(struct obd_device *obd)
422 {
423         struct mds_obd *mds = &obd->u.mds;
424         struct dentry *dentry;
425         int rc = 0;
426         ENTRY;
427        
428         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
429         if (IS_ERR(dentry)) {
430                 CERROR("Can't find ROOT, err = %d\n",
431                        (int)PTR_ERR(dentry));
432                 RETURN(PTR_ERR(dentry));
433         }
434         
435         rc = fsfilt_post_setup(obd, dentry);
436         if (rc)
437                 goto out_dentry;
438
439         LASSERT(dentry->d_inode != NULL);
440         
441         fsfilt_set_fs_flags(obd, dentry->d_inode, 
442                             SM_DO_REC | SM_DO_COW);
443         
444         fsfilt_set_fs_flags(obd, mds->mds_pending_dir->d_inode, 
445                             SM_DO_REC | SM_DO_COW);
446         
447         fsfilt_set_mds_flags(obd, mds->mds_sb);
448
449 out_dentry:
450         l_dput(dentry);
451         RETURN(rc); 
452 }
453
454 /*
455  * sets up root inode lustre_id. It tries to read it first from root inode and
456  * if it is not there, new rootid is allocated and saved there.
457  */
458 int mds_fs_setup_rootid(struct obd_device *obd)
459 {
460         int rc = 0;
461         void *handle;
462         struct inode *inode;
463         struct dentry *dentry;
464         struct mds_obd *mds = &obd->u.mds;
465         ENTRY;
466
467         /* getting root directory and setup its fid. */
468         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
469         if (IS_ERR(dentry)) {
470                 CERROR("Can't find ROOT by "DLID4", err = %d\n",
471                        OLID4(&mds->mds_rootid), (int)PTR_ERR(dentry));
472                 RETURN(PTR_ERR(dentry));
473         }
474
475         inode = dentry->d_inode;
476         LASSERT(dentry->d_inode);
477
478         rc = mds_pack_inode2id(obd, &mds->mds_rootid, inode, 1);
479         if (rc < 0) {
480                 if (rc != -ENODATA)
481                         GOTO(out_dentry, rc);
482         } else {
483                 /*
484                  * rootid is filled by mds_read_inode_sid(), so we do not need
485                  * to allocate it and update. The only thing we need to check is
486                  * mds_num.
487                  */
488                 LASSERT(id_group(&mds->mds_rootid) == mds->mds_num);
489                 mds_set_last_fid(obd, id_fid(&mds->mds_rootid));
490                 GOTO(out_dentry, rc);
491         }
492
493         /* allocating new one, as it is not found in root inode. */
494         handle = fsfilt_start(obd, inode,
495                               FSFILT_OP_SETATTR, NULL);
496         
497         if (IS_ERR(handle)) {
498                 rc = PTR_ERR(handle);
499                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
500                 GOTO(out_dentry, rc);
501         }
502         
503         down(&inode->i_sem);
504         rc = mds_alloc_inode_sid(obd, inode, handle, &mds->mds_rootid);
505         up(&inode->i_sem);
506         
507         if (rc) {
508                 CERROR("mds_alloc_inode_sid() failed, rc = %d\n",
509                        rc);
510                 GOTO(out_dentry, rc);
511         }
512
513         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
514         if (rc)
515                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
516
517         EXIT;
518 out_dentry:
519         l_dput(dentry);
520         if (rc == 0)
521                 CWARN("%s: rootid: "DLID4"\n", obd->obd_name,
522                       OLID4(&mds->mds_rootid));
523         return rc;
524 }
525
526 /*
527  * initializes lustre_id for virtual id directory, it is needed sometimes, as it
528  * is possible that it will be the parent for object an operations is going to
529  * be performed on.
530  */
531 int mds_fs_setup_virtid(struct obd_device *obd)
532 {
533         int rc = 0;
534         void *handle;
535         struct lustre_id id;
536         struct mds_obd *mds = &obd->u.mds;
537         struct inode *inode = mds->mds_id_dir->d_inode;
538         ENTRY;
539
540         handle = fsfilt_start(obd, inode,
541                               FSFILT_OP_SETATTR, NULL);
542         
543         if (IS_ERR(handle)) {
544                 rc = PTR_ERR(handle);
545                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
546                 RETURN(rc);
547         }
548         
549         down(&inode->i_sem);
550         rc = mds_alloc_inode_sid(obd, inode, handle, &id);
551         up(&inode->i_sem);
552         
553         if (rc) {
554                 CERROR("mds_alloc_inode_sid() failed, rc = %d\n",
555                        rc);
556                 RETURN(rc);
557         }
558
559         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
560         if (rc) {
561                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
562                 RETURN(rc);
563         }
564
565         RETURN(rc);
566 }
567
568 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
569 {
570         struct mds_obd *mds = &obd->u.mds;
571         struct lvfs_run_ctxt saved;
572         struct dentry *dentry;
573         struct file *file;
574         int rc;
575         ENTRY;
576
577         rc = cleanup_group_info();
578         if (rc)
579                 RETURN(rc);
580
581         mds->mds_vfsmnt = mnt;
582         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
583
584         fsfilt_setup(obd, mds->mds_sb);
585
586         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
587         obd->obd_lvfs_ctxt.pwdmnt = mnt;
588         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
589         obd->obd_lvfs_ctxt.fs = get_ds();
590         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
591
592         /* setup the directory tree */
593         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
594         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
595         if (IS_ERR(dentry)) {
596                 rc = PTR_ERR(dentry);
597                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
598                 GOTO(err_pop, rc);
599         }
600
601         mdc_pack_id(&mds->mds_rootid, dentry->d_inode->i_ino,
602                     dentry->d_inode->i_generation, S_IFDIR, 0, 0);
603
604         dput(dentry);
605         
606         dentry = lookup_one_len("__iopen__", current->fs->pwd,
607                                 strlen("__iopen__"));
608         if (IS_ERR(dentry)) {
609                 rc = PTR_ERR(dentry);
610                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
611                 GOTO(err_pop, rc);
612         }
613         mds->mds_id_de = dentry;
614
615         if (!dentry->d_inode) {
616                 rc = -ENOENT;
617                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
618                 GOTO(err_id_de, rc);
619         }
620
621         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
622         if (IS_ERR(dentry)) {
623                 rc = PTR_ERR(dentry);
624                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
625                 GOTO(err_id_de, rc);
626         }
627         mds->mds_pending_dir = dentry;
628       
629         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
630         if (IS_ERR(dentry)) {
631                 rc = PTR_ERR(dentry);
632                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
633                 GOTO(err_pending, rc);
634         }
635         mds->mds_logs_dir = dentry;
636
637         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
638         if (IS_ERR(dentry)) {
639                 rc = PTR_ERR(dentry);
640                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
641                 GOTO(err_logs, rc);
642         }
643         mds->mds_objects_dir = dentry;
644
645         dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
646         if (IS_ERR(dentry)) {
647                 rc = PTR_ERR(dentry);
648                 CERROR("cannot create FIDS directory: rc = %d\n", rc);
649                 GOTO(err_objects, rc);
650         }
651         mds->mds_id_dir = dentry;
652
653         dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
654         if (IS_ERR(dentry)) {
655                 rc = PTR_ERR(dentry);
656                 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
657                 GOTO(err_unnamed, rc);
658         }
659         mds->mds_unnamed_dir = dentry;
660
661         /* open and test the last rcvd file */
662         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
663         if (IS_ERR(file)) {
664                 rc = PTR_ERR(file);
665                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
666                 GOTO(err_id_dir, rc = PTR_ERR(file));
667         }
668         mds->mds_rcvd_filp = file;
669         
670         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
671                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
672                        file->f_dentry->d_inode->i_mode);
673                 GOTO(err_last_rcvd, rc = -ENOENT);
674         }
675
676         rc = mds_read_last_rcvd(obd, file);
677         if (rc) {
678                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
679                 GOTO(err_last_rcvd, rc);
680         }
681
682         /* open and test the last fid file */
683         file = filp_open(LAST_FID, O_RDWR | O_CREAT, 0644);
684         if (IS_ERR(file)) {
685                 rc = PTR_ERR(file);
686                 CERROR("cannot open/create %s file: rc = %d\n",
687                        LAST_FID, rc);
688                 GOTO(err_client, rc = PTR_ERR(file));
689         }
690         mds->mds_fid_filp = file;
691         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
692                 CERROR("%s is not a regular file!: mode = %o\n",
693                        LAST_FID, file->f_dentry->d_inode->i_mode);
694                 GOTO(err_last_fid, rc = -ENOENT);
695         }
696
697         rc = mds_read_last_fid(obd, file);
698         if (rc) {
699                 CERROR("cannot read %s: rc = %d\n", LAST_FID, rc);
700                 GOTO(err_last_fid, rc);
701         }
702
703         /* open and test the lov objid file */
704         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
705         if (IS_ERR(file)) {
706                 rc = PTR_ERR(file);
707                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
708                 GOTO(err_last_fid, rc = PTR_ERR(file));
709         }
710         mds->mds_lov_objid_filp = file;
711         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
712                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
713                        file->f_dentry->d_inode->i_mode);
714                 GOTO(err_lov_objid, rc = -ENOENT);
715         }
716 err_pop:
717         if (!rc) {
718                 rc = mds_fs_post_setup(obd);
719                 if (rc)
720                         CERROR("can not post setup fsfilt\n");        
721         }
722         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
723         return rc;
724
725 err_lov_objid:
726         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
727                 CERROR("can't close %s after error\n", LOV_OBJID);
728 err_last_fid:
729         if (mds->mds_fid_filp && filp_close(mds->mds_fid_filp, 0))
730                 CERROR("can't close %s after error\n", LAST_FID);
731 err_client:
732         class_disconnect_exports(obd, 0);
733 err_last_rcvd:
734         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
735                 CERROR("can't close %s after error\n", LAST_RCVD);
736 err_unnamed:
737         dput(mds->mds_unnamed_dir);
738 err_id_dir:
739         dput(mds->mds_id_dir);
740 err_objects:
741         dput(mds->mds_objects_dir);
742 err_logs:
743         dput(mds->mds_logs_dir);
744 err_pending:
745         dput(mds->mds_pending_dir);
746 err_id_de:
747         dput(mds->mds_id_de);
748         goto err_pop;
749 }
750
751 static int  mds_fs_post_cleanup(struct obd_device *obd)
752 {
753         int    rc = 0;
754         rc = fsfilt_post_cleanup(obd);
755         return rc; 
756 }
757
758 int mds_fs_cleanup(struct obd_device *obd, int flags)
759 {
760         struct mds_obd *mds = &obd->u.mds;
761         struct lvfs_run_ctxt saved;
762         int rc = 0;
763
764         if (flags & OBD_OPT_FAILOVER)
765                 CERROR("%s: shutting down for failover; client state will"
766                        " be preserved.\n", obd->obd_name);
767
768         class_disconnect_exports(obd, flags); /* cleans up client info too */
769         target_cleanup_recovery(obd);
770         mds_server_free_data(mds);
771
772         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
773         if (mds->mds_fid_filp) {
774                 rc = filp_close(mds->mds_fid_filp, 0);
775                 mds->mds_fid_filp = NULL;
776                 if (rc)
777                         CERROR("%s file won't close, rc = %d\n", LAST_FID, rc);
778         }
779         if (mds->mds_rcvd_filp) {
780                 rc = filp_close(mds->mds_rcvd_filp, 0);
781                 mds->mds_rcvd_filp = NULL;
782                 if (rc)
783                         CERROR("%s file won't close, rc = %d\n", LAST_RCVD, rc);
784         }
785         if (mds->mds_lov_objid_filp) {
786                 rc = filp_close(mds->mds_lov_objid_filp, 0);
787                 mds->mds_lov_objid_filp = NULL;
788                 if (rc)
789                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
790         }
791         if (mds->mds_unnamed_dir != NULL) {
792                 l_dput(mds->mds_unnamed_dir);
793                 mds->mds_unnamed_dir = NULL;
794         }
795         if (mds->mds_id_dir != NULL) {
796                 l_dput(mds->mds_id_dir);
797                 mds->mds_id_dir = NULL;
798         }
799         if (mds->mds_objects_dir != NULL) {
800                 l_dput(mds->mds_objects_dir);
801                 mds->mds_objects_dir = NULL;
802         }
803         if (mds->mds_logs_dir) {
804                 l_dput(mds->mds_logs_dir);
805                 mds->mds_logs_dir = NULL;
806         }
807         if (mds->mds_pending_dir) {
808                 l_dput(mds->mds_pending_dir);
809                 mds->mds_pending_dir = NULL;
810         }
811         rc = mds_fs_post_cleanup(obd);
812         
813         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
814         shrink_dcache_parent(mds->mds_id_de);
815         dput(mds->mds_id_de);
816
817         return rc;
818 }
819
820 /* Creates an object with the same name as its id.  Because this is not at all
821  * performance sensitive, it is accomplished by creating a file, checking the
822  * id, and renaming it. */
823 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
824                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
825 {
826         struct mds_obd *mds = &exp->exp_obd->u.mds;
827         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
828         struct file *filp;
829         struct dentry *dchild;
830         struct lvfs_run_ctxt saved;
831         char idname[LL_ID_NAMELEN];
832         int rc = 0, err, idlen;
833         void *handle;
834         ENTRY;
835
836         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
837         down(&parent_inode->i_sem);
838         if (oa->o_id) {
839                 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
840                 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
841                 if (IS_ERR(dchild))
842                         GOTO(out_pop, rc = PTR_ERR(dchild));
843
844                 if (dchild->d_inode == NULL) {
845                         struct dentry_params dp;
846                         struct inode *inode;
847
848                         CWARN("creating log with ID "LPU64"\n", oa->o_id);
849                         
850                         dchild->d_fsdata = (void *) &dp;
851                         dp.p_ptr = NULL;
852                         dp.p_inum = oa->o_id;
853                         rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
854                         if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
855                                 dchild->d_fsdata = NULL;
856                         if (rc) {
857                                 CDEBUG(D_INODE, "err during create: %d\n", rc);
858                                 dput(dchild);
859                                 GOTO(out_pop, rc);
860                         }
861                         inode = dchild->d_inode;
862                         LASSERT(inode->i_ino == oa->o_id);
863                         inode->i_generation = oa->o_generation;
864                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
865                                inode->i_ino, inode->i_generation);
866                         mark_inode_dirty(inode);
867                 } else {
868                         CWARN("it should be here!\n");
869                 }
870                 GOTO(out_pop, rc);
871         }
872
873         sprintf(idname, "OBJECTS/%u.%u", ll_insecure_random_int(), current->pid);
874         filp = filp_open(idname, O_CREAT | O_EXCL, 0644);
875         if (IS_ERR(filp)) {
876                 rc = PTR_ERR(filp);
877                 if (rc == -EEXIST) {
878                         CERROR("impossible object name collision %s\n",
879                                idname);
880                         LBUG();
881                 }
882                 CERROR("error creating tmp object %s: rc %d\n", 
883                        idname, rc);
884                 GOTO(out_pop, rc);
885         }
886
887         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
888
889         oa->o_id = filp->f_dentry->d_inode->i_ino;
890         oa->o_generation = filp->f_dentry->d_inode->i_generation;
891         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
892         
893         CWARN("created log anonymous "LPU64"/%u\n",
894               oa->o_id, oa->o_generation);
895
896         dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
897         if (IS_ERR(dchild)) {
898                 CERROR("getting neg dentry for obj rename: %d\n", rc);
899                 GOTO(out_close, rc = PTR_ERR(dchild));
900         }
901         if (dchild->d_inode != NULL) {
902                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
903                        oa->o_id, oa->o_generation);
904                 LBUG();
905         }
906
907         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
908                               FSFILT_OP_RENAME, NULL);
909         if (IS_ERR(handle))
910                 GOTO(out_dput, rc = PTR_ERR(handle));
911
912         lock_kernel();
913         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
914                         mds->mds_objects_dir->d_inode, dchild);
915         unlock_kernel();
916         if (rc)
917                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
918                        oa->o_id, oa->o_generation, rc);
919
920         err = fsfilt_commit(exp->exp_obd, mds->mds_sb, 
921                             mds->mds_objects_dir->d_inode, handle, 0);
922         if (!err) {
923                 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
924                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
925         } else if (!rc)
926                 rc = err;
927 out_dput:
928         dput(dchild);
929 out_close:
930         err = filp_close(filp, 0);
931         if (err) {
932                 CERROR("closing tmpfile %s: rc %d\n", idname, rc);
933                 if (!rc)
934                         rc = err;
935         }
936 out_pop:
937         up(&parent_inode->i_sem);
938         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
939         RETURN(rc);
940 }
941
942 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
943                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
944 {
945         struct mds_obd *mds = &exp->exp_obd->u.mds;
946         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
947         struct obd_device *obd = exp->exp_obd;
948         struct lvfs_run_ctxt saved;
949         char idname[LL_ID_NAMELEN];
950         struct dentry *de;
951         void *handle;
952         int err, idlen, rc = 0;
953         ENTRY;
954
955         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
956
957         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
958
959         down(&parent_inode->i_sem);
960         de = lookup_one_len(idname, mds->mds_objects_dir, idlen);
961         if (IS_ERR(de) || de->d_inode == NULL) {
962                 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
963                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
964                        oa->o_id, idname, rc);
965                 GOTO(out_dput, rc);
966         }
967         /* Stripe count is 1 here since this is some MDS specific stuff
968            that is unlinked, not spanned across multiple OSTs */
969         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
970                                   FSFILT_OP_UNLINK, oti, 1);
971
972         if (IS_ERR(handle))
973                 GOTO(out_dput, rc = PTR_ERR(handle));
974         
975         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
976         if (rc) 
977                 CERROR("error destroying object "LPU64":%u: rc %d\n",
978                        oa->o_id, oa->o_generation, rc);
979         
980         err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode, 
981                             handle, 0);
982         if (err && !rc)
983                 rc = err;
984 out_dput:
985         if (de != NULL)
986                 l_dput(de);
987         up(&parent_inode->i_sem);
988         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
989         RETURN(rc);
990 }