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