Whamcloud - gitweb
- b_hd_audit landing
[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 <libcfs/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
50 #define LAST_RCVD "last_rcvd"
51 #define LOV_OBJID "lov_objid"
52 #define LAST_FID  "last_fid"
53 #define VIRT_FID  "virt_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((char *)med->med_mcd->mcd_uuid, (char *)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((char *)med->med_mcd->mcd_uuid, (char *)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         med->med_mcd = NULL;
180         return 0;
181 }
182
183 static int mds_server_free_data(struct mds_obd *mds)
184 {
185         OBD_FREE(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
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, MDS_MAX_CLIENTS / 8);
256         if (!mds->mds_client_bitmap) {
257                 OBD_FREE(msd, sizeof(*msd));
258                 RETURN(-ENOMEM);
259         }
260
261         mds->mds_server_data = msd;
262
263         if (last_rcvd_size == 0) {
264                 CWARN("%s: initializing new %s\n", obd->obd_name,
265                       file->f_dentry->d_name.name);
266
267                 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
268                 msd->msd_last_transno = 0;
269                 mount_count = msd->msd_mount_count = 0;
270                 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
271                 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
272                 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
273                 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
274         } else {
275                 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
276                 if (rc) {
277                         CERROR("error reading MDS %s: rc = %d\n",
278                                file->f_dentry->d_name.name, rc);
279                         GOTO(err_msd, rc);
280                 }
281                 if (strcmp((char *)msd->msd_uuid, (char *)obd->obd_uuid.uuid)) {
282                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
283                                obd->obd_uuid.uuid, msd->msd_uuid);
284                         GOTO(err_msd, rc = -EINVAL);
285                 }
286                 mount_count = le64_to_cpu(msd->msd_mount_count);
287         }
288         if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
289                 CERROR("unsupported incompat feature %x\n",
290                        le32_to_cpu(msd->msd_feature_incompat) &
291                        ~MDS_INCOMPAT_SUPP);
292                 GOTO(err_msd, rc = -EINVAL);
293         }
294         /* XXX updating existing b_devel fs only, can be removed in future */
295         msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
296         if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
297                 CERROR("unsupported read-only feature %x\n",
298                        le32_to_cpu(msd->msd_feature_rocompat) &
299                        ~MDS_ROCOMPAT_SUPP);
300                 /* Do something like remount filesystem read-only */
301                 GOTO(err_msd, rc = -EINVAL);
302         }
303
304         mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
305
306         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
307                obd->obd_name, mds->mds_last_transno);
308         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
309                obd->obd_name, mount_count + 1);
310         CDEBUG(D_INODE, "%s: server data size: %u\n",
311                obd->obd_name, le32_to_cpu(msd->msd_server_size));
312         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
313                obd->obd_name, le32_to_cpu(msd->msd_client_start));
314         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
315                obd->obd_name, le32_to_cpu(msd->msd_client_size));
316         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
317                obd->obd_name, last_rcvd_size);
318         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
319                last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
320                (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
321                 le16_to_cpu(msd->msd_client_size));
322
323         /* When we do a clean MDS shutdown, we save the last_transno into
324          * the header.  If we find clients with higher last_transno values
325          * then those clients may need recovery done. */
326         for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
327              off < last_rcvd_size; cl_idx++) {
328                 __u64 last_transno;
329                 struct obd_export *exp;
330                 struct mds_export_data *med;
331
332                 if (!mcd) {
333                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
334                         if (!mcd)
335                                 GOTO(err_client, rc = -ENOMEM);
336                 }
337
338                 /* Don't assume off is incremented properly by
339                  * fsfilt_read_record(), in case sizeof(*mcd)
340                  * isn't the same as msd->msd_client_size.  */
341                 off = le32_to_cpu(msd->msd_client_start) +
342                         cl_idx * le16_to_cpu(msd->msd_client_size);
343                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
344                 if (rc) {
345                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
346                                file->f_dentry->d_name.name, cl_idx, off, rc);
347                         break; /* read error shouldn't cause startup to fail */
348                 }
349
350                 if (mcd->mcd_uuid[0] == '\0') {
351                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
352                                cl_idx);
353                         continue;
354                 }
355
356                 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
357                                le64_to_cpu(mcd->mcd_last_close_transno) ?
358                                le64_to_cpu(mcd->mcd_last_transno) :
359                                le64_to_cpu(mcd->mcd_last_close_transno);
360
361                 /* These exports are cleaned up by mds_disconnect(), so they
362                  * need to be set up like real exports as mds_connect() does.
363                  */
364                 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
365                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
366                        last_transno, le64_to_cpu(msd->msd_last_transno),
367                        mcd->mcd_last_xid);
368
369                 exp = class_new_export(obd);
370                 if (exp == NULL)
371                         GOTO(err_client, rc = -ENOMEM);
372
373                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
374                        sizeof exp->exp_client_uuid.uuid);
375                 med = &exp->exp_mds_data;
376                 med->med_mcd = mcd;
377                 mds_client_add(obd, mds, med, cl_idx);
378                 /* create helper if export init gets more complex */
379                 INIT_LIST_HEAD(&med->med_open_head);
380                 spin_lock_init(&med->med_open_lock);
381
382                 mcd = NULL;
383                 exp->exp_connected = 0;
384                 exp->exp_req_replay_needed = 1;
385                 obd->obd_recoverable_clients++;
386                 obd->obd_max_recoverable_clients++;
387
388                 /* track clients to separate req replay
389                  * from lock replay. bug 6063 */
390                 atomic_inc(&obd->obd_req_replay_clients);
391                 exp->exp_req_replay_needed = 1;
392                 atomic_inc(&obd->obd_lock_replay_clients);
393                 exp->exp_lock_replay_needed = 1;
394                 
395                 class_export_put(exp);
396
397                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
398                        cl_idx, last_transno);
399
400                 if (last_transno > mds->mds_last_transno)
401                        mds->mds_last_transno = last_transno;
402         }
403         if (mcd)
404                 OBD_FREE(mcd, sizeof(*mcd));
405         obd->obd_last_committed = mds->mds_last_transno;
406         if (obd->obd_recoverable_clients) {
407                 CWARN("RECOVERY: service %s, %d recoverable clients, "
408                       "last_transno "LPU64"\n", obd->obd_name,
409                       obd->obd_recoverable_clients, mds->mds_last_transno);
410                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
411                 target_start_recovery_thread(obd, mds_handle);
412                 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
413         }
414         
415         mds->mds_mount_count = mount_count + 1;
416         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
417
418         /* save it, so mount count and last_transno is current */
419         rc = mds_update_server_data(obd, 1);
420         if (rc)
421                 GOTO(err_client, rc);
422
423         RETURN(0);
424
425 err_client:
426         class_disconnect_exports(obd, 0);
427 err_msd:
428         mds_server_free_data(mds);
429         RETURN(rc);
430 }
431
432 /*
433  * sets up root inode lustre_id. It tries to read it first from root inode and
434  * if it is not there, new rootid is allocated and saved there.
435  */
436 int mds_fs_setup_rootid(struct obd_device *obd)
437 {
438         int rc = 0;
439         void *handle;
440         struct inode *inode;
441         struct dentry *dentry;
442         struct mds_obd *mds = &obd->u.mds;
443         ENTRY;
444
445         /* getting root directory and setup its fid. */
446         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
447         if (IS_ERR(dentry)) {
448                 CERROR("Can't find ROOT by "DLID4", err = %d\n",
449                        OLID4(&mds->mds_rootid), (int)PTR_ERR(dentry));
450                 RETURN(PTR_ERR(dentry));
451         }
452
453         inode = dentry->d_inode;
454         LASSERT(dentry->d_inode);
455
456         rc = mds_pack_inode2id(obd, &mds->mds_rootid, inode, 1);
457         if (rc < 0) {
458                 if (rc != -ENODATA)
459                         GOTO(out_dentry, rc);
460         } else {
461                 /*
462                  * rootid is filled by mds_read_inode_sid(), so we do not need
463                  * to allocate it and update. The only thing we need to check is
464                  * mds_num.
465                  */
466                 LASSERT(id_group(&mds->mds_rootid) == mds->mds_num);
467                 mds_set_last_fid(obd, id_fid(&mds->mds_rootid));
468                 GOTO(out_dentry, rc);
469         }
470
471         /* allocating new one, as it is not found in root inode. */
472         handle = fsfilt_start(obd, inode,
473                               FSFILT_OP_SETATTR, NULL);
474         
475         if (IS_ERR(handle)) {
476                 rc = PTR_ERR(handle);
477                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
478                 GOTO(out_dentry, rc);
479         }
480         
481         mds_inode2id(obd, &mds->mds_rootid, inode, mds_alloc_fid(obd));
482         rc = mds_update_inode_ids(obd, inode, handle, &mds->mds_rootid, NULL);
483         if (rc) {
484                 CERROR("mds_update_inode_ids() failed, rc = %d\n", rc);
485                 GOTO(out_dentry, rc);
486         }
487
488         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
489         if (rc)
490                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
491
492         EXIT;
493 out_dentry:
494         l_dput(dentry);
495         if (rc == 0)
496                 CWARN("%s: rootid: "DLID4"\n", obd->obd_name,
497                       OLID4(&mds->mds_rootid));
498         return rc;
499 }
500
501 static int mds_update_virtid_fid(struct obd_device *obd,
502                                  void *handle, int force_sync)
503 {
504         struct mds_obd *mds = &obd->u.mds;
505         struct file *filp = mds->mds_virtid_filp;
506         struct lvfs_run_ctxt saved;
507         loff_t off = 0;
508         int rc = 0;
509         ENTRY;
510
511         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
512         rc = fsfilt_write_record(obd, filp, &mds->mds_virtid_fid,
513                                  sizeof(mds->mds_virtid_fid),
514                                  &off, force_sync);
515         if (rc) {
516                 CERROR("error writing MDS virtid_fid #"LPU64
517                        ", err = %d\n", mds->mds_virtid_fid, rc);
518         }
519                 
520         CDEBUG(D_SUPER, "wrote virtid fid #"LPU64" at idx "
521                "%llu: err = %d\n", mds->mds_virtid_fid,
522                off, rc);
523         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
524
525         RETURN(rc);
526 }
527
528 static int mds_read_virtid_fid(struct obd_device *obd,
529                                struct file *file)
530 {
531         int rc = 0;
532         loff_t off = 0;
533         struct mds_obd *mds = &obd->u.mds;
534         unsigned long virtid_fid_size = file->f_dentry->d_inode->i_size;
535         ENTRY;
536
537         if (virtid_fid_size == 0) {
538                 mds->mds_virtid_fid = mds_alloc_fid(obd);
539         } else {
540                 rc = fsfilt_read_record(obd, file, &mds->mds_virtid_fid,
541                                         sizeof(mds->mds_virtid_fid), &off);
542                 if (rc) {
543                         CERROR("error reading MDS %s: rc = %d\n",
544                                file->f_dentry->d_name.name, rc);
545                         RETURN(rc);
546                 }
547         }
548         rc = mds_update_virtid_fid(obd, NULL, 1);
549
550         RETURN(rc);
551 }
552
553 /*
554  * initializes lustre_id for virtual id directory, it is needed sometimes, as it
555  * is possible that it will be the parent for object an operations is going to
556  * be performed on.
557  */
558 int mds_fs_setup_virtid(struct obd_device *obd)
559 {
560         int rc = 0;
561         void *handle;
562         struct lustre_id sid;
563         struct mds_obd *mds = &obd->u.mds;
564         struct inode *inode = mds->mds_id_dir->d_inode;
565         ENTRY;
566
567         handle = fsfilt_start(obd, inode,
568                               FSFILT_OP_SETATTR, NULL);
569         
570         if (IS_ERR(handle)) {
571                 rc = PTR_ERR(handle);
572                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
573                 RETURN(rc);
574         }
575
576         id_group(&sid) = mds->mds_num;
577         id_fid(&sid) = mds->mds_virtid_fid;
578
579         id_ino(&sid) = inode->i_ino;
580         id_gen(&sid) = inode->i_generation;
581         id_type(&sid) = (S_IFMT & inode->i_mode);
582
583         rc = mds_update_inode_ids(obd, inode, handle, &sid, NULL);
584
585         if (rc) {
586                 CERROR("mds_update_inode_ids() failed, rc = %d\n", rc);
587                 RETURN(rc);
588         }
589
590         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
591         if (rc) {
592                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
593                 RETURN(rc);
594         }
595
596         RETURN(rc);
597 }
598
599 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
600 {
601         struct mds_obd *mds = &obd->u.mds;
602         struct lvfs_run_ctxt saved;
603         struct dentry *dentry;
604         struct file *file;
605         int rc;
606         ENTRY;
607
608         rc = cleanup_group_info();
609         if (rc)
610                 RETURN(rc);
611
612         mds->mds_vfsmnt = mnt;
613         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
614
615         fsfilt_setup(obd, mds->mds_sb);
616
617         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
618         obd->obd_lvfs_ctxt.pwdmnt = mnt;
619         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
620         obd->obd_lvfs_ctxt.fs = get_ds();
621         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
622
623         /* setup the directory tree */
624         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
625         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
626         if (IS_ERR(dentry)) {
627                 rc = PTR_ERR(dentry);
628                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
629                 GOTO(err_pop, rc);
630         }
631
632         mdc_pack_id(&mds->mds_rootid, dentry->d_inode->i_ino,
633                     dentry->d_inode->i_generation, S_IFDIR, 0, 0);
634
635         dput(dentry);
636         
637         dentry = lookup_one_len("__iopen__", current->fs->pwd,
638                                 strlen("__iopen__"));
639         if (IS_ERR(dentry)) {
640                 rc = PTR_ERR(dentry);
641                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
642                 GOTO(err_pop, rc);
643         }
644         mds->mds_id_de = dentry;
645         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
646                 rc = -ENOENT;
647                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
648                 GOTO(err_id_de, rc);
649         }
650
651         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
652         if (IS_ERR(dentry)) {
653                 rc = PTR_ERR(dentry);
654                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
655                 GOTO(err_id_de, rc);
656         }
657         mds->mds_pending_dir = dentry;
658       
659         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
660         if (IS_ERR(dentry)) {
661                 rc = PTR_ERR(dentry);
662                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
663                 GOTO(err_pending, rc);
664         }
665         mds->mds_logs_dir = dentry;
666
667         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
668         if (IS_ERR(dentry)) {
669                 rc = PTR_ERR(dentry);
670                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
671                 GOTO(err_logs, rc);
672         }
673         mds->mds_objects_dir = dentry;
674
675         dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
676         if (IS_ERR(dentry)) {
677                 rc = PTR_ERR(dentry);
678                 CERROR("cannot create FIDS directory: rc = %d\n", rc);
679                 GOTO(err_objects, rc);
680         }
681         mds->mds_id_dir = dentry;
682
683         dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
684         if (IS_ERR(dentry)) {
685                 rc = PTR_ERR(dentry);
686                 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
687                 GOTO(err_id_dir, rc);
688         }
689         mds->mds_unnamed_dir = dentry;
690
691         /* open and test the last rcvd file */
692         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
693         if (IS_ERR(file)) {
694                 rc = PTR_ERR(file);
695                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
696                 GOTO(err_unnamed, rc = PTR_ERR(file));
697         }
698         mds->mds_rcvd_filp = file;
699         
700         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
701                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
702                        file->f_dentry->d_inode->i_mode);
703                 GOTO(err_last_rcvd, rc = -ENOENT);
704         }
705
706         rc = mds_read_last_rcvd(obd, file);
707         if (rc) {
708                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
709                 GOTO(err_last_rcvd, rc);
710         }
711
712         /* open and test last fid file */
713         file = filp_open(LAST_FID, O_RDWR | O_CREAT, 0644);
714         if (IS_ERR(file)) {
715                 rc = PTR_ERR(file);
716                 CERROR("cannot open/create %s file: rc = %d\n",
717                        LAST_FID, rc);
718                 GOTO(err_client, rc = PTR_ERR(file));
719         }
720         mds->mds_fid_filp = file;
721         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
722                 CERROR("%s is not a regular file!: mode = %o\n",
723                        LAST_FID, file->f_dentry->d_inode->i_mode);
724                 GOTO(err_last_fid, rc = -ENOENT);
725         }
726
727         rc = mds_read_last_fid(obd, file);
728         if (rc) {
729                 CERROR("cannot read %s: rc = %d\n", LAST_FID, rc);
730                 GOTO(err_last_fid, rc);
731         }
732
733         /* open and test virtid fid file */
734         file = filp_open(VIRT_FID, O_RDWR | O_CREAT, 0644);
735         if (IS_ERR(file)) {
736                 rc = PTR_ERR(file);
737                 CERROR("cannot open/create %s file: rc = %d\n",
738                        VIRT_FID, rc);
739                 GOTO(err_last_fid, rc = PTR_ERR(file));
740         }
741         mds->mds_virtid_filp = file;
742         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
743                 CERROR("%s is not a regular file!: mode = %o\n",
744                        VIRT_FID, file->f_dentry->d_inode->i_mode);
745                 GOTO(err_virtid_fid, rc = -ENOENT);
746         }
747
748         rc = mds_read_virtid_fid(obd, file);
749         if (rc) {
750                 CERROR("cannot read %s: rc = %d\n", VIRT_FID, rc);
751                 GOTO(err_virtid_fid, rc);
752         }
753         
754         /* open and test the lov objid file */
755         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
756         if (IS_ERR(file)) {
757                 rc = PTR_ERR(file);
758                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
759                 GOTO(err_last_fid, rc = PTR_ERR(file));
760         }
761         mds->mds_dt_objid_filp = file;
762         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
763                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
764                        file->f_dentry->d_inode->i_mode);
765                 GOTO(err_lov_objid, rc = -ENOENT);
766         }
767 err_pop:
768         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
769         return rc;
770
771 err_lov_objid:
772         if (mds->mds_dt_objid_filp && filp_close(mds->mds_dt_objid_filp, 0))
773                 CERROR("can't close %s after error\n", LOV_OBJID);
774 err_virtid_fid:
775         if (mds->mds_virtid_filp && filp_close(mds->mds_virtid_filp, 0))
776                 CERROR("can't close %s after error\n", VIRT_FID);
777 err_last_fid:
778         if (mds->mds_fid_filp && filp_close(mds->mds_fid_filp, 0))
779                 CERROR("can't close %s after error\n", LAST_FID);
780 err_client:
781         class_disconnect_exports(obd, 0);
782 err_last_rcvd:
783         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
784                 CERROR("can't close %s after error\n", LAST_RCVD);
785 err_unnamed:
786         dput(mds->mds_unnamed_dir);
787 err_id_dir:
788         dput(mds->mds_id_dir);
789 err_objects:
790         dput(mds->mds_objects_dir);
791 err_logs:
792         dput(mds->mds_logs_dir);
793 err_pending:
794         dput(mds->mds_pending_dir);
795 err_id_de:
796         dput(mds->mds_id_de);
797         goto err_pop;
798 }
799
800 static int  mds_fs_post_cleanup(struct obd_device *obd)
801 {
802         int    rc = 0;
803         rc = fsfilt_post_cleanup(obd);
804         return rc; 
805 }
806
807 int mds_fs_cleanup(struct obd_device *obd, int flags)
808 {
809         struct mds_obd *mds = &obd->u.mds;
810         struct lvfs_run_ctxt saved;
811         int rc = 0;
812
813         if (flags & OBD_OPT_FAILOVER)
814                 CERROR("%s: shutting down for failover; client state will"
815                        " be preserved.\n", obd->obd_name);
816
817         class_disconnect_exports(obd, flags); /* cleans up client info too */
818         target_cleanup_recovery(obd);
819         mds_server_free_data(mds);
820
821         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
822         if (mds->mds_virtid_filp) {
823                 rc = filp_close(mds->mds_virtid_filp, 0);
824                 mds->mds_virtid_filp = NULL;
825                 if (rc)
826                         CERROR("%s file won't close, rc = %d\n", VIRT_FID, rc);
827         }
828         if (mds->mds_fid_filp) {
829                 rc = filp_close(mds->mds_fid_filp, 0);
830                 mds->mds_fid_filp = NULL;
831                 if (rc)
832                         CERROR("%s file won't close, rc = %d\n", LAST_FID, rc);
833         }
834         if (mds->mds_rcvd_filp) {
835                 rc = filp_close(mds->mds_rcvd_filp, 0);
836                 mds->mds_rcvd_filp = NULL;
837                 if (rc)
838                         CERROR("%s file won't close, rc = %d\n", LAST_RCVD, rc);
839         }
840         if (mds->mds_dt_objid_filp) {
841                 rc = filp_close(mds->mds_dt_objid_filp, 0);
842                 mds->mds_dt_objid_filp = NULL;
843                 if (rc)
844                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
845         }
846         if (mds->mds_unnamed_dir != NULL) {
847                 l_dput(mds->mds_unnamed_dir);
848                 mds->mds_unnamed_dir = NULL;
849         }
850         if (mds->mds_id_dir != NULL) {
851                 l_dput(mds->mds_id_dir);
852                 mds->mds_id_dir = NULL;
853         }
854         if (mds->mds_objects_dir != NULL) {
855                 l_dput(mds->mds_objects_dir);
856                 mds->mds_objects_dir = NULL;
857         }
858         if (mds->mds_logs_dir) {
859                 l_dput(mds->mds_logs_dir);
860                 mds->mds_logs_dir = NULL;
861         }
862         if (mds->mds_pending_dir) {
863                 l_dput(mds->mds_pending_dir);
864                 mds->mds_pending_dir = NULL;
865         }
866         rc = mds_fs_post_cleanup(obd);
867         
868         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
869         shrink_dcache_parent(mds->mds_id_de);
870         dput(mds->mds_id_de);
871
872         return rc;
873 }
874
875 /* Creates an object with the same name as its id.  Because this is not at all
876  * performance sensitive, it is accomplished by creating a file, checking the
877  * id, and renaming it. */
878 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
879                    void *acl, int acl_size,
880                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
881 {
882         struct mds_obd *mds = &exp->exp_obd->u.mds;
883         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
884         struct file *filp;
885         struct dentry *dchild;
886         struct lvfs_run_ctxt saved;
887         char idname[LL_ID_NAMELEN];
888         int rc = 0, err, idlen;
889         void *handle;
890         ENTRY;
891
892         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
893         down(&parent_inode->i_sem);
894         if (oa->o_id) {
895                 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
896                 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
897                 if (IS_ERR(dchild))
898                         GOTO(out_pop, rc = PTR_ERR(dchild));
899
900                 if (dchild->d_inode == NULL) {
901                         struct dentry_params dp;
902                         struct inode *inode;
903
904                         CWARN("creating log with ID "LPU64"\n", oa->o_id);
905                         
906                         dchild->d_fsdata = (void *) &dp;
907                         dp.p_ptr = NULL;
908                         dp.p_inum = oa->o_id;
909                         rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
910                         if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
911                                 dchild->d_fsdata = NULL;
912                         if (rc) {
913                                 CDEBUG(D_INODE, "err during create: %d\n", rc);
914                                 dput(dchild);
915                                 GOTO(out_pop, rc);
916                         }
917                         inode = dchild->d_inode;
918                         LASSERT(inode->i_ino == oa->o_id);
919                         inode->i_generation = oa->o_generation;
920                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
921                                inode->i_ino, inode->i_generation);
922                         mark_inode_dirty(inode);
923                 } else {
924                         CWARN("it should be here!\n");
925                 }
926                 GOTO(out_pop, rc);
927         }
928
929         sprintf(idname, "OBJECTS/%u.%u", ll_insecure_random_int(), current->pid);
930         filp = filp_open(idname, O_CREAT | O_EXCL, 0644);
931         if (IS_ERR(filp)) {
932                 rc = PTR_ERR(filp);
933                 if (rc == -EEXIST) {
934                         CERROR("impossible object name collision %s\n",
935                                idname);
936                         LBUG();
937                 }
938                 CERROR("error creating tmp object %s: rc %d\n", 
939                        idname, rc);
940                 GOTO(out_pop, rc);
941         }
942
943         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
944
945         oa->o_id = filp->f_dentry->d_inode->i_ino;
946         oa->o_generation = filp->f_dentry->d_inode->i_generation;
947         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
948         
949         CWARN("created log anonymous "LPU64"/%u\n",
950               oa->o_id, oa->o_generation);
951
952         dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
953         if (IS_ERR(dchild)) {
954                 CERROR("getting neg dentry for obj rename: %d\n", rc);
955                 GOTO(out_close, rc = PTR_ERR(dchild));
956         }
957         if (dchild->d_inode != NULL) {
958                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
959                        oa->o_id, oa->o_generation);
960                 LBUG();
961         }
962
963         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
964                               FSFILT_OP_RENAME, NULL);
965         if (IS_ERR(handle))
966                 GOTO(out_dput, rc = PTR_ERR(handle));
967
968         lock_kernel();
969         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
970                         mds->mds_objects_dir->d_inode, dchild);
971         unlock_kernel();
972         if (rc)
973                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
974                        oa->o_id, oa->o_generation, rc);
975
976         err = fsfilt_commit(exp->exp_obd, mds->mds_sb, 
977                             mds->mds_objects_dir->d_inode, handle, 0);
978         if (!err) {
979                 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
980                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
981         } else if (!rc)
982                 rc = err;
983 out_dput:
984         dput(dchild);
985 out_close:
986         err = filp_close(filp, 0);
987         if (err) {
988                 CERROR("closing tmpfile %s: rc %d\n", idname, rc);
989                 if (!rc)
990                         rc = err;
991         }
992 out_pop:
993         up(&parent_inode->i_sem);
994         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
995         RETURN(rc);
996 }
997
998 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
999                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
1000 {
1001         struct mds_obd *mds = &exp->exp_obd->u.mds;
1002         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
1003         struct obd_device *obd = exp->exp_obd;
1004         struct lvfs_run_ctxt saved;
1005         char idname[LL_ID_NAMELEN];
1006         struct dentry *de;
1007         void *handle;
1008         int err, idlen, rc = 0;
1009         ENTRY;
1010
1011         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1012
1013         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
1014
1015         down(&parent_inode->i_sem);
1016         de = lookup_one_len(idname, mds->mds_objects_dir, idlen);
1017         if (IS_ERR(de) || de->d_inode == NULL) {
1018                 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
1019                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
1020                        oa->o_id, idname, rc);
1021                 GOTO(out_dput, rc);
1022         }
1023         /* Stripe count is 1 here since this is some MDS specific stuff
1024            that is unlinked, not spanned across multiple OSTs */
1025         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
1026                                   FSFILT_OP_UNLINK, oti, 1);
1027
1028         if (IS_ERR(handle))
1029                 GOTO(out_dput, rc = PTR_ERR(handle));
1030         
1031         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
1032         if (rc) 
1033                 CERROR("error destroying object "LPU64":%u: rc %d\n",
1034                        oa->o_id, oa->o_generation, rc);
1035         
1036         err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode, 
1037                             handle, exp->exp_sync);
1038         if (err && !rc)
1039                 rc = err;
1040 out_dput:
1041         if (de != NULL)
1042                 l_dput(de);
1043         up(&parent_inode->i_sem);
1044         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1045         RETURN(rc);
1046 }