Whamcloud - gitweb
LU-5823 clio: add cl_object_find_cbdata()
[fs/lustre-release.git] / lustre / lov / lov_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_object for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOV
43
44 #include "lov_cl_internal.h"
45
46 /** \addtogroup lov
47  *  @{
48  */
49
50 /*****************************************************************************
51  *
52  * Layout operations.
53  *
54  */
55
56 struct lov_layout_operations {
57         int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
58                         struct lov_object *lov,
59                         const struct cl_object_conf *conf,
60                         union lov_layout_state *state);
61         int (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
62                            union lov_layout_state *state);
63         void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
64                          union lov_layout_state *state);
65         void (*llo_install)(const struct lu_env *env, struct lov_object *lov,
66                             union lov_layout_state *state);
67         int  (*llo_print)(const struct lu_env *env, void *cookie,
68                           lu_printer_t p, const struct lu_object *o);
69         int  (*llo_page_init)(const struct lu_env *env, struct cl_object *obj,
70                               struct cl_page *page, pgoff_t index);
71         int  (*llo_lock_init)(const struct lu_env *env,
72                               struct cl_object *obj, struct cl_lock *lock,
73                               const struct cl_io *io);
74         int  (*llo_io_init)(const struct lu_env *env,
75                             struct cl_object *obj, struct cl_io *io);
76         int  (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
77                             struct cl_attr *attr);
78         int  (*llo_find_cbdata)(const struct lu_env *env, struct cl_object *obj,
79                                 ldlm_iterator_t iter, void *data);
80 };
81
82 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov);
83
84 struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj)
85 {
86         struct lu_object *luobj;
87         struct lov_stripe_md *lsm = NULL;
88
89         if (clobj == NULL)
90                 return NULL;
91
92         luobj = lu_object_locate(&cl_object_header(clobj)->coh_lu,
93                                  &lov_device_type);
94         if (luobj != NULL)
95                 lsm = lov_lsm_addref(lu2lov(luobj));
96         return lsm;
97 }
98 EXPORT_SYMBOL(lov_lsm_get);
99
100 void lov_lsm_put(struct cl_object *unused, struct lov_stripe_md *lsm)
101 {
102         if (lsm != NULL)
103                 lov_free_memmd(&lsm);
104 }
105 EXPORT_SYMBOL(lov_lsm_put);
106
107 /*****************************************************************************
108  *
109  * Lov object layout operations.
110  *
111  */
112
113 static void lov_install_empty(const struct lu_env *env,
114                               struct lov_object *lov,
115                               union  lov_layout_state *state)
116 {
117         /*
118          * File without objects.
119          */
120 }
121
122 static int lov_init_empty(const struct lu_env *env,
123                           struct lov_device *dev, struct lov_object *lov,
124                           const struct cl_object_conf *conf,
125                           union  lov_layout_state *state)
126 {
127         return 0;
128 }
129
130 static void lov_install_raid0(const struct lu_env *env,
131                               struct lov_object *lov,
132                               union  lov_layout_state *state)
133 {
134 }
135
136 static struct cl_object *lov_sub_find(const struct lu_env *env,
137                                       struct cl_device *dev,
138                                       const struct lu_fid *fid,
139                                       const struct cl_object_conf *conf)
140 {
141         struct lu_object *o;
142
143         ENTRY;
144         o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
145         LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
146         RETURN(lu2cl(o));
147 }
148
149 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
150                         struct cl_object *stripe, struct lov_layout_raid0 *r0,
151                         int idx)
152 {
153         struct cl_object_header *hdr;
154         struct cl_object_header *subhdr;
155         struct cl_object_header *parent;
156         struct lov_oinfo        *oinfo;
157         int result;
158
159         if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) {
160                 /* For sanity:test_206.
161                  * Do not leave the object in cache to avoid accessing
162                  * freed memory. This is because osc_object is referring to
163                  * lov_oinfo of lsm_stripe_data which will be freed due to
164                  * this failure. */
165                 cl_object_kill(env, stripe);
166                 cl_object_put(env, stripe);
167                 return -EIO;
168         }
169
170         hdr    = cl_object_header(lov2cl(lov));
171         subhdr = cl_object_header(stripe);
172
173         oinfo = lov->lo_lsm->lsm_oinfo[idx];
174         CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: ostid: "DOSTID
175                " idx: %d gen: %d\n",
176                PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
177                PFID(&hdr->coh_lu.loh_fid), hdr, POSTID(&oinfo->loi_oi),
178                oinfo->loi_ost_idx, oinfo->loi_ost_gen);
179
180         /* reuse ->coh_attr_guard to protect coh_parent change */
181         spin_lock(&subhdr->coh_attr_guard);
182         parent = subhdr->coh_parent;
183         if (parent == NULL) {
184                 subhdr->coh_parent = hdr;
185                 spin_unlock(&subhdr->coh_attr_guard);
186                 subhdr->coh_nesting = hdr->coh_nesting + 1;
187                 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
188                 r0->lo_sub[idx] = cl2lovsub(stripe);
189                 r0->lo_sub[idx]->lso_super = lov;
190                 r0->lo_sub[idx]->lso_index = idx;
191                 result = 0;
192         } else {
193                 struct lu_object  *old_obj;
194                 struct lov_object *old_lov;
195                 unsigned int mask = D_INODE;
196
197                 spin_unlock(&subhdr->coh_attr_guard);
198                 old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type);
199                 LASSERT(old_obj != NULL);
200                 old_lov = cl2lov(lu2cl(old_obj));
201                 if (old_lov->lo_layout_invalid) {
202                         /* the object's layout has already changed but isn't
203                          * refreshed */
204                         lu_object_unhash(env, &stripe->co_lu);
205                         result = -EAGAIN;
206                 } else {
207                         mask = D_ERROR;
208                         result = -EIO;
209                 }
210
211                 LU_OBJECT_DEBUG(mask, env, &stripe->co_lu,
212                                 "stripe %d is already owned.\n", idx);
213                 LU_OBJECT_DEBUG(mask, env, old_obj, "owned.\n");
214                 LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n");
215                 cl_object_put(env, stripe);
216         }
217         return result;
218 }
219
220 static int lov_page_slice_fixup(struct lov_object *lov,
221                                 struct cl_object *stripe)
222 {
223         struct cl_object_header *hdr = cl_object_header(&lov->lo_cl);
224         struct cl_object *o;
225
226         if (stripe == NULL)
227                 return hdr->coh_page_bufsize - lov->lo_cl.co_slice_off -
228                        cfs_size_round(sizeof(struct lov_page));
229
230         cl_object_for_each(o, stripe)
231                 o->co_slice_off += hdr->coh_page_bufsize;
232
233         return cl_object_header(stripe)->coh_page_bufsize;
234 }
235
236 static int lov_init_raid0(const struct lu_env *env,
237                           struct lov_device *dev, struct lov_object *lov,
238                           const struct cl_object_conf *conf,
239                           union  lov_layout_state *state)
240 {
241         int result;
242         int i;
243
244         struct cl_object        *stripe;
245         struct lov_thread_info  *lti     = lov_env_info(env);
246         struct cl_object_conf   *subconf = &lti->lti_stripe_conf;
247         struct lov_stripe_md    *lsm     = conf->u.coc_md->lsm;
248         struct lu_fid           *ofid    = &lti->lti_fid;
249         struct lov_layout_raid0 *r0      = &state->raid0;
250
251         ENTRY;
252
253         if (lsm->lsm_magic != LOV_MAGIC_V1 && lsm->lsm_magic != LOV_MAGIC_V3) {
254                 dump_lsm(D_ERROR, lsm);
255                 LASSERTF(0, "magic mismatch, expected %d/%d, actual %d.\n",
256                          LOV_MAGIC_V1, LOV_MAGIC_V3, lsm->lsm_magic);
257         }
258
259         LASSERT(lov->lo_lsm == NULL);
260         lov->lo_lsm = lsm_addref(lsm);
261         r0->lo_nr  = lsm->lsm_stripe_count;
262         LASSERT(r0->lo_nr <= lov_targets_nr(dev));
263
264         lov->lo_layout_invalid = true;
265
266         OBD_ALLOC_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
267         if (r0->lo_sub != NULL) {
268                 int psz = 0;
269
270                 result = 0;
271                 subconf->coc_inode = conf->coc_inode;
272                 spin_lock_init(&r0->lo_sub_lock);
273                 /*
274                  * Create stripe cl_objects.
275                  */
276                 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
277                         struct cl_device *subdev;
278                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
279                         int ost_idx = oinfo->loi_ost_idx;
280
281                         if (lov_oinfo_is_dummy(oinfo))
282                                 continue;
283
284                         result = ostid_to_fid(ofid, &oinfo->loi_oi,
285                                               oinfo->loi_ost_idx);
286                         if (result != 0)
287                                 GOTO(out, result);
288
289                         subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
290                         subconf->u.coc_oinfo = oinfo;
291                         LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
292                         /* In the function below, .hs_keycmp resolves to
293                          * lu_obj_hop_keycmp() */
294                         /* coverity[overrun-buffer-val] */
295                         stripe = lov_sub_find(env, subdev, ofid, subconf);
296                         if (!IS_ERR(stripe)) {
297                                 result = lov_init_sub(env, lov, stripe, r0, i);
298                                 if (result == -EAGAIN) { /* try again */
299                                         --i;
300                                         result = 0;
301                                         continue;
302                                 }
303                         } else {
304                                 result = PTR_ERR(stripe);
305                         }
306
307                         if (result == 0) {
308                                 int sz = lov_page_slice_fixup(lov, stripe);
309                                 LASSERT(ergo(psz > 0, psz == sz));
310                                 psz = sz;
311                         }
312                 }
313                 if (result == 0)
314                         cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz;
315         } else
316                 result = -ENOMEM;
317 out:
318         RETURN(result);
319 }
320
321 static int lov_init_released(const struct lu_env *env,
322                         struct lov_device *dev, struct lov_object *lov,
323                         const struct cl_object_conf *conf,
324                         union  lov_layout_state *state)
325 {
326         struct lov_stripe_md *lsm = conf->u.coc_md->lsm;
327
328         LASSERT(lsm != NULL);
329         LASSERT(lsm_is_released(lsm));
330         LASSERT(lov->lo_lsm == NULL);
331
332         lov->lo_lsm = lsm_addref(lsm);
333         return 0;
334 }
335
336 static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
337                             union lov_layout_state *state)
338 {
339         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
340
341         lov_layout_wait(env, lov);
342         return 0;
343 }
344
345 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
346                                struct lovsub_object *los, int idx)
347 {
348         struct cl_object        *sub;
349         struct lov_layout_raid0 *r0;
350         struct lu_site          *site;
351         struct lu_site_bkt_data *bkt;
352         wait_queue_t          *waiter;
353
354         r0  = &lov->u.raid0;
355         LASSERT(r0->lo_sub[idx] == los);
356
357         sub  = lovsub2cl(los);
358         site = sub->co_lu.lo_dev->ld_site;
359         bkt  = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid);
360
361         cl_object_kill(env, sub);
362         /* release a reference to the sub-object and ... */
363         lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
364         cl_object_put(env, sub);
365
366         /* ... wait until it is actually destroyed---sub-object clears its
367          * ->lo_sub[] slot in lovsub_object_fini() */
368         if (r0->lo_sub[idx] == los) {
369                 waiter = &lov_env_info(env)->lti_waiter;
370                 init_waitqueue_entry_current(waiter);
371                 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
372                 set_current_state(TASK_UNINTERRUPTIBLE);
373                 while (1) {
374                         /* this wait-queue is signaled at the end of
375                          * lu_object_free(). */
376                         set_current_state(TASK_UNINTERRUPTIBLE);
377                         spin_lock(&r0->lo_sub_lock);
378                         if (r0->lo_sub[idx] == los) {
379                                 spin_unlock(&r0->lo_sub_lock);
380                                 waitq_wait(waiter, TASK_UNINTERRUPTIBLE);
381                         } else {
382                                 spin_unlock(&r0->lo_sub_lock);
383                                 set_current_state(TASK_RUNNING);
384                                 break;
385                         }
386                 }
387                 remove_wait_queue(&bkt->lsb_marche_funebre, waiter);
388         }
389         LASSERT(r0->lo_sub[idx] == NULL);
390 }
391
392 static int lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
393                             union lov_layout_state *state)
394 {
395         struct lov_layout_raid0 *r0 = &state->raid0;
396         struct lov_stripe_md    *lsm = lov->lo_lsm;
397         int i;
398
399         ENTRY;
400
401         dump_lsm(D_INODE, lsm);
402
403         lov_layout_wait(env, lov);
404         if (r0->lo_sub != NULL) {
405                 for (i = 0; i < r0->lo_nr; ++i) {
406                         struct lovsub_object *los = r0->lo_sub[i];
407
408                         if (los != NULL) {
409                                 cl_object_prune(env, &los->lso_cl);
410                                 /*
411                                  * If top-level object is to be evicted from
412                                  * the cache, so are its sub-objects.
413                                  */
414                                 lov_subobject_kill(env, lov, los, i);
415                         }
416                 }
417         }
418         RETURN(0);
419 }
420
421 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
422                            union lov_layout_state *state)
423 {
424         LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED);
425 }
426
427 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
428                            union lov_layout_state *state)
429 {
430         struct lov_layout_raid0 *r0 = &state->raid0;
431         ENTRY;
432
433         if (r0->lo_sub != NULL) {
434                 OBD_FREE_LARGE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
435                 r0->lo_sub = NULL;
436         }
437
438         dump_lsm(D_INODE, lov->lo_lsm);
439         lov_free_memmd(&lov->lo_lsm);
440
441         EXIT;
442 }
443
444 static void lov_fini_released(const struct lu_env *env, struct lov_object *lov,
445                                 union lov_layout_state *state)
446 {
447         ENTRY;
448         dump_lsm(D_INODE, lov->lo_lsm);
449         lov_free_memmd(&lov->lo_lsm);
450         EXIT;
451 }
452
453 static int lov_print_empty(const struct lu_env *env, void *cookie,
454                            lu_printer_t p, const struct lu_object *o)
455 {
456         (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid);
457         return 0;
458 }
459
460 static int lov_print_raid0(const struct lu_env *env, void *cookie,
461                            lu_printer_t p, const struct lu_object *o)
462 {
463         struct lov_object       *lov = lu2lov(o);
464         struct lov_layout_raid0 *r0  = lov_r0(lov);
465         struct lov_stripe_md    *lsm = lov->lo_lsm;
466         int                      i;
467
468         (*p)(env, cookie, "stripes: %d, %s, lsm{%p 0x%08X %d %u %u}:\n",
469                 r0->lo_nr, lov->lo_layout_invalid ? "invalid" : "valid", lsm,
470                 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
471                 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
472         for (i = 0; i < r0->lo_nr; ++i) {
473                 struct lu_object *sub;
474
475                 if (r0->lo_sub[i] != NULL) {
476                         sub = lovsub2lu(r0->lo_sub[i]);
477                         lu_object_print(env, cookie, p, sub);
478                 } else {
479                         (*p)(env, cookie, "sub %d absent\n", i);
480                 }
481         }
482         return 0;
483 }
484
485 static int lov_print_released(const struct lu_env *env, void *cookie,
486                                 lu_printer_t p, const struct lu_object *o)
487 {
488         struct lov_object       *lov = lu2lov(o);
489         struct lov_stripe_md    *lsm = lov->lo_lsm;
490
491         (*p)(env, cookie,
492                 "released: %s, lsm{%p 0x%08X %d %u %u}:\n",
493                 lov->lo_layout_invalid ? "invalid" : "valid", lsm,
494                 lsm->lsm_magic, atomic_read(&lsm->lsm_refc),
495                 lsm->lsm_stripe_count, lsm->lsm_layout_gen);
496         return 0;
497 }
498
499 /**
500  * Implements cl_object_operations::coo_attr_get() method for an object
501  * without stripes (LLT_EMPTY layout type).
502  *
503  * The only attributes this layer is authoritative in this case is
504  * cl_attr::cat_blocks---it's 0.
505  */
506 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
507                               struct cl_attr *attr)
508 {
509         attr->cat_blocks = 0;
510         return 0;
511 }
512
513 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
514                               struct cl_attr *attr)
515 {
516         struct lov_object       *lov = cl2lov(obj);
517         struct lov_layout_raid0 *r0 = lov_r0(lov);
518         struct cl_attr          *lov_attr = &r0->lo_attr;
519         int                      result = 0;
520
521         ENTRY;
522
523         /* this is called w/o holding type guard mutex, so it must be inside
524          * an on going IO otherwise lsm may be replaced.
525          * LU-2117: it turns out there exists one exception. For mmaped files,
526          * the lock of those files may be requested in the other file's IO
527          * context, and this function is called in ccc_lock_state(), it will
528          * hit this assertion.
529          * Anyway, it's still okay to call attr_get w/o type guard as layout
530          * can't go if locks exist. */
531         /* LASSERT(atomic_read(&lsm->lsm_refc) > 1); */
532
533         if (!r0->lo_attr_valid) {
534                 struct lov_stripe_md    *lsm = lov->lo_lsm;
535                 struct ost_lvb          *lvb = &lov_env_info(env)->lti_lvb;
536                 __u64                    kms = 0;
537
538                 memset(lvb, 0, sizeof(*lvb));
539                 /* XXX: timestamps can be negative by sanity:test_39m,
540                  * how can it be? */
541                 lvb->lvb_atime = LLONG_MIN;
542                 lvb->lvb_ctime = LLONG_MIN;
543                 lvb->lvb_mtime = LLONG_MIN;
544
545                 /*
546                  * XXX that should be replaced with a loop over sub-objects,
547                  * doing cl_object_attr_get() on them. But for now, let's
548                  * reuse old lov code.
549                  */
550
551                 /*
552                  * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
553                  * happy. It's not needed, because new code uses
554                  * ->coh_attr_guard spin-lock to protect consistency of
555                  * sub-object attributes.
556                  */
557                 lov_stripe_lock(lsm);
558                 result = lov_merge_lvb_kms(lsm, lvb, &kms);
559                 lov_stripe_unlock(lsm);
560                 if (result == 0) {
561                         cl_lvb2attr(lov_attr, lvb);
562                         lov_attr->cat_kms = kms;
563                         r0->lo_attr_valid = 1;
564                 }
565         }
566         if (result == 0) { /* merge results */
567                 attr->cat_blocks = lov_attr->cat_blocks;
568                 attr->cat_size = lov_attr->cat_size;
569                 attr->cat_kms = lov_attr->cat_kms;
570                 if (attr->cat_atime < lov_attr->cat_atime)
571                         attr->cat_atime = lov_attr->cat_atime;
572                 if (attr->cat_ctime < lov_attr->cat_ctime)
573                         attr->cat_ctime = lov_attr->cat_ctime;
574                 if (attr->cat_mtime < lov_attr->cat_mtime)
575                         attr->cat_mtime = lov_attr->cat_mtime;
576         }
577         RETURN(result);
578 }
579
580 static int lov_find_cbdata_empty(const struct lu_env *env,
581                                  struct cl_object *obj, ldlm_iterator_t iter,
582                                  void *data)
583 {
584         return 0;
585 }
586
587 static int lov_find_cbdata_raid0(const struct lu_env *env,
588                                  struct cl_object *obj, ldlm_iterator_t iter,
589                                  void *data)
590 {
591         struct lov_object       *lov = cl2lov(obj);
592         struct lov_layout_raid0 *r0 = lov_r0(lov);
593         struct cl_object        *subobj;
594         int                     i;
595         int                     rc = 0;
596
597         for (i = 0; i < r0->lo_nr; ++i) {
598                 if (r0->lo_sub[i] == NULL)
599                         continue;
600
601                 subobj = lovsub2cl(r0->lo_sub[i]);
602
603                 rc = cl_object_find_cbdata(env, subobj, iter, data);
604                 if (rc != 0)
605                         break;
606         }
607
608         return rc;
609 }
610
611 const static struct lov_layout_operations lov_dispatch[] = {
612         [LLT_EMPTY] = {
613                 .llo_init      = lov_init_empty,
614                 .llo_delete    = lov_delete_empty,
615                 .llo_fini      = lov_fini_empty,
616                 .llo_install   = lov_install_empty,
617                 .llo_print     = lov_print_empty,
618                 .llo_page_init = lov_page_init_empty,
619                 .llo_lock_init = lov_lock_init_empty,
620                 .llo_io_init   = lov_io_init_empty,
621                 .llo_getattr   = lov_attr_get_empty,
622                 .llo_find_cbdata = lov_find_cbdata_empty
623         },
624         [LLT_RAID0] = {
625                 .llo_init      = lov_init_raid0,
626                 .llo_delete    = lov_delete_raid0,
627                 .llo_fini      = lov_fini_raid0,
628                 .llo_install   = lov_install_raid0,
629                 .llo_print     = lov_print_raid0,
630                 .llo_page_init = lov_page_init_raid0,
631                 .llo_lock_init = lov_lock_init_raid0,
632                 .llo_io_init   = lov_io_init_raid0,
633                 .llo_getattr   = lov_attr_get_raid0,
634                 .llo_find_cbdata = lov_find_cbdata_raid0
635         },
636         [LLT_RELEASED] = {
637                 .llo_init      = lov_init_released,
638                 .llo_delete    = lov_delete_empty,
639                 .llo_fini      = lov_fini_released,
640                 .llo_install   = lov_install_empty,
641                 .llo_print     = lov_print_released,
642                 .llo_page_init = lov_page_init_empty,
643                 .llo_lock_init = lov_lock_init_empty,
644                 .llo_io_init   = lov_io_init_released,
645                 .llo_getattr   = lov_attr_get_empty,
646                 .llo_find_cbdata = lov_find_cbdata_empty
647         }
648 };
649
650 /**
651  * Performs a double-dispatch based on the layout type of an object.
652  */
653 #define LOV_2DISPATCH_NOLOCK(obj, op, ...)                              \
654 ({                                                                      \
655         struct lov_object                      *__obj = (obj);          \
656         enum lov_layout_type                    __llt;                  \
657                                                                         \
658         __llt = __obj->lo_type;                                         \
659         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
660         lov_dispatch[__llt].op(__VA_ARGS__);                            \
661 })
662
663 /**
664  * Return lov_layout_type associated with a given lsm
665  */
666 static enum lov_layout_type lov_type(struct lov_stripe_md *lsm)
667 {
668         if (lsm == NULL)
669                 return LLT_EMPTY;
670         if (lsm_is_released(lsm))
671                 return LLT_RELEASED;
672         return LLT_RAID0;
673 }
674
675 static inline void lov_conf_freeze(struct lov_object *lov)
676 {
677         if (lov->lo_owner != current)
678                 down_read(&lov->lo_type_guard);
679 }
680
681 static inline void lov_conf_thaw(struct lov_object *lov)
682 {
683         if (lov->lo_owner != current)
684                 up_read(&lov->lo_type_guard);
685 }
686
687 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...)                       \
688 ({                                                                      \
689         struct lov_object                      *__obj = (obj);          \
690         int                                     __lock = !!(lock);      \
691         typeof(lov_dispatch[0].op(__VA_ARGS__)) __result;               \
692                                                                         \
693         if (__lock)                                                     \
694                 lov_conf_freeze(__obj);                                 \
695         __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__);          \
696         if (__lock)                                                     \
697                 lov_conf_thaw(__obj);                                   \
698         __result;                                                       \
699 })
700
701 /**
702  * Performs a locked double-dispatch based on the layout type of an object.
703  */
704 #define LOV_2DISPATCH(obj, op, ...)                     \
705         LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
706
707 #define LOV_2DISPATCH_VOID(obj, op, ...)                                \
708 do {                                                                    \
709         struct lov_object                      *__obj = (obj);          \
710         enum lov_layout_type                    __llt;                  \
711                                                                         \
712         lov_conf_freeze(__obj);                                         \
713         __llt = __obj->lo_type;                                         \
714         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
715         lov_dispatch[__llt].op(__VA_ARGS__);                            \
716         lov_conf_thaw(__obj);                                           \
717 } while (0)
718
719 static void lov_conf_lock(struct lov_object *lov)
720 {
721         LASSERT(lov->lo_owner != current);
722         down_write(&lov->lo_type_guard);
723         LASSERT(lov->lo_owner == NULL);
724         lov->lo_owner = current;
725 }
726
727 static void lov_conf_unlock(struct lov_object *lov)
728 {
729         lov->lo_owner = NULL;
730         up_write(&lov->lo_type_guard);
731 }
732
733 static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov)
734 {
735         struct l_wait_info lwi = { 0 };
736         ENTRY;
737
738         while (atomic_read(&lov->lo_active_ios) > 0) {
739                 CDEBUG(D_INODE, "file:"DFID" wait for active IO, now: %d.\n",
740                         PFID(lu_object_fid(lov2lu(lov))),
741                         atomic_read(&lov->lo_active_ios));
742
743                 l_wait_event(lov->lo_waitq,
744                              atomic_read(&lov->lo_active_ios) == 0, &lwi);
745         }
746         RETURN(0);
747 }
748
749 static int lov_layout_change(const struct lu_env *unused,
750                              struct lov_object *lov,
751                              const struct cl_object_conf *conf)
752 {
753         int result;
754         enum lov_layout_type llt = LLT_EMPTY;
755         union lov_layout_state *state = &lov->u;
756         const struct lov_layout_operations *old_ops;
757         const struct lov_layout_operations *new_ops;
758
759         void *cookie;
760         struct lu_env *env;
761         int refcheck;
762         ENTRY;
763
764         LASSERT(0 <= lov->lo_type && lov->lo_type < ARRAY_SIZE(lov_dispatch));
765
766         if (conf->u.coc_md != NULL)
767                 llt = lov_type(conf->u.coc_md->lsm);
768         LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
769
770         cookie = cl_env_reenter();
771         env = cl_env_get(&refcheck);
772         if (IS_ERR(env)) {
773                 cl_env_reexit(cookie);
774                 RETURN(PTR_ERR(env));
775         }
776
777         CDEBUG(D_INODE, DFID" from %s to %s\n",
778                PFID(lu_object_fid(lov2lu(lov))),
779                llt2str(lov->lo_type), llt2str(llt));
780
781         old_ops = &lov_dispatch[lov->lo_type];
782         new_ops = &lov_dispatch[llt];
783
784         result = cl_object_prune(env, &lov->lo_cl);
785         if (result != 0)
786                 GOTO(out, result);
787
788         result = old_ops->llo_delete(env, lov, &lov->u);
789         if (result == 0) {
790                 old_ops->llo_fini(env, lov, &lov->u);
791
792                 LASSERT(atomic_read(&lov->lo_active_ios) == 0);
793
794                 lov->lo_type = LLT_EMPTY;
795                 /* page bufsize fixup */
796                 cl_object_header(&lov->lo_cl)->coh_page_bufsize -=
797                         lov_page_slice_fixup(lov, NULL);
798
799                 result = new_ops->llo_init(env,
800                                         lu2lov_dev(lov->lo_cl.co_lu.lo_dev),
801                                         lov, conf, state);
802                 if (result == 0) {
803                         new_ops->llo_install(env, lov, state);
804                         lov->lo_type = llt;
805                 } else {
806                         new_ops->llo_delete(env, lov, state);
807                         new_ops->llo_fini(env, lov, state);
808                         /* this file becomes an EMPTY file. */
809                 }
810         }
811
812 out:
813         cl_env_put(env, &refcheck);
814         cl_env_reexit(cookie);
815         RETURN(result);
816 }
817
818 /*****************************************************************************
819  *
820  * Lov object operations.
821  *
822  */
823 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
824                     const struct lu_object_conf *conf)
825 {
826         struct lov_device            *dev   = lu2lov_dev(obj->lo_dev);
827         struct lov_object            *lov   = lu2lov(obj);
828         const struct cl_object_conf  *cconf = lu2cl_conf(conf);
829         union  lov_layout_state      *set   = &lov->u;
830         const struct lov_layout_operations *ops;
831         int result;
832
833         ENTRY;
834         init_rwsem(&lov->lo_type_guard);
835         atomic_set(&lov->lo_active_ios, 0);
836         init_waitqueue_head(&lov->lo_waitq);
837
838         cl_object_page_init(lu2cl(obj), sizeof(struct lov_page));
839
840         /* no locking is necessary, as object is being created */
841         lov->lo_type = lov_type(cconf->u.coc_md->lsm);
842         ops = &lov_dispatch[lov->lo_type];
843         result = ops->llo_init(env, dev, lov, cconf, set);
844         if (result == 0)
845                 ops->llo_install(env, lov, set);
846         RETURN(result);
847 }
848
849 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
850                         const struct cl_object_conf *conf)
851 {
852         struct lov_stripe_md    *lsm = NULL;
853         struct lov_object       *lov = cl2lov(obj);
854         int                      result = 0;
855         ENTRY;
856
857         lov_conf_lock(lov);
858         if (conf->coc_opc == OBJECT_CONF_INVALIDATE) {
859                 lov->lo_layout_invalid = true;
860                 GOTO(out, result = 0);
861         }
862
863         if (conf->coc_opc == OBJECT_CONF_WAIT) {
864                 if (lov->lo_layout_invalid &&
865                     atomic_read(&lov->lo_active_ios) > 0) {
866                         lov_conf_unlock(lov);
867                         result = lov_layout_wait(env, lov);
868                         lov_conf_lock(lov);
869                 }
870                 GOTO(out, result);
871         }
872
873         LASSERT(conf->coc_opc == OBJECT_CONF_SET);
874
875         if (conf->u.coc_md != NULL)
876                 lsm = conf->u.coc_md->lsm;
877         if ((lsm == NULL && lov->lo_lsm == NULL) ||
878             ((lsm != NULL && lov->lo_lsm != NULL) &&
879              (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) &&
880              (lov->lo_lsm->lsm_pattern == lsm->lsm_pattern))) {
881                 /* same version of layout */
882                 lov->lo_layout_invalid = false;
883                 GOTO(out, result = 0);
884         }
885
886         /* will change layout - check if there still exists active IO. */
887         if (atomic_read(&lov->lo_active_ios) > 0) {
888                 lov->lo_layout_invalid = true;
889                 GOTO(out, result = -EBUSY);
890         }
891
892         result = lov_layout_change(env, lov, conf);
893         lov->lo_layout_invalid = result != 0;
894         EXIT;
895
896 out:
897         lov_conf_unlock(lov);
898         CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n",
899                PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid);
900         RETURN(result);
901 }
902
903 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
904 {
905         struct lov_object *lov = lu2lov(obj);
906
907         ENTRY;
908         LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
909         EXIT;
910 }
911
912 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
913 {
914         struct lov_object *lov = lu2lov(obj);
915
916         ENTRY;
917         LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
918         lu_object_fini(obj);
919         OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
920         EXIT;
921 }
922
923 static int lov_object_print(const struct lu_env *env, void *cookie,
924                             lu_printer_t p, const struct lu_object *o)
925 {
926         return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o);
927 }
928
929 int lov_page_init(const struct lu_env *env, struct cl_object *obj,
930                   struct cl_page *page, pgoff_t index)
931 {
932         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page,
933                                     index);
934 }
935
936 /**
937  * Implements cl_object_operations::clo_io_init() method for lov
938  * layer. Dispatches to the appropriate layout io initialization method.
939  */
940 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
941                 struct cl_io *io)
942 {
943         CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
944         return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
945                                      !io->ci_ignore_layout, env, obj, io);
946 }
947
948 /**
949  * An implementation of cl_object_operations::clo_attr_get() method for lov
950  * layer. For raid0 layout this collects and merges attributes of all
951  * sub-objects.
952  */
953 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
954                         struct cl_attr *attr)
955 {
956         /* do not take lock, as this function is called under a
957          * spin-lock. Layout is protected from changing by ongoing IO. */
958         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
959 }
960
961 static int lov_attr_update(const struct lu_env *env, struct cl_object *obj,
962                            const struct cl_attr *attr, unsigned valid)
963 {
964         /*
965          * No dispatch is required here, as no layout implements this.
966          */
967         return 0;
968 }
969
970 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
971                   struct cl_lock *lock, const struct cl_io *io)
972 {
973         /* No need to lock because we've taken one refcount of layout.  */
974         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock,
975                                     io);
976 }
977
978 static int lov_object_getstripe(const struct lu_env *env, struct cl_object *obj,
979                                 struct lov_user_md __user *lum)
980 {
981         struct lov_object       *lov = cl2lov(obj);
982         struct lov_stripe_md    *lsm;
983         int                     rc = 0;
984         ENTRY;
985
986         lsm = lov_lsm_addref(lov);
987         if (lsm == NULL)
988                 RETURN(-ENODATA);
989
990         rc = lov_getstripe(cl2lov(obj), lsm, lum);
991         lov_lsm_put(obj, lsm);
992         RETURN(rc);
993 }
994
995 static int lov_object_find_cbdata(const struct lu_env *env,
996                                   struct cl_object *obj, ldlm_iterator_t iter,
997                                   void *data)
998 {
999         int rc;
1000         ENTRY;
1001
1002         /* call cl_object_find_cbdata for sub obj */
1003         rc = LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_find_cbdata,
1004                                   env, obj, iter, data);
1005         RETURN(rc);
1006 }
1007
1008 static const struct cl_object_operations lov_ops = {
1009         .coo_page_init    = lov_page_init,
1010         .coo_lock_init    = lov_lock_init,
1011         .coo_io_init      = lov_io_init,
1012         .coo_attr_get     = lov_attr_get,
1013         .coo_attr_update  = lov_attr_update,
1014         .coo_conf_set     = lov_conf_set,
1015         .coo_getstripe    = lov_object_getstripe,
1016         .coo_find_cbdata  = lov_object_find_cbdata
1017 };
1018
1019 static const struct lu_object_operations lov_lu_obj_ops = {
1020         .loo_object_init      = lov_object_init,
1021         .loo_object_delete    = lov_object_delete,
1022         .loo_object_release   = NULL,
1023         .loo_object_free      = lov_object_free,
1024         .loo_object_print     = lov_object_print,
1025         .loo_object_invariant = NULL
1026 };
1027
1028 struct lu_object *lov_object_alloc(const struct lu_env *env,
1029                                    const struct lu_object_header *unused,
1030                                    struct lu_device *dev)
1031 {
1032         struct lov_object *lov;
1033         struct lu_object  *obj;
1034
1035         ENTRY;
1036         OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, GFP_NOFS);
1037         if (lov != NULL) {
1038                 obj = lov2lu(lov);
1039                 lu_object_init(obj, NULL, dev);
1040                 lov->lo_cl.co_ops = &lov_ops;
1041                 lov->lo_type = -1; /* invalid, to catch uninitialized type */
1042                 /*
1043                  * object io operation vector (cl_object::co_iop) is installed
1044                  * later in lov_object_init(), as different vectors are used
1045                  * for object with different layouts.
1046                  */
1047                 obj->lo_ops = &lov_lu_obj_ops;
1048         } else
1049                 obj = NULL;
1050         RETURN(obj);
1051 }
1052
1053 struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov)
1054 {
1055         struct lov_stripe_md *lsm = NULL;
1056
1057         lov_conf_freeze(lov);
1058         if (lov->lo_lsm != NULL) {
1059                 lsm = lsm_addref(lov->lo_lsm);
1060                 CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n",
1061                         lsm, atomic_read(&lsm->lsm_refc),
1062                         lov->lo_layout_invalid, current);
1063         }
1064         lov_conf_thaw(lov);
1065         return lsm;
1066 }
1067
1068 int lov_read_and_clear_async_rc(struct cl_object *clob)
1069 {
1070         struct lu_object *luobj;
1071         int rc = 0;
1072         ENTRY;
1073
1074         luobj = lu_object_locate(&cl_object_header(clob)->coh_lu,
1075                                  &lov_device_type);
1076         if (luobj != NULL) {
1077                 struct lov_object *lov = lu2lov(luobj);
1078
1079                 lov_conf_freeze(lov);
1080                 switch (lov->lo_type) {
1081                 case LLT_RAID0: {
1082                         struct lov_stripe_md *lsm;
1083                         int i;
1084
1085                         lsm = lov->lo_lsm;
1086                         LASSERT(lsm != NULL);
1087                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1088                                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
1089
1090                                 if (lov_oinfo_is_dummy(loi))
1091                                         continue;
1092
1093                                 if (loi->loi_ar.ar_rc && !rc)
1094                                         rc = loi->loi_ar.ar_rc;
1095                                 loi->loi_ar.ar_rc = 0;
1096                         }
1097                 }
1098                 case LLT_RELEASED:
1099                 case LLT_EMPTY:
1100                         break;
1101                 default:
1102                         LBUG();
1103                 }
1104                 lov_conf_thaw(lov);
1105         }
1106         RETURN(rc);
1107 }
1108 EXPORT_SYMBOL(lov_read_and_clear_async_rc);
1109
1110 /** @} lov */