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