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