Whamcloud - gitweb
- landing of b_fid after merge with b_hd_cleanup_merge.
[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 lvfs_run_ctxt saved;
105                 loff_t off = med->med_off;
106                 struct file *file = mds->mds_rcvd_filp;
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         struct obd_device *obd = exp->exp_obd;
128         struct mds_client_data zero_mcd;
129         struct lvfs_run_ctxt saved;
130         int rc;
131         unsigned long *bitmap = mds->mds_client_bitmap;
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
180         return 0;
181 }
182
183 static int mds_server_free_data(struct mds_obd *mds)
184 {
185         OBD_FREE(mds->mds_client_bitmap,
186                  MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
187         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
188         mds->mds_server_data = NULL;
189
190         return 0;
191 }
192
193 static int mds_read_last_fid(struct obd_device *obd, struct file *file)
194 {
195         int rc = 0;
196         loff_t off = 0;
197         struct mds_obd *mds = &obd->u.mds;
198         unsigned long last_fid_size = file->f_dentry->d_inode->i_size;
199         ENTRY;
200
201         if (last_fid_size == 0) {
202                 CWARN("%s: initializing new %s\n", obd->obd_name,
203                       file->f_dentry->d_name.name);
204
205                 /* 
206                  * as fid is used for forming res_id for locking, it should not
207                  * be zero. This will keep us out of lots possible problems,
208                  * asserts, etc.
209                  */
210                 mds->mds_last_fid = 1;
211         } else {
212                 __u64 last_fid;
213                 
214                 rc = fsfilt_read_record(obd, file, &last_fid,
215                                         sizeof(last_fid), &off);
216                 if (rc) {
217                         CERROR("error reading MDS %s: rc = %d\n",
218                                file->f_dentry->d_name.name, rc);
219                         RETURN(rc);
220                 }
221
222                 if (last_fid > mds->mds_last_fid)
223                         mds->mds_last_fid = last_fid;
224         }
225
226         CDEBUG(D_INODE, "%s: server last_fid: "LPU64"\n",
227                obd->obd_name, mds->mds_last_fid);
228
229         rc = mds_update_last_fid(obd, 1);
230         RETURN(rc);
231 }
232
233 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
234 {
235         struct mds_obd *mds = &obd->u.mds;
236         struct mds_server_data *msd;
237         struct mds_client_data *mcd = NULL;
238         loff_t off = 0;
239         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
240         __u64 mount_count;
241         int cl_idx, rc = 0;
242         ENTRY;
243
244         /* ensure padding in the struct is the correct size */
245         LASSERT(offsetof(struct mds_server_data, msd_padding) +
246                 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
247         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
248                 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
249
250         OBD_ALLOC_WAIT(msd, sizeof(*msd));
251         if (!msd)
252                 RETURN(-ENOMEM);
253
254         OBD_ALLOC_WAIT(mds->mds_client_bitmap,
255                   MDS_MAX_CLIENT_WORDS * sizeof(unsigned long));
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(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
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
358                 /* These exports are cleaned up by mds_disconnect(), so they
359                  * need to be set up like real exports as mds_connect() does.
360                  */
361                 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
362                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
363                        last_transno, le64_to_cpu(msd->msd_last_transno),
364                        mcd->mcd_last_xid);
365
366                 exp = class_new_export(obd);
367                 if (exp == NULL)
368                         GOTO(err_client, rc = -ENOMEM);
369
370                 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
371                        sizeof exp->exp_client_uuid.uuid);
372                 med = &exp->exp_mds_data;
373                 med->med_mcd = mcd;
374                 mds_client_add(obd, mds, med, cl_idx);
375                 /* create helper if export init gets more complex */
376                 INIT_LIST_HEAD(&med->med_open_head);
377                 spin_lock_init(&med->med_open_lock);
378
379                 mcd = NULL;
380                 exp->exp_replay_needed = 1;
381                 obd->obd_recoverable_clients++;
382                 obd->obd_max_recoverable_clients++;
383                 class_export_put(exp);
384
385                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
386                        cl_idx, last_transno);
387
388                 if (last_transno > mds->mds_last_transno)
389                        mds->mds_last_transno = last_transno;
390         }
391         if (mcd)
392                 OBD_FREE(mcd, sizeof(*mcd));
393         obd->obd_last_committed = mds->mds_last_transno;
394         if (obd->obd_recoverable_clients) {
395                 CWARN("RECOVERY: service %s, %d recoverable clients, "
396                       "last_transno "LPU64"\n", obd->obd_name,
397                       obd->obd_recoverable_clients, mds->mds_last_transno);
398                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
399                 target_start_recovery_thread(obd, mds_handle);
400                 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
401         }
402         
403         mds->mds_mount_count = mount_count + 1;
404         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
405
406         /* save it, so mount count and last_transno is current */
407         rc = mds_update_server_data(obd, 1);
408         if (rc)
409                 GOTO(err_client, rc);
410
411         RETURN(0);
412
413 err_client:
414         class_disconnect_exports(obd, 0);
415 err_msd:
416         mds_server_free_data(mds);
417         RETURN(rc);
418 }
419
420 static int mds_fs_post_setup(struct obd_device *obd)
421 {
422         struct mds_obd *mds = &obd->u.mds;
423         struct dentry *dentry;
424         int rc = 0;
425        
426         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
427         rc = fsfilt_post_setup(obd, dentry);
428         if (rc)
429                 GOTO(out_dentry, rc);
430  
431         fsfilt_set_fs_flags(obd, dentry->d_inode, 
432                             SM_DO_REC | SM_DO_COW);
433         
434         fsfilt_set_fs_flags(obd, mds->mds_pending_dir->d_inode, 
435                             SM_DO_REC | SM_DO_COW);
436         
437         fsfilt_set_mds_flags(obd, mds->mds_sb);
438 out_dentry:
439         l_dput(dentry);
440         return rc; 
441 }
442
443 /*
444  * sets up root inode id. It tries to read it first from root inode and if it is
445  * not there, new rootid is allocated and saved there.
446  */
447 int mds_fs_setup_rootid(struct obd_device *obd)
448 {
449         int rc = 0;
450         void *handle;
451         struct inode *inode;
452         struct dentry *dentry;
453         struct lustre_id rootid;
454         struct mds_obd *mds = &obd->u.mds;
455         ENTRY;
456
457         memcpy(&rootid, &mds->mds_rootid, sizeof(rootid));
458         
459         /* getting root directory and setup its fid. */
460         dentry = mds_id2dentry(obd, &rootid, NULL);
461         if (IS_ERR(dentry))
462                 RETURN(PTR_ERR(dentry));
463
464         inode = dentry->d_inode;
465
466         rc = mds_pack_inode2id(obd, &rootid, inode, 1);
467         if (rc < 0) {
468                 if (rc != -ENODATA)
469                         GOTO(out_dentry, rc);
470         } else {
471                 /*
472                  * rootid is filled by mds_read_inode_sid(), so we do not need
473                  * to allocate it and update. The only thing we need to check is
474                  * mds_num.
475                  */
476                 LASSERT(id_group(&rootid) == mds->mds_num);
477                 GOTO(out_dentry, rc);
478         }
479
480         /* allocating new one, as it is not found in root inode. */
481         handle = fsfilt_start(obd, inode,
482                               FSFILT_OP_SETATTR, NULL);
483         
484         if (IS_ERR(handle)) {
485                 rc = PTR_ERR(handle);
486                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
487                 GOTO(out_dentry, rc);
488         }
489         
490         down(&inode->i_sem);
491         rc = mds_alloc_inode_sid(obd, inode, handle, &rootid);
492         up(&inode->i_sem);
493         
494         if (rc) {
495                 CERROR("mds_alloc_inode_sid() failed, rc = %d\n",
496                        rc);
497                 GOTO(out_dentry, rc);
498         }
499
500         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
501         if (rc)
502                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
503
504 out_dentry:
505         l_dput(dentry);
506
507         if (rc == 0) {
508                 memcpy(&mds->mds_rootid, &rootid, sizeof(rootid));
509                 CWARN("%s: rootid: "DLID4"\n", obd->obd_name,
510                       OLID4(&rootid));
511         }
512         RETURN(rc);
513 }
514
515 /*
516  * initializes lustre_id for virtual id directory, it is needed sometimes, as it
517  * is possible that it will be the parent for object an operations is going to
518  * be performed on.
519  */
520 int mds_fs_setup_virtid(struct obd_device *obd)
521 {
522         int rc = 0;
523         void *handle;
524         struct lustre_id id;
525         struct mds_obd *mds = &obd->u.mds;
526         struct inode *inode = mds->mds_id_dir->d_inode;
527         ENTRY;
528
529         handle = fsfilt_start(obd, inode,
530                               FSFILT_OP_SETATTR, NULL);
531         
532         if (IS_ERR(handle)) {
533                 rc = PTR_ERR(handle);
534                 CERROR("fsfilt_start() failed, rc = %d\n", rc);
535                 RETURN(rc);
536         }
537         
538         down(&inode->i_sem);
539         rc = mds_alloc_inode_sid(obd, inode, handle, &id);
540         up(&inode->i_sem);
541         
542         if (rc) {
543                 CERROR("mds_alloc_inode_sid() failed, rc = %d\n",
544                        rc);
545                 RETURN(rc);
546         }
547
548         rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
549         if (rc) {
550                 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
551                 RETURN(rc);
552         }
553
554         RETURN(rc);
555 }
556
557 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
558 {
559         struct mds_obd *mds = &obd->u.mds;
560         struct lvfs_run_ctxt saved;
561         struct dentry *dentry;
562         struct file *file;
563         int rc;
564         ENTRY;
565
566         rc = cleanup_group_info();
567         if (rc)
568                 RETURN(rc);
569
570         mds->mds_vfsmnt = mnt;
571         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
572
573         fsfilt_setup(obd, mds->mds_sb);
574
575         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
576         obd->obd_lvfs_ctxt.pwdmnt = mnt;
577         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
578         obd->obd_lvfs_ctxt.fs = get_ds();
579         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
580
581         /* setup the directory tree */
582         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
583         
584         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
585         if (IS_ERR(dentry)) {
586                 rc = PTR_ERR(dentry);
587                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
588                 GOTO(err_pop, rc);
589         }
590
591         mdc_pack_id(&mds->mds_rootid, dentry->d_inode->i_ino,
592                     dentry->d_inode->i_generation, S_IFDIR, 0, 0);
593
594         dput(dentry);
595         
596         dentry = lookup_one_len("__iopen__", current->fs->pwd,
597                                 strlen("__iopen__"));
598         if (IS_ERR(dentry)) {
599                 rc = PTR_ERR(dentry);
600                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
601                 GOTO(err_pop, rc);
602         }
603         mds->mds_id_de = dentry;
604
605         if (!dentry->d_inode) {
606                 rc = -ENOENT;
607                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
608                 GOTO(err_id_de, rc);
609         }
610
611         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
612         if (IS_ERR(dentry)) {
613                 rc = PTR_ERR(dentry);
614                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
615                 GOTO(err_id_de, rc);
616         }
617         mds->mds_pending_dir = dentry;
618       
619         dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
620         if (IS_ERR(dentry)) {
621                 rc = PTR_ERR(dentry);
622                 CERROR("cannot create LOGS directory: rc = %d\n", rc);
623                 GOTO(err_pending, rc);
624         }
625         mds->mds_logs_dir = dentry;
626
627         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
628         if (IS_ERR(dentry)) {
629                 rc = PTR_ERR(dentry);
630                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
631                 GOTO(err_logs, rc);
632         }
633         mds->mds_objects_dir = dentry;
634
635         dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
636         if (IS_ERR(dentry)) {
637                 rc = PTR_ERR(dentry);
638                 CERROR("cannot create FIDS directory: rc = %d\n", rc);
639                 GOTO(err_objects, rc);
640         }
641         mds->mds_id_dir = dentry;
642
643         dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
644         if (IS_ERR(dentry)) {
645                 rc = PTR_ERR(dentry);
646                 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
647                 GOTO(err_unnamed, rc);
648         }
649         mds->mds_unnamed_dir = dentry;
650
651         /* open and test the last rcvd file */
652         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
653         if (IS_ERR(file)) {
654                 rc = PTR_ERR(file);
655                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
656                 GOTO(err_id_dir, rc = PTR_ERR(file));
657         }
658         mds->mds_rcvd_filp = file;
659         
660         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
661                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
662                        file->f_dentry->d_inode->i_mode);
663                 GOTO(err_last_rcvd, rc = -ENOENT);
664         }
665
666         rc = mds_read_last_rcvd(obd, file);
667         if (rc) {
668                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
669                 GOTO(err_last_rcvd, rc);
670         }
671
672         /* open and test the last fid file */
673         file = filp_open(LAST_FID, O_RDWR | O_CREAT, 0644);
674         if (IS_ERR(file)) {
675                 rc = PTR_ERR(file);
676                 CERROR("cannot open/create %s file: rc = %d\n",
677                        LAST_FID, rc);
678                 GOTO(err_client, rc = PTR_ERR(file));
679         }
680         mds->mds_fid_filp = file;
681         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
682                 CERROR("%s is not a regular file!: mode = %o\n",
683                        LAST_FID, file->f_dentry->d_inode->i_mode);
684                 GOTO(err_last_fid, rc = -ENOENT);
685         }
686
687         rc = mds_read_last_fid(obd, file);
688         if (rc) {
689                 CERROR("cannot read %s: rc = %d\n", LAST_FID, rc);
690                 GOTO(err_last_fid, rc);
691         }
692
693         /* open and test the lov objid file */
694         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
695         if (IS_ERR(file)) {
696                 rc = PTR_ERR(file);
697                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
698                 GOTO(err_last_fid, rc = PTR_ERR(file));
699         }
700         mds->mds_lov_objid_filp = file;
701         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
702                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
703                        file->f_dentry->d_inode->i_mode);
704                 GOTO(err_lov_objid, rc = -ENOENT);
705         }
706 err_pop:
707         if (!rc) {
708                 rc = mds_fs_post_setup(obd);
709                 if (rc)
710                         CERROR("can not post setup fsfilt\n");        
711         }
712         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
713         return rc;
714
715 err_lov_objid:
716         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
717                 CERROR("can't close %s after error\n", LOV_OBJID);
718 err_last_fid:
719         if (mds->mds_fid_filp && filp_close(mds->mds_fid_filp, 0))
720                 CERROR("can't close %s after error\n", LAST_FID);
721 err_client:
722         class_disconnect_exports(obd, 0);
723 err_last_rcvd:
724         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
725                 CERROR("can't close %s after error\n", LAST_RCVD);
726 err_unnamed:
727         dput(mds->mds_unnamed_dir);
728 err_id_dir:
729         dput(mds->mds_id_dir);
730 err_objects:
731         dput(mds->mds_objects_dir);
732 err_logs:
733         dput(mds->mds_logs_dir);
734 err_pending:
735         dput(mds->mds_pending_dir);
736 err_id_de:
737         dput(mds->mds_id_de);
738         goto err_pop;
739 }
740
741 static int  mds_fs_post_cleanup(struct obd_device *obd)
742 {
743         int    rc = 0;
744         rc = fsfilt_post_cleanup(obd);
745         return rc; 
746 }
747
748 int mds_fs_cleanup(struct obd_device *obd, int flags)
749 {
750         struct mds_obd *mds = &obd->u.mds;
751         struct lvfs_run_ctxt saved;
752         int rc = 0;
753
754         if (flags & OBD_OPT_FAILOVER)
755                 CERROR("%s: shutting down for failover; client state will"
756                        " be preserved.\n", obd->obd_name);
757
758         class_disconnect_exports(obd, flags); /* cleans up client info too */
759         target_cleanup_recovery(obd);
760         mds_server_free_data(mds);
761
762         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
763         if (mds->mds_fid_filp) {
764                 rc = filp_close(mds->mds_fid_filp, 0);
765                 mds->mds_fid_filp = NULL;
766                 if (rc)
767                         CERROR("%s file won't close, rc = %d\n", LAST_FID, rc);
768         }
769         if (mds->mds_rcvd_filp) {
770                 rc = filp_close(mds->mds_rcvd_filp, 0);
771                 mds->mds_rcvd_filp = NULL;
772                 if (rc)
773                         CERROR("%s file won't close, rc = %d\n", LAST_RCVD, rc);
774         }
775         if (mds->mds_lov_objid_filp) {
776                 rc = filp_close(mds->mds_lov_objid_filp, 0);
777                 mds->mds_lov_objid_filp = NULL;
778                 if (rc)
779                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
780         }
781         if (mds->mds_unnamed_dir != NULL) {
782                 l_dput(mds->mds_unnamed_dir);
783                 mds->mds_unnamed_dir = NULL;
784         }
785         if (mds->mds_id_dir != NULL) {
786                 l_dput(mds->mds_id_dir);
787                 mds->mds_id_dir = NULL;
788         }
789         if (mds->mds_objects_dir != NULL) {
790                 l_dput(mds->mds_objects_dir);
791                 mds->mds_objects_dir = NULL;
792         }
793         if (mds->mds_logs_dir) {
794                 l_dput(mds->mds_logs_dir);
795                 mds->mds_logs_dir = NULL;
796         }
797         if (mds->mds_pending_dir) {
798                 l_dput(mds->mds_pending_dir);
799                 mds->mds_pending_dir = NULL;
800         }
801         rc = mds_fs_post_cleanup(obd);
802         
803         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
804         shrink_dcache_parent(mds->mds_id_de);
805         dput(mds->mds_id_de);
806
807         return rc;
808 }
809
810 /* Creates an object with the same name as its id.  Because this is not at all
811  * performance sensitive, it is accomplished by creating a file, checking the
812  * id, and renaming it. */
813 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
814                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
815 {
816         struct mds_obd *mds = &exp->exp_obd->u.mds;
817         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
818         struct file *filp;
819         struct dentry *dchild;
820         struct lvfs_run_ctxt saved;
821         char idname[LL_ID_NAMELEN];
822         int rc = 0, err, idlen;
823         void *handle;
824         ENTRY;
825
826         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
827         down(&parent_inode->i_sem);
828         if (oa->o_id) {
829                 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
830                 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
831                 if (IS_ERR(dchild))
832                         GOTO(out_pop, rc = PTR_ERR(dchild));
833
834                 if (dchild->d_inode == NULL) {
835                         struct dentry_params dp;
836                         struct inode *inode;
837
838                         CWARN("creating log with ID "LPU64"\n", oa->o_id);
839                         
840                         dchild->d_fsdata = (void *) &dp;
841                         dp.p_ptr = NULL;
842                         dp.p_inum = oa->o_id;
843                         rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
844                         if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
845                                 dchild->d_fsdata = NULL;
846                         if (rc) {
847                                 CDEBUG(D_INODE, "err during create: %d\n", rc);
848                                 dput(dchild);
849                                 GOTO(out_pop, rc);
850                         }
851                         inode = dchild->d_inode;
852                         LASSERT(inode->i_ino == oa->o_id);
853                         inode->i_generation = oa->o_generation;
854                         CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
855                                inode->i_ino, inode->i_generation);
856                         mark_inode_dirty(inode);
857                 } else {
858                         CWARN("it should be here!\n");
859                 }
860                 GOTO(out_pop, rc);
861         }
862
863         sprintf(idname, "OBJECTS/%u.%u", ll_insecure_random_int(), current->pid);
864         filp = filp_open(idname, O_CREAT | O_EXCL, 0644);
865         if (IS_ERR(filp)) {
866                 rc = PTR_ERR(filp);
867                 if (rc == -EEXIST) {
868                         CERROR("impossible object name collision %s\n",
869                                idname);
870                         LBUG();
871                 }
872                 CERROR("error creating tmp object %s: rc %d\n", 
873                        idname, rc);
874                 GOTO(out_pop, rc);
875         }
876
877         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
878
879         oa->o_id = filp->f_dentry->d_inode->i_ino;
880         oa->o_generation = filp->f_dentry->d_inode->i_generation;
881         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
882         
883         CWARN("created log anonymous "LPU64"/%u\n",
884               oa->o_id, oa->o_generation);
885
886         dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
887         if (IS_ERR(dchild)) {
888                 CERROR("getting neg dentry for obj rename: %d\n", rc);
889                 GOTO(out_close, rc = PTR_ERR(dchild));
890         }
891         if (dchild->d_inode != NULL) {
892                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
893                        oa->o_id, oa->o_generation);
894                 LBUG();
895         }
896
897         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
898                               FSFILT_OP_RENAME, NULL);
899         if (IS_ERR(handle))
900                 GOTO(out_dput, rc = PTR_ERR(handle));
901
902         lock_kernel();
903         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
904                         mds->mds_objects_dir->d_inode, dchild);
905         unlock_kernel();
906         if (rc)
907                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
908                        oa->o_id, oa->o_generation, rc);
909
910         err = fsfilt_commit(exp->exp_obd, mds->mds_sb, 
911                             mds->mds_objects_dir->d_inode, handle, 0);
912         if (!err) {
913                 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
914                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
915         } else if (!rc)
916                 rc = err;
917 out_dput:
918         dput(dchild);
919 out_close:
920         err = filp_close(filp, 0);
921         if (err) {
922                 CERROR("closing tmpfile %s: rc %d\n", idname, rc);
923                 if (!rc)
924                         rc = err;
925         }
926 out_pop:
927         up(&parent_inode->i_sem);
928         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
929         RETURN(rc);
930 }
931
932 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
933                     struct lov_stripe_md *ea, struct obd_trans_info *oti)
934 {
935         struct mds_obd *mds = &exp->exp_obd->u.mds;
936         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
937         struct obd_device *obd = exp->exp_obd;
938         struct lvfs_run_ctxt saved;
939         char idname[LL_ID_NAMELEN];
940         struct dentry *de;
941         void *handle;
942         int err, idlen, rc = 0;
943         ENTRY;
944
945         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
946
947         idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
948
949         down(&parent_inode->i_sem);
950         de = lookup_one_len(idname, mds->mds_objects_dir, idlen);
951         if (IS_ERR(de) || de->d_inode == NULL) {
952                 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
953                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
954                        oa->o_id, idname, rc);
955                 GOTO(out_dput, rc);
956         }
957         /* Stripe count is 1 here since this is some MDS specific stuff
958            that is unlinked, not spanned across multiple OSTs */
959         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
960                                   FSFILT_OP_UNLINK, oti, 1);
961
962         if (IS_ERR(handle))
963                 GOTO(out_dput, rc = PTR_ERR(handle));
964         
965         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
966         if (rc) 
967                 CERROR("error destroying object "LPU64":%u: rc %d\n",
968                        oa->o_id, oa->o_generation, rc);
969         
970         err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode, 
971                             handle, 0);
972         if (err && !rc)
973                 rc = err;
974 out_dput:
975         if (de != NULL)
976                 l_dput(de);
977         up(&parent_inode->i_sem);
978         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
979         RETURN(rc);
980 }