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