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