Whamcloud - gitweb
b=4920
[fs/lustre-release.git] / lustre / mds / mds_lov.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_lov.c
5  *  Lustre Metadata Server (mds) handling of striped file data
6  *
7  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@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/lustre_mds.h>
33 #include <linux/obd_ost.h>
34 #include <linux/lustre_idl.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd_lov.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_fsfilt.h>
39
40 #include "mds_internal.h"
41
42 void le_lov_desc_to_cpu (struct lov_desc *ld)
43 {
44         ld->ld_tgt_count = le32_to_cpu (ld->ld_tgt_count);
45         ld->ld_default_stripe_count = le32_to_cpu (ld->ld_default_stripe_count);
46         ld->ld_default_stripe_size = le32_to_cpu (ld->ld_default_stripe_size);
47         ld->ld_pattern = le32_to_cpu (ld->ld_pattern);
48 }
49
50 void cpu_to_le_lov_desc (struct lov_desc *ld)
51 {
52         ld->ld_tgt_count = cpu_to_le32 (ld->ld_tgt_count);
53         ld->ld_default_stripe_count = cpu_to_le32 (ld->ld_default_stripe_count);
54         ld->ld_default_stripe_size = cpu_to_le32 (ld->ld_default_stripe_size);
55         ld->ld_pattern = cpu_to_le32 (ld->ld_pattern);
56 }
57
58 void mds_dt_update_objids(struct obd_device *obd, obd_id *ids)
59 {
60         struct mds_obd *mds = &obd->u.mds;
61         int i;
62         ENTRY;
63
64         spin_lock(&mds->mds_dt_lock);
65         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
66                 if (ids[i] > (mds->mds_dt_objids)[i])
67                         (mds->mds_dt_objids)[i] = ids[i];
68         spin_unlock(&mds->mds_dt_lock);
69         EXIT;
70 }
71
72 static int mds_dt_read_objids(struct obd_device *obd)
73 {
74         struct mds_obd *mds = &obd->u.mds;
75         obd_id *ids;
76         loff_t off = 0;
77         int i, rc, size = mds->mds_dt_desc.ld_tgt_count * sizeof(*ids);
78         ENTRY;
79
80         if (mds->mds_dt_objids != NULL)
81                 RETURN(0);
82
83         OBD_ALLOC(ids, size);
84         if (ids == NULL)
85                 RETURN(-ENOMEM);
86         mds->mds_dt_objids = ids;
87
88         if (mds->mds_dt_objid_filp->f_dentry->d_inode->i_size == 0)
89                 RETURN(0);
90         rc = fsfilt_read_record(obd, mds->mds_dt_objid_filp, ids, size, &off);
91         if (rc < 0) {
92                 CERROR("Error reading objids %d\n", rc);
93         } else {
94                 mds->mds_dt_objids_valid = 1;
95                 rc = 0;
96         }
97
98         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
99                 CDEBUG(D_INFO, "read last object "LPU64" for idx %d\n",
100                        mds->mds_dt_objids[i], i);
101
102         RETURN(rc);
103 }
104
105 int mds_dt_write_objids(struct obd_device *obd)
106 {
107         struct mds_obd *mds = &obd->u.mds;
108         loff_t off = 0;
109         int i, rc, size = mds->mds_dt_desc.ld_tgt_count * sizeof(obd_id);
110         ENTRY;
111
112         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
113                 CDEBUG(D_INFO, "writing last object "LPU64" for idx %d\n",
114                        mds->mds_dt_objids[i], i);
115
116         rc = fsfilt_write_record(obd, mds->mds_dt_objid_filp,
117                                  mds->mds_dt_objids, size, &off, 0);
118         RETURN(rc);
119 }
120
121 int mds_dt_clearorphans(struct mds_obd *mds, struct obd_uuid *ost_uuid)
122 {
123         int rc;
124         struct obdo *oa = NULL;
125         struct obd_trans_info oti = {0};
126         struct lov_stripe_md  *empty_ea = NULL;
127         ENTRY;
128
129         LASSERT(mds->mds_dt_objids != NULL);
130
131         /*
132          * this create will in fact either create or destroy: If the OST is
133          * missing objects below this ID, they will be created.  If it finds
134          * objects above this ID, they will be removed.
135          */
136         oa = obdo_alloc();
137         if (oa == NULL)
138                 RETURN(-ENOMEM);
139         
140         memset(oa, 0, sizeof(*oa));
141         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
142         oa->o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
143         oa->o_flags = OBD_FL_DELORPHAN;
144         
145         if (ost_uuid != NULL) {
146                 memcpy(&oa->o_inline, ost_uuid, sizeof(*ost_uuid));
147                 oa->o_valid |= OBD_MD_FLINLINE;
148         }
149         rc = obd_create(mds->mds_dt_exp, oa, &empty_ea, &oti);
150         obdo_free(oa);
151         RETURN(rc);
152 }
153
154 /* update the LOV-OSC knowledge of the last used object id's */
155 int mds_dt_set_nextid(struct obd_device *obd)
156 {
157         struct mds_obd *mds = &obd->u.mds;
158         int rc;
159         ENTRY;
160
161         LASSERT(!obd->obd_recovering);
162
163         LASSERT(mds->mds_dt_objids != NULL);
164
165         rc = obd_set_info(mds->mds_dt_exp, strlen("next_id"), "next_id",
166                           mds->mds_dt_desc.ld_tgt_count, mds->mds_dt_objids);
167         RETURN(rc);
168 }
169
170 /* tell the LOV-OSC by how much to pre-create */
171 int mds_dt_set_growth(struct mds_obd *mds, int count)
172 {
173         int rc;
174         ENTRY;
175
176         rc = obd_set_info(mds->mds_dt_exp, strlen("growth_count"),
177                           "growth_count", sizeof(count), &count);
178
179         RETURN(rc);
180 }
181
182 static int mds_dt_update_desc(struct obd_device *obd, struct obd_export *lov)
183 {
184         struct mds_obd *mds = &obd->u.mds;
185         int valsize = sizeof(mds->mds_dt_desc), rc, i;
186         int old_count = mds->mds_dt_desc.ld_tgt_count;
187         ENTRY;
188
189         rc = obd_get_info(lov, strlen("lovdesc") + 1, "lovdesc", &valsize,
190                           &mds->mds_dt_desc);
191         if (rc)
192                 RETURN(rc);
193
194         /* The size of the LOV target table may have increased. */
195         if (old_count >= mds->mds_dt_desc.ld_tgt_count) {
196                 obd_id *ids;
197                 int     size;
198
199                 size = mds->mds_dt_desc.ld_tgt_count * sizeof(*ids);
200                 OBD_ALLOC(ids, size);
201                 if (ids == NULL)
202                         RETURN(-ENOMEM);
203
204                 memset(ids, 0, size);
205
206                 if (mds->mds_dt_objids != NULL) {
207                         int oldsize = old_count * sizeof(*ids);
208
209                         memcpy(ids, mds->mds_dt_objids, oldsize);
210                         OBD_FREE(mds->mds_dt_objids, oldsize);
211                 }
212                 mds->mds_dt_objids = ids;
213         }
214
215         i = lov_mds_md_size(mds->mds_dt_desc.ld_tgt_count);
216         if (i > mds->mds_max_mdsize)
217                 mds->mds_max_mdsize = i;
218         mds->mds_max_cookiesize = mds->mds_dt_desc.ld_tgt_count *
219                                   sizeof(struct llog_cookie);
220         mds->mds_has_dt_desc = 1;
221         RETURN(0);
222 }
223
224 int mds_dt_connect(struct obd_device *obd, char * lov_name)
225 {
226         struct mds_obd *mds = &obd->u.mds;
227         struct lustre_handle conn = {0,};
228         int rc, i;
229         ENTRY;
230
231         if (IS_ERR(mds->mds_dt_obd))
232                 RETURN(PTR_ERR(mds->mds_dt_obd));
233
234         if (mds->mds_dt_obd)
235                 RETURN(0);
236
237         spin_lock_init(&mds->mds_dt_lock);
238         mds->mds_dt_obd = class_name2obd(lov_name);
239         if (!mds->mds_dt_obd) {
240                 CERROR("MDS cannot locate LOV %s\n", lov_name);
241                 mds->mds_dt_obd = ERR_PTR(-ENOTCONN);
242                 RETURN(-ENOTCONN);
243         }
244
245         if (mds->mds_ost_sec) {
246                 rc = obd_set_info(mds->mds_dt_obd->obd_self_export,
247                                   strlen("sec"), "sec",
248                                   strlen(mds->mds_ost_sec), mds->mds_ost_sec);
249                 if (rc) {
250                         mds->mds_dt_obd = ERR_PTR(rc);
251                         RETURN(rc);
252                 }
253         }
254
255         CDEBUG(D_HA, "obd: %s osc: %s lov_name: %s\n",
256                obd->obd_name, mds->mds_dt_obd->obd_name, lov_name);
257
258         rc = obd_connect(&conn, mds->mds_dt_obd, &obd->obd_uuid, NULL,
259                          mds->mds_num + FILTER_GROUP_FIRST_MDS);
260         if (rc) {
261                 CERROR("MDS cannot connect to LOV %s (%d)\n", lov_name, rc);
262                 mds->mds_dt_obd = ERR_PTR(rc);
263                 RETURN(rc);
264         }
265         mds->mds_dt_exp = class_conn2export(&conn);
266
267         rc = obd_register_observer(mds->mds_dt_obd, obd);
268         if (rc) {
269                 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
270                        lov_name, rc);
271                 GOTO(err_discon, rc);
272         }
273
274         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
275         if (rc)
276                 GOTO(err_reg, rc);
277
278         rc = mds_dt_read_objids(obd);
279         if (rc) {
280                 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
281                 GOTO(err_reg, rc);
282         }
283
284         rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 
285                                      mds->mds_dt_desc.ld_tgt_count, CATLIST);
286         if (rc) {
287                 CERROR("failed to initialize catalog %d\n", rc);
288                 GOTO(err_reg, rc);
289         }
290
291         /* If we're mounting this code for the first time on an existing FS,
292          * we need to populate the objids array from the real OST values */
293         if (!mds->mds_dt_objids_valid) {
294                 int size = sizeof(obd_id) * mds->mds_dt_desc.ld_tgt_count;
295                 rc = obd_get_info(mds->mds_dt_exp, strlen("last_id"),
296                                   "last_id", &size, mds->mds_dt_objids);
297                 if (!rc) {
298                         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
299                                 CWARN("got last object "LPU64" from OST %d\n",
300                                       mds->mds_dt_objids[i], i);
301                         mds->mds_dt_objids_valid = 1;
302                         rc = mds_dt_write_objids(obd);
303                         if (rc)
304                                 CERROR("got last objids from OSTs, but error "
305                                        "writing objids file: %d\n", rc);
306                 }
307         }
308
309         /* I want to see a callback happen when the OBD moves to a
310          * "For General Use" state, and that's when we'll call
311          * set_nextid().  The class driver can help us here, because
312          * it can use the obd_recovering flag to determine when the
313          * the OBD is full available. */
314         if (!obd->obd_recovering)
315                 rc = mds_postrecov_common(obd);
316         RETURN(rc);
317
318 err_reg:
319         obd_register_observer(mds->mds_dt_obd, NULL);
320 err_discon:
321         obd_disconnect(mds->mds_dt_exp, 0);
322         mds->mds_dt_exp = NULL;
323         mds->mds_dt_obd = ERR_PTR(rc);
324         return rc;
325 }
326
327 int mds_dt_disconnect(struct obd_device *obd, int flags)
328 {
329         struct mds_obd *mds = &obd->u.mds;
330         int rc = 0;
331         ENTRY;
332
333         if (!IS_ERR(mds->mds_dt_obd) && mds->mds_dt_exp != NULL) {
334                 /* cleanup all llogging subsystems */
335                 rc = obd_llog_finish(obd, &obd->obd_llogs,
336                                      mds->mds_dt_desc.ld_tgt_count);
337                 if (rc)
338                         CERROR("failed to cleanup llogging subsystems\n");
339
340                 obd_register_observer(mds->mds_dt_obd, NULL);
341
342                 rc = obd_disconnect(mds->mds_dt_exp, flags);
343                 /* if obd_disconnect fails (probably because the
344                  * export was disconnected by class_disconnect_exports)
345                  * then we just need to drop our ref. */
346                 if (rc != 0)
347                         class_export_put(mds->mds_dt_exp);
348                 mds->mds_dt_exp = NULL;
349                 mds->mds_dt_obd = NULL;
350         }
351
352         RETURN(rc);
353 }
354
355 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
356                   void *karg, void *uarg)
357 {
358         static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
359         struct obd_device *obd = exp->exp_obd;
360         struct mds_obd *mds = &obd->u.mds;
361         struct obd_ioctl_data *data = karg;
362         struct lvfs_run_ctxt saved;
363         int rc = 0;
364         ENTRY;
365
366         CDEBUG(D_INFO, "ioctl cmd %x\n", cmd);
367         switch (cmd) {
368         case OBD_IOC_RECORD: {
369                 char *name = data->ioc_inlbuf1;
370                 if (mds->mds_cfg_llh)
371                         RETURN(-EBUSY);
372
373                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
374                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
375                                                 LLOG_CONFIG_ORIG_CTXT),
376                                &mds->mds_cfg_llh, NULL, name,
377                                OBD_LLOG_FL_CREATE);
378                 if (rc == 0)
379                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
380                                          &cfg_uuid);
381                 else
382                         mds->mds_cfg_llh = NULL;
383                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
384
385                 RETURN(rc);
386         }
387
388         case OBD_IOC_ENDRECORD: {
389                 if (!mds->mds_cfg_llh)
390                         RETURN(-EBADF);
391
392                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
393                 rc = llog_close(mds->mds_cfg_llh);
394                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
395
396                 mds->mds_cfg_llh = NULL;
397                 RETURN(rc);
398         }
399
400         case OBD_IOC_CLEAR_LOG: {
401                 char *name = data->ioc_inlbuf1;
402                 if (mds->mds_cfg_llh)
403                         RETURN(-EBUSY);
404
405                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
406                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
407                                                 LLOG_CONFIG_ORIG_CTXT),
408                                &mds->mds_cfg_llh, NULL, name,
409                                OBD_LLOG_FL_CREATE);
410                 if (rc == 0) {
411                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
412                                          NULL);
413
414                         rc = llog_destroy(mds->mds_cfg_llh);
415                         llog_free_handle(mds->mds_cfg_llh);
416                 }
417                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
418
419                 mds->mds_cfg_llh = NULL;
420                 RETURN(rc);
421         }
422
423         case OBD_IOC_DORECORD: {
424                 char *cfg_buf;
425                 struct llog_rec_hdr rec;
426                 if (!mds->mds_cfg_llh)
427                         RETURN(-EBADF);
428
429                 /* XXX - this probably should be a parameter to this ioctl.
430                  * For now, just use llh_max_transno for expediency. */
431                 rec.lrh_len = llog_data_len(data->ioc_plen1);
432
433                 if (data->ioc_type == LUSTRE_CFG_TYPE) {
434                         rec.lrh_type = OBD_CFG_REC;
435                 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
436                         rec.lrh_type = PTL_CFG_REC;
437                 } else {
438                         CERROR("unknown cfg record type:%d \n", data->ioc_type);
439                         RETURN(-EINVAL);
440                 }
441
442                 OBD_ALLOC(cfg_buf, data->ioc_plen1);
443                 if (cfg_buf == NULL)
444                         RETURN(-EINVAL);
445                 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
446                 if (rc) {
447                         OBD_FREE(cfg_buf, data->ioc_plen1);
448                         RETURN(rc);
449                 }
450
451                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
452                 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
453                                     cfg_buf, -1);
454                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
455
456                 OBD_FREE(cfg_buf, data->ioc_plen1);
457                 RETURN(rc);
458         }
459         case OBD_IOC_PARSE: {
460                 struct llog_ctxt *ctxt =
461                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
462                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
463                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
464                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
465                 if (rc)
466                         RETURN(rc);
467
468                 RETURN(rc);
469         }
470         case OBD_IOC_DUMP_LOG: {
471                 struct llog_ctxt *ctxt =
472                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
473                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
474                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
475                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
476
477                 RETURN(rc);
478         }
479         case OBD_IOC_SET_READONLY: {
480                 void *handle;
481                 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
482                 BDEVNAME_DECLARE_STORAGE(tmp);
483                 CERROR("setting device %s read-only\n",
484                        ll_bdevname(obd->u.mds.mds_sb, tmp));
485
486                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
487                 LASSERT(handle);
488                 rc = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 1);
489
490                 ll_set_rdonly(ll_sbdev(obd->u.mds.mds_sb));
491                 RETURN(0);
492         }
493
494         case OBD_IOC_CATLOGLIST: {
495                 int count = mds->mds_dt_desc.ld_tgt_count;
496                 rc = llog_catalog_list(obd, count, data);
497                 RETURN(rc);
498
499         }
500         case OBD_IOC_LLOG_CHECK:
501         case OBD_IOC_LLOG_CANCEL:
502         case OBD_IOC_LLOG_REMOVE: {
503                 struct llog_ctxt *ctxt =
504                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
505                 int rc2, valsize;
506                 __u32 group;
507
508                 obd_llog_finish(obd, &obd->obd_llogs,
509                                 mds->mds_dt_desc.ld_tgt_count);
510                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
511                 rc = llog_ioctl(ctxt, cmd, data);
512                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
513                 obd_llog_cat_initialize(obd, &obd->obd_llogs, 
514                                         mds->mds_dt_desc.ld_tgt_count,
515                                         CATLIST);
516                 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
517                 valsize = sizeof(group);
518                 rc2 = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
519                                    "mds_conn", valsize, &group);
520                 if (!rc)
521                         rc = rc2;
522                 RETURN(rc);
523         }
524         case OBD_IOC_LLOG_INFO:
525         case OBD_IOC_LLOG_PRINT: {
526                 struct llog_ctxt *ctxt =
527                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
528
529                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
530                 rc = llog_ioctl(ctxt, cmd, data);
531                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
532
533                 RETURN(rc);
534         }
535
536         case OBD_IOC_ABORT_RECOVERY:
537                 target_stop_recovery_thread(obd);
538                 RETURN(0);
539         case OBD_IOC_ROOT_SQUASH: {
540                 __u32 *p = (__u32 *) data->ioc_inlbuf1;
541
542                 if (data->ioc_inllen1 !=
543                     (sizeof(__u32) * 4 + sizeof(ptl_nid_t)))
544                         RETURN(-EINVAL);
545
546                 if (*p == 0) { /* get */
547                         p += 2;
548                         *p++ = mds->mds_squash_uid;
549                         *p++ = mds->mds_squash_gid;
550                         *((ptl_nid_t*) p) = mds->mds_nosquash_nid;
551                 } else { /* set */
552                         p += 2;
553                         mds->mds_squash_uid = *p++;
554                         mds->mds_squash_gid = *p++;
555                         mds->mds_nosquash_nid = *((ptl_nid_t*) p);
556                         CWARN("MDS: squash root to %d:%d, except nid 0x%llx\n",
557                                mds->mds_squash_uid, mds->mds_squash_gid,
558                                mds->mds_nosquash_nid);
559                 }
560                 RETURN(0);
561         }
562         default:
563                 CDEBUG(D_INFO, "unknown command %x\n", cmd);
564                 RETURN(-EINVAL);
565         }
566         RETURN(0);
567
568 }
569
570 struct mds_dt_sync_info {
571         struct obd_device *mlsi_obd;      /* the mds to sync */
572         struct obd_device *mlsi_watched;  /* new lov target */
573         int                mlsi_index;    /* index into mds_dt_objids */ 
574 };
575
576 int mds_dt_synchronize(void *data)
577 {
578         struct mds_dt_sync_info *mlsi = data;
579         struct llog_ctxt *ctxt;
580         struct obd_device *obd;
581         struct obd_device *watched;
582         struct mds_obd *mds;
583         struct obd_uuid *uuid;
584         obd_id vals[2];
585         unsigned long flags;
586         __u32  vallen;
587         __u32  group;
588         int old_count;
589         int count;
590         int index;
591         int rc;
592         char name[32] = "CATLIST";
593
594         lock_kernel();
595
596         ptlrpc_daemonize();
597
598         SIGNAL_MASK_LOCK(current, flags);
599         sigfillset(&current->blocked);
600         RECALC_SIGPENDING;
601         SIGNAL_MASK_UNLOCK(current, flags);
602
603         unlock_kernel();
604
605         obd = mlsi->mlsi_obd;
606         watched = mlsi->mlsi_watched;
607         index = mlsi->mlsi_index;
608
609         LASSERT(obd != NULL);
610         LASSERT(watched != NULL);
611
612         OBD_FREE(mlsi, sizeof(*mlsi));
613
614         mds = &obd->u.mds;
615         down(&mds->mds_orphan_recovery_sem);
616
617         uuid = &watched->u.cli.cl_import->imp_target_uuid;
618
619         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
620         rc = obd_set_info(watched->obd_self_export, strlen("mds_conn"),
621                           "mds_conn", sizeof(group), &group);
622         if (rc)
623                 GOTO(cleanup, rc);
624
625         old_count = mds->mds_dt_desc.ld_tgt_count;
626
627         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
628         if (rc)
629                 GOTO(cleanup, rc);
630
631         count = mds->mds_dt_desc.ld_tgt_count;
632         LASSERT(count >= old_count);
633
634         vallen = sizeof(vals[1]);
635         rc = obd_get_info(watched->obd_self_export, strlen("last_id"),
636                           "last_id", &vallen, &vals[1]);
637         if (rc)
638                 GOTO(cleanup, rc);
639
640         vals[0] = index;
641         rc = mds_dt_set_info(obd->obd_self_export, strlen("next_id"),
642                              "next_id", 2, vals);
643         if (rc)
644                 GOTO(cleanup, rc);
645
646         obd_llog_finish(obd, &obd->obd_llogs, old_count);
647         obd_llog_cat_initialize(obd, &obd->obd_llogs, count, name);
648
649         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
650         LASSERT(ctxt != NULL);
651
652         rc = llog_connect(ctxt, count, NULL, NULL, uuid);
653         if (rc != 0) {
654                 CERROR("%s: failed at llog_origin_connect: %d\n", 
655                        obd->obd_name, rc);
656                 GOTO(cleanup, rc);
657         }
658         
659         CWARN("MDS %s: %s now active, resetting orphans\n",
660               obd->obd_name, uuid->uuid);
661
662         rc = mds_dt_clearorphans(&obd->u.mds, uuid);
663         if (rc != 0) {
664                 CERROR("%s: failed at mds_dt_clearorphans(): %d\n", 
665                        obd->obd_name, rc);
666                 GOTO(cleanup, rc);
667         }
668         rc = 0;
669
670         EXIT;
671 cleanup:
672         up(&mds->mds_orphan_recovery_sem);
673         return rc;
674 }
675
676 int mds_dt_start_synchronize(struct obd_device *obd,
677                              struct obd_device *watched, 
678                              void *data)
679 {
680         struct mds_dt_sync_info *mlsi;
681         int rc;
682         
683         ENTRY;
684
685         OBD_ALLOC(mlsi, sizeof(*mlsi));
686         if (mlsi == NULL)
687                 RETURN(-ENOMEM);
688
689         mlsi->mlsi_obd = obd;
690         mlsi->mlsi_watched = watched;
691         mlsi->mlsi_index = (int)data;
692
693         rc = kernel_thread(mds_dt_synchronize, mlsi, CLONE_VM | CLONE_FILES);
694         if (rc < 0)
695                 CERROR("%s: error starting mds_dt_synchronize(): %d\n", 
696                        obd->obd_name, rc);
697         else {
698                 CDEBUG(D_HA, "%s: mds_dt_synchronize() thread: %d\n", 
699                        obd->obd_name, rc);
700                 rc = 0;
701         }
702
703         RETURN(rc);
704 }
705
706 int mds_notify(struct obd_device *obd, struct obd_device *watched,
707                int active, void *data)
708 {
709         int rc = 0;
710         ENTRY;
711
712         if (!active)
713                 RETURN(0);
714
715         if (!strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME))
716                 RETURN(0);
717
718         if (strcmp(watched->obd_type->typ_name, LUSTRE_OSC_NAME)) {
719                 CERROR("unexpected notification of %s %s!\n",
720                        watched->obd_type->typ_name, watched->obd_name);
721                 RETURN(-EINVAL);
722         }
723
724         if (obd->obd_recovering) {
725                 struct obd_uuid *uuid;
726                 uuid = &watched->u.cli.cl_import->imp_target_uuid;
727
728                 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
729                       obd->obd_name, uuid->uuid);
730         } else {
731                 rc = mds_dt_start_synchronize(obd, watched, data);
732         }
733         RETURN(rc);
734 }
735
736 int mds_dt_set_info(struct obd_export *exp, obd_count keylen,
737                     void *key, obd_count vallen, void *val)
738 {
739         struct obd_device *obd = class_exp2obd(exp);
740         struct mds_obd *mds = &obd->u.mds;
741         ENTRY;
742
743 #define KEY_IS(str) \
744         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
745
746         if (KEY_IS("next_id")) {
747                 obd_id *id = (obd_id *)val;
748                 int idx;
749
750                 /* XXX - this really should be vallen != (2 * sizeof(*id)) *
751                  * Just following the precedent set by lov_set_info.       */
752                 if (vallen != 2)
753                         RETURN(-EINVAL);
754
755                 idx = *id;
756                 if ((idx != *id) || (idx >= mds->mds_dt_desc.ld_tgt_count))
757                         RETURN(-EINVAL);
758
759                 CDEBUG(D_CONFIG, "idx: %d id: %llu\n", idx, *(id + 1));
760
761                 mds->mds_dt_objids[idx] = *++id;
762                 CDEBUG(D_CONFIG, "objid: %d: %lld\n", idx, *id);
763                 /* XXX - should we be writing this out here ? */
764                 RETURN(mds_dt_write_objids(obd));
765         }
766
767         RETURN(-EINVAL);
768 }
769
770 int mds_dt_update_config(struct obd_device *obd, int clean)
771 {
772         struct mds_obd *mds = &obd->u.mds;
773         struct lvfs_run_ctxt saved;
774         struct config_llog_instance cfg;
775         struct llog_ctxt *ctxt;
776         char *profile = mds->mds_profile, *name;
777         int rc, version, namelen;
778         ENTRY;
779
780         if (profile == NULL)
781                 RETURN(0);
782
783         cfg.cfg_instance = NULL;
784         cfg.cfg_uuid = mds->mds_dt_uuid;
785
786         namelen = strlen(profile) + 20; /* -clean-######### */
787         OBD_ALLOC(name, namelen);
788         if (name == NULL)
789                 RETURN(-ENOMEM);
790
791         if (clean) {
792                 version = mds->mds_config_version - 1;
793                 sprintf(name, "%s-clean-%d", profile, version);
794         } else {
795                 version = mds->mds_config_version + 1;
796                 sprintf(name, "%s-%d", profile, version);
797         }
798
799         CWARN("Applying configuration log %s\n", name);
800
801         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
802         ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
803         rc = class_config_process_llog(ctxt, name, &cfg);
804         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
805         if (rc == 0)
806                 mds->mds_config_version = version;
807         CWARN("Finished applying configuration log %s: %d\n", name, rc);
808
809         OBD_FREE(name, namelen);
810         RETURN(rc);
811 }
812
813 /* Convert the on-disk LOV EA structre.
814  * We always try to convert from an old LOV EA format to the common in-memory
815  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
816  * then convert back to the new on-disk format and save it back to disk
817  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
818  * to convert it each time this inode is accessed.
819  *
820  * This function is a bit interesting in the error handling.  We can safely
821  * ship the old lmm to the client in case of failure, since it uses the same
822  * obd_unpackmd() code and can do the conversion if the MDS fails for some
823  * reason.  We will not delete the old lmm data until we have written the
824  * new format lmm data in fsfilt_set_md(). */
825 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
826                        struct lov_mds_md *lmm, int lmm_size)
827 {
828         struct lov_stripe_md *lsm = NULL;
829         void *handle;
830         int rc, err;
831         ENTRY;
832
833         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
834                 RETURN(0);
835
836         CDEBUG(D_INODE, "converting LOV EA on %lu/%u from V0 to V1\n",      
837                 inode->i_ino, inode->i_generation);
838         rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, lmm, lmm_size);
839         if (rc < 0)
840                 GOTO(conv_end, rc);
841
842         rc = obd_packmd(obd->u.mds.mds_dt_exp, &lmm, lsm);
843         if (rc < 0)
844                 GOTO(conv_free, rc);
845         lmm_size = rc;
846
847         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
848         if (IS_ERR(handle)) {
849                 rc = PTR_ERR(handle);
850                 GOTO(conv_free, rc);
851         }
852
853         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
854         err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
855         if (!rc)
856                 rc = err ? err : lmm_size;
857         GOTO(conv_free, rc);
858 conv_free:
859         obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
860 conv_end:
861         return rc;
862 }
863
864 /* Must be called with i_sem held */
865 int mds_revalidate_lov_ea(struct obd_device *obd, struct inode *inode,
866                           struct lustre_msg *msg, int offset)
867 {
868         struct mds_obd *mds = &obd->u.mds;
869         struct obd_export *dt_exp = mds->mds_dt_exp;
870         struct lov_mds_md *lmm= NULL;
871         struct lov_stripe_md *lsm = NULL;
872         struct obdo *oa = NULL;
873         struct obd_trans_info oti = {0};
874         obd_valid valid = 0;
875         int lmm_size = 0, lsm_size = 0, err, rc;
876         void *handle;
877         ENTRY;
878
879         LASSERT(down_trylock(&inode->i_sem) != 0);
880
881         /* XXX - add way to know if EA is already up to date & return
882          * without doing anything. Easy to do since we get notified of
883          * LOV updates. */
884
885         lmm = lustre_msg_buf(msg, offset, 0);
886         if (lmm == NULL) {
887                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
888                        inode->i_ino);
889                 RETURN(0);
890         }
891         lmm_size = msg->buflens[offset];
892
893         rc = obd_unpackmd(dt_exp, &lsm, lmm, lmm_size);
894         if (rc < 0)
895                 RETURN(0);
896
897         lsm_size = rc;
898
899         LASSERT(lsm->lsm_magic == LOV_MAGIC);
900
901         oa = obdo_alloc();
902         if (oa == NULL)
903                 GOTO(out_lsm, rc = -ENOMEM);
904         oa->o_mode = S_IFREG | 0600;
905         oa->o_id = inode->i_ino;
906         oa->o_generation = inode->i_generation;
907         oa->o_uid = 0; /* must have 0 uid / gid on OST */
908         oa->o_gid = 0;
909         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
910
911         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
912                       OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID |
913                       OBD_MD_FLGROUP;
914         valid = OBD_MD_FLTYPE | OBD_MD_FLATIME | OBD_MD_FLMTIME |
915                 OBD_MD_FLCTIME;
916         obdo_from_inode(oa, inode, valid);
917
918         rc = obd_revalidate_md(dt_exp, oa, lsm, &oti);
919         if (rc == 0)
920                 GOTO(out_oa, rc);
921         if (rc < 0) {
922                 CERROR("Error validating LOV EA on %lu/%u: %d\n",
923                        inode->i_ino, inode->i_generation, rc);
924                 GOTO(out_oa, rc);
925         }
926
927         rc = obd_packmd(dt_exp, &lmm, lsm);
928         if (rc < 0)
929                 GOTO(out_oa, rc);
930         lmm_size = rc;
931
932         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
933         if (IS_ERR(handle)) {
934                 rc = PTR_ERR(handle);
935                 GOTO(out_oa, rc);
936         }
937
938         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
939         err = fsfilt_commit(obd, inode->i_sb, inode, handle, 0);
940         if (!rc)
941                 rc = err;
942
943         EXIT;
944 out_oa:
945         obdo_free(oa);
946 out_lsm:
947         obd_free_memmd(dt_exp, &lsm);
948         return rc;
949 }