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