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