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