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