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