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