Whamcloud - gitweb
LU-3850 obdecho: create remote dir from echo client
[fs/lustre-release.git] / lustre / lod / lod_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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2013, Intel Corporation.
27  */
28 /*
29  * lustre/lod/lod_object.c
30  *
31  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
32  */
33
34 #define DEBUG_SUBSYSTEM S_MDS
35
36 #include <obd.h>
37 #include <obd_class.h>
38 #include <lustre_ver.h>
39 #include <obd_support.h>
40 #include <lprocfs_status.h>
41
42 #include <lustre_fid.h>
43 #include <lustre_param.h>
44 #include <lustre_fid.h>
45 #include <obd_lov.h>
46
47 #include "lod_internal.h"
48
49 extern struct kmem_cache *lod_object_kmem;
50 static const struct dt_body_operations lod_body_lnk_ops;
51
52 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
53                             struct dt_rec *rec, const struct dt_key *key,
54                             struct lustre_capa *capa)
55 {
56         struct dt_object *next = dt_object_child(dt);
57         return next->do_index_ops->dio_lookup(env, next, rec, key, capa);
58 }
59
60 static int lod_declare_index_insert(const struct lu_env *env,
61                                     struct dt_object *dt,
62                                     const struct dt_rec *rec,
63                                     const struct dt_key *key,
64                                     struct thandle *handle)
65 {
66         return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
67 }
68
69 static int lod_index_insert(const struct lu_env *env,
70                             struct dt_object *dt,
71                             const struct dt_rec *rec,
72                             const struct dt_key *key,
73                             struct thandle *th,
74                             struct lustre_capa *capa,
75                             int ign)
76 {
77         return dt_insert(env, dt_object_child(dt), rec, key, th, capa, ign);
78 }
79
80 static int lod_declare_index_delete(const struct lu_env *env,
81                                     struct dt_object *dt,
82                                     const struct dt_key *key,
83                                     struct thandle *th)
84 {
85         return dt_declare_delete(env, dt_object_child(dt), key, th);
86 }
87
88 static int lod_index_delete(const struct lu_env *env,
89                             struct dt_object *dt,
90                             const struct dt_key *key,
91                             struct thandle *th,
92                             struct lustre_capa *capa)
93 {
94         return dt_delete(env, dt_object_child(dt), key, th, capa);
95 }
96
97 static struct dt_it *lod_it_init(const struct lu_env *env,
98                                  struct dt_object *dt, __u32 attr,
99                                  struct lustre_capa *capa)
100 {
101         struct dt_object        *next = dt_object_child(dt);
102         struct lod_it           *it = &lod_env_info(env)->lti_it;
103         struct dt_it            *it_next;
104
105
106         it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
107         if (IS_ERR(it_next))
108                 return it_next;
109
110         /* currently we do not use more than one iterator per thread
111          * so we store it in thread info. if at some point we need
112          * more active iterators in a single thread, we can allocate
113          * additional ones */
114         LASSERT(it->lit_obj == NULL);
115
116         it->lit_it = it_next;
117         it->lit_obj = next;
118
119         return (struct dt_it *)it;
120 }
121
122 #define LOD_CHECK_IT(env, it)                                   \
123 {                                                               \
124         LASSERT((it)->lit_obj != NULL);                         \
125         LASSERT((it)->lit_it != NULL);                          \
126 } while(0)
127
128 void lod_it_fini(const struct lu_env *env, struct dt_it *di)
129 {
130         struct lod_it *it = (struct lod_it *)di;
131
132         LOD_CHECK_IT(env, it);
133         it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
134
135         /* the iterator not in use any more */
136         it->lit_obj = NULL;
137         it->lit_it = NULL;
138 }
139
140 int lod_it_get(const struct lu_env *env, struct dt_it *di,
141                const struct dt_key *key)
142 {
143         const struct lod_it *it = (const struct lod_it *)di;
144
145         LOD_CHECK_IT(env, it);
146         return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
147 }
148
149 void lod_it_put(const struct lu_env *env, struct dt_it *di)
150 {
151         struct lod_it *it = (struct lod_it *)di;
152
153         LOD_CHECK_IT(env, it);
154         return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
155 }
156
157 int lod_it_next(const struct lu_env *env, struct dt_it *di)
158 {
159         struct lod_it *it = (struct lod_it *)di;
160
161         LOD_CHECK_IT(env, it);
162         return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
163 }
164
165 struct dt_key *lod_it_key(const struct lu_env *env, const struct dt_it *di)
166 {
167         const struct lod_it *it = (const struct lod_it *)di;
168
169         LOD_CHECK_IT(env, it);
170         return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
171 }
172
173 int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
174 {
175         struct lod_it *it = (struct lod_it *)di;
176
177         LOD_CHECK_IT(env, it);
178         return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
179 }
180
181 int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
182                struct dt_rec *rec, __u32 attr)
183 {
184         const struct lod_it *it = (const struct lod_it *)di;
185
186         LOD_CHECK_IT(env, it);
187         return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
188 }
189
190 __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
191 {
192         const struct lod_it *it = (const struct lod_it *)di;
193
194         LOD_CHECK_IT(env, it);
195         return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
196 }
197
198 int lod_it_load(const struct lu_env *env, const struct dt_it *di, __u64 hash)
199 {
200         const struct lod_it *it = (const struct lod_it *)di;
201
202         LOD_CHECK_IT(env, it);
203         return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
204 }
205
206 int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
207                    void* key_rec)
208 {
209         const struct lod_it *it = (const struct lod_it *)di;
210
211         LOD_CHECK_IT(env, it);
212         return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it, key_rec);
213 }
214
215 static struct dt_index_operations lod_index_ops = {
216         .dio_lookup             = lod_index_lookup,
217         .dio_declare_insert     = lod_declare_index_insert,
218         .dio_insert             = lod_index_insert,
219         .dio_declare_delete     = lod_declare_index_delete,
220         .dio_delete             = lod_index_delete,
221         .dio_it = {
222                 .init           = lod_it_init,
223                 .fini           = lod_it_fini,
224                 .get            = lod_it_get,
225                 .put            = lod_it_put,
226                 .next           = lod_it_next,
227                 .key            = lod_it_key,
228                 .key_size       = lod_it_key_size,
229                 .rec            = lod_it_rec,
230                 .store          = lod_it_store,
231                 .load           = lod_it_load,
232                 .key_rec        = lod_it_key_rec,
233         }
234 };
235
236 static void lod_object_read_lock(const struct lu_env *env,
237                                  struct dt_object *dt, unsigned role)
238 {
239         dt_read_lock(env, dt_object_child(dt), role);
240 }
241
242 static void lod_object_write_lock(const struct lu_env *env,
243                                   struct dt_object *dt, unsigned role)
244 {
245         dt_write_lock(env, dt_object_child(dt), role);
246 }
247
248 static void lod_object_read_unlock(const struct lu_env *env,
249                                    struct dt_object *dt)
250 {
251         dt_read_unlock(env, dt_object_child(dt));
252 }
253
254 static void lod_object_write_unlock(const struct lu_env *env,
255                                     struct dt_object *dt)
256 {
257         dt_write_unlock(env, dt_object_child(dt));
258 }
259
260 static int lod_object_write_locked(const struct lu_env *env,
261                                    struct dt_object *dt)
262 {
263         return dt_write_locked(env, dt_object_child(dt));
264 }
265
266 static int lod_attr_get(const struct lu_env *env,
267                         struct dt_object *dt,
268                         struct lu_attr *attr,
269                         struct lustre_capa *capa)
270 {
271         return dt_attr_get(env, dt_object_child(dt), attr, capa);
272 }
273
274 static int lod_declare_attr_set(const struct lu_env *env,
275                                 struct dt_object *dt,
276                                 const struct lu_attr *attr,
277                                 struct thandle *handle)
278 {
279         struct dt_object  *next = dt_object_child(dt);
280         struct lod_object *lo = lod_dt_obj(dt);
281         int                rc, i;
282         ENTRY;
283
284         /*
285          * declare setattr on the local object
286          */
287         rc = dt_declare_attr_set(env, next, attr, handle);
288         if (rc)
289                 RETURN(rc);
290
291         /* osp_declare_attr_set() ignores all attributes other than
292          * UID, GID, and size, and osp_attr_set() ignores all but UID
293          * and GID.  Declaration of size attr setting happens through
294          * lod_declare_init_size(), and not through this function.
295          * Therefore we need not load striping unless ownership is
296          * changing.  This should save memory and (we hope) speed up
297          * rename(). */
298         if (!(attr->la_valid & (LA_UID | LA_GID)))
299                 RETURN(rc);
300
301         /*
302          * load striping information, notice we don't do this when object
303          * is being initialized as we don't need this information till
304          * few specific cases like destroy, chown
305          */
306         rc = lod_load_striping(env, lo);
307         if (rc)
308                 RETURN(rc);
309
310         /*
311          * if object is striped declare changes on the stripes
312          */
313         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
314         for (i = 0; i < lo->ldo_stripenr; i++) {
315                 LASSERT(lo->ldo_stripe[i]);
316                 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr, handle);
317                 if (rc) {
318                         CERROR("failed declaration: %d\n", rc);
319                         break;
320                 }
321         }
322
323         RETURN(rc);
324 }
325
326 static int lod_attr_set(const struct lu_env *env,
327                         struct dt_object *dt,
328                         const struct lu_attr *attr,
329                         struct thandle *handle,
330                         struct lustre_capa *capa)
331 {
332         struct dt_object  *next = dt_object_child(dt);
333         struct lod_object *lo = lod_dt_obj(dt);
334         int                rc, i;
335         ENTRY;
336
337         /*
338          * apply changes to the local object
339          */
340         rc = dt_attr_set(env, next, attr, handle, capa);
341         if (rc)
342                 RETURN(rc);
343
344         if (!(attr->la_valid & (LA_UID | LA_GID)))
345                 RETURN(rc);
346
347         /*
348          * if object is striped, apply changes to all the stripes
349          */
350         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
351         for (i = 0; i < lo->ldo_stripenr; i++) {
352                 LASSERT(lo->ldo_stripe[i]);
353                 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
354                 if (rc) {
355                         CERROR("failed declaration: %d\n", rc);
356                         break;
357                 }
358         }
359
360         RETURN(rc);
361 }
362
363 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
364                          struct lu_buf *buf, const char *name,
365                          struct lustre_capa *capa)
366 {
367         struct lod_thread_info  *info = lod_env_info(env);
368         struct lod_device       *dev = lu2lod_dev(dt->do_lu.lo_dev);
369         int                      rc, is_root;
370         ENTRY;
371
372         rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
373         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
374                 RETURN(rc);
375
376         /*
377          * lod returns default striping on the real root of the device
378          * this is like the root stores default striping for the whole
379          * filesystem. historically we've been using a different approach
380          * and store it in the config.
381          */
382         dt_root_get(env, dev->lod_child, &info->lti_fid);
383         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
384
385         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
386                 struct lov_user_md *lum = buf->lb_buf;
387                 struct lov_desc    *desc = &dev->lod_desc;
388
389                 if (buf->lb_buf == NULL) {
390                         rc = sizeof(*lum);
391                 } else if (buf->lb_len >= sizeof(*lum)) {
392                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
393                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
394                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
395                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
396                         lum->lmm_stripe_size = cpu_to_le32(
397                                                 desc->ld_default_stripe_size);
398                         lum->lmm_stripe_count = cpu_to_le16(
399                                                 desc->ld_default_stripe_count);
400                         lum->lmm_stripe_offset = cpu_to_le16(
401                                                 desc->ld_default_stripe_offset);
402                         rc = sizeof(*lum);
403                 } else {
404                         rc = -ERANGE;
405                 }
406         }
407
408         RETURN(rc);
409 }
410
411 /*
412  * LOV xattr is a storage for striping, and LOD owns this xattr.
413  * but LOD allows others to control striping to some extent
414  * - to reset strping
415  * - to set new defined striping
416  * - to set new semi-defined striping
417  *   - number of stripes is defined
418  *   - number of stripes + osts are defined
419  *   - ??
420  */
421 static int lod_declare_xattr_set(const struct lu_env *env,
422                                  struct dt_object *dt,
423                                  const struct lu_buf *buf,
424                                  const char *name, int fl,
425                                  struct thandle *th)
426 {
427         struct dt_object *next = dt_object_child(dt);
428         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
429         __u32             mode;
430         int               rc;
431         ENTRY;
432
433         /*
434          * allow to declare predefined striping on a new (!mode) object
435          * which is supposed to be replay of regular file creation
436          * (when LOV setting is declared)
437          * LU_XATTR_REPLACE is set to indicate a layout swap
438          */
439         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
440         if ((S_ISREG(mode) || !mode) && !strcmp(name, XATTR_NAME_LOV) &&
441              !(fl & LU_XATTR_REPLACE)) {
442                 /*
443                  * this is a request to manipulate object's striping
444                  */
445                 if (dt_object_exists(dt)) {
446                         rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
447                         if (rc)
448                                 RETURN(rc);
449                 } else {
450                         memset(attr, 0, sizeof(*attr));
451                         attr->la_valid = LA_TYPE | LA_MODE;
452                         attr->la_mode = S_IFREG;
453                 }
454                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
455                 if (rc)
456                         RETURN(rc);
457         }
458
459         rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
460
461         RETURN(rc);
462 }
463
464 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
465                                     struct dt_object *dt,
466                                     const struct lu_buf *buf,
467                                     const char *name, int fl,
468                                     struct thandle *th,
469                                     struct lustre_capa *capa)
470 {
471         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
472         struct dt_object        *next = dt_object_child(dt);
473         struct lod_object       *l = lod_dt_obj(dt);
474         struct lov_user_md_v1   *lum;
475         struct lov_user_md_v3   *v3 = NULL;
476         int                      rc;
477         ENTRY;
478
479         LASSERT(l->ldo_stripe == NULL);
480         l->ldo_striping_cached = 0;
481         l->ldo_def_striping_set = 0;
482         lod_object_set_pool(l, NULL);
483         l->ldo_def_stripe_size = 0;
484         l->ldo_def_stripenr = 0;
485
486         LASSERT(buf != NULL && buf->lb_buf != NULL);
487         lum = buf->lb_buf;
488
489         rc = lod_verify_striping(d, buf, 0);
490         if (rc)
491                 RETURN(rc);
492
493         if (lum->lmm_magic == LOV_USER_MAGIC_V3)
494                 v3 = buf->lb_buf;
495
496         /* if { size, offset, count } = { 0, -1, 0 } and no pool
497          * (i.e. all default values specified) then delete default
498          * striping from dir. */
499         CDEBUG(D_OTHER,
500                 "set default striping: sz %u # %u offset %d %s %s\n",
501                 (unsigned)lum->lmm_stripe_size,
502                 (unsigned)lum->lmm_stripe_count,
503                 (int)lum->lmm_stripe_offset,
504                 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
505
506         if (LOVEA_DELETE_VALUES((lum->lmm_stripe_size),
507                                 (lum->lmm_stripe_count),
508                                 (lum->lmm_stripe_offset)) &&
509                         lum->lmm_magic == LOV_USER_MAGIC_V1) {
510                 rc = dt_xattr_del(env, next, name, th, capa);
511                 if (rc == -ENODATA)
512                         rc = 0;
513         } else {
514                 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
515         }
516
517         RETURN(rc);
518 }
519
520 static int lod_xattr_set(const struct lu_env *env,
521                          struct dt_object *dt, const struct lu_buf *buf,
522                          const char *name, int fl, struct thandle *th,
523                          struct lustre_capa *capa)
524 {
525         struct dt_object        *next = dt_object_child(dt);
526         __u32                    attr;
527         int                      rc;
528         ENTRY;
529
530         attr = dt->do_lu.lo_header->loh_attr & S_IFMT;
531         if (S_ISDIR(attr)) {
532                 if (strncmp(name, XATTR_NAME_LOV, strlen(XATTR_NAME_LOV)) == 0)
533                         rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
534                                                       fl, th, capa);
535                 else
536                         rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
537
538         } else if (S_ISREG(attr) && !strcmp(name, XATTR_NAME_LOV)) {
539                 /* in case of lov EA swap, just set it
540                  * if not, it is a replay so check striping match what we
541                  * already have during req replay, declare_xattr_set()
542                  * defines striping, then create() does the work
543                 */
544                 if (fl & LU_XATTR_REPLACE) {
545                         /* free stripes, then update disk */
546                         lod_object_free_striping(env, lod_dt_obj(dt));
547                         rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
548                 } else {
549                         rc = lod_striping_create(env, dt, NULL, NULL, th);
550                 }
551                 RETURN(rc);
552         } else {
553                 /*
554                  * behave transparantly for all other EAs
555                  */
556                 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
557         }
558
559         RETURN(rc);
560 }
561
562 static int lod_declare_xattr_del(const struct lu_env *env,
563                                  struct dt_object *dt, const char *name,
564                                  struct thandle *th)
565 {
566         return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
567 }
568
569 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
570                          const char *name, struct thandle *th,
571                          struct lustre_capa *capa)
572 {
573         if (!strcmp(name, XATTR_NAME_LOV))
574                 lod_object_free_striping(env, lod_dt_obj(dt));
575         return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
576 }
577
578 static int lod_xattr_list(const struct lu_env *env,
579                           struct dt_object *dt, struct lu_buf *buf,
580                           struct lustre_capa *capa)
581 {
582         return dt_xattr_list(env, dt_object_child(dt), buf, capa);
583 }
584
585 int lod_object_set_pool(struct lod_object *o, char *pool)
586 {
587         int len;
588
589         if (o->ldo_pool) {
590                 len = strlen(o->ldo_pool);
591                 OBD_FREE(o->ldo_pool, len + 1);
592                 o->ldo_pool = NULL;
593         }
594         if (pool) {
595                 len = strlen(pool);
596                 OBD_ALLOC(o->ldo_pool, len + 1);
597                 if (o->ldo_pool == NULL)
598                         return -ENOMEM;
599                 strcpy(o->ldo_pool, pool);
600         }
601         return 0;
602 }
603
604 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
605 {
606         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
607 }
608
609 static int lod_cache_parent_striping(const struct lu_env *env,
610                                      struct lod_object *lp)
611 {
612         struct lov_user_md_v1   *v1 = NULL;
613         struct lov_user_md_v3   *v3 = NULL;
614         int                      rc;
615         ENTRY;
616
617         /* dt_ah_init() is called from MDD without parent being write locked
618          * lock it here */
619         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
620         if (lp->ldo_striping_cached)
621                 GOTO(unlock, rc = 0);
622
623         rc = lod_get_lov_ea(env, lp);
624         if (rc < 0)
625                 GOTO(unlock, rc);
626
627         if (rc < sizeof(struct lov_user_md)) {
628                 /* don't lookup for non-existing or invalid striping */
629                 lp->ldo_def_striping_set = 0;
630                 lp->ldo_striping_cached = 1;
631                 lp->ldo_def_stripe_size = 0;
632                 lp->ldo_def_stripenr = 0;
633                 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
634                 GOTO(unlock, rc = 0);
635         }
636
637         v1 = (struct lov_user_md_v1 *)lod_env_info(env)->lti_ea_store;
638         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1))
639                 lustre_swab_lov_user_md_v1(v1);
640         else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3))
641                 lustre_swab_lov_user_md_v3(v3);
642
643         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
644                 GOTO(unlock, rc = 0);
645
646         if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
647                 GOTO(unlock, rc = 0);
648
649         lp->ldo_def_stripenr = v1->lmm_stripe_count;
650         lp->ldo_def_stripe_size = v1->lmm_stripe_size;
651         lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
652         lp->ldo_striping_cached = 1;
653         lp->ldo_def_striping_set = 1;
654
655         if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
656                 /* XXX: sanity check here */
657                 v3 = (struct lov_user_md_v3 *) v1;
658                 if (v3->lmm_pool_name[0])
659                         lod_object_set_pool(lp, v3->lmm_pool_name);
660         }
661
662         CDEBUG(D_OTHER, "def. striping: # %d, sz %d, off %d %s%s on "DFID"\n",
663                lp->ldo_def_stripenr, lp->ldo_def_stripe_size,
664                lp->ldo_def_stripe_offset, v3 ? "from " : "",
665                v3 ? lp->ldo_pool : "", PFID(lu_object_fid(&lp->ldo_obj.do_lu)));
666
667         EXIT;
668 unlock:
669         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
670         return rc;
671 }
672
673 /**
674  * used to transfer default striping data to the object being created
675  */
676 static void lod_ah_init(const struct lu_env *env,
677                         struct dt_allocation_hint *ah,
678                         struct dt_object *parent,
679                         struct dt_object *child,
680                         umode_t child_mode)
681 {
682         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
683         struct dt_object  *nextp = NULL;
684         struct dt_object  *nextc;
685         struct lod_object *lp = NULL;
686         struct lod_object *lc;
687         struct lov_desc   *desc;
688         ENTRY;
689
690         LASSERT(child);
691
692         if (likely(parent)) {
693                 nextp = dt_object_child(parent);
694                 lp = lod_dt_obj(parent);
695         }
696
697         nextc = dt_object_child(child);
698         lc = lod_dt_obj(child);
699
700         LASSERT(lc->ldo_stripenr == 0);
701         LASSERT(lc->ldo_stripe == NULL);
702
703         /*
704          * local object may want some hints
705          * in case of late striping creation, ->ah_init()
706          * can be called with local object existing
707          */
708         if (!dt_object_exists(nextc) || dt_object_remote(nextc))
709                 nextc->do_ops->do_ah_init(env, ah, dt_object_remote(nextp) ?
710                                           NULL : nextp, nextc, child_mode);
711
712         if (S_ISDIR(child_mode)) {
713                 if (lp->ldo_striping_cached == 0) {
714                         /* we haven't tried to get default striping for
715                          * the directory yet, let's cache it in the object */
716                         lod_cache_parent_striping(env, lp);
717                 }
718                 /* transfer defaults to new directory */
719                 if (lp->ldo_striping_cached) {
720                         if (lp->ldo_pool)
721                                 lod_object_set_pool(lc, lp->ldo_pool);
722                         lc->ldo_def_stripenr = lp->ldo_def_stripenr;
723                         lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
724                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
725                         lc->ldo_striping_cached = 1;
726                         lc->ldo_def_striping_set = 1;
727                         CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
728                                (int)lc->ldo_def_stripenr,
729                                (int)lc->ldo_def_stripe_size,
730                                (int)lc->ldo_def_stripe_offset);
731                 }
732                 return;
733         }
734
735         /*
736          * if object is going to be striped over OSTs, transfer default
737          * striping information to the child, so that we can use it
738          * during declaration and creation
739          */
740         if (!lod_object_will_be_striped(S_ISREG(child_mode),
741                                         lu_object_fid(&child->do_lu)))
742                 return;
743
744         /*
745          * try from the parent
746          */
747         if (likely(parent)) {
748                 if (lp->ldo_striping_cached == 0) {
749                         /* we haven't tried to get default striping for
750                          * the directory yet, let's cache it in the object */
751                         lod_cache_parent_striping(env, lp);
752                 }
753
754                 lc->ldo_def_stripe_offset = (__u16) -1;
755
756                 if (lp->ldo_def_striping_set) {
757                         if (lp->ldo_pool)
758                                 lod_object_set_pool(lc, lp->ldo_pool);
759                         lc->ldo_stripenr = lp->ldo_def_stripenr;
760                         lc->ldo_stripe_size = lp->ldo_def_stripe_size;
761                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
762                         CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
763                                lc->ldo_stripenr, lc->ldo_stripe_size,
764                                lp->ldo_pool ? lp->ldo_pool : "");
765                 }
766         }
767
768         /*
769          * if the parent doesn't provide with specific pattern, grab fs-wide one
770          */
771         desc = &d->lod_desc;
772         if (lc->ldo_stripenr == 0)
773                 lc->ldo_stripenr = desc->ld_default_stripe_count;
774         if (lc->ldo_stripe_size == 0)
775                 lc->ldo_stripe_size = desc->ld_default_stripe_size;
776         CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
777                lc->ldo_stripenr, lc->ldo_stripe_size,
778                lc->ldo_pool ? lc->ldo_pool : "");
779
780         EXIT;
781 }
782
783 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
784 /*
785  * this function handles a special case when truncate was done
786  * on a stripeless object and now striping is being created
787  * we can't lose that size, so we have to propagate it to newly
788  * created object
789  */
790 static int lod_declare_init_size(const struct lu_env *env,
791                                  struct dt_object *dt, struct thandle *th)
792 {
793         struct dt_object   *next = dt_object_child(dt);
794         struct lod_object  *lo = lod_dt_obj(dt);
795         struct lu_attr     *attr = &lod_env_info(env)->lti_attr;
796         uint64_t            size, offs;
797         int                 rc, stripe;
798         ENTRY;
799
800         /* XXX: we support the simplest (RAID0) striping so far */
801         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
802         LASSERT(lo->ldo_stripe_size > 0);
803
804         rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
805         LASSERT(attr->la_valid & LA_SIZE);
806         if (rc)
807                 RETURN(rc);
808
809         size = attr->la_size;
810         if (size == 0)
811                 RETURN(0);
812
813         /* ll_do_div64(a, b) returns a % b, and a = a / b */
814         ll_do_div64(size, (__u64) lo->ldo_stripe_size);
815         stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
816
817         size = size * lo->ldo_stripe_size;
818         offs = attr->la_size;
819         size += ll_do_div64(offs, lo->ldo_stripe_size);
820
821         attr->la_valid = LA_SIZE;
822         attr->la_size = size;
823
824         rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
825
826         RETURN(rc);
827 }
828
829
830 /**
831  * Create declaration of striped object
832  */
833 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
834                                struct lu_attr *attr,
835                                const struct lu_buf *lovea, struct thandle *th)
836 {
837         struct lod_thread_info  *info = lod_env_info(env);
838         struct dt_object        *next = dt_object_child(dt);
839         struct lod_object       *lo = lod_dt_obj(dt);
840         int                      rc;
841         ENTRY;
842
843         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
844                 /* failed to create striping, let's reset
845                  * config so that others don't get confused */
846                 lod_object_free_striping(env, lo);
847                 GOTO(out, rc = -ENOMEM);
848         }
849
850         /* choose OST and generate appropriate objects */
851         rc = lod_qos_prep_create(env, lo, attr, lovea, th);
852         if (rc) {
853                 /* failed to create striping, let's reset
854                  * config so that others don't get confused */
855                 lod_object_free_striping(env, lo);
856                 GOTO(out, rc);
857         }
858
859         /*
860          * declare storage for striping data
861          */
862         info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
863                                 lo->ldo_pool ?  LOV_MAGIC_V3 : LOV_MAGIC_V1);
864         rc = dt_declare_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV,
865                                   0, th);
866         if (rc)
867                 GOTO(out, rc);
868
869         /*
870          * if striping is created with local object's size > 0,
871          * we have to propagate this size to specific object
872          * the case is possible only when local object was created previously
873          */
874         if (dt_object_exists(next))
875                 rc = lod_declare_init_size(env, dt, th);
876
877 out:
878         RETURN(rc);
879 }
880
881 static int lod_declare_object_create(const struct lu_env *env,
882                                      struct dt_object *dt,
883                                      struct lu_attr *attr,
884                                      struct dt_allocation_hint *hint,
885                                      struct dt_object_format *dof,
886                                      struct thandle *th)
887 {
888         struct dt_object   *next = dt_object_child(dt);
889         struct lod_object  *lo = lod_dt_obj(dt);
890         int                 rc;
891         ENTRY;
892
893         LASSERT(dof);
894         LASSERT(attr);
895         LASSERT(th);
896
897         /*
898          * first of all, we declare creation of local object
899          */
900         rc = dt_declare_create(env, next, attr, hint, dof, th);
901         if (rc)
902                 GOTO(out, rc);
903
904         if (dof->dof_type == DFT_SYM)
905                 dt->do_body_ops = &lod_body_lnk_ops;
906
907         /*
908          * it's lod_ah_init() who has decided the object will striped
909          */
910         if (dof->dof_type == DFT_REGULAR) {
911                 /* callers don't want stripes */
912                 /* XXX: all tricky interactions with ->ah_make_hint() decided
913                  * to use striping, then ->declare_create() behaving differently
914                  * should be cleaned */
915                 if (dof->u.dof_reg.striped == 0)
916                         lo->ldo_stripenr = 0;
917                 if (lo->ldo_stripenr > 0)
918                         rc = lod_declare_striped_object(env, dt, attr,
919                                                         NULL, th);
920         } else if (dof->dof_type == DFT_DIR && lo->ldo_striping_cached) {
921                 struct lod_thread_info *info = lod_env_info(env);
922
923                 struct lov_user_md_v3 *v3;
924
925                 if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
926                                         lo->ldo_def_stripenr,
927                                         lo->ldo_def_stripe_offset))
928                         RETURN(0);
929
930                 OBD_ALLOC_PTR(v3);
931                 if (v3 == NULL)
932                         RETURN(-ENOMEM);
933
934                 v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
935                 v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
936                 fid_to_lmm_oi(lu_object_fid(&dt->do_lu), &v3->lmm_oi);
937                 lmm_oi_cpu_to_le(&v3->lmm_oi, &v3->lmm_oi);
938                 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
939                 v3->lmm_stripe_count = cpu_to_le32(lo->ldo_def_stripenr);
940                 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
941                 if (lo->ldo_pool)
942                         strncpy(v3->lmm_pool_name, lo->ldo_pool,
943                                 LOV_MAXPOOLNAME);
944
945                 info->lti_buf.lb_buf = v3;
946                 info->lti_buf.lb_len = sizeof(*v3);
947
948                 /* to transfer default striping from the parent */
949                 rc = dt_declare_xattr_set(env, next, &info->lti_buf,
950                                           XATTR_NAME_LOV, 0, th);
951                 OBD_FREE_PTR(v3);
952         }
953
954 out:
955         RETURN(rc);
956 }
957
958 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
959                         struct lu_attr *attr, struct dt_object_format *dof,
960                         struct thandle *th)
961 {
962         struct lod_object *lo = lod_dt_obj(dt);
963         int                rc = 0, i;
964         ENTRY;
965
966         LASSERT(lo->ldo_striping_cached == 0);
967
968         /* create all underlying objects */
969         for (i = 0; i < lo->ldo_stripenr; i++) {
970                 LASSERT(lo->ldo_stripe[i]);
971                 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
972
973                 if (rc)
974                         break;
975         }
976         if (rc == 0)
977                 rc = lod_generate_and_set_lovea(env, lo, th);
978
979         RETURN(rc);
980 }
981
982 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
983                              struct lu_attr *attr,
984                              struct dt_allocation_hint *hint,
985                              struct dt_object_format *dof, struct thandle *th)
986 {
987         struct dt_object   *next = dt_object_child(dt);
988         struct lod_object  *lo = lod_dt_obj(dt);
989         int                 rc;
990         ENTRY;
991
992         /* create local object */
993         rc = dt_create(env, next, attr, hint, dof, th);
994
995         if (rc == 0) {
996                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
997                         rc = lod_store_def_striping(env, dt, th);
998                 else if (lo->ldo_stripe)
999                         rc = lod_striping_create(env, dt, attr, dof, th);
1000         }
1001
1002         RETURN(rc);
1003 }
1004
1005 static int lod_declare_object_destroy(const struct lu_env *env,
1006                                       struct dt_object *dt,
1007                                       struct thandle *th)
1008 {
1009         struct dt_object   *next = dt_object_child(dt);
1010         struct lod_object  *lo = lod_dt_obj(dt);
1011         int                 rc, i;
1012         ENTRY;
1013
1014         /*
1015          * we declare destroy for the local object
1016          */
1017         rc = dt_declare_destroy(env, next, th);
1018         if (rc)
1019                 RETURN(rc);
1020
1021         /*
1022          * load striping information, notice we don't do this when object
1023          * is being initialized as we don't need this information till
1024          * few specific cases like destroy, chown
1025          */
1026         rc = lod_load_striping(env, lo);
1027         if (rc)
1028                 RETURN(rc);
1029
1030         /* declare destroy for all underlying objects */
1031         for (i = 0; i < lo->ldo_stripenr; i++) {
1032                 LASSERT(lo->ldo_stripe[i]);
1033                 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
1034
1035                 if (rc)
1036                         break;
1037         }
1038
1039         RETURN(rc);
1040 }
1041
1042 static int lod_object_destroy(const struct lu_env *env,
1043                 struct dt_object *dt, struct thandle *th)
1044 {
1045         struct dt_object  *next = dt_object_child(dt);
1046         struct lod_object *lo = lod_dt_obj(dt);
1047         int                rc, i;
1048         ENTRY;
1049
1050         /* destroy local object */
1051         rc = dt_destroy(env, next, th);
1052         if (rc)
1053                 RETURN(rc);
1054
1055         /* destroy all underlying objects */
1056         for (i = 0; i < lo->ldo_stripenr; i++) {
1057                 LASSERT(lo->ldo_stripe[i]);
1058                 rc = dt_destroy(env, lo->ldo_stripe[i], th);
1059                 if (rc)
1060                         break;
1061         }
1062
1063         RETURN(rc);
1064 }
1065
1066 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1067                          const struct dt_index_features *feat)
1068 {
1069         struct dt_object *next = dt_object_child(dt);
1070         int               rc;
1071         ENTRY;
1072
1073         LASSERT(next->do_ops);
1074         LASSERT(next->do_ops->do_index_try);
1075
1076         rc = next->do_ops->do_index_try(env, next, feat);
1077         if (next->do_index_ops && dt->do_index_ops == NULL)
1078                 dt->do_index_ops = &lod_index_ops;
1079
1080         RETURN(rc);
1081 }
1082
1083 static int lod_declare_ref_add(const struct lu_env *env,
1084                                struct dt_object *dt, struct thandle *th)
1085 {
1086         return dt_declare_ref_add(env, dt_object_child(dt), th);
1087 }
1088
1089 static int lod_ref_add(const struct lu_env *env,
1090                        struct dt_object *dt, struct thandle *th)
1091 {
1092         return dt_ref_add(env, dt_object_child(dt), th);
1093 }
1094
1095 static int lod_declare_ref_del(const struct lu_env *env,
1096                                struct dt_object *dt, struct thandle *th)
1097 {
1098         return dt_declare_ref_del(env, dt_object_child(dt), th);
1099 }
1100
1101 static int lod_ref_del(const struct lu_env *env,
1102                        struct dt_object *dt, struct thandle *th)
1103 {
1104         return dt_ref_del(env, dt_object_child(dt), th);
1105 }
1106
1107 static struct obd_capa *lod_capa_get(const struct lu_env *env,
1108                                      struct dt_object *dt,
1109                                      struct lustre_capa *old, __u64 opc)
1110 {
1111         return dt_capa_get(env, dt_object_child(dt), old, opc);
1112 }
1113
1114 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt)
1115 {
1116         return dt_object_sync(env, dt_object_child(dt));
1117 }
1118
1119 static int lod_object_lock(const struct lu_env *env,
1120                            struct dt_object *dt, struct lustre_handle *lh,
1121                            struct ldlm_enqueue_info *einfo,
1122                            void *policy)
1123 {
1124         struct dt_object   *next = dt_object_child(dt);
1125         int              rc;
1126         ENTRY;
1127
1128         /*
1129          * declare setattr on the local object
1130          */
1131         rc = dt_object_lock(env, next, lh, einfo, policy);
1132
1133         RETURN(rc);
1134 }
1135
1136 struct dt_object_operations lod_obj_ops = {
1137         .do_read_lock           = lod_object_read_lock,
1138         .do_write_lock          = lod_object_write_lock,
1139         .do_read_unlock         = lod_object_read_unlock,
1140         .do_write_unlock        = lod_object_write_unlock,
1141         .do_write_locked        = lod_object_write_locked,
1142         .do_attr_get            = lod_attr_get,
1143         .do_declare_attr_set    = lod_declare_attr_set,
1144         .do_attr_set            = lod_attr_set,
1145         .do_xattr_get           = lod_xattr_get,
1146         .do_declare_xattr_set   = lod_declare_xattr_set,
1147         .do_xattr_set           = lod_xattr_set,
1148         .do_declare_xattr_del   = lod_declare_xattr_del,
1149         .do_xattr_del           = lod_xattr_del,
1150         .do_xattr_list          = lod_xattr_list,
1151         .do_ah_init             = lod_ah_init,
1152         .do_declare_create      = lod_declare_object_create,
1153         .do_create              = lod_object_create,
1154         .do_declare_destroy     = lod_declare_object_destroy,
1155         .do_destroy             = lod_object_destroy,
1156         .do_index_try           = lod_index_try,
1157         .do_declare_ref_add     = lod_declare_ref_add,
1158         .do_ref_add             = lod_ref_add,
1159         .do_declare_ref_del     = lod_declare_ref_del,
1160         .do_ref_del             = lod_ref_del,
1161         .do_capa_get            = lod_capa_get,
1162         .do_object_sync         = lod_object_sync,
1163         .do_object_lock         = lod_object_lock,
1164 };
1165
1166 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
1167                         struct lu_buf *buf, loff_t *pos,
1168                         struct lustre_capa *capa)
1169 {
1170         struct dt_object *next = dt_object_child(dt);
1171         return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
1172 }
1173
1174 static ssize_t lod_declare_write(const struct lu_env *env,
1175                                  struct dt_object *dt,
1176                                  const loff_t size, loff_t pos,
1177                                  struct thandle *th)
1178 {
1179         return dt_declare_record_write(env, dt_object_child(dt),
1180                                        size, pos, th);
1181 }
1182
1183 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
1184                          const struct lu_buf *buf, loff_t *pos,
1185                          struct thandle *th, struct lustre_capa *capa, int iq)
1186 {
1187         struct dt_object *next = dt_object_child(dt);
1188         LASSERT(next);
1189         return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
1190 }
1191
1192 static const struct dt_body_operations lod_body_lnk_ops = {
1193         .dbo_read               = lod_read,
1194         .dbo_declare_write      = lod_declare_write,
1195         .dbo_write              = lod_write
1196 };
1197
1198 static int lod_object_init(const struct lu_env *env, struct lu_object *o,
1199                            const struct lu_object_conf *conf)
1200 {
1201         struct lod_device *d = lu2lod_dev(o->lo_dev);
1202         struct lu_object  *below;
1203         struct lu_device  *under;
1204         ENTRY;
1205
1206         /*
1207          * create local object
1208          */
1209         under = &d->lod_child->dd_lu_dev;
1210         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1211         if (below == NULL)
1212                 RETURN(-ENOMEM);
1213
1214         lu_object_add(o, below);
1215
1216         RETURN(0);
1217 }
1218
1219 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
1220 {
1221         int i;
1222
1223         if (lo->ldo_stripe) {
1224                 LASSERT(lo->ldo_stripes_allocated > 0);
1225
1226                 for (i = 0; i < lo->ldo_stripenr; i++) {
1227                         if (lo->ldo_stripe[i])
1228                                 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
1229                 }
1230
1231                 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
1232                 OBD_FREE(lo->ldo_stripe, i);
1233                 lo->ldo_stripe = NULL;
1234                 lo->ldo_stripes_allocated = 0;
1235         }
1236         lo->ldo_stripenr = 0;
1237         lo->ldo_pattern = 0;
1238 }
1239
1240 /*
1241  * ->start is called once all slices are initialized, including header's
1242  * cache for mode (object type). using the type we can initialize ops
1243  */
1244 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
1245 {
1246         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
1247                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
1248         return 0;
1249 }
1250
1251 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
1252 {
1253         struct lod_object *mo = lu2lod_obj(o);
1254
1255         /*
1256          * release all underlying object pinned
1257          */
1258
1259         lod_object_free_striping(env, mo);
1260
1261         lod_object_set_pool(mo, NULL);
1262
1263         lu_object_fini(o);
1264         OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
1265 }
1266
1267 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
1268 {
1269         /* XXX: shouldn't we release everything here in case if object
1270          * creation failed before? */
1271 }
1272
1273 static int lod_object_print(const struct lu_env *env, void *cookie,
1274                             lu_printer_t p, const struct lu_object *l)
1275 {
1276         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
1277
1278         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
1279 }
1280
1281 struct lu_object_operations lod_lu_obj_ops = {
1282         .loo_object_init        = lod_object_init,
1283         .loo_object_start       = lod_object_start,
1284         .loo_object_free        = lod_object_free,
1285         .loo_object_release     = lod_object_release,
1286         .loo_object_print       = lod_object_print,
1287 };
1288
1289 /**
1290  * Init remote lod object
1291  */
1292 static int lod_robject_init(const struct lu_env *env, struct lu_object *lo,
1293                             const struct lu_object_conf *conf)
1294 {
1295         struct lod_device *lod = lu2lod_dev(lo->lo_dev);
1296         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1297         struct lu_device  *c_dev = NULL;
1298         struct lu_object  *c_obj;
1299         int i;
1300         ENTRY;
1301
1302         lod_getref(ltd);
1303         if (ltd->ltd_tgts_size > 0) {
1304                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1305                         struct lod_tgt_desc *tgt;
1306                         tgt = LTD_TGT(ltd, i);
1307                         LASSERT(tgt && tgt->ltd_tgt);
1308                         if (tgt->ltd_index ==
1309                             lu2lod_obj(lo)->ldo_mds_num) {
1310                                 c_dev = &(tgt->ltd_tgt->dd_lu_dev);
1311                                 break;
1312                         }
1313                 }
1314         }
1315         lod_putref(lod, ltd);
1316
1317         if (unlikely(c_dev == NULL))
1318                 RETURN(-ENOENT);
1319
1320         c_obj = c_dev->ld_ops->ldo_object_alloc(env, lo->lo_header, c_dev);
1321         if (unlikely(c_obj == NULL))
1322                 RETURN(-ENOMEM);
1323
1324         lu_object_add(lo, c_obj);
1325
1326         RETURN(0);
1327 }
1328
1329 struct lu_object_operations lod_lu_robj_ops = {
1330         .loo_object_init      = lod_robject_init,
1331         .loo_object_start     = lod_object_start,
1332         .loo_object_free      = lod_object_free,
1333         .loo_object_release   = lod_object_release,
1334         .loo_object_print     = lod_object_print,
1335 };