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