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