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