Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License version 2 for more details (a copy is included |
| 14 | * in the LICENSE file that accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 along with this program; If not, see |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | * |
Andreas Dilger | 1dc563a | 2015-11-08 18:09:37 -0500 | [diff] [blame] | 26 | * Copyright (c) 2011, 2015, Intel Corporation. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 27 | */ |
| 28 | /* |
| 29 | * This file is part of Lustre, http://www.lustre.org/ |
| 30 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 31 | * |
| 32 | * Implementation of cl_object for LOV layer. |
| 33 | * |
| 34 | * Author: Nikita Danilov <nikita.danilov@sun.com> |
| 35 | * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com> |
| 36 | */ |
| 37 | |
| 38 | #define DEBUG_SUBSYSTEM S_LOV |
| 39 | |
| 40 | #include "lov_cl_internal.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 41 | |
| 42 | /** \addtogroup lov |
| 43 | * @{ |
| 44 | */ |
| 45 | |
| 46 | /***************************************************************************** |
| 47 | * |
| 48 | * Layout operations. |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | struct lov_layout_operations { |
| 53 | int (*llo_init)(const struct lu_env *env, struct lov_device *dev, |
| 54 | struct lov_object *lov, |
| 55 | const struct cl_object_conf *conf, |
| 56 | union lov_layout_state *state); |
| 57 | int (*llo_delete)(const struct lu_env *env, struct lov_object *lov, |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 58 | union lov_layout_state *state); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 59 | void (*llo_fini)(const struct lu_env *env, struct lov_object *lov, |
| 60 | union lov_layout_state *state); |
| 61 | void (*llo_install)(const struct lu_env *env, struct lov_object *lov, |
| 62 | union lov_layout_state *state); |
| 63 | int (*llo_print)(const struct lu_env *env, void *cookie, |
| 64 | lu_printer_t p, const struct lu_object *o); |
| 65 | int (*llo_page_init)(const struct lu_env *env, struct cl_object *obj, |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 66 | struct cl_page *page, pgoff_t index); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 67 | int (*llo_lock_init)(const struct lu_env *env, |
| 68 | struct cl_object *obj, struct cl_lock *lock, |
| 69 | const struct cl_io *io); |
| 70 | int (*llo_io_init)(const struct lu_env *env, |
| 71 | struct cl_object *obj, struct cl_io *io); |
| 72 | int (*llo_getattr)(const struct lu_env *env, struct cl_object *obj, |
| 73 | struct cl_attr *attr); |
| 74 | }; |
| 75 | |
| 76 | static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov); |
| 77 | |
| 78 | /***************************************************************************** |
| 79 | * |
| 80 | * Lov object layout operations. |
| 81 | * |
| 82 | */ |
| 83 | |
| 84 | static void lov_install_empty(const struct lu_env *env, |
| 85 | struct lov_object *lov, |
| 86 | union lov_layout_state *state) |
| 87 | { |
| 88 | /* |
| 89 | * File without objects. |
| 90 | */ |
| 91 | } |
| 92 | |
| 93 | static int lov_init_empty(const struct lu_env *env, |
| 94 | struct lov_device *dev, struct lov_object *lov, |
| 95 | const struct cl_object_conf *conf, |
| 96 | union lov_layout_state *state) |
| 97 | { |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void lov_install_raid0(const struct lu_env *env, |
| 102 | struct lov_object *lov, |
| 103 | union lov_layout_state *state) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | static struct cl_object *lov_sub_find(const struct lu_env *env, |
| 108 | struct cl_device *dev, |
| 109 | const struct lu_fid *fid, |
| 110 | const struct cl_object_conf *conf) |
| 111 | { |
| 112 | struct lu_object *o; |
| 113 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 114 | o = lu_object_find_at(env, cl2lu_dev(dev), fid, &conf->coc_lu); |
| 115 | LASSERT(ergo(!IS_ERR(o), o->lo_dev->ld_type == &lovsub_device_type)); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 116 | return lu2cl(o); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int lov_init_sub(const struct lu_env *env, struct lov_object *lov, |
Bobi Jam | f34b6cd | 2014-02-09 02:51:45 -0500 | [diff] [blame] | 120 | struct cl_object *stripe, struct lov_layout_raid0 *r0, |
| 121 | int idx) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 122 | { |
| 123 | struct cl_object_header *hdr; |
| 124 | struct cl_object_header *subhdr; |
| 125 | struct cl_object_header *parent; |
| 126 | struct lov_oinfo *oinfo; |
| 127 | int result; |
| 128 | |
| 129 | if (OBD_FAIL_CHECK(OBD_FAIL_LOV_INIT)) { |
| 130 | /* For sanity:test_206. |
| 131 | * Do not leave the object in cache to avoid accessing |
| 132 | * freed memory. This is because osc_object is referring to |
| 133 | * lov_oinfo of lsm_stripe_data which will be freed due to |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 134 | * this failure. |
| 135 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 136 | cl_object_kill(env, stripe); |
| 137 | cl_object_put(env, stripe); |
| 138 | return -EIO; |
| 139 | } |
| 140 | |
| 141 | hdr = cl_object_header(lov2cl(lov)); |
| 142 | subhdr = cl_object_header(stripe); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 143 | |
| 144 | oinfo = lov->lo_lsm->lsm_oinfo[idx]; |
| 145 | CDEBUG(D_INODE, DFID"@%p[%d] -> "DFID"@%p: ostid: "DOSTID |
| 146 | " idx: %d gen: %d\n", |
| 147 | PFID(&subhdr->coh_lu.loh_fid), subhdr, idx, |
| 148 | PFID(&hdr->coh_lu.loh_fid), hdr, POSTID(&oinfo->loi_oi), |
| 149 | oinfo->loi_ost_idx, oinfo->loi_ost_gen); |
| 150 | |
Bobi Jam | f34b6cd | 2014-02-09 02:51:45 -0500 | [diff] [blame] | 151 | /* reuse ->coh_attr_guard to protect coh_parent change */ |
| 152 | spin_lock(&subhdr->coh_attr_guard); |
| 153 | parent = subhdr->coh_parent; |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 154 | if (!parent) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 155 | subhdr->coh_parent = hdr; |
Bobi Jam | f34b6cd | 2014-02-09 02:51:45 -0500 | [diff] [blame] | 156 | spin_unlock(&subhdr->coh_attr_guard); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 157 | subhdr->coh_nesting = hdr->coh_nesting + 1; |
| 158 | lu_object_ref_add(&stripe->co_lu, "lov-parent", lov); |
| 159 | r0->lo_sub[idx] = cl2lovsub(stripe); |
| 160 | r0->lo_sub[idx]->lso_super = lov; |
| 161 | r0->lo_sub[idx]->lso_index = idx; |
| 162 | result = 0; |
| 163 | } else { |
| 164 | struct lu_object *old_obj; |
| 165 | struct lov_object *old_lov; |
| 166 | unsigned int mask = D_INODE; |
| 167 | |
Bobi Jam | f34b6cd | 2014-02-09 02:51:45 -0500 | [diff] [blame] | 168 | spin_unlock(&subhdr->coh_attr_guard); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 169 | old_obj = lu_object_locate(&parent->coh_lu, &lov_device_type); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 170 | LASSERT(old_obj); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 171 | old_lov = cl2lov(lu2cl(old_obj)); |
| 172 | if (old_lov->lo_layout_invalid) { |
| 173 | /* the object's layout has already changed but isn't |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 174 | * refreshed |
| 175 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 176 | lu_object_unhash(env, &stripe->co_lu); |
| 177 | result = -EAGAIN; |
| 178 | } else { |
| 179 | mask = D_ERROR; |
| 180 | result = -EIO; |
| 181 | } |
| 182 | |
| 183 | LU_OBJECT_DEBUG(mask, env, &stripe->co_lu, |
Alex Zhuravlev | e93876d | 2016-06-20 16:55:36 -0400 | [diff] [blame] | 184 | "stripe %d is already owned.", idx); |
| 185 | LU_OBJECT_DEBUG(mask, env, old_obj, "owned."); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 186 | LU_OBJECT_HEADER(mask, env, lov2lu(lov), "try to own.\n"); |
| 187 | cl_object_put(env, stripe); |
| 188 | } |
| 189 | return result; |
| 190 | } |
| 191 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 192 | static int lov_page_slice_fixup(struct lov_object *lov, |
| 193 | struct cl_object *stripe) |
| 194 | { |
| 195 | struct cl_object_header *hdr = cl_object_header(&lov->lo_cl); |
| 196 | struct cl_object *o; |
| 197 | |
| 198 | cl_object_for_each(o, stripe) |
| 199 | o->co_slice_off += hdr->coh_page_bufsize; |
| 200 | |
| 201 | return cl_object_header(stripe)->coh_page_bufsize; |
| 202 | } |
| 203 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 204 | static int lov_init_raid0(const struct lu_env *env, |
| 205 | struct lov_device *dev, struct lov_object *lov, |
| 206 | const struct cl_object_conf *conf, |
| 207 | union lov_layout_state *state) |
| 208 | { |
| 209 | int result; |
| 210 | int i; |
| 211 | |
| 212 | struct cl_object *stripe; |
| 213 | struct lov_thread_info *lti = lov_env_info(env); |
| 214 | struct cl_object_conf *subconf = <i->lti_stripe_conf; |
| 215 | struct lov_stripe_md *lsm = conf->u.coc_md->lsm; |
| 216 | struct lu_fid *ofid = <i->lti_fid; |
| 217 | struct lov_layout_raid0 *r0 = &state->raid0; |
| 218 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 219 | if (lsm->lsm_magic != LOV_MAGIC_V1 && lsm->lsm_magic != LOV_MAGIC_V3) { |
| 220 | dump_lsm(D_ERROR, lsm); |
| 221 | LASSERTF(0, "magic mismatch, expected %d/%d, actual %d.\n", |
| 222 | LOV_MAGIC_V1, LOV_MAGIC_V3, lsm->lsm_magic); |
| 223 | } |
| 224 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 225 | LASSERT(!lov->lo_lsm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 226 | lov->lo_lsm = lsm_addref(lsm); |
| 227 | r0->lo_nr = lsm->lsm_stripe_count; |
| 228 | LASSERT(r0->lo_nr <= lov_targets_nr(dev)); |
| 229 | |
Julia Lawall | 3d0ba71 | 2015-06-11 14:02:54 +0200 | [diff] [blame] | 230 | r0->lo_sub = libcfs_kvzalloc(r0->lo_nr * sizeof(r0->lo_sub[0]), |
| 231 | GFP_NOFS); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 232 | if (r0->lo_sub) { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 233 | int psz = 0; |
| 234 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 235 | result = 0; |
| 236 | subconf->coc_inode = conf->coc_inode; |
| 237 | spin_lock_init(&r0->lo_sub_lock); |
| 238 | /* |
| 239 | * Create stripe cl_objects. |
| 240 | */ |
| 241 | for (i = 0; i < r0->lo_nr && result == 0; ++i) { |
| 242 | struct cl_device *subdev; |
| 243 | struct lov_oinfo *oinfo = lsm->lsm_oinfo[i]; |
| 244 | int ost_idx = oinfo->loi_ost_idx; |
| 245 | |
Yang Sheng | 397632e | 2015-03-25 21:53:22 -0400 | [diff] [blame] | 246 | if (lov_oinfo_is_dummy(oinfo)) |
| 247 | continue; |
| 248 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 249 | result = ostid_to_fid(ofid, &oinfo->loi_oi, |
| 250 | oinfo->loi_ost_idx); |
| 251 | if (result != 0) |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 252 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 253 | |
| 254 | subdev = lovsub2cl_dev(dev->ld_target[ost_idx]); |
| 255 | subconf->u.coc_oinfo = oinfo; |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 256 | LASSERTF(subdev, "not init ost %d\n", ost_idx); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 257 | /* In the function below, .hs_keycmp resolves to |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 258 | * lu_obj_hop_keycmp() |
| 259 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 260 | /* coverity[overrun-buffer-val] */ |
| 261 | stripe = lov_sub_find(env, subdev, ofid, subconf); |
| 262 | if (!IS_ERR(stripe)) { |
| 263 | result = lov_init_sub(env, lov, stripe, r0, i); |
| 264 | if (result == -EAGAIN) { /* try again */ |
| 265 | --i; |
| 266 | result = 0; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 267 | continue; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 268 | } |
| 269 | } else { |
| 270 | result = PTR_ERR(stripe); |
| 271 | } |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 272 | |
| 273 | if (result == 0) { |
| 274 | int sz = lov_page_slice_fixup(lov, stripe); |
| 275 | |
| 276 | LASSERT(ergo(psz > 0, psz == sz)); |
| 277 | psz = sz; |
| 278 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 279 | } |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 280 | if (result == 0) |
| 281 | cl_object_header(&lov->lo_cl)->coh_page_bufsize += psz; |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 282 | } else { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 283 | result = -ENOMEM; |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 284 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 285 | out: |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 286 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 287 | } |
| 288 | |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 289 | static int lov_init_released(const struct lu_env *env, |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 290 | struct lov_device *dev, struct lov_object *lov, |
| 291 | const struct cl_object_conf *conf, |
| 292 | union lov_layout_state *state) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 293 | { |
| 294 | struct lov_stripe_md *lsm = conf->u.coc_md->lsm; |
| 295 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 296 | LASSERT(lsm); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 297 | LASSERT(lsm_is_released(lsm)); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 298 | LASSERT(!lov->lo_lsm); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 299 | |
| 300 | lov->lo_lsm = lsm_addref(lsm); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 304 | static int lov_delete_empty(const struct lu_env *env, struct lov_object *lov, |
| 305 | union lov_layout_state *state) |
| 306 | { |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 307 | LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 308 | |
| 309 | lov_layout_wait(env, lov); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void lov_subobject_kill(const struct lu_env *env, struct lov_object *lov, |
| 314 | struct lovsub_object *los, int idx) |
| 315 | { |
| 316 | struct cl_object *sub; |
| 317 | struct lov_layout_raid0 *r0; |
| 318 | struct lu_site *site; |
| 319 | struct lu_site_bkt_data *bkt; |
| 320 | wait_queue_t *waiter; |
| 321 | |
| 322 | r0 = &lov->u.raid0; |
| 323 | LASSERT(r0->lo_sub[idx] == los); |
| 324 | |
| 325 | sub = lovsub2cl(los); |
| 326 | site = sub->co_lu.lo_dev->ld_site; |
| 327 | bkt = lu_site_bkt_from_fid(site, &sub->co_lu.lo_header->loh_fid); |
| 328 | |
| 329 | cl_object_kill(env, sub); |
| 330 | /* release a reference to the sub-object and ... */ |
| 331 | lu_object_ref_del(&sub->co_lu, "lov-parent", lov); |
| 332 | cl_object_put(env, sub); |
| 333 | |
| 334 | /* ... wait until it is actually destroyed---sub-object clears its |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 335 | * ->lo_sub[] slot in lovsub_object_fini() |
| 336 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 337 | if (r0->lo_sub[idx] == los) { |
| 338 | waiter = &lov_env_info(env)->lti_waiter; |
Peng Tao | 9e795d3 | 2014-03-18 21:05:52 +0800 | [diff] [blame] | 339 | init_waitqueue_entry(waiter, current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 340 | add_wait_queue(&bkt->lsb_marche_funebre, waiter); |
| 341 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 342 | while (1) { |
| 343 | /* this wait-queue is signaled at the end of |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 344 | * lu_object_free(). |
| 345 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 346 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 347 | spin_lock(&r0->lo_sub_lock); |
| 348 | if (r0->lo_sub[idx] == los) { |
| 349 | spin_unlock(&r0->lo_sub_lock); |
Peng Tao | b3669a7 | 2014-03-18 21:05:53 +0800 | [diff] [blame] | 350 | schedule(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 351 | } else { |
| 352 | spin_unlock(&r0->lo_sub_lock); |
| 353 | set_current_state(TASK_RUNNING); |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | remove_wait_queue(&bkt->lsb_marche_funebre, waiter); |
| 358 | } |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 359 | LASSERT(!r0->lo_sub[idx]); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static int lov_delete_raid0(const struct lu_env *env, struct lov_object *lov, |
| 363 | union lov_layout_state *state) |
| 364 | { |
| 365 | struct lov_layout_raid0 *r0 = &state->raid0; |
| 366 | struct lov_stripe_md *lsm = lov->lo_lsm; |
| 367 | int i; |
| 368 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 369 | dump_lsm(D_INODE, lsm); |
| 370 | |
| 371 | lov_layout_wait(env, lov); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 372 | if (r0->lo_sub) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 373 | for (i = 0; i < r0->lo_nr; ++i) { |
| 374 | struct lovsub_object *los = r0->lo_sub[i]; |
| 375 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 376 | if (los) { |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 377 | cl_object_prune(env, &los->lso_cl); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 378 | /* |
| 379 | * If top-level object is to be evicted from |
| 380 | * the cache, so are its sub-objects. |
| 381 | */ |
| 382 | lov_subobject_kill(env, lov, los, i); |
| 383 | } |
| 384 | } |
| 385 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 386 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void lov_fini_empty(const struct lu_env *env, struct lov_object *lov, |
| 390 | union lov_layout_state *state) |
| 391 | { |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 392 | LASSERT(lov->lo_type == LLT_EMPTY || lov->lo_type == LLT_RELEASED); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void lov_fini_raid0(const struct lu_env *env, struct lov_object *lov, |
| 396 | union lov_layout_state *state) |
| 397 | { |
| 398 | struct lov_layout_raid0 *r0 = &state->raid0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 399 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 400 | if (r0->lo_sub) { |
Julia Lawall | 3d0ba71 | 2015-06-11 14:02:54 +0200 | [diff] [blame] | 401 | kvfree(r0->lo_sub); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 402 | r0->lo_sub = NULL; |
| 403 | } |
| 404 | |
| 405 | dump_lsm(D_INODE, lov->lo_lsm); |
| 406 | lov_free_memmd(&lov->lo_lsm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 407 | } |
| 408 | |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 409 | static void lov_fini_released(const struct lu_env *env, struct lov_object *lov, |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 410 | union lov_layout_state *state) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 411 | { |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 412 | dump_lsm(D_INODE, lov->lo_lsm); |
| 413 | lov_free_memmd(&lov->lo_lsm); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 416 | static int lov_print_empty(const struct lu_env *env, void *cookie, |
| 417 | lu_printer_t p, const struct lu_object *o) |
| 418 | { |
| 419 | (*p)(env, cookie, "empty %d\n", lu2lov(o)->lo_layout_invalid); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int lov_print_raid0(const struct lu_env *env, void *cookie, |
| 424 | lu_printer_t p, const struct lu_object *o) |
| 425 | { |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 426 | struct lov_object *lov = lu2lov(o); |
| 427 | struct lov_layout_raid0 *r0 = lov_r0(lov); |
| 428 | struct lov_stripe_md *lsm = lov->lo_lsm; |
| 429 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 430 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 431 | (*p)(env, cookie, "stripes: %d, %s, lsm{%p 0x%08X %d %u %u}:\n", |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 432 | r0->lo_nr, lov->lo_layout_invalid ? "invalid" : "valid", lsm, |
| 433 | lsm->lsm_magic, atomic_read(&lsm->lsm_refc), |
| 434 | lsm->lsm_stripe_count, lsm->lsm_layout_gen); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 435 | for (i = 0; i < r0->lo_nr; ++i) { |
| 436 | struct lu_object *sub; |
| 437 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 438 | if (r0->lo_sub[i]) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 439 | sub = lovsub2lu(r0->lo_sub[i]); |
| 440 | lu_object_print(env, cookie, p, sub); |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 441 | } else { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 442 | (*p)(env, cookie, "sub %d absent\n", i); |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 443 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 448 | static int lov_print_released(const struct lu_env *env, void *cookie, |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 449 | lu_printer_t p, const struct lu_object *o) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 450 | { |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 451 | struct lov_object *lov = lu2lov(o); |
| 452 | struct lov_stripe_md *lsm = lov->lo_lsm; |
| 453 | |
| 454 | (*p)(env, cookie, |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 455 | "released: %s, lsm{%p 0x%08X %d %u %u}:\n", |
| 456 | lov->lo_layout_invalid ? "invalid" : "valid", lsm, |
| 457 | lsm->lsm_magic, atomic_read(&lsm->lsm_refc), |
| 458 | lsm->lsm_stripe_count, lsm->lsm_layout_gen); |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 462 | /** |
| 463 | * Implements cl_object_operations::coo_attr_get() method for an object |
| 464 | * without stripes (LLT_EMPTY layout type). |
| 465 | * |
| 466 | * The only attributes this layer is authoritative in this case is |
| 467 | * cl_attr::cat_blocks---it's 0. |
| 468 | */ |
| 469 | static int lov_attr_get_empty(const struct lu_env *env, struct cl_object *obj, |
| 470 | struct cl_attr *attr) |
| 471 | { |
| 472 | attr->cat_blocks = 0; |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int lov_attr_get_raid0(const struct lu_env *env, struct cl_object *obj, |
| 477 | struct cl_attr *attr) |
| 478 | { |
| 479 | struct lov_object *lov = cl2lov(obj); |
| 480 | struct lov_layout_raid0 *r0 = lov_r0(lov); |
| 481 | struct cl_attr *lov_attr = &r0->lo_attr; |
| 482 | int result = 0; |
| 483 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 484 | /* this is called w/o holding type guard mutex, so it must be inside |
| 485 | * an on going IO otherwise lsm may be replaced. |
| 486 | * LU-2117: it turns out there exists one exception. For mmaped files, |
| 487 | * the lock of those files may be requested in the other file's IO |
| 488 | * context, and this function is called in ccc_lock_state(), it will |
| 489 | * hit this assertion. |
| 490 | * Anyway, it's still okay to call attr_get w/o type guard as layout |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 491 | * can't go if locks exist. |
| 492 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 493 | /* LASSERT(atomic_read(&lsm->lsm_refc) > 1); */ |
| 494 | |
| 495 | if (!r0->lo_attr_valid) { |
| 496 | struct lov_stripe_md *lsm = lov->lo_lsm; |
| 497 | struct ost_lvb *lvb = &lov_env_info(env)->lti_lvb; |
| 498 | __u64 kms = 0; |
| 499 | |
| 500 | memset(lvb, 0, sizeof(*lvb)); |
| 501 | /* XXX: timestamps can be negative by sanity:test_39m, |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 502 | * how can it be? |
| 503 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 504 | lvb->lvb_atime = LLONG_MIN; |
| 505 | lvb->lvb_ctime = LLONG_MIN; |
| 506 | lvb->lvb_mtime = LLONG_MIN; |
| 507 | |
| 508 | /* |
| 509 | * XXX that should be replaced with a loop over sub-objects, |
| 510 | * doing cl_object_attr_get() on them. But for now, let's |
| 511 | * reuse old lov code. |
| 512 | */ |
| 513 | |
| 514 | /* |
| 515 | * XXX take lsm spin-lock to keep lov_merge_lvb_kms() |
| 516 | * happy. It's not needed, because new code uses |
| 517 | * ->coh_attr_guard spin-lock to protect consistency of |
| 518 | * sub-object attributes. |
| 519 | */ |
| 520 | lov_stripe_lock(lsm); |
| 521 | result = lov_merge_lvb_kms(lsm, lvb, &kms); |
| 522 | lov_stripe_unlock(lsm); |
| 523 | if (result == 0) { |
| 524 | cl_lvb2attr(lov_attr, lvb); |
| 525 | lov_attr->cat_kms = kms; |
| 526 | r0->lo_attr_valid = 1; |
| 527 | } |
| 528 | } |
| 529 | if (result == 0) { /* merge results */ |
| 530 | attr->cat_blocks = lov_attr->cat_blocks; |
| 531 | attr->cat_size = lov_attr->cat_size; |
| 532 | attr->cat_kms = lov_attr->cat_kms; |
| 533 | if (attr->cat_atime < lov_attr->cat_atime) |
| 534 | attr->cat_atime = lov_attr->cat_atime; |
| 535 | if (attr->cat_ctime < lov_attr->cat_ctime) |
| 536 | attr->cat_ctime = lov_attr->cat_ctime; |
| 537 | if (attr->cat_mtime < lov_attr->cat_mtime) |
| 538 | attr->cat_mtime = lov_attr->cat_mtime; |
| 539 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 540 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 541 | } |
| 542 | |
Tobias Klauser | fcda2f5 | 2014-03-05 17:37:05 +0100 | [diff] [blame] | 543 | static const struct lov_layout_operations lov_dispatch[] = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 544 | [LLT_EMPTY] = { |
| 545 | .llo_init = lov_init_empty, |
| 546 | .llo_delete = lov_delete_empty, |
| 547 | .llo_fini = lov_fini_empty, |
| 548 | .llo_install = lov_install_empty, |
| 549 | .llo_print = lov_print_empty, |
| 550 | .llo_page_init = lov_page_init_empty, |
| 551 | .llo_lock_init = lov_lock_init_empty, |
| 552 | .llo_io_init = lov_io_init_empty, |
| 553 | .llo_getattr = lov_attr_get_empty |
| 554 | }, |
| 555 | [LLT_RAID0] = { |
| 556 | .llo_init = lov_init_raid0, |
| 557 | .llo_delete = lov_delete_raid0, |
| 558 | .llo_fini = lov_fini_raid0, |
| 559 | .llo_install = lov_install_raid0, |
| 560 | .llo_print = lov_print_raid0, |
| 561 | .llo_page_init = lov_page_init_raid0, |
| 562 | .llo_lock_init = lov_lock_init_raid0, |
| 563 | .llo_io_init = lov_io_init_raid0, |
| 564 | .llo_getattr = lov_attr_get_raid0 |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 565 | }, |
| 566 | [LLT_RELEASED] = { |
| 567 | .llo_init = lov_init_released, |
| 568 | .llo_delete = lov_delete_empty, |
| 569 | .llo_fini = lov_fini_released, |
| 570 | .llo_install = lov_install_empty, |
| 571 | .llo_print = lov_print_released, |
| 572 | .llo_page_init = lov_page_init_empty, |
| 573 | .llo_lock_init = lov_lock_init_empty, |
| 574 | .llo_io_init = lov_io_init_released, |
| 575 | .llo_getattr = lov_attr_get_empty |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 576 | } |
| 577 | }; |
| 578 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 579 | /** |
| 580 | * Performs a double-dispatch based on the layout type of an object. |
| 581 | */ |
| 582 | #define LOV_2DISPATCH_NOLOCK(obj, op, ...) \ |
| 583 | ({ \ |
| 584 | struct lov_object *__obj = (obj); \ |
| 585 | enum lov_layout_type __llt; \ |
| 586 | \ |
| 587 | __llt = __obj->lo_type; \ |
| 588 | LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \ |
| 589 | lov_dispatch[__llt].op(__VA_ARGS__); \ |
| 590 | }) |
| 591 | |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 592 | /** |
| 593 | * Return lov_layout_type associated with a given lsm |
| 594 | */ |
Le Tan | 12e397c | 2015-02-11 12:13:14 +0800 | [diff] [blame] | 595 | static enum lov_layout_type lov_type(struct lov_stripe_md *lsm) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 596 | { |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 597 | if (!lsm) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 598 | return LLT_EMPTY; |
| 599 | if (lsm_is_released(lsm)) |
| 600 | return LLT_RELEASED; |
| 601 | return LLT_RAID0; |
| 602 | } |
| 603 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 604 | static inline void lov_conf_freeze(struct lov_object *lov) |
| 605 | { |
| 606 | if (lov->lo_owner != current) |
| 607 | down_read(&lov->lo_type_guard); |
| 608 | } |
| 609 | |
| 610 | static inline void lov_conf_thaw(struct lov_object *lov) |
| 611 | { |
| 612 | if (lov->lo_owner != current) |
| 613 | up_read(&lov->lo_type_guard); |
| 614 | } |
| 615 | |
| 616 | #define LOV_2DISPATCH_MAYLOCK(obj, op, lock, ...) \ |
| 617 | ({ \ |
| 618 | struct lov_object *__obj = (obj); \ |
| 619 | int __lock = !!(lock); \ |
| 620 | typeof(lov_dispatch[0].op(__VA_ARGS__)) __result; \ |
| 621 | \ |
| 622 | if (__lock) \ |
| 623 | lov_conf_freeze(__obj); \ |
| 624 | __result = LOV_2DISPATCH_NOLOCK(obj, op, __VA_ARGS__); \ |
| 625 | if (__lock) \ |
| 626 | lov_conf_thaw(__obj); \ |
| 627 | __result; \ |
| 628 | }) |
| 629 | |
| 630 | /** |
| 631 | * Performs a locked double-dispatch based on the layout type of an object. |
| 632 | */ |
| 633 | #define LOV_2DISPATCH(obj, op, ...) \ |
| 634 | LOV_2DISPATCH_MAYLOCK(obj, op, 1, __VA_ARGS__) |
| 635 | |
| 636 | #define LOV_2DISPATCH_VOID(obj, op, ...) \ |
| 637 | do { \ |
| 638 | struct lov_object *__obj = (obj); \ |
| 639 | enum lov_layout_type __llt; \ |
| 640 | \ |
| 641 | lov_conf_freeze(__obj); \ |
| 642 | __llt = __obj->lo_type; \ |
| 643 | LASSERT(0 <= __llt && __llt < ARRAY_SIZE(lov_dispatch)); \ |
| 644 | lov_dispatch[__llt].op(__VA_ARGS__); \ |
| 645 | lov_conf_thaw(__obj); \ |
| 646 | } while (0) |
| 647 | |
| 648 | static void lov_conf_lock(struct lov_object *lov) |
| 649 | { |
| 650 | LASSERT(lov->lo_owner != current); |
| 651 | down_write(&lov->lo_type_guard); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 652 | LASSERT(!lov->lo_owner); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 653 | lov->lo_owner = current; |
| 654 | } |
| 655 | |
| 656 | static void lov_conf_unlock(struct lov_object *lov) |
| 657 | { |
| 658 | lov->lo_owner = NULL; |
| 659 | up_write(&lov->lo_type_guard); |
| 660 | } |
| 661 | |
| 662 | static int lov_layout_wait(const struct lu_env *env, struct lov_object *lov) |
| 663 | { |
| 664 | struct l_wait_info lwi = { 0 }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 665 | |
| 666 | while (atomic_read(&lov->lo_active_ios) > 0) { |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 667 | CDEBUG(D_INODE, "file:" DFID " wait for active IO, now: %d.\n", |
| 668 | PFID(lu_object_fid(lov2lu(lov))), |
| 669 | atomic_read(&lov->lo_active_ios)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 670 | |
| 671 | l_wait_event(lov->lo_waitq, |
| 672 | atomic_read(&lov->lo_active_ios) == 0, &lwi); |
| 673 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 674 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static int lov_layout_change(const struct lu_env *unused, |
| 678 | struct lov_object *lov, |
| 679 | const struct cl_object_conf *conf) |
| 680 | { |
| 681 | int result; |
| 682 | enum lov_layout_type llt = LLT_EMPTY; |
| 683 | union lov_layout_state *state = &lov->u; |
| 684 | const struct lov_layout_operations *old_ops; |
| 685 | const struct lov_layout_operations *new_ops; |
| 686 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 687 | void *cookie; |
| 688 | struct lu_env *env; |
| 689 | int refcheck; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 690 | |
| 691 | LASSERT(0 <= lov->lo_type && lov->lo_type < ARRAY_SIZE(lov_dispatch)); |
| 692 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 693 | if (conf->u.coc_md) |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 694 | llt = lov_type(conf->u.coc_md->lsm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 695 | LASSERT(0 <= llt && llt < ARRAY_SIZE(lov_dispatch)); |
| 696 | |
| 697 | cookie = cl_env_reenter(); |
| 698 | env = cl_env_get(&refcheck); |
| 699 | if (IS_ERR(env)) { |
| 700 | cl_env_reexit(cookie); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 701 | return PTR_ERR(env); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 702 | } |
| 703 | |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 704 | CDEBUG(D_INODE, DFID" from %s to %s\n", |
| 705 | PFID(lu_object_fid(lov2lu(lov))), |
| 706 | llt2str(lov->lo_type), llt2str(llt)); |
| 707 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 708 | old_ops = &lov_dispatch[lov->lo_type]; |
| 709 | new_ops = &lov_dispatch[llt]; |
| 710 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 711 | result = cl_object_prune(env, &lov->lo_cl); |
| 712 | if (result != 0) |
| 713 | goto out; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 714 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 715 | result = old_ops->llo_delete(env, lov, &lov->u); |
| 716 | if (result == 0) { |
| 717 | old_ops->llo_fini(env, lov, &lov->u); |
| 718 | |
| 719 | LASSERT(atomic_read(&lov->lo_active_ios) == 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 720 | |
| 721 | lov->lo_type = LLT_EMPTY; |
| 722 | result = new_ops->llo_init(env, |
| 723 | lu2lov_dev(lov->lo_cl.co_lu.lo_dev), |
| 724 | lov, conf, state); |
| 725 | if (result == 0) { |
| 726 | new_ops->llo_install(env, lov, state); |
| 727 | lov->lo_type = llt; |
| 728 | } else { |
| 729 | new_ops->llo_delete(env, lov, state); |
| 730 | new_ops->llo_fini(env, lov, state); |
| 731 | /* this file becomes an EMPTY file. */ |
| 732 | } |
| 733 | } |
| 734 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 735 | out: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 736 | cl_env_put(env, &refcheck); |
| 737 | cl_env_reexit(cookie); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 738 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /***************************************************************************** |
| 742 | * |
| 743 | * Lov object operations. |
| 744 | * |
| 745 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 746 | int lov_object_init(const struct lu_env *env, struct lu_object *obj, |
| 747 | const struct lu_object_conf *conf) |
| 748 | { |
| 749 | struct lov_device *dev = lu2lov_dev(obj->lo_dev); |
| 750 | struct lov_object *lov = lu2lov(obj); |
| 751 | const struct cl_object_conf *cconf = lu2cl_conf(conf); |
| 752 | union lov_layout_state *set = &lov->u; |
| 753 | const struct lov_layout_operations *ops; |
| 754 | int result; |
| 755 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 756 | init_rwsem(&lov->lo_type_guard); |
| 757 | atomic_set(&lov->lo_active_ios, 0); |
| 758 | init_waitqueue_head(&lov->lo_waitq); |
| 759 | |
| 760 | cl_object_page_init(lu2cl(obj), sizeof(struct lov_page)); |
| 761 | |
| 762 | /* no locking is necessary, as object is being created */ |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 763 | lov->lo_type = lov_type(cconf->u.coc_md->lsm); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 764 | ops = &lov_dispatch[lov->lo_type]; |
| 765 | result = ops->llo_init(env, dev, lov, cconf, set); |
| 766 | if (result == 0) |
| 767 | ops->llo_install(env, lov, set); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 768 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static int lov_conf_set(const struct lu_env *env, struct cl_object *obj, |
| 772 | const struct cl_object_conf *conf) |
| 773 | { |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 774 | struct lov_stripe_md *lsm = NULL; |
| 775 | struct lov_object *lov = cl2lov(obj); |
| 776 | int result = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 777 | |
| 778 | lov_conf_lock(lov); |
| 779 | if (conf->coc_opc == OBJECT_CONF_INVALIDATE) { |
| 780 | lov->lo_layout_invalid = true; |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 781 | result = 0; |
| 782 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | if (conf->coc_opc == OBJECT_CONF_WAIT) { |
| 786 | if (lov->lo_layout_invalid && |
| 787 | atomic_read(&lov->lo_active_ios) > 0) { |
| 788 | lov_conf_unlock(lov); |
| 789 | result = lov_layout_wait(env, lov); |
| 790 | lov_conf_lock(lov); |
| 791 | } |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 792 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | LASSERT(conf->coc_opc == OBJECT_CONF_SET); |
| 796 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 797 | if (conf->u.coc_md) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 798 | lsm = conf->u.coc_md->lsm; |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 799 | if ((!lsm && !lov->lo_lsm) || |
| 800 | ((lsm && lov->lo_lsm) && |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 801 | (lov->lo_lsm->lsm_layout_gen == lsm->lsm_layout_gen) && |
| 802 | (lov->lo_lsm->lsm_pattern == lsm->lsm_pattern))) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 803 | /* same version of layout */ |
| 804 | lov->lo_layout_invalid = false; |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 805 | result = 0; |
| 806 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* will change layout - check if there still exists active IO. */ |
| 810 | if (atomic_read(&lov->lo_active_ios) > 0) { |
| 811 | lov->lo_layout_invalid = true; |
Julia Lawall | 1875166 | 2014-09-09 15:44:08 +0200 | [diff] [blame] | 812 | result = -EBUSY; |
| 813 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 814 | } |
| 815 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 816 | result = lov_layout_change(env, lov, conf); |
| 817 | lov->lo_layout_invalid = result != 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 818 | |
| 819 | out: |
| 820 | lov_conf_unlock(lov); |
Jinshan Xiong | 48d23e6 | 2013-12-03 21:58:48 +0800 | [diff] [blame] | 821 | CDEBUG(D_INODE, DFID" lo_layout_invalid=%d\n", |
| 822 | PFID(lu_object_fid(lov2lu(lov))), lov->lo_layout_invalid); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 823 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static void lov_object_delete(const struct lu_env *env, struct lu_object *obj) |
| 827 | { |
| 828 | struct lov_object *lov = lu2lov(obj); |
| 829 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 830 | LOV_2DISPATCH_VOID(lov, llo_delete, env, lov, &lov->u); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void lov_object_free(const struct lu_env *env, struct lu_object *obj) |
| 834 | { |
| 835 | struct lov_object *lov = lu2lov(obj); |
| 836 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 837 | LOV_2DISPATCH_VOID(lov, llo_fini, env, lov, &lov->u); |
| 838 | lu_object_fini(obj); |
Mike Rapoport | 50d3036 | 2015-10-20 12:39:51 +0300 | [diff] [blame] | 839 | kmem_cache_free(lov_object_kmem, lov); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int lov_object_print(const struct lu_env *env, void *cookie, |
| 843 | lu_printer_t p, const struct lu_object *o) |
| 844 | { |
| 845 | return LOV_2DISPATCH_NOLOCK(lu2lov(o), llo_print, env, cookie, p, o); |
| 846 | } |
| 847 | |
| 848 | int lov_page_init(const struct lu_env *env, struct cl_object *obj, |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 849 | struct cl_page *page, pgoff_t index) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 850 | { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 851 | return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_page_init, env, obj, page, |
| 852 | index); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Implements cl_object_operations::clo_io_init() method for lov |
| 857 | * layer. Dispatches to the appropriate layout io initialization method. |
| 858 | */ |
| 859 | int lov_io_init(const struct lu_env *env, struct cl_object *obj, |
| 860 | struct cl_io *io) |
| 861 | { |
| 862 | CL_IO_SLICE_CLEAN(lov_env_io(env), lis_cl); |
| 863 | return LOV_2DISPATCH_MAYLOCK(cl2lov(obj), llo_io_init, |
| 864 | !io->ci_ignore_layout, env, obj, io); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * An implementation of cl_object_operations::clo_attr_get() method for lov |
| 869 | * layer. For raid0 layout this collects and merges attributes of all |
| 870 | * sub-objects. |
| 871 | */ |
| 872 | static int lov_attr_get(const struct lu_env *env, struct cl_object *obj, |
| 873 | struct cl_attr *attr) |
| 874 | { |
| 875 | /* do not take lock, as this function is called under a |
Oleg Drokin | acb9abc | 2016-02-24 22:00:32 -0500 | [diff] [blame] | 876 | * spin-lock. Layout is protected from changing by ongoing IO. |
| 877 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 878 | return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_getattr, env, obj, attr); |
| 879 | } |
| 880 | |
| 881 | static int lov_attr_set(const struct lu_env *env, struct cl_object *obj, |
| 882 | const struct cl_attr *attr, unsigned valid) |
| 883 | { |
| 884 | /* |
| 885 | * No dispatch is required here, as no layout implements this. |
| 886 | */ |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | int lov_lock_init(const struct lu_env *env, struct cl_object *obj, |
| 891 | struct cl_lock *lock, const struct cl_io *io) |
| 892 | { |
| 893 | /* No need to lock because we've taken one refcount of layout. */ |
| 894 | return LOV_2DISPATCH_NOLOCK(cl2lov(obj), llo_lock_init, env, obj, lock, |
| 895 | io); |
| 896 | } |
| 897 | |
| 898 | static const struct cl_object_operations lov_ops = { |
| 899 | .coo_page_init = lov_page_init, |
| 900 | .coo_lock_init = lov_lock_init, |
| 901 | .coo_io_init = lov_io_init, |
| 902 | .coo_attr_get = lov_attr_get, |
| 903 | .coo_attr_set = lov_attr_set, |
| 904 | .coo_conf_set = lov_conf_set |
| 905 | }; |
| 906 | |
| 907 | static const struct lu_object_operations lov_lu_obj_ops = { |
| 908 | .loo_object_init = lov_object_init, |
| 909 | .loo_object_delete = lov_object_delete, |
| 910 | .loo_object_release = NULL, |
| 911 | .loo_object_free = lov_object_free, |
| 912 | .loo_object_print = lov_object_print, |
| 913 | .loo_object_invariant = NULL |
| 914 | }; |
| 915 | |
| 916 | struct lu_object *lov_object_alloc(const struct lu_env *env, |
| 917 | const struct lu_object_header *unused, |
| 918 | struct lu_device *dev) |
| 919 | { |
| 920 | struct lov_object *lov; |
| 921 | struct lu_object *obj; |
| 922 | |
Amitoj Kaur Chawla | 8210132 | 2016-02-26 14:24:35 +0530 | [diff] [blame] | 923 | lov = kmem_cache_zalloc(lov_object_kmem, GFP_NOFS); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 924 | if (lov) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 925 | obj = lov2lu(lov); |
| 926 | lu_object_init(obj, NULL, dev); |
| 927 | lov->lo_cl.co_ops = &lov_ops; |
| 928 | lov->lo_type = -1; /* invalid, to catch uninitialized type */ |
| 929 | /* |
| 930 | * object io operation vector (cl_object::co_iop) is installed |
| 931 | * later in lov_object_init(), as different vectors are used |
| 932 | * for object with different layouts. |
| 933 | */ |
| 934 | obj->lo_ops = &lov_lu_obj_ops; |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 935 | } else { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 936 | obj = NULL; |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 937 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 938 | return obj; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 939 | } |
| 940 | |
Oleg Drokin | 3ddca06f | 2015-10-01 00:12:16 -0400 | [diff] [blame] | 941 | static struct lov_stripe_md *lov_lsm_addref(struct lov_object *lov) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 942 | { |
| 943 | struct lov_stripe_md *lsm = NULL; |
| 944 | |
| 945 | lov_conf_freeze(lov); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 946 | if (lov->lo_lsm) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 947 | lsm = lsm_addref(lov->lo_lsm); |
| 948 | CDEBUG(D_INODE, "lsm %p addref %d/%d by %p.\n", |
Oleg Drokin | f1564f1 | 2016-02-26 01:50:05 -0500 | [diff] [blame] | 949 | lsm, atomic_read(&lsm->lsm_refc), |
| 950 | lov->lo_layout_invalid, current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 951 | } |
| 952 | lov_conf_thaw(lov); |
| 953 | return lsm; |
| 954 | } |
| 955 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 956 | struct lov_stripe_md *lov_lsm_get(struct cl_object *clobj) |
| 957 | { |
| 958 | struct lu_object *luobj; |
| 959 | struct lov_stripe_md *lsm = NULL; |
| 960 | |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 961 | if (!clobj) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 962 | return NULL; |
| 963 | |
| 964 | luobj = lu_object_locate(&cl_object_header(clobj)->coh_lu, |
| 965 | &lov_device_type); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 966 | if (luobj) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 967 | lsm = lov_lsm_addref(lu2lov(luobj)); |
| 968 | return lsm; |
| 969 | } |
| 970 | EXPORT_SYMBOL(lov_lsm_get); |
| 971 | |
| 972 | void lov_lsm_put(struct cl_object *unused, struct lov_stripe_md *lsm) |
| 973 | { |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 974 | if (lsm) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 975 | lov_free_memmd(&lsm); |
| 976 | } |
| 977 | EXPORT_SYMBOL(lov_lsm_put); |
| 978 | |
| 979 | int lov_read_and_clear_async_rc(struct cl_object *clob) |
| 980 | { |
| 981 | struct lu_object *luobj; |
| 982 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 983 | |
| 984 | luobj = lu_object_locate(&cl_object_header(clob)->coh_lu, |
| 985 | &lov_device_type); |
Oleg Drokin | 00697c4 | 2016-02-16 00:46:45 -0500 | [diff] [blame] | 986 | if (luobj) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 987 | struct lov_object *lov = lu2lov(luobj); |
| 988 | |
| 989 | lov_conf_freeze(lov); |
| 990 | switch (lov->lo_type) { |
| 991 | case LLT_RAID0: { |
| 992 | struct lov_stripe_md *lsm; |
| 993 | int i; |
| 994 | |
| 995 | lsm = lov->lo_lsm; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 996 | for (i = 0; i < lsm->lsm_stripe_count; i++) { |
| 997 | struct lov_oinfo *loi = lsm->lsm_oinfo[i]; |
Yang Sheng | 397632e | 2015-03-25 21:53:22 -0400 | [diff] [blame] | 998 | |
| 999 | if (lov_oinfo_is_dummy(loi)) |
| 1000 | continue; |
| 1001 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1002 | if (loi->loi_ar.ar_rc && !rc) |
| 1003 | rc = loi->loi_ar.ar_rc; |
| 1004 | loi->loi_ar.ar_rc = 0; |
| 1005 | } |
| 1006 | } |
Jinshan Xiong | 5dd1641 | 2013-07-23 00:06:39 +0800 | [diff] [blame] | 1007 | case LLT_RELEASED: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1008 | case LLT_EMPTY: |
| 1009 | break; |
| 1010 | default: |
| 1011 | LBUG(); |
| 1012 | } |
| 1013 | lov_conf_thaw(lov); |
| 1014 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1015 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1016 | } |
| 1017 | EXPORT_SYMBOL(lov_read_and_clear_async_rc); |
| 1018 | |
| 1019 | /** @} lov */ |