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