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