Whamcloud - gitweb
65326fa5a1a2b257985915cb4249be1c3dba7a5b
[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, Intel Corporation.
27  */
28 /*
29  * lustre/lod/lod_lov.c
30  *
31  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com> 
32  */
33
34 #ifndef EXPORT_SYMTAB
35 # define EXPORT_SYMTAB
36 #endif
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include <obd_class.h>
40 #include <obd_lov.h>
41
42 #include "lod_internal.h"
43
44 /*
45  * Keep a refcount of lod->ltd_tgts usage to prevent racing with
46  * addition/deletion. Any function that expects lov_tgts to remain stationary
47  * must take a ref.
48  *
49  * \param lod - is the lod device from which we want to grab a reference
50  */
51 void lod_getref(struct lod_tgt_descs *ltd)
52 {
53         down_read(&ltd->ltd_rw_sem);
54         mutex_lock(&ltd->ltd_mutex);
55         ltd->ltd_refcount++;
56         mutex_unlock(&ltd->ltd_mutex);
57 }
58
59 /*
60  * Companion of lod_getref() to release a reference on the lod table.
61  * If this is the last reference and the ost entry was scheduled for deletion,
62  * the descriptor is removed from the array.
63  *
64  * \param lod - is the lod device from which we release a reference
65  */
66 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd)
67 {
68         mutex_lock(&ltd->ltd_mutex);
69         ltd->ltd_refcount--;
70         if (ltd->ltd_refcount == 0 && ltd->ltd_death_row) {
71                 struct lod_tgt_desc *tgt_desc, *tmp;
72                 int                  idx;
73                 CFS_LIST_HEAD(kill);
74
75                 CDEBUG(D_CONFIG, "destroying %d ltd desc\n",
76                        ltd->ltd_death_row);
77
78                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx) {
79                         tgt_desc = LTD_TGT(ltd, idx);
80                         LASSERT(tgt_desc);
81
82                         if (!tgt_desc->ltd_reap)
83                                 continue;
84
85                         cfs_list_add(&tgt_desc->ltd_kill, &kill);
86                         LTD_TGT(ltd, idx) = NULL;
87                         /*FIXME: only support ost pool for now */
88                         if (ltd == &lod->lod_ost_descs) {
89                                 lod_ost_pool_remove(&lod->lod_pool_info, idx);
90                                 if (tgt_desc->ltd_active)
91                                         lod->lod_desc.ld_active_tgt_count--;
92                         }
93                         ltd->ltd_tgtnr--;
94                         cfs_bitmap_clear(ltd->ltd_tgt_bitmap, idx);
95                         ltd->ltd_death_row--;
96                 }
97                 mutex_unlock(&ltd->ltd_mutex);
98                 up_read(&ltd->ltd_rw_sem);
99
100                 cfs_list_for_each_entry_safe(tgt_desc, tmp, &kill, ltd_kill) {
101                         int rc;
102                         cfs_list_del(&tgt_desc->ltd_kill);
103                         if (ltd == &lod->lod_ost_descs) {
104                                 /* remove from QoS structures */
105                                 rc = qos_del_tgt(lod, tgt_desc);
106                                 if (rc)
107                                         CERROR("%s: qos_del_tgt(%s) failed:"
108                                                "rc = %d\n",
109                                                lod2obd(lod)->obd_name,
110                                               obd_uuid2str(&tgt_desc->ltd_uuid),
111                                                rc);
112                         }
113                         rc = obd_disconnect(tgt_desc->ltd_exp);
114                         if (rc)
115                                 CERROR("%s: failed to disconnect %s: rc = %d\n",
116                                        lod2obd(lod)->obd_name,
117                                        obd_uuid2str(&tgt_desc->ltd_uuid), rc);
118                         OBD_FREE_PTR(tgt_desc);
119                 }
120         } else {
121                 mutex_unlock(&ltd->ltd_mutex);
122                 up_read(&ltd->ltd_rw_sem);
123         }
124 }
125
126 static int ltd_bitmap_resize(struct lod_tgt_descs *ltd, __u32 newsize)
127 {
128         cfs_bitmap_t *new_bitmap, *old_bitmap = NULL;
129         int           rc = 0;
130         ENTRY;
131
132         /* grab write reference on the lod. Relocating the array requires
133          * exclusive access */
134
135         down_write(&ltd->ltd_rw_sem);
136         if (newsize <= ltd->ltd_tgts_size)
137                 /* someone else has already resize the array */
138                 GOTO(out, rc = 0);
139
140         /* allocate new bitmap */
141         new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
142         if (!new_bitmap)
143                 GOTO(out, rc = -ENOMEM);
144
145         if (ltd->ltd_tgts_size > 0) {
146                 /* the bitmap already exists, we need
147                  * to copy data from old one */
148                 cfs_bitmap_copy(new_bitmap, ltd->ltd_tgt_bitmap);
149                 old_bitmap = ltd->ltd_tgt_bitmap;
150         }
151
152         ltd->ltd_tgts_size  = newsize;
153         ltd->ltd_tgt_bitmap = new_bitmap;
154
155         if (old_bitmap)
156                 CFS_FREE_BITMAP(old_bitmap);
157
158         CDEBUG(D_CONFIG, "tgt size: %d\n", ltd->ltd_tgts_size);
159
160         EXIT;
161 out:
162         up_write(&ltd->ltd_rw_sem);
163         return rc;
164 }
165
166 /*
167  * Connect LOD to a new OSP and add it to the device table.
168  *
169  * \param env - is the environment passed by the caller
170  * \param lod - is the LOD device to be connected to the new OSP
171  * \param osp - is the name of OSP device name about to be added
172  * \param index - is the OSP index
173  * \param gen - is the generation number
174  * \param tgt_index - is the group of the OSP.
175  * \param type - is the type of device (mdc or osc)
176  */
177 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
178                    char *osp, unsigned index, unsigned gen, int tgt_index,
179                    char *type, int active)
180 {
181         struct obd_connect_data *data = NULL;
182         struct obd_export       *exp = NULL;
183         struct obd_device       *obd;
184         struct lu_device        *ldev;
185         struct dt_device        *d;
186         int                      rc;
187         struct lod_tgt_desc     *tgt_desc;
188         struct lod_tgt_descs    *ltd;
189         struct obd_uuid         obd_uuid;
190         ENTRY;
191
192         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, index, gen);
193
194         if (gen <= 0) {
195                 CERROR("request to add OBD %s with invalid generation: %d\n",
196                        osp, gen);
197                 RETURN(-EINVAL);
198         }
199
200         obd_str2uuid(&obd_uuid, osp);
201
202         obd = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
203                                 &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
204         if (obd == NULL) {
205                 CERROR("can't find %s device\n", osp);
206                 RETURN(-EINVAL);
207         }
208
209         OBD_ALLOC_PTR(data);
210         if (data == NULL)
211                 RETURN(-ENOMEM);
212
213         data->ocd_connect_flags = OBD_CONNECT_INDEX | OBD_CONNECT_VERSION;
214         data->ocd_version = LUSTRE_VERSION_CODE;
215         data->ocd_index = index;
216
217         if (strcmp(LUSTRE_OSC_NAME, type) == 0) {
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                 data->ocd_ibits_known = MDS_INODELOCK_UPDATE;
239                 data->ocd_connect_flags |= OBD_CONNECT_ACL |
240                                            OBD_CONNECT_MDS_CAPA |
241                                            OBD_CONNECT_OSS_CAPA |
242                                            OBD_CONNECT_IBITS |
243                                            OBD_CONNECT_MDS_MDS |
244                                            OBD_CONNECT_FID |
245                                            OBD_CONNECT_AT |
246                                            OBD_CONNECT_FULL20 |
247                                            OBD_CONNECT_PINGLESS;
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 = MDS_MDS_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         RETURN(0);
355
356 out_pool:
357         lod_ost_pool_remove(&lod->lod_pool_info, index);
358 out_mutex:
359         mutex_unlock(&ltd->ltd_mutex);
360         lod_putref(lod, ltd);
361 out_desc:
362         OBD_FREE_PTR(tgt_desc);
363 out_conn:
364         obd_disconnect(exp);
365 out_free:
366         return rc;
367 }
368
369 /*
370  * helper function to schedule OST removal from the device table
371  */
372 static void __lod_del_device(struct lod_tgt_descs *ltd,
373                              unsigned idx)
374 {
375         LASSERT(LTD_TGT(ltd, idx));
376         if (LTD_TGT(ltd, idx)->ltd_reap == 0) {
377                 LTD_TGT(ltd, idx)->ltd_reap = 1;
378                 ltd->ltd_death_row++;
379         }
380 }
381
382 int lod_fini_tgt(struct lod_device *lod, struct lod_tgt_descs *ltd)
383 {
384         int idx;
385
386         if (ltd->ltd_tgts_size <= 0)
387                 return 0;
388         lod_getref(ltd);
389         mutex_lock(&ltd->ltd_mutex);
390         cfs_foreach_bit(ltd->ltd_tgt_bitmap, idx)
391                 __lod_del_device(ltd, idx);
392         mutex_unlock(&ltd->ltd_mutex);
393         lod_putref(lod, ltd);
394         CFS_FREE_BITMAP(ltd->ltd_tgt_bitmap);
395         for (idx = 0; idx < TGT_PTRS; idx++) {
396                 if (ltd->ltd_tgt_idx[idx])
397                         OBD_FREE_PTR(ltd->ltd_tgt_idx[idx]);
398         }
399         ltd->ltd_tgts_size = 0;
400         return 0;
401 }
402
403 /*
404  * Add support for administratively disabled OST (through the MGS).
405  * Schedule a target for deletion.  Disconnection and real removal from the
406  * table takes place in lod_putref() once the last table user release its
407  * reference.
408  *
409  * \param env - is the environment passed by the caller
410  * \param lod - is the lod device currently connected to the OSP about to be
411  *              removed
412  * \param osp - is the name of OSP device about to be removed
413  * \param idx - is the OSP index
414  * \param gen - is the generation number, not used currently
415  */
416 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
417                    struct lod_tgt_descs *ltd, char *osp, unsigned idx,
418                    unsigned gen)
419 {
420         struct obd_device *obd;
421         int                rc = 0;
422         struct obd_uuid    uuid;
423         ENTRY;
424
425         CDEBUG(D_CONFIG, "osp:%s idx:%d gen:%d\n", osp, idx, gen);
426
427         obd_str2uuid(&uuid, osp);
428
429         obd = class_find_client_obd(&uuid, LUSTRE_OSP_NAME,
430                                    &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
431         if (obd == NULL) {
432                 CERROR("can't find %s device\n", osp);
433                 RETURN(-EINVAL);
434         }
435
436         if (gen <= 0) {
437                 CERROR("%s: request to remove OBD %s with invalid generation %d"
438                        "\n", obd->obd_name, osp, gen);
439                 RETURN(-EINVAL);
440         }
441
442         obd_str2uuid(&uuid,  osp);
443
444         lod_getref(ltd);
445         mutex_lock(&ltd->ltd_mutex);
446         /* check that the index is allocated in the bitmap */
447         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) ||
448             !LTD_TGT(ltd, idx)) {
449                 CERROR("%s: device %d is not set up\n", obd->obd_name, idx);
450                 GOTO(out, rc = -EINVAL);
451         }
452
453         /* check that the UUID matches */
454         if (!obd_uuid_equals(&uuid, &LTD_TGT(ltd, idx)->ltd_uuid)) {
455                 CERROR("%s: LOD target UUID %s at index %d does not match %s\n",
456                        obd->obd_name, obd_uuid2str(&LTD_TGT(ltd,idx)->ltd_uuid),
457                        idx, osp);
458                 GOTO(out, rc = -EINVAL);
459         }
460
461         __lod_del_device(ltd, idx);
462         EXIT;
463 out:
464         mutex_unlock(&ltd->ltd_mutex);
465         lod_putref(lod, ltd);
466         return(rc);
467 }
468
469 int lod_ea_store_resize(struct lod_thread_info *info, int size)
470 {
471         int round = size_roundup_power2(size);
472
473         LASSERT(round <= lov_mds_md_size(LOV_MAX_STRIPE_COUNT, LOV_MAGIC_V3));
474         if (info->lti_ea_store) {
475                 LASSERT(info->lti_ea_store_size);
476                 LASSERT(info->lti_ea_store_size < round);
477                 CDEBUG(D_INFO, "EA store size %d is not enough, need %d\n",
478                        info->lti_ea_store_size, round);
479                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
480                 info->lti_ea_store = NULL;
481                 info->lti_ea_store_size = 0;
482         }
483
484         OBD_ALLOC_LARGE(info->lti_ea_store, round);
485         if (info->lti_ea_store == NULL)
486                 RETURN(-ENOMEM);
487         info->lti_ea_store_size = round;
488         RETURN(0);
489 }
490
491 /*
492  * generate and write LOV EA for given striped object
493  */
494 int lod_generate_and_set_lovea(const struct lu_env *env,
495                                struct lod_object *lo, struct thandle *th)
496 {
497         struct lod_thread_info  *info = lod_env_info(env);
498         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
499         const struct lu_fid     *fid  = lu_object_fid(&lo->ldo_obj.do_lu);
500         struct lov_mds_md_v1    *lmm;
501         struct lov_ost_data_v1  *objs;
502         __u32                    magic;
503         int                      i, rc, lmm_size;
504         int                      cplen = 0;
505         ENTRY;
506
507         LASSERT(lo);
508         LASSERT(lo->ldo_stripenr > 0);
509
510         magic = lo->ldo_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
511         lmm_size = lov_mds_md_size(lo->ldo_stripenr, magic);
512         if (info->lti_ea_store_size < lmm_size) {
513                 rc = lod_ea_store_resize(info, lmm_size);
514                 if (rc)
515                         RETURN(rc);
516         }
517
518         lmm = info->lti_ea_store;
519
520         lmm->lmm_magic = cpu_to_le32(magic);
521         lmm->lmm_pattern = cpu_to_le32(LOV_PATTERN_RAID0);
522         lmm->lmm_object_id = cpu_to_le64(fid_ver_oid(fid));
523         lmm->lmm_object_seq = cpu_to_le64(fid_seq(fid));
524         lmm->lmm_stripe_size = cpu_to_le32(lo->ldo_stripe_size);
525         lmm->lmm_stripe_count = cpu_to_le16(lo->ldo_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_ostid_pack(fid, &info->lti_ostid);
548                 LASSERT(rc == 0);
549
550                 objs[i].l_object_id  = cpu_to_le64(info->lti_ostid.oi_id);
551                 objs[i].l_object_seq = cpu_to_le64(info->lti_ostid.oi_seq);
552                 objs[i].l_ost_gen    = cpu_to_le32(0);
553                 rc = lod_fld_lookup(env, lod, fid, &index, LU_SEQ_RANGE_OST);
554                 if (rc < 0) {
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_object_id = 0;
646         v3->lmm_object_seq = 0;
647         v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
648         v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
649         v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
650         if (lo->ldo_pool) {
651                 cplen = strlcpy(v3->lmm_pool_name, lo->ldo_pool,
652                                 sizeof(v3->lmm_pool_name));
653                 if (cplen >= sizeof(v3->lmm_pool_name)) {
654                         OBD_FREE_PTR(v3);
655                         RETURN(-E2BIG);
656                 }
657         }
658
659         info->lti_buf.lb_buf = v3;
660         info->lti_buf.lb_len = sizeof(*v3);
661         rc = dt_xattr_set(env, next, &info->lti_buf, XATTR_NAME_LOV, 0, th,
662                         BYPASS_CAPA);
663
664         OBD_FREE_PTR(v3);
665
666         RETURN(rc);
667 }
668
669 /*
670  * allocate array of objects pointers, find/create objects
671  * stripenr and other fields should be initialized by this moment
672  */
673 int lod_initialize_objects(const struct lu_env *env, struct lod_object *lo,
674                            struct lov_ost_data_v1 *objs)
675 {
676         struct lod_thread_info  *info = lod_env_info(env);
677         struct lod_device       *md = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
678         struct lu_object        *o, *n;
679         struct lu_device        *nd;
680         struct dt_object       **stripe;
681         int                      stripe_len;
682         int                      i, idx, rc = 0;
683         ENTRY;
684
685         LASSERT(lo);
686         LASSERT(lo->ldo_stripe == NULL);
687         LASSERT(lo->ldo_stripenr > 0);
688         LASSERT(lo->ldo_stripe_size > 0);
689
690         stripe_len = lo->ldo_stripenr;
691         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_len);
692         if (stripe == NULL)
693                 RETURN(-ENOMEM);
694
695         for (i = 0; i < lo->ldo_stripenr; i++) {
696                 info->lti_ostid.oi_id = le64_to_cpu(objs[i].l_object_id);
697                 info->lti_ostid.oi_seq = le64_to_cpu(objs[i].l_object_seq);
698                 idx = le64_to_cpu(objs[i].l_ost_idx);
699                 fid_ostid_unpack(&info->lti_fid, &info->lti_ostid, idx);
700                 LASSERTF(fid_is_sane(&info->lti_fid), ""DFID" insane!\n",
701                          PFID(&info->lti_fid));
702                 /*
703                  * XXX: assertion is left for testing, to make
704                  * sure we never process requests till configuration
705                  * is completed. to be changed to -EINVAL
706                  */
707
708                 lod_getref(&md->lod_ost_descs);
709                 LASSERT(cfs_bitmap_check(md->lod_ost_bitmap, idx));
710                 LASSERT(OST_TGT(md,idx));
711                 LASSERTF(OST_TGT(md,idx)->ltd_ost, "idx %d\n", idx);
712                 nd = &OST_TGT(md,idx)->ltd_ost->dd_lu_dev;
713                 lod_putref(md, &md->lod_ost_descs);
714
715                 /* In the function below, .hs_keycmp resolves to
716                  * u_obj_hop_keycmp() */
717                 /* coverity[overrun-buffer-val] */
718                 o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
719                 if (IS_ERR(o))
720                         GOTO(out, rc = PTR_ERR(o));
721
722                 n = lu_object_locate(o->lo_header, nd->ld_type);
723                 LASSERT(n);
724
725                 stripe[i] = container_of(n, struct dt_object, do_lu);
726         }
727
728 out:
729         if (rc != 0) {
730                 for (i = 0; i < stripe_len; i++)
731                         if (stripe[i] != NULL)
732                                 lu_object_put(env, &stripe[i]->do_lu);
733
734                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_len);
735         } else {
736                 lo->ldo_stripe = stripe;
737                 lo->ldo_stripes_allocated = stripe_len;
738         }
739
740         RETURN(rc);
741 }
742
743 /*
744  * Parse striping information stored in lti_ea_store
745  */
746 int lod_parse_striping(const struct lu_env *env, struct lod_object *lo,
747                        const struct lu_buf *buf)
748 {
749         struct lov_mds_md_v1    *lmm;
750         struct lov_ost_data_v1  *objs;
751         __u32                    magic;
752         int                      rc = 0;
753         ENTRY;
754
755         LASSERT(buf);
756         LASSERT(buf->lb_buf);
757         LASSERT(buf->lb_len);
758
759         lmm = (struct lov_mds_md_v1 *) buf->lb_buf;
760         magic = le32_to_cpu(lmm->lmm_magic);
761
762         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
763                 GOTO(out, rc = -EINVAL);
764         if (le32_to_cpu(lmm->lmm_pattern) != LOV_PATTERN_RAID0)
765                 GOTO(out, rc = -EINVAL);
766
767         lo->ldo_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
768         lo->ldo_stripenr = le16_to_cpu(lmm->lmm_stripe_count);
769         lo->ldo_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
770
771         LASSERT(buf->lb_len >= lov_mds_md_size(lo->ldo_stripenr, magic));
772
773         if (magic == LOV_MAGIC_V3) {
774                 struct lov_mds_md_v3 *v3 = (struct lov_mds_md_v3 *) lmm;
775                 objs = &v3->lmm_objects[0];
776                 lod_object_set_pool(lo, v3->lmm_pool_name);
777         } else {
778                 objs = &lmm->lmm_objects[0];
779         }
780
781         rc = lod_initialize_objects(env, lo, objs);
782
783 out:
784         RETURN(rc);
785 }
786
787 /*
788  * Load and parse striping information, create in-core representation for the
789  * stripes
790  */
791 int lod_load_striping(const struct lu_env *env, struct lod_object *lo)
792 {
793         struct lod_thread_info  *info = lod_env_info(env);
794         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
795         int                      rc;
796         ENTRY;
797
798         /*
799          * currently this code is supposed to be called from declaration
800          * phase only, thus the object is not expected to be locked by caller
801          */
802         dt_write_lock(env, next, 0);
803         /* already initialized? */
804         if (lo->ldo_stripe) {
805                 int i;
806                 /* check validity */
807                 for (i = 0; i < lo->ldo_stripenr; i++)
808                         LASSERTF(lo->ldo_stripe[i], "stripe %d is NULL\n", i);
809                 GOTO(out, rc = 0);
810         }
811
812         if (!dt_object_exists(next))
813                 GOTO(out, rc = 0);
814
815         /* only regular files can be striped */
816         if (!(lu_object_attr(lod2lu_obj(lo)) & S_IFREG))
817                 GOTO(out, rc = 0);
818
819         LASSERT(lo->ldo_stripenr == 0);
820
821         rc = lod_get_lov_ea(env, lo);
822         if (rc <= 0)
823                 GOTO(out, rc);
824
825         /*
826          * there is LOV EA (striping information) in this object
827          * let's parse it and create in-core objects for the stripes
828          */
829         info->lti_buf.lb_buf = info->lti_ea_store;
830         info->lti_buf.lb_len = info->lti_ea_store_size;
831         rc = lod_parse_striping(env, lo, &info->lti_buf);
832 out:
833         dt_write_unlock(env, next);
834         RETURN(rc);
835 }
836
837 int lod_verify_striping(struct lod_device *d, const struct lu_buf *buf,
838                         int specific)
839 {
840         struct lov_user_md_v1   *lum;
841         struct lov_user_md_v3   *v3 = NULL;
842         struct pool_desc        *pool = NULL;
843         int                      rc;
844         ENTRY;
845
846         lum = buf->lb_buf;
847
848         if (lum->lmm_magic != LOV_USER_MAGIC_V1 &&
849             lum->lmm_magic != LOV_USER_MAGIC_V3 &&
850             lum->lmm_magic != LOV_MAGIC_V1_DEF &&
851             lum->lmm_magic != LOV_MAGIC_V3_DEF) {
852                 CDEBUG(D_IOCTL, "bad userland LOV MAGIC: %#x\n",
853                        lum->lmm_magic);
854                 RETURN(-EINVAL);
855         }
856
857         if ((specific && lum->lmm_pattern != LOV_PATTERN_RAID0) ||
858             (specific == 0 && lum->lmm_pattern != 0)) {
859                 CDEBUG(D_IOCTL, "bad userland stripe pattern: %#x\n",
860                        lum->lmm_pattern);
861                 RETURN(-EINVAL);
862         }
863
864         /* 64kB is the largest common page size we see (ia64), and matches the
865          * check in lfs */
866         if (lum->lmm_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
867                 CDEBUG(D_IOCTL, "stripe size %u not multiple of %u, fixing\n",
868                        lum->lmm_stripe_size, LOV_MIN_STRIPE_SIZE);
869                 RETURN(-EINVAL);
870         }
871
872         /* an offset of -1 is treated as a "special" valid offset */
873         if (lum->lmm_stripe_offset != (typeof(lum->lmm_stripe_offset))(-1)) {
874                 /* if offset is not within valid range [0, osts_size) */
875                 if (lum->lmm_stripe_offset >= d->lod_osts_size) {
876                         CDEBUG(D_IOCTL, "stripe offset %u >= bitmap size %u\n",
877                                lum->lmm_stripe_offset, d->lod_osts_size);
878                         RETURN(-EINVAL);
879                 }
880
881                 /* if lmm_stripe_offset is *not* in bitmap */
882                 if (!cfs_bitmap_check(d->lod_ost_bitmap,
883                                       lum->lmm_stripe_offset)) {
884                         CDEBUG(D_IOCTL, "stripe offset %u not in bitmap\n",
885                                lum->lmm_stripe_offset);
886                         RETURN(-EINVAL);
887                 }
888         }
889
890         if (lum->lmm_magic == LOV_USER_MAGIC_V3)
891                 v3 = buf->lb_buf;
892
893         if (v3)
894                 /* In the function below, .hs_keycmp resolves to
895                  * pool_hashkey_keycmp() */
896                 /* coverity[overrun-buffer-val] */
897                 pool = lod_find_pool(d, v3->lmm_pool_name);
898
899         if (pool != NULL) {
900                 __u16 offs = v3->lmm_stripe_offset;
901
902                 if (offs != (typeof(v3->lmm_stripe_offset))(-1)) {
903                         rc = lod_check_index_in_pool(offs, pool);
904                         if (rc < 0) {
905                                 lod_pool_putref(pool);
906                                 RETURN(-EINVAL);
907                         }
908                 }
909
910                 if (specific && lum->lmm_stripe_count > pool_tgt_count(pool)) {
911                         CDEBUG(D_IOCTL,
912                                "stripe count %u > # OSTs %u in the pool\n",
913                                lum->lmm_stripe_count, pool_tgt_count(pool));
914                         lod_pool_putref(pool);
915                         RETURN(-EINVAL);
916                 }
917
918                 lod_pool_putref(pool);
919         }
920
921         RETURN(0);
922 }
923
924 void lod_fix_desc_stripe_size(__u64 *val)
925 {
926         if (*val < LOV_DEFAULT_STRIPE_SIZE) {
927                 LCONSOLE_WARN("Increasing default stripe size to min %u\n",
928                               LOV_DEFAULT_STRIPE_SIZE);
929                 *val = LOV_DEFAULT_STRIPE_SIZE;
930         } else if (*val & (LOV_MIN_STRIPE_SIZE - 1)) {
931                 *val &= ~(LOV_MIN_STRIPE_SIZE - 1);
932                 LCONSOLE_WARN("Changing default stripe size to "LPU64" (a "
933                               "multiple of %u)\n",
934                               *val, LOV_MIN_STRIPE_SIZE);
935         }
936 }
937
938 void lod_fix_desc_stripe_count(__u32 *val)
939 {
940         if (*val == 0)
941                 *val = 1;
942 }
943
944 void lod_fix_desc_pattern(__u32 *val)
945 {
946         /* from lov_setstripe */
947         if ((*val != 0) && (*val != LOV_PATTERN_RAID0)) {
948                 LCONSOLE_WARN("Unknown stripe pattern: %#x\n", *val);
949                 *val = 0;
950         }
951 }
952
953 void lod_fix_desc_qos_maxage(__u32 *val)
954 {
955         /* fix qos_maxage */
956         if (*val == 0)
957                 *val = QOS_DEFAULT_MAXAGE;
958 }
959
960 void lod_fix_desc(struct lov_desc *desc)
961 {
962         lod_fix_desc_stripe_size(&desc->ld_default_stripe_size);
963         lod_fix_desc_stripe_count(&desc->ld_default_stripe_count);
964         lod_fix_desc_pattern(&desc->ld_pattern);
965         lod_fix_desc_qos_maxage(&desc->ld_qos_maxage);
966 }
967
968 int lod_pools_init(struct lod_device *lod, struct lustre_cfg *lcfg)
969 {
970         struct obd_device          *obd;
971         struct lov_desc            *desc;
972         int                         rc;
973         ENTRY;
974
975         obd = class_name2obd(lustre_cfg_string(lcfg, 0));
976         LASSERT(obd != NULL);
977         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
978
979         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
980                 CERROR("LOD setup requires a descriptor\n");
981                 RETURN(-EINVAL);
982         }
983
984         desc = (struct lov_desc *)lustre_cfg_buf(lcfg, 1);
985
986         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
987                 CERROR("descriptor size wrong: %d > %d\n",
988                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
989                 RETURN(-EINVAL);
990         }
991
992         if (desc->ld_magic != LOV_DESC_MAGIC) {
993                 if (desc->ld_magic == __swab32(LOV_DESC_MAGIC)) {
994                         CDEBUG(D_OTHER, "%s: Swabbing lov desc %p\n",
995                                obd->obd_name, desc);
996                         lustre_swab_lov_desc(desc);
997                 } else {
998                         CERROR("%s: Bad lov desc magic: %#x\n",
999                                obd->obd_name, desc->ld_magic);
1000                         RETURN(-EINVAL);
1001                 }
1002         }
1003
1004         lod_fix_desc(desc);
1005
1006         desc->ld_active_tgt_count = 0;
1007         lod->lod_desc = *desc;
1008
1009         lod->lod_sp_me = LUSTRE_SP_CLI;
1010
1011         /* Set up allocation policy (QoS and RR) */
1012         CFS_INIT_LIST_HEAD(&lod->lod_qos.lq_oss_list);
1013         init_rwsem(&lod->lod_qos.lq_rw_sem);
1014         lod->lod_qos.lq_dirty = 1;
1015         lod->lod_qos.lq_rr.lqr_dirty = 1;
1016         lod->lod_qos.lq_reset = 1;
1017         /* Default priority is toward free space balance */
1018         lod->lod_qos.lq_prio_free = 232;
1019         /* Default threshold for rr (roughly 17%) */
1020         lod->lod_qos.lq_threshold_rr = 43;
1021         /* Init statfs fields */
1022         OBD_ALLOC_PTR(lod->lod_qos.lq_statfs_data);
1023         if (NULL == lod->lod_qos.lq_statfs_data)
1024                 RETURN(-ENOMEM);
1025         cfs_waitq_init(&lod->lod_qos.lq_statfs_waitq);
1026
1027         /* Set up OST pool environment */
1028         lod->lod_pools_hash_body = cfs_hash_create("POOLS", HASH_POOLS_CUR_BITS,
1029                                                    HASH_POOLS_MAX_BITS,
1030                                                    HASH_POOLS_BKT_BITS, 0,
1031                                                    CFS_HASH_MIN_THETA,
1032                                                    CFS_HASH_MAX_THETA,
1033                                                    &pool_hash_operations,
1034                                                    CFS_HASH_DEFAULT);
1035         if (!lod->lod_pools_hash_body)
1036                 GOTO(out_statfs, rc = -ENOMEM);
1037         CFS_INIT_LIST_HEAD(&lod->lod_pool_list);
1038         lod->lod_pool_count = 0;
1039         rc = lod_ost_pool_init(&lod->lod_pool_info, 0);
1040         if (rc)
1041                 GOTO(out_hash, rc);
1042         rc = lod_ost_pool_init(&lod->lod_qos.lq_rr.lqr_pool, 0);
1043         if (rc)
1044                 GOTO(out_pool_info, rc);
1045
1046         RETURN(0);
1047
1048 out_pool_info:
1049         lod_ost_pool_free(&lod->lod_pool_info);
1050 out_hash:
1051         cfs_hash_putref(lod->lod_pools_hash_body);
1052 out_statfs:
1053         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
1054         return rc;
1055 }
1056
1057 int lod_pools_fini(struct lod_device *lod)
1058 {
1059         struct obd_device   *obd = lod2obd(lod);
1060         cfs_list_t          *pos, *tmp;
1061         struct pool_desc    *pool;
1062         ENTRY;
1063
1064         cfs_list_for_each_safe(pos, tmp, &lod->lod_pool_list) {
1065                 pool = cfs_list_entry(pos, struct pool_desc, pool_list);
1066                 /* free pool structs */
1067                 CDEBUG(D_INFO, "delete pool %p\n", pool);
1068                 /* In the function below, .hs_keycmp resolves to
1069                  * pool_hashkey_keycmp() */
1070                 /* coverity[overrun-buffer-val] */
1071                 lod_pool_del(obd, pool->pool_name);
1072         }
1073
1074         cfs_hash_putref(lod->lod_pools_hash_body);
1075         lod_ost_pool_free(&(lod->lod_qos.lq_rr.lqr_pool));
1076         lod_ost_pool_free(&lod->lod_pool_info);
1077         OBD_FREE_PTR(lod->lod_qos.lq_statfs_data);
1078         RETURN(0);
1079 }
1080