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