Whamcloud - gitweb
LU-14182 lov: cancel layout lock on replay deadlock
[fs/lustre-release.git] / lustre / lov / lov_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_object for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOV
39
40 #include <linux/random.h>
41
42 #include "lov_cl_internal.h"
43
44 static inline struct lov_device *lov_object_dev(struct lov_object *obj)
45 {
46         return lu2lov_dev(obj->lo_cl.co_lu.lo_dev);
47 }
48
49 /** \addtogroup lov
50  *  @{
51  */
52
53 /*****************************************************************************
54  *
55  * Layout operations.
56  *
57  */
58
59 struct lov_layout_operations {
60         int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
61                         struct lov_object *lov, struct lov_stripe_md *lsm,
62                         const struct cl_object_conf *conf,
63                         union lov_layout_state *state);
64         int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
65                            union lov_layout_state *state);
66         void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
67                          union lov_layout_state *state);
68         int  (*llo_print)(const struct lu_env *env, void *cookie,
69                           lu_printer_t p, const struct lu_object *o);
70         int  (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
71                               struct cl_page *page, pgoff_t index);
72         int  (*llo_lock_init)(const struct lu_env *env,
73                               struct cl_object *obj, struct cl_lock *lock,
74                               const struct cl_io *io);
75         int  (*llo_io_init)(const struct lu_env *env,
76                             struct cl_object *obj, struct cl_io *io);
77         int  (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
78                             struct cl_attr *attr);
79         int  (*llo_flush)(const struct lu_env *env, struct cl_object *obj,
80                           struct ldlm_lock *lock);
81 };
82
83 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
84 static struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov);
85
86 static void lov_lsm_put(struct lov_stripe_md *lsm)
87 {
88         if (lsm != NULL)
89                 lov_free_memmd(&lsm);
90 }
91
92 /*****************************************************************************
93  *
94  * Lov object layout operations.
95  *
96  */
97
98 static struct cl_object *lov_sub_find(const struct lu_env *env,
99                                       struct cl_device *dev,
100                                       const struct lu_fid *fid,
101                                       const struct cl_object_conf *conf)
102 {
103         struct lu_object *o;
104
105         ENTRY;
106
107         o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
108         LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
109         RETURN(lu2cl(o));
110 }
111
112 static int lov_page_slice_fixup(struct lov_object *lov,
113                                 struct cl_object *stripe)
114 {
115         struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
116         struct cl_object *o;
117
118         if (stripe == NULL)
119                 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
120                        cfs_size_round(sizeof(struct lov_page));
121
122         cl_object_for_each(o, stripe)
123                 o->co_slice_off += hdr->coh_page_bufsize;
124
125         return cl_object_header(stripe)->coh_page_bufsize;
126 }
127
128 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
129                         struct cl_object *subobj, struct lov_oinfo *oinfo,
130                         int idx)
131 {
132         struct cl_object_header *hdr;
133         struct cl_object_header *subhdr;
134         struct cl_object_header *parent;
135         int entry = lov_comp_entry(idx);
136         int stripe = lov_comp_stripe(idx);
137         int result;
138
139         if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
140                 /* For sanity:test_206.
141                  * Do not leave the object in cache to avoid accessing
142                  * freed memory. This is because osc_object is referring to
143                  * lov_oinfo of lsm_stripe_data which will be freed due to
144                  * this failure. */
145                 cl_object_kill(env, subobj);
146                 cl_object_put(env, subobj);
147                 return -EIO;
148         }
149
150         hdr = cl_object_header(lov2cl(lov));
151         subhdr = cl_object_header(subobj);
152
153         CDEBUG(D_INODE, DFID"@%p[%d:%d] -> "DFID"@%p: ostid: "DOSTID
154                " ost idx: %d gen: %d\n",
155                PFID(lu_object_fid(&subobj->co_lu)), subhdr, entry, stripe,
156                PFID(lu_object_fid(lov2lu(lov))), hdr, POSTID(&oinfo->loi_oi),
157                oinfo->loi_ost_idx, oinfo->loi_ost_gen);
158
159         /* reuse ->coh_attr_guard to protect coh_parent change */
160         spin_lock(&subhdr->coh_attr_guard);
161         parent = subhdr->coh_parent;
162         if (parent == NULL) {
163                 struct lovsub_object *lso = cl2lovsub(subobj);
164
165                 subhdr->coh_parent = hdr;
166                 spin_unlock(&subhdr->coh_attr_guard);
167                 subhdr->coh_nesting = hdr->coh_nesting + 1;
168                 lu_object_ref_add(&subobj->co_lu, "lov-parent", lov);
169                 lso->lso_super = lov;
170                 lso->lso_index = idx;
171                 result = 0;
172         } else {
173                 struct lu_object  *old_obj;
174                 struct lov_object *old_lov;
175                 unsigned int mask = D_INODE;
176
177                 spin_unlock(&subhdr->coh_attr_guard);
178                 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
179                 LASSERT(old_obj != NULL);
180                 old_lov = cl2lov(lu2cl(old_obj));
181                 if (test_bit(LO_LAYOUT_INVALID, &old_lov->lo_obj_flags)) {
182                         /* the object's layout has already changed but isn't
183                          * refreshed */
184                         lu_object_unhash(env, &subobj->co_lu);
185                         result = -EAGAIN;
186                 } else {
187                         mask = D_ERROR;
188                         result = -EIO;
189                 }
190
191                 LU_OBJECT_DEBUG(mask, env, &subobj->co_lu,
192                                 "stripe %d is already owned.", idx);
193                 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.");
194                 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
195                 cl_object_put(env, subobj);
196         }
197         return result;
198 }
199
200 static int lov_init_raid0(const struct lu_env *env, struct lov_device *dev,
201                           struct lov_object *lov, unsigned int index,
202                           const struct cl_object_conf *conf,
203                           struct lov_layout_entry *lle)
204 {
205         struct lov_layout_raid0 *r0 = &lle->lle_raid0;
206         struct lov_thread_info *lti = lov_env_info(env);
207         struct cl_object_conf *subconf = &lti->lti_stripe_conf;
208         struct lu_fid *ofid = &lti->lti_fid;
209         struct cl_object *stripe;
210         struct lov_stripe_md_entry *lse  = lov_lse(lov, index);
211         int result;
212         int psz, sz;
213         int i;
214
215         ENTRY;
216
217         spin_lock_init(&r0->lo_sub_lock);
218         r0->lo_nr = lse->lsme_stripe_count;
219         r0->lo_trunc_stripeno = -1;
220
221         OBD_ALLOC_PTR_ARRAY_LARGE(r0->lo_sub, r0->lo_nr);
222         if (r0->lo_sub == NULL)
223                 GOTO(out, result = -ENOMEM);
224
225         psz = 0;
226         result = 0;
227         memset(subconf, 0, sizeof(*subconf));
228
229         /*
230          * Create stripe cl_objects.
231          */
232         for (i = 0; i < r0->lo_nr; ++i) {
233                 struct cl_device *subdev;
234                 struct lov_oinfo *oinfo = lse->lsme_oinfo[i];
235                 int ost_idx = oinfo->loi_ost_idx;
236
237                 if (lov_oinfo_is_dummy(oinfo))
238                         continue;
239
240                 result = ostid_to_fid(ofid, &oinfo->loi_oi, oinfo->loi_ost_idx);
241                 if (result != 0)
242                         GOTO(out, result);
243
244                 if (dev->ld_target[ost_idx] == NULL) {
245                         CERROR("%s: OST %04x is not initialized\n",
246                                lov2obd(dev->ld_lov)->obd_name, ost_idx);
247                         GOTO(out, result = -EIO);
248                 }
249
250                 subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
251                 subconf->u.coc_oinfo = oinfo;
252                 LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
253                 /* In the function below, .hs_keycmp resolves to
254                  * lu_obj_hop_keycmp() */
255                 /* coverity[overrun-buffer-val] */
256                 stripe = lov_sub_find(env, subdev, ofid, subconf);
257                 if (IS_ERR(stripe))
258                         GOTO(out, result = PTR_ERR(stripe));
259
260                 result = lov_init_sub(env, lov, stripe, oinfo,
261                                       lov_comp_index(index, i));
262                 if (result == -EAGAIN) { /* try again */
263                         --i;
264                         result = 0;
265                         continue;
266                 }
267
268                 if (result == 0) {
269                         r0->lo_sub[i] = cl2lovsub(stripe);
270
271                         sz = lov_page_slice_fixup(lov, stripe);
272                         LASSERT(ergo(psz > 0, psz == sz));
273                         psz = sz;
274                 }
275         }
276         if (result == 0)
277                 result = psz;
278 out:
279         RETURN(result);
280 }
281
282 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
283                                struct lov_layout_raid0 *r0,
284                                struct lovsub_object *los, int idx)
285 {
286         struct cl_object        *sub;
287         struct lu_site          *site;
288         wait_queue_head_t *wq;
289
290         LASSERT(r0->lo_sub[idx] == los);
291
292         sub = lovsub2cl(los);
293         site = sub->co_lu.lo_dev->ld_site;
294         wq = lu_site_wq_from_fid(site, &sub->co_lu.lo_header->loh_fid);
295
296         cl_object_kill(env, sub);
297         /* release a reference to the sub-object and ... */
298         lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
299         cl_object_put(env, sub);
300
301         /* ... wait until it is actually destroyed---sub-object clears its
302          * ->lo_sub[] slot in lovsub_object_free() */
303         wait_event(*wq, r0->lo_sub[idx] != los);
304         LASSERT(r0->lo_sub[idx] == NULL);
305 }
306
307 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
308                              struct lov_layout_entry *lle)
309 {
310         struct lov_layout_raid0 *r0 = &lle->lle_raid0;
311
312         ENTRY;
313
314         if (r0->lo_sub != NULL) {
315                 int i;
316
317                 for (i = 0; i < r0->lo_nr; ++i) {
318                         struct lovsub_object *los = r0->lo_sub[i];
319
320                         if (los != NULL) {
321                                 cl_object_prune(env, &los->lso_cl);
322                                 /*
323                                  * If top-level object is to be evicted from
324                                  * the cache, so are its sub-objects.
325                                  */
326                                 lov_subobject_kill(env, lov, r0, los, i);
327                         }
328                 }
329         }
330
331         EXIT;
332 }
333
334 static void lov_fini_raid0(const struct lu_env *env,
335                            struct lov_layout_entry *lle)
336 {
337         struct lov_layout_raid0 *r0 = &lle->lle_raid0;
338
339         if (r0->lo_sub != NULL) {
340                 OBD_FREE_PTR_ARRAY_LARGE(r0->lo_sub, r0->lo_nr);
341                 r0->lo_sub = NULL;
342         }
343 }
344
345 static int lov_print_raid0(const struct lu_env *env, void *cookie,
346                            lu_printer_t p, const struct lov_layout_entry *lle)
347 {
348         const struct lov_layout_raid0 *r0 = &lle->lle_raid0;
349         int i;
350
351         for (i = 0; i < r0->lo_nr; ++i) {
352                 struct lu_object *sub;
353
354                 if (r0->lo_sub[i] != NULL) {
355                         sub = lovsub2lu(r0->lo_sub[i]);
356                         lu_object_print(env, cookie, p, sub);
357                 } else {
358                         (*p)(env, cookie, "sub %d absent\n", i);
359                 }
360         }
361         return 0;
362 }
363
364 static int lov_attr_get_raid0(const struct lu_env *env, struct lov_object *lov,
365                               unsigned int index, struct lov_layout_entry *lle,
366                               struct cl_attr **lov_attr)
367 {
368         struct lov_layout_raid0 *r0 = &lle->lle_raid0;
369         struct lov_stripe_md *lsm = lov->lo_lsm;
370         struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb;
371         struct cl_attr *attr = &r0->lo_attr;
372         __u64 kms = 0;
373         int result = 0;
374
375         if (r0->lo_attr_valid) {
376                 *lov_attr = attr;
377                 return 0;
378         }
379
380         memset(lvb, 0, sizeof(*lvb));
381
382         /* XXX: timestamps can be negative by sanity:test_39m,
383          * how can it be? */
384         lvb->lvb_atime = LLONG_MIN;
385         lvb->lvb_ctime = LLONG_MIN;
386         lvb->lvb_mtime = LLONG_MIN;
387
388         /*
389          * XXX that should be replaced with a loop over sub-objects,
390          * doing cl_object_attr_get() on them. But for now, let's
391          * reuse old lov code.
392          */
393
394         /*
395          * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
396          * happy. It's not needed, because new code uses
397          * ->coh_attr_guard spin-lock to protect consistency of
398          * sub-object attributes.
399          */
400         lov_stripe_lock(lsm);
401         result = lov_merge_lvb_kms(lsm, index, lvb, &kms);
402         lov_stripe_unlock(lsm);
403         if (result == 0) {
404                 cl_lvb2attr(attr, lvb);
405                 attr->cat_kms = kms;
406                 r0->lo_attr_valid = 1;
407                 *lov_attr = attr;
408         }
409
410         return result;
411 }
412
413 static struct lov_comp_layout_entry_ops raid0_ops = {
414         .lco_init      = lov_init_raid0,
415         .lco_fini      = lov_fini_raid0,
416         .lco_getattr   = lov_attr_get_raid0,
417 };
418
419 static int lov_attr_get_dom(const struct lu_env *env, struct lov_object *lov,
420                             unsigned int index, struct lov_layout_entry *lle,
421                             struct cl_attr **lov_attr)
422 {
423         struct lov_layout_dom *dom = &lle->lle_dom;
424         struct lov_oinfo *loi = dom->lo_loi;
425         struct cl_attr *attr = &dom->lo_dom_r0.lo_attr;
426
427         if (dom->lo_dom_r0.lo_attr_valid) {
428                 *lov_attr = attr;
429                 return 0;
430         }
431
432         if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks))
433                 return OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
434
435         cl_lvb2attr(attr, &loi->loi_lvb);
436
437         /* DoM component size can be bigger than stripe size after
438          * client's setattr RPC, so do not count anything beyond
439          * component end. Alternatively, check that limit on server
440          * and do not allow size overflow there. */
441         if (attr->cat_size > lle->lle_extent->e_end)
442                 attr->cat_size = lle->lle_extent->e_end;
443
444         attr->cat_kms = attr->cat_size;
445
446         dom->lo_dom_r0.lo_attr_valid = 1;
447         *lov_attr = attr;
448
449         return 0;
450 }
451
452 /**
453  * Lookup FLD to get MDS index of the given DOM object FID.
454  *
455  * \param[in]  ld       LOV device
456  * \param[in]  fid      FID to lookup
457  * \param[out] nr       index in MDC array to return back
458  *
459  * \retval              0 and \a mds filled with MDS index if successful
460  * \retval              negative value on error
461  */
462 static int lov_fld_lookup(struct lov_device *ld, const struct lu_fid *fid,
463                           __u32 *nr)
464 {
465         __u32 mds_idx;
466         int i, rc;
467
468         ENTRY;
469
470         rc = fld_client_lookup(&ld->ld_lmv->u.lmv.lmv_fld, fid_seq(fid),
471                                &mds_idx, LU_SEQ_RANGE_MDT, NULL);
472         if (rc) {
473                 CERROR("%s: error while looking for mds number. Seq %#llx"
474                        ", err = %d\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
475                        fid_seq(fid), rc);
476                 RETURN(rc);
477         }
478
479         CDEBUG(D_INODE, "FLD lookup got mds #%x for fid="DFID"\n",
480                mds_idx, PFID(fid));
481
482         /* find proper MDC device in the array */
483         for (i = 0; i < ld->ld_md_tgts_nr; i++) {
484                 if (ld->ld_md_tgts[i].ldm_mdc != NULL &&
485                     ld->ld_md_tgts[i].ldm_idx == mds_idx)
486                         break;
487         }
488
489         if (i == ld->ld_md_tgts_nr) {
490                 CERROR("%s: cannot find corresponding MDC device for mds #%x "
491                        "for fid="DFID"\n", lu_dev_name(cl2lu_dev(&ld->ld_cl)),
492                        mds_idx, PFID(fid));
493                 rc = -EINVAL;
494         } else {
495                 *nr = i;
496         }
497         RETURN(rc);
498 }
499
500 /**
501  * Implementation of lov_comp_layout_entry_ops::lco_init for DOM object.
502  *
503  * Init the DOM object for the first time. It prepares also RAID0 entry
504  * for it to use in common methods with ordinary RAID0 layout entries.
505  *
506  * \param[in] env       execution environment
507  * \param[in] dev       LOV device
508  * \param[in] lov       LOV object
509  * \param[in] index     Composite layout entry index in LSM
510  * \param[in] lle       Composite LOV layout entry
511  */
512 static int lov_init_dom(const struct lu_env *env, struct lov_device *dev,
513                         struct lov_object *lov, unsigned int index,
514                         const struct cl_object_conf *conf,
515                         struct lov_layout_entry *lle)
516 {
517         struct lov_thread_info *lti = lov_env_info(env);
518         struct lov_stripe_md_entry *lsme = lov_lse(lov, index);
519         struct cl_object *clo;
520         struct lu_object *o = lov2lu(lov);
521         const struct lu_fid *fid = lu_object_fid(o);
522         struct cl_device *mdcdev;
523         struct lov_oinfo *loi = NULL;
524         struct cl_object_conf *sconf = &lti->lti_stripe_conf;
525         int rc;
526         __u32 idx = 0;
527
528         ENTRY;
529
530         /* DOM entry may be not zero index due to FLR but must start from 0 */
531         if (unlikely(lle->lle_extent->e_start != 0)) {
532                 CERROR("%s: DOM entry must be the first stripe in a mirror\n",
533                        lov2obd(dev->ld_lov)->obd_name);
534                 dump_lsm(D_ERROR, lov->lo_lsm);
535                 RETURN(-EINVAL);
536         }
537
538         /* find proper MDS device */
539         rc = lov_fld_lookup(dev, fid, &idx);
540         if (rc)
541                 RETURN(rc);
542
543         LASSERTF(dev->ld_md_tgts[idx].ldm_mdc != NULL,
544                  "LOV md target[%u] is NULL\n", idx);
545
546         /* check lsm is DOM, more checks are needed */
547         LASSERT(lsme->lsme_stripe_count == 0);
548
549         /*
550          * Create lower cl_objects.
551          */
552         mdcdev = dev->ld_md_tgts[idx].ldm_mdc;
553
554         LASSERTF(mdcdev != NULL, "non-initialized mdc subdev\n");
555
556         /* DoM object has no oinfo in LSM entry, create it exclusively */
557         OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
558         if (loi == NULL)
559                 RETURN(-ENOMEM);
560
561         fid_to_ostid(lu_object_fid(lov2lu(lov)), &loi->loi_oi);
562
563         sconf->u.coc_oinfo = loi;
564 again:
565         clo = lov_sub_find(env, mdcdev, fid, sconf);
566         if (IS_ERR(clo))
567                 GOTO(out, rc = PTR_ERR(clo));
568
569         rc = lov_init_sub(env, lov, clo, loi, lov_comp_index(index, 0));
570         if (rc == -EAGAIN) /* try again */
571                 goto again;
572         else if (rc != 0)
573                 GOTO(out, rc);
574
575         lle->lle_dom.lo_dom = cl2lovsub(clo);
576         spin_lock_init(&lle->lle_dom.lo_dom_r0.lo_sub_lock);
577         lle->lle_dom.lo_dom_r0.lo_nr = 1;
578         lle->lle_dom.lo_dom_r0.lo_sub = &lle->lle_dom.lo_dom;
579         lle->lle_dom.lo_loi = loi;
580
581         rc = lov_page_slice_fixup(lov, clo);
582         RETURN(rc);
583
584 out:
585         if (loi != NULL)
586                 OBD_SLAB_FREE_PTR(loi, lov_oinfo_slab);
587         return rc;
588 }
589
590 /**
591  * Implementation of lov_layout_operations::llo_fini for DOM object.
592  *
593  * Finish the DOM object and free related memory.
594  *
595  * \param[in] env       execution environment
596  * \param[in] lov       LOV object
597  * \param[in] state     LOV layout state
598  */
599 static void lov_fini_dom(const struct lu_env *env,
600                          struct lov_layout_entry *lle)
601 {
602         if (lle->lle_dom.lo_dom != NULL)
603                 lle->lle_dom.lo_dom = NULL;
604         if (lle->lle_dom.lo_loi != NULL)
605                 OBD_SLAB_FREE_PTR(lle->lle_dom.lo_loi, lov_oinfo_slab);
606 }
607
608 static struct lov_comp_layout_entry_ops dom_ops = {
609         .lco_init = lov_init_dom,
610         .lco_fini = lov_fini_dom,
611         .lco_getattr = lov_attr_get_dom,
612 };
613
614 static int lov_init_composite(const struct lu_env *env, struct lov_device *dev,
615                               struct lov_object *lov, struct lov_stripe_md *lsm,
616                               const struct cl_object_conf *conf,
617                               union lov_layout_state *state)
618 {
619         struct lov_layout_composite *comp = &state->composite;
620         struct lov_layout_entry *lle;
621         struct lov_mirror_entry *lre;
622         unsigned int entry_count;
623         unsigned int psz = 0;
624         unsigned int mirror_count;
625         int flr_state = lsm->lsm_flags & LCM_FL_FLR_MASK;
626         int result = 0;
627         unsigned int seq;
628         int i, j;
629         bool dom_size = 0;
630
631         ENTRY;
632
633         LASSERT(lsm->lsm_entry_count > 0);
634         LASSERT(lov->lo_lsm == NULL);
635         lov->lo_lsm = lsm_addref(lsm);
636         set_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
637
638         dump_lsm(D_INODE, lsm);
639
640         entry_count = lsm->lsm_entry_count;
641
642         spin_lock_init(&comp->lo_write_lock);
643         comp->lo_flags = lsm->lsm_flags;
644         comp->lo_mirror_count = lsm->lsm_mirror_count + 1;
645         comp->lo_entry_count = lsm->lsm_entry_count;
646         comp->lo_preferred_mirror = -1;
647
648         if (equi(flr_state == LCM_FL_NONE, comp->lo_mirror_count > 1))
649                 RETURN(-EINVAL);
650
651         OBD_ALLOC_PTR_ARRAY(comp->lo_mirrors, comp->lo_mirror_count);
652         if (comp->lo_mirrors == NULL)
653                 RETURN(-ENOMEM);
654
655         OBD_ALLOC_PTR_ARRAY(comp->lo_entries, entry_count);
656         if (comp->lo_entries == NULL)
657                 RETURN(-ENOMEM);
658
659         /* Initiate all entry types and extents data at first */
660         for (i = 0, j = 0, mirror_count = 1; i < entry_count; i++) {
661                 int mirror_id = 0;
662
663                 lle = &comp->lo_entries[i];
664
665                 lle->lle_lsme = lsm->lsm_entries[i];
666                 lle->lle_type = lov_entry_type(lle->lle_lsme);
667                 switch (lle->lle_type) {
668                 case LOV_PATTERN_RAID0:
669                         lle->lle_comp_ops = &raid0_ops;
670                         break;
671                 case LOV_PATTERN_MDT:
672                         /* Allowed to have several DOM stripes in different
673                          * mirrors with the same DoM size.
674                          */
675                         if (!dom_size) {
676                                 dom_size = lle->lle_lsme->lsme_extent.e_end;
677                         } else if (dom_size !=
678                                    lle->lle_lsme->lsme_extent.e_end) {
679                                 CERROR("%s: DOM entries with different sizes\n",
680                                        lov2obd(dev->ld_lov)->obd_name);
681                                 dump_lsm(D_ERROR, lsm);
682                                 RETURN(-EINVAL);
683                         }
684                         lle->lle_comp_ops = &dom_ops;
685                         break;
686                 case LOV_PATTERN_FOREIGN:
687                         lle->lle_comp_ops = NULL;
688                         break;
689                 default:
690                         CERROR("%s: unknown composite layout entry type %i\n",
691                                lov2obd(dev->ld_lov)->obd_name,
692                                lsm->lsm_entries[i]->lsme_pattern);
693                         dump_lsm(D_ERROR, lsm);
694                         RETURN(-EIO);
695                 }
696
697                 lle->lle_extent = &lle->lle_lsme->lsme_extent;
698                 lle->lle_valid = !(lle->lle_lsme->lsme_flags & LCME_FL_STALE);
699
700                 if (flr_state != LCM_FL_NONE)
701                         mirror_id = mirror_id_of(lle->lle_lsme->lsme_id);
702
703                 lre = &comp->lo_mirrors[j];
704                 if (i > 0) {
705                         if (mirror_id == lre->lre_mirror_id) {
706                                 lre->lre_valid |= lle->lle_valid;
707                                 lre->lre_stale |= !lle->lle_valid;
708                                 lre->lre_foreign |=
709                                         lsme_is_foreign(lle->lle_lsme);
710                                 lre->lre_end = i;
711                                 continue;
712                         }
713
714                         /* new mirror detected, assume that the mirrors
715                          * are shorted in layout */
716                         ++mirror_count;
717                         ++j;
718                         if (j >= comp->lo_mirror_count)
719                                 break;
720
721                         lre = &comp->lo_mirrors[j];
722                 }
723
724                 /* entries must be sorted by mirrors */
725                 lre->lre_mirror_id = mirror_id;
726                 lre->lre_start = lre->lre_end = i;
727                 lre->lre_preferred = !!(lle->lle_lsme->lsme_flags &
728                                         LCME_FL_PREF_RD);
729                 lre->lre_valid = lle->lle_valid;
730                 lre->lre_stale = !lle->lle_valid;
731                 lre->lre_foreign = lsme_is_foreign(lle->lle_lsme);
732         }
733
734         /* sanity check for FLR */
735         if (mirror_count != comp->lo_mirror_count) {
736                 CDEBUG(D_INODE, DFID
737                        " doesn't have the # of mirrors it claims, %u/%u\n",
738                        PFID(lu_object_fid(lov2lu(lov))), mirror_count,
739                        comp->lo_mirror_count + 1);
740
741                 GOTO(out, result = -EINVAL);
742         }
743
744         lov_foreach_layout_entry(lov, lle) {
745                 int index = lov_layout_entry_index(lov, lle);
746
747                 /**
748                  * If the component has not been init-ed on MDS side, for
749                  * PFL layout, we'd know that the components beyond this one
750                  * will be dynamically init-ed later on file write/trunc ops.
751                  */
752                 if (!lsme_inited(lle->lle_lsme))
753                         continue;
754
755                 if (lsme_is_foreign(lle->lle_lsme))
756                         continue;
757
758                 result = lle->lle_comp_ops->lco_init(env, dev, lov, index,
759                                                      conf, lle);
760                 if (result < 0)
761                         break;
762
763                 LASSERT(ergo(psz > 0, psz == result));
764                 psz = result;
765         }
766
767         if (psz > 0)
768                 cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
769
770         /* decide the preferred mirror. It uses the hash value of lov_object
771          * so that different clients would use different mirrors for read. */
772         mirror_count = 0;
773         seq = hash_long((unsigned long)lov, 8);
774         for (i = 0; i < comp->lo_mirror_count; i++) {
775                 unsigned int idx = (i + seq) % comp->lo_mirror_count;
776
777                 lre = lov_mirror_entry(lov, idx);
778                 if (lre->lre_stale)
779                         continue;
780
781                 if (lre->lre_foreign)
782                         continue;
783
784                 mirror_count++; /* valid mirror */
785
786                 if (lre->lre_preferred || comp->lo_preferred_mirror < 0)
787                         comp->lo_preferred_mirror = idx;
788         }
789         if (!mirror_count) {
790                 CDEBUG(D_INODE, DFID
791                        " doesn't have any valid mirrors\n",
792                        PFID(lu_object_fid(lov2lu(lov))));
793
794                 comp->lo_preferred_mirror = 0;
795         }
796
797         LASSERT(comp->lo_preferred_mirror >= 0);
798
799         EXIT;
800 out:
801         return result > 0 ? 0 : result;
802 }
803
804 static int lov_init_empty(const struct lu_env *env, struct lov_device *dev,
805                           struct lov_object *lov, struct lov_stripe_md *lsm,
806                           const struct cl_object_conf *conf,
807                           union lov_layout_state *state)
808 {
809         return 0;
810 }
811
812 static int lov_init_released(const struct lu_env *env,
813                              struct lov_device *dev, struct lov_object *lov,
814                              struct lov_stripe_md *lsm,
815                              const struct cl_object_conf *conf,
816                              union lov_layout_state *state)
817 {
818         LASSERT(lsm != NULL);
819         LASSERT(lsm->lsm_is_released);
820         LASSERT(lov->lo_lsm == NULL);
821
822         lov->lo_lsm = lsm_addref(lsm);
823         return 0;
824 }
825
826 static int lov_init_foreign(const struct lu_env *env,
827                             struct lov_device *dev, struct lov_object *lov,
828                             struct lov_stripe_md *lsm,
829                             const struct cl_object_conf *conf,
830                             union lov_layout_state *state)
831 {
832         LASSERT(lsm != NULL);
833         LASSERT(lov->lo_type == LLT_FOREIGN);
834         LASSERT(lov->lo_lsm == NULL);
835
836         lov->lo_lsm = lsm_addref(lsm);
837         return 0;
838 }
839
840 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
841                             union lov_layout_state *state)
842 {
843         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED ||
844                 lov->lo_type == LLT_FOREIGN);
845
846         lov_layout_wait(env, lov);
847         return 0;
848 }
849
850 static int lov_delete_composite(const struct lu_env *env,
851                                 struct lov_object *lov,
852                                 union lov_layout_state *state)
853 {
854         struct lov_layout_entry *entry;
855         struct lov_layout_composite *comp = &state->composite;
856
857         ENTRY;
858
859         dump_lsm(D_INODE, lov->lo_lsm);
860
861         lov_layout_wait(env, lov);
862         if (comp->lo_entries)
863                 lov_foreach_layout_entry(lov, entry) {
864                         if (entry->lle_lsme && lsme_is_foreign(entry->lle_lsme))
865                                 continue;
866
867                         lov_delete_raid0(env, lov, entry);
868         }
869
870         RETURN(0);
871 }
872
873 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
874                            union lov_layout_state *state)
875 {
876         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
877 }
878
879 static void lov_fini_composite(const struct lu_env *env,
880                                struct lov_object *lov,
881                                union lov_layout_state *state)
882 {
883         struct lov_layout_composite *comp = &state->composite;
884         ENTRY;
885
886         if (comp->lo_entries != NULL) {
887                 struct lov_layout_entry *entry;
888
889                 lov_foreach_layout_entry(lov, entry)
890                         if (entry->lle_comp_ops)
891                                 entry->lle_comp_ops->lco_fini(env, entry);
892
893                 OBD_FREE_PTR_ARRAY(comp->lo_entries, comp->lo_entry_count);
894                 comp->lo_entries = NULL;
895         }
896
897         if (comp->lo_mirrors != NULL) {
898                 OBD_FREE_PTR_ARRAY(comp->lo_mirrors, comp->lo_mirror_count);
899                 comp->lo_mirrors = NULL;
900         }
901
902         memset(comp, 0, sizeof(*comp));
903
904         dump_lsm(D_INODE, lov->lo_lsm);
905         lov_free_memmd(&lov->lo_lsm);
906
907         EXIT;
908 }
909
910 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
911                                 union lov_layout_state *state)
912 {
913         ENTRY;
914         dump_lsm(D_INODE, lov->lo_lsm);
915         lov_free_memmd(&lov->lo_lsm);
916         EXIT;
917 }
918
919 static int lov_print_empty(const struct lu_env *env, void *cookie,
920                            lu_printer_t p, const struct lu_object *o)
921 {
922         (*p)(env, cookie, "empty %d\n",
923              test_bit(LO_LAYOUT_INVALID, &lu2lov(o)->lo_obj_flags));
924         return 0;
925 }
926
927 static int lov_print_composite(const struct lu_env *env, void *cookie,
928                                lu_printer_t p, const struct lu_object *o)
929 {
930         struct lov_object *lov = lu2lov(o);
931         struct lov_stripe_md *lsm = lov->lo_lsm;
932         int i;
933
934         (*p)(env, cookie, "entries: %d, %s, lsm{%p 0x%08X %d %u}:\n",
935              lsm->lsm_entry_count,
936              test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags) ? "invalid" :
937              "valid", lsm, lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
938              lsm->lsm_layout_gen);
939
940         for (i = 0; i < lsm->lsm_entry_count; i++) {
941                 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
942                 struct lov_layout_entry *lle = lov_entry(lov, i);
943
944                 (*p)(env, cookie,
945                      DEXT ": { 0x%08X, %u, %#x, %u, %#x, %u, %u }\n",
946                      PEXT(&lse->lsme_extent), lse->lsme_magic,
947                      lse->lsme_id, lse->lsme_pattern, lse->lsme_layout_gen,
948                      lse->lsme_flags, lse->lsme_stripe_count,
949                      lse->lsme_stripe_size);
950
951                 if (!lsme_is_foreign(lse))
952                         lov_print_raid0(env, cookie, p, lle);
953         }
954
955         return 0;
956 }
957
958 static int lov_print_released(const struct lu_env *env, void *cookie,
959                                 lu_printer_t p, const struct lu_object *o)
960 {
961         struct lov_object       *lov = lu2lov(o);
962         struct lov_stripe_md    *lsm = lov->lo_lsm;
963
964         (*p)(env, cookie,
965                 "released: %s, lsm{%p 0x%08X %d %u}:\n",
966                 test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags) ? "invalid" :
967                 "valid", lsm, lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
968                 lsm->lsm_layout_gen);
969         return 0;
970 }
971
972 static int lov_print_foreign(const struct lu_env *env, void *cookie,
973                                 lu_printer_t p, const struct lu_object *o)
974 {
975         struct lov_object       *lov = lu2lov(o);
976         struct lov_stripe_md    *lsm = lov->lo_lsm;
977
978         (*p)(env, cookie,
979                 "foreign: %s, lsm{%p 0x%08X %d %u}:\n",
980                 test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags) ?
981                 "invalid" : "valid", lsm,
982                 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
983                 lsm->lsm_layout_gen);
984         (*p)(env, cookie,
985                 "raw_ea_content '%.*s'\n",
986                 (int)lsm->lsm_foreign_size, (char *)lsm_foreign(lsm));
987         return 0;
988 }
989
990 /**
991  * Implements cl_object_operations::coo_attr_get() method for an object
992  * without stripes (LLT_EMPTY layout type).
993  *
994  * The only attributes this layer is authoritative in this case is
995  * cl_attr::cat_blocks---it's 0.
996  */
997 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
998                               struct cl_attr *attr)
999 {
1000         attr->cat_blocks = 0;
1001         return 0;
1002 }
1003
1004 static int lov_attr_get_composite(const struct lu_env *env,
1005                                   struct cl_object *obj,
1006                                   struct cl_attr *attr)
1007 {
1008         struct lov_object       *lov = cl2lov(obj);
1009         struct lov_layout_entry *entry;
1010         int                      result = 0;
1011
1012         ENTRY;
1013
1014         attr->cat_size = 0;
1015         attr->cat_blocks = 0;
1016         lov_foreach_layout_entry(lov, entry) {
1017                 struct cl_attr *lov_attr = NULL;
1018                 int index = lov_layout_entry_index(lov, entry);
1019
1020                 if (!entry->lle_valid)
1021                         continue;
1022
1023                 /* PFL: This component has not been init-ed. */
1024                 if (!lsm_entry_inited(lov->lo_lsm, index))
1025                         continue;
1026
1027                 result = entry->lle_comp_ops->lco_getattr(env, lov, index,
1028                                                           entry, &lov_attr);
1029                 if (result < 0)
1030                         RETURN(result);
1031
1032                 if (lov_attr == NULL)
1033                         continue;
1034
1035                 CDEBUG(D_INODE, "COMP ID #%i: s=%llu m=%llu a=%llu c=%llu "
1036                        "b=%llu\n", index - 1, lov_attr->cat_size,
1037                        lov_attr->cat_mtime, lov_attr->cat_atime,
1038                        lov_attr->cat_ctime, lov_attr->cat_blocks);
1039
1040                 /* merge results */
1041                 attr->cat_blocks += lov_attr->cat_blocks;
1042                 if (attr->cat_size < lov_attr->cat_size)
1043                         attr->cat_size = lov_attr->cat_size;
1044                 if (attr->cat_kms < lov_attr->cat_kms)
1045                         attr->cat_kms = lov_attr->cat_kms;
1046                 if (attr->cat_atime < lov_attr->cat_atime)
1047                         attr->cat_atime = lov_attr->cat_atime;
1048                 if (attr->cat_ctime < lov_attr->cat_ctime)
1049                         attr->cat_ctime = lov_attr->cat_ctime;
1050                 if (attr->cat_mtime < lov_attr->cat_mtime)
1051                         attr->cat_mtime = lov_attr->cat_mtime;
1052         }
1053
1054         RETURN(0);
1055 }
1056
1057 static int lov_flush_composite(const struct lu_env *env,
1058                                struct cl_object *obj,
1059                                struct ldlm_lock *lock)
1060 {
1061         struct lov_object *lov = cl2lov(obj);
1062         struct lov_layout_entry *lle;
1063         int rc = -ENODATA;
1064
1065         ENTRY;
1066
1067         lov_foreach_layout_entry(lov, lle) {
1068                 if (!lsme_is_dom(lle->lle_lsme))
1069                         continue;
1070                 rc = cl_object_flush(env, lovsub2cl(lle->lle_dom.lo_dom), lock);
1071                 break;
1072         }
1073
1074         RETURN(rc);
1075 }
1076
1077 static int lov_flush_empty(const struct lu_env *env, struct cl_object *obj,
1078                            struct ldlm_lock *lock)
1079 {
1080         return 0;
1081 }
1082
1083 const static struct lov_layout_operations lov_dispatch[] = {
1084         [LLT_EMPTY] = {
1085                 .llo_init      = lov_init_empty,
1086                 .llo_delete    = lov_delete_empty,
1087                 .llo_fini      = lov_fini_empty,
1088                 .llo_print     = lov_print_empty,
1089                 .llo_page_init = lov_page_init_empty,
1090                 .llo_lock_init = lov_lock_init_empty,
1091                 .llo_io_init   = lov_io_init_empty,
1092                 .llo_getattr   = lov_attr_get_empty,
1093                 .llo_flush     = lov_flush_empty,
1094         },
1095         [LLT_RELEASED] = {
1096                 .llo_init      = lov_init_released,
1097                 .llo_delete    = lov_delete_empty,
1098                 .llo_fini      = lov_fini_released,
1099                 .llo_print     = lov_print_released,
1100                 .llo_page_init = lov_page_init_empty,
1101                 .llo_lock_init = lov_lock_init_empty,
1102                 .llo_io_init   = lov_io_init_released,
1103                 .llo_getattr   = lov_attr_get_empty,
1104                 .llo_flush     = lov_flush_empty,
1105         },
1106         [LLT_COMP] = {
1107                 .llo_init      = lov_init_composite,
1108                 .llo_delete    = lov_delete_composite,
1109                 .llo_fini      = lov_fini_composite,
1110                 .llo_print     = lov_print_composite,
1111                 .llo_page_init = lov_page_init_composite,
1112                 .llo_lock_init = lov_lock_init_composite,
1113                 .llo_io_init   = lov_io_init_composite,
1114                 .llo_getattr   = lov_attr_get_composite,
1115                 .llo_flush     = lov_flush_composite,
1116         },
1117         [LLT_FOREIGN] = {
1118                 .llo_init      = lov_init_foreign,
1119                 .llo_delete    = lov_delete_empty,
1120                 .llo_fini      = lov_fini_released,
1121                 .llo_print     = lov_print_foreign,
1122                 .llo_page_init = lov_page_init_foreign,
1123                 .llo_lock_init = lov_lock_init_empty,
1124                 .llo_io_init   = lov_io_init_empty,
1125                 .llo_getattr   = lov_attr_get_empty,
1126                 .llo_flush     = lov_flush_empty,
1127         },
1128 };
1129
1130 /**
1131  * Performs a double-dispatch based on the layout type of an object.
1132  */
1133 #define LOV_2DISPATCH_NOLOCK(obj, op, ...)              \
1134 ({                                                      \
1135         struct lov_object *__obj = (obj);               \
1136         enum lov_layout_type __llt;                     \
1137                                                         \
1138         __llt = __obj->lo_type;                         \
1139         LASSERT(__llt < ARRAY_SIZE(lov_dispatch));      \
1140         lov_dispatch[__llt].op(__VA_ARGS__);            \
1141 })
1142
1143 /**
1144  * Return lov_layout_type associated with a given lsm
1145  */
1146 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
1147 {
1148         if (lsm == NULL)
1149                 return LLT_EMPTY;
1150
1151         if (lsm->lsm_is_released)
1152                 return LLT_RELEASED;
1153
1154         if (lsm->lsm_magic == LOV_MAGIC_V1 ||
1155             lsm->lsm_magic == LOV_MAGIC_V3 ||
1156             lsm->lsm_magic == LOV_MAGIC_COMP_V1)
1157                 return LLT_COMP;
1158
1159         if (lsm->lsm_magic == LOV_MAGIC_FOREIGN)
1160                 return LLT_FOREIGN;
1161
1162         return LLT_EMPTY;
1163 }
1164
1165 static inline void lov_conf_freeze(struct lov_object *lov)
1166 {
1167         CDEBUG(D_INODE, "To take share lov(%p) owner %p/%p\n",
1168                 lov, lov->lo_owner, current);
1169         if (lov->lo_owner != current)
1170                 down_read(&lov->lo_type_guard);
1171 }
1172
1173 static inline void lov_conf_thaw(struct lov_object *lov)
1174 {
1175         CDEBUG(D_INODE, "To release share lov(%p) owner %p/%p\n",
1176                 lov, lov->lo_owner, current);
1177         if (lov->lo_owner != current)
1178                 up_read(&lov->lo_type_guard);
1179 }
1180
1181 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...)                       \
1182 ({                                                                      \
1183         struct lov_object                      *__obj = (obj);          \
1184         int                                     __lock = !!(lock);      \
1185         typeof(lov_dispatch[0].op(__VA_ARGS__)) __result;               \
1186                                                                         \
1187         if (__lock)                                                     \
1188                 lov_conf_freeze(__obj);                                 \
1189         __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__);          \
1190         if (__lock)                                                     \
1191                 lov_conf_thaw(__obj);                                   \
1192         __result;                                                       \
1193 })
1194
1195 /**
1196  * Performs a locked double-dispatch based on the layout type of an object.
1197  */
1198 #define LOV_2DISPATCH(obj, op, ...)                     \
1199         LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
1200
1201 #define LOV_2DISPATCH_VOID(obj, op, ...)                                \
1202 do {                                                                    \
1203         struct lov_object                      *__obj = (obj);          \
1204         enum lov_layout_type                    __llt;                  \
1205                                                                         \
1206         lov_conf_freeze(__obj);                                         \
1207         __llt = __obj->lo_type;                                         \
1208         LASSERT(__llt < ARRAY_SIZE(lov_dispatch));                      \
1209         lov_dispatch[__llt].op(__VA_ARGS__);                            \
1210         lov_conf_thaw(__obj);                                           \
1211 } while (0)
1212
1213 static void lov_conf_lock(struct lov_object *lov)
1214 {
1215         LASSERT(lov->lo_owner != current);
1216         down_write(&lov->lo_type_guard);
1217         LASSERT(lov->lo_owner == NULL);
1218         lov->lo_owner = current;
1219         CDEBUG(D_INODE, "Took exclusive lov(%p) owner %p\n",
1220                 lov, lov->lo_owner);
1221 }
1222
1223 static void lov_conf_unlock(struct lov_object *lov)
1224 {
1225         CDEBUG(D_INODE, "To release exclusive lov(%p) owner %p\n",
1226                 lov, lov->lo_owner);
1227         lov->lo_owner = NULL;
1228         up_write(&lov->lo_type_guard);
1229 }
1230
1231 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
1232 {
1233         ENTRY;
1234
1235         while (atomic_read(&lov->lo_active_ios) > 0) {
1236                 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
1237                         PFID(lu_object_fid(lov2lu(lov))),
1238                         atomic_read(&lov->lo_active_ios));
1239
1240                 wait_event_idle(lov->lo_waitq,
1241                                 atomic_read(&lov->lo_active_ios) == 0);
1242         }
1243         RETURN(0);
1244 }
1245
1246 static int lov_layout_change(const struct lu_env *unused,
1247                              struct lov_object *lov, struct lov_stripe_md *lsm,
1248                              const struct cl_object_conf *conf)
1249 {
1250         enum lov_layout_type llt = lov_type(lsm);
1251         union lov_layout_state *state = &lov->u;
1252         const struct lov_layout_operations *old_ops;
1253         const struct lov_layout_operations *new_ops;
1254         struct lov_device *lov_dev = lov_object_dev(lov);
1255         struct lu_env *env;
1256         __u16 refcheck;
1257         int rc;
1258         ENTRY;
1259
1260         LASSERT(lov->lo_type < ARRAY_SIZE(lov_dispatch));
1261
1262         env = cl_env_get(&refcheck);
1263         if (IS_ERR(env))
1264                 RETURN(PTR_ERR(env));
1265
1266         LASSERT(llt < ARRAY_SIZE(lov_dispatch));
1267
1268         CDEBUG(D_INODE, DFID" from %s to %s\n",
1269                PFID(lu_object_fid(lov2lu(lov))),
1270                llt2str(lov->lo_type), llt2str(llt));
1271
1272         old_ops = &lov_dispatch[lov->lo_type];
1273         new_ops = &lov_dispatch[llt];
1274
1275         rc = cl_object_prune(env, &lov->lo_cl);
1276         if (rc != 0)
1277                 GOTO(out, rc);
1278
1279         rc = old_ops->llo_delete(env, lov, &lov->u);
1280         if (rc != 0)
1281                 GOTO(out, rc);
1282
1283         old_ops->llo_fini(env, lov, &lov->u);
1284
1285         LASSERT(atomic_read(&lov->lo_active_ios) == 0);
1286
1287         CDEBUG(D_INODE, DFID "Apply new layout lov %p, type %d\n",
1288                PFID(lu_object_fid(lov2lu(lov))), lov, llt);
1289
1290         /* page bufsize fixup */
1291         cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
1292                 lov_page_slice_fixup(lov, NULL);
1293
1294         lov->lo_type = llt;
1295         rc = new_ops->llo_init(env, lov_dev, lov, lsm, conf, state);
1296         if (rc != 0) {
1297                 struct obd_device *obd = lov2obd(lov_dev->ld_lov);
1298
1299                 CERROR("%s: cannot apply new layout on "DFID" : rc = %d\n",
1300                        obd->obd_name, PFID(lu_object_fid(lov2lu(lov))), rc);
1301                 new_ops->llo_delete(env, lov, state);
1302                 new_ops->llo_fini(env, lov, state);
1303                 /* this file becomes an EMPTY file. */
1304                 lov->lo_type = LLT_EMPTY;
1305                 GOTO(out, rc);
1306         }
1307
1308 out:
1309         cl_env_put(env, &refcheck);
1310         RETURN(rc);
1311 }
1312
1313 /*****************************************************************************
1314  *
1315  * Lov object operations.
1316  *
1317  */
1318 static int lov_object_init(const struct lu_env *env, struct lu_object *obj,
1319                            const struct lu_object_conf *conf)
1320 {
1321         struct lov_object            *lov   = lu2lov(obj);
1322         struct lov_device            *dev   = lov_object_dev(lov);
1323         const struct cl_object_conf  *cconf = lu2cl_conf(conf);
1324         union lov_layout_state       *set   = &lov->u;
1325         const struct lov_layout_operations *ops;
1326         struct lov_stripe_md *lsm = NULL;
1327         int rc;
1328         ENTRY;
1329
1330         init_rwsem(&lov->lo_type_guard);
1331         atomic_set(&lov->lo_active_ios, 0);
1332         init_waitqueue_head(&lov->lo_waitq);
1333         cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
1334
1335         lov->lo_type = LLT_EMPTY;
1336         if (cconf->u.coc_layout.lb_buf != NULL) {
1337                 lsm = lov_unpackmd(dev->ld_lov,
1338                                    cconf->u.coc_layout.lb_buf,
1339                                    cconf->u.coc_layout.lb_len);
1340                 if (IS_ERR(lsm))
1341                         RETURN(PTR_ERR(lsm));
1342
1343                 dump_lsm(D_INODE, lsm);
1344         }
1345
1346         /* no locking is necessary, as object is being created */
1347         lov->lo_type = lov_type(lsm);
1348         ops = &lov_dispatch[lov->lo_type];
1349         rc = ops->llo_init(env, dev, lov, lsm, cconf, set);
1350         if (rc != 0)
1351                 GOTO(out_lsm, rc);
1352
1353 out_lsm:
1354         lov_lsm_put(lsm);
1355
1356         RETURN(rc);
1357 }
1358
1359 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
1360                         const struct cl_object_conf *conf)
1361 {
1362         struct lov_stripe_md    *lsm = NULL;
1363         struct lov_object       *lov = cl2lov(obj);
1364         int                      result = 0;
1365         ENTRY;
1366
1367         if (conf->coc_opc == OBJECT_CONF_SET &&
1368             conf->u.coc_layout.lb_buf != NULL) {
1369                 lsm = lov_unpackmd(lov_object_dev(lov)->ld_lov,
1370                                    conf->u.coc_layout.lb_buf,
1371                                    conf->u.coc_layout.lb_len);
1372                 if (IS_ERR(lsm))
1373                         RETURN(PTR_ERR(lsm));
1374                 dump_lsm(D_INODE, lsm);
1375         }
1376
1377         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
1378                 set_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
1379                 GOTO(out_lsm, result = 0);
1380         }
1381
1382         lov_conf_lock(lov);
1383         if (conf->coc_opc == OBJECT_CONF_WAIT) {
1384                 if (test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags) &&
1385                     atomic_read(&lov->lo_active_ios) > 0) {
1386                         lov_conf_unlock(lov);
1387                         result = lov_layout_wait(env, lov);
1388                         lov_conf_lock(lov);
1389                 }
1390                 GOTO(out, result);
1391         }
1392
1393         LASSERT(conf->coc_opc == OBJECT_CONF_SET);
1394
1395         if ((lsm == NULL && lov->lo_lsm == NULL) ||
1396             ((lsm != NULL && lov->lo_lsm != NULL) &&
1397              (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
1398              (lov->lo_lsm->lsm_flags == lsm->lsm_flags) &&
1399              (lov->lo_lsm->lsm_entries[0]->lsme_pattern ==
1400               lsm->lsm_entries[0]->lsme_pattern))) {
1401                 /* same version of layout */
1402                 clear_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
1403                 GOTO(out, result = 0);
1404         }
1405
1406         /* will change layout - check if there still exists active IO. */
1407         if (atomic_read(&lov->lo_active_ios) > 0) {
1408                 set_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
1409                 GOTO(out, result = -EBUSY);
1410         }
1411
1412         result = lov_layout_change(env, lov, lsm, conf);
1413         if (result)
1414                 set_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
1415         else
1416                 clear_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags);
1417         EXIT;
1418
1419 out:
1420         lov_conf_unlock(lov);
1421 out_lsm:
1422         lov_lsm_put(lsm);
1423         CDEBUG(D_INODE, DFID" lo_layout_invalid=%u\n",
1424                PFID(lu_object_fid(lov2lu(lov))),
1425                test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags));
1426         RETURN(result);
1427 }
1428
1429 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
1430 {
1431         struct lov_object *lov = lu2lov(obj);
1432
1433         ENTRY;
1434         LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
1435         EXIT;
1436 }
1437
1438 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
1439 {
1440         struct lov_object *lov = lu2lov(obj);
1441
1442         ENTRY;
1443         LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
1444         lu_object_fini(obj);
1445         OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
1446         EXIT;
1447 }
1448
1449 static int lov_object_print(const struct lu_env *env, void *cookie,
1450                             lu_printer_t p, const struct lu_object *o)
1451 {
1452         return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
1453 }
1454
1455 static int lov_page_init(const struct lu_env *env, struct cl_object *obj,
1456                          struct cl_page *page, pgoff_t index)
1457 {
1458         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
1459                                     index);
1460 }
1461
1462 /**
1463  * Implements cl_object_operations::clo_io_init() method for lov
1464  * layer. Dispatches to the appropriate layout io initialization method.
1465  */
1466 static int lov_io_init(const struct lu_env *env, struct cl_object *obj,
1467                        struct cl_io *io)
1468 {
1469         CL_IO_SLICE_CLEAN(lov_env_io(env), lis_preserved);
1470
1471         CDEBUG(D_INODE, DFID "io %p type %d ignore/verify layout %d/%d\n",
1472                PFID(lu_object_fid(&obj->co_lu)), io, io->ci_type,
1473                io->ci_ignore_layout, io->ci_verify_layout);
1474
1475         /* IO type CIT_MISC with ci_ignore_layout set are usually invoked from
1476          * the OSC layer. It shouldn't take lov layout conf lock in that case,
1477          * because as long as the OSC object exists, the layout can't be
1478          * reconfigured. */
1479         return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
1480                         !(io->ci_ignore_layout && io->ci_type == CIT_MISC),
1481                         env, obj, io);
1482 }
1483
1484 /**
1485  * An implementation of cl_object_operations::clo_attr_get() method for lov
1486  * layer. For raid0 layout this collects and merges attributes of all
1487  * sub-objects.
1488  */
1489 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
1490                         struct cl_attr *attr)
1491 {
1492         /* do not take lock, as this function is called under a
1493          * spin-lock. Layout is protected from changing by ongoing IO. */
1494         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
1495 }
1496
1497 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
1498                            const struct cl_attr *attr, unsigned valid)
1499 {
1500         /*
1501          * No dispatch is required here, as no layout implements this.
1502          */
1503         return 0;
1504 }
1505
1506 static int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1507                   struct cl_lock *lock, const struct cl_io *io)
1508 {
1509         /* No need to lock because we've taken one refcount of layout.  */
1510         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1511                                     io);
1512 }
1513
1514 /**
1515  * We calculate on which OST the mapping will end. If the length of mapping
1516  * is greater than (stripe_size * stripe_count) then the last_stripe will
1517  * will be one just before start_stripe. Else we check if the mapping
1518  * intersects each OST and find last_stripe.
1519  * This function returns the last_stripe and also sets the stripe_count
1520  * over which the mapping is spread
1521  *
1522  * \param lsm [in]              striping information for the file
1523  * \param index [in]            stripe component index
1524  * \param ext [in]              logical extent of mapping
1525  * \param start_stripe [in]     starting stripe of the mapping
1526  * \param stripe_count [out]    the number of stripes across which to map is
1527  *                              returned
1528  *
1529  * \retval last_stripe          return the last stripe of the mapping
1530  */
1531 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm, int index,
1532                                    struct lu_extent *ext,
1533                                    int start_stripe, int *stripe_count)
1534 {
1535         struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1536         int init_stripe;
1537         int last_stripe;
1538         int i, j;
1539
1540         init_stripe = lov_stripe_number(lsm, index, ext->e_start);
1541
1542         if (ext->e_end - ext->e_start >
1543             lsme->lsme_stripe_size * lsme->lsme_stripe_count) {
1544                 if (init_stripe == start_stripe) {
1545                         last_stripe = (start_stripe < 1) ?
1546                                 lsme->lsme_stripe_count - 1 : start_stripe - 1;
1547                         *stripe_count = lsme->lsme_stripe_count;
1548                 } else if (init_stripe < start_stripe) {
1549                         last_stripe = (init_stripe < 1) ?
1550                                 lsme->lsme_stripe_count - 1 : init_stripe - 1;
1551                         *stripe_count = lsme->lsme_stripe_count -
1552                                         (start_stripe - init_stripe);
1553                 } else {
1554                         last_stripe = init_stripe - 1;
1555                         *stripe_count = init_stripe - start_stripe;
1556                 }
1557         } else {
1558                 for (j = 0, i = start_stripe; j < lsme->lsme_stripe_count;
1559                      i = (i + 1) % lsme->lsme_stripe_count, j++) {
1560                         if (!lov_stripe_intersects(lsm, index,  i, ext, NULL,
1561                                                    NULL))
1562                                 break;
1563                         if ((start_stripe != init_stripe) && (i == init_stripe))
1564                                 break;
1565                 }
1566                 *stripe_count = j;
1567                 last_stripe = (start_stripe + j - 1) % lsme->lsme_stripe_count;
1568         }
1569
1570         return last_stripe;
1571 }
1572
1573 /**
1574  * Set fe_device and copy extents from local buffer into main return buffer.
1575  *
1576  * \param fiemap [out]          fiemap to hold all extents
1577  * \param lcl_fm_ext [in]       array of fiemap extents get from OSC layer
1578  * \param ost_index [in]        OST index to be written into the fm_device
1579  *                              field for each extent
1580  * \param ext_count [in]        number of extents to be copied
1581  * \param current_extent [in]   where to start copying in the extent array
1582  */
1583 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1584                                          struct fiemap_extent *lcl_fm_ext,
1585                                          int ost_index, unsigned int ext_count,
1586                                          int current_extent, int abs_stripeno)
1587 {
1588         char            *to;
1589         unsigned int    ext;
1590
1591         for (ext = 0; ext < ext_count; ext++) {
1592                 set_fe_device_stripenr(&lcl_fm_ext[ext], ost_index,
1593                                        abs_stripeno);
1594                 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1595         }
1596
1597         /* Copy fm_extent's from fm_local to return buffer */
1598         to = (char *)fiemap + fiemap_count_to_size(current_extent);
1599         memcpy(to, lcl_fm_ext, ext_count * sizeof(struct fiemap_extent));
1600 }
1601
1602 #define FIEMAP_BUFFER_SIZE 4096
1603
1604 /**
1605  * Non-zero fe_logical indicates that this is a continuation FIEMAP
1606  * call. The local end offset and the device are sent in the first
1607  * fm_extent. This function calculates the stripe number from the index.
1608  * This function returns a stripe_no on which mapping is to be restarted.
1609  *
1610  * This function returns fm_end_offset which is the in-OST offset at which
1611  * mapping should be restarted. If fm_end_offset=0 is returned then caller
1612  * will re-calculate proper offset in next stripe.
1613  * Note that the first extent is passed to lov_get_info via the value field.
1614  *
1615  * \param fiemap [in]           fiemap request header
1616  * \param lsm [in]              striping information for the file
1617  * \param index [in]            stripe component index
1618  * \param ext [in]              logical extent of mapping
1619  * \param start_stripe [out]    starting stripe will be returned in this
1620  */
1621 static u64 fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1622                                      struct lov_stripe_md *lsm,
1623                                      int index, struct lu_extent *ext,
1624                                      int *start_stripe)
1625 {
1626         struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1627         u64 local_end = fiemap->fm_extents[0].fe_logical;
1628         u64 lun_end;
1629         u64 fm_end_offset;
1630         int stripe_no = -1;
1631
1632         if (fiemap->fm_extent_count == 0 ||
1633             fiemap->fm_extents[0].fe_logical == 0)
1634                 return 0;
1635
1636         stripe_no = *start_stripe;
1637
1638         if (stripe_no == -1)
1639                 return -EINVAL;
1640
1641         /* If we have finished mapping on previous device, shift logical
1642          * offset to start of next device */
1643         if (lov_stripe_intersects(lsm, index, stripe_no, ext, NULL, &lun_end) &&
1644             local_end < lun_end) {
1645                 fm_end_offset = local_end;
1646         } else {
1647                 /* This is a special value to indicate that caller should
1648                  * calculate offset in next stripe. */
1649                 fm_end_offset = 0;
1650                 *start_stripe = (stripe_no + 1) % lsme->lsme_stripe_count;
1651         }
1652
1653         return fm_end_offset;
1654 }
1655
1656 struct fiemap_state {
1657         struct fiemap           *fs_fm;
1658         struct lu_extent        fs_ext;         /* current entry extent */
1659         u64                     fs_length;
1660         u64                     fs_end_offset;  /* last iteration offset */
1661         int                     fs_cur_extent;  /* collected exts so far */
1662         int                     fs_cnt_need;    /* # of extents buf can hold */
1663         int                     fs_start_stripe;
1664         int                     fs_last_stripe;
1665         bool                    fs_device_done; /* enough for this OST */
1666         bool                    fs_finish_stripe; /* reached fs_last_stripe */
1667         bool                    fs_enough;      /* enough for this call */
1668 };
1669
1670 static struct cl_object *lov_find_subobj(const struct lu_env *env,
1671                                          struct lov_object *lov,
1672                                          struct lov_stripe_md *lsm,
1673                                          int index)
1674 {
1675         struct lov_device       *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
1676         struct lov_thread_info  *lti = lov_env_info(env);
1677         struct lu_fid           *ofid = &lti->lti_fid;
1678         struct lov_oinfo        *oinfo;
1679         struct cl_device        *subdev;
1680         int                     entry = lov_comp_entry(index);
1681         int                     stripe = lov_comp_stripe(index);
1682         int                     ost_idx;
1683         int                     rc;
1684         struct cl_object        *result;
1685
1686         if (lov->lo_type != LLT_COMP)
1687                 GOTO(out, result = NULL);
1688
1689         if (entry >= lsm->lsm_entry_count ||
1690             stripe >= lsm->lsm_entries[entry]->lsme_stripe_count)
1691                 GOTO(out, result = NULL);
1692
1693         oinfo = lsm->lsm_entries[entry]->lsme_oinfo[stripe];
1694         ost_idx = oinfo->loi_ost_idx;
1695         rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
1696         if (rc != 0)
1697                 GOTO(out, result = NULL);
1698
1699         subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
1700         result = lov_sub_find(env, subdev, ofid, NULL);
1701 out:
1702         if (result == NULL)
1703                 result = ERR_PTR(-EINVAL);
1704         return result;
1705 }
1706
1707 static int fiemap_for_stripe(const struct lu_env *env, struct cl_object *obj,
1708                              struct lov_stripe_md *lsm, struct fiemap *fiemap,
1709                              size_t *buflen, struct ll_fiemap_info_key *fmkey,
1710                              int index, int stripe_last, int stripeno,
1711                              struct fiemap_state *fs)
1712 {
1713         struct lov_stripe_md_entry *lsme = lsm->lsm_entries[index];
1714         struct cl_object *subobj;
1715         struct lov_obd *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1716         struct fiemap_extent *fm_ext = &fs->fs_fm->fm_extents[0];
1717         u64 req_fm_len; /* max requested extent coverage */
1718         u64 len_mapped_single_call;
1719         u64 obd_start;
1720         u64 obd_end;
1721         unsigned int ext_count;
1722         /* EOF for object */
1723         bool ost_eof = false;
1724         /* done with required mapping for this OST? */
1725         bool ost_done = false;
1726         int ost_index;
1727         int rc = 0;
1728
1729         fs->fs_device_done = false;
1730         /* Find out range of mapping on this stripe */
1731         if ((lov_stripe_intersects(lsm, index, stripeno, &fs->fs_ext,
1732                                    &obd_start, &obd_end)) == 0)
1733                 return 0;
1734
1735         if (lov_oinfo_is_dummy(lsme->lsme_oinfo[stripeno]))
1736                 return -EIO;
1737
1738         /* If this is a continuation FIEMAP call and we are on
1739          * starting stripe then obd_start needs to be set to
1740          * end_offset */
1741         if (fs->fs_end_offset != 0 && stripeno == fs->fs_start_stripe)
1742                 obd_start = fs->fs_end_offset;
1743
1744         if (lov_size_to_stripe(lsm, index, fs->fs_ext.e_end, stripeno) ==
1745             obd_start)
1746                 return 0;
1747
1748         req_fm_len = obd_end - obd_start + 1;
1749         fs->fs_fm->fm_length = 0;
1750         len_mapped_single_call = 0;
1751
1752         /* find lobsub object */
1753         subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1754                                  lov_comp_index(index, stripeno));
1755         if (IS_ERR(subobj))
1756                 return PTR_ERR(subobj);
1757         /* If the output buffer is very large and the objects have many
1758          * extents we may need to loop on a single OST repeatedly */
1759         do {
1760                 if (fiemap->fm_extent_count > 0) {
1761                         /* Don't get too many extents. */
1762                         if (fs->fs_cur_extent + fs->fs_cnt_need >
1763                             fiemap->fm_extent_count)
1764                                 fs->fs_cnt_need = fiemap->fm_extent_count -
1765                                                   fs->fs_cur_extent;
1766                 }
1767
1768                 obd_start += len_mapped_single_call;
1769                 fs->fs_fm->fm_length = req_fm_len - len_mapped_single_call;
1770                 req_fm_len = fs->fs_fm->fm_length;
1771                 /**
1772                  * If we've collected enough extent map, we'd request 1 more,
1773                  * to see whether we coincidentally finished all available
1774                  * extent map, so that FIEMAP_EXTENT_LAST would be set.
1775                  */
1776                 fs->fs_fm->fm_extent_count = fs->fs_enough ?
1777                                              1 : fs->fs_cnt_need;
1778                 fs->fs_fm->fm_mapped_extents = 0;
1779                 fs->fs_fm->fm_flags = fiemap->fm_flags;
1780
1781                 ost_index = lsme->lsme_oinfo[stripeno]->loi_ost_idx;
1782
1783                 if (ost_index < 0 || ost_index >= lov->desc.ld_tgt_count)
1784                         GOTO(obj_put, rc = -EINVAL);
1785                 /* If OST is inactive, return extent with UNKNOWN flag. */
1786                 if (!lov->lov_tgts[ost_index]->ltd_active) {
1787                         fs->fs_fm->fm_flags |= FIEMAP_EXTENT_LAST;
1788                         fs->fs_fm->fm_mapped_extents = 1;
1789
1790                         fm_ext[0].fe_logical = obd_start;
1791                         fm_ext[0].fe_length = obd_end - obd_start + 1;
1792                         fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1793
1794                         goto inactive_tgt;
1795                 }
1796
1797                 fs->fs_fm->fm_start = obd_start;
1798                 fs->fs_fm->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1799                 memcpy(&fmkey->lfik_fiemap, fs->fs_fm, sizeof(*fs->fs_fm));
1800                 *buflen = fiemap_count_to_size(fs->fs_fm->fm_extent_count);
1801
1802                 rc = cl_object_fiemap(env, subobj, fmkey, fs->fs_fm, buflen);
1803                 if (rc != 0)
1804                         GOTO(obj_put, rc);
1805 inactive_tgt:
1806                 ext_count = fs->fs_fm->fm_mapped_extents;
1807                 if (ext_count == 0) {
1808                         ost_done = true;
1809                         fs->fs_device_done = true;
1810                         /* If last stripe has hold at the end,
1811                          * we need to return */
1812                         if (stripeno == fs->fs_last_stripe) {
1813                                 fiemap->fm_mapped_extents = 0;
1814                                 fs->fs_finish_stripe = true;
1815                                 GOTO(obj_put, rc);
1816                         }
1817                         break;
1818                 } else if (fs->fs_enough) {
1819                         /*
1820                          * We've collected enough extents and there are
1821                          * more extents after it.
1822                          */
1823                         GOTO(obj_put, rc);
1824                 }
1825
1826                 /* If we just need num of extents, got to next device */
1827                 if (fiemap->fm_extent_count == 0) {
1828                         fs->fs_cur_extent += ext_count;
1829                         break;
1830                 }
1831
1832                 /* prepare to copy retrived map extents */
1833                 len_mapped_single_call = fm_ext[ext_count - 1].fe_logical +
1834                                          fm_ext[ext_count - 1].fe_length -
1835                                          obd_start;
1836
1837                 /* Have we finished mapping on this device? */
1838                 if (req_fm_len <= len_mapped_single_call) {
1839                         ost_done = true;
1840                         fs->fs_device_done = true;
1841                 }
1842
1843                 /* Clear the EXTENT_LAST flag which can be present on
1844                  * the last extent */
1845                 if (fm_ext[ext_count - 1].fe_flags & FIEMAP_EXTENT_LAST)
1846                         fm_ext[ext_count - 1].fe_flags &= ~FIEMAP_EXTENT_LAST;
1847                 if (lov_stripe_size(lsm, index,
1848                                     fm_ext[ext_count - 1].fe_logical +
1849                                     fm_ext[ext_count - 1].fe_length,
1850                                     stripeno) >= fmkey->lfik_oa.o_size) {
1851                         ost_eof = true;
1852                         fs->fs_device_done = true;
1853                 }
1854
1855                 fiemap_prepare_and_copy_exts(fiemap, fm_ext, ost_index,
1856                                              ext_count, fs->fs_cur_extent,
1857                                              stripe_last + stripeno);
1858                 fs->fs_cur_extent += ext_count;
1859
1860                 /* Ran out of available extents? */
1861                 if (fs->fs_cur_extent >= fiemap->fm_extent_count)
1862                         fs->fs_enough = true;
1863         } while (!ost_done && !ost_eof);
1864
1865         if (stripeno == fs->fs_last_stripe)
1866                 fs->fs_finish_stripe = true;
1867 obj_put:
1868         cl_object_put(env, subobj);
1869
1870         return rc;
1871 }
1872
1873 /**
1874  * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1875  * This also handles the restarting of FIEMAP calls in case mapping overflows
1876  * the available number of extents in single call.
1877  *
1878  * \param env [in]              lustre environment
1879  * \param obj [in]              file object
1880  * \param fmkey [in]            fiemap request header and other info
1881  * \param fiemap [out]          fiemap buffer holding retrived map extents
1882  * \param buflen [in/out]       max buffer length of @fiemap, when iterate
1883  *                              each OST, it is used to limit max map needed
1884  * \retval 0    success
1885  * \retval < 0  error
1886  */
1887 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1888                              struct ll_fiemap_info_key *fmkey,
1889                              struct fiemap *fiemap, size_t *buflen)
1890 {
1891         struct lov_stripe_md_entry *lsme;
1892         struct lov_stripe_md *lsm;
1893         struct fiemap *fm_local = NULL;
1894         loff_t whole_start;
1895         loff_t whole_end;
1896         int entry;
1897         int start_entry = -1;
1898         int end_entry;
1899         int cur_stripe = 0;
1900         int stripe_count;
1901         unsigned int buffer_size = FIEMAP_BUFFER_SIZE;
1902         int rc = 0;
1903         struct fiemap_state fs = { 0 };
1904         struct lu_extent range;
1905         int cur_ext;
1906         int stripe_last;
1907         int start_stripe = 0;
1908         bool resume = false;
1909         ENTRY;
1910
1911         lsm = lov_lsm_addref(cl2lov(obj));
1912         if (lsm == NULL) {
1913                 /* no extent: there is no object for mapping */
1914                 fiemap->fm_mapped_extents = 0;
1915                 return 0;
1916         }
1917
1918         if (!(fiemap->fm_flags & FIEMAP_FLAG_DEVICE_ORDER)) {
1919                 /**
1920                  * If the entry count > 1 or stripe_count > 1 and the
1921                  * application does not understand DEVICE_ORDER flag,
1922                  * it cannot interpret the extents correctly.
1923                  */
1924                 if (lsm->lsm_entry_count > 1 ||
1925                     (lsm->lsm_entry_count == 1 &&
1926                      lsm->lsm_entries[0]->lsme_stripe_count > 1))
1927                         GOTO(out_lsm, rc = -ENOTSUPP);
1928         }
1929
1930         /* No support for DOM layout yet. */
1931         if (lsme_is_dom(lsm->lsm_entries[0]))
1932                 GOTO(out_lsm, rc = -ENOTSUPP);
1933
1934         if (lsm->lsm_is_released) {
1935                 if (fiemap->fm_start < fmkey->lfik_oa.o_size) {
1936                         /**
1937                          * released file, return a minimal FIEMAP if
1938                          * request fits in file-size.
1939                          */
1940                         fiemap->fm_mapped_extents = 1;
1941                         fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1942                         if (fiemap->fm_start + fiemap->fm_length <
1943                             fmkey->lfik_oa.o_size)
1944                                 fiemap->fm_extents[0].fe_length =
1945                                         fiemap->fm_length;
1946                         else
1947                                 fiemap->fm_extents[0].fe_length =
1948                                         fmkey->lfik_oa.o_size -
1949                                         fiemap->fm_start;
1950                         fiemap->fm_extents[0].fe_flags |=
1951                                 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1952                 }
1953                 GOTO(out_lsm, rc = 0);
1954         }
1955
1956         /* buffer_size is small to hold fm_extent_count of extents. */
1957         if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1958                 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1959
1960         OBD_ALLOC_LARGE(fm_local, buffer_size);
1961         if (fm_local == NULL)
1962                 GOTO(out_lsm, rc = -ENOMEM);
1963
1964         /**
1965          * Requested extent count exceeds the fiemap buffer size, shrink our
1966          * ambition.
1967          */
1968         if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1969                 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1970
1971         fs.fs_enough = false;
1972         fs.fs_cur_extent = 0;
1973         fs.fs_fm = fm_local;
1974         fs.fs_cnt_need = fiemap_size_to_count(buffer_size);
1975
1976         whole_start = fiemap->fm_start;
1977         /* whole_start is beyond the end of the file */
1978         if (whole_start > fmkey->lfik_oa.o_size)
1979                 GOTO(out_fm_local, rc = -EINVAL);
1980         whole_end = (fiemap->fm_length == OBD_OBJECT_EOF) ?
1981                                         fmkey->lfik_oa.o_size + 1 :
1982                                         whole_start + fiemap->fm_length;
1983         /**
1984          * If fiemap->fm_length != OBD_OBJECT_EOF but whole_end exceeds file
1985          * size
1986          */
1987         if (whole_end > fmkey->lfik_oa.o_size + 1)
1988                 whole_end = fmkey->lfik_oa.o_size + 1;
1989
1990         /**
1991          * the high 16bits of fe_device remember which stripe the last
1992          * call has been arrived, we'd continue from there in this call.
1993          */
1994         if (fiemap->fm_extent_count && fiemap->fm_extents[0].fe_logical)
1995                 resume = true;
1996         stripe_last = get_fe_stripenr(&fiemap->fm_extents[0]);
1997         /**
1998          * stripe_last records stripe number we've been processed in the last
1999          * call
2000          */
2001         end_entry = lsm->lsm_entry_count - 1;
2002         cur_stripe = 0;
2003         for (entry = 0; entry <= end_entry; entry++) {
2004                 lsme = lsm->lsm_entries[entry];
2005                 if (cur_stripe + lsme->lsme_stripe_count >= stripe_last) {
2006                         start_entry = entry;
2007                         start_stripe = stripe_last - cur_stripe;
2008                         break;
2009                 }
2010
2011                 cur_stripe += lsme->lsme_stripe_count;
2012         }
2013         if (start_entry == -1) {
2014                 CERROR(DFID": FIEMAP does not init start entry, cur_stripe=%d, "
2015                        "stripe_last=%d\n", PFID(lu_object_fid(&obj->co_lu)),
2016                        cur_stripe, stripe_last);
2017                 GOTO(out_fm_local, rc = -EINVAL);
2018         }
2019         /**
2020          * @start_entry & @start_stripe records the position of fiemap
2021          * resumption @stripe_last keeps recording the absolution position
2022          * we'are processing. @resume indicates we'd honor @start_stripe.
2023          */
2024
2025         range.e_start = whole_start;
2026         range.e_end = whole_end;
2027
2028         for (entry = start_entry; entry <= end_entry; entry++) {
2029                 /* remeber to update stripe_last accordingly */
2030                 lsme = lsm->lsm_entries[entry];
2031
2032                 /* FLR could contain component holes between entries */
2033                 if (!lsme_inited(lsme)) {
2034                         stripe_last += lsme->lsme_stripe_count;
2035                         resume = false;
2036                         continue;
2037                 }
2038
2039                 if (!lu_extent_is_overlapped(&range, &lsme->lsme_extent)) {
2040                         stripe_last += lsme->lsme_stripe_count;
2041                         resume = false;
2042                         continue;
2043                 }
2044
2045                 /* prepare for a component entry iteration */
2046                 if (lsme->lsme_extent.e_start > whole_start)
2047                         fs.fs_ext.e_start = lsme->lsme_extent.e_start;
2048                 else
2049                         fs.fs_ext.e_start = whole_start;
2050                 if (lsme->lsme_extent.e_end > whole_end)
2051                         fs.fs_ext.e_end = whole_end;
2052                 else
2053                         fs.fs_ext.e_end = lsme->lsme_extent.e_end;
2054
2055                 /* Calculate start stripe, last stripe and length of mapping */
2056                 if (resume) {
2057                         fs.fs_start_stripe = start_stripe;
2058                         /* put stripe_last to the first stripe of the comp */
2059                         stripe_last -= start_stripe;
2060                         resume = false;
2061                 } else {
2062                         fs.fs_start_stripe = lov_stripe_number(lsm, entry,
2063                                                         fs.fs_ext.e_start);
2064                 }
2065                 fs.fs_last_stripe = fiemap_calc_last_stripe(lsm, entry,
2066                                         &fs.fs_ext, fs.fs_start_stripe,
2067                                         &stripe_count);
2068                 /**
2069                  * A new mirror component is under process, reset
2070                  * fs.fs_end_offset and then fiemap_for_stripe() starts from
2071                  * the overlapping extent, otherwise starts from
2072                  * fs.fs_end_offset.
2073                  */
2074                 if (entry > start_entry && lsme->lsme_extent.e_start == 0) {
2075                         /* new mirror */
2076                         fs.fs_end_offset = 0;
2077                 } else {
2078                         fs.fs_end_offset = fiemap_calc_fm_end_offset(fiemap,
2079                                                 lsm, entry, &fs.fs_ext,
2080                                                 &fs.fs_start_stripe);
2081                 }
2082
2083                 /* Check each stripe */
2084                 for (cur_stripe = fs.fs_start_stripe; stripe_count > 0;
2085                      --stripe_count,
2086                      cur_stripe = (cur_stripe + 1) % lsme->lsme_stripe_count) {
2087                         /* reset fs_finish_stripe */
2088                         fs.fs_finish_stripe = false;
2089                         rc = fiemap_for_stripe(env, obj, lsm, fiemap, buflen,
2090                                                fmkey, entry, stripe_last,
2091                                                cur_stripe, &fs);
2092                         if (rc < 0)
2093                                 GOTO(out_fm_local, rc);
2094                         if (fs.fs_enough) {
2095                                 stripe_last += cur_stripe;
2096                                 GOTO(finish, rc);
2097                         }
2098                         if (fs.fs_finish_stripe)
2099                                 break;
2100                 } /* for each stripe */
2101                 stripe_last += lsme->lsme_stripe_count;
2102         } /* for covering layout component entry */
2103
2104 finish:
2105         if (fs.fs_cur_extent > 0)
2106                 cur_ext = fs.fs_cur_extent - 1;
2107         else
2108                 cur_ext = 0;
2109
2110         /* done all the processing */
2111         if (entry > end_entry)
2112                 fiemap->fm_extents[cur_ext].fe_flags |= FIEMAP_EXTENT_LAST;
2113
2114         /* Indicate that we are returning device offsets unless file just has
2115          * single stripe */
2116         if (lsm->lsm_entry_count > 1 ||
2117             (lsm->lsm_entry_count == 1 &&
2118              lsm->lsm_entries[0]->lsme_stripe_count > 1))
2119                 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
2120
2121         if (fiemap->fm_extent_count == 0)
2122                 goto skip_last_device_calc;
2123
2124 skip_last_device_calc:
2125         fiemap->fm_mapped_extents = fs.fs_cur_extent;
2126 out_fm_local:
2127         OBD_FREE_LARGE(fm_local, buffer_size);
2128
2129 out_lsm:
2130         lov_lsm_put(lsm);
2131         return rc;
2132 }
2133
2134 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
2135                                 struct lov_user_md __user *lum, size_t size)
2136 {
2137         struct lov_object       *lov = cl2lov(obj);
2138         struct lov_stripe_md    *lsm;
2139         int                     rc = 0;
2140         ENTRY;
2141
2142         lsm = lov_lsm_addref(lov);
2143         if (lsm == NULL)
2144                 RETURN(-ENODATA);
2145
2146         rc = lov_getstripe(env, cl2lov(obj), lsm, lum, size);
2147         lov_lsm_put(lsm);
2148         RETURN(rc);
2149 }
2150
2151 static int lov_object_layout_get(const struct lu_env *env,
2152                                  struct cl_object *obj,
2153                                  struct cl_layout *cl)
2154 {
2155         struct lov_object *lov = cl2lov(obj);
2156         struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2157         struct lu_buf *buf = &cl->cl_buf;
2158         ssize_t rc;
2159         ENTRY;
2160
2161         if (lsm == NULL) {
2162                 cl->cl_size = 0;
2163                 cl->cl_layout_gen = CL_LAYOUT_GEN_EMPTY;
2164
2165                 RETURN(0);
2166         }
2167
2168         cl->cl_size = lov_comp_md_size(lsm);
2169         cl->cl_layout_gen = lsm->lsm_layout_gen;
2170         cl->cl_is_released = lsm->lsm_is_released;
2171         cl->cl_is_composite = lsm_is_composite(lsm->lsm_magic);
2172
2173         rc = lov_lsm_pack(lsm, buf->lb_buf, buf->lb_len);
2174         lov_lsm_put(lsm);
2175
2176         /* return error or number of bytes */
2177         RETURN(rc);
2178 }
2179
2180 static loff_t lov_object_maxbytes(struct cl_object *obj)
2181 {
2182         struct lov_object *lov = cl2lov(obj);
2183         struct lov_stripe_md *lsm = lov_lsm_addref(lov);
2184         loff_t maxbytes;
2185
2186         if (lsm == NULL)
2187                 return LLONG_MAX;
2188
2189         maxbytes = lsm->lsm_maxbytes;
2190
2191         lov_lsm_put(lsm);
2192
2193         return maxbytes;
2194 }
2195
2196 static int lov_object_flush(const struct lu_env *env, struct cl_object *obj,
2197                             struct ldlm_lock *lock)
2198 {
2199         return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_flush, true, env, obj,
2200                                      lock);
2201 }
2202
2203 static const struct cl_object_operations lov_ops = {
2204         .coo_page_init    = lov_page_init,
2205         .coo_lock_init    = lov_lock_init,
2206         .coo_io_init      = lov_io_init,
2207         .coo_attr_get     = lov_attr_get,
2208         .coo_attr_update  = lov_attr_update,
2209         .coo_conf_set     = lov_conf_set,
2210         .coo_getstripe    = lov_object_getstripe,
2211         .coo_layout_get   = lov_object_layout_get,
2212         .coo_maxbytes     = lov_object_maxbytes,
2213         .coo_fiemap       = lov_object_fiemap,
2214         .coo_object_flush = lov_object_flush
2215 };
2216
2217 static const struct lu_object_operations lov_lu_obj_ops = {
2218         .loo_object_init        = lov_object_init,
2219         .loo_object_delete      = lov_object_delete,
2220         .loo_object_release     = NULL,
2221         .loo_object_free        = lov_object_free,
2222         .loo_object_print       = lov_object_print,
2223         .loo_object_invariant   = NULL,
2224 };
2225
2226 struct lu_object *lov_object_alloc(const struct lu_env *env,
2227                                    const struct lu_object_header *unused,
2228                                    struct lu_device *dev)
2229 {
2230         struct lov_object *lov;
2231         struct lu_object  *obj;
2232
2233         ENTRY;
2234         OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
2235         if (lov != NULL) {
2236                 obj = lov2lu(lov);
2237                 lu_object_init(obj, NULL, dev);
2238                 lov->lo_cl.co_ops = &lov_ops;
2239                 lov->lo_type = -1; /* invalid, to catch uninitialized type */
2240                 /*
2241                  * object io operation vector (cl_object::co_iop) is installed
2242                  * later in lov_object_init(), as different vectors are used
2243                  * for object with different layouts.
2244                  */
2245                 obj->lo_ops = &lov_lu_obj_ops;
2246         } else
2247                 obj = NULL;
2248         RETURN(obj);
2249 }
2250
2251 static struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
2252 {
2253         struct lov_stripe_md *lsm = NULL;
2254
2255         lov_conf_freeze(lov);
2256         if (lov->lo_lsm != NULL) {
2257                 lsm = lsm_addref(lov->lo_lsm);
2258                 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
2259                         lsm, atomic_read(&lsm->lsm_refc),
2260                         test_bit(LO_LAYOUT_INVALID, &lov->lo_obj_flags),
2261                         current);
2262         }
2263         lov_conf_thaw(lov);
2264         return lsm;
2265 }
2266
2267 int lov_read_and_clear_async_rc(struct cl_object *clob)
2268 {
2269         struct lu_object *luobj;
2270         int rc = 0;
2271         ENTRY;
2272
2273         luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
2274                                  &lov_device_type);
2275         if (luobj != NULL) {
2276                 struct lov_object *lov = lu2lov(luobj);
2277
2278                 lov_conf_freeze(lov);
2279                 switch (lov->lo_type) {
2280                 case LLT_COMP: {
2281                         struct lov_stripe_md *lsm;
2282                         int i;
2283
2284                         lsm = lov->lo_lsm;
2285                         LASSERT(lsm != NULL);
2286                         for (i = 0; i < lsm->lsm_entry_count; i++) {
2287                                 struct lov_stripe_md_entry *lse =
2288                                                 lsm->lsm_entries[i];
2289                                 int j;
2290
2291                                 if (!lsme_inited(lse))
2292                                         break;
2293
2294                                 for (j = 0; j < lse->lsme_stripe_count; j++) {
2295                                         struct lov_oinfo *loi =
2296                                                         lse->lsme_oinfo[j];
2297
2298                                         if (lov_oinfo_is_dummy(loi))
2299                                                 continue;
2300
2301                                         if (loi->loi_ar.ar_rc && !rc)
2302                                                 rc = loi->loi_ar.ar_rc;
2303                                         loi->loi_ar.ar_rc = 0;
2304                                 }
2305                         }
2306                 }
2307                 case LLT_RELEASED:
2308                 case LLT_EMPTY:
2309                         /* fall through */
2310                 case LLT_FOREIGN:
2311                         break;
2312                 default:
2313                         LBUG();
2314                 }
2315                 lov_conf_thaw(lov);
2316         }
2317         RETURN(rc);
2318 }
2319 EXPORT_SYMBOL(lov_read_and_clear_async_rc);
2320
2321 /** @} lov */