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