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