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