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