Whamcloud - gitweb
* Compiles after merging b1_4
[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/lustre_idl.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_lov.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_fsfilt.h>
38
39 #include "mds_internal.h"
40
41 void mds_lov_update_objids(struct obd_device *obd, obd_id *ids)
42 {
43         struct mds_obd *mds = &obd->u.mds;
44         int i;
45         ENTRY;
46
47         lock_kernel();
48         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
49                 if (ids[i] > (mds->mds_lov_objids)[i])
50                         (mds->mds_lov_objids)[i] = ids[i];
51         unlock_kernel();
52         EXIT;
53 }
54
55 static int mds_lov_read_objids(struct obd_device *obd)
56 {
57         struct mds_obd *mds = &obd->u.mds;
58         obd_id *ids;
59         loff_t off = 0;
60         int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(*ids);
61         ENTRY;
62
63         if (mds->mds_lov_objids != NULL)
64                 RETURN(0);
65
66         OBD_ALLOC(ids, size);
67         if (ids == NULL)
68                 RETURN(-ENOMEM);
69         mds->mds_lov_objids = ids;
70
71         if (mds->mds_lov_objid_filp->f_dentry->d_inode->i_size == 0)
72                 RETURN(0);
73         rc = fsfilt_read_record(obd, mds->mds_lov_objid_filp, ids, size, &off);
74         if (rc < 0) {
75                 CERROR("Error reading objids %d\n", rc);
76         } else {
77                 mds->mds_lov_objids_valid = 1;
78                 rc = 0;
79         }
80
81         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
82                 CDEBUG(D_INFO, "read last object "LPU64" for idx %d\n",
83                        mds->mds_lov_objids[i], i);
84
85         RETURN(rc);
86 }
87
88 int mds_lov_write_objids(struct obd_device *obd)
89 {
90         struct mds_obd *mds = &obd->u.mds;
91         loff_t off = 0;
92         int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id);
93         ENTRY;
94
95         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
96                 CDEBUG(D_INFO, "writing last object "LPU64" for idx %d\n",
97                        mds->mds_lov_objids[i], i);
98
99         rc = fsfilt_write_record(obd, mds->mds_lov_objid_filp,
100                                  mds->mds_lov_objids, size, &off, 0);
101         RETURN(rc);
102 }
103
104 int mds_lov_clearorphans(struct mds_obd *mds, struct obd_uuid *ost_uuid)
105 {
106         int rc;
107         struct obdo oa;
108         struct obd_trans_info oti = {0};
109         struct lov_stripe_md  *empty_ea = NULL;
110         ENTRY;
111
112         LASSERT(mds->mds_lov_objids != NULL);
113
114         /* This create will in fact either create or destroy:  If the OST is
115          * missing objects below this ID, they will be created.  If it finds
116          * objects above this ID, they will be removed. */
117         memset(&oa, 0, sizeof(oa));
118         oa.o_valid = OBD_MD_FLFLAGS;
119         oa.o_flags = OBD_FL_DELORPHAN;
120         if (ost_uuid != NULL) {
121                 memcpy(&oa.o_inline, ost_uuid, sizeof(*ost_uuid));
122                 oa.o_valid |= OBD_MD_FLINLINE;
123         }
124         rc = obd_create(mds->mds_osc_exp, &oa, &empty_ea, &oti);
125
126         RETURN(rc);
127 }
128
129 /* update the LOV-OSC knowledge of the last used object id's */
130 int mds_lov_set_nextid(struct obd_device *obd)
131 {
132         struct mds_obd *mds = &obd->u.mds;
133         int rc;
134         ENTRY;
135
136         LASSERT(!obd->obd_recovering);
137
138         LASSERT(mds->mds_lov_objids != NULL);
139
140         rc = obd_set_info(mds->mds_osc_exp, strlen("next_id"), "next_id",
141                           mds->mds_lov_desc.ld_tgt_count, mds->mds_lov_objids);
142         RETURN(rc);
143 }
144
145 int mds_lov_connect(struct obd_device *obd, char * lov_name)
146 {
147         struct mds_obd *mds = &obd->u.mds;
148         struct lustre_handle conn = {0,};
149         int valsize;
150         int rc, i;
151         ENTRY;
152
153         if (IS_ERR(mds->mds_osc_obd))
154                 RETURN(PTR_ERR(mds->mds_osc_obd));
155
156         if (mds->mds_osc_obd)
157                 RETURN(0);
158
159         mds->mds_osc_obd = class_name2obd(lov_name);
160         if (!mds->mds_osc_obd) {
161                 CERROR("MDS cannot locate LOV %s\n", lov_name);
162                 mds->mds_osc_obd = ERR_PTR(-ENOTCONN);
163                 RETURN(-ENOTCONN);
164         }
165
166         rc = obd_connect(&conn, mds->mds_osc_obd, &obd->obd_uuid,
167                          NULL /* obd_connect_data */);
168         if (rc) {
169                 CERROR("MDS cannot connect to LOV %s (%d)\n", lov_name, rc);
170                 mds->mds_osc_obd = ERR_PTR(rc);
171                 RETURN(rc);
172         }
173         mds->mds_osc_exp = class_conn2export(&conn);
174
175         rc = obd_register_observer(mds->mds_osc_obd, obd);
176         if (rc) {
177                 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
178                        lov_name, rc);
179                 GOTO(err_discon, rc);
180         }
181
182         valsize = sizeof(mds->mds_lov_desc);
183         rc = obd_get_info(mds->mds_osc_exp, strlen("lovdesc") + 1, "lovdesc",
184                           &valsize, &mds->mds_lov_desc);
185         if (rc)
186                 GOTO(err_reg, rc);
187
188         mds->mds_max_mdsize = lov_mds_md_size(mds->mds_lov_desc.ld_tgt_count);
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         rc = mds_lov_read_objids(obd);
193         if (rc) {
194                 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
195                 GOTO(err_reg, rc);
196         }
197
198         rc = llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
199         if (rc) {
200                 CERROR("failed to initialize catalog %d\n", rc);
201                 GOTO(err_reg, rc);
202         }
203
204         /* If we're mounting this code for the first time on an existing FS,
205          * we need to populate the objids array from the real OST values */
206         if (!mds->mds_lov_objids_valid) {
207                 int size = sizeof(obd_id) * mds->mds_lov_desc.ld_tgt_count;
208                 rc = obd_get_info(mds->mds_osc_exp, strlen("last_id"),
209                                   "last_id", &size, mds->mds_lov_objids);
210                 if (!rc) {
211                         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
212                                 CWARN("got last object "LPU64" from OST %d\n",
213                                       mds->mds_lov_objids[i], i);
214                         mds->mds_lov_objids_valid = 1;
215                         rc = mds_lov_write_objids(obd);
216                         if (rc)
217                                 CERROR("got last objids from OSTs, but error "
218                                        "writing objids file: %d\n", rc);
219                 }
220         }
221
222         /* I want to see a callback happen when the OBD moves to a
223          * "For General Use" state, and that's when we'll call
224          * set_nextid().  The class driver can help us here, because
225          * it can use the obd_recovering flag to determine when the
226          * the OBD is full available. */
227         if (!obd->obd_recovering)
228                 rc = mds_postrecov(obd);
229         RETURN(rc);
230
231 err_reg:
232         obd_register_observer(mds->mds_osc_obd, NULL);
233 err_discon:
234         obd_disconnect(mds->mds_osc_exp);
235         mds->mds_osc_exp = NULL;
236         mds->mds_osc_obd = ERR_PTR(rc);
237         RETURN(rc);
238 }
239
240 int mds_lov_disconnect(struct obd_device *obd)
241 {
242         struct mds_obd *mds = &obd->u.mds;
243         int rc = 0;
244         ENTRY;
245
246         if (!IS_ERR(mds->mds_osc_obd) && mds->mds_osc_exp != NULL) {
247                 obd_register_observer(mds->mds_osc_obd, NULL);
248
249                 /* The actual disconnect of the mds_lov will be called from
250                  * class_disconnect_exports from mds_lov_clean. So we have to
251                  * ensure that class_cleanup doesn't fail due to the extra ref
252                  * we're holding now. The mechanism to do that already exists -
253                  * the obd_force flag. We'll drop the final ref to the
254                  * mds_osc_exp in mds_cleanup. */
255                 mds->mds_osc_obd->obd_force = 1;
256         }
257
258         RETURN(rc);
259 }
260
261 /* for consistency, let's make the lov and the lov's
262  * osc's see the same cleanup flags as our mds */
263 void mds_lov_set_cleanup_flags(struct obd_device *obd)
264 {
265         struct mds_obd *mds = &obd->u.mds;
266         struct lov_obd *lov;
267
268         if (IS_ERR(mds->mds_osc_obd) || (mds->mds_osc_exp == NULL))
269                 return;
270
271         lov = &mds->mds_osc_obd->u.lov;
272         mds->mds_osc_obd->obd_force = obd->obd_force;
273         mds->mds_osc_obd->obd_fail = obd->obd_fail;
274         if (lov->tgts) {
275                 struct obd_export *osc_exp;
276                 int i;
277                 spin_lock(&lov->lov_lock);
278                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
279                         if (lov->tgts[i].ltd_exp != NULL) {
280                                 osc_exp = lov->tgts[i].ltd_exp;
281                                 osc_exp->exp_obd->obd_force = obd->obd_force;
282                                 osc_exp->exp_obd->obd_fail = obd->obd_fail;
283                         }
284                 }
285                 spin_unlock(&lov->lov_lock);
286         }
287 }
288
289 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
290                   void *karg, void *uarg)
291 {
292         static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
293         struct obd_device *obd = exp->exp_obd;
294         struct mds_obd *mds = &obd->u.mds;
295         struct obd_ioctl_data *data = karg;
296         struct lvfs_run_ctxt saved;
297         int rc = 0;
298
299         ENTRY;
300         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
301
302         switch (cmd) {
303         case OBD_IOC_RECORD: {
304                 char *name = data->ioc_inlbuf1;
305                 if (mds->mds_cfg_llh)
306                         RETURN(-EBUSY);
307
308                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
309                 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
310                                  &mds->mds_cfg_llh, NULL, name);
311                 if (rc == 0)
312                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
313                                          &cfg_uuid);
314                 else
315                         mds->mds_cfg_llh = NULL;
316                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
317
318                 RETURN(rc);
319         }
320
321         case OBD_IOC_ENDRECORD: {
322                 if (!mds->mds_cfg_llh)
323                         RETURN(-EBADF);
324
325                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
326                 rc = llog_close(mds->mds_cfg_llh);
327                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
328
329                 mds->mds_cfg_llh = NULL;
330                 RETURN(rc);
331         }
332
333         case OBD_IOC_CLEAR_LOG: {
334                 char *name = data->ioc_inlbuf1;
335                 if (mds->mds_cfg_llh)
336                         RETURN(-EBUSY);
337
338                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
339                 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT),
340                                  &mds->mds_cfg_llh, NULL, name);
341                 if (rc == 0) {
342                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
343                                          NULL);
344
345                         rc = llog_destroy(mds->mds_cfg_llh);
346                         llog_free_handle(mds->mds_cfg_llh);
347                 }
348                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
349
350                 mds->mds_cfg_llh = NULL;
351                 RETURN(rc);
352         }
353
354         case OBD_IOC_DORECORD: {
355                 char *cfg_buf;
356                 struct llog_rec_hdr rec;
357                 if (!mds->mds_cfg_llh)
358                         RETURN(-EBADF);
359
360                 rec.lrh_len = llog_data_len(data->ioc_plen1);
361
362                 if (data->ioc_type == LUSTRE_CFG_TYPE) {
363                         rec.lrh_type = OBD_CFG_REC;
364                 } else {
365                         CERROR("unknown cfg record type:%d \n", data->ioc_type);
366                         RETURN(-EINVAL);
367                 }
368
369                 OBD_ALLOC(cfg_buf, data->ioc_plen1);
370                 if (cfg_buf == NULL)
371                         RETURN(-EINVAL);
372                 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
373                 if (rc) {
374                         OBD_FREE(cfg_buf, data->ioc_plen1);
375                         RETURN(rc);
376                 }
377
378                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
379                 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
380                                     cfg_buf, -1);
381                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
382
383                 OBD_FREE(cfg_buf, data->ioc_plen1);
384                 RETURN(rc);
385         }
386
387         case OBD_IOC_PARSE: {
388                 struct llog_ctxt *ctxt =
389                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
390                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
391                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
392                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
393                 if (rc)
394                         RETURN(rc);
395
396                 RETURN(rc);
397         }
398
399         case OBD_IOC_DUMP_LOG: {
400                 struct llog_ctxt *ctxt =
401                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
402                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
403                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
404                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
405                 if (rc)
406                         RETURN(rc);
407
408                 RETURN(rc);
409         }
410
411         case OBD_IOC_SYNC: {
412                 CDEBUG(D_HA, "syncing mds %s\n", obd->obd_name);
413                 rc = fsfilt_sync(obd, obd->u.mds.mds_sb);
414                 RETURN(rc);
415         }
416
417         case OBD_IOC_SET_READONLY: {
418                 void *handle;
419                 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
420                 BDEVNAME_DECLARE_STORAGE(tmp);
421                 CERROR("*** setting device %s read-only ***\n",
422                        ll_bdevname(obd->u.mds.mds_sb, tmp));
423
424                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
425                 if (!IS_ERR(handle))
426                         rc = fsfilt_commit(obd, inode, handle, 1);
427
428                 CDEBUG(D_HA, "syncing mds %s\n", obd->obd_name);
429                 rc = fsfilt_sync(obd, obd->u.mds.mds_sb);
430
431                 lvfs_set_rdonly(lvfs_sbdev(obd->u.mds.mds_sb));
432                 RETURN(0);
433         }
434
435         case OBD_IOC_CATLOGLIST: {
436                 int count = mds->mds_lov_desc.ld_tgt_count;
437                 rc = llog_catalog_list(obd, count, data);
438                 RETURN(rc);
439
440         }
441         case OBD_IOC_LLOG_CHECK:
442         case OBD_IOC_LLOG_CANCEL:
443         case OBD_IOC_LLOG_REMOVE: {
444                 struct llog_ctxt *ctxt =
445                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
446                 int rc2;
447
448                 obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
449                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
450                 rc = llog_ioctl(ctxt, cmd, data);
451                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
452                 llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
453                 rc2 = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"),
454                                    "mds_conn", 0, NULL);
455                 if (!rc)
456                         rc = rc2;
457                 RETURN(rc);
458         }
459         case OBD_IOC_LLOG_INFO:
460         case OBD_IOC_LLOG_PRINT: {
461                 struct llog_ctxt *ctxt =
462                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
463
464                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
465                 rc = llog_ioctl(ctxt, cmd, data);
466                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
467
468                 RETURN(rc);
469         }
470
471         case OBD_IOC_ABORT_RECOVERY:
472                 CERROR("aborting recovery for device %s\n", obd->obd_name);
473                 target_abort_recovery(obd);
474                 RETURN(0);
475
476         default:
477                 CDEBUG(D_INFO, "unknown command %x\n", cmd);
478                 RETURN(-EINVAL);
479         }
480         RETURN(0);
481
482 }
483
484 struct mds_lov_sync_info {
485         struct obd_device *mlsi_obd; /* the lov device to sync */
486         struct obd_uuid   *mlsi_uuid;  /* target to sync */
487 };
488
489 int mds_lov_synchronize(void *data)
490 {
491         struct mds_lov_sync_info *mlsi = data;
492         struct obd_device *obd;
493         struct obd_uuid *uuid;
494         unsigned long flags;
495         int rc = 0;
496
497         lock_kernel();
498         ptlrpc_daemonize();
499
500         SIGNAL_MASK_LOCK(current, flags);
501         sigfillset(&current->blocked);
502         RECALC_SIGPENDING;
503         SIGNAL_MASK_UNLOCK(current, flags);
504         unlock_kernel();
505
506         obd = mlsi->mlsi_obd;
507         uuid = mlsi->mlsi_uuid;
508
509         OBD_FREE(mlsi, sizeof(*mlsi));
510
511         LASSERT(obd != NULL);
512         LASSERT(uuid != NULL);
513
514         rc = obd_set_info(obd->u.mds.mds_osc_exp, strlen("mds_conn"),
515                           "mds_conn", 0, uuid);
516         if (rc != 0)
517                 GOTO(out, rc);
518
519         rc = llog_connect(llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT),
520                           obd->u.mds.mds_lov_desc.ld_tgt_count,
521                           NULL, NULL, uuid);
522         if (rc != 0) {
523                 CERROR("%s: failed at llog_origin_connect: %d\n",
524                        obd->obd_name, rc);
525                 GOTO(out, rc);
526         }
527
528         CWARN("MDS %s: %s now active, resetting orphans\n",
529               obd->obd_name, uuid->uuid);
530         rc = mds_lov_clearorphans(&obd->u.mds, uuid);
531         if (rc != 0) {
532                 CERROR("%s: failed at mds_lov_clearorphans: %d\n",
533                        obd->obd_name, rc);
534                 GOTO(out, rc);
535         }
536
537 out:
538         class_export_put(obd->obd_self_export);
539         RETURN(rc);
540 }
541
542 int mds_lov_start_synchronize(struct obd_device *obd, struct obd_uuid *uuid)
543 {
544         struct mds_lov_sync_info *mlsi;
545         int rc;
546
547         ENTRY;
548
549         OBD_ALLOC(mlsi, sizeof(*mlsi));
550         if (mlsi == NULL)
551                 RETURN(-ENOMEM);
552
553         mlsi->mlsi_obd = obd;
554         mlsi->mlsi_uuid = uuid;
555         
556         /* We need to lock the mds in place for our new thread context. */
557         class_export_get(obd->obd_self_export);
558
559         rc = kernel_thread(mds_lov_synchronize, mlsi, CLONE_VM | CLONE_FILES);
560         if (rc < 0)
561                 CERROR("%s: error starting mds_lov_synchronize: %d\n",
562                        obd->obd_name, rc);
563         else {
564                 CDEBUG(D_HA, "%s: mds_lov_synchronize thread: %d\n",
565                        obd->obd_name, rc);
566                 rc = 0;
567         }
568
569         RETURN(rc);
570 }
571
572 int mds_notify(struct obd_device *obd, struct obd_device *watched, int active)
573 {
574         struct obd_uuid *uuid;
575         int rc = 0;
576         ENTRY;
577
578         if (!active)
579                 RETURN(0);
580
581         if (strcmp(watched->obd_type->typ_name, "osc")) {
582                 CERROR("unexpected notification of %s %s!\n",
583                        watched->obd_type->typ_name, watched->obd_name);
584                 RETURN(-EINVAL);
585         }
586
587         uuid = &watched->u.cli.cl_import->imp_target_uuid;
588         if (obd->obd_recovering) {
589                 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
590                       obd->obd_name, uuid->uuid);
591         } else {
592                 LASSERT(llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT) != NULL);
593                 
594                 rc = mds_lov_start_synchronize(obd, uuid);
595         }
596         RETURN(rc);
597 }
598
599 /* Convert the on-disk LOV EA structre.
600  * We always try to convert from an old LOV EA format to the common in-memory
601  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
602  * then convert back to the new on-disk format and save it back to disk
603  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
604  * to convert it each time this inode is accessed.
605  *
606  * This function is a bit interesting in the error handling.  We can safely
607  * ship the old lmm to the client in case of failure, since it uses the same
608  * obd_unpackmd() code and can do the conversion if the MDS fails for some
609  * reason.  We will not delete the old lmm data until we have written the
610  * new format lmm data in fsfilt_set_md(). */
611 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
612                        struct lov_mds_md *lmm, int lmm_size)
613 {
614         struct lov_stripe_md *lsm = NULL;
615         void *handle;
616         int rc, err;
617         ENTRY;
618
619         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
620                 RETURN(0);
621
622         CDEBUG(D_INODE, "converting LOV EA on %lu/%u from %#08x to %#08x\n",
623                inode->i_ino, inode->i_generation, le32_to_cpu(lmm->lmm_magic),
624                LOV_MAGIC);
625         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, lmm, lmm_size);
626         if (rc < 0)
627                 GOTO(conv_end, rc);
628
629         rc = obd_packmd(obd->u.mds.mds_osc_exp, &lmm, lsm);
630         if (rc < 0)
631                 GOTO(conv_free, rc);
632         lmm_size = rc;
633
634         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
635         if (IS_ERR(handle)) {
636                 rc = PTR_ERR(handle);
637                 GOTO(conv_free, rc);
638         }
639
640         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size);
641
642         err = fsfilt_commit(obd, inode, handle, 0);
643         if (!rc)
644                 rc = err ? err : lmm_size;
645         GOTO(conv_free, rc);
646 conv_free:
647         obd_free_memmd(obd->u.mds.mds_osc_exp, &lsm);
648 conv_end:
649         return rc;
650 }
651
652 void mds_objids_from_lmm(obd_id *ids, struct lov_mds_md *lmm,
653                          struct lov_desc *desc)
654 {
655         int i;
656         for (i = 0; i < le32_to_cpu(lmm->lmm_stripe_count); i++) {
657                 ids[le32_to_cpu(lmm->lmm_objects[i].l_ost_idx)] =
658                         le64_to_cpu(lmm->lmm_objects[i].l_object_id);
659         }
660 }
661