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