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