Whamcloud - gitweb
5f69061e2abdf83d19d9babea2ba2918000c973a
[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 <lustre_lmv.h>
46 #include <obd_lov.h>
47 #include <md_object.h>
48
49 #include "lod_internal.h"
50
51 static const char dot[] = ".";
52 static const char dotdot[] = "..";
53
54 extern struct kmem_cache *lod_object_kmem;
55 static const struct dt_body_operations lod_body_lnk_ops;
56
57 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
58                             struct dt_rec *rec, const struct dt_key *key,
59                             struct lustre_capa *capa)
60 {
61         struct dt_object *next = dt_object_child(dt);
62         return next->do_index_ops->dio_lookup(env, next, rec, key, capa);
63 }
64
65 static int lod_declare_index_insert(const struct lu_env *env,
66                                     struct dt_object *dt,
67                                     const struct dt_rec *rec,
68                                     const struct dt_key *key,
69                                     struct thandle *handle)
70 {
71         return dt_declare_insert(env, dt_object_child(dt), rec, key, handle);
72 }
73
74 static int lod_index_insert(const struct lu_env *env,
75                             struct dt_object *dt,
76                             const struct dt_rec *rec,
77                             const struct dt_key *key,
78                             struct thandle *th,
79                             struct lustre_capa *capa,
80                             int ign)
81 {
82         return dt_insert(env, dt_object_child(dt), rec, key, th, capa, ign);
83 }
84
85 static int lod_declare_index_delete(const struct lu_env *env,
86                                     struct dt_object *dt,
87                                     const struct dt_key *key,
88                                     struct thandle *th)
89 {
90         return dt_declare_delete(env, dt_object_child(dt), key, th);
91 }
92
93 static int lod_index_delete(const struct lu_env *env,
94                             struct dt_object *dt,
95                             const struct dt_key *key,
96                             struct thandle *th,
97                             struct lustre_capa *capa)
98 {
99         return dt_delete(env, dt_object_child(dt), key, th, capa);
100 }
101
102 static struct dt_it *lod_it_init(const struct lu_env *env,
103                                  struct dt_object *dt, __u32 attr,
104                                  struct lustre_capa *capa)
105 {
106         struct dt_object        *next = dt_object_child(dt);
107         struct lod_it           *it = &lod_env_info(env)->lti_it;
108         struct dt_it            *it_next;
109
110
111         it_next = next->do_index_ops->dio_it.init(env, next, attr, capa);
112         if (IS_ERR(it_next))
113                 return it_next;
114
115         /* currently we do not use more than one iterator per thread
116          * so we store it in thread info. if at some point we need
117          * more active iterators in a single thread, we can allocate
118          * additional ones */
119         LASSERT(it->lit_obj == NULL);
120
121         it->lit_it = it_next;
122         it->lit_obj = next;
123
124         return (struct dt_it *)it;
125 }
126
127 #define LOD_CHECK_IT(env, it)                                   \
128 {                                                               \
129         LASSERT((it)->lit_obj != NULL);                         \
130         LASSERT((it)->lit_it != NULL);                          \
131 } while(0)
132
133 void lod_it_fini(const struct lu_env *env, struct dt_it *di)
134 {
135         struct lod_it *it = (struct lod_it *)di;
136
137         LOD_CHECK_IT(env, it);
138         it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
139
140         /* the iterator not in use any more */
141         it->lit_obj = NULL;
142         it->lit_it = NULL;
143 }
144
145 int lod_it_get(const struct lu_env *env, struct dt_it *di,
146                const struct dt_key *key)
147 {
148         const struct lod_it *it = (const struct lod_it *)di;
149
150         LOD_CHECK_IT(env, it);
151         return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
152 }
153
154 void lod_it_put(const struct lu_env *env, struct dt_it *di)
155 {
156         struct lod_it *it = (struct lod_it *)di;
157
158         LOD_CHECK_IT(env, it);
159         return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
160 }
161
162 int lod_it_next(const struct lu_env *env, struct dt_it *di)
163 {
164         struct lod_it *it = (struct lod_it *)di;
165
166         LOD_CHECK_IT(env, it);
167         return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
168 }
169
170 struct dt_key *lod_it_key(const struct lu_env *env, const struct dt_it *di)
171 {
172         const struct lod_it *it = (const struct lod_it *)di;
173
174         LOD_CHECK_IT(env, it);
175         return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
176 }
177
178 int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
179 {
180         struct lod_it *it = (struct lod_it *)di;
181
182         LOD_CHECK_IT(env, it);
183         return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
184 }
185
186 int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
187                struct dt_rec *rec, __u32 attr)
188 {
189         const struct lod_it *it = (const struct lod_it *)di;
190
191         LOD_CHECK_IT(env, it);
192         return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
193 }
194
195 __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
196 {
197         const struct lod_it *it = (const struct lod_it *)di;
198
199         LOD_CHECK_IT(env, it);
200         return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
201 }
202
203 int lod_it_load(const struct lu_env *env, const struct dt_it *di, __u64 hash)
204 {
205         const struct lod_it *it = (const struct lod_it *)di;
206
207         LOD_CHECK_IT(env, it);
208         return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
209 }
210
211 int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
212                    void* key_rec)
213 {
214         const struct lod_it *it = (const struct lod_it *)di;
215
216         LOD_CHECK_IT(env, it);
217         return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it, key_rec);
218 }
219
220 static struct dt_index_operations lod_index_ops = {
221         .dio_lookup             = lod_index_lookup,
222         .dio_declare_insert     = lod_declare_index_insert,
223         .dio_insert             = lod_index_insert,
224         .dio_declare_delete     = lod_declare_index_delete,
225         .dio_delete             = lod_index_delete,
226         .dio_it = {
227                 .init           = lod_it_init,
228                 .fini           = lod_it_fini,
229                 .get            = lod_it_get,
230                 .put            = lod_it_put,
231                 .next           = lod_it_next,
232                 .key            = lod_it_key,
233                 .key_size       = lod_it_key_size,
234                 .rec            = lod_it_rec,
235                 .store          = lod_it_store,
236                 .load           = lod_it_load,
237                 .key_rec        = lod_it_key_rec,
238         }
239 };
240
241 static void lod_object_read_lock(const struct lu_env *env,
242                                  struct dt_object *dt, unsigned role)
243 {
244         dt_read_lock(env, dt_object_child(dt), role);
245 }
246
247 static void lod_object_write_lock(const struct lu_env *env,
248                                   struct dt_object *dt, unsigned role)
249 {
250         dt_write_lock(env, dt_object_child(dt), role);
251 }
252
253 static void lod_object_read_unlock(const struct lu_env *env,
254                                    struct dt_object *dt)
255 {
256         dt_read_unlock(env, dt_object_child(dt));
257 }
258
259 static void lod_object_write_unlock(const struct lu_env *env,
260                                     struct dt_object *dt)
261 {
262         dt_write_unlock(env, dt_object_child(dt));
263 }
264
265 static int lod_object_write_locked(const struct lu_env *env,
266                                    struct dt_object *dt)
267 {
268         return dt_write_locked(env, dt_object_child(dt));
269 }
270
271 static int lod_attr_get(const struct lu_env *env,
272                         struct dt_object *dt,
273                         struct lu_attr *attr,
274                         struct lustre_capa *capa)
275 {
276         return dt_attr_get(env, dt_object_child(dt), attr, capa);
277 }
278
279 static int lod_declare_attr_set(const struct lu_env *env,
280                                 struct dt_object *dt,
281                                 const struct lu_attr *attr,
282                                 struct thandle *handle)
283 {
284         struct dt_object  *next = dt_object_child(dt);
285         struct lod_object *lo = lod_dt_obj(dt);
286         int                rc, i;
287         ENTRY;
288
289         /*
290          * declare setattr on the local object
291          */
292         rc = dt_declare_attr_set(env, next, attr, handle);
293         if (rc)
294                 RETURN(rc);
295
296         /* osp_declare_attr_set() ignores all attributes other than
297          * UID, GID, and size, and osp_attr_set() ignores all but UID
298          * and GID.  Declaration of size attr setting happens through
299          * lod_declare_init_size(), and not through this function.
300          * Therefore we need not load striping unless ownership is
301          * changing.  This should save memory and (we hope) speed up
302          * rename(). */
303         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
304                 if (!(attr->la_valid & (LA_UID | LA_GID)))
305                         RETURN(rc);
306
307                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
308                         RETURN(0);
309         } else {
310                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
311                                         LA_ATIME | LA_MTIME | LA_CTIME)))
312                         RETURN(rc);
313         }
314         /*
315          * load striping information, notice we don't do this when object
316          * is being initialized as we don't need this information till
317          * few specific cases like destroy, chown
318          */
319         rc = lod_load_striping(env, lo);
320         if (rc)
321                 RETURN(rc);
322
323         if (lo->ldo_stripenr == 0)
324                 RETURN(0);
325
326         if (!(attr->la_valid & ~(LA_ATIME | LA_MTIME | LA_CTIME))) {
327                 struct lu_attr   *la = &lod_env_info(env)->lti_attr;
328                 bool             setattr_time = false;
329
330                 rc = dt_attr_get(env, dt_object_child(dt), la,
331                                  BYPASS_CAPA);
332                 if (rc != 0)
333                         RETURN(rc);
334
335                 /* If it will only setattr time, it will only set
336                  * time < current_time */
337                 if ((attr->la_valid & LA_ATIME &&
338                      attr->la_atime < la->la_atime) ||
339                     (attr->la_valid & LA_CTIME &&
340                      attr->la_ctime < la->la_ctime) ||
341                     (attr->la_valid & LA_MTIME &&
342                      attr->la_mtime < la->la_mtime))
343                         setattr_time = true;
344
345                 if (!setattr_time)
346                         RETURN(0);
347         }
348         /*
349          * if object is striped declare changes on the stripes
350          */
351         LASSERT(lo->ldo_stripe);
352         for (i = 0; i < lo->ldo_stripenr; i++) {
353                 LASSERT(lo->ldo_stripe[i]);
354
355                 rc = dt_declare_attr_set(env, lo->ldo_stripe[i], attr, handle);
356                 if (rc) {
357                         CERROR("failed declaration: %d\n", rc);
358                         break;
359                 }
360         }
361
362         RETURN(rc);
363 }
364
365 static int lod_attr_set(const struct lu_env *env,
366                         struct dt_object *dt,
367                         const struct lu_attr *attr,
368                         struct thandle *handle,
369                         struct lustre_capa *capa)
370 {
371         struct dt_object  *next = dt_object_child(dt);
372         struct lod_object *lo = lod_dt_obj(dt);
373         int                rc, i;
374         ENTRY;
375
376         /*
377          * apply changes to the local object
378          */
379         rc = dt_attr_set(env, next, attr, handle, capa);
380         if (rc)
381                 RETURN(rc);
382
383         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
384                 if (!(attr->la_valid & (LA_UID | LA_GID)))
385                         RETURN(rc);
386
387                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
388                         RETURN(0);
389         } else {
390                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
391                                         LA_ATIME | LA_MTIME | LA_CTIME)))
392                         RETURN(rc);
393         }
394
395         if (lo->ldo_stripenr == 0)
396                 RETURN(0);
397
398         if (!(attr->la_valid & ~(LA_ATIME | LA_MTIME | LA_CTIME))) {
399                 struct lu_attr   *la = &lod_env_info(env)->lti_attr;
400                 bool             setattr_time = false;
401
402                 rc = dt_attr_get(env, dt_object_child(dt), la,
403                                  BYPASS_CAPA);
404                 if (rc != 0)
405                         RETURN(rc);
406
407                 /* If it will only setattr time, it will only set
408                  * time < current_time */
409                 if ((attr->la_valid & LA_ATIME &&
410                      attr->la_atime < la->la_atime) ||
411                     (attr->la_valid & LA_CTIME &&
412                      attr->la_atime < la->la_ctime) ||
413                     (attr->la_valid & LA_MTIME &&
414                      attr->la_atime < la->la_mtime))
415                         setattr_time = true;
416
417                 if (!setattr_time)
418                         RETURN(0);
419         }
420
421         /*
422          * if object is striped, apply changes to all the stripes
423          */
424         LASSERT(lo->ldo_stripe);
425         for (i = 0; i < lo->ldo_stripenr; i++) {
426                 LASSERT(lo->ldo_stripe[i]);
427                 rc = dt_attr_set(env, lo->ldo_stripe[i], attr, handle, capa);
428                 if (rc) {
429                         CERROR("failed declaration: %d\n", rc);
430                         break;
431                 }
432         }
433
434         RETURN(rc);
435 }
436
437 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
438                          struct lu_buf *buf, const char *name,
439                          struct lustre_capa *capa)
440 {
441         struct lod_thread_info  *info = lod_env_info(env);
442         struct lod_device       *dev = lu2lod_dev(dt->do_lu.lo_dev);
443         int                      rc, is_root;
444         ENTRY;
445
446         rc = dt_xattr_get(env, dt_object_child(dt), buf, name, capa);
447         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
448                 RETURN(rc);
449
450         /*
451          * lod returns default striping on the real root of the device
452          * this is like the root stores default striping for the whole
453          * filesystem. historically we've been using a different approach
454          * and store it in the config.
455          */
456         dt_root_get(env, dev->lod_child, &info->lti_fid);
457         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
458
459         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
460                 struct lov_user_md *lum = buf->lb_buf;
461                 struct lov_desc    *desc = &dev->lod_desc;
462
463                 if (buf->lb_buf == NULL) {
464                         rc = sizeof(*lum);
465                 } else if (buf->lb_len >= sizeof(*lum)) {
466                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
467                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
468                         lmm_oi_set_id(&lum->lmm_oi, 0);
469                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
470                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
471                         lum->lmm_stripe_size = cpu_to_le32(
472                                                 desc->ld_default_stripe_size);
473                         lum->lmm_stripe_count = cpu_to_le16(
474                                                 desc->ld_default_stripe_count);
475                         lum->lmm_stripe_offset = cpu_to_le16(
476                                                 desc->ld_default_stripe_offset);
477                         rc = sizeof(*lum);
478                 } else {
479                         rc = -ERANGE;
480                 }
481         }
482
483         RETURN(rc);
484 }
485
486 static int lod_verify_md_striping(struct lod_device *lod,
487                                   const struct lmv_user_md_v1 *lum)
488 {
489         int     rc = 0;
490         ENTRY;
491
492         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC))
493                 GOTO(out, rc = -EINVAL);
494
495         if (unlikely(le32_to_cpu(lum->lum_stripe_count) == 0))
496                 GOTO(out, rc = -EINVAL);
497
498         if (unlikely(le32_to_cpu(lum->lum_stripe_count) >
499                                 lod->lod_remote_mdt_count + 1))
500                 GOTO(out, rc = -EINVAL);
501 out:
502         if (rc != 0)
503                 CERROR("%s: invalid lmv_user_md: magic = %x, "
504                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
505                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
506                        (int)le32_to_cpu(lum->lum_stripe_offset),
507                        le32_to_cpu(lum->lum_stripe_count), rc);
508         return rc;
509 }
510
511 int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
512                     struct lu_buf *lmv_buf)
513 {
514         struct lod_thread_info  *info = lod_env_info(env);
515         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
516         struct lod_object       *lo = lod_dt_obj(dt);
517         struct lmv_mds_md_v1    *lmm1;
518         int                     stripe_count;
519         int                     lmm_size;
520         int                     i;
521         int                     rc;
522         __u32                   mdtidx;
523         ENTRY;
524
525         LASSERT(lo->ldo_dir_striped != 0);
526         LASSERT(lo->ldo_stripenr > 0);
527         stripe_count = lo->ldo_stripenr + 1;
528         lmm_size = lmv_mds_md_size(stripe_count, LMV_MAGIC);
529         if (info->lti_ea_store_size < lmm_size) {
530                 rc = lod_ea_store_resize(info, lmm_size);
531                 if (rc != 0)
532                         RETURN(rc);
533         }
534
535         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
536         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
537         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
538         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
539         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
540                             &mdtidx, LU_SEQ_RANGE_MDT);
541         if (rc != 0)
542                 RETURN(rc);
543
544         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
545         fid_cpu_to_le(&lmm1->lmv_stripe_fids[0], lu_object_fid(&dt->do_lu));
546         for (i = 0; i < lo->ldo_stripenr; i++) {
547                 struct dt_object *dto;
548
549                 dto = lo->ldo_stripe[i];
550                 LASSERT(dto != NULL);
551                 fid_cpu_to_le(&lmm1->lmv_stripe_fids[i + 1],
552                               lu_object_fid(&dto->do_lu));
553         }
554
555         lmv_buf->lb_buf = info->lti_ea_store;
556         lmv_buf->lb_len = lmm_size;
557         lo->ldo_dir_striping_cached = 1;
558
559         RETURN(rc);
560 }
561
562 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
563                            const struct lu_buf *buf)
564 {
565         struct lod_thread_info  *info = lod_env_info(env);
566         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
567         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
568         struct dt_object        **stripe;
569         union lmv_mds_md        *lmm = buf->lb_buf;
570         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
571         struct lu_fid           *fid = &info->lti_fid;
572         int                     i;
573         int                     rc = 0;
574         ENTRY;
575
576         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
577                 RETURN(-EINVAL);
578
579         if (le32_to_cpu(lmv1->lmv_stripe_count) <= 1)
580                 RETURN(0);
581
582         fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[0]);
583         /* Do not load striping information for slave inode */
584         if (!lu_fid_eq(fid, lu_object_fid(&lo->ldo_obj.do_lu))) {
585                 lo->ldo_dir_slave_stripe = 1;
586                 RETURN(0);
587         }
588
589         LASSERT(lo->ldo_stripe == NULL);
590         OBD_ALLOC(stripe, sizeof(stripe[0]) *
591                   (le32_to_cpu(lmv1->lmv_stripe_count) - 1));
592         if (stripe == NULL)
593                 RETURN(-ENOMEM);
594
595         /* skip master stripe */
596         for (i = 1; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
597                 struct lod_tgt_desc     *tgt;
598                 int                     idx;
599                 struct dt_object        *dto;
600
601                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
602                 rc = lod_fld_lookup(env, lod, fid,
603                                     &idx, LU_SEQ_RANGE_MDT);
604                 if (rc != 0)
605                         GOTO(out, rc);
606
607                 tgt = LTD_TGT(ltd, idx);
608                 if (tgt == NULL)
609                         GOTO(out, rc = -ESTALE);
610
611                 dto = dt_locate_at(env, tgt->ltd_tgt, fid,
612                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
613                                   NULL);
614                 if (IS_ERR(dto))
615                         GOTO(out, rc = PTR_ERR(dto));
616
617                 stripe[i - 1] = dto;
618         }
619 out:
620         lo->ldo_stripe = stripe;
621         lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count) - 1;
622         lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count) - 1;
623         if (rc != 0)
624                 lod_object_free_striping(env, lo);
625
626         RETURN(rc);
627 }
628
629 static int lod_prep_md_striped_create(const struct lu_env *env,
630                                       struct dt_object *dt,
631                                       struct lu_attr *attr,
632                                       const struct lmv_user_md_v1 *lum,
633                                       struct thandle *th)
634 {
635         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
636         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
637         struct lod_object       *lo = lod_dt_obj(dt);
638         struct dt_object        **stripe;
639         struct lu_buf           lmv_buf;
640         int                     stripe_count;
641         int                     *idx_array;
642         int                     rc = 0;
643         int                     i;
644         int                     j;
645         ENTRY;
646
647         /* The lum has been verifed in lod_verify_md_striping */
648         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
649         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
650
651         /* Do not need allocated master stripe */
652         stripe_count = le32_to_cpu(lum->lum_stripe_count);
653         OBD_ALLOC(stripe, sizeof(stripe[0]) * (stripe_count - 1));
654         if (stripe == NULL)
655                 RETURN(-ENOMEM);
656
657         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
658         if (idx_array == NULL)
659                 GOTO(out_free, rc = -ENOMEM);
660
661         idx_array[0] = le32_to_cpu(lum->lum_stripe_offset);
662         for (i = 1; i < stripe_count; i++) {
663                 struct lod_tgt_desc     *tgt;
664                 struct dt_object        *dto;
665                 struct lu_fid           fid;
666                 int                     idx;
667                 struct lu_object_conf   conf = { 0 };
668
669                 idx = (idx_array[i - 1] + 1) % (lod->lod_remote_mdt_count + 1);
670
671                 for (j = 0; j < lod->lod_remote_mdt_count;
672                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
673                         bool already_allocated = false;
674                         int k;
675
676                         CDEBUG(D_INFO, "try idx %d, mdt cnt %d,"
677                                " allocated %d, last allocated %d\n", idx,
678                                lod->lod_remote_mdt_count, i, idx_array[i - 1]);
679
680                         /* Find next avaible target */
681                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
682                                 continue;
683
684                         /* check whether the idx already exists
685                          * in current allocated array */
686                         for (k = 0; k < i; k++) {
687                                 if (idx_array[k] == idx) {
688                                         already_allocated = true;
689                                         break;
690                                 }
691                         }
692
693                         if (already_allocated)
694                                 continue;
695
696                         break;
697                 }
698
699                 /* Can not allocate more stripes */
700                 if (j == lod->lod_remote_mdt_count) {
701                         CDEBUG(D_INFO, "%s: require stripes %d only get %d\n",
702                                lod2obd(lod)->obd_name, stripe_count, i - 1);
703                         break;
704                 }
705
706                 CDEBUG(D_INFO, "idx %d, mdt cnt %d,"
707                        " allocated %d, last allocated %d\n", idx,
708                        lod->lod_remote_mdt_count, i, idx_array[i - 1]);
709
710                 tgt = LTD_TGT(ltd, idx);
711                 LASSERT(tgt != NULL);
712
713                 rc = obd_fid_alloc(tgt->ltd_exp, &fid, NULL);
714                 if (rc < 0)
715                         GOTO(out_put, rc);
716                 rc = 0;
717
718                 conf.loc_flags = LOC_F_NEW;
719                 dto = dt_locate_at(env, tgt->ltd_tgt, &fid,
720                                   dt->do_lu.lo_dev->ld_site->ls_top_dev, &conf);
721                 if (IS_ERR(dto))
722                         GOTO(out_put, rc = PTR_ERR(dto));
723                 stripe[i - 1] = dto;
724                 idx_array[i] = idx;
725         }
726
727         lo->ldo_dir_striped = 1;
728         lo->ldo_stripe = stripe;
729         lo->ldo_stripenr = i - 1;
730         lo->ldo_stripes_allocated = stripe_count - 1;
731
732         if (lo->ldo_stripenr == 0)
733                 GOTO(out_put, rc = -ENOSPC);
734
735         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
736         if (rc != 0)
737                 GOTO(out_put, rc);
738
739         for (i = 0; i < lo->ldo_stripenr; i++) {
740                 struct dt_object *dto;
741
742                 dto = stripe[i];
743                 /* only create slave striped object */
744                 rc = dt_declare_create(env, dto, attr, NULL, NULL, th);
745                 if (rc != 0)
746                         GOTO(out_put, rc);
747
748                 if (!dt_try_as_dir(env, dto))
749                         GOTO(out_put, rc = -EINVAL);
750
751                 rc = dt_declare_insert(env, dto,
752                      (const struct dt_rec *)lu_object_fid(&dto->do_lu),
753                      (const struct dt_key *)dot, th);
754                 if (rc != 0)
755                         GOTO(out_put, rc);
756
757                 /* master stripe FID will be put to .. */
758                 rc = dt_declare_insert(env, dto,
759                      (const struct dt_rec *)lu_object_fid(&dt->do_lu),
760                      (const struct dt_key *)dotdot, th);
761                 if (rc != 0)
762                         GOTO(out_put, rc);
763
764                 /* probably nothing to inherite */
765                 if (lo->ldo_striping_cached &&
766                     !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
767                                          lo->ldo_def_stripenr,
768                                          lo->ldo_def_stripe_offset)) {
769                         struct lod_thread_info  *info;
770                         struct lov_user_md_v3   *v3;
771
772                         /* sigh, lti_ea_store has been used for lmv_buf,
773                          * so we have to allocate buffer for default
774                          * stripe EA */
775                         OBD_ALLOC_PTR(v3);
776                         if (v3 == NULL)
777                                 GOTO(out_put, rc = -ENOMEM);
778
779                         memset(v3, 0, sizeof(*v3));
780                         v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
781                         v3->lmm_stripe_count =
782                                 cpu_to_le32(lo->ldo_def_stripenr);
783                         v3->lmm_stripe_offset =
784                                 cpu_to_le32(lo->ldo_def_stripe_offset);
785                         v3->lmm_stripe_size =
786                                 cpu_to_le32(lo->ldo_def_stripe_size);
787                         if (lo->ldo_pool)
788                                 strncpy(v3->lmm_pool_name, lo->ldo_pool,
789                                         LOV_MAXPOOLNAME);
790
791                         info = lod_env_info(env);
792                         info->lti_buf.lb_buf = v3;
793                         info->lti_buf.lb_len = sizeof(*v3);
794                         rc = dt_declare_xattr_set(env, dto,
795                                                   &info->lti_buf,
796                                                   XATTR_NAME_LOV,
797                                                   0, th);
798                         OBD_FREE_PTR(v3);
799                         if (rc != 0)
800                                 GOTO(out_put, rc);
801                 }
802                 rc = dt_declare_xattr_set(env, dto, &lmv_buf, XATTR_NAME_LMV, 0,
803                                           th);
804                 if (rc != 0)
805                         GOTO(out_put, rc);
806         }
807
808         rc = dt_declare_xattr_set(env, dt, &lmv_buf, XATTR_NAME_LMV, 0, th);
809         if (rc != 0)
810                 GOTO(out_put, rc);
811
812 out_put:
813         if (rc < 0) {
814                 for (i = 0; i < stripe_count - 1; i++)
815                         if (stripe[i] != NULL)
816                                 lu_object_put(env, &stripe[i]->do_lu);
817                 OBD_FREE(stripe, sizeof(stripe[0]) * (stripe_count - 1));
818         }
819
820 out_free:
821         if (idx_array != NULL)
822                 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
823
824         RETURN(rc);
825 }
826
827 /**
828  * Declare create striped md object.
829  */
830 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
831                                      struct dt_object *dt,
832                                      struct lu_attr *attr,
833                                      const struct lu_buf *lum_buf,
834                                      struct thandle *th)
835 {
836         struct lod_object       *lo = lod_dt_obj(dt);
837         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
838         struct lmv_user_md_v1   *lum;
839         int                     rc;
840         ENTRY;
841
842         lum = lum_buf->lb_buf;
843         LASSERT(lum != NULL);
844
845         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
846                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
847                (int)le32_to_cpu(lum->lum_stripe_offset));
848
849         if (le32_to_cpu(lum->lum_stripe_count) <= 1)
850                 GOTO(out, rc = 0);
851
852         rc = lod_verify_md_striping(lod, lum);
853         if (rc != 0)
854                 GOTO(out, rc);
855
856         /* prepare dir striped objects */
857         rc = lod_prep_md_striped_create(env, dt, attr, lum, th);
858         if (rc != 0) {
859                 /* failed to create striping, let's reset
860                  * config so that others don't get confused */
861                 lod_object_free_striping(env, lo);
862                 GOTO(out, rc);
863         }
864 out:
865         RETURN(rc);
866 }
867
868 /*
869  * LOV xattr is a storage for striping, and LOD owns this xattr.
870  * but LOD allows others to control striping to some extent
871  * - to reset strping
872  * - to set new defined striping
873  * - to set new semi-defined striping
874  *   - number of stripes is defined
875  *   - number of stripes + osts are defined
876  *   - ??
877  */
878 static int lod_declare_xattr_set(const struct lu_env *env,
879                                  struct dt_object *dt,
880                                  const struct lu_buf *buf,
881                                  const char *name, int fl,
882                                  struct thandle *th)
883 {
884         struct dt_object *next = dt_object_child(dt);
885         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
886         __u32             mode;
887         int               rc;
888         ENTRY;
889
890         /*
891          * allow to declare predefined striping on a new (!mode) object
892          * which is supposed to be replay of regular file creation
893          * (when LOV setting is declared)
894          * LU_XATTR_REPLACE is set to indicate a layout swap
895          */
896         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
897         if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
898              !(fl & LU_XATTR_REPLACE)) {
899                 /*
900                  * this is a request to manipulate object's striping
901                  */
902                 if (dt_object_exists(dt)) {
903                         rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
904                         if (rc)
905                                 RETURN(rc);
906                 } else {
907                         memset(attr, 0, sizeof(*attr));
908                         attr->la_valid = LA_TYPE | LA_MODE;
909                         attr->la_mode = S_IFREG;
910                 }
911                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
912         } else if (S_ISDIR(mode)) {
913                 struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
914                 struct lod_object       *lo = lod_dt_obj(dt);
915                 int                     i;
916
917                 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
918                         struct lmv_user_md_v1 *lum;
919
920                         LASSERT(buf != NULL && buf->lb_buf != NULL);
921                         lum = buf->lb_buf;
922                         rc = lod_verify_md_striping(d, lum);
923                         if (rc != 0)
924                                 RETURN(rc);
925                 }
926
927                 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
928                 if (rc != 0)
929                         RETURN(rc);
930
931                 /* set xattr to each stripes, if needed */
932                 rc = lod_load_striping(env, lo);
933                 if (rc != 0)
934                         RETURN(rc);
935
936                 if (lo->ldo_stripenr == 0)
937                         RETURN(rc);
938
939                 for (i = 0; i < lo->ldo_stripenr; i++) {
940                         LASSERT(lo->ldo_stripe[i]);
941                         rc = dt_declare_xattr_set(env, lo->ldo_stripe[i], buf,
942                                                   name, fl, th);
943                         if (rc != 0)
944                                 break;
945                 }
946         } else {
947                 rc = dt_declare_xattr_set(env, next, buf, name, fl, th);
948         }
949
950         RETURN(rc);
951 }
952
953 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
954 {
955         lo->ldo_striping_cached = 0;
956         lo->ldo_def_striping_set = 0;
957         lod_object_set_pool(lo, NULL);
958         lo->ldo_def_stripe_size = 0;
959         lo->ldo_def_stripenr = 0;
960 }
961
962 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
963                                     struct dt_object *dt,
964                                     const struct lu_buf *buf,
965                                     const char *name, int fl,
966                                     struct thandle *th,
967                                     struct lustre_capa *capa)
968 {
969         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
970         struct dt_object        *next = dt_object_child(dt);
971         struct lod_object       *l = lod_dt_obj(dt);
972         struct lov_user_md_v1   *lum;
973         struct lov_user_md_v3   *v3 = NULL;
974         int                      rc;
975         ENTRY;
976
977         /* If it is striped dir, we should clear the stripe cache for
978          * slave stripe as well, but there are no effective way to
979          * notify the LOD on the slave MDT, so we do not cache stripe
980          * information for slave stripe for now. XXX*/
981         lod_lov_stripe_cache_clear(l);
982         LASSERT(buf != NULL && buf->lb_buf != NULL);
983         lum = buf->lb_buf;
984
985         rc = lod_verify_striping(d, buf, 0);
986         if (rc)
987                 RETURN(rc);
988
989         if (lum->lmm_magic == LOV_USER_MAGIC_V3)
990                 v3 = buf->lb_buf;
991
992         /* if { size, offset, count } = { 0, -1, 0 } and no pool
993          * (i.e. all default values specified) then delete default
994          * striping from dir. */
995         CDEBUG(D_OTHER,
996                 "set default striping: sz %u # %u offset %d %s %s\n",
997                 (unsigned)lum->lmm_stripe_size,
998                 (unsigned)lum->lmm_stripe_count,
999                 (int)lum->lmm_stripe_offset,
1000                 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
1001
1002         if (LOVEA_DELETE_VALUES((lum->lmm_stripe_size),
1003                                 (lum->lmm_stripe_count),
1004                                 (lum->lmm_stripe_offset)) &&
1005                         lum->lmm_magic == LOV_USER_MAGIC_V1) {
1006                 rc = dt_xattr_del(env, next, name, th, capa);
1007                 if (rc == -ENODATA)
1008                         rc = 0;
1009         } else {
1010                 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
1011         }
1012
1013         RETURN(rc);
1014 }
1015
1016 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
1017                                             struct dt_object *dt,
1018                                             const struct lu_buf *buf,
1019                                             const char *name, int fl,
1020                                             struct thandle *th,
1021                                             struct lustre_capa *capa)
1022 {
1023         struct dt_object        *next = dt_object_child(dt);
1024         struct lod_object       *l = lod_dt_obj(dt);
1025         struct lmv_user_md_v1   *lum;
1026         int                      rc;
1027         ENTRY;
1028
1029         LASSERT(buf != NULL && buf->lb_buf != NULL);
1030         lum = buf->lb_buf;
1031
1032         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
1033               le32_to_cpu(lum->lum_stripe_count),
1034               (int)le32_to_cpu(lum->lum_stripe_offset));
1035
1036         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
1037                                  le32_to_cpu(lum->lum_stripe_offset)) &&
1038                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
1039                 rc = dt_xattr_del(env, next, name, th, capa);
1040                 if (rc == -ENODATA)
1041                         rc = 0;
1042         } else {
1043                 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
1044                 if (rc != 0)
1045                         RETURN(rc);
1046
1047                 /* Update default stripe cache */
1048                 if (l->ldo_dir_stripe == NULL) {
1049                         OBD_ALLOC_PTR(l->ldo_dir_stripe);
1050                         if (l->ldo_dir_stripe == NULL)
1051                                 RETURN(-ENOMEM);
1052                 }
1053
1054                 l->ldo_dir_striping_cached = 0;
1055                 l->ldo_dir_def_striping_set = 1;
1056                 l->ldo_dir_def_stripenr =
1057                         le32_to_cpu(lum->lum_stripe_count) - 1;
1058         }
1059
1060         RETURN(rc);
1061 }
1062
1063 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
1064                              const struct lu_buf *buf, const char *name,
1065                              int fl, struct thandle *th,
1066                              struct lustre_capa *capa)
1067 {
1068         struct lod_object       *lo = lod_dt_obj(dt);
1069         struct lu_buf           lmv_buf;
1070         int                     i;
1071         int                     rc;
1072         ENTRY;
1073
1074         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1075                 RETURN(-ENOTDIR);
1076
1077         /* The stripes are supposed to be allocated in declare phase,
1078          * if there are no stripes being allocated, it will skip */
1079         if (lo->ldo_stripenr == 0)
1080                 RETURN(0);
1081
1082         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1083         if (rc != 0)
1084                 RETURN(rc);
1085
1086         for (i = 0; i < lo->ldo_stripenr; i++) {
1087                 struct dt_object *dto;
1088                 struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
1089
1090                 dto = lo->ldo_stripe[i];
1091                 memset(attr, 0, sizeof(*attr));
1092                 attr->la_valid = LA_TYPE | LA_MODE;
1093                 attr->la_mode = S_IFDIR;
1094                 rc = dt_create(env, dto, attr, NULL, NULL, th);
1095                 if (rc != 0)
1096                         RETURN(rc);
1097
1098                 rc = dt_insert(env, dto,
1099                               (const struct dt_rec *)lu_object_fid(&dto->do_lu),
1100                               (const struct dt_key *)dot, th, capa, 0);
1101                 if (rc != 0)
1102                         RETURN(rc);
1103
1104                 rc = dt_insert(env, dto,
1105                               (struct dt_rec *)lu_object_fid(&dt->do_lu),
1106                               (const struct dt_key *)dotdot, th, capa, 0);
1107                 if (rc != 0)
1108                         RETURN(rc);
1109
1110                 if (lo->ldo_striping_cached &&
1111                     !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
1112                                          lo->ldo_def_stripenr,
1113                                          lo->ldo_def_stripe_offset)) {
1114                         struct lod_thread_info  *info;
1115                         struct lov_user_md_v3   *v3;
1116
1117                         /* sigh, lti_ea_store has been used for lmv_buf,
1118                          * so we have to allocate buffer for default
1119                          * stripe EA */
1120                         OBD_ALLOC_PTR(v3);
1121                         if (v3 == NULL)
1122                                 RETURN(-ENOMEM);
1123
1124                         memset(v3, 0, sizeof(*v3));
1125                         v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
1126                         v3->lmm_stripe_count =
1127                                 cpu_to_le32(lo->ldo_def_stripenr);
1128                         v3->lmm_stripe_offset =
1129                                 cpu_to_le32(lo->ldo_def_stripe_offset);
1130                         v3->lmm_stripe_size =
1131                                 cpu_to_le32(lo->ldo_def_stripe_size);
1132                         if (lo->ldo_pool)
1133                                 strncpy(v3->lmm_pool_name, lo->ldo_pool,
1134                                         LOV_MAXPOOLNAME);
1135
1136                         info = lod_env_info(env);
1137                         info->lti_buf.lb_buf = v3;
1138                         info->lti_buf.lb_len = sizeof(*v3);
1139                         rc = dt_xattr_set(env, dto, &info->lti_buf,
1140                                           XATTR_NAME_LOV, 0, th, capa);
1141                         OBD_FREE_PTR(v3);
1142                         if (rc != 0)
1143                                 RETURN(rc);
1144                 }
1145
1146                 rc = dt_xattr_set(env, dto, &lmv_buf, XATTR_NAME_LMV, fl, th,
1147                                   capa);
1148         }
1149
1150         rc = dt_xattr_set(env, dt, &lmv_buf, XATTR_NAME_LMV, fl, th, capa);
1151
1152         RETURN(rc);
1153 }
1154
1155 static int lod_xattr_set(const struct lu_env *env,
1156                          struct dt_object *dt, const struct lu_buf *buf,
1157                          const char *name, int fl, struct thandle *th,
1158                          struct lustre_capa *capa)
1159 {
1160         struct lod_object       *lo = lod_dt_obj(dt);
1161         struct dt_object        *next = dt_object_child(dt);
1162         __u32                    attr;
1163         int                      rc;
1164         int                     i;
1165         ENTRY;
1166
1167         attr = dt->do_lu.lo_header->loh_attr & S_IFMT;
1168         if (S_ISDIR(attr) && strcmp(name, XATTR_NAME_LOV) == 0) {
1169                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th, capa);
1170         } else if (S_ISREG(attr) && !strcmp(name, XATTR_NAME_LOV)) {
1171                 /* in case of lov EA swap, just set it
1172                  * if not, it is a replay so check striping match what we
1173                  * already have during req replay, declare_xattr_set()
1174                  * defines striping, then create() does the work
1175                 */
1176                 if (fl & LU_XATTR_REPLACE) {
1177                         /* free stripes, then update disk */
1178                         lod_object_free_striping(env, lod_dt_obj(dt));
1179                         rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
1180                 } else {
1181                         rc = lod_striping_create(env, dt, NULL, NULL, th);
1182                 }
1183         } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1184                 if (!S_ISDIR(attr))
1185                         RETURN(-ENOTDIR);
1186                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
1187                                                       th, capa);
1188         } else {
1189                 /*
1190                  * behave transparantly for all other EAs
1191                  */
1192                 rc = dt_xattr_set(env, next, buf, name, fl, th, capa);
1193         }
1194
1195         if (rc != 0 || !S_ISDIR(attr))
1196                 RETURN(rc);
1197
1198         if (lo->ldo_stripenr == 0)
1199                 RETURN(rc);
1200
1201         for (i = 0; i < lo->ldo_stripenr; i++) {
1202                 LASSERT(lo->ldo_stripe[i]);
1203                 rc = dt_xattr_set(env, lo->ldo_stripe[i], buf, name, fl, th,
1204                                   capa);
1205                 if (rc != 0)
1206                         break;
1207         }
1208
1209         RETURN(rc);
1210 }
1211
1212 static int lod_declare_xattr_del(const struct lu_env *env,
1213                                  struct dt_object *dt, const char *name,
1214                                  struct thandle *th)
1215 {
1216         return dt_declare_xattr_del(env, dt_object_child(dt), name, th);
1217 }
1218
1219 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
1220                          const char *name, struct thandle *th,
1221                          struct lustre_capa *capa)
1222 {
1223         if (!strcmp(name, XATTR_NAME_LOV))
1224                 lod_object_free_striping(env, lod_dt_obj(dt));
1225         return dt_xattr_del(env, dt_object_child(dt), name, th, capa);
1226 }
1227
1228 static int lod_xattr_list(const struct lu_env *env,
1229                           struct dt_object *dt, struct lu_buf *buf,
1230                           struct lustre_capa *capa)
1231 {
1232         return dt_xattr_list(env, dt_object_child(dt), buf, capa);
1233 }
1234
1235 int lod_object_set_pool(struct lod_object *o, char *pool)
1236 {
1237         int len;
1238
1239         if (o->ldo_pool) {
1240                 len = strlen(o->ldo_pool);
1241                 OBD_FREE(o->ldo_pool, len + 1);
1242                 o->ldo_pool = NULL;
1243         }
1244         if (pool) {
1245                 len = strlen(pool);
1246                 OBD_ALLOC(o->ldo_pool, len + 1);
1247                 if (o->ldo_pool == NULL)
1248                         return -ENOMEM;
1249                 strcpy(o->ldo_pool, pool);
1250         }
1251         return 0;
1252 }
1253
1254 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
1255 {
1256         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
1257 }
1258
1259
1260 static int lod_cache_parent_lov_striping(const struct lu_env *env,
1261                                          struct lod_object *lp)
1262 {
1263         struct lod_thread_info  *info = lod_env_info(env);
1264         struct lov_user_md_v1   *v1 = NULL;
1265         struct lov_user_md_v3   *v3 = NULL;
1266         int                      rc;
1267         ENTRY;
1268
1269         /* called from MDD without parent being write locked,
1270          * lock it here */
1271         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
1272         rc = lod_get_lov_ea(env, lp);
1273         if (rc < 0)
1274                 GOTO(unlock, rc);
1275
1276         if (rc < sizeof(struct lov_user_md)) {
1277                 /* don't lookup for non-existing or invalid striping */
1278                 lp->ldo_def_striping_set = 0;
1279                 lp->ldo_striping_cached = 1;
1280                 lp->ldo_def_stripe_size = 0;
1281                 lp->ldo_def_stripenr = 0;
1282                 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
1283                 GOTO(unlock, rc = 0);
1284         }
1285
1286         rc = 0;
1287         v1 = info->lti_ea_store;
1288         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1))
1289                 lustre_swab_lov_user_md_v1(v1);
1290         else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3))
1291                 lustre_swab_lov_user_md_v3(v3);
1292
1293         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
1294                 GOTO(unlock, rc = 0);
1295
1296         if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
1297                 GOTO(unlock, rc = 0);
1298
1299         lp->ldo_def_stripenr = v1->lmm_stripe_count;
1300         lp->ldo_def_stripe_size = v1->lmm_stripe_size;
1301         lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
1302         lp->ldo_striping_cached = 1;
1303         lp->ldo_def_striping_set = 1;
1304         if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
1305                 /* XXX: sanity check here */
1306                 v3 = (struct lov_user_md_v3 *) v1;
1307                 if (v3->lmm_pool_name[0])
1308                         lod_object_set_pool(lp, v3->lmm_pool_name);
1309         }
1310         EXIT;
1311 unlock:
1312         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
1313         return rc;
1314 }
1315
1316
1317 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
1318                                          struct lod_object *lp)
1319 {
1320         struct lod_thread_info  *info = lod_env_info(env);
1321         struct lmv_user_md_v1   *v1 = NULL;
1322         int                      rc;
1323         ENTRY;
1324
1325         /* called from MDD without parent being write locked,
1326          * lock it here */
1327         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
1328         rc = lod_get_default_lmv_ea(env, lp);
1329         if (rc < 0)
1330                 GOTO(unlock, rc);
1331
1332         if (rc < sizeof(struct lmv_user_md)) {
1333                 /* don't lookup for non-existing or invalid striping */
1334                 lp->ldo_dir_def_striping_set = 0;
1335                 lp->ldo_dir_striping_cached = 1;
1336                 lp->ldo_dir_def_stripenr = 0;
1337                 lp->ldo_dir_def_stripe_offset =
1338                                         (typeof(v1->lum_stripe_offset))(-1);
1339                 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1340                 GOTO(unlock, rc = 0);
1341         }
1342
1343         rc = 0;
1344         v1 = info->lti_ea_store;
1345
1346         lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count) - 1;
1347         lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
1348         lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
1349         lp->ldo_dir_def_striping_set = 1;
1350         lp->ldo_dir_striping_cached = 1;
1351
1352         EXIT;
1353 unlock:
1354         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
1355         return rc;
1356 }
1357
1358 static int lod_cache_parent_striping(const struct lu_env *env,
1359                                      struct lod_object *lp,
1360                                      umode_t child_mode)
1361 {
1362         int rc = 0;
1363         ENTRY;
1364
1365         rc = lod_load_striping(env, lp);
1366         if (rc != 0)
1367                 RETURN(rc);
1368
1369         if (!lp->ldo_striping_cached) {
1370                 /* we haven't tried to get default striping for
1371                  * the directory yet, let's cache it in the object */
1372                 rc = lod_cache_parent_lov_striping(env, lp);
1373                 if (rc != 0)
1374                         RETURN(rc);
1375         }
1376
1377         if (S_ISDIR(child_mode) && !lp->ldo_dir_striping_cached)
1378                 rc = lod_cache_parent_lmv_striping(env, lp);
1379
1380         RETURN(rc);
1381 }
1382
1383 /**
1384  * used to transfer default striping data to the object being created
1385  */
1386 static void lod_ah_init(const struct lu_env *env,
1387                         struct dt_allocation_hint *ah,
1388                         struct dt_object *parent,
1389                         struct dt_object *child,
1390                         umode_t child_mode)
1391 {
1392         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
1393         struct dt_object  *nextp = NULL;
1394         struct dt_object  *nextc;
1395         struct lod_object *lp = NULL;
1396         struct lod_object *lc;
1397         struct lov_desc   *desc;
1398         ENTRY;
1399
1400         LASSERT(child);
1401
1402         if (likely(parent)) {
1403                 nextp = dt_object_child(parent);
1404                 lp = lod_dt_obj(parent);
1405         }
1406
1407         nextc = dt_object_child(child);
1408         lc = lod_dt_obj(child);
1409
1410         LASSERT(lc->ldo_stripenr == 0);
1411         LASSERT(lc->ldo_stripe == NULL);
1412
1413         /*
1414          * local object may want some hints
1415          * in case of late striping creation, ->ah_init()
1416          * can be called with local object existing
1417          */
1418         if (!dt_object_exists(nextc) || dt_object_remote(nextc))
1419                 nextc->do_ops->do_ah_init(env, ah, dt_object_remote(nextp) ?
1420                                           NULL : nextp, nextc, child_mode);
1421
1422         if (S_ISDIR(child_mode)) {
1423                 int rc;
1424
1425                 if (lc->ldo_dir_stripe == NULL) {
1426                         OBD_ALLOC_PTR(lc->ldo_dir_stripe);
1427                         if (lc->ldo_dir_stripe == NULL)
1428                                 return;
1429                 }
1430
1431                 if (lp->ldo_dir_stripe == NULL) {
1432                         OBD_ALLOC_PTR(lp->ldo_dir_stripe);
1433                         if (lp->ldo_dir_stripe == NULL)
1434                                 return;
1435                 }
1436
1437                 rc = lod_cache_parent_striping(env, lp, child_mode);
1438                 if (rc != 0)
1439                         return;
1440
1441                 /* transfer defaults to new directory */
1442                 if (lp->ldo_striping_cached) {
1443                         if (lp->ldo_pool)
1444                                 lod_object_set_pool(lc, lp->ldo_pool);
1445                         lc->ldo_def_stripenr = lp->ldo_def_stripenr;
1446                         lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
1447                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
1448                         lc->ldo_striping_cached = 1;
1449                         lc->ldo_def_striping_set = 1;
1450                         CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
1451                                (int)lc->ldo_def_stripe_size,
1452                                (int)lc->ldo_def_stripe_offset,
1453                                (int)lc->ldo_def_stripenr);
1454                 }
1455
1456                 /* transfer dir defaults to new directory */
1457                 if (lp->ldo_dir_striping_cached) {
1458                         lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
1459                         lc->ldo_dir_def_stripe_offset =
1460                                                   lp->ldo_dir_def_stripe_offset;
1461                         lc->ldo_dir_def_hash_type =
1462                                                   lp->ldo_dir_def_hash_type;
1463                         lc->ldo_dir_striping_cached = 1;
1464                         lc->ldo_dir_def_striping_set = 1;
1465                         CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
1466                                (int)lc->ldo_dir_def_stripenr,
1467                                (int)lc->ldo_dir_def_stripe_offset,
1468                                lc->ldo_dir_def_hash_type);
1469                 }
1470
1471                 /* If the directory is specified with certain stripes */
1472                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
1473                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
1474                         int rc;
1475
1476                         rc = lod_verify_md_striping(d, lum1);
1477                         if (rc == 0 &&
1478                                 le32_to_cpu(lum1->lum_stripe_count) > 1) {
1479                                 /* Directory will be striped only if
1480                                  * stripe_count > 1 */
1481                                 lc->ldo_stripenr =
1482                                         le32_to_cpu(lum1->lum_stripe_count) - 1;
1483                                 lc->ldo_dir_stripe_offset =
1484                                         le32_to_cpu(lum1->lum_stripe_offset);
1485                                 lc->ldo_dir_hash_type =
1486                                         le32_to_cpu(lum1->lum_hash_type);
1487                                 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
1488                                        lc->ldo_stripenr,
1489                                        (int)lc->ldo_dir_stripe_offset);
1490                         }
1491                 } else if (lp->ldo_dir_def_striping_set) {
1492                         /* If there are default dir stripe from parent */
1493                         lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
1494                         lc->ldo_dir_stripe_offset =
1495                                         lp->ldo_dir_def_stripe_offset;
1496                         lc->ldo_dir_hash_type =
1497                                         lp->ldo_dir_def_hash_type;
1498                         CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
1499                                lc->ldo_stripenr,
1500                                (int)lc->ldo_dir_stripe_offset);
1501                 } else {
1502                         /* set default stripe for this directory */
1503                         lc->ldo_stripenr = 0;
1504                         lc->ldo_dir_stripe_offset = -1;
1505                 }
1506
1507                 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
1508                        lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
1509
1510                 goto out;
1511         }
1512
1513         /*
1514          * if object is going to be striped over OSTs, transfer default
1515          * striping information to the child, so that we can use it
1516          * during declaration and creation
1517          */
1518         if (!lod_object_will_be_striped(S_ISREG(child_mode),
1519                                         lu_object_fid(&child->do_lu)))
1520                 goto out;
1521         /*
1522          * try from the parent
1523          */
1524         if (likely(parent)) {
1525                 lod_cache_parent_striping(env, lp, child_mode);
1526
1527                 lc->ldo_def_stripe_offset = (__u16) -1;
1528
1529                 if (lp->ldo_def_striping_set) {
1530                         if (lp->ldo_pool)
1531                                 lod_object_set_pool(lc, lp->ldo_pool);
1532                         lc->ldo_stripenr = lp->ldo_def_stripenr;
1533                         lc->ldo_stripe_size = lp->ldo_def_stripe_size;
1534                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
1535                         CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
1536                                lc->ldo_stripenr, lc->ldo_stripe_size,
1537                                lp->ldo_pool ? lp->ldo_pool : "");
1538                 }
1539         }
1540
1541         /*
1542          * if the parent doesn't provide with specific pattern, grab fs-wide one
1543          */
1544         desc = &d->lod_desc;
1545         if (lc->ldo_stripenr == 0)
1546                 lc->ldo_stripenr = desc->ld_default_stripe_count;
1547         if (lc->ldo_stripe_size == 0)
1548                 lc->ldo_stripe_size = desc->ld_default_stripe_size;
1549         CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
1550                lc->ldo_stripenr, lc->ldo_stripe_size,
1551                lc->ldo_pool ? lc->ldo_pool : "");
1552
1553 out:
1554         /* we do not cache stripe information for slave stripe, see
1555          * lod_xattr_set_lov_on_dir */
1556         if (lp != NULL && lp->ldo_dir_slave_stripe)
1557                 lod_lov_stripe_cache_clear(lp);
1558
1559         EXIT;
1560 }
1561
1562 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
1563 /*
1564  * this function handles a special case when truncate was done
1565  * on a stripeless object and now striping is being created
1566  * we can't lose that size, so we have to propagate it to newly
1567  * created object
1568  */
1569 static int lod_declare_init_size(const struct lu_env *env,
1570                                  struct dt_object *dt, struct thandle *th)
1571 {
1572         struct dt_object   *next = dt_object_child(dt);
1573         struct lod_object  *lo = lod_dt_obj(dt);
1574         struct lu_attr     *attr = &lod_env_info(env)->lti_attr;
1575         uint64_t            size, offs;
1576         int                 rc, stripe;
1577         ENTRY;
1578
1579         /* XXX: we support the simplest (RAID0) striping so far */
1580         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
1581         LASSERT(lo->ldo_stripe_size > 0);
1582
1583         rc = dt_attr_get(env, next, attr, BYPASS_CAPA);
1584         LASSERT(attr->la_valid & LA_SIZE);
1585         if (rc)
1586                 RETURN(rc);
1587
1588         size = attr->la_size;
1589         if (size == 0)
1590                 RETURN(0);
1591
1592         /* ll_do_div64(a, b) returns a % b, and a = a / b */
1593         ll_do_div64(size, (__u64) lo->ldo_stripe_size);
1594         stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
1595
1596         size = size * lo->ldo_stripe_size;
1597         offs = attr->la_size;
1598         size += ll_do_div64(offs, lo->ldo_stripe_size);
1599
1600         attr->la_valid = LA_SIZE;
1601         attr->la_size = size;
1602
1603         rc = dt_declare_attr_set(env, lo->ldo_stripe[stripe], attr, th);
1604
1605         RETURN(rc);
1606 }
1607
1608 /**
1609  * Create declaration of striped object
1610  */
1611 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
1612                                struct lu_attr *attr,
1613                                const struct lu_buf *lovea, struct thandle *th)
1614 {
1615         struct lod_thread_info  *info = lod_env_info(env);
1616         struct dt_object        *next = dt_object_child(dt);
1617         struct lod_object       *lo = lod_dt_obj(dt);
1618         int                      rc;
1619         ENTRY;
1620
1621         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
1622                 /* failed to create striping, let's reset
1623                  * config so that others don't get confused */
1624                 lod_object_free_striping(env, lo);
1625                 GOTO(out, rc = -ENOMEM);
1626         }
1627
1628         /* choose OST and generate appropriate objects */
1629         rc = lod_qos_prep_create(env, lo, attr, lovea, th);
1630         if (rc) {
1631                 /* failed to create striping, let's reset
1632                  * config so that others don't get confused */
1633                 lod_object_free_striping(env, lo);
1634                 GOTO(out, rc);
1635         }
1636
1637         /*
1638          * declare storage for striping data
1639          */
1640         info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
1641                                 lo->ldo_pool ?  LOV_MAGIC_V3 : LOV_MAGIC_V1);
1642         rc = dt_declare_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV,
1643                                   0, th);
1644         if (rc)
1645                 GOTO(out, rc);
1646
1647         /*
1648          * if striping is created with local object's size > 0,
1649          * we have to propagate this size to specific object
1650          * the case is possible only when local object was created previously
1651          */
1652         if (dt_object_exists(next))
1653                 rc = lod_declare_init_size(env, dt, th);
1654
1655 out:
1656         RETURN(rc);
1657 }
1658
1659 int lod_dir_striping_create_internal(const struct lu_env *env,
1660                                      struct dt_object *dt,
1661                                      struct lu_attr *attr,
1662                                      const struct dt_object_format *dof,
1663                                      struct thandle *th,
1664                                      bool declare)
1665 {
1666         struct lod_thread_info  *info = lod_env_info(env);
1667         struct dt_object        *next = dt_object_child(dt);
1668         struct lod_object       *lo = lod_dt_obj(dt);
1669         int                     rc;
1670         ENTRY;
1671
1672         if (lo->ldo_dir_def_striping_set &&
1673             !LMVEA_DELETE_VALUES(lo->ldo_stripenr,
1674                                  lo->ldo_dir_stripe_offset)) {
1675                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
1676                 int stripe_count = lo->ldo_stripenr + 1;
1677
1678                 if (info->lti_ea_store_size < sizeof(*v1)) {
1679                         rc = lod_ea_store_resize(info, sizeof(*v1));
1680                         if (rc != 0)
1681                                 RETURN(rc);
1682                         v1 = info->lti_ea_store;
1683                 }
1684
1685                 memset(v1, 0, sizeof(*v1));
1686                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
1687                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
1688                 v1->lum_stripe_offset =
1689                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
1690
1691                 info->lti_buf.lb_buf = v1;
1692                 info->lti_buf.lb_len = sizeof(*v1);
1693
1694                 if (declare)
1695                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
1696                                                        &info->lti_buf, th);
1697                 else
1698                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
1699                                                XATTR_NAME_LMV, 0, th,
1700                                                BYPASS_CAPA);
1701                 if (rc != 0)
1702                         RETURN(rc);
1703         }
1704
1705         /* Transfer default LMV striping from the parent */
1706         if (lo->ldo_dir_striping_cached &&
1707             !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
1708                                  lo->ldo_dir_def_stripe_offset)) {
1709                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
1710                 int def_stripe_count = lo->ldo_dir_def_stripenr + 1;
1711
1712                 if (info->lti_ea_store_size < sizeof(*v1)) {
1713                         rc = lod_ea_store_resize(info, sizeof(*v1));
1714                         if (rc != 0)
1715                                 RETURN(rc);
1716                         v1 = info->lti_ea_store;
1717                 }
1718
1719                 memset(v1, 0, sizeof(*v1));
1720                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
1721                 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
1722                 v1->lum_stripe_offset =
1723                                 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
1724                 v1->lum_hash_type =
1725                                 cpu_to_le32(lo->ldo_dir_def_hash_type);
1726
1727                 info->lti_buf.lb_buf = v1;
1728                 info->lti_buf.lb_len = sizeof(*v1);
1729                 if (declare)
1730                         rc = dt_declare_xattr_set(env, next, &info->lti_buf,
1731                                                   XATTR_NAME_DEFAULT_LMV, 0,
1732                                                   th);
1733                 else
1734                         rc = dt_xattr_set(env, next, &info->lti_buf,
1735                                            XATTR_NAME_DEFAULT_LMV, 0, th,
1736                                            BYPASS_CAPA);
1737                 if (rc != 0)
1738                         RETURN(rc);
1739         }
1740
1741         /* Transfer default LOV striping from the parent */
1742         if (lo->ldo_striping_cached &&
1743             !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
1744                                  lo->ldo_def_stripenr,
1745                                  lo->ldo_def_stripe_offset)) {
1746                 struct lov_user_md_v3 *v3 = info->lti_ea_store;
1747
1748                 if (info->lti_ea_store_size < sizeof(*v3)) {
1749                         rc = lod_ea_store_resize(info, sizeof(*v3));
1750                         if (rc != 0)
1751                                 RETURN(rc);
1752                         v3 = info->lti_ea_store;
1753                 }
1754
1755                 memset(v3, 0, sizeof(*v3));
1756                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
1757                 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
1758                 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
1759                 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
1760                 if (lo->ldo_pool)
1761                         strncpy(v3->lmm_pool_name, lo->ldo_pool,
1762                                 LOV_MAXPOOLNAME);
1763
1764                 info->lti_buf.lb_buf = v3;
1765                 info->lti_buf.lb_len = sizeof(*v3);
1766
1767                 if (declare)
1768                         rc = dt_declare_xattr_set(env, next, &info->lti_buf,
1769                                                   XATTR_NAME_LOV, 0, th);
1770                 else
1771                         rc = dt_xattr_set(env, next, &info->lti_buf,
1772                                           XATTR_NAME_LOV, 0, th,
1773                                           BYPASS_CAPA);
1774                 if (rc != 0)
1775                         RETURN(rc);
1776         }
1777
1778         RETURN(0);
1779 }
1780
1781 static int lod_declare_dir_striping_create(const struct lu_env *env,
1782                                            struct dt_object *dt,
1783                                            struct lu_attr *attr,
1784                                            struct dt_object_format *dof,
1785                                            struct thandle *th)
1786 {
1787         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
1788 }
1789
1790 static int lod_dir_striping_create(const struct lu_env *env,
1791                                    struct dt_object *dt,
1792                                    struct lu_attr *attr,
1793                                    struct dt_object_format *dof,
1794                                    struct thandle *th)
1795 {
1796         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
1797 }
1798
1799 static int lod_declare_object_create(const struct lu_env *env,
1800                                      struct dt_object *dt,
1801                                      struct lu_attr *attr,
1802                                      struct dt_allocation_hint *hint,
1803                                      struct dt_object_format *dof,
1804                                      struct thandle *th)
1805 {
1806         struct dt_object   *next = dt_object_child(dt);
1807         struct lod_object  *lo = lod_dt_obj(dt);
1808         int                 rc;
1809         ENTRY;
1810
1811         LASSERT(dof);
1812         LASSERT(attr);
1813         LASSERT(th);
1814
1815         /*
1816          * first of all, we declare creation of local object
1817          */
1818         rc = dt_declare_create(env, next, attr, hint, dof, th);
1819         if (rc)
1820                 GOTO(out, rc);
1821
1822         if (dof->dof_type == DFT_SYM)
1823                 dt->do_body_ops = &lod_body_lnk_ops;
1824
1825         /*
1826          * it's lod_ah_init() who has decided the object will striped
1827          */
1828         if (dof->dof_type == DFT_REGULAR) {
1829                 /* callers don't want stripes */
1830                 /* XXX: all tricky interactions with ->ah_make_hint() decided
1831                  * to use striping, then ->declare_create() behaving differently
1832                  * should be cleaned */
1833                 if (dof->u.dof_reg.striped == 0)
1834                         lo->ldo_stripenr = 0;
1835                 if (lo->ldo_stripenr > 0)
1836                         rc = lod_declare_striped_object(env, dt, attr,
1837                                                         NULL, th);
1838         } else if (dof->dof_type == DFT_DIR) {
1839                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
1840         }
1841 out:
1842         RETURN(rc);
1843 }
1844
1845 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
1846                         struct lu_attr *attr, struct dt_object_format *dof,
1847                         struct thandle *th)
1848 {
1849         struct lod_object *lo = lod_dt_obj(dt);
1850         int                rc = 0, i;
1851         ENTRY;
1852
1853         LASSERT(lo->ldo_striping_cached == 0);
1854
1855         /* create all underlying objects */
1856         for (i = 0; i < lo->ldo_stripenr; i++) {
1857                 LASSERT(lo->ldo_stripe[i]);
1858                 rc = dt_create(env, lo->ldo_stripe[i], attr, NULL, dof, th);
1859
1860                 if (rc)
1861                         break;
1862         }
1863         if (rc == 0)
1864                 rc = lod_generate_and_set_lovea(env, lo, th);
1865
1866         RETURN(rc);
1867 }
1868
1869 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
1870                              struct lu_attr *attr,
1871                              struct dt_allocation_hint *hint,
1872                              struct dt_object_format *dof, struct thandle *th)
1873 {
1874         struct dt_object   *next = dt_object_child(dt);
1875         struct lod_object  *lo = lod_dt_obj(dt);
1876         int                 rc;
1877         ENTRY;
1878
1879         /* create local object */
1880         rc = dt_create(env, next, attr, hint, dof, th);
1881
1882         if (rc == 0) {
1883                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
1884                         rc = lod_dir_striping_create(env, dt, attr, dof, th);
1885                 else if (lo->ldo_stripe && dof->u.dof_reg.striped != 0)
1886                         rc = lod_striping_create(env, dt, attr, dof, th);
1887         }
1888
1889         RETURN(rc);
1890 }
1891
1892 static int lod_declare_object_destroy(const struct lu_env *env,
1893                                       struct dt_object *dt,
1894                                       struct thandle *th)
1895 {
1896         struct dt_object   *next = dt_object_child(dt);
1897         struct lod_object  *lo = lod_dt_obj(dt);
1898         int                 rc, i;
1899         ENTRY;
1900
1901         /*
1902          * we declare destroy for the local object
1903          */
1904         rc = dt_declare_destroy(env, next, th);
1905         if (rc)
1906                 RETURN(rc);
1907
1908         /*
1909          * load striping information, notice we don't do this when object
1910          * is being initialized as we don't need this information till
1911          * few specific cases like destroy, chown
1912          */
1913         rc = lod_load_striping(env, lo);
1914         if (rc)
1915                 RETURN(rc);
1916
1917         /* declare destroy for all underlying objects */
1918         for (i = 0; i < lo->ldo_stripenr; i++) {
1919                 LASSERT(lo->ldo_stripe[i]);
1920                 rc = dt_declare_destroy(env, lo->ldo_stripe[i], th);
1921
1922                 if (rc)
1923                         break;
1924         }
1925
1926         RETURN(rc);
1927 }
1928
1929 static int lod_object_destroy(const struct lu_env *env,
1930                 struct dt_object *dt, struct thandle *th)
1931 {
1932         struct dt_object  *next = dt_object_child(dt);
1933         struct lod_object *lo = lod_dt_obj(dt);
1934         int                rc, i;
1935         ENTRY;
1936
1937         /* destroy local object */
1938         rc = dt_destroy(env, next, th);
1939         if (rc)
1940                 RETURN(rc);
1941
1942         /* destroy all underlying objects */
1943         for (i = 0; i < lo->ldo_stripenr; i++) {
1944                 LASSERT(lo->ldo_stripe[i]);
1945                 rc = dt_destroy(env, lo->ldo_stripe[i], th);
1946                 if (rc)
1947                         break;
1948         }
1949
1950         RETURN(rc);
1951 }
1952
1953 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
1954                          const struct dt_index_features *feat)
1955 {
1956         struct dt_object *next = dt_object_child(dt);
1957         int               rc;
1958         ENTRY;
1959
1960         LASSERT(next->do_ops);
1961         LASSERT(next->do_ops->do_index_try);
1962
1963         rc = next->do_ops->do_index_try(env, next, feat);
1964         if (next->do_index_ops && dt->do_index_ops == NULL)
1965                 dt->do_index_ops = &lod_index_ops;
1966
1967         RETURN(rc);
1968 }
1969
1970 static int lod_declare_ref_add(const struct lu_env *env,
1971                                struct dt_object *dt, struct thandle *th)
1972 {
1973         return dt_declare_ref_add(env, dt_object_child(dt), th);
1974 }
1975
1976 static int lod_ref_add(const struct lu_env *env,
1977                        struct dt_object *dt, struct thandle *th)
1978 {
1979         return dt_ref_add(env, dt_object_child(dt), th);
1980 }
1981
1982 static int lod_declare_ref_del(const struct lu_env *env,
1983                                struct dt_object *dt, struct thandle *th)
1984 {
1985         return dt_declare_ref_del(env, dt_object_child(dt), th);
1986 }
1987
1988 static int lod_ref_del(const struct lu_env *env,
1989                        struct dt_object *dt, struct thandle *th)
1990 {
1991         return dt_ref_del(env, dt_object_child(dt), th);
1992 }
1993
1994 static struct obd_capa *lod_capa_get(const struct lu_env *env,
1995                                      struct dt_object *dt,
1996                                      struct lustre_capa *old, __u64 opc)
1997 {
1998         return dt_capa_get(env, dt_object_child(dt), old, opc);
1999 }
2000
2001 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt)
2002 {
2003         return dt_object_sync(env, dt_object_child(dt));
2004 }
2005
2006 static int lod_object_lock(const struct lu_env *env,
2007                            struct dt_object *dt, struct lustre_handle *lh,
2008                            struct ldlm_enqueue_info *einfo,
2009                            void *policy)
2010 {
2011         struct dt_object   *next = dt_object_child(dt);
2012         int              rc;
2013         ENTRY;
2014
2015         /*
2016          * declare setattr on the local object
2017          */
2018         rc = dt_object_lock(env, next, lh, einfo, policy);
2019
2020         RETURN(rc);
2021 }
2022
2023 struct dt_object_operations lod_obj_ops = {
2024         .do_read_lock           = lod_object_read_lock,
2025         .do_write_lock          = lod_object_write_lock,
2026         .do_read_unlock         = lod_object_read_unlock,
2027         .do_write_unlock        = lod_object_write_unlock,
2028         .do_write_locked        = lod_object_write_locked,
2029         .do_attr_get            = lod_attr_get,
2030         .do_declare_attr_set    = lod_declare_attr_set,
2031         .do_attr_set            = lod_attr_set,
2032         .do_xattr_get           = lod_xattr_get,
2033         .do_declare_xattr_set   = lod_declare_xattr_set,
2034         .do_xattr_set           = lod_xattr_set,
2035         .do_declare_xattr_del   = lod_declare_xattr_del,
2036         .do_xattr_del           = lod_xattr_del,
2037         .do_xattr_list          = lod_xattr_list,
2038         .do_ah_init             = lod_ah_init,
2039         .do_declare_create      = lod_declare_object_create,
2040         .do_create              = lod_object_create,
2041         .do_declare_destroy     = lod_declare_object_destroy,
2042         .do_destroy             = lod_object_destroy,
2043         .do_index_try           = lod_index_try,
2044         .do_declare_ref_add     = lod_declare_ref_add,
2045         .do_ref_add             = lod_ref_add,
2046         .do_declare_ref_del     = lod_declare_ref_del,
2047         .do_ref_del             = lod_ref_del,
2048         .do_capa_get            = lod_capa_get,
2049         .do_object_sync         = lod_object_sync,
2050         .do_object_lock         = lod_object_lock,
2051 };
2052
2053 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
2054                         struct lu_buf *buf, loff_t *pos,
2055                         struct lustre_capa *capa)
2056 {
2057         struct dt_object *next = dt_object_child(dt);
2058         return next->do_body_ops->dbo_read(env, next, buf, pos, capa);
2059 }
2060
2061 static ssize_t lod_declare_write(const struct lu_env *env,
2062                                  struct dt_object *dt,
2063                                  const loff_t size, loff_t pos,
2064                                  struct thandle *th)
2065 {
2066         return dt_declare_record_write(env, dt_object_child(dt),
2067                                        size, pos, th);
2068 }
2069
2070 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
2071                          const struct lu_buf *buf, loff_t *pos,
2072                          struct thandle *th, struct lustre_capa *capa, int iq)
2073 {
2074         struct dt_object *next = dt_object_child(dt);
2075         LASSERT(next);
2076         return next->do_body_ops->dbo_write(env, next, buf, pos, th, capa, iq);
2077 }
2078
2079 static const struct dt_body_operations lod_body_lnk_ops = {
2080         .dbo_read               = lod_read,
2081         .dbo_declare_write      = lod_declare_write,
2082         .dbo_write              = lod_write
2083 };
2084
2085 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
2086                            const struct lu_object_conf *conf)
2087 {
2088         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
2089         struct lu_device        *cdev   = NULL;
2090         struct lu_object        *cobj;
2091         struct lod_tgt_descs    *ltd    = NULL;
2092         struct lod_tgt_desc     *tgt;
2093         mdsno_t                  idx    = 0;
2094         int                      type   = LU_SEQ_RANGE_ANY;
2095         int                      rc;
2096         ENTRY;
2097
2098         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
2099         if (rc != 0)
2100                 RETURN(rc);
2101
2102         if (type == LU_SEQ_RANGE_MDT &&
2103             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
2104                 cdev = &lod->lod_child->dd_lu_dev;
2105         } else if (type == LU_SEQ_RANGE_MDT) {
2106                 ltd = &lod->lod_mdt_descs;
2107                 lod_getref(ltd);
2108         } else if (type == LU_SEQ_RANGE_OST) {
2109                 ltd = &lod->lod_ost_descs;
2110                 lod_getref(ltd);
2111         } else {
2112                 LBUG();
2113         }
2114
2115         if (ltd != NULL) {
2116                 if (ltd->ltd_tgts_size > idx &&
2117                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
2118                         tgt = LTD_TGT(ltd, idx);
2119
2120                         LASSERT(tgt != NULL);
2121                         LASSERT(tgt->ltd_tgt != NULL);
2122
2123                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
2124                 }
2125                 lod_putref(lod, ltd);
2126         }
2127
2128         if (unlikely(cdev == NULL))
2129                 RETURN(-ENOENT);
2130
2131         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
2132         if (unlikely(cobj == NULL))
2133                 RETURN(-ENOMEM);
2134
2135         lu_object_add(lo, cobj);
2136
2137         RETURN(0);
2138 }
2139
2140 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
2141 {
2142         int i;
2143
2144         if (lo->ldo_dir_stripe != NULL) {
2145                 OBD_FREE_PTR(lo->ldo_dir_stripe);
2146                 lo->ldo_dir_stripe = NULL;
2147         }
2148
2149         if (lo->ldo_stripe) {
2150                 LASSERT(lo->ldo_stripes_allocated > 0);
2151
2152                 for (i = 0; i < lo->ldo_stripenr; i++) {
2153                         if (lo->ldo_stripe[i])
2154                                 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
2155                 }
2156
2157                 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
2158                 OBD_FREE(lo->ldo_stripe, i);
2159                 lo->ldo_stripe = NULL;
2160                 lo->ldo_stripes_allocated = 0;
2161         }
2162         lo->ldo_stripenr = 0;
2163         lo->ldo_pattern = 0;
2164 }
2165
2166 /*
2167  * ->start is called once all slices are initialized, including header's
2168  * cache for mode (object type). using the type we can initialize ops
2169  */
2170 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
2171 {
2172         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT))
2173                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
2174         return 0;
2175 }
2176
2177 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
2178 {
2179         struct lod_object *mo = lu2lod_obj(o);
2180
2181         /*
2182          * release all underlying object pinned
2183          */
2184
2185         lod_object_free_striping(env, mo);
2186
2187         lod_object_set_pool(mo, NULL);
2188
2189         lu_object_fini(o);
2190         OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
2191 }
2192
2193 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
2194 {
2195         /* XXX: shouldn't we release everything here in case if object
2196          * creation failed before? */
2197 }
2198
2199 static int lod_object_print(const struct lu_env *env, void *cookie,
2200                             lu_printer_t p, const struct lu_object *l)
2201 {
2202         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
2203
2204         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
2205 }
2206
2207 struct lu_object_operations lod_lu_obj_ops = {
2208         .loo_object_init        = lod_object_init,
2209         .loo_object_start       = lod_object_start,
2210         .loo_object_free        = lod_object_free,
2211         .loo_object_release     = lod_object_release,
2212         .loo_object_print       = lod_object_print,
2213 };