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