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