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