Whamcloud - gitweb
Revert "b=19427 correct lmm_object_id and reserve fids for fid-on-OST."
[fs/lustre-release.git] / lustre / lov / lov_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42
43 #include "lov_cl_internal.h"
44
45 /** \addtogroup lov
46  *  @{
47  */
48
49 /*****************************************************************************
50  *
51  * Layout operations.
52  *
53  */
54
55 struct lov_layout_operations {
56         int (*llo_init)(const struct lu_env *env, struct lov_device *dev,
57                         struct lov_object *lov,
58                         const struct cl_object_conf *conf,
59                         union lov_layout_state *state);
60         void (*llo_delete)(const struct lu_env *env, struct lov_object *lov,
61                            union lov_layout_state *state);
62         void (*llo_fini)(const struct lu_env *env, struct lov_object *lov,
63                          union lov_layout_state *state);
64         void (*llo_install)(const struct lu_env *env, struct lov_object *lov,
65                             union lov_layout_state *state);
66         int  (*llo_print)(const struct lu_env *env, void *cookie,
67                           lu_printer_t p, const struct lu_object *o);
68         struct cl_page *(*llo_page_init)(const struct lu_env *env,
69                                          struct cl_object *obj,
70                                          struct cl_page *page,
71                                          cfs_page_t *vmpage);
72         int  (*llo_lock_init)(const struct lu_env *env,
73                               struct cl_object *obj, struct cl_lock *lock,
74                               const struct cl_io *io);
75         int  (*llo_io_init)(const struct lu_env *env,
76                             struct cl_object *obj, struct cl_io *io);
77         int  (*llo_getattr)(const struct lu_env *env, struct cl_object *obj,
78                             struct cl_attr *attr);
79 };
80
81 /*****************************************************************************
82  *
83  * Lov object layout operations.
84  *
85  */
86
87 static void lov_install_empty(const struct lu_env *env,
88                               struct lov_object *lov,
89                               union  lov_layout_state *state)
90 {
91         /*
92          * File without objects.
93          */
94 }
95
96 static int lov_init_empty(const struct lu_env *env,
97                           struct lov_device *dev, struct lov_object *lov,
98                           const struct cl_object_conf *conf,
99                           union  lov_layout_state *state)
100 {
101         return 0;
102 }
103
104 static void lov_install_raid0(const struct lu_env *env,
105                               struct lov_object *lov,
106                               union  lov_layout_state *state)
107 {
108         lov->u = *state;
109 }
110
111 static void oinfo_get_fid(const struct lov_oinfo *oinfo, struct lu_fid *fid)
112 {
113         __u64 idx = oinfo->loi_id;
114
115         /* See idif definition in wiki:CMD3_interoperability_architecture */
116
117         LASSERT(oinfo->loi_gr < 1ULL << 16);
118         LASSERT(oinfo->loi_id < 1ULL << 49);
119         ENTRY;
120
121         /*
122          * Now that the fid of stripe is not unique now, ost_idx have to
123          * be used to make it unique. This is ok because the stripe fids
124          * are just used in client side(to locate the objects). -jay
125          */
126         fid->f_seq = ((__u64)oinfo->loi_ost_idx) << 32 |
127                      oinfo->loi_gr << 16 | idx >> 32;
128         fid->f_oid = idx; /* truncated to 32 bits by assignment */
129         fid->f_ver = 0;
130         EXIT;
131 }
132
133 static struct cl_object *lov_sub_find(const struct lu_env *env,
134                                       struct cl_device *dev,
135                                       const struct lu_fid *fid,
136                                       const struct cl_object_conf *conf)
137 {
138         struct lu_object *o;
139
140         ENTRY;
141         o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu);
142         LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type));
143         RETURN(lu2cl(o));
144 }
145
146 static int lov_init_sub(const struct lu_env *env, struct lov_object *lov,
147                         struct cl_object *stripe,
148                         struct lov_layout_raid0 *r0, int idx)
149 {
150         struct cl_object_header *hdr;
151         struct cl_object_header *subhdr;
152         struct cl_object_header *parent;
153         struct lov_oinfo        *oinfo;
154         int result;
155
156         hdr    = cl_object_header(lov2cl(lov));
157         subhdr = cl_object_header(stripe);
158         parent = subhdr->coh_parent;
159
160         oinfo = r0->lo_lsm->lsm_oinfo[idx];
161         CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: id: "LPU64" gr: "LPU64
162                " idx: %d gen: %d\n",
163                PFID(&subhdr->coh_lu.loh_fid), subhdr, idx,
164                PFID(&hdr->coh_lu.loh_fid), hdr,
165                oinfo->loi_id, oinfo->loi_gr,
166                oinfo->loi_ost_idx, oinfo->loi_ost_gen);
167
168         if (parent == NULL) {
169                 subhdr->coh_parent = hdr;
170                 subhdr->coh_nesting = hdr->coh_nesting + 1;
171                 lu_object_ref_add(&stripe->co_lu, "lov-parent", lov);
172                 r0->lo_sub[idx] = cl2lovsub(stripe);
173                 r0->lo_sub[idx]->lso_super = lov;
174                 r0->lo_sub[idx]->lso_index = idx;
175                 result = 0;
176         } else {
177                 CERROR("Stripe is already owned by other file (%i).\n", idx);
178                 LU_OBJECT_DEBUG(D_ERROR, env, &stripe->co_lu, "\n");
179                 LU_OBJECT_DEBUG(D_ERROR, env, lu_object_top(&parent->coh_lu),
180                                 "old\n");
181                 LU_OBJECT_HEADER(D_ERROR, env, lov2lu(lov), "new\n");
182                 cl_object_put(env, stripe);
183                 result = -EIO;
184         }
185         return result;
186 }
187
188 static int lov_init_raid0(const struct lu_env *env,
189                           struct lov_device *dev, struct lov_object *lov,
190                           const struct cl_object_conf *conf,
191                           union  lov_layout_state *state)
192 {
193         int result;
194         int i;
195
196         struct cl_object        *stripe;
197         struct lov_thread_info  *lti     = lov_env_info(env);
198         struct cl_object_conf   *subconf = &lti->lti_stripe_conf;
199         struct lov_stripe_md    *lsm     = conf->u.coc_md->lsm;
200         struct lu_fid           *ofid    = &lti->lti_fid;
201         struct lov_layout_raid0 *r0      = &state->raid0;
202
203         ENTRY;
204         r0->lo_nr  = conf->u.coc_md->lsm->lsm_stripe_count;
205         r0->lo_lsm = conf->u.coc_md->lsm;
206         LASSERT(r0->lo_nr <= lov_targets_nr(dev));
207
208         OBD_ALLOC(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
209         if (r0->lo_sub != NULL) {
210                 result = 0;
211                 subconf->coc_inode = conf->coc_inode;
212                 cfs_spin_lock_init(&r0->lo_sub_lock);
213                 /*
214                  * Create stripe cl_objects.
215                  */
216                 for (i = 0; i < r0->lo_nr && result == 0; ++i) {
217                         struct cl_device *subdev;
218                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[i];
219                         int ost_idx = oinfo->loi_ost_idx;
220
221                         oinfo_get_fid(oinfo, ofid);
222                         subdev = lovsub2cl_dev(dev->ld_target[ost_idx]);
223                         subconf->u.coc_oinfo = oinfo;
224                         LASSERTF(subdev != NULL, "not init ost %d\n", ost_idx);
225                         stripe = lov_sub_find(env, subdev, ofid, subconf);
226                         if (!IS_ERR(stripe))
227                                 result = lov_init_sub(env, lov, stripe, r0, i);
228                         else
229                                 result = PTR_ERR(stripe);
230                 }
231         } else
232                 result = -ENOMEM;
233         RETURN(result);
234 }
235
236 static void lov_delete_empty(const struct lu_env *env, struct lov_object *lov,
237                              union lov_layout_state *state)
238 {
239         LASSERT(lov->lo_type == LLT_EMPTY);
240 }
241
242 static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov,
243                                struct lovsub_object *los, int idx)
244 {
245         struct cl_object        *sub;
246         struct lov_layout_raid0 *r0;
247         struct lu_site          *site;
248         cfs_waitlink_t          *waiter;
249
250         r0  = &lov->u.raid0;
251         LASSERT(r0->lo_sub[idx] == los);
252
253         sub  = lovsub2cl(los);
254         site = sub->co_lu.lo_dev->ld_site;
255
256         cl_object_kill(env, sub);
257         /* release a reference to the sub-object and ... */
258         lu_object_ref_del(&sub->co_lu, "lov-parent", lov);
259         cl_object_put(env, sub);
260
261         /* ... wait until it is actually destroyed---sub-object clears its
262          * ->lo_sub[] slot in lovsub_object_fini() */
263         if (r0->lo_sub[idx] == los) {
264                 waiter = &lov_env_info(env)->lti_waiter;
265                 cfs_waitlink_init(waiter);
266                 cfs_waitq_add(&site->ls_marche_funebre, waiter);
267                 cfs_set_current_state(CFS_TASK_UNINT);
268                 while (1) {
269                         /* this wait-queue is signaled at the end of
270                          * lu_object_free(). */
271                         cfs_set_current_state(CFS_TASK_UNINT);
272                         cfs_spin_lock(&r0->lo_sub_lock);
273                         if (r0->lo_sub[idx] == los) {
274                                 cfs_spin_unlock(&r0->lo_sub_lock);
275                                 cfs_waitq_wait(waiter, CFS_TASK_UNINT);
276                         } else {
277                                 cfs_spin_unlock(&r0->lo_sub_lock);
278                                 cfs_set_current_state(CFS_TASK_RUNNING);
279                                 break;
280                         }
281                 }
282                 cfs_waitq_del(&site->ls_marche_funebre, waiter);
283         }
284         LASSERT(r0->lo_sub[idx] == NULL);
285 }
286
287 static void lov_delete_raid0(const struct lu_env *env, struct lov_object *lov,
288                              union lov_layout_state *state)
289 {
290         struct lov_layout_raid0 *r0 = &state->raid0;
291         int                      i;
292
293         ENTRY;
294         if (r0->lo_sub != NULL) {
295                 for (i = 0; i < r0->lo_nr; ++i) {
296                         struct lovsub_object *los = r0->lo_sub[i];
297
298                         if (los != NULL)
299                                 /*
300                                  * If top-level object is to be evicted from
301                                  * the cache, so are its sub-objects.
302                                  */
303                                 lov_subobject_kill(env, lov, los, i);
304                 }
305         }
306         EXIT;
307 }
308
309 static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov,
310                            union lov_layout_state *state)
311 {
312         LASSERT(lov->lo_type == LLT_EMPTY);
313 }
314
315 static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov,
316                            union lov_layout_state *state)
317 {
318         struct lov_layout_raid0 *r0 = &state->raid0;
319
320         ENTRY;
321         if (r0->lo_sub != NULL) {
322                 OBD_FREE(r0->lo_sub, r0->lo_nr * sizeof r0->lo_sub[0]);
323                 r0->lo_sub = NULL;
324         }
325         EXIT;
326 }
327
328 static int lov_print_empty(const struct lu_env *env, void *cookie,
329                            lu_printer_t p, const struct lu_object *o)
330 {
331         (*p)(env, cookie, "empty\n");
332         return 0;
333 }
334
335 static int lov_print_raid0(const struct lu_env *env, void *cookie,
336                            lu_printer_t p, const struct lu_object *o)
337 {
338         struct lov_object       *lov = lu2lov(o);
339         struct lov_layout_raid0 *r0  = lov_r0(lov);
340         int i;
341
342         (*p)(env, cookie, "stripes: %d:\n", r0->lo_nr);
343         for (i = 0; i < r0->lo_nr; ++i) {
344                 struct lu_object *sub;
345
346                 if (r0->lo_sub[i] != NULL) {
347                         sub = lovsub2lu(r0->lo_sub[i]);
348                         lu_object_print(env, cookie, p, sub);
349                 } else
350                         (*p)(env, cookie, "sub %d absent\n", i);
351         }
352         return 0;
353 }
354
355 /**
356  * Implements cl_object_operations::coo_attr_get() method for an object
357  * without stripes (LLT_EMPTY layout type).
358  *
359  * The only attributes this layer is authoritative in this case is
360  * cl_attr::cat_blocks---it's 0.
361  */
362 static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj,
363                               struct cl_attr *attr)
364 {
365         attr->cat_blocks = 0;
366         return 0;
367 }
368
369 static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj,
370                               struct cl_attr *attr)
371 {
372         struct lov_object       *lov = cl2lov(obj);
373         struct lov_layout_raid0 *r0 = lov_r0(lov);
374         struct lov_stripe_md    *lsm = lov->u.raid0.lo_lsm;
375         struct ost_lvb          *lvb = &lov_env_info(env)->lti_lvb;
376         __u64                    kms;
377         int                      result = 0;
378
379         ENTRY;
380         if (!r0->lo_attr_valid) {
381                 /*
382                  * Fill LVB with attributes already initialized by the upper
383                  * layer.
384                  */
385                 cl_attr2lvb(lvb, attr);
386                 kms = attr->cat_kms;
387
388                 /*
389                  * XXX that should be replaced with a loop over sub-objects,
390                  * doing cl_object_attr_get() on them. But for now, let's
391                  * reuse old lov code.
392                  */
393
394                 /*
395                  * XXX take lsm spin-lock to keep lov_merge_lvb_kms()
396                  * happy. It's not needed, because new code uses
397                  * ->coh_attr_guard spin-lock to protect consistency of
398                  * sub-object attributes.
399                  */
400                 lov_stripe_lock(lsm);
401                 result = lov_merge_lvb_kms(lsm, lvb, &kms);
402                 lov_stripe_unlock(lsm);
403                 if (result == 0) {
404                         cl_lvb2attr(attr, lvb);
405                         attr->cat_kms = kms;
406                         r0->lo_attr_valid = 1;
407                         r0->lo_attr = *attr;
408                 }
409         } else
410                 *attr = r0->lo_attr;
411         RETURN(result);
412 }
413
414 const static struct lov_layout_operations lov_dispatch[] = {
415         [LLT_EMPTY] = {
416                 .llo_init      = lov_init_empty,
417                 .llo_delete    = lov_delete_empty,
418                 .llo_fini      = lov_fini_empty,
419                 .llo_install   = lov_install_empty,
420                 .llo_print     = lov_print_empty,
421                 .llo_page_init = lov_page_init_empty,
422                 .llo_lock_init = NULL,
423                 .llo_io_init   = lov_io_init_empty,
424                 .llo_getattr   = lov_attr_get_empty
425         },
426         [LLT_RAID0] = {
427                 .llo_init      = lov_init_raid0,
428                 .llo_delete    = lov_delete_raid0,
429                 .llo_fini      = lov_fini_raid0,
430                 .llo_install   = lov_install_raid0,
431                 .llo_print     = lov_print_raid0,
432                 .llo_page_init = lov_page_init_raid0,
433                 .llo_lock_init = lov_lock_init_raid0,
434                 .llo_io_init   = lov_io_init_raid0,
435                 .llo_getattr   = lov_attr_get_raid0
436         }
437 };
438
439
440 /**
441  * Performs a double-dispatch based on the layout type of an object.
442  */
443 #define LOV_2DISPATCH_NOLOCK(obj, op, ...)                              \
444 ({                                                                      \
445         struct lov_object                      *__obj = (obj);          \
446         enum lov_layout_type                    __llt;                  \
447                                                                         \
448         __llt = __obj->lo_type;                                         \
449         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
450         lov_dispatch[__llt].op(__VA_ARGS__);                            \
451 })
452
453 #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...)                       \
454 ({                                                                      \
455         struct lov_object                      *__obj = (obj);          \
456         int                                     __lock = !!(lock);      \
457         typeof(lov_dispatch[0].op(__VA_ARGS__)) __result;               \
458                                                                         \
459         __lock &= __obj->lo_owner != cfs_current();                     \
460         if (__lock)                                                     \
461                 cfs_down_read(&__obj->lo_type_guard);                   \
462         __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__);          \
463         if (__lock)                                                     \
464                 cfs_up_read(&__obj->lo_type_guard);                     \
465         __result;                                                       \
466 })
467
468 /**
469  * Performs a locked double-dispatch based on the layout type of an object.
470  */
471 #define LOV_2DISPATCH(obj, op, ...)                     \
472         LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__)
473
474 #define LOV_2DISPATCH_VOID(obj, op, ...)                                \
475 do {                                                                    \
476         struct lov_object                      *__obj = (obj);          \
477         enum lov_layout_type                    __llt;                  \
478                                                                         \
479         if (__obj->lo_owner != cfs_current())                           \
480                 cfs_down_read(&__obj->lo_type_guard);                   \
481         __llt = __obj->lo_type;                                         \
482         LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch));        \
483         lov_dispatch[__llt].op(__VA_ARGS__);                            \
484         if (__obj->lo_owner != cfs_current())                           \
485                 cfs_up_read(&__obj->lo_type_guard);                     \
486 } while (0)
487
488 static int lov_layout_change(const struct lu_env *env,
489                              struct lov_object *obj, enum lov_layout_type llt,
490                              const struct cl_object_conf *conf)
491 {
492         int result;
493         union lov_layout_state       *state = &lov_env_info(env)->lti_state;
494         const struct lov_layout_operations *old_ops;
495         const struct lov_layout_operations *new_ops;
496
497         LASSERT(0 <= obj->lo_type && obj->lo_type < ARRAY_SIZE(lov_dispatch));
498         LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch));
499         ENTRY;
500
501         old_ops = &lov_dispatch[obj->lo_type];
502         new_ops = &lov_dispatch[llt];
503
504         result = new_ops->llo_init(env, lu2lov_dev(obj->lo_cl.co_lu.lo_dev),
505                                    obj, conf, state);
506         if (result == 0) {
507                 struct cl_object_header *hdr = cl_object_header(&obj->lo_cl);
508                 void                    *cookie;
509                 struct lu_env           *nested;
510                 int                      refcheck;
511
512                 cookie = cl_env_reenter();
513                 nested = cl_env_get(&refcheck);
514                 if (!IS_ERR(nested))
515                         cl_object_prune(nested, &obj->lo_cl);
516                 else
517                         result = PTR_ERR(nested);
518                 cl_env_put(nested, &refcheck);
519                 cl_env_reexit(cookie);
520
521                 old_ops->llo_fini(env, obj, &obj->u);
522                 LASSERT(cfs_list_empty(&hdr->coh_locks));
523                 LASSERT(hdr->coh_tree.rnode == NULL);
524                 LASSERT(hdr->coh_pages == 0);
525
526                 new_ops->llo_install(env, obj, state);
527                 obj->lo_type = llt;
528         } else
529                 new_ops->llo_fini(env, obj, state);
530         RETURN(result);
531 }
532
533 /*****************************************************************************
534  *
535  * Lov object operations.
536  *
537  */
538
539 int lov_object_init(const struct lu_env *env, struct lu_object *obj,
540                     const struct lu_object_conf *conf)
541 {
542         struct lov_device            *dev   = lu2lov_dev(obj->lo_dev);
543         struct lov_object            *lov   = lu2lov(obj);
544         const struct cl_object_conf  *cconf = lu2cl_conf(conf);
545         union  lov_layout_state      *set   = &lov_env_info(env)->lti_state;
546         const struct lov_layout_operations *ops;
547         int result;
548
549         ENTRY;
550         cfs_init_rwsem(&lov->lo_type_guard);
551
552         /* no locking is necessary, as object is being created */
553         lov->lo_type = cconf->u.coc_md->lsm != NULL ? LLT_RAID0 : LLT_EMPTY;
554         ops = &lov_dispatch[lov->lo_type];
555         result = ops->llo_init(env, dev, lov, cconf, set);
556         if (result == 0)
557                 ops->llo_install(env, lov, set);
558         else
559                 ops->llo_fini(env, lov, set);
560         RETURN(result);
561 }
562
563 static int lov_conf_set(const struct lu_env *env, struct cl_object *obj,
564                         const struct cl_object_conf *conf)
565 {
566         struct lov_object *lov = cl2lov(obj);
567         int result;
568
569         ENTRY;
570         /*
571          * Currently only LLT_EMPTY -> LLT_RAID0 transition is supported.
572          */
573         LASSERT(lov->lo_owner != cfs_current());
574         cfs_down_write(&lov->lo_type_guard);
575         LASSERT(lov->lo_owner == NULL);
576         lov->lo_owner = cfs_current();
577         if (lov->lo_type == LLT_EMPTY && conf->u.coc_md->lsm != NULL)
578                 result = lov_layout_change(env, lov, LLT_RAID0, conf);
579         else
580                 result = -EOPNOTSUPP;
581         lov->lo_owner = NULL;
582         cfs_up_write(&lov->lo_type_guard);
583         RETURN(result);
584 }
585
586 static void lov_object_delete(const struct lu_env *env, struct lu_object *obj)
587 {
588         struct lov_object *lov = lu2lov(obj);
589
590         ENTRY;
591         LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u);
592         EXIT;
593 }
594
595 static void lov_object_free(const struct lu_env *env, struct lu_object *obj)
596 {
597         struct lov_object *lov = lu2lov(obj);
598
599         ENTRY;
600         LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u);
601         lu_object_fini(obj);
602         OBD_SLAB_FREE_PTR(lov, lov_object_kmem);
603         EXIT;
604 }
605
606 static int lov_object_print(const struct lu_env *env, void *cookie,
607                             lu_printer_t p, const struct lu_object *o)
608 {
609         return LOV_2DISPATCH(lu2lov(o), llo_print, env, cookie, p, o);
610 }
611
612 struct cl_page *lov_page_init(const struct lu_env *env, struct cl_object *obj,
613                               struct cl_page *page, cfs_page_t *vmpage)
614 {
615         return LOV_2DISPATCH(cl2lov(obj),
616                              llo_page_init, env, obj, page, vmpage);
617 }
618
619 /**
620  * Implements cl_object_operations::clo_io_init() method for lov
621  * layer. Dispatches to the appropriate layout io initialization method.
622  */
623 int lov_io_init(const struct lu_env *env, struct cl_object *obj,
624                 struct cl_io *io)
625 {
626         CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl);
627         /*
628          * Do not take lock in case of CIT_MISC io, because
629          *
630          *     - if this is an io for a glimpse, then we don't care;
631          *
632          *     - if this not a glimpse (writepage or lock cancellation), then
633          *       layout change cannot happen because a page or a lock
634          *       already exist; and
635          *
636          *     - lock ordering (lock mutex nests within layout rw-semaphore)
637          *       is obeyed in case of lock cancellation.
638          */
639         return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init,
640                                      io->ci_type != CIT_MISC, env, obj, io);
641 }
642
643 /**
644  * An implementation of cl_object_operations::clo_attr_get() method for lov
645  * layer. For raid0 layout this collects and merges attributes of all
646  * sub-objects.
647  */
648 static int lov_attr_get(const struct lu_env *env, struct cl_object *obj,
649                         struct cl_attr *attr)
650 {
651         /* do not take lock, as this function is called under a
652          * spin-lock. Layout is protected from changing by ongoing IO. */
653         return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr);
654 }
655
656 static int lov_attr_set(const struct lu_env *env, struct cl_object *obj,
657                         const struct cl_attr *attr, unsigned valid)
658 {
659         /*
660          * No dispatch is required here, as no layout implements this.
661          */
662         return 0;
663 }
664
665 int lov_lock_init(const struct lu_env *env, struct cl_object *obj,
666                   struct cl_lock *lock, const struct cl_io *io)
667 {
668         return LOV_2DISPATCH(cl2lov(obj), llo_lock_init, env, obj, lock, io);
669 }
670
671 static const struct cl_object_operations lov_ops = {
672         .coo_page_init = lov_page_init,
673         .coo_lock_init = lov_lock_init,
674         .coo_io_init   = lov_io_init,
675         .coo_attr_get  = lov_attr_get,
676         .coo_attr_set  = lov_attr_set,
677         .coo_conf_set  = lov_conf_set
678 };
679
680 static const struct lu_object_operations lov_lu_obj_ops = {
681         .loo_object_init      = lov_object_init,
682         .loo_object_delete    = lov_object_delete,
683         .loo_object_release   = NULL,
684         .loo_object_free      = lov_object_free,
685         .loo_object_print     = lov_object_print,
686         .loo_object_invariant = NULL
687 };
688
689 struct lu_object *lov_object_alloc(const struct lu_env *env,
690                                    const struct lu_object_header *unused,
691                                    struct lu_device *dev)
692 {
693         struct lov_object *lov;
694         struct lu_object  *obj;
695
696         ENTRY;
697         OBD_SLAB_ALLOC_PTR_GFP(lov, lov_object_kmem, CFS_ALLOC_IO);
698         if (lov != NULL) {
699                 obj = lov2lu(lov);
700                 lu_object_init(obj, NULL, dev);
701                 lov->lo_cl.co_ops = &lov_ops;
702                 lov->lo_type = -1; /* invalid, to catch uninitialized type */
703                 /*
704                  * object io operation vector (cl_object::co_iop) is installed
705                  * later in lov_object_init(), as different vectors are used
706                  * for object with different layouts.
707                  */
708                 obj->lo_ops = &lov_lu_obj_ops;
709         } else
710                 obj = NULL;
711         RETURN(obj);
712 }
713
714 /** @} lov */