Whamcloud - gitweb
LU-2675 lod: remove lov and lod stuff from obd.h
[fs/lustre-release.git] / lustre / lod / lod_lov.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_lov.c
30  *
31  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com> 
32  */
33
34 #define DEBUG_SUBSYSTEM S_MDS
35
36 #include <obd_class.h>
37 #include <obd_lov.h>
38
39 #include "lod_internal.h"
40
41 /*
42  * Keep a refcount of lod->ltd_tgts usage to prevent racing with
43  * addition/deletion. Any function that expects lov_tgts to remain stationary
44  * must take a ref.
45  *
46  * \param lod - is the lod device from which we want to grab a reference
47  */
48 void lod_getref(struct lod_tgt_descs *ltd)
49 {
50         down_read(&ltd->ltd_rw_sem);
51         mutex_lock(&ltd->ltd_mutex);
52         ltd->ltd_refcount++;
53         mutex_unlock(&ltd->ltd_mutex);
54 }
55
56 /*
57  * Companion of lod_getref() to release a reference on the lod table.
58  * If this is the last reference and the ost entry was scheduled for deletion,
59  * the descriptor is removed from the array.
60  *
61  * \param lod - is the lod device from which we release a reference
62  */
63 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd)
64 {
65         mutex_lock(&ltd->ltd_mutex);
66         ltd->ltd_refcount--;
67         if (ltd->ltd_refcount == 0 && ltd->ltd_death_row) {
68                 struct lod_tgt_desc *tgt_desc, *tmp;
69                 int                  idx;
70                 CFS_LIST_HEAD(kill);
71
72                 CDEBUG(D_CONFIG, "destroying %d ltd desc\n",
73                        ltd->ltd_death_row);
74
75                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx) {
76                         tgt_desc = LTD_TGT(ltd, idx);
77                         LASSERT(tgt_desc);
78
79                         if (!tgt_desc->ltd_reap)
80                                 continue;
81
82                         cfs_list_add(&tgt_desc->ltd_kill, &kill);
83                         LTD_TGT(ltd, idx) = NULL;
84                         /*FIXME: only support ost pool for now */
85                         if (ltd == &lod->lod_ost_descs) {
86                                 lod_ost_pool_remove(&lod->lod_pool_info, idx);
87                                 if (tgt_desc->ltd_active)
88                                         lod->lod_desc.ld_active_tgt_count--;
89                         }
90                         ltd->ltd_tgtnr--;
91                         cfs_bitmap_clear(ltd->ltd_tgt_bitmap, idx);
92                         ltd->ltd_death_row--;
93                 }
94                 mutex_unlock(&ltd->ltd_mutex);
95                 up_read(&ltd->ltd_rw_sem);
96
97                 cfs_list_for_each_entry_safe(tgt_desc, tmp, &kill, ltd_kill) {
98                         int rc;
99                         cfs_list_del(&tgt_desc->ltd_kill);
100                         if (ltd == &lod->lod_ost_descs) {
101                                 /* remove from QoS structures */
102                                 rc = qos_del_tgt(lod, tgt_desc);
103                                 if (rc)
104                                         CERROR("%s: qos_del_tgt(%s) failed:"
105                                                "rc = %d\n",
106                                                lod2obd(lod)->obd_name,
107                                               obd_uuid2str(&tgt_desc->ltd_uuid),
108                                                rc);
109                         }
110                         rc = obd_disconnect(tgt_desc->ltd_exp);
111                         if (rc)
112                                 CERROR("%s: failed to disconnect %s: rc = %d\n",
113                                        lod2obd(lod)->obd_name,
114                                        obd_uuid2str(&tgt_desc->ltd_uuid), rc);
115                         OBD_FREE_PTR(tgt_desc);
116                 }
117         } else {
118                 mutex_unlock(&ltd->ltd_mutex);
119                 up_read(&ltd->ltd_rw_sem);
120         }
121 }
122
123 static int ltd_bitmap_resize(struct lod_tgt_descs *ltd, __u32 newsize)
124 {
125         cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
126         int           rc = 0;
127         ENTRY;
128
129         /* grab write reference on the lod. Relocating the array requires
130          * exclusive access */
131
132         down_write(&ltd->ltd_rw_sem);
133         if (newsize <= ltd->ltd_tgts_size)
134                 /* someone else has already resize the array */
135                 GOTO(out, rc = 0);
136
137         /* allocate new bitmap */
138         new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
139         if (!new_bitmap)
140                 GOTO(out, rc = -ENOMEM);
141
142         if (ltd->ltd_tgts_size > 0) {
143                 /* the bitmap already exists, we need
144                  * to copy data from old one */
145                 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
146                 old_bitmap = ltd->ltd_tgt_bitmap;
147         }
148
149         ltd->ltd_tgts_size  = newsize;
150         ltd->ltd_tgt_bitmap = new_bitmap;
151
152         if (old_bitmap)
153                 CFS_FREE_BITMAP(old_bitmap);
154
155         CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
156
157         EXIT;
158 out:
159         up_write(&ltd->ltd_rw_sem);
160         return rc;
161 }
162
163 /*
164  * Connect LOD to a new OSP and add it to the device table.
165  *
166  * \param env - is the environment passed by the caller
167  * \param lod - is the LOD device to be connected to the new OSP
168  * \param osp - is the name of OSP device name about to be added
169  * \param index - is the OSP index
170  * \param gen - is the generation number
171  * \param tgt_index - is the group of the OSP.
172  * \param type - is the type of device (mdc or osc)
173  */
174 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
175                    char *osp, unsigned index, unsigned gen, int tgt_index,
176                    char *type, int active)
177 {
178         struct obd_connect_data *data = NULL;
179         struct obd_export       *exp = NULL;
180         struct obd_device       *obd;
181         struct lu_device        *ldev;
182         struct dt_device        *d;
183         int                      rc;
184         struct lod_tgt_desc     *tgt_desc;
185         struct lod_tgt_descs    *ltd;
186         struct obd_uuid         obd_uuid;
187         ENTRY;
188
189         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
190
191         if (gen <= 0) {
192                 CERROR("request to add OBD %s with invalid generation: %d\n",
193                        osp, gen);
194                 RETURN(-EINVAL);
195         }
196
197         obd_str2uuid(&obd_uuid, osp);
198
199         obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
200                                 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
201         if (obd == NULL) {
202                 CERROR("can't find %s device\n", osp);
203                 RETURN(-EINVAL);
204         }
205
206         OBD_ALLOC_PTR(data);
207         if (data == NULL)
208                 RETURN(-ENOMEM);
209
210         data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
211         data->ocd_version = LUSTRE_VERSION_CODE;
212         data->ocd_index = index;
213
214         if (strcmp(LUSTRE_OSC_NAME, type) == 0) {
215                 data->ocd_connect_flags |= OBD_CONNECT_AT |
216                                            OBD_CONNECT_FULL20 |
217                                            OBD_CONNECT_INDEX |
218 #ifdef HAVE_LRU_RESIZE_SUPPORT
219                                            OBD_CONNECT_LRU_RESIZE |
220 #endif
221                                            OBD_CONNECT_MDS |
222                                            OBD_CONNECT_OSS_CAPA |
223                                            OBD_CONNECT_REQPORTAL |
224                                            OBD_CONNECT_SKIP_ORPHAN |
225                                            OBD_CONNECT_FID |
226                                            OBD_CONNECT_LVB_TYPE |
227                                            OBD_CONNECT_VERSION |
228                                            OBD_CONNECT_PINGLESS;
229
230                 data->ocd_group = tgt_index;
231                 ltd = &lod->lod_ost_descs;
232         } else {
233                 struct obd_import *imp = obd->u.cli.cl_import;
234
235                 data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
236                 data->ocd_connect_flags |= OBD_CONNECT_ACL |
237                                            OBD_CONNECT_MDS_CAPA |
238                                            OBD_CONNECT_OSS_CAPA |
239                                            OBD_CONNECT_IBITS |
240                                            OBD_CONNECT_MDS_MDS |
241                                            OBD_CONNECT_FID |
242                                            OBD_CONNECT_AT |
243                                            OBD_CONNECT_FULL20;
244                 /* XXX set MDS-MDS flags, remove this when running this
245                  * on client*/
246                 data->ocd_connect_flags |= OBD_CONNECT_MDS_MDS;
247                 spin_lock(&imp->imp_lock);
248                 imp->imp_server_timeout = 1;
249                 spin_unlock(&imp->imp_lock);
250                 imp->imp_client->cli_request_portal = OUT_PORTAL;
251                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
252                       obd->obd_name);
253                 ltd = &lod->lod_mdt_descs;
254         }
255
256         rc = obd_connect(env, &exp, obd, &obd->obd_uuid, data, NULL);
257         OBD_FREE_PTR(data);
258         if (rc) {
259                 CERROR("%s: cannot connect to next dev %s (%d)\n",
260                        obd->obd_name, osp, rc);
261                 GOTO(out_free, rc);
262         }
263
264         LASSERT(obd->obd_lu_dev);
265         LASSERT(obd->obd_lu_dev->ld_site == lod->lod_dt_dev.dd_lu_dev.ld_site);
266
267         ldev = obd->obd_lu_dev;
268         d = lu2dt_dev(ldev);
269
270         /* Allocate ost descriptor and fill it */
271         OBD_ALLOC_PTR(tgt_desc);
272         if (!tgt_desc)
273                 GOTO(out_conn, rc = -ENOMEM);
274
275         tgt_desc->ltd_tgt    = d;
276         tgt_desc->ltd_exp    = exp;
277         tgt_desc->ltd_uuid   = obd->u.cli.cl_target_uuid;
278         tgt_desc->ltd_gen    = gen;
279         tgt_desc->ltd_index  = index;
280         tgt_desc->ltd_active = active;
281
282         lod_getref(ltd);
283         if (index >= ltd->ltd_tgts_size) {
284                 /* we have to increase the size of the lod_osts array */
285                 __u32  newsize;
286
287                 newsize = max(ltd->ltd_tgts_size, (__u32)2);
288                 while (newsize < index + 1)
289                         newsize = newsize << 1;
290
291                 /* lod_bitmap_resize() needs lod_rw_sem
292                  * which we hold with th reference */
293                 lod_putref(lod, ltd);
294
295                 rc = ltd_bitmap_resize(ltd, newsize);
296                 if (rc)
297                         GOTO(out_desc, rc);
298
299                 lod_getref(ltd);
300         }
301
302         mutex_lock(&ltd->ltd_mutex);
303         if (cfs_bitmap_check(ltd->ltd_tgt_bitmap, index)) {
304                 CERROR("%s: device %d is registered already\n", obd->obd_name,
305                        index);
306                 GOTO(out_mutex, rc = -EEXIST);
307         }
308
309         if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
310                 OBD_ALLOC_PTR(ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK]);
311                 if (ltd->ltd_tgt_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
312                         CERROR("can't allocate index to add %s\n",
313                                obd->obd_name);
314                         GOTO(out_mutex, rc = -ENOMEM);
315                 }
316         }
317
318         if (!strcmp(LUSTRE_OSC_NAME, type)) {
319                 /* pool and qos are not supported for MDS stack yet */
320                 rc = lod_ost_pool_add(&lod->lod_pool_info, index,
321                                       lod->lod_osts_size);
322                 if (rc) {
323                         CERROR("%s: can't set up pool, failed with %d\n",
324                                obd->obd_name, rc);
325                         GOTO(out_mutex, rc);
326                 }
327
328                 rc = qos_add_tgt(lod, tgt_desc);
329                 if (rc) {
330                         CERROR("%s: qos_add_tgt failed with %d\n",
331                                 obd->obd_name, rc);
332                         GOTO(out_pool, rc);
333                 }
334
335                 /* The new OST is now a full citizen */
336                 if (index >= lod->lod_desc.ld_tgt_count)
337                         lod->lod_desc.ld_tgt_count = index + 1;
338                 if (active)
339                         lod->lod_desc.ld_active_tgt_count++;
340         }
341
342         LTD_TGT(ltd, index) = tgt_desc;
343         cfs_bitmap_set(ltd->ltd_tgt_bitmap, index);
344         ltd->ltd_tgtnr++;
345         mutex_unlock(&ltd->ltd_mutex);
346         lod_putref(lod, ltd);
347         if (lod->lod_recovery_completed)
348                 ldev->ld_ops->ldo_recovery_complete(env, ldev);
349
350         RETURN(0);
351
352 out_pool:
353         lod_ost_pool_remove(&lod->lod_pool_info, index);
354 out_mutex:
355         mutex_unlock(&ltd->ltd_mutex);
356         lod_putref(lod, ltd);
357 out_desc:
358         OBD_FREE_PTR(tgt_desc);
359 out_conn:
360         obd_disconnect(exp);
361 out_free:
362         return rc;
363 }
364
365 /*
366  * helper function to schedule OST removal from the device table
367  */
368 static void __lod_del_device(struct lod_tgt_descs *ltd,
369                              unsigned idx)
370 {
371         LASSERT(LTD_TGT(ltd, idx));
372         if (LTD_TGT(ltd, idx)->ltd_reap == 0) {
373                 LTD_TGT(ltd, idx)->ltd_reap = 1;
374                 ltd->ltd_death_row++;
375         }
376 }
377
378 int lod_fini_tgt(struct lod_device *lod, struct lod_tgt_descs *ltd)
379 {
380         int idx;
381
382         if (ltd->ltd_tgts_size <= 0)
383                 return 0;
384         lod_getref(ltd);
385         mutex_lock(&ltd->ltd_mutex);
386         cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx)
387                 __lod_del_device(ltd, idx);
388         mutex_unlock(&ltd->ltd_mutex);
389         lod_putref(lod, ltd);
390         CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
391         for (idx = 0; idx < TGT_PTRS; idx++) {
392                 if (ltd->ltd_tgt_idx[idx])
393                         OBD_FREE_PTR(ltd->ltd_tgt_idx[idx]);
394         }
395         ltd->ltd_tgts_size = 0;
396         return 0;
397 }
398
399 /*
400  * Add support for administratively disabled OST (through the MGS).
401  * Schedule a target for deletion.  Disconnection and real removal from the
402  * table takes place in lod_putref() once the last table user release its
403  * reference.
404  *
405  * \param env - is the environment passed by the caller
406  * \param lod - is the lod device currently connected to the OSP about to be
407  *              removed
408  * \param osp - is the name of OSP device about to be removed
409  * \param idx - is the OSP index
410  * \param gen - is the generation number, not used currently
411  */
412 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
413                    struct lod_tgt_descs *ltd, char *osp, unsigned idx,
414                    unsigned gen)
415 {
416         struct obd_device *obd;
417         int                rc = 0;
418         struct obd_uuid    uuid;
419         ENTRY;
420
421         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
422
423         obd_str2uuid(&uuid, osp);
424
425         obd = class_find_client_obd(&uuid, LUSTRE_OSP_NAME,
426                                    &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
427         if (obd == NULL) {
428                 CERROR("can't find %s device\n", osp);
429                 RETURN(-EINVAL);
430         }
431
432         if (gen <= 0) {
433                 CERROR("%s: request to remove OBD %s with invalid generation %d"
434                        "\n", obd->obd_name, osp, gen);
435                 RETURN(-EINVAL);
436         }
437
438         obd_str2uuid(&uuid,  osp);
439
440         lod_getref(ltd);
441         mutex_lock(&ltd->ltd_mutex);
442         /* check that the index is allocated in the bitmap */
443         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) ||
444             !LTD_TGT(ltd, idx)) {
445                 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
446                 GOTO(out, rc = -EINVAL);
447         }
448
449         /* check that the UUID matches */
450         if (!obd_uuid_equals(&uuid, &LTD_TGT(ltd, idx)->ltd_uuid)) {
451                 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
452                        obd->obd_name, obd_uuid2str(&LTD_TGT(ltd,idx)->ltd_uuid),
453                        idx, osp);
454                 GOTO(out, rc = -EINVAL);
455         }
456
457         __lod_del_device(ltd, idx);
458         EXIT;
459 out:
460         mutex_unlock(&ltd->ltd_mutex);
461         lod_putref(lod, ltd);
462         return(rc);
463 }
464
465 int lod_ea_store_resize(struct lod_thread_info *info, int size)
466 {
467         int round = size_roundup_power2(size);
468
469         LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
470         if (info->lti_ea_store) {
471                 LASSERT(info->lti_ea_store_size);
472                 LASSERT(info->lti_ea_store_size < round);
473                 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
474                        info->lti_ea_store_size, round);
475                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
476                 info->lti_ea_store = NULL;
477                 info->lti_ea_store_size = 0;
478         }
479
480         OBD_ALLOC_LARGE(info->lti_ea_store, round);
481         if (info->lti_ea_store == NULL)
482                 RETURN(-ENOMEM);
483         info->lti_ea_store_size = round;
484         RETURN(0);
485 }
486
487 /*
488  * generate and write LOV EA for given striped object
489  */
490 int lod_generate_and_set_lovea(const struct lu_env *env,
491                                struct lod_object *lo, struct thandle *th)
492 {
493         struct lod_thread_info  *info = lod_env_info(env);
494         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
495         const struct lu_fid     *fid  = lu_object_fid(&lo->ldo_obj.do_lu);
496         struct lov_mds_md_v1    *lmm;
497         struct lov_ost_data_v1  *objs;
498         __u32                    magic;
499         int                      i, rc, lmm_size;
500         int                      cplen = 0;
501         ENTRY;
502
503         LASSERT(lo);
504
505         magic = lo->ldo_pool != NULL ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
506         lmm_size = lov_mds_md_size(lo->ldo_stripenr, magic);
507         if (info->lti_ea_store_size < lmm_size) {
508                 rc = lod_ea_store_resize(info, lmm_size);
509                 if (rc)
510                         RETURN(rc);
511         }
512
513         if (lo->ldo_pattern == 0) /* default striping */
514                 lo->ldo_pattern = LOV_PATTERN_RAID0;
515
516         lmm = info->lti_ea_store;
517
518         lmm->lmm_magic = cpu_to_le32(magic);
519         lmm->lmm_pattern = cpu_to_le32(lo->ldo_pattern);
520         fid_to_lmm_oi(fid, &lmm->lmm_oi);
521         lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
522         lmm->lmm_stripe_size = cpu_to_le32(lo->ldo_stripe_size);
523         lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_stripenr);
524         if (lo->ldo_pattern & LOV_PATTERN_F_RELEASED)
525                 lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_released_stripenr);
526         lmm->lmm_layout_gen = 0;
527         if (magic == LOV_MAGIC_V1) {
528                 objs = &lmm->lmm_objects[0];
529         } else {
530                 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
531                 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
532                                 sizeof(v3->lmm_pool_name));
533                 if (cplen >= sizeof(v3->lmm_pool_name))
534                         RETURN(-E2BIG);
535                 objs = &v3->lmm_objects[0];
536         }
537
538         for (i = 0; i < lo->ldo_stripenr; i++) {
539                 const struct lu_fid     *fid;
540                 struct lod_device       *lod;
541                 __u32                   index;
542
543                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
544                 LASSERT(lo->ldo_stripe[i]);
545                 fid = lu_object_fid(&lo->ldo_stripe[i]->do_lu);
546
547                 rc = fid_to_ostid(fid, &info->lti_ostid);
548                 LASSERT(rc == 0);
549
550                 ostid_cpu_to_le(&info->lti_ostid, &objs[i].l_ost_oi);
551                 objs[i].l_ost_gen    = cpu_to_le32(0);
552                 rc = lod_fld_lookup(env, lod, fid, &index, LU_SEQ_RANGE_OST);
553                 if (rc < 0) {
554                         lod_object_free_striping(env, lo);
555                         CERROR("%s: Can not locate "DFID": rc = %d\n",
556                                lod2obd(lod)->obd_name, PFID(fid), rc);
557                         RETURN(rc);
558                 }
559                 objs[i].l_ost_idx = cpu_to_le32(index);
560         }
561
562         info->lti_buf.lb_buf = lmm;
563         info->lti_buf.lb_len = lmm_size;
564         rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0,
565                           th, BYPASS_CAPA);
566
567         RETURN(rc);
568 }
569
570 int lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
571 {
572         struct lod_thread_info *info = lod_env_info(env);
573         struct dt_object       *next = dt_object_child(&lo->ldo_obj);
574         int                     rc;
575         ENTRY;
576
577         LASSERT(info);
578
579         if (unlikely(info->lti_ea_store_size == 0)) {
580                 /* just to enter in allocation block below */
581                 rc = -ERANGE;
582         } else {
583 repeat:
584                 info->lti_buf.lb_buf = info->lti_ea_store;
585                 info->lti_buf.lb_len = info->lti_ea_store_size;
586                 rc = dt_xattr_get(env, next, &info->lti_buf, XATTR_NAME_LOV,
587                                   BYPASS_CAPA);
588         }
589         /* if object is not striped or inaccessible */
590         if (rc == -ENODATA)
591                 RETURN(0);
592
593         if (rc == -ERANGE) {
594                 /* EA doesn't fit, reallocate new buffer */
595                 rc = dt_xattr_get(env, next, &LU_BUF_NULL, XATTR_NAME_LOV,
596                                   BYPASS_CAPA);
597                 if (rc == -ENODATA)
598                         RETURN(0);
599                 else if (rc < 0)
600                         RETURN(rc);
601
602                 LASSERT(rc > 0);
603                 rc = lod_ea_store_resize(info, rc);
604                 if (rc)
605                         RETURN(rc);
606                 goto repeat;
607         }
608
609         RETURN(rc);
610 }
611
612 int lod_store_def_striping(const struct lu_env *env, struct dt_object *dt,
613                            struct thandle *th)
614 {
615         struct lod_thread_info  *info = lod_env_info(env);
616         struct lod_object       *lo = lod_dt_obj(dt);
617         struct dt_object        *next = dt_object_child(dt);
618         struct lov_user_md_v3   *v3;
619         int                      rc;
620         int                      cplen = 0;
621         ENTRY;
622
623         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
624
625         /*
626          * store striping defaults into new directory
627          * used to implement defaults inheritance
628          */
629
630         /* probably nothing to inherite */
631         if (lo->ldo_striping_cached == 0)
632                 RETURN(0);
633
634         if (LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size, lo->ldo_def_stripenr,
635                                 lo->ldo_def_stripe_offset))
636                 RETURN(0);
637
638         /* XXX: use thread info */
639         OBD_ALLOC_PTR(v3);
640         if (v3 == NULL)
641                 RETURN(-ENOMEM);
642
643         v3->lmm_magic = cpu_to_le32(LOV_MAGIC_V3);
644         v3->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
645         v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
646         v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
647         v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
648         if (lo->ldo_pool) {
649                 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
650                                 sizeof(v3->lmm_pool_name));
651                 if (cplen >= sizeof(v3->lmm_pool_name)) {
652                         OBD_FREE_PTR(v3);
653                         RETURN(-E2BIG);
654                 }
655         }
656
657         info->lti_buf.lb_buf = v3;
658         info->lti_buf.lb_len = sizeof(*v3);
659         rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
660                         BYPASS_CAPA);
661
662         OBD_FREE_PTR(v3);
663
664         RETURN(rc);
665 }
666
667 static int validate_lod_and_idx(struct lod_device *md, int idx)
668 {
669         if (unlikely(idx >= md->lod_ost_descs.ltd_tgts_size ||
670                      !cfs_bitmap_check(md->lod_ost_bitmap, idx))) {
671                 CERROR("%s: bad idx: %d of %d\n", lod2obd(md)->obd_name, idx,
672                        md->lod_ost_descs.ltd_tgts_size);
673                 return -EINVAL;
674         }
675
676         if (unlikely(OST_TGT(md, idx) == NULL)) {
677                 CERROR("%s: bad lod_tgt_desc for idx: %d\n",
678                        lod2obd(md)->obd_name, idx);
679                 return -EINVAL;
680         }
681
682         if (unlikely(OST_TGT(md, idx)->ltd_ost == NULL)) {
683                 CERROR("%s: invalid lod device, for idx: %d\n",
684                        lod2obd(md)->obd_name , idx);
685                 return -EINVAL;
686         }
687
688         return 0;
689 }
690
691 /*
692  * allocate array of objects pointers, find/create objects
693  * stripenr and other fields should be initialized by this moment
694  */
695 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
696                            struct lov_ost_data_v1 *objs)
697 {
698         struct lod_thread_info  *info = lod_env_info(env);
699         struct lod_device       *md;
700         struct lu_object        *o, *n;
701         struct lu_device        *nd;
702         struct dt_object       **stripe;
703         int                      stripe_len;
704         int                      i, idx, rc = 0;
705         ENTRY;
706
707         LASSERT(lo != NULL);
708         md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
709         LASSERT(lo->ldo_stripe == NULL);
710         LASSERT(lo->ldo_stripenr > 0);
711         LASSERT(lo->ldo_stripe_size > 0);
712
713         stripe_len = lo->ldo_stripenr;
714         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
715         if (stripe == NULL)
716                 RETURN(-ENOMEM);
717
718         for (i = 0; i < lo->ldo_stripenr; i++) {
719                 ostid_le_to_cpu(&objs[i].l_ost_oi, &info->lti_ostid);
720                 idx = le64_to_cpu(objs[i].l_ost_idx);
721                 rc = ostid_to_fid(&info->lti_fid, &info->lti_ostid, idx);
722                 if (rc != 0)
723                         GOTO(out, rc);
724                 LASSERTF(fid_is_sane(&info->lti_fid), ""DFID" insane!\n",
725                          PFID(&info->lti_fid));
726                 lod_getref(&md->lod_ost_descs);
727
728                 rc = validate_lod_and_idx(md, idx);
729                 if (unlikely(rc != 0)) {
730                         lod_putref(md, &md->lod_ost_descs);
731                         GOTO(out, rc);
732                 }
733
734                 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
735                 lod_putref(md, &md->lod_ost_descs);
736
737                 /* In the function below, .hs_keycmp resolves to
738                  * u_obj_hop_keycmp() */
739                 /* coverity[overrun-buffer-val] */
740                 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
741                 if (IS_ERR(o))
742                         GOTO(out, rc = PTR_ERR(o));
743
744                 n = lu_object_locate(o->lo_header, nd->ld_type);
745                 LASSERT(n);
746
747                 stripe[i] = container_of(n, struct dt_object, do_lu);
748         }
749
750 out:
751         if (rc != 0) {
752                 for (i = 0; i < stripe_len; i++)
753                         if (stripe[i] != NULL)
754                                 lu_object_put(env, &stripe[i]->do_lu);
755
756                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
757         } else {
758                 lo->ldo_stripe = stripe;
759                 lo->ldo_stripes_allocated = stripe_len;
760         }
761
762         RETURN(rc);
763 }
764
765 /*
766  * Parse striping information stored in lti_ea_store
767  */
768 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
769                        const struct lu_buf *buf)
770 {
771         struct lov_mds_md_v1    *lmm;
772         struct lov_ost_data_v1  *objs;
773         __u32                    magic;
774         __u32                    pattern;
775         int                      rc = 0;
776         ENTRY;
777
778         LASSERT(buf);
779         LASSERT(buf->lb_buf);
780         LASSERT(buf->lb_len);
781
782         lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
783         magic = le32_to_cpu(lmm->lmm_magic);
784         pattern = le32_to_cpu(lmm->lmm_pattern);
785
786         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
787                 GOTO(out, rc = -EINVAL);
788         if (lov_pattern(pattern) != LOV_PATTERN_RAID0)
789                 GOTO(out, rc = -EINVAL);
790
791         lo->ldo_pattern = pattern;
792         lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
793         lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
794         lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
795         /* released file stripenr fixup. */
796         if (pattern & LOV_PATTERN_F_RELEASED)
797                 lo->ldo_stripenr = 0;
798
799         LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
800
801         if (magic == LOV_MAGIC_V3) {
802                 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
803                 objs = &v3->lmm_objects[0];
804                 lod_object_set_pool(lo, v3->lmm_pool_name);
805         } else {
806                 objs = &lmm->lmm_objects[0];
807         }
808
809         if (lo->ldo_stripenr > 0)
810                 rc = lod_initialize_objects(env, lo, objs);
811
812 out:
813         RETURN(rc);
814 }
815
816 /*
817  * Load and parse striping information, create in-core representation for the
818  * stripes
819  */
820 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
821 {
822         struct lod_thread_info  *info = lod_env_info(env);
823         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
824         int                      rc;
825         ENTRY;
826
827         /*
828          * currently this code is supposed to be called from declaration
829          * phase only, thus the object is not expected to be locked by caller
830          */
831         dt_write_lock(env, next, 0);
832         /* already initialized? */
833         if (lo->ldo_stripe != NULL)
834                 GOTO(out, rc = 0);
835
836         if (!dt_object_exists(next))
837                 GOTO(out, rc = 0);
838
839         /* only regular files can be striped */
840         if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
841                 GOTO(out, rc = 0);
842
843         rc = lod_get_lov_ea(env, lo);
844         if (rc <= 0)
845                 GOTO(out, rc);
846
847         /*
848          * there is LOV EA (striping information) in this object
849          * let's parse it and create in-core objects for the stripes
850          */
851         info->lti_buf.lb_buf = info->lti_ea_store;
852         info->lti_buf.lb_len = info->lti_ea_store_size;
853         rc = lod_parse_striping(env, lo, &info->lti_buf);
854 out:
855         dt_write_unlock(env, next);
856         RETURN(rc);
857 }
858
859 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
860                         int specific)
861 {
862         struct lov_user_md_v1   *lum;
863         struct lov_user_md_v3   *lum3;
864         struct pool_desc        *pool = NULL;
865         __u32                    magic;
866         __u32                    stripe_size;
867         __u16                    stripe_count;
868         __u16                    stripe_offset;
869         size_t                   lum_size;
870         int                      rc = 0;
871         ENTRY;
872
873         lum = buf->lb_buf;
874
875         LASSERT(sizeof(*lum) < sizeof(*lum3));
876
877         if (buf->lb_len < sizeof(*lum)) {
878                 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md\n",
879                        buf->lb_len);
880                 GOTO(out, rc = -EINVAL);
881         }
882
883         magic = le32_to_cpu(lum->lmm_magic);
884         if (magic != LOV_USER_MAGIC_V1 &&
885             magic != LOV_USER_MAGIC_V3 &&
886             magic != LOV_MAGIC_V1_DEF &&
887             magic != LOV_MAGIC_V3_DEF) {
888                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n", magic);
889                 GOTO(out, rc = -EINVAL);
890         }
891
892         if ((specific && le32_to_cpu(lum->lmm_pattern) != LOV_PATTERN_RAID0) ||
893             (!specific && lum->lmm_pattern != 0)) {
894                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
895                        le32_to_cpu(lum->lmm_pattern));
896                 GOTO(out, rc = -EINVAL);
897         }
898
899         /* 64kB is the largest common page size we see (ia64), and matches the
900          * check in lfs */
901         stripe_size = le32_to_cpu(lum->lmm_stripe_size);
902         if (stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
903                 CDEBUG(D_IOCTL, "stripe size %u not a multiple of %u\n",
904                        stripe_size, LOV_MIN_STRIPE_SIZE);
905                 GOTO(out, rc = -EINVAL);
906         }
907
908         /* an offset of -1 is treated as a "special" valid offset */
909         stripe_offset = le16_to_cpu(lum->lmm_stripe_offset);
910         if (stripe_offset != (typeof(stripe_offset))-1) {
911                 /* if offset is not within valid range [0, osts_size) */
912                 if (stripe_offset >= d->lod_osts_size) {
913                         CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
914                                stripe_offset, d->lod_osts_size);
915                         GOTO(out, rc = -EINVAL);
916                 }
917
918                 /* if lmm_stripe_offset is *not* in bitmap */
919                 if (!cfs_bitmap_check(d->lod_ost_bitmap, stripe_offset)) {
920                         CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
921                                stripe_offset);
922                         GOTO(out, rc = -EINVAL);
923                 }
924         }
925
926         stripe_count = le16_to_cpu(lum->lmm_stripe_count);
927         if (magic == LOV_USER_MAGIC_V1 || magic == LOV_MAGIC_V1_DEF)
928                 lum_size = offsetof(struct lov_user_md_v1,
929                                     lmm_objects[stripe_count]);
930         else if (magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF)
931                 lum_size = offsetof(struct lov_user_md_v3,
932                                     lmm_objects[stripe_count]);
933         else
934                 LBUG();
935
936         if (specific && buf->lb_len != lum_size) {
937                 CDEBUG(D_IOCTL, "invalid buf len %zd for lov_user_md with "
938                        "magic %#x and stripe_count %u\n",
939                        buf->lb_len, magic, stripe_count);
940                 GOTO(out, rc = -EINVAL);
941         }
942
943         if (!(magic == LOV_USER_MAGIC_V3 || magic == LOV_MAGIC_V3_DEF))
944                 goto out;
945
946         lum3 = buf->lb_buf;
947         if (buf->lb_len < sizeof(*lum3)) {
948                 CDEBUG(D_IOCTL, "buf len %zd too small for lov_user_md_v3\n",
949                        buf->lb_len);
950                 GOTO(out, rc = -EINVAL);
951         }
952
953         /* In the function below, .hs_keycmp resolves to
954          * pool_hashkey_keycmp() */
955         /* coverity[overrun-buffer-val] */
956         pool = lod_find_pool(d, lum3->lmm_pool_name);
957         if (pool == NULL)
958                 goto out;
959
960         if (stripe_offset != (typeof(stripe_offset))-1) {
961                 rc = lod_check_index_in_pool(stripe_offset, pool);
962                 if (rc < 0)
963                         GOTO(out, rc = -EINVAL);
964         }
965
966         if (specific && stripe_count > pool_tgt_count(pool)) {
967                 CDEBUG(D_IOCTL,
968                        "stripe count %u > # OSTs %u in the pool\n",
969                        stripe_count, pool_tgt_count(pool));
970                 GOTO(out, rc = -EINVAL);
971         }
972
973 out:
974         if (pool != NULL)
975                 lod_pool_putref(pool);
976
977         RETURN(rc);
978 }
979
980 void lod_fix_desc_stripe_size(__u64 *val)
981 {
982         if (*val < LOV_MIN_STRIPE_SIZE) {
983                 if (*val != 0)
984                         LCONSOLE_INFO("Increasing default stripe size to "
985                                       "minimum value %u\n",
986                                       LOV_DEFAULT_STRIPE_SIZE);
987                 *val = LOV_DEFAULT_STRIPE_SIZE;
988         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
989                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
990                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
991                               "multiple of %u)\n",
992                               *val, LOV_MIN_STRIPE_SIZE);
993         }
994 }
995
996 void lod_fix_desc_stripe_count(__u32 *val)
997 {
998         if (*val == 0)
999                 *val = 1;
1000 }
1001
1002 void lod_fix_desc_pattern(__u32 *val)
1003 {
1004         /* from lov_setstripe */
1005         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
1006                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
1007                 *val = 0;
1008         }
1009 }
1010
1011 void lod_fix_desc_qos_maxage(__u32 *val)
1012 {
1013         /* fix qos_maxage */
1014         if (*val == 0)
1015                 *val = QOS_DEFAULT_MAXAGE;
1016 }
1017
1018 void lod_fix_desc(struct lov_desc *desc)
1019 {
1020         lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
1021         lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
1022         lod_fix_desc_pattern(&desc->ld_pattern);
1023         lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
1024 }
1025
1026 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
1027 {
1028         struct obd_device          *obd;
1029         struct lov_desc            *desc;
1030         int                         rc;
1031         ENTRY;
1032
1033         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1034         LASSERT(obd != NULL);
1035         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1036
1037         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1038                 CERROR("LOD setup requires a descriptor\n");
1039                 RETURN(-EINVAL);
1040         }
1041
1042         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
1043
1044         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1045                 CERROR("descriptor size wrong: %d > %d\n",
1046                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1047                 RETURN(-EINVAL);
1048         }
1049
1050         if (desc->ld_magic != LOV_DESC_MAGIC) {
1051                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
1052                         CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
1053                                obd->obd_name, desc);
1054                         lustre_swab_lov_desc(desc);
1055                 } else {
1056                         CERROR("%s: Bad lov desc magic: %#x\n",
1057                                obd->obd_name, desc->ld_magic);
1058                         RETURN(-EINVAL);
1059                 }
1060         }
1061
1062         lod_fix_desc(desc);
1063
1064         desc->ld_active_tgt_count = 0;
1065         lod->lod_desc = *desc;
1066
1067         lod->lod_sp_me = LUSTRE_SP_CLI;
1068
1069         /* Set up allocation policy (QoS and RR) */
1070         CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
1071         init_rwsem(&lod->lod_qos.lq_rw_sem);
1072         lod->lod_qos.lq_dirty = 1;
1073         lod->lod_qos.lq_rr.lqr_dirty = 1;
1074         lod->lod_qos.lq_reset = 1;
1075         /* Default priority is toward free space balance */
1076         lod->lod_qos.lq_prio_free = 232;
1077         /* Default threshold for rr (roughly 17%) */
1078         lod->lod_qos.lq_threshold_rr = 43;
1079
1080         /* Set up OST pool environment */
1081         lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
1082                                                    HASH_POOLS_MAX_BITS,
1083                                                    HASH_POOLS_BKT_BITS, 0,
1084                                                    CFS_HASH_MIN_THETA,
1085                                                    CFS_HASH_MAX_THETA,
1086                                                    &pool_hash_operations,
1087                                                    CFS_HASH_DEFAULT);
1088         if (lod->lod_pools_hash_body == NULL)
1089                 RETURN(-ENOMEM);
1090
1091         CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
1092         lod->lod_pool_count = 0;
1093         rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
1094         if (rc)
1095                 GOTO(out_hash, rc);
1096         rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
1097         if (rc)
1098                 GOTO(out_pool_info, rc);
1099
1100         RETURN(0);
1101
1102 out_pool_info:
1103         lod_ost_pool_free(&lod->lod_pool_info);
1104 out_hash:
1105         cfs_hash_putref(lod->lod_pools_hash_body);
1106
1107         return rc;
1108 }
1109
1110 int lod_pools_fini(struct lod_device *lod)
1111 {
1112         struct obd_device   *obd = lod2obd(lod);
1113         cfs_list_t          *pos, *tmp;
1114         struct pool_desc    *pool;
1115         ENTRY;
1116
1117         cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
1118                 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
1119                 /* free pool structs */
1120                 CDEBUG(D_INFO, "delete pool %p\n", pool);
1121                 /* In the function below, .hs_keycmp resolves to
1122                  * pool_hashkey_keycmp() */
1123                 /* coverity[overrun-buffer-val] */
1124                 lod_pool_del(obd, pool->pool_name);
1125         }
1126
1127         cfs_hash_putref(lod->lod_pools_hash_body);
1128         lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
1129         lod_ost_pool_free(&lod->lod_pool_info);
1130
1131         RETURN(0);
1132 }
1133