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