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