Whamcloud - gitweb
8feeee246b5bf167bfbc018b799ff22e61e6c7a0
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_object for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOV
43
44 #include "lov_cl_internal.h"
45
46 /** \addtogroup lov
47  *  @{
48  */
49
50 /*****************************************************************************
51  *
52  * Layout operations.
53  *
54  */
55
56 struct lov_layout_operations {
57         int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
58                         struct lov_object *lov,
59                         const struct cl_object_conf *conf,
60                         union lov_layout_state *state);
61         int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
62                            union lov_layout_state *state);
63         void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
64                          union lov_layout_state *state);
65         void (*llo_install)(const struct lu_env *env, struct lov_object *lov,
66                             union lov_layout_state *state);
67         int  (*llo_print)(const struct lu_env *env, void *cookie,
68                           lu_printer_t p, const struct lu_object *o);
69         int  (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
70                               struct cl_page *page, pgoff_t index);
71         int  (*llo_lock_init)(const struct lu_env *env,
72                               struct cl_object *obj, struct cl_lock *lock,
73                               const struct cl_io *io);
74         int  (*llo_io_init)(const struct lu_env *env,
75                             struct cl_object *obj, struct cl_io *io);
76         int  (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
77                             struct cl_attr *attr);
78         int  (*llo_find_cbdata)(const struct lu_env *env, struct cl_object *obj,
79                                 ldlm_iterator_t iter, void *data);
80 };
81
82 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
83
84 struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj)
85 {
86         struct lu_object *luobj;
87         struct lov_stripe_md *lsm = NULL;
88
89         if (clobj == NULL)
90                 return NULL;
91
92         luobj = lu_object_locate(&cl_object_header(clobj)->coh_lu,
93                                  &lov_device_type);
94         if (luobj != NULL)
95                 lsm = lov_lsm_addref(lu2lov(luobj));
96         return lsm;
97 }
98 EXPORT_SYMBOL(lov_lsm_get);
99
100 void lov_lsm_put(struct cl_object *unused, struct lov_stripe_md *lsm)
101 {
102         if (lsm != NULL)
103                 lov_free_memmd(&lsm);
104 }
105 EXPORT_SYMBOL(lov_lsm_put);
106
107 /*****************************************************************************
108  *
109  * Lov object layout operations.
110  *
111  */
112
113 static void lov_install_empty(const struct lu_env *env,
114                               struct lov_object *lov,
115                               union  lov_layout_state *state)
116 {
117         /*
118          * File without objects.
119          */
120 }
121
122 static int lov_init_empty(const struct lu_env *env,
123                           struct lov_device *dev, struct lov_object *lov,
124                           const struct cl_object_conf *conf,
125                           union  lov_layout_state *state)
126 {
127         return 0;
128 }
129
130 static void lov_install_raid0(const struct lu_env *env,
131                               struct lov_object *lov,
132                               union  lov_layout_state *state)
133 {
134 }
135
136 static struct cl_object *lov_sub_find(const struct lu_env *env,
137                                       struct cl_device *dev,
138                                       const struct lu_fid *fid,
139                                       const struct cl_object_conf *conf)
140 {
141         struct lu_object *o;
142
143         ENTRY;
144         o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
145         LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
146         RETURN(lu2cl(o));
147 }
148
149 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
150                         struct cl_object *stripe, struct lov_layout_raid0 *r0,
151                         int idx)
152 {
153         struct cl_object_header *hdr;
154         struct cl_object_header *subhdr;
155         struct cl_object_header *parent;
156         struct lov_oinfo        *oinfo;
157         int result;
158
159         if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
160                 /* For sanity:test_206.
161                  * Do not leave the object in cache to avoid accessing
162                  * freed memory. This is because osc_object is referring to
163                  * lov_oinfo of lsm_stripe_data which will be freed due to
164                  * this failure. */
165                 cl_object_kill(env, stripe);
166                 cl_object_put(env, stripe);
167                 return -EIO;
168         }
169
170         hdr    = cl_object_header(lov2cl(lov));
171         subhdr = cl_object_header(stripe);
172
173         oinfo = lov->lo_lsm->lsm_oinfo[idx];
174         CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: ostid: "DOSTID
175                " idx: %d gen: %d\n",
176                PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
177                PFID(&hdr->coh_lu.loh_fid), hdr, POSTID(&oinfo->loi_oi),
178                oinfo->loi_ost_idx, oinfo->loi_ost_gen);
179
180         /* reuse ->coh_attr_guard to protect coh_parent change */
181         spin_lock(&subhdr->coh_attr_guard);
182         parent = subhdr->coh_parent;
183         if (parent == NULL) {
184                 subhdr->coh_parent = hdr;
185                 spin_unlock(&subhdr->coh_attr_guard);
186                 subhdr->coh_nesting = hdr->coh_nesting + 1;
187                 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
188                 r0->lo_sub[idx] = cl2lovsub(stripe);
189                 r0->lo_sub[idx]->lso_super = lov;
190                 r0->lo_sub[idx]->lso_index = idx;
191                 result = 0;
192         } else {
193                 struct lu_object  *old_obj;
194                 struct lov_object *old_lov;
195                 unsigned int mask = D_INODE;
196
197                 spin_unlock(&subhdr->coh_attr_guard);
198                 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
199                 LASSERT(old_obj != NULL);
200                 old_lov = cl2lov(lu2cl(old_obj));
201                 if (old_lov->lo_layout_invalid) {
202                         /* the object's layout has already changed but isn't
203                          * refreshed */
204                         lu_object_unhash(env, &stripe->co_lu);
205                         result = -EAGAIN;
206                 } else {
207                         mask = D_ERROR;
208                         result = -EIO;
209                 }
210
211                 LU_OBJECT_DEBUG(mask, env, &stripe->co_lu,
212                                 "stripe %d is already owned.\n", idx);
213                 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.\n");
214                 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
215                 cl_object_put(env, stripe);
216         }
217         return result;
218 }
219
220 static int lov_page_slice_fixup(struct lov_object *lov,
221                                 struct cl_object *stripe)
222 {
223         struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
224         struct cl_object *o;
225
226         if (stripe == NULL)
227                 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
228                        cfs_size_round(sizeof(struct lov_page));
229
230         cl_object_for_each(o, stripe)
231                 o->co_slice_off += hdr->coh_page_bufsize;
232
233         return cl_object_header(stripe)->coh_page_bufsize;
234 }
235
236 static int lov_init_raid0(const struct lu_env *env,
237                           struct lov_device *dev, struct lov_object *lov,
238                           const struct cl_object_conf *conf,
239                           union  lov_layout_state *state)
240 {
241         int result;
242         int i;
243
244         struct cl_object        *stripe;
245         struct lov_thread_info  *lti     = lov_env_info(env);
246         struct cl_object_conf   *subconf = &lti->lti_stripe_conf;
247         struct lov_stripe_md    *lsm     = conf->u.coc_md->lsm;
248         struct lu_fid           *ofid    = &lti->lti_fid;
249         struct lov_layout_raid0 *r0      = &state->raid0;
250
251         ENTRY;
252
253         if (lsm->lsm_magic != LOV_MAGIC_V1 && lsm->lsm_magic != LOV_MAGIC_V3) {
254                 dump_lsm(D_ERROR, lsm);
255                 LASSERTF(0, "magic mismatch, expected %d/%d, actual %d.\n",
256                          LOV_MAGIC_V1, LOV_MAGIC_V3, lsm->lsm_magic);
257         }
258
259         LASSERT(lov->lo_lsm == NULL);
260         lov->lo_lsm = lsm_addref(lsm);
261         r0->lo_nr  = lsm->lsm_stripe_count;
262         LASSERT(r0->lo_nr <= lov_targets_nr(dev));
263
264         lov->lo_layout_invalid = true;
265
266         OBD_ALLOC_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
267         if (r0->lo_sub != NULL) {
268                 int psz = 0;
269
270                 result = 0;
271                 subconf->coc_inode = conf->coc_inode;
272                 spin_lock_init(&r0->lo_sub_lock);
273                 /*
274                  * Create stripe cl_objects.
275                  */
276                 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
277                         struct cl_device *subdev;
278                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
279                         int ost_idx = oinfo->loi_ost_idx;
280
281                         if (lov_oinfo_is_dummy(oinfo))
282                                 continue;
283
284                         result = ostid_to_fid(ofid, &oinfo->loi_oi,
285                                               oinfo->loi_ost_idx);
286                         if (result != 0)
287                                 GOTO(out, result);
288
289                         subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
290                         subconf->u.coc_oinfo = oinfo;
291                         LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
292                         /* In the function below, .hs_keycmp resolves to
293                          * lu_obj_hop_keycmp() */
294                         /* coverity[overrun-buffer-val] */
295                         stripe = lov_sub_find(env, subdev, ofid, subconf);
296                         if (!IS_ERR(stripe)) {
297                                 result = lov_init_sub(env, lov, stripe, r0, i);
298                                 if (result == -EAGAIN) { /* try again */
299                                         --i;
300                                         result = 0;
301                                         continue;
302                                 }
303                         } else {
304                                 result = PTR_ERR(stripe);
305                         }
306
307                         if (result == 0) {
308                                 int sz = lov_page_slice_fixup(lov, stripe);
309                                 LASSERT(ergo(psz > 0, psz == sz));
310                                 psz = sz;
311                         }
312                 }
313                 if (result == 0)
314                         cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
315         } else
316                 result = -ENOMEM;
317 out:
318         RETURN(result);
319 }
320
321 static int lov_init_released(const struct lu_env *env,
322                         struct lov_device *dev, struct lov_object *lov,
323                         const struct cl_object_conf *conf,
324                         union  lov_layout_state *state)
325 {
326         struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
327
328         LASSERT(lsm != NULL);
329         LASSERT(lsm_is_released(lsm));
330         LASSERT(lov->lo_lsm == NULL);
331
332         lov->lo_lsm = lsm_addref(lsm);
333         return 0;
334 }
335
336 static struct cl_object *lov_find_subobj(const struct lu_env *env,
337                                          struct lov_object *lov,
338                                          struct lov_stripe_md *lsm,
339                                          int stripe_idx)
340 {
341         struct lov_device       *dev = lu2lov_dev(lov2lu(lov)->lo_dev);
342         struct lov_oinfo        *oinfo = lsm->lsm_oinfo[stripe_idx];
343         struct lov_thread_info  *lti = lov_env_info(env);
344         struct lu_fid           *ofid = &lti->lti_fid;
345         struct cl_device        *subdev;
346         int                     ost_idx;
347         int                     rc;
348         struct cl_object        *result;
349
350         if (lov->lo_type != LLT_RAID0)
351                 GOTO(out, result = NULL);
352
353         ost_idx = oinfo->loi_ost_idx;
354         rc = ostid_to_fid(ofid, &oinfo->loi_oi, ost_idx);
355         if (rc != 0)
356                 GOTO(out, result = NULL);
357
358         subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
359         result = lov_sub_find(env, subdev, ofid, NULL);
360 out:
361         if (result == NULL)
362                 result = ERR_PTR(-EINVAL);
363         return result;
364 }
365
366 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
367                             union lov_layout_state *state)
368 {
369         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
370
371         lov_layout_wait(env, lov);
372         return 0;
373 }
374
375 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
376                                struct lovsub_object *los, int idx)
377 {
378         struct cl_object        *sub;
379         struct lov_layout_raid0 *r0;
380         struct lu_site          *site;
381         struct lu_site_bkt_data *bkt;
382         wait_queue_t          *waiter;
383
384         r0  = &lov->u.raid0;
385         LASSERT(r0->lo_sub[idx] == los);
386
387         sub  = lovsub2cl(los);
388         site = sub->co_lu.lo_dev->ld_site;
389         bkt  = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid);
390
391         cl_object_kill(env, sub);
392         /* release a reference to the sub-object and ... */
393         lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
394         cl_object_put(env, sub);
395
396         /* ... wait until it is actually destroyed---sub-object clears its
397          * ->lo_sub[] slot in lovsub_object_fini() */
398         if (r0->lo_sub[idx] == los) {
399                 waiter = &lov_env_info(env)->lti_waiter;
400                 init_waitqueue_entry_current(waiter);
401                 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
402                 set_current_state(TASK_UNINTERRUPTIBLE);
403                 while (1) {
404                         /* this wait-queue is signaled at the end of
405                          * lu_object_free(). */
406                         set_current_state(TASK_UNINTERRUPTIBLE);
407                         spin_lock(&r0->lo_sub_lock);
408                         if (r0->lo_sub[idx] == los) {
409                                 spin_unlock(&r0->lo_sub_lock);
410                                 waitq_wait(waiter, TASK_UNINTERRUPTIBLE);
411                         } else {
412                                 spin_unlock(&r0->lo_sub_lock);
413                                 set_current_state(TASK_RUNNING);
414                                 break;
415                         }
416                 }
417                 remove_wait_queue(&bkt->lsb_marche_funebre, waiter);
418         }
419         LASSERT(r0->lo_sub[idx] == NULL);
420 }
421
422 static int lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
423                             union lov_layout_state *state)
424 {
425         struct lov_layout_raid0 *r0 = &state->raid0;
426         struct lov_stripe_md    *lsm = lov->lo_lsm;
427         int i;
428
429         ENTRY;
430
431         dump_lsm(D_INODE, lsm);
432
433         lov_layout_wait(env, lov);
434         if (r0->lo_sub != NULL) {
435                 for (i = 0; i < r0->lo_nr; ++i) {
436                         struct lovsub_object *los = r0->lo_sub[i];
437
438                         if (los != NULL) {
439                                 cl_object_prune(env, &los->lso_cl);
440                                 /*
441                                  * If top-level object is to be evicted from
442                                  * the cache, so are its sub-objects.
443                                  */
444                                 lov_subobject_kill(env, lov, los, i);
445                         }
446                 }
447         }
448         RETURN(0);
449 }
450
451 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
452                            union lov_layout_state *state)
453 {
454         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
455 }
456
457 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
458                            union lov_layout_state *state)
459 {
460         struct lov_layout_raid0 *r0 = &state->raid0;
461         ENTRY;
462
463         if (r0->lo_sub != NULL) {
464                 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
465                 r0->lo_sub = NULL;
466         }
467
468         dump_lsm(D_INODE, lov->lo_lsm);
469         lov_free_memmd(&lov->lo_lsm);
470
471         EXIT;
472 }
473
474 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
475                                 union lov_layout_state *state)
476 {
477         ENTRY;
478         dump_lsm(D_INODE, lov->lo_lsm);
479         lov_free_memmd(&lov->lo_lsm);
480         EXIT;
481 }
482
483 static int lov_print_empty(const struct lu_env *env, void *cookie,
484                            lu_printer_t p, const struct lu_object *o)
485 {
486         (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
487         return 0;
488 }
489
490 static int lov_print_raid0(const struct lu_env *env, void *cookie,
491                            lu_printer_t p, const struct lu_object *o)
492 {
493         struct lov_object       *lov = lu2lov(o);
494         struct lov_layout_raid0 *r0  = lov_r0(lov);
495         struct lov_stripe_md    *lsm = lov->lo_lsm;
496         int                      i;
497
498         (*p)(env, cookie, "stripes: %d, %s, lsm{%p 0x%08X %d %u %u}:\n",
499                 r0->lo_nr, lov->lo_layout_invalid ? "invalid" : "valid", lsm,
500                 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
501                 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
502         for (i = 0; i < r0->lo_nr; ++i) {
503                 struct lu_object *sub;
504
505                 if (r0->lo_sub[i] != NULL) {
506                         sub = lovsub2lu(r0->lo_sub[i]);
507                         lu_object_print(env, cookie, p, sub);
508                 } else {
509                         (*p)(env, cookie, "sub %d absent\n", i);
510                 }
511         }
512         return 0;
513 }
514
515 static int lov_print_released(const struct lu_env *env, void *cookie,
516                                 lu_printer_t p, const struct lu_object *o)
517 {
518         struct lov_object       *lov = lu2lov(o);
519         struct lov_stripe_md    *lsm = lov->lo_lsm;
520
521         (*p)(env, cookie,
522                 "released: %s, lsm{%p 0x%08X %d %u %u}:\n",
523                 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
524                 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
525                 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
526         return 0;
527 }
528
529 /**
530  * Implements cl_object_operations::coo_attr_get() method for an object
531  * without stripes (LLT_EMPTY layout type).
532  *
533  * The only attributes this layer is authoritative in this case is
534  * cl_attr::cat_blocks---it's 0.
535  */
536 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
537                               struct cl_attr *attr)
538 {
539         attr->cat_blocks = 0;
540         return 0;
541 }
542
543 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
544                               struct cl_attr *attr)
545 {
546         struct lov_object       *lov = cl2lov(obj);
547         struct lov_layout_raid0 *r0 = lov_r0(lov);
548         struct cl_attr          *lov_attr = &r0->lo_attr;
549         int                      result = 0;
550
551         ENTRY;
552
553         /* this is called w/o holding type guard mutex, so it must be inside
554          * an on going IO otherwise lsm may be replaced.
555          * LU-2117: it turns out there exists one exception. For mmaped files,
556          * the lock of those files may be requested in the other file's IO
557          * context, and this function is called in ccc_lock_state(), it will
558          * hit this assertion.
559          * Anyway, it's still okay to call attr_get w/o type guard as layout
560          * can't go if locks exist. */
561         /* LASSERT(atomic_read(&lsm->lsm_refc) > 1); */
562
563         if (!r0->lo_attr_valid) {
564                 struct lov_stripe_md    *lsm = lov->lo_lsm;
565                 struct ost_lvb          *lvb = &lov_env_info(env)->lti_lvb;
566                 __u64                    kms = 0;
567
568                 memset(lvb, 0, sizeof(*lvb));
569                 /* XXX: timestamps can be negative by sanity:test_39m,
570                  * how can it be? */
571                 lvb->lvb_atime = LLONG_MIN;
572                 lvb->lvb_ctime = LLONG_MIN;
573                 lvb->lvb_mtime = LLONG_MIN;
574
575                 /*
576                  * XXX that should be replaced with a loop over sub-objects,
577                  * doing cl_object_attr_get() on them. But for now, let's
578                  * reuse old lov code.
579                  */
580
581                 /*
582                  * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
583                  * happy. It's not needed, because new code uses
584                  * ->coh_attr_guard spin-lock to protect consistency of
585                  * sub-object attributes.
586                  */
587                 lov_stripe_lock(lsm);
588                 result = lov_merge_lvb_kms(lsm, lvb, &kms);
589                 lov_stripe_unlock(lsm);
590                 if (result == 0) {
591                         cl_lvb2attr(lov_attr, lvb);
592                         lov_attr->cat_kms = kms;
593                         r0->lo_attr_valid = 1;
594                 }
595         }
596         if (result == 0) { /* merge results */
597                 attr->cat_blocks = lov_attr->cat_blocks;
598                 attr->cat_size = lov_attr->cat_size;
599                 attr->cat_kms = lov_attr->cat_kms;
600                 if (attr->cat_atime < lov_attr->cat_atime)
601                         attr->cat_atime = lov_attr->cat_atime;
602                 if (attr->cat_ctime < lov_attr->cat_ctime)
603                         attr->cat_ctime = lov_attr->cat_ctime;
604                 if (attr->cat_mtime < lov_attr->cat_mtime)
605                         attr->cat_mtime = lov_attr->cat_mtime;
606         }
607         RETURN(result);
608 }
609
610 static int lov_find_cbdata_empty(const struct lu_env *env,
611                                  struct cl_object *obj, ldlm_iterator_t iter,
612                                  void *data)
613 {
614         return 0;
615 }
616
617 static int lov_find_cbdata_raid0(const struct lu_env *env,
618                                  struct cl_object *obj, ldlm_iterator_t iter,
619                                  void *data)
620 {
621         struct lov_object       *lov = cl2lov(obj);
622         struct lov_layout_raid0 *r0 = lov_r0(lov);
623         struct cl_object        *subobj;
624         int                     i;
625         int                     rc = 0;
626
627         for (i = 0; i < r0->lo_nr; ++i) {
628                 if (r0->lo_sub[i] == NULL)
629                         continue;
630
631                 subobj = lovsub2cl(r0->lo_sub[i]);
632
633                 rc = cl_object_find_cbdata(env, subobj, iter, data);
634                 if (rc != 0)
635                         break;
636         }
637
638         return rc;
639 }
640
641 const static struct lov_layout_operations lov_dispatch[] = {
642         [LLT_EMPTY] = {
643                 .llo_init      = lov_init_empty,
644                 .llo_delete    = lov_delete_empty,
645                 .llo_fini      = lov_fini_empty,
646                 .llo_install   = lov_install_empty,
647                 .llo_print     = lov_print_empty,
648                 .llo_page_init = lov_page_init_empty,
649                 .llo_lock_init = lov_lock_init_empty,
650                 .llo_io_init   = lov_io_init_empty,
651                 .llo_getattr   = lov_attr_get_empty,
652                 .llo_find_cbdata = lov_find_cbdata_empty
653         },
654         [LLT_RAID0] = {
655                 .llo_init      = lov_init_raid0,
656                 .llo_delete    = lov_delete_raid0,
657                 .llo_fini      = lov_fini_raid0,
658                 .llo_install   = lov_install_raid0,
659                 .llo_print     = lov_print_raid0,
660                 .llo_page_init = lov_page_init_raid0,
661                 .llo_lock_init = lov_lock_init_raid0,
662                 .llo_io_init   = lov_io_init_raid0,
663                 .llo_getattr   = lov_attr_get_raid0,
664                 .llo_find_cbdata = lov_find_cbdata_raid0
665         },
666         [LLT_RELEASED] = {
667                 .llo_init      = lov_init_released,
668                 .llo_delete    = lov_delete_empty,
669                 .llo_fini      = lov_fini_released,
670                 .llo_install   = lov_install_empty,
671                 .llo_print     = lov_print_released,
672                 .llo_page_init = lov_page_init_empty,
673                 .llo_lock_init = lov_lock_init_empty,
674                 .llo_io_init   = lov_io_init_released,
675                 .llo_getattr   = lov_attr_get_empty,
676                 .llo_find_cbdata = lov_find_cbdata_empty
677         }
678 };
679
680 /**
681  * Performs a double-dispatch based on the layout type of an object.
682  */
683 #define LOV_2DISPATCH_NOLOCK(obj, op, ...)                              \
684 ({                                                                      \
685         struct lov_object                      *__obj = (obj);          \
686         enum lov_layout_type                    __llt;                  \
687                                                                         \
688         __llt = __obj->lo_type;                                         \
689         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
690         lov_dispatch[__llt].op(__VA_ARGS__);                            \
691 })
692
693 /**
694  * Return lov_layout_type associated with a given lsm
695  */
696 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
697 {
698         if (lsm == NULL)
699                 return LLT_EMPTY;
700         if (lsm_is_released(lsm))
701                 return LLT_RELEASED;
702         return LLT_RAID0;
703 }
704
705 static inline void lov_conf_freeze(struct lov_object *lov)
706 {
707         if (lov->lo_owner != current)
708                 down_read(&lov->lo_type_guard);
709 }
710
711 static inline void lov_conf_thaw(struct lov_object *lov)
712 {
713         if (lov->lo_owner != current)
714                 up_read(&lov->lo_type_guard);
715 }
716
717 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...)                       \
718 ({                                                                      \
719         struct lov_object                      *__obj = (obj);          \
720         int                                     __lock = !!(lock);      \
721         typeof(lov_dispatch[0].op(__VA_ARGS__)) __result;               \
722                                                                         \
723         if (__lock)                                                     \
724                 lov_conf_freeze(__obj);                                 \
725         __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__);          \
726         if (__lock)                                                     \
727                 lov_conf_thaw(__obj);                                   \
728         __result;                                                       \
729 })
730
731 /**
732  * Performs a locked double-dispatch based on the layout type of an object.
733  */
734 #define LOV_2DISPATCH(obj, op, ...)                     \
735         LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
736
737 #define LOV_2DISPATCH_VOID(obj, op, ...)                                \
738 do {                                                                    \
739         struct lov_object                      *__obj = (obj);          \
740         enum lov_layout_type                    __llt;                  \
741                                                                         \
742         lov_conf_freeze(__obj);                                         \
743         __llt = __obj->lo_type;                                         \
744         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
745         lov_dispatch[__llt].op(__VA_ARGS__);                            \
746         lov_conf_thaw(__obj);                                           \
747 } while (0)
748
749 static void lov_conf_lock(struct lov_object *lov)
750 {
751         LASSERT(lov->lo_owner != current);
752         down_write(&lov->lo_type_guard);
753         LASSERT(lov->lo_owner == NULL);
754         lov->lo_owner = current;
755 }
756
757 static void lov_conf_unlock(struct lov_object *lov)
758 {
759         lov->lo_owner = NULL;
760         up_write(&lov->lo_type_guard);
761 }
762
763 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
764 {
765         struct l_wait_info lwi = { 0 };
766         ENTRY;
767
768         while (atomic_read(&lov->lo_active_ios) > 0) {
769                 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
770                         PFID(lu_object_fid(lov2lu(lov))),
771                         atomic_read(&lov->lo_active_ios));
772
773                 l_wait_event(lov->lo_waitq,
774                              atomic_read(&lov->lo_active_ios) == 0, &lwi);
775         }
776         RETURN(0);
777 }
778
779 static int lov_layout_change(const struct lu_env *unused,
780                              struct lov_object *lov,
781                              const struct cl_object_conf *conf)
782 {
783         int result;
784         enum lov_layout_type llt = LLT_EMPTY;
785         union lov_layout_state *state = &lov->u;
786         const struct lov_layout_operations *old_ops;
787         const struct lov_layout_operations *new_ops;
788
789         void *cookie;
790         struct lu_env *env;
791         int refcheck;
792         ENTRY;
793
794         LASSERT(0 <= lov->lo_type && lov->lo_type < ARRAY_SIZE(lov_dispatch));
795
796         if (conf->u.coc_md != NULL)
797                 llt = lov_type(conf->u.coc_md->lsm);
798         LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
799
800         cookie = cl_env_reenter();
801         env = cl_env_get(&refcheck);
802         if (IS_ERR(env)) {
803                 cl_env_reexit(cookie);
804                 RETURN(PTR_ERR(env));
805         }
806
807         CDEBUG(D_INODE, DFID" from %s to %s\n",
808                PFID(lu_object_fid(lov2lu(lov))),
809                llt2str(lov->lo_type), llt2str(llt));
810
811         old_ops = &lov_dispatch[lov->lo_type];
812         new_ops = &lov_dispatch[llt];
813
814         result = cl_object_prune(env, &lov->lo_cl);
815         if (result != 0)
816                 GOTO(out, result);
817
818         result = old_ops->llo_delete(env, lov, &lov->u);
819         if (result == 0) {
820                 old_ops->llo_fini(env, lov, &lov->u);
821
822                 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
823
824                 lov->lo_type = LLT_EMPTY;
825                 /* page bufsize fixup */
826                 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
827                         lov_page_slice_fixup(lov, NULL);
828
829                 result = new_ops->llo_init(env,
830                                         lu2lov_dev(lov->lo_cl.co_lu.lo_dev),
831                                         lov, conf, state);
832                 if (result == 0) {
833                         new_ops->llo_install(env, lov, state);
834                         lov->lo_type = llt;
835                 } else {
836                         new_ops->llo_delete(env, lov, state);
837                         new_ops->llo_fini(env, lov, state);
838                         /* this file becomes an EMPTY file. */
839                 }
840         }
841
842 out:
843         cl_env_put(env, &refcheck);
844         cl_env_reexit(cookie);
845         RETURN(result);
846 }
847
848 /*****************************************************************************
849  *
850  * Lov object operations.
851  *
852  */
853 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
854                     const struct lu_object_conf *conf)
855 {
856         struct lov_device            *dev   = lu2lov_dev(obj->lo_dev);
857         struct lov_object            *lov   = lu2lov(obj);
858         const struct cl_object_conf  *cconf = lu2cl_conf(conf);
859         union  lov_layout_state      *set   = &lov->u;
860         const struct lov_layout_operations *ops;
861         int result;
862
863         ENTRY;
864         init_rwsem(&lov->lo_type_guard);
865         atomic_set(&lov->lo_active_ios, 0);
866         init_waitqueue_head(&lov->lo_waitq);
867
868         cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
869
870         /* no locking is necessary, as object is being created */
871         lov->lo_type = lov_type(cconf->u.coc_md->lsm);
872         ops = &lov_dispatch[lov->lo_type];
873         result = ops->llo_init(env, dev, lov, cconf, set);
874         if (result == 0)
875                 ops->llo_install(env, lov, set);
876         RETURN(result);
877 }
878
879 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
880                         const struct cl_object_conf *conf)
881 {
882         struct lov_stripe_md    *lsm = NULL;
883         struct lov_object       *lov = cl2lov(obj);
884         int                      result = 0;
885         ENTRY;
886
887         lov_conf_lock(lov);
888         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
889                 lov->lo_layout_invalid = true;
890                 GOTO(out, result = 0);
891         }
892
893         if (conf->coc_opc == OBJECT_CONF_WAIT) {
894                 if (lov->lo_layout_invalid &&
895                     atomic_read(&lov->lo_active_ios) > 0) {
896                         lov_conf_unlock(lov);
897                         result = lov_layout_wait(env, lov);
898                         lov_conf_lock(lov);
899                 }
900                 GOTO(out, result);
901         }
902
903         LASSERT(conf->coc_opc == OBJECT_CONF_SET);
904
905         if (conf->u.coc_md != NULL)
906                 lsm = conf->u.coc_md->lsm;
907         if ((lsm == NULL && lov->lo_lsm == NULL) ||
908             ((lsm != NULL && lov->lo_lsm != NULL) &&
909              (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
910              (lov->lo_lsm->lsm_pattern == lsm->lsm_pattern))) {
911                 /* same version of layout */
912                 lov->lo_layout_invalid = false;
913                 GOTO(out, result = 0);
914         }
915
916         /* will change layout - check if there still exists active IO. */
917         if (atomic_read(&lov->lo_active_ios) > 0) {
918                 lov->lo_layout_invalid = true;
919                 GOTO(out, result = -EBUSY);
920         }
921
922         result = lov_layout_change(env, lov, conf);
923         lov->lo_layout_invalid = result != 0;
924         EXIT;
925
926 out:
927         lov_conf_unlock(lov);
928         CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
929                PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
930         RETURN(result);
931 }
932
933 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
934 {
935         struct lov_object *lov = lu2lov(obj);
936
937         ENTRY;
938         LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
939         EXIT;
940 }
941
942 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
943 {
944         struct lov_object *lov = lu2lov(obj);
945
946         ENTRY;
947         LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
948         lu_object_fini(obj);
949         OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
950         EXIT;
951 }
952
953 static int lov_object_print(const struct lu_env *env, void *cookie,
954                             lu_printer_t p, const struct lu_object *o)
955 {
956         return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
957 }
958
959 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
960                   struct cl_page *page, pgoff_t index)
961 {
962         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
963                                     index);
964 }
965
966 /**
967  * Implements cl_object_operations::clo_io_init() method for lov
968  * layer. Dispatches to the appropriate layout io initialization method.
969  */
970 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
971                 struct cl_io *io)
972 {
973         CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
974         return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
975                                      !io->ci_ignore_layout, env, obj, io);
976 }
977
978 /**
979  * An implementation of cl_object_operations::clo_attr_get() method for lov
980  * layer. For raid0 layout this collects and merges attributes of all
981  * sub-objects.
982  */
983 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
984                         struct cl_attr *attr)
985 {
986         /* do not take lock, as this function is called under a
987          * spin-lock. Layout is protected from changing by ongoing IO. */
988         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
989 }
990
991 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
992                            const struct cl_attr *attr, unsigned valid)
993 {
994         /*
995          * No dispatch is required here, as no layout implements this.
996          */
997         return 0;
998 }
999
1000 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
1001                   struct cl_lock *lock, const struct cl_io *io)
1002 {
1003         /* No need to lock because we've taken one refcount of layout.  */
1004         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
1005                                     io);
1006 }
1007
1008 /**
1009  * We calculate on which OST the mapping will end. If the length of mapping
1010  * is greater than (stripe_size * stripe_count) then the last_stripe will
1011  * will be one just before start_stripe. Else we check if the mapping
1012  * intersects each OST and find last_stripe.
1013  * This function returns the last_stripe and also sets the stripe_count
1014  * over which the mapping is spread
1015  *
1016  * \param lsm [in]              striping information for the file
1017  * \param fm_start [in]         logical start of mapping
1018  * \param fm_end [in]           logical end of mapping
1019  * \param start_stripe [in]     starting stripe of the mapping
1020  * \param stripe_count [out]    the number of stripes across which to map is
1021  *                              returned
1022  *
1023  * \retval last_stripe          return the last stripe of the mapping
1024  */
1025 static int fiemap_calc_last_stripe(struct lov_stripe_md *lsm,
1026                                    loff_t fm_start, loff_t fm_end,
1027                                    int start_stripe, int *stripe_count)
1028 {
1029         int last_stripe;
1030         loff_t obd_start;
1031         loff_t obd_end;
1032         int i, j;
1033
1034         if (fm_end - fm_start > lsm->lsm_stripe_size * lsm->lsm_stripe_count) {
1035                 last_stripe = (start_stripe < 1 ? lsm->lsm_stripe_count - 1 :
1036                                                               start_stripe - 1);
1037                 *stripe_count = lsm->lsm_stripe_count;
1038         } else {
1039                 for (j = 0, i = start_stripe; j < lsm->lsm_stripe_count;
1040                      i = (i + 1) % lsm->lsm_stripe_count, j++) {
1041                         if ((lov_stripe_intersects(lsm, i, fm_start, fm_end,
1042                                                    &obd_start, &obd_end)) == 0)
1043                                 break;
1044                 }
1045                 *stripe_count = j;
1046                 last_stripe = (start_stripe + j - 1) % lsm->lsm_stripe_count;
1047         }
1048
1049         return last_stripe;
1050 }
1051
1052 /**
1053  * Set fe_device and copy extents from local buffer into main return buffer.
1054  *
1055  * \param fiemap [out]          fiemap to hold all extents
1056  * \param lcl_fm_ext [in]       array of fiemap extents get from OSC layer
1057  * \param ost_index [in]        OST index to be written into the fm_device
1058  *                              field for each extent
1059  * \param ext_count [in]        number of extents to be copied
1060  * \param current_extent [in]   where to start copying in the extent array
1061  */
1062 static void fiemap_prepare_and_copy_exts(struct fiemap *fiemap,
1063                                          struct ll_fiemap_extent *lcl_fm_ext,
1064                                          int ost_index, unsigned int ext_count,
1065                                          int current_extent)
1066 {
1067         char            *to;
1068         unsigned int    ext;
1069
1070         for (ext = 0; ext < ext_count; ext++) {
1071                 lcl_fm_ext[ext].fe_device = ost_index;
1072                 lcl_fm_ext[ext].fe_flags |= FIEMAP_EXTENT_NET;
1073         }
1074
1075         /* Copy fm_extent's from fm_local to return buffer */
1076         to = (char *)fiemap + fiemap_count_to_size(current_extent);
1077         memcpy(to, lcl_fm_ext, ext_count * sizeof(struct ll_fiemap_extent));
1078 }
1079
1080 #define FIEMAP_BUFFER_SIZE 4096
1081
1082 /**
1083  * Non-zero fe_logical indicates that this is a continuation FIEMAP
1084  * call. The local end offset and the device are sent in the first
1085  * fm_extent. This function calculates the stripe number from the index.
1086  * This function returns a stripe_no on which mapping is to be restarted.
1087  *
1088  * This function returns fm_end_offset which is the in-OST offset at which
1089  * mapping should be restarted. If fm_end_offset=0 is returned then caller
1090  * will re-calculate proper offset in next stripe.
1091  * Note that the first extent is passed to lov_get_info via the value field.
1092  *
1093  * \param fiemap [in]           fiemap request header
1094  * \param lsm [in]              striping information for the file
1095  * \param fm_start [in]         logical start of mapping
1096  * \param fm_end [in]           logical end of mapping
1097  * \param start_stripe [out]    starting stripe will be returned in this
1098  */
1099 static loff_t fiemap_calc_fm_end_offset(struct fiemap *fiemap,
1100                                         struct lov_stripe_md *lsm,
1101                                         loff_t fm_start, loff_t fm_end,
1102                                         int *start_stripe)
1103 {
1104         loff_t local_end = fiemap->fm_extents[0].fe_logical;
1105         loff_t lun_start;
1106         loff_t lun_end;
1107         loff_t fm_end_offset;
1108         int stripe_no = -1;
1109         int i;
1110
1111         if (fiemap->fm_extent_count == 0 ||
1112             fiemap->fm_extents[0].fe_logical == 0)
1113                 return 0;
1114
1115         /* Find out stripe_no from ost_index saved in the fe_device */
1116         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1117                 struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
1118
1119                 if (lov_oinfo_is_dummy(oinfo))
1120                         continue;
1121
1122                 if (oinfo->loi_ost_idx == fiemap->fm_extents[0].fe_device) {
1123                         stripe_no = i;
1124                         break;
1125                 }
1126         }
1127
1128         if (stripe_no == -1)
1129                 return -EINVAL;
1130
1131         /* If we have finished mapping on previous device, shift logical
1132          * offset to start of next device */
1133         if (lov_stripe_intersects(lsm, stripe_no, fm_start, fm_end,
1134                                   &lun_start, &lun_end) != 0 &&
1135             local_end < lun_end) {
1136                 fm_end_offset = local_end;
1137                 *start_stripe = stripe_no;
1138         } else {
1139                 /* This is a special value to indicate that caller should
1140                  * calculate offset in next stripe. */
1141                 fm_end_offset = 0;
1142                 *start_stripe = (stripe_no + 1) % lsm->lsm_stripe_count;
1143         }
1144
1145         return fm_end_offset;
1146 }
1147
1148 /**
1149  * Break down the FIEMAP request and send appropriate calls to individual OSTs.
1150  * This also handles the restarting of FIEMAP calls in case mapping overflows
1151  * the available number of extents in single call.
1152  *
1153  * \param env [in]              lustre environment
1154  * \param obj [in]              file object
1155  * \param fmkey [in]            fiemap request header and other info
1156  * \param fiemap [out]          fiemap buffer holding retrived map extents
1157  * \param buflen [in/out]       max buffer length of @fiemap, when iterate
1158  *                              each OST, it is used to limit max map needed
1159  * \retval 0    success
1160  * \retval < 0  error
1161  */
1162 static int lov_object_fiemap(const struct lu_env *env, struct cl_object *obj,
1163                              struct ll_fiemap_info_key *fmkey,
1164                              struct fiemap *fiemap, size_t *buflen)
1165 {
1166         struct lov_stripe_md    *lsm;
1167         struct cl_object        *subobj = NULL;
1168         struct lov_obd          *lov = lu2lov_dev(obj->co_lu.lo_dev)->ld_lov;
1169         struct fiemap           *fm_local = NULL;
1170         struct ll_fiemap_extent *lcl_fm_ext;
1171         loff_t                  fm_start;
1172         loff_t                  fm_end;
1173         loff_t                  fm_length;
1174         loff_t                  fm_end_offset;
1175         int                     count_local;
1176         int                     ost_index = 0;
1177         int                     start_stripe;
1178         int                     current_extent = 0;
1179         int                     rc = 0;
1180         int                     last_stripe;
1181         int                     cur_stripe = 0;
1182         int                     cur_stripe_wrap = 0;
1183         int                     stripe_count;
1184         unsigned int            buffer_size = FIEMAP_BUFFER_SIZE;
1185         /* Whether have we collected enough extents */
1186         bool                    enough = false;
1187         /* EOF for object */
1188         bool                    ost_eof = false;
1189         /* done with required mapping for this OST? */
1190         bool                    ost_done = false;
1191         ENTRY;
1192
1193         lsm = lov_lsm_addref(cl2lov(obj));
1194         if (lsm == NULL)
1195                 RETURN(-ENODATA);
1196
1197         /**
1198          * If the stripe_count > 1 and the application does not understand
1199          * DEVICE_ORDER flag, it cannot interpret the extents correctly.
1200          */
1201         if (lsm->lsm_stripe_count > 1 && !(fiemap->fm_flags &
1202                                            FIEMAP_FLAG_DEVICE_ORDER))
1203                 GOTO(out, rc = -ENOTSUPP);
1204
1205         if (lsm_is_released(lsm)) {
1206                 if (fiemap->fm_start < fmkey->oa.o_size) {
1207                         /**
1208                          * released file, return a minimal FIEMAP if
1209                          * request fits in file-size.
1210                          */
1211                         fiemap->fm_mapped_extents = 1;
1212                         fiemap->fm_extents[0].fe_logical = fiemap->fm_start;
1213                         if (fiemap->fm_start + fiemap->fm_length <
1214                             fmkey->oa.o_size)
1215                                 fiemap->fm_extents[0].fe_length =
1216                                         fiemap->fm_length;
1217                         else
1218                                 fiemap->fm_extents[0].fe_length =
1219                                         fmkey->oa.o_size - fiemap->fm_start;
1220                         fiemap->fm_extents[0].fe_flags |=
1221                                 FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_LAST;
1222                 }
1223                 GOTO(out, rc = 0);
1224         }
1225
1226         if (fiemap_count_to_size(fiemap->fm_extent_count) < buffer_size)
1227                 buffer_size = fiemap_count_to_size(fiemap->fm_extent_count);
1228
1229         OBD_ALLOC_LARGE(fm_local, buffer_size);
1230         if (fm_local == NULL)
1231                 GOTO(out, rc = -ENOMEM);
1232         lcl_fm_ext = &fm_local->fm_extents[0];
1233         count_local = fiemap_size_to_count(buffer_size);
1234
1235         fm_start = fiemap->fm_start;
1236         fm_length = fiemap->fm_length;
1237         /* Calculate start stripe, last stripe and length of mapping */
1238         start_stripe = lov_stripe_number(lsm, fm_start);
1239         fm_end = (fm_length == ~0ULL) ? fmkey->oa.o_size :
1240                                         fm_start + fm_length - 1;
1241         /* If fm_length != ~0ULL but fm_start_fm_length-1 exceeds file size */
1242         if (fm_end > fmkey->oa.o_size)
1243                 fm_end = fmkey->oa.o_size;
1244
1245         last_stripe = fiemap_calc_last_stripe(lsm, fm_start, fm_end,
1246                                               start_stripe, &stripe_count);
1247         fm_end_offset = fiemap_calc_fm_end_offset(fiemap, lsm, fm_start, fm_end,
1248                                                   &start_stripe);
1249         if (fm_end_offset == -EINVAL)
1250                 GOTO(out, rc = -EINVAL);
1251
1252         /**
1253          * Requested extent count exceeds the fiemap buffer size, shrink our
1254          * ambition.
1255          */
1256         if (fiemap_count_to_size(fiemap->fm_extent_count) > *buflen)
1257                 fiemap->fm_extent_count = fiemap_size_to_count(*buflen);
1258         if (fiemap->fm_extent_count == 0)
1259                 count_local = 0;
1260
1261         /* Check each stripe */
1262         for (cur_stripe = start_stripe; stripe_count > 0;
1263              --stripe_count,
1264              cur_stripe = (cur_stripe + 1) % lsm->lsm_stripe_count) {
1265                 loff_t req_fm_len; /* Stores length of required mapping */
1266                 loff_t len_mapped_single_call;
1267                 loff_t lun_start;
1268                 loff_t lun_end;
1269                 loff_t obd_object_end;
1270                 unsigned int ext_count;
1271
1272                 cur_stripe_wrap = cur_stripe;
1273
1274                 /* Find out range of mapping on this stripe */
1275                 if ((lov_stripe_intersects(lsm, cur_stripe, fm_start, fm_end,
1276                                            &lun_start, &obd_object_end)) == 0)
1277                         continue;
1278
1279                 if (lov_oinfo_is_dummy(lsm->lsm_oinfo[cur_stripe]))
1280                         GOTO(out, rc = -EIO);
1281
1282                 /* If this is a continuation FIEMAP call and we are on
1283                  * starting stripe then lun_start needs to be set to
1284                  * fm_end_offset */
1285                 if (fm_end_offset != 0 && cur_stripe == start_stripe)
1286                         lun_start = fm_end_offset;
1287
1288                 if (fm_length != ~0ULL) {
1289                         /* Handle fm_start + fm_length overflow */
1290                         if (fm_start + fm_length < fm_start)
1291                                 fm_length = ~0ULL - fm_start;
1292                         lun_end = lov_size_to_stripe(lsm, fm_start + fm_length,
1293                                                      cur_stripe);
1294                 } else {
1295                         lun_end = ~0ULL;
1296                 }
1297
1298                 if (lun_start == lun_end)
1299                         continue;
1300
1301                 req_fm_len = obd_object_end - lun_start;
1302                 fm_local->fm_length = 0;
1303                 len_mapped_single_call = 0;
1304
1305                 /* find lobsub object */
1306                 subobj = lov_find_subobj(env, cl2lov(obj), lsm,
1307                                              cur_stripe);
1308                 if (IS_ERR(subobj))
1309                         GOTO(out, rc = PTR_ERR(subobj));
1310                 /* If the output buffer is very large and the objects have many
1311                  * extents we may need to loop on a single OST repeatedly */
1312                 ost_eof = false;
1313                 ost_done = false;
1314                 do {
1315                         if (fiemap->fm_extent_count > 0) {
1316                                 /* Don't get too many extents. */
1317                                 if (current_extent + count_local >
1318                                     fiemap->fm_extent_count)
1319                                         count_local = fiemap->fm_extent_count -
1320                                                       current_extent;
1321                         }
1322
1323                         lun_start += len_mapped_single_call;
1324                         fm_local->fm_length = req_fm_len -
1325                                               len_mapped_single_call;
1326                         req_fm_len = fm_local->fm_length;
1327                         fm_local->fm_extent_count = enough ? 1 : count_local;
1328                         fm_local->fm_mapped_extents = 0;
1329                         fm_local->fm_flags = fiemap->fm_flags;
1330
1331                         ost_index = lsm->lsm_oinfo[cur_stripe]->loi_ost_idx;
1332
1333                         if (ost_index < 0 ||
1334                             ost_index >= lov->desc.ld_tgt_count)
1335                                 GOTO(obj_put, rc = -EINVAL);
1336                         /* If OST is inactive, return extent with UNKNOWN
1337                          * flag. */
1338                         if (!lov->lov_tgts[ost_index]->ltd_active) {
1339                                 fm_local->fm_flags |= FIEMAP_EXTENT_LAST;
1340                                 fm_local->fm_mapped_extents = 1;
1341
1342                                 lcl_fm_ext[0].fe_logical = lun_start;
1343                                 lcl_fm_ext[0].fe_length = obd_object_end -
1344                                                           lun_start;
1345                                 lcl_fm_ext[0].fe_flags |= FIEMAP_EXTENT_UNKNOWN;
1346
1347                                 goto inactive_tgt;
1348                         }
1349
1350                         fm_local->fm_start = lun_start;
1351                         fm_local->fm_flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
1352                         memcpy(&fmkey->fiemap, fm_local, sizeof(*fm_local));
1353                         *buflen = fiemap_count_to_size(
1354                                                 fm_local->fm_extent_count);
1355
1356                         rc = cl_object_fiemap(env, subobj, fmkey, fm_local,
1357                                               buflen);
1358                         if (rc != 0)
1359                                 GOTO(obj_put, rc);
1360 inactive_tgt:
1361                         ext_count = fm_local->fm_mapped_extents;
1362                         if (ext_count == 0) {
1363                                 ost_done = true;
1364                                 /* If last stripe has hold at the end,
1365                                  * we need to return */
1366                                 if (cur_stripe_wrap == last_stripe) {
1367                                         fiemap->fm_mapped_extents = 0;
1368                                         goto finish;
1369                                 }
1370                                 break;
1371                         } else if (enough) {
1372                                 /*
1373                                  * We've collected enough extents and there are
1374                                  * more extents after it.
1375                                  */
1376                                 goto finish;
1377                         }
1378
1379                         /* If we just need num of extents, got to next device */
1380                         if (fiemap->fm_extent_count == 0) {
1381                                 current_extent += ext_count;
1382                                 break;
1383                         }
1384
1385                         /* prepare to copy retrived map extents */
1386                         len_mapped_single_call =
1387                                 lcl_fm_ext[ext_count - 1].fe_logical -
1388                                 lun_start + lcl_fm_ext[ext_count - 1].fe_length;
1389
1390                         /* Have we finished mapping on this device? */
1391                         if (req_fm_len <= len_mapped_single_call)
1392                                 ost_done = true;
1393
1394                         /* Clear the EXTENT_LAST flag which can be present on
1395                          * the last extent */
1396                         if (lcl_fm_ext[ext_count - 1].fe_flags &
1397                             FIEMAP_EXTENT_LAST)
1398                                 lcl_fm_ext[ext_count - 1].fe_flags &=
1399                                                         ~FIEMAP_EXTENT_LAST;
1400                         if (lov_stripe_size(lsm,
1401                                         lcl_fm_ext[ext_count - 1].fe_logical +
1402                                         lcl_fm_ext[ext_count - 1].fe_length,
1403                                         cur_stripe) >= fmkey->oa.o_size)
1404                                 ost_eof = true;
1405
1406                         fiemap_prepare_and_copy_exts(fiemap, lcl_fm_ext,
1407                                                      ost_index, ext_count,
1408                                                      current_extent);
1409                         current_extent += ext_count;
1410
1411                         /* Ran out of available extents? */
1412                         if (current_extent >= fiemap->fm_extent_count)
1413                                 enough = true;
1414                 } while (!ost_done && !ost_eof);
1415
1416                 cl_object_put(env, subobj);
1417                 subobj = NULL;
1418
1419                 if (cur_stripe_wrap == last_stripe)
1420                         goto finish;
1421         } /* for each stripe */
1422 finish:
1423         /* Indicate that we are returning device offsets unless file just has
1424          * single stripe */
1425         if (lsm->lsm_stripe_count > 1)
1426                 fiemap->fm_flags |= FIEMAP_FLAG_DEVICE_ORDER;
1427
1428         if (fiemap->fm_extent_count == 0)
1429                 goto skip_last_device_calc;
1430
1431         /* Check if we have reached the last stripe and whether mapping for that
1432          * stripe is done. */
1433         if ((cur_stripe_wrap == last_stripe) && (ost_done || ost_eof))
1434                 fiemap->fm_extents[current_extent - 1].fe_flags |=
1435                                                              FIEMAP_EXTENT_LAST;
1436 skip_last_device_calc:
1437         fiemap->fm_mapped_extents = current_extent;
1438 obj_put:
1439         if (subobj != NULL)
1440                 cl_object_put(env, subobj);
1441 out:
1442         if (fm_local != NULL)
1443                 OBD_FREE_LARGE(fm_local, buffer_size);
1444         lov_lsm_put(obj, lsm);
1445         RETURN(rc);
1446 }
1447
1448 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
1449                                 struct lov_user_md __user *lum)
1450 {
1451         struct lov_object       *lov = cl2lov(obj);
1452         struct lov_stripe_md    *lsm;
1453         int                     rc = 0;
1454         ENTRY;
1455
1456         lsm = lov_lsm_addref(lov);
1457         if (lsm == NULL)
1458                 RETURN(-ENODATA);
1459
1460         rc = lov_getstripe(cl2lov(obj), lsm, lum);
1461         lov_lsm_put(obj, lsm);
1462         RETURN(rc);
1463 }
1464
1465 static int lov_object_find_cbdata(const struct lu_env *env,
1466                                   struct cl_object *obj, ldlm_iterator_t iter,
1467                                   void *data)
1468 {
1469         int rc;
1470         ENTRY;
1471
1472         /* call cl_object_find_cbdata for sub obj */
1473         rc = LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_find_cbdata,
1474                                   env, obj, iter, data);
1475         RETURN(rc);
1476 }
1477
1478 static const struct cl_object_operations lov_ops = {
1479         .coo_page_init   = lov_page_init,
1480         .coo_lock_init   = lov_lock_init,
1481         .coo_io_init     = lov_io_init,
1482         .coo_attr_get    = lov_attr_get,
1483         .coo_attr_update = lov_attr_update,
1484         .coo_conf_set    = lov_conf_set,
1485         .coo_getstripe   = lov_object_getstripe,
1486         .coo_find_cbdata = lov_object_find_cbdata,
1487         .coo_fiemap      = lov_object_fiemap,
1488 };
1489
1490 static const struct lu_object_operations lov_lu_obj_ops = {
1491         .loo_object_init      = lov_object_init,
1492         .loo_object_delete    = lov_object_delete,
1493         .loo_object_release   = NULL,
1494         .loo_object_free      = lov_object_free,
1495         .loo_object_print     = lov_object_print,
1496         .loo_object_invariant = NULL
1497 };
1498
1499 struct lu_object *lov_object_alloc(const struct lu_env *env,
1500                                    const struct lu_object_header *unused,
1501                                    struct lu_device *dev)
1502 {
1503         struct lov_object *lov;
1504         struct lu_object  *obj;
1505
1506         ENTRY;
1507         OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
1508         if (lov != NULL) {
1509                 obj = lov2lu(lov);
1510                 lu_object_init(obj, NULL, dev);
1511                 lov->lo_cl.co_ops = &lov_ops;
1512                 lov->lo_type = -1; /* invalid, to catch uninitialized type */
1513                 /*
1514                  * object io operation vector (cl_object::co_iop) is installed
1515                  * later in lov_object_init(), as different vectors are used
1516                  * for object with different layouts.
1517                  */
1518                 obj->lo_ops = &lov_lu_obj_ops;
1519         } else
1520                 obj = NULL;
1521         RETURN(obj);
1522 }
1523
1524 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
1525 {
1526         struct lov_stripe_md *lsm = NULL;
1527
1528         lov_conf_freeze(lov);
1529         if (lov->lo_lsm != NULL) {
1530                 lsm = lsm_addref(lov->lo_lsm);
1531                 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
1532                         lsm, atomic_read(&lsm->lsm_refc),
1533                         lov->lo_layout_invalid, current);
1534         }
1535         lov_conf_thaw(lov);
1536         return lsm;
1537 }
1538
1539 int lov_read_and_clear_async_rc(struct cl_object *clob)
1540 {
1541         struct lu_object *luobj;
1542         int rc = 0;
1543         ENTRY;
1544
1545         luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
1546                                  &lov_device_type);
1547         if (luobj != NULL) {
1548                 struct lov_object *lov = lu2lov(luobj);
1549
1550                 lov_conf_freeze(lov);
1551                 switch (lov->lo_type) {
1552                 case LLT_RAID0: {
1553                         struct lov_stripe_md *lsm;
1554                         int i;
1555
1556                         lsm = lov->lo_lsm;
1557                         LASSERT(lsm != NULL);
1558                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1559                                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1560
1561                                 if (lov_oinfo_is_dummy(loi))
1562                                         continue;
1563
1564                                 if (loi->loi_ar.ar_rc && !rc)
1565                                         rc = loi->loi_ar.ar_rc;
1566                                 loi->loi_ar.ar_rc = 0;
1567                         }
1568                 }
1569                 case LLT_RELEASED:
1570                 case LLT_EMPTY:
1571                         break;
1572                 default:
1573                         LBUG();
1574                 }
1575                 lov_conf_thaw(lov);
1576         }
1577         RETURN(rc);
1578 }
1579 EXPORT_SYMBOL(lov_read_and_clear_async_rc);
1580
1581 /** @} lov */