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