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) 2012, 2015, Intel Corporation. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 27 | * |
| 28 | */ |
| 29 | /* |
| 30 | * This file is part of Lustre, http://www.lustre.org/ |
| 31 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 32 | * |
| 33 | * osc cache management. |
| 34 | * |
| 35 | * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com> |
| 36 | */ |
| 37 | |
| 38 | #define DEBUG_SUBSYSTEM S_OSC |
| 39 | |
| 40 | #include "osc_cl_internal.h" |
| 41 | #include "osc_internal.h" |
| 42 | |
| 43 | static int extent_debug; /* set it to be true for more debug */ |
| 44 | |
| 45 | static void osc_update_pending(struct osc_object *obj, int cmd, int delta); |
| 46 | static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext, |
| 47 | int state); |
| 48 | static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli, |
| 49 | struct osc_async_page *oap, int sent, int rc); |
| 50 | static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap, |
| 51 | int cmd); |
| 52 | static int osc_refresh_count(const struct lu_env *env, |
| 53 | struct osc_async_page *oap, int cmd); |
| 54 | static int osc_io_unplug_async(const struct lu_env *env, |
| 55 | struct client_obd *cli, struct osc_object *osc); |
| 56 | static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages, |
| 57 | unsigned int lost_grant); |
| 58 | |
| 59 | static void osc_extent_tree_dump0(int level, struct osc_object *obj, |
| 60 | const char *func, int line); |
| 61 | #define osc_extent_tree_dump(lvl, obj) \ |
| 62 | osc_extent_tree_dump0(lvl, obj, __func__, __LINE__) |
| 63 | |
| 64 | /** \addtogroup osc |
| 65 | * @{ |
| 66 | */ |
| 67 | |
| 68 | /* ------------------ osc extent ------------------ */ |
| 69 | static inline char *ext_flags(struct osc_extent *ext, char *flags) |
| 70 | { |
| 71 | char *buf = flags; |
| 72 | *buf++ = ext->oe_rw ? 'r' : 'w'; |
| 73 | if (ext->oe_intree) |
| 74 | *buf++ = 'i'; |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 75 | if (ext->oe_sync) |
| 76 | *buf++ = 'S'; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 77 | if (ext->oe_srvlock) |
| 78 | *buf++ = 's'; |
| 79 | if (ext->oe_hp) |
| 80 | *buf++ = 'h'; |
| 81 | if (ext->oe_urgent) |
| 82 | *buf++ = 'u'; |
| 83 | if (ext->oe_memalloc) |
| 84 | *buf++ = 'm'; |
| 85 | if (ext->oe_trunc_pending) |
| 86 | *buf++ = 't'; |
| 87 | if (ext->oe_fsync_wait) |
| 88 | *buf++ = 'Y'; |
| 89 | *buf = 0; |
| 90 | return flags; |
| 91 | } |
| 92 | |
| 93 | static inline char list_empty_marker(struct list_head *list) |
| 94 | { |
| 95 | return list_empty(list) ? '-' : '+'; |
| 96 | } |
| 97 | |
| 98 | #define EXTSTR "[%lu -> %lu/%lu]" |
| 99 | #define EXTPARA(ext) (ext)->oe_start, (ext)->oe_end, (ext)->oe_max_end |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 100 | static const char *oes_strings[] = { |
| 101 | "inv", "active", "cache", "locking", "lockdone", "rpc", "trunc", NULL }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 102 | |
| 103 | #define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \ |
| 104 | struct osc_extent *__ext = (extent); \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 105 | char __buf[16]; \ |
| 106 | \ |
| 107 | CDEBUG(lvl, \ |
| 108 | "extent %p@{" EXTSTR ", " \ |
| 109 | "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \ |
| 110 | /* ----- extent part 0 ----- */ \ |
| 111 | __ext, EXTPARA(__ext), \ |
| 112 | /* ----- part 1 ----- */ \ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 113 | atomic_read(&__ext->oe_refc), \ |
| 114 | atomic_read(&__ext->oe_users), \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 115 | list_empty_marker(&__ext->oe_link), \ |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 116 | oes_strings[__ext->oe_state], ext_flags(__ext, __buf), \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 117 | __ext->oe_obj, \ |
| 118 | /* ----- part 2 ----- */ \ |
| 119 | __ext->oe_grants, __ext->oe_nr_pages, \ |
| 120 | list_empty_marker(&__ext->oe_pages), \ |
| 121 | waitqueue_active(&__ext->oe_waitq) ? '+' : '-', \ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 122 | __ext->oe_dlmlock, __ext->oe_mppr, __ext->oe_owner, \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 123 | /* ----- part 4 ----- */ \ |
| 124 | ## __VA_ARGS__); \ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 125 | if (lvl == D_ERROR && __ext->oe_dlmlock) \ |
Alex Zhuravlev | e93876d | 2016-06-20 16:55:36 -0400 | [diff] [blame] | 126 | LDLM_ERROR(__ext->oe_dlmlock, "extent: %p", __ext); \ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 127 | else \ |
Alex Zhuravlev | e93876d | 2016-06-20 16:55:36 -0400 | [diff] [blame] | 128 | LDLM_DEBUG(__ext->oe_dlmlock, "extent: %p", __ext); \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 129 | } while (0) |
| 130 | |
| 131 | #undef EASSERTF |
| 132 | #define EASSERTF(expr, ext, fmt, args...) do { \ |
| 133 | if (!(expr)) { \ |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 134 | OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \ |
| 135 | osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 136 | LASSERT(expr); \ |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 137 | } \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 138 | } while (0) |
| 139 | |
| 140 | #undef EASSERT |
| 141 | #define EASSERT(expr, ext) EASSERTF(expr, ext, "\n") |
| 142 | |
| 143 | static inline struct osc_extent *rb_extent(struct rb_node *n) |
| 144 | { |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 145 | if (!n) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 146 | return NULL; |
| 147 | |
| 148 | return container_of(n, struct osc_extent, oe_node); |
| 149 | } |
| 150 | |
| 151 | static inline struct osc_extent *next_extent(struct osc_extent *ext) |
| 152 | { |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 153 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 154 | return NULL; |
| 155 | |
| 156 | LASSERT(ext->oe_intree); |
| 157 | return rb_extent(rb_next(&ext->oe_node)); |
| 158 | } |
| 159 | |
| 160 | static inline struct osc_extent *prev_extent(struct osc_extent *ext) |
| 161 | { |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 162 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 163 | return NULL; |
| 164 | |
| 165 | LASSERT(ext->oe_intree); |
| 166 | return rb_extent(rb_prev(&ext->oe_node)); |
| 167 | } |
| 168 | |
| 169 | static inline struct osc_extent *first_extent(struct osc_object *obj) |
| 170 | { |
| 171 | return rb_extent(rb_first(&obj->oo_root)); |
| 172 | } |
| 173 | |
| 174 | /* object must be locked by caller. */ |
| 175 | static int osc_extent_sanity_check0(struct osc_extent *ext, |
| 176 | const char *func, const int line) |
| 177 | { |
| 178 | struct osc_object *obj = ext->oe_obj; |
| 179 | struct osc_async_page *oap; |
| 180 | int page_count; |
| 181 | int rc = 0; |
| 182 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 183 | if (!osc_object_is_locked(obj)) { |
| 184 | rc = 9; |
| 185 | goto out; |
| 186 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 187 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 188 | if (ext->oe_state >= OES_STATE_MAX) { |
| 189 | rc = 10; |
| 190 | goto out; |
| 191 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 192 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 193 | if (atomic_read(&ext->oe_refc) <= 0) { |
| 194 | rc = 20; |
| 195 | goto out; |
| 196 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 197 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 198 | if (atomic_read(&ext->oe_refc) < atomic_read(&ext->oe_users)) { |
| 199 | rc = 30; |
| 200 | goto out; |
| 201 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 202 | |
| 203 | switch (ext->oe_state) { |
| 204 | case OES_INV: |
| 205 | if (ext->oe_nr_pages > 0 || !list_empty(&ext->oe_pages)) |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 206 | rc = 35; |
| 207 | else |
| 208 | rc = 0; |
| 209 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 210 | case OES_ACTIVE: |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 211 | if (atomic_read(&ext->oe_users) == 0) { |
| 212 | rc = 40; |
| 213 | goto out; |
| 214 | } |
| 215 | if (ext->oe_hp) { |
| 216 | rc = 50; |
| 217 | goto out; |
| 218 | } |
| 219 | if (ext->oe_fsync_wait && !ext->oe_urgent) { |
| 220 | rc = 55; |
| 221 | goto out; |
| 222 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 223 | break; |
| 224 | case OES_CACHE: |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 225 | if (ext->oe_grants == 0) { |
| 226 | rc = 60; |
| 227 | goto out; |
| 228 | } |
| 229 | if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp) { |
| 230 | rc = 65; |
| 231 | goto out; |
| 232 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 233 | default: |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 234 | if (atomic_read(&ext->oe_users) > 0) { |
| 235 | rc = 70; |
| 236 | goto out; |
| 237 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 238 | } |
| 239 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 240 | if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start) { |
| 241 | rc = 80; |
| 242 | goto out; |
| 243 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 244 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 245 | if (ext->oe_sync && ext->oe_grants > 0) { |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 246 | rc = 90; |
| 247 | goto out; |
| 248 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 249 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 250 | if (ext->oe_dlmlock) { |
| 251 | struct ldlm_extent *extent; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 252 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 253 | extent = &ext->oe_dlmlock->l_policy_data.l_extent; |
| 254 | if (!(extent->start <= cl_offset(osc2cl(obj), ext->oe_start) && |
| 255 | extent->end >= cl_offset(osc2cl(obj), ext->oe_max_end))) { |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 256 | rc = 100; |
| 257 | goto out; |
| 258 | } |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 259 | |
| 260 | if (!(ext->oe_dlmlock->l_granted_mode & (LCK_PW | LCK_GROUP))) { |
| 261 | rc = 102; |
| 262 | goto out; |
| 263 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 266 | if (ext->oe_nr_pages > ext->oe_mppr) { |
| 267 | rc = 105; |
| 268 | goto out; |
| 269 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 270 | |
| 271 | /* Do not verify page list if extent is in RPC. This is because an |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 272 | * in-RPC extent is supposed to be exclusively accessible w/o lock. |
| 273 | */ |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 274 | if (ext->oe_state > OES_CACHE) { |
| 275 | rc = 0; |
| 276 | goto out; |
| 277 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 278 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 279 | if (!extent_debug) { |
| 280 | rc = 0; |
| 281 | goto out; |
| 282 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 283 | |
| 284 | page_count = 0; |
| 285 | list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 286 | pgoff_t index = osc_index(oap2osc(oap)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 287 | ++page_count; |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 288 | if (index > ext->oe_end || index < ext->oe_start) { |
| 289 | rc = 110; |
| 290 | goto out; |
| 291 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 292 | } |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 293 | if (page_count != ext->oe_nr_pages) { |
| 294 | rc = 120; |
| 295 | goto out; |
| 296 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 297 | |
| 298 | out: |
| 299 | if (rc != 0) |
| 300 | OSC_EXTENT_DUMP(D_ERROR, ext, |
| 301 | "%s:%d sanity check %p failed with rc = %d\n", |
| 302 | func, line, ext, rc); |
| 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | #define sanity_check_nolock(ext) \ |
| 307 | osc_extent_sanity_check0(ext, __func__, __LINE__) |
| 308 | |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 309 | #define sanity_check(ext) ({ \ |
| 310 | int __res; \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 311 | osc_object_lock((ext)->oe_obj); \ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 312 | __res = sanity_check_nolock(ext); \ |
| 313 | osc_object_unlock((ext)->oe_obj); \ |
| 314 | __res; \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 315 | }) |
| 316 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 317 | /** |
| 318 | * sanity check - to make sure there is no overlapped extent in the tree. |
| 319 | */ |
| 320 | static int osc_extent_is_overlapped(struct osc_object *obj, |
| 321 | struct osc_extent *ext) |
| 322 | { |
| 323 | struct osc_extent *tmp; |
| 324 | |
| 325 | LASSERT(osc_object_is_locked(obj)); |
| 326 | |
| 327 | if (!extent_debug) |
| 328 | return 0; |
| 329 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 330 | for (tmp = first_extent(obj); tmp; tmp = next_extent(tmp)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 331 | if (tmp == ext) |
| 332 | continue; |
| 333 | if (tmp->oe_end >= ext->oe_start && |
| 334 | tmp->oe_start <= ext->oe_end) |
| 335 | return 1; |
| 336 | } |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static void osc_extent_state_set(struct osc_extent *ext, int state) |
| 341 | { |
| 342 | LASSERT(osc_object_is_locked(ext->oe_obj)); |
| 343 | LASSERT(state >= OES_INV && state < OES_STATE_MAX); |
| 344 | |
| 345 | /* Never try to sanity check a state changing extent :-) */ |
| 346 | /* LASSERT(sanity_check_nolock(ext) == 0); */ |
| 347 | |
| 348 | /* TODO: validate the state machine */ |
| 349 | ext->oe_state = state; |
| 350 | wake_up_all(&ext->oe_waitq); |
| 351 | } |
| 352 | |
| 353 | static struct osc_extent *osc_extent_alloc(struct osc_object *obj) |
| 354 | { |
| 355 | struct osc_extent *ext; |
| 356 | |
Amitoj Kaur Chawla | c4418da | 2016-02-26 14:24:55 +0530 | [diff] [blame] | 357 | ext = kmem_cache_zalloc(osc_extent_kmem, GFP_NOFS); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 358 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 359 | return NULL; |
| 360 | |
| 361 | RB_CLEAR_NODE(&ext->oe_node); |
| 362 | ext->oe_obj = obj; |
| 363 | atomic_set(&ext->oe_refc, 1); |
| 364 | atomic_set(&ext->oe_users, 0); |
| 365 | INIT_LIST_HEAD(&ext->oe_link); |
| 366 | ext->oe_state = OES_INV; |
| 367 | INIT_LIST_HEAD(&ext->oe_pages); |
| 368 | init_waitqueue_head(&ext->oe_waitq); |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 369 | ext->oe_dlmlock = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 370 | |
| 371 | return ext; |
| 372 | } |
| 373 | |
| 374 | static void osc_extent_free(struct osc_extent *ext) |
| 375 | { |
Mike Rapoport | 50d3036 | 2015-10-20 12:39:51 +0300 | [diff] [blame] | 376 | kmem_cache_free(osc_extent_kmem, ext); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static struct osc_extent *osc_extent_get(struct osc_extent *ext) |
| 380 | { |
| 381 | LASSERT(atomic_read(&ext->oe_refc) >= 0); |
| 382 | atomic_inc(&ext->oe_refc); |
| 383 | return ext; |
| 384 | } |
| 385 | |
| 386 | static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext) |
| 387 | { |
| 388 | LASSERT(atomic_read(&ext->oe_refc) > 0); |
| 389 | if (atomic_dec_and_test(&ext->oe_refc)) { |
| 390 | LASSERT(list_empty(&ext->oe_link)); |
| 391 | LASSERT(atomic_read(&ext->oe_users) == 0); |
| 392 | LASSERT(ext->oe_state == OES_INV); |
| 393 | LASSERT(!ext->oe_intree); |
| 394 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 395 | if (ext->oe_dlmlock) { |
| 396 | lu_ref_add(&ext->oe_dlmlock->l_reference, |
| 397 | "osc_extent", ext); |
| 398 | LDLM_LOCK_PUT(ext->oe_dlmlock); |
| 399 | ext->oe_dlmlock = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 400 | } |
| 401 | osc_extent_free(ext); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * osc_extent_put_trust() is a special version of osc_extent_put() when |
| 407 | * it's known that the caller is not the last user. This is to address the |
| 408 | * problem of lacking of lu_env ;-). |
| 409 | */ |
| 410 | static void osc_extent_put_trust(struct osc_extent *ext) |
| 411 | { |
| 412 | LASSERT(atomic_read(&ext->oe_refc) > 1); |
| 413 | LASSERT(osc_object_is_locked(ext->oe_obj)); |
| 414 | atomic_dec(&ext->oe_refc); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Return the extent which includes pgoff @index, or return the greatest |
| 419 | * previous extent in the tree. |
| 420 | */ |
| 421 | static struct osc_extent *osc_extent_search(struct osc_object *obj, |
| 422 | pgoff_t index) |
| 423 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 424 | struct rb_node *n = obj->oo_root.rb_node; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 425 | struct osc_extent *tmp, *p = NULL; |
| 426 | |
| 427 | LASSERT(osc_object_is_locked(obj)); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 428 | while (n) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 429 | tmp = rb_extent(n); |
| 430 | if (index < tmp->oe_start) { |
| 431 | n = n->rb_left; |
| 432 | } else if (index > tmp->oe_end) { |
| 433 | p = rb_extent(n); |
| 434 | n = n->rb_right; |
| 435 | } else { |
| 436 | return tmp; |
| 437 | } |
| 438 | } |
| 439 | return p; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Return the extent covering @index, otherwise return NULL. |
| 444 | * caller must have held object lock. |
| 445 | */ |
| 446 | static struct osc_extent *osc_extent_lookup(struct osc_object *obj, |
| 447 | pgoff_t index) |
| 448 | { |
| 449 | struct osc_extent *ext; |
| 450 | |
| 451 | ext = osc_extent_search(obj, index); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 452 | if (ext && ext->oe_start <= index && index <= ext->oe_end) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 453 | return osc_extent_get(ext); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | /* caller must have held object lock. */ |
| 458 | static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext) |
| 459 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 460 | struct rb_node **n = &obj->oo_root.rb_node; |
| 461 | struct rb_node *parent = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 462 | struct osc_extent *tmp; |
| 463 | |
| 464 | LASSERT(ext->oe_intree == 0); |
| 465 | LASSERT(ext->oe_obj == obj); |
| 466 | LASSERT(osc_object_is_locked(obj)); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 467 | while (*n) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 468 | tmp = rb_extent(*n); |
| 469 | parent = *n; |
| 470 | |
| 471 | if (ext->oe_end < tmp->oe_start) |
| 472 | n = &(*n)->rb_left; |
| 473 | else if (ext->oe_start > tmp->oe_end) |
| 474 | n = &(*n)->rb_right; |
| 475 | else |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 476 | EASSERTF(0, tmp, EXTSTR"\n", EXTPARA(ext)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 477 | } |
| 478 | rb_link_node(&ext->oe_node, parent, n); |
| 479 | rb_insert_color(&ext->oe_node, &obj->oo_root); |
| 480 | osc_extent_get(ext); |
| 481 | ext->oe_intree = 1; |
| 482 | } |
| 483 | |
| 484 | /* caller must have held object lock. */ |
| 485 | static void osc_extent_erase(struct osc_extent *ext) |
| 486 | { |
| 487 | struct osc_object *obj = ext->oe_obj; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 488 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 489 | LASSERT(osc_object_is_locked(obj)); |
| 490 | if (ext->oe_intree) { |
| 491 | rb_erase(&ext->oe_node, &obj->oo_root); |
| 492 | ext->oe_intree = 0; |
| 493 | /* rbtree held a refcount */ |
| 494 | osc_extent_put_trust(ext); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static struct osc_extent *osc_extent_hold(struct osc_extent *ext) |
| 499 | { |
| 500 | struct osc_object *obj = ext->oe_obj; |
| 501 | |
| 502 | LASSERT(osc_object_is_locked(obj)); |
| 503 | LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE); |
| 504 | if (ext->oe_state == OES_CACHE) { |
| 505 | osc_extent_state_set(ext, OES_ACTIVE); |
| 506 | osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages); |
| 507 | } |
| 508 | atomic_inc(&ext->oe_users); |
| 509 | list_del_init(&ext->oe_link); |
| 510 | return osc_extent_get(ext); |
| 511 | } |
| 512 | |
| 513 | static void __osc_extent_remove(struct osc_extent *ext) |
| 514 | { |
| 515 | LASSERT(osc_object_is_locked(ext->oe_obj)); |
| 516 | LASSERT(list_empty(&ext->oe_pages)); |
| 517 | osc_extent_erase(ext); |
| 518 | list_del_init(&ext->oe_link); |
| 519 | osc_extent_state_set(ext, OES_INV); |
| 520 | OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n"); |
| 521 | } |
| 522 | |
| 523 | static void osc_extent_remove(struct osc_extent *ext) |
| 524 | { |
| 525 | struct osc_object *obj = ext->oe_obj; |
| 526 | |
| 527 | osc_object_lock(obj); |
| 528 | __osc_extent_remove(ext); |
| 529 | osc_object_unlock(obj); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * This function is used to merge extents to get better performance. It checks |
| 534 | * if @cur and @victim are contiguous at chunk level. |
| 535 | */ |
| 536 | static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur, |
| 537 | struct osc_extent *victim) |
| 538 | { |
| 539 | struct osc_object *obj = cur->oe_obj; |
| 540 | pgoff_t chunk_start; |
| 541 | pgoff_t chunk_end; |
| 542 | int ppc_bits; |
| 543 | |
| 544 | LASSERT(cur->oe_state == OES_CACHE); |
| 545 | LASSERT(osc_object_is_locked(obj)); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 546 | if (!victim) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 547 | return -EINVAL; |
| 548 | |
| 549 | if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait) |
| 550 | return -EBUSY; |
| 551 | |
| 552 | if (cur->oe_max_end != victim->oe_max_end) |
| 553 | return -ERANGE; |
| 554 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 555 | LASSERT(cur->oe_dlmlock == victim->oe_dlmlock); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 556 | ppc_bits = osc_cli(obj)->cl_chunkbits - PAGE_SHIFT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 557 | chunk_start = cur->oe_start >> ppc_bits; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 558 | chunk_end = cur->oe_end >> ppc_bits; |
| 559 | if (chunk_start != (victim->oe_end >> ppc_bits) + 1 && |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 560 | chunk_end + 1 != victim->oe_start >> ppc_bits) |
| 561 | return -ERANGE; |
| 562 | |
| 563 | OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur); |
| 564 | |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 565 | cur->oe_start = min(cur->oe_start, victim->oe_start); |
| 566 | cur->oe_end = max(cur->oe_end, victim->oe_end); |
| 567 | cur->oe_grants += victim->oe_grants; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 568 | cur->oe_nr_pages += victim->oe_nr_pages; |
| 569 | /* only the following bits are needed to merge */ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 570 | cur->oe_urgent |= victim->oe_urgent; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 571 | cur->oe_memalloc |= victim->oe_memalloc; |
| 572 | list_splice_init(&victim->oe_pages, &cur->oe_pages); |
| 573 | list_del_init(&victim->oe_link); |
| 574 | victim->oe_nr_pages = 0; |
| 575 | |
| 576 | osc_extent_get(victim); |
| 577 | __osc_extent_remove(victim); |
| 578 | osc_extent_put(env, victim); |
| 579 | |
| 580 | OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Drop user count of osc_extent, and unplug IO asynchronously. |
| 586 | */ |
Heena Sirwani | 882e7e2 | 2014-10-07 17:29:11 +0530 | [diff] [blame] | 587 | void osc_extent_release(const struct lu_env *env, struct osc_extent *ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 588 | { |
| 589 | struct osc_object *obj = ext->oe_obj; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 590 | |
| 591 | LASSERT(atomic_read(&ext->oe_users) > 0); |
| 592 | LASSERT(sanity_check(ext) == 0); |
| 593 | LASSERT(ext->oe_grants > 0); |
| 594 | |
| 595 | if (atomic_dec_and_lock(&ext->oe_users, &obj->oo_lock)) { |
| 596 | LASSERT(ext->oe_state == OES_ACTIVE); |
| 597 | if (ext->oe_trunc_pending) { |
| 598 | /* a truncate process is waiting for this extent. |
| 599 | * This may happen due to a race, check |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 600 | * osc_cache_truncate_start(). |
| 601 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 602 | osc_extent_state_set(ext, OES_TRUNC); |
| 603 | ext->oe_trunc_pending = 0; |
| 604 | } else { |
| 605 | osc_extent_state_set(ext, OES_CACHE); |
| 606 | osc_update_pending(obj, OBD_BRW_WRITE, |
| 607 | ext->oe_nr_pages); |
| 608 | |
| 609 | /* try to merge the previous and next extent. */ |
| 610 | osc_extent_merge(env, ext, prev_extent(ext)); |
| 611 | osc_extent_merge(env, ext, next_extent(ext)); |
| 612 | |
| 613 | if (ext->oe_urgent) |
| 614 | list_move_tail(&ext->oe_link, |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 615 | &obj->oo_urgent_exts); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 616 | } |
| 617 | osc_object_unlock(obj); |
| 618 | |
| 619 | osc_io_unplug_async(env, osc_cli(obj), obj); |
| 620 | } |
| 621 | osc_extent_put(env, ext); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2) |
| 625 | { |
| 626 | return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Find or create an extent which includes @index, core function to manage |
| 631 | * extent tree. |
| 632 | */ |
Shraddha Barke | 6ef3f3c | 2015-12-18 02:40:28 +0530 | [diff] [blame] | 633 | static struct osc_extent *osc_extent_find(const struct lu_env *env, |
| 634 | struct osc_object *obj, pgoff_t index, |
| 635 | int *grants) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 636 | { |
| 637 | struct client_obd *cli = osc_cli(obj); |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 638 | struct osc_lock *olck; |
| 639 | struct cl_lock_descr *descr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 640 | struct osc_extent *cur; |
| 641 | struct osc_extent *ext; |
| 642 | struct osc_extent *conflict = NULL; |
| 643 | struct osc_extent *found = NULL; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 644 | pgoff_t chunk; |
| 645 | pgoff_t max_end; |
| 646 | int max_pages; /* max_pages_per_rpc */ |
| 647 | int chunksize; |
| 648 | int ppc_bits; /* pages per chunk bits */ |
| 649 | int chunk_mask; |
| 650 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 651 | |
| 652 | cur = osc_extent_alloc(obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 653 | if (!cur) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 654 | return ERR_PTR(-ENOMEM); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 655 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 656 | olck = osc_env_io(env)->oi_write_osclock; |
| 657 | LASSERTF(olck, "page %lu is not covered by lock\n", index); |
| 658 | LASSERT(olck->ols_state == OLS_GRANTED); |
| 659 | |
| 660 | descr = &olck->ols_cl.cls_lock->cll_descr; |
| 661 | LASSERT(descr->cld_mode >= CLM_WRITE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 662 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 663 | LASSERT(cli->cl_chunkbits >= PAGE_SHIFT); |
| 664 | ppc_bits = cli->cl_chunkbits - PAGE_SHIFT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 665 | chunk_mask = ~((1 << ppc_bits) - 1); |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 666 | chunksize = 1 << cli->cl_chunkbits; |
| 667 | chunk = index >> ppc_bits; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 668 | |
| 669 | /* align end to rpc edge, rpc size may not be a power 2 integer. */ |
| 670 | max_pages = cli->cl_max_pages_per_rpc; |
| 671 | LASSERT((max_pages & ~chunk_mask) == 0); |
| 672 | max_end = index - (index % max_pages) + max_pages - 1; |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 673 | max_end = min_t(pgoff_t, max_end, descr->cld_end); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 674 | |
| 675 | /* initialize new extent by parameters so far */ |
| 676 | cur->oe_max_end = max_end; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 677 | cur->oe_start = index & chunk_mask; |
| 678 | cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1; |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 679 | if (cur->oe_start < descr->cld_start) |
| 680 | cur->oe_start = descr->cld_start; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 681 | if (cur->oe_end > max_end) |
| 682 | cur->oe_end = max_end; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 683 | cur->oe_grants = 0; |
| 684 | cur->oe_mppr = max_pages; |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 685 | if (olck->ols_dlmlock) { |
| 686 | LASSERT(olck->ols_hold); |
| 687 | cur->oe_dlmlock = LDLM_LOCK_GET(olck->ols_dlmlock); |
| 688 | lu_ref_add(&olck->ols_dlmlock->l_reference, "osc_extent", cur); |
| 689 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 690 | |
| 691 | /* grants has been allocated by caller */ |
| 692 | LASSERTF(*grants >= chunksize + cli->cl_extent_tax, |
| 693 | "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax); |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 694 | LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR"\n", |
| 695 | EXTPARA(cur)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 696 | |
| 697 | restart: |
| 698 | osc_object_lock(obj); |
| 699 | ext = osc_extent_search(obj, cur->oe_start); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 700 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 701 | ext = first_extent(obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 702 | while (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 703 | loff_t ext_chk_start = ext->oe_start >> ppc_bits; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 704 | loff_t ext_chk_end = ext->oe_end >> ppc_bits; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 705 | |
| 706 | LASSERT(sanity_check_nolock(ext) == 0); |
| 707 | if (chunk > ext_chk_end + 1) |
| 708 | break; |
| 709 | |
| 710 | /* if covering by different locks, no chance to match */ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 711 | if (olck->ols_dlmlock != ext->oe_dlmlock) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 712 | EASSERTF(!overlapped(ext, cur), ext, |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 713 | EXTSTR"\n", EXTPARA(cur)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 714 | |
| 715 | ext = next_extent(ext); |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | /* discontiguous chunks? */ |
| 720 | if (chunk + 1 < ext_chk_start) { |
| 721 | ext = next_extent(ext); |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | /* ok, from now on, ext and cur have these attrs: |
| 726 | * 1. covered by the same lock |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 727 | * 2. contiguous at chunk level or overlapping. |
| 728 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 729 | |
| 730 | if (overlapped(ext, cur)) { |
| 731 | /* cur is the minimum unit, so overlapping means |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 732 | * full contain. |
| 733 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 734 | EASSERTF((ext->oe_start <= cur->oe_start && |
| 735 | ext->oe_end >= cur->oe_end), |
James Nunez | 19b2056 | 2016-03-07 18:10:21 -0500 | [diff] [blame] | 736 | ext, EXTSTR"\n", EXTPARA(cur)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 737 | |
| 738 | if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) { |
| 739 | /* for simplicity, we wait for this extent to |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 740 | * finish before going forward. |
| 741 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 742 | conflict = osc_extent_get(ext); |
| 743 | break; |
| 744 | } |
| 745 | |
| 746 | found = osc_extent_hold(ext); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | /* non-overlapped extent */ |
| 751 | if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) { |
| 752 | /* we can't do anything for a non OES_CACHE extent, or |
| 753 | * if there is someone waiting for this extent to be |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 754 | * flushed, try next one. |
| 755 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 756 | ext = next_extent(ext); |
| 757 | continue; |
| 758 | } |
| 759 | |
| 760 | /* check if they belong to the same rpc slot before trying to |
| 761 | * merge. the extents are not overlapped and contiguous at |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 762 | * chunk level to get here. |
| 763 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 764 | if (ext->oe_max_end != max_end) { |
| 765 | /* if they don't belong to the same RPC slot or |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 766 | * max_pages_per_rpc has ever changed, do not merge. |
| 767 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 768 | ext = next_extent(ext); |
| 769 | continue; |
| 770 | } |
| 771 | |
| 772 | /* it's required that an extent must be contiguous at chunk |
| 773 | * level so that we know the whole extent is covered by grant |
| 774 | * (the pages in the extent are NOT required to be contiguous). |
| 775 | * Otherwise, it will be too much difficult to know which |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 776 | * chunks have grants allocated. |
| 777 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 778 | |
| 779 | /* try to do front merge - extend ext's start */ |
| 780 | if (chunk + 1 == ext_chk_start) { |
| 781 | /* ext must be chunk size aligned */ |
| 782 | EASSERT((ext->oe_start & ~chunk_mask) == 0, ext); |
| 783 | |
| 784 | /* pull ext's start back to cover cur */ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 785 | ext->oe_start = cur->oe_start; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 786 | ext->oe_grants += chunksize; |
| 787 | *grants -= chunksize; |
| 788 | |
| 789 | found = osc_extent_hold(ext); |
| 790 | } else if (chunk == ext_chk_end + 1) { |
| 791 | /* rear merge */ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 792 | ext->oe_end = cur->oe_end; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 793 | ext->oe_grants += chunksize; |
| 794 | *grants -= chunksize; |
| 795 | |
| 796 | /* try to merge with the next one because we just fill |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 797 | * in a gap |
| 798 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 799 | if (osc_extent_merge(env, ext, next_extent(ext)) == 0) |
| 800 | /* we can save extent tax from next extent */ |
| 801 | *grants += cli->cl_extent_tax; |
| 802 | |
| 803 | found = osc_extent_hold(ext); |
| 804 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 805 | if (found) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 806 | break; |
| 807 | |
| 808 | ext = next_extent(ext); |
| 809 | } |
| 810 | |
| 811 | osc_extent_tree_dump(D_CACHE, obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 812 | if (found) { |
| 813 | LASSERT(!conflict); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 814 | if (!IS_ERR(found)) { |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 815 | LASSERT(found->oe_dlmlock == cur->oe_dlmlock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 816 | OSC_EXTENT_DUMP(D_CACHE, found, |
| 817 | "found caching ext for %lu.\n", index); |
| 818 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 819 | } else if (!conflict) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 820 | /* create a new extent */ |
| 821 | EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur); |
| 822 | cur->oe_grants = chunksize + cli->cl_extent_tax; |
| 823 | *grants -= cur->oe_grants; |
| 824 | LASSERT(*grants >= 0); |
| 825 | |
| 826 | cur->oe_state = OES_CACHE; |
| 827 | found = osc_extent_hold(cur); |
| 828 | osc_extent_insert(obj, cur); |
| 829 | OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n", |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 830 | index, descr->cld_end); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 831 | } |
| 832 | osc_object_unlock(obj); |
| 833 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 834 | if (conflict) { |
| 835 | LASSERT(!found); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 836 | |
| 837 | /* waiting for IO to finish. Please notice that it's impossible |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 838 | * to be an OES_TRUNC extent. |
| 839 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 840 | rc = osc_extent_wait(env, conflict, OES_INV); |
| 841 | osc_extent_put(env, conflict); |
| 842 | conflict = NULL; |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 843 | if (rc < 0) { |
| 844 | found = ERR_PTR(rc); |
| 845 | goto out; |
| 846 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 847 | |
| 848 | goto restart; |
| 849 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 850 | |
| 851 | out: |
| 852 | osc_extent_put(env, cur); |
| 853 | LASSERT(*grants >= 0); |
| 854 | return found; |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Called when IO is finished to an extent. |
| 859 | */ |
| 860 | int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext, |
| 861 | int sent, int rc) |
| 862 | { |
| 863 | struct client_obd *cli = osc_cli(ext->oe_obj); |
| 864 | struct osc_async_page *oap; |
| 865 | struct osc_async_page *tmp; |
| 866 | int nr_pages = ext->oe_nr_pages; |
| 867 | int lost_grant = 0; |
| 868 | int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096; |
| 869 | __u64 last_off = 0; |
| 870 | int last_count = -1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 871 | |
| 872 | OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n"); |
| 873 | |
| 874 | ext->oe_rc = rc ?: ext->oe_nr_pages; |
| 875 | EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext); |
Jinshan Xiong | 5196e42 | 2016-03-30 19:48:26 -0400 | [diff] [blame] | 876 | |
| 877 | osc_lru_add_batch(cli, &ext->oe_pages); |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 878 | list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 879 | list_del_init(&oap->oap_rpc_item); |
| 880 | list_del_init(&oap->oap_pending_item); |
| 881 | if (last_off <= oap->oap_obj_off) { |
| 882 | last_off = oap->oap_obj_off; |
| 883 | last_count = oap->oap_count; |
| 884 | } |
| 885 | |
| 886 | --ext->oe_nr_pages; |
| 887 | osc_ap_completion(env, cli, oap, sent, rc); |
| 888 | } |
| 889 | EASSERT(ext->oe_nr_pages == 0, ext); |
| 890 | |
| 891 | if (!sent) { |
| 892 | lost_grant = ext->oe_grants; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 893 | } else if (blocksize < PAGE_SIZE && |
| 894 | last_count != PAGE_SIZE) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 895 | /* For short writes we shouldn't count parts of pages that |
| 896 | * span a whole chunk on the OST side, or our accounting goes |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 897 | * wrong. Should match the code in filter_grant_check. |
| 898 | */ |
Jinshan Xiong | 77605e41 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 899 | int offset = last_off & ~PAGE_MASK; |
| 900 | int count = last_count + (offset & (blocksize - 1)); |
| 901 | int end = (offset + last_count) & (blocksize - 1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 902 | if (end) |
| 903 | count += blocksize - end; |
| 904 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 905 | lost_grant = PAGE_SIZE - count; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 906 | } |
| 907 | if (ext->oe_grants > 0) |
| 908 | osc_free_grant(cli, nr_pages, lost_grant); |
| 909 | |
| 910 | osc_extent_remove(ext); |
| 911 | /* put the refcount for RPC */ |
| 912 | osc_extent_put(env, ext); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 913 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static int extent_wait_cb(struct osc_extent *ext, int state) |
| 917 | { |
| 918 | int ret; |
| 919 | |
| 920 | osc_object_lock(ext->oe_obj); |
| 921 | ret = ext->oe_state == state; |
| 922 | osc_object_unlock(ext->oe_obj); |
| 923 | |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Wait for the extent's state to become @state. |
| 929 | */ |
| 930 | static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext, |
| 931 | int state) |
| 932 | { |
| 933 | struct osc_object *obj = ext->oe_obj; |
| 934 | struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL, |
| 935 | LWI_ON_SIGNAL_NOOP, NULL); |
| 936 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 937 | |
| 938 | osc_object_lock(obj); |
| 939 | LASSERT(sanity_check_nolock(ext) == 0); |
| 940 | /* `Kick' this extent only if the caller is waiting for it to be |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 941 | * written out. |
| 942 | */ |
Andriy Skulysh | ce248d5 | 2014-06-22 21:32:16 -0400 | [diff] [blame] | 943 | if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp && |
| 944 | !ext->oe_trunc_pending) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 945 | if (ext->oe_state == OES_ACTIVE) { |
| 946 | ext->oe_urgent = 1; |
| 947 | } else if (ext->oe_state == OES_CACHE) { |
| 948 | ext->oe_urgent = 1; |
| 949 | osc_extent_hold(ext); |
| 950 | rc = 1; |
| 951 | } |
| 952 | } |
| 953 | osc_object_unlock(obj); |
| 954 | if (rc == 1) |
| 955 | osc_extent_release(env, ext); |
| 956 | |
| 957 | /* wait for the extent until its state becomes @state */ |
| 958 | rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi); |
| 959 | if (rc == -ETIMEDOUT) { |
| 960 | OSC_EXTENT_DUMP(D_ERROR, ext, |
| 961 | "%s: wait ext to %d timedout, recovery in progress?\n", |
| 962 | osc_export(obj)->exp_obd->obd_name, state); |
| 963 | |
Jinshan Xiong | b59d812 | 2016-04-12 16:14:11 -0400 | [diff] [blame] | 964 | lwi = LWI_INTR(NULL, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 965 | rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), |
| 966 | &lwi); |
| 967 | } |
| 968 | if (rc == 0 && ext->oe_rc < 0) |
| 969 | rc = ext->oe_rc; |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 970 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Discard pages with index greater than @size. If @ext is overlapped with |
| 975 | * @size, then partial truncate happens. |
| 976 | */ |
| 977 | static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index, |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 978 | bool partial) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 979 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 980 | struct cl_env_nest nest; |
| 981 | struct lu_env *env; |
| 982 | struct cl_io *io; |
| 983 | struct osc_object *obj = ext->oe_obj; |
| 984 | struct client_obd *cli = osc_cli(obj); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 985 | struct osc_async_page *oap; |
| 986 | struct osc_async_page *tmp; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 987 | int pages_in_chunk = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 988 | int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 989 | __u64 trunc_chunk = trunc_index >> ppc_bits; |
| 990 | int grants = 0; |
| 991 | int nr_pages = 0; |
| 992 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 993 | |
| 994 | LASSERT(sanity_check(ext) == 0); |
Andriy Skulysh | ce248d5 | 2014-06-22 21:32:16 -0400 | [diff] [blame] | 995 | EASSERT(ext->oe_state == OES_TRUNC, ext); |
| 996 | EASSERT(!ext->oe_urgent, ext); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 997 | |
| 998 | /* Request new lu_env. |
| 999 | * We can't use that env from osc_cache_truncate_start() because |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1000 | * it's from lov_io_sub and not fully initialized. |
| 1001 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1002 | env = cl_env_nested_get(&nest); |
| 1003 | io = &osc_env_info(env)->oti_io; |
| 1004 | io->ci_obj = cl_object_top(osc2cl(obj)); |
| 1005 | rc = cl_io_init(env, io, CIT_MISC, io->ci_obj); |
| 1006 | if (rc < 0) |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1007 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1008 | |
| 1009 | /* discard all pages with index greater then trunc_index */ |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 1010 | list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1011 | pgoff_t index = osc_index(oap2osc(oap)); |
| 1012 | struct cl_page *page = oap2cl_page(oap); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1013 | |
| 1014 | LASSERT(list_empty(&oap->oap_rpc_item)); |
| 1015 | |
| 1016 | /* only discard the pages with their index greater than |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1017 | * trunc_index, and ... |
| 1018 | */ |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1019 | if (index < trunc_index || |
| 1020 | (index == trunc_index && partial)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1021 | /* accounting how many pages remaining in the chunk |
| 1022 | * so that we can calculate grants correctly. */ |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1023 | if (index >> ppc_bits == trunc_chunk) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1024 | ++pages_in_chunk; |
| 1025 | continue; |
| 1026 | } |
| 1027 | |
| 1028 | list_del_init(&oap->oap_pending_item); |
| 1029 | |
| 1030 | cl_page_get(page); |
| 1031 | lu_ref_add(&page->cp_reference, "truncate", current); |
| 1032 | |
| 1033 | if (cl_page_own(env, io, page) == 0) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1034 | cl_page_discard(env, io, page); |
| 1035 | cl_page_disown(env, io, page); |
| 1036 | } else { |
| 1037 | LASSERT(page->cp_state == CPS_FREEING); |
| 1038 | LASSERT(0); |
| 1039 | } |
| 1040 | |
| 1041 | lu_ref_del(&page->cp_reference, "truncate", current); |
| 1042 | cl_page_put(env, page); |
| 1043 | |
| 1044 | --ext->oe_nr_pages; |
| 1045 | ++nr_pages; |
| 1046 | } |
| 1047 | EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial, |
| 1048 | ext->oe_nr_pages == 0), |
| 1049 | ext, "trunc_index %lu, partial %d\n", trunc_index, partial); |
| 1050 | |
| 1051 | osc_object_lock(obj); |
| 1052 | if (ext->oe_nr_pages == 0) { |
| 1053 | LASSERT(pages_in_chunk == 0); |
| 1054 | grants = ext->oe_grants; |
| 1055 | ext->oe_grants = 0; |
| 1056 | } else { /* calculate how many grants we can free */ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1057 | int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1058 | pgoff_t last_index; |
| 1059 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1060 | /* if there is no pages in this chunk, we can also free grants |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1061 | * for the last chunk |
| 1062 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1063 | if (pages_in_chunk == 0) { |
| 1064 | /* if this is the 1st chunk and no pages in this chunk, |
| 1065 | * ext->oe_nr_pages must be zero, so we should be in |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1066 | * the other if-clause. |
| 1067 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1068 | LASSERT(trunc_chunk > 0); |
| 1069 | --trunc_chunk; |
| 1070 | ++chunks; |
| 1071 | } |
| 1072 | |
| 1073 | /* this is what we can free from this extent */ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1074 | grants = chunks << cli->cl_chunkbits; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1075 | ext->oe_grants -= grants; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1076 | last_index = ((trunc_chunk + 1) << ppc_bits) - 1; |
| 1077 | ext->oe_end = min(last_index, ext->oe_max_end); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1078 | LASSERT(ext->oe_end >= ext->oe_start); |
| 1079 | LASSERT(ext->oe_grants > 0); |
| 1080 | } |
| 1081 | osc_object_unlock(obj); |
| 1082 | |
| 1083 | if (grants > 0 || nr_pages > 0) |
| 1084 | osc_free_grant(cli, nr_pages, grants); |
| 1085 | |
| 1086 | out: |
| 1087 | cl_io_fini(env, io); |
| 1088 | cl_env_nested_put(&nest, env); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1089 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * This function is used to make the extent prepared for transfer. |
Masanari Iida | 34ca8748 | 2014-03-08 22:58:36 +0900 | [diff] [blame] | 1094 | * A race with flushing page - ll_writepage() has to be handled cautiously. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1095 | */ |
| 1096 | static int osc_extent_make_ready(const struct lu_env *env, |
| 1097 | struct osc_extent *ext) |
| 1098 | { |
| 1099 | struct osc_async_page *oap; |
| 1100 | struct osc_async_page *last = NULL; |
| 1101 | struct osc_object *obj = ext->oe_obj; |
| 1102 | int page_count = 0; |
| 1103 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1104 | |
| 1105 | /* we're going to grab page lock, so object lock must not be taken. */ |
| 1106 | LASSERT(sanity_check(ext) == 0); |
| 1107 | /* in locking state, any process should not touch this extent. */ |
| 1108 | EASSERT(ext->oe_state == OES_LOCKING, ext); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1109 | EASSERT(ext->oe_owner, ext); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1110 | |
| 1111 | OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n"); |
| 1112 | |
| 1113 | list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) { |
| 1114 | ++page_count; |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1115 | if (!last || last->oap_obj_off < oap->oap_obj_off) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1116 | last = oap; |
| 1117 | |
| 1118 | /* checking ASYNC_READY is race safe */ |
| 1119 | if ((oap->oap_async_flags & ASYNC_READY) != 0) |
| 1120 | continue; |
| 1121 | |
| 1122 | rc = osc_make_ready(env, oap, OBD_BRW_WRITE); |
| 1123 | switch (rc) { |
| 1124 | case 0: |
| 1125 | spin_lock(&oap->oap_lock); |
| 1126 | oap->oap_async_flags |= ASYNC_READY; |
| 1127 | spin_unlock(&oap->oap_lock); |
| 1128 | break; |
| 1129 | case -EALREADY: |
| 1130 | LASSERT((oap->oap_async_flags & ASYNC_READY) != 0); |
| 1131 | break; |
| 1132 | default: |
| 1133 | LASSERTF(0, "unknown return code: %d\n", rc); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | LASSERT(page_count == ext->oe_nr_pages); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1138 | LASSERT(last); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1139 | /* the last page is the only one we need to refresh its count by |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1140 | * the size of file. |
| 1141 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1142 | if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) { |
| 1143 | last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE); |
| 1144 | LASSERT(last->oap_count > 0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1145 | LASSERT(last->oap_page_off + last->oap_count <= PAGE_SIZE); |
Sebastien Buisson | 82281bc | 2016-04-12 16:14:01 -0400 | [diff] [blame] | 1146 | spin_lock(&last->oap_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1147 | last->oap_async_flags |= ASYNC_COUNT_STABLE; |
Sebastien Buisson | 82281bc | 2016-04-12 16:14:01 -0400 | [diff] [blame] | 1148 | spin_unlock(&last->oap_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /* for the rest of pages, we don't need to call osf_refresh_count() |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1152 | * because it's known they are not the last page |
| 1153 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1154 | list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) { |
| 1155 | if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1156 | oap->oap_count = PAGE_SIZE - oap->oap_page_off; |
Sebastien Buisson | 82281bc | 2016-04-12 16:14:01 -0400 | [diff] [blame] | 1157 | spin_lock(&last->oap_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1158 | oap->oap_async_flags |= ASYNC_COUNT_STABLE; |
Sebastien Buisson | 82281bc | 2016-04-12 16:14:01 -0400 | [diff] [blame] | 1159 | spin_unlock(&last->oap_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | osc_object_lock(obj); |
| 1164 | osc_extent_state_set(ext, OES_RPC); |
| 1165 | osc_object_unlock(obj); |
| 1166 | /* get a refcount for RPC. */ |
| 1167 | osc_extent_get(ext); |
| 1168 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1169 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Quick and simple version of osc_extent_find(). This function is frequently |
| 1174 | * called to expand the extent for the same IO. To expand the extent, the |
| 1175 | * page index must be in the same or next chunk of ext->oe_end. |
| 1176 | */ |
| 1177 | static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants) |
| 1178 | { |
| 1179 | struct osc_object *obj = ext->oe_obj; |
| 1180 | struct client_obd *cli = osc_cli(obj); |
| 1181 | struct osc_extent *next; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1182 | int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1183 | pgoff_t chunk = index >> ppc_bits; |
| 1184 | pgoff_t end_chunk; |
| 1185 | pgoff_t end_index; |
| 1186 | int chunksize = 1 << cli->cl_chunkbits; |
| 1187 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1188 | |
| 1189 | LASSERT(ext->oe_max_end >= index && ext->oe_start <= index); |
| 1190 | osc_object_lock(obj); |
| 1191 | LASSERT(sanity_check_nolock(ext) == 0); |
| 1192 | end_chunk = ext->oe_end >> ppc_bits; |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1193 | if (chunk > end_chunk + 1) { |
| 1194 | rc = -ERANGE; |
| 1195 | goto out; |
| 1196 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1197 | |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1198 | if (end_chunk >= chunk) { |
| 1199 | rc = 0; |
| 1200 | goto out; |
| 1201 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1202 | |
| 1203 | LASSERT(end_chunk + 1 == chunk); |
| 1204 | /* try to expand this extent to cover @index */ |
| 1205 | end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1); |
| 1206 | |
| 1207 | next = next_extent(ext); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1208 | if (next && next->oe_start <= end_index) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1209 | /* complex mode - overlapped with the next extent, |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1210 | * this case will be handled by osc_extent_find() |
| 1211 | */ |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1212 | rc = -EAGAIN; |
| 1213 | goto out; |
| 1214 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1215 | |
| 1216 | ext->oe_end = end_index; |
| 1217 | ext->oe_grants += chunksize; |
| 1218 | *grants -= chunksize; |
| 1219 | LASSERT(*grants >= 0); |
| 1220 | EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext, |
| 1221 | "overlapped after expanding for %lu.\n", index); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1222 | |
| 1223 | out: |
| 1224 | osc_object_unlock(obj); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1225 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void osc_extent_tree_dump0(int level, struct osc_object *obj, |
| 1229 | const char *func, int line) |
| 1230 | { |
| 1231 | struct osc_extent *ext; |
| 1232 | int cnt; |
| 1233 | |
| 1234 | CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n", |
| 1235 | obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc); |
| 1236 | |
| 1237 | /* osc_object_lock(obj); */ |
| 1238 | cnt = 1; |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1239 | for (ext = first_extent(obj); ext; ext = next_extent(ext)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1240 | OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++); |
| 1241 | |
| 1242 | cnt = 1; |
| 1243 | list_for_each_entry(ext, &obj->oo_hp_exts, oe_link) |
| 1244 | OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++); |
| 1245 | |
| 1246 | cnt = 1; |
| 1247 | list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link) |
| 1248 | OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++); |
| 1249 | |
| 1250 | cnt = 1; |
| 1251 | list_for_each_entry(ext, &obj->oo_reading_exts, oe_link) |
| 1252 | OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++); |
| 1253 | /* osc_object_unlock(obj); */ |
| 1254 | } |
| 1255 | |
| 1256 | /* ------------------ osc extent end ------------------ */ |
| 1257 | |
| 1258 | static inline int osc_is_ready(struct osc_object *osc) |
| 1259 | { |
| 1260 | return !list_empty(&osc->oo_ready_item) || |
| 1261 | !list_empty(&osc->oo_hp_ready_item); |
| 1262 | } |
| 1263 | |
| 1264 | #define OSC_IO_DEBUG(OSC, STR, args...) \ |
| 1265 | CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \ |
| 1266 | (OSC), osc_is_ready(OSC), \ |
| 1267 | list_empty_marker(&(OSC)->oo_hp_ready_item), \ |
| 1268 | list_empty_marker(&(OSC)->oo_ready_item), \ |
| 1269 | atomic_read(&(OSC)->oo_nr_writes), \ |
| 1270 | list_empty_marker(&(OSC)->oo_hp_exts), \ |
| 1271 | list_empty_marker(&(OSC)->oo_urgent_exts), \ |
| 1272 | atomic_read(&(OSC)->oo_nr_reads), \ |
| 1273 | list_empty_marker(&(OSC)->oo_reading_exts), \ |
| 1274 | ##args) |
| 1275 | |
| 1276 | static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap, |
| 1277 | int cmd) |
| 1278 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1279 | struct osc_page *opg = oap2osc_page(oap); |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1280 | struct cl_page *page = oap2cl_page(oap); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1281 | int result; |
| 1282 | |
| 1283 | LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */ |
| 1284 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1285 | result = cl_page_make_ready(env, page, CRT_WRITE); |
| 1286 | if (result == 0) |
| 1287 | opg->ops_submit_time = cfs_time_current(); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1288 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | static int osc_refresh_count(const struct lu_env *env, |
| 1292 | struct osc_async_page *oap, int cmd) |
| 1293 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1294 | struct osc_page *opg = oap2osc_page(oap); |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1295 | pgoff_t index = osc_index(oap2osc(oap)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1296 | struct cl_object *obj; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1297 | struct cl_attr *attr = &osc_env_info(env)->oti_attr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1298 | |
| 1299 | int result; |
| 1300 | loff_t kms; |
| 1301 | |
| 1302 | /* readpage queues with _COUNT_STABLE, shouldn't get here. */ |
| 1303 | LASSERT(!(cmd & OBD_BRW_READ)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1304 | obj = opg->ops_cl.cpl_obj; |
| 1305 | |
| 1306 | cl_object_attr_lock(obj); |
| 1307 | result = cl_object_attr_get(env, obj, attr); |
| 1308 | cl_object_attr_unlock(obj); |
| 1309 | if (result < 0) |
| 1310 | return result; |
| 1311 | kms = attr->cat_kms; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1312 | if (cl_offset(obj, index) >= kms) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1313 | /* catch race with truncate */ |
| 1314 | return 0; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1315 | else if (cl_offset(obj, index + 1) > kms) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1316 | /* catch sub-page write at end of file */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1317 | return kms % PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1318 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1319 | return PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static int osc_completion(const struct lu_env *env, struct osc_async_page *oap, |
| 1323 | int cmd, int rc) |
| 1324 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1325 | struct osc_page *opg = oap2osc_page(oap); |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 1326 | struct cl_page *page = oap2cl_page(oap); |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1327 | struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj); |
| 1328 | enum cl_req_type crt; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1329 | int srvlock; |
| 1330 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1331 | cmd &= ~OBD_BRW_NOQUOTA; |
Niu Yawei | 72a50e4 | 2016-04-27 18:21:02 -0400 | [diff] [blame] | 1332 | LASSERTF(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ), |
| 1333 | "cp_state:%u, cmd:%d\n", page->cp_state, cmd); |
| 1334 | LASSERTF(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE), |
| 1335 | "cp_state:%u, cmd:%d\n", page->cp_state, cmd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1336 | LASSERT(opg->ops_transfer_pinned); |
| 1337 | |
| 1338 | /* |
| 1339 | * page->cp_req can be NULL if io submission failed before |
| 1340 | * cl_req was allocated. |
| 1341 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1342 | if (page->cp_req) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1343 | cl_req_page_done(env, page); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1344 | LASSERT(!page->cp_req); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1345 | |
| 1346 | crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE; |
| 1347 | /* Clear opg->ops_transfer_pinned before VM lock is released. */ |
| 1348 | opg->ops_transfer_pinned = 0; |
| 1349 | |
| 1350 | spin_lock(&obj->oo_seatbelt); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1351 | LASSERT(opg->ops_submitter); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1352 | LASSERT(!list_empty(&opg->ops_inflight)); |
| 1353 | list_del_init(&opg->ops_inflight); |
| 1354 | opg->ops_submitter = NULL; |
| 1355 | spin_unlock(&obj->oo_seatbelt); |
| 1356 | |
| 1357 | opg->ops_submit_time = 0; |
| 1358 | srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK; |
| 1359 | |
| 1360 | /* statistic */ |
| 1361 | if (rc == 0 && srvlock) { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1362 | struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1363 | struct osc_stats *stats = &lu2osc_dev(ld)->od_stats; |
| 1364 | int bytes = oap->oap_count; |
| 1365 | |
| 1366 | if (crt == CRT_READ) |
| 1367 | stats->os_lockless_reads += bytes; |
| 1368 | else |
| 1369 | stats->os_lockless_writes += bytes; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * This has to be the last operation with the page, as locks are |
| 1374 | * released in cl_page_completion() and nothing except for the |
| 1375 | * reference counter protects page from concurrent reclaim. |
| 1376 | */ |
| 1377 | lu_ref_del(&page->cp_reference, "transfer", page); |
| 1378 | |
| 1379 | cl_page_completion(env, page, crt, rc); |
| 1380 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1381 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1382 | } |
| 1383 | |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1384 | #define OSC_DUMP_GRANT(lvl, cli, fmt, args...) do { \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1385 | struct client_obd *__tmp = (cli); \ |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1386 | CDEBUG(lvl, "%s: grant { dirty: %ld/%ld dirty_pages: %d/%d " \ |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1387 | "unstable_pages: %d/%d dropped: %ld avail: %ld, " \ |
| 1388 | "reserved: %ld, flight: %d } lru {in list: %d, " \ |
| 1389 | "left: %d, waiters: %d }" fmt, \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1390 | __tmp->cl_import->imp_obd->obd_name, \ |
| 1391 | __tmp->cl_dirty, __tmp->cl_dirty_max, \ |
| 1392 | atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \ |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1393 | atomic_read(&obd_unstable_pages), obd_max_dirty_pages, \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1394 | __tmp->cl_lost_grant, __tmp->cl_avail_grant, \ |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1395 | __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, \ |
| 1396 | atomic_read(&__tmp->cl_lru_in_list), \ |
| 1397 | atomic_read(&__tmp->cl_lru_busy), \ |
| 1398 | atomic_read(&__tmp->cl_lru_shrinkers), ##args); \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1399 | } while (0) |
| 1400 | |
| 1401 | /* caller must hold loi_list_lock */ |
| 1402 | static void osc_consume_write_grant(struct client_obd *cli, |
| 1403 | struct brw_page *pga) |
| 1404 | { |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1405 | assert_spin_locked(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1406 | LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT)); |
| 1407 | atomic_inc(&obd_dirty_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1408 | cli->cl_dirty += PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1409 | pga->flag |= OBD_BRW_FROM_GRANT; |
| 1410 | CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1411 | PAGE_SIZE, pga, pga->pg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1412 | osc_update_next_shrink(cli); |
| 1413 | } |
| 1414 | |
| 1415 | /* the companion to osc_consume_write_grant, called when a brw has completed. |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1416 | * must be called with the loi lock held. |
| 1417 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1418 | static void osc_release_write_grant(struct client_obd *cli, |
| 1419 | struct brw_page *pga) |
| 1420 | { |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1421 | assert_spin_locked(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1422 | if (!(pga->flag & OBD_BRW_FROM_GRANT)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1423 | return; |
| 1424 | } |
| 1425 | |
| 1426 | pga->flag &= ~OBD_BRW_FROM_GRANT; |
| 1427 | atomic_dec(&obd_dirty_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1428 | cli->cl_dirty -= PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1429 | if (pga->flag & OBD_BRW_NOCACHE) { |
| 1430 | pga->flag &= ~OBD_BRW_NOCACHE; |
| 1431 | atomic_dec(&obd_dirty_transit_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1432 | cli->cl_dirty_transit -= PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1433 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * To avoid sleeping with object lock held, it's good for us allocate enough |
| 1438 | * grants before entering into critical section. |
| 1439 | * |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1440 | * spin_lock held by caller |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1441 | */ |
| 1442 | static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes) |
| 1443 | { |
| 1444 | int rc = -EDQUOT; |
| 1445 | |
| 1446 | if (cli->cl_avail_grant >= bytes) { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1447 | cli->cl_avail_grant -= bytes; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1448 | cli->cl_reserved_grant += bytes; |
| 1449 | rc = 0; |
| 1450 | } |
| 1451 | return rc; |
| 1452 | } |
| 1453 | |
| 1454 | static void __osc_unreserve_grant(struct client_obd *cli, |
| 1455 | unsigned int reserved, unsigned int unused) |
| 1456 | { |
| 1457 | /* it's quite normal for us to get more grant than reserved. |
| 1458 | * Thinking about a case that two extents merged by adding a new |
| 1459 | * chunk, we can save one extent tax. If extent tax is greater than |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1460 | * one chunk, we can save more grant by adding a new chunk |
| 1461 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1462 | cli->cl_reserved_grant -= reserved; |
| 1463 | if (unused > reserved) { |
| 1464 | cli->cl_avail_grant += reserved; |
| 1465 | cli->cl_lost_grant += unused - reserved; |
| 1466 | } else { |
| 1467 | cli->cl_avail_grant += unused; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Shraddha Barke | 6ef3f3c | 2015-12-18 02:40:28 +0530 | [diff] [blame] | 1471 | static void osc_unreserve_grant(struct client_obd *cli, |
| 1472 | unsigned int reserved, unsigned int unused) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1473 | { |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1474 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1475 | __osc_unreserve_grant(cli, reserved, unused); |
| 1476 | if (unused > 0) |
| 1477 | osc_wake_cache_waiters(cli); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1478 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * Free grant after IO is finished or canceled. |
| 1483 | * |
| 1484 | * @lost_grant is used to remember how many grants we have allocated but not |
| 1485 | * used, we should return these grants to OST. There're two cases where grants |
| 1486 | * can be lost: |
| 1487 | * 1. truncate; |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 1488 | * 2. blocksize at OST is less than PAGE_SIZE and a partial page was |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1489 | * written. In this case OST may use less chunks to serve this partial |
| 1490 | * write. OSTs don't actually know the page size on the client side. so |
| 1491 | * clients have to calculate lost grant by the blocksize on the OST. |
| 1492 | * See filter_grant_check() for details. |
| 1493 | */ |
| 1494 | static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages, |
| 1495 | unsigned int lost_grant) |
| 1496 | { |
| 1497 | int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax; |
| 1498 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1499 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1500 | atomic_sub(nr_pages, &obd_dirty_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1501 | cli->cl_dirty -= nr_pages << PAGE_SHIFT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1502 | cli->cl_lost_grant += lost_grant; |
| 1503 | if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) { |
| 1504 | /* borrow some grant from truncate to avoid the case that |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1505 | * truncate uses up all avail grant |
| 1506 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1507 | cli->cl_lost_grant -= grant; |
| 1508 | cli->cl_avail_grant += grant; |
| 1509 | } |
| 1510 | osc_wake_cache_waiters(cli); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1511 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1512 | CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n", |
| 1513 | lost_grant, cli->cl_lost_grant, |
| 1514 | cli->cl_avail_grant, cli->cl_dirty); |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * The companion to osc_enter_cache(), called when @oap is no longer part of |
| 1519 | * the dirty accounting due to error. |
| 1520 | */ |
| 1521 | static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap) |
| 1522 | { |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1523 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1524 | osc_release_write_grant(cli, &oap->oap_brw_page); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1525 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Non-blocking version of osc_enter_cache() that consumes grant only when it |
| 1530 | * is available. |
| 1531 | */ |
| 1532 | static int osc_enter_cache_try(struct client_obd *cli, |
| 1533 | struct osc_async_page *oap, |
| 1534 | int bytes, int transient) |
| 1535 | { |
| 1536 | int rc; |
| 1537 | |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1538 | OSC_DUMP_GRANT(D_CACHE, cli, "need:%d.\n", bytes); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1539 | |
| 1540 | rc = osc_reserve_grant(cli, bytes); |
| 1541 | if (rc < 0) |
| 1542 | return 0; |
| 1543 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1544 | if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max && |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1545 | atomic_read(&obd_unstable_pages) + 1 + |
| 1546 | atomic_read(&obd_dirty_pages) <= obd_max_dirty_pages) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1547 | osc_consume_write_grant(cli, &oap->oap_brw_page); |
| 1548 | if (transient) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1549 | cli->cl_dirty_transit += PAGE_SIZE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1550 | atomic_inc(&obd_dirty_transit_pages); |
| 1551 | oap->oap_brw_flags |= OBD_BRW_NOCACHE; |
| 1552 | } |
| 1553 | rc = 1; |
| 1554 | } else { |
| 1555 | __osc_unreserve_grant(cli, bytes, bytes); |
| 1556 | rc = 0; |
| 1557 | } |
| 1558 | return rc; |
| 1559 | } |
| 1560 | |
| 1561 | static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw) |
| 1562 | { |
| 1563 | int rc; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 1564 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1565 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1566 | rc = list_empty(&ocw->ocw_entry); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1567 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1568 | return rc; |
| 1569 | } |
| 1570 | |
| 1571 | /** |
| 1572 | * The main entry to reserve dirty page accounting. Usually the grant reserved |
| 1573 | * in this function will be freed in bulk in osc_free_grant() unless it fails |
| 1574 | * to add osc cache, in that case, it will be freed in osc_exit_cache(). |
| 1575 | * |
| 1576 | * The process will be put into sleep if it's already run out of grant. |
| 1577 | */ |
| 1578 | static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli, |
| 1579 | struct osc_async_page *oap, int bytes) |
| 1580 | { |
| 1581 | struct osc_object *osc = oap->oap_obj; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1582 | struct lov_oinfo *loi = osc->oo_oinfo; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1583 | struct osc_cache_waiter ocw; |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1584 | struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL, |
| 1585 | LWI_ON_SIGNAL_NOOP, NULL); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1586 | int rc = -EDQUOT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1587 | |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1588 | OSC_DUMP_GRANT(D_CACHE, cli, "need:%d.\n", bytes); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1589 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1590 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1591 | |
| 1592 | /* force the caller to try sync io. this can jump the list |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1593 | * of queued writes and create a discontiguous rpc stream |
| 1594 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1595 | if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) || |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1596 | cli->cl_dirty_max < PAGE_SIZE || |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1597 | cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync) { |
| 1598 | rc = -EDQUOT; |
| 1599 | goto out; |
| 1600 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1601 | |
| 1602 | /* Hopefully normal case - cache space and write credits available */ |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1603 | if (osc_enter_cache_try(cli, oap, bytes, 0)) { |
| 1604 | rc = 0; |
| 1605 | goto out; |
| 1606 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1607 | |
| 1608 | /* We can get here for two reasons: too many dirty pages in cache, or |
| 1609 | * run out of grants. In both cases we should write dirty pages out. |
| 1610 | * Adding a cache waiter will trigger urgent write-out no matter what |
| 1611 | * RPC size will be. |
| 1612 | * The exiting condition is no avail grants and no dirty pages caching, |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1613 | * that really means there is no space on the OST. |
| 1614 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1615 | init_waitqueue_head(&ocw.ocw_waitq); |
| 1616 | ocw.ocw_oap = oap; |
| 1617 | ocw.ocw_grant = bytes; |
| 1618 | while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) { |
| 1619 | list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters); |
| 1620 | ocw.ocw_rc = 0; |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1621 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1622 | |
| 1623 | osc_io_unplug_async(env, cli, NULL); |
| 1624 | |
| 1625 | CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n", |
| 1626 | cli->cl_import->imp_obd->obd_name, &ocw, oap); |
| 1627 | |
| 1628 | rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi); |
| 1629 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1630 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1631 | |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1632 | /* l_wait_event is interrupted by signal, or timed out */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1633 | if (rc < 0) { |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1634 | if (rc == -ETIMEDOUT) { |
| 1635 | OSC_DUMP_GRANT(D_ERROR, cli, |
| 1636 | "try to reserve %d.\n", bytes); |
| 1637 | osc_extent_tree_dump(D_ERROR, osc); |
| 1638 | rc = -EDQUOT; |
| 1639 | } |
| 1640 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1641 | list_del_init(&ocw.ocw_entry); |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1642 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | LASSERT(list_empty(&ocw.ocw_entry)); |
| 1646 | rc = ocw.ocw_rc; |
| 1647 | |
| 1648 | if (rc != -EDQUOT) |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 1649 | goto out; |
| 1650 | if (osc_enter_cache_try(cli, oap, bytes, 0)) { |
| 1651 | rc = 0; |
| 1652 | goto out; |
| 1653 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1654 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1655 | out: |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1656 | spin_unlock(&cli->cl_loi_list_lock); |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 1657 | OSC_DUMP_GRANT(D_CACHE, cli, "returned %d.\n", rc); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1658 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* caller must hold loi_list_lock */ |
| 1662 | void osc_wake_cache_waiters(struct client_obd *cli) |
| 1663 | { |
| 1664 | struct list_head *l, *tmp; |
| 1665 | struct osc_cache_waiter *ocw; |
| 1666 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1667 | list_for_each_safe(l, tmp, &cli->cl_cache_waiters) { |
| 1668 | ocw = list_entry(l, struct osc_cache_waiter, ocw_entry); |
| 1669 | list_del_init(&ocw->ocw_entry); |
| 1670 | |
| 1671 | ocw->ocw_rc = -EDQUOT; |
| 1672 | /* we can't dirty more */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1673 | if ((cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) || |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1674 | (atomic_read(&obd_unstable_pages) + 1 + |
| 1675 | atomic_read(&obd_dirty_pages) > obd_max_dirty_pages)) { |
Joe Perches | 2d00bd1 | 2014-11-23 11:28:50 -0800 | [diff] [blame] | 1676 | CDEBUG(D_CACHE, "no dirty room: dirty: %ld osc max %ld, sys max %d\n", |
| 1677 | cli->cl_dirty, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1678 | cli->cl_dirty_max, obd_max_dirty_pages); |
| 1679 | goto wakeup; |
| 1680 | } |
| 1681 | |
| 1682 | ocw->ocw_rc = 0; |
| 1683 | if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0)) |
| 1684 | ocw->ocw_rc = -EDQUOT; |
| 1685 | |
| 1686 | wakeup: |
| 1687 | CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n", |
| 1688 | ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc); |
| 1689 | |
| 1690 | wake_up(&ocw->ocw_waitq); |
| 1691 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc) |
| 1695 | { |
| 1696 | int hprpc = !!list_empty(&osc->oo_hp_exts); |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 1697 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1698 | return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc; |
| 1699 | } |
| 1700 | |
| 1701 | /* This maintains the lists of pending pages to read/write for a given object |
| 1702 | * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint() |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1703 | * to quickly find objects that are ready to send an RPC. |
| 1704 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1705 | static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc, |
| 1706 | int cmd) |
| 1707 | { |
| 1708 | int invalid_import = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1709 | |
| 1710 | /* if we have an invalid import we want to drain the queued pages |
| 1711 | * by forcing them through rpcs that immediately fail and complete |
| 1712 | * the pages. recovery relies on this to empty the queued pages |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1713 | * before canceling the locks and evicting down the llite pages |
| 1714 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1715 | if (!cli->cl_import || cli->cl_import->imp_invalid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1716 | invalid_import = 1; |
| 1717 | |
| 1718 | if (cmd & OBD_BRW_WRITE) { |
| 1719 | if (atomic_read(&osc->oo_nr_writes) == 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1720 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1721 | if (invalid_import) { |
| 1722 | CDEBUG(D_CACHE, "invalid import forcing RPC\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1723 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1724 | } |
| 1725 | if (!list_empty(&osc->oo_hp_exts)) { |
| 1726 | CDEBUG(D_CACHE, "high prio request forcing RPC\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1727 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1728 | } |
| 1729 | if (!list_empty(&osc->oo_urgent_exts)) { |
| 1730 | CDEBUG(D_CACHE, "urgent request forcing RPC\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1731 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1732 | } |
| 1733 | /* trigger a write rpc stream as long as there are dirtiers |
| 1734 | * waiting for space. as they're waiting, they're not going to |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1735 | * create more pages to coalesce with what's waiting.. |
| 1736 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1737 | if (!list_empty(&cli->cl_cache_waiters)) { |
| 1738 | CDEBUG(D_CACHE, "cache waiters forcing RPC\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1739 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1740 | } |
| 1741 | if (atomic_read(&osc->oo_nr_writes) >= |
| 1742 | cli->cl_max_pages_per_rpc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1743 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1744 | } else { |
| 1745 | if (atomic_read(&osc->oo_nr_reads) == 0) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1746 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1747 | if (invalid_import) { |
| 1748 | CDEBUG(D_CACHE, "invalid import forcing RPC\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1749 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1750 | } |
| 1751 | /* all read are urgent. */ |
| 1752 | if (!list_empty(&osc->oo_reading_exts)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1753 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1754 | } |
| 1755 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1756 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | static void osc_update_pending(struct osc_object *obj, int cmd, int delta) |
| 1760 | { |
| 1761 | struct client_obd *cli = osc_cli(obj); |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 1762 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1763 | if (cmd & OBD_BRW_WRITE) { |
| 1764 | atomic_add(delta, &obj->oo_nr_writes); |
| 1765 | atomic_add(delta, &cli->cl_pending_w_pages); |
| 1766 | LASSERT(atomic_read(&obj->oo_nr_writes) >= 0); |
| 1767 | } else { |
| 1768 | atomic_add(delta, &obj->oo_nr_reads); |
| 1769 | atomic_add(delta, &cli->cl_pending_r_pages); |
| 1770 | LASSERT(atomic_read(&obj->oo_nr_reads) >= 0); |
| 1771 | } |
| 1772 | OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta); |
| 1773 | } |
| 1774 | |
| 1775 | static int osc_makes_hprpc(struct osc_object *obj) |
| 1776 | { |
| 1777 | return !list_empty(&obj->oo_hp_exts); |
| 1778 | } |
| 1779 | |
| 1780 | static void on_list(struct list_head *item, struct list_head *list, int should_be_on) |
| 1781 | { |
| 1782 | if (list_empty(item) && should_be_on) |
| 1783 | list_add_tail(item, list); |
| 1784 | else if (!list_empty(item) && !should_be_on) |
| 1785 | list_del_init(item); |
| 1786 | } |
| 1787 | |
| 1788 | /* maintain the osc's cli list membership invariants so that osc_send_oap_rpc |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1789 | * can find pages to build into rpcs quickly |
| 1790 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1791 | static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc) |
| 1792 | { |
| 1793 | if (osc_makes_hprpc(osc)) { |
| 1794 | /* HP rpc */ |
| 1795 | on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0); |
| 1796 | on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1); |
| 1797 | } else { |
| 1798 | on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0); |
| 1799 | on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, |
| 1800 | osc_makes_rpc(cli, osc, OBD_BRW_WRITE) || |
| 1801 | osc_makes_rpc(cli, osc, OBD_BRW_READ)); |
| 1802 | } |
| 1803 | |
| 1804 | on_list(&osc->oo_write_item, &cli->cl_loi_write_list, |
| 1805 | atomic_read(&osc->oo_nr_writes) > 0); |
| 1806 | |
| 1807 | on_list(&osc->oo_read_item, &cli->cl_loi_read_list, |
| 1808 | atomic_read(&osc->oo_nr_reads) > 0); |
| 1809 | |
| 1810 | return osc_is_ready(osc); |
| 1811 | } |
| 1812 | |
| 1813 | static int osc_list_maint(struct client_obd *cli, struct osc_object *osc) |
| 1814 | { |
| 1815 | int is_ready; |
| 1816 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1817 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1818 | is_ready = __osc_list_maint(cli, osc); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1819 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1820 | |
| 1821 | return is_ready; |
| 1822 | } |
| 1823 | |
Masanari Iida | 11d66e8 | 2013-12-14 02:24:04 +0900 | [diff] [blame] | 1824 | /* this is trying to propagate async writeback errors back up to the |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1825 | * application. As an async write fails we record the error code for later if |
| 1826 | * the app does an fsync. As long as errors persist we force future rpcs to be |
| 1827 | * sync so that the app can get a sync error and break the cycle of queueing |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1828 | * pages for which writeback will fail. |
| 1829 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1830 | static void osc_process_ar(struct osc_async_rc *ar, __u64 xid, |
| 1831 | int rc) |
| 1832 | { |
| 1833 | if (rc) { |
| 1834 | if (!ar->ar_rc) |
| 1835 | ar->ar_rc = rc; |
| 1836 | |
| 1837 | ar->ar_force_sync = 1; |
| 1838 | ar->ar_min_xid = ptlrpc_sample_next_xid(); |
| 1839 | return; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | if (ar->ar_force_sync && (xid >= ar->ar_min_xid)) |
| 1843 | ar->ar_force_sync = 0; |
| 1844 | } |
| 1845 | |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1846 | /** |
| 1847 | * Performs "unstable" page accounting. This function balances the |
| 1848 | * increment operations performed in osc_inc_unstable_pages. It is |
| 1849 | * registered as the RPC request callback, and is executed when the |
| 1850 | * bulk RPC is committed on the server. Thus at this point, the pages |
| 1851 | * involved in the bulk transfer are no longer considered unstable. |
| 1852 | */ |
| 1853 | void osc_dec_unstable_pages(struct ptlrpc_request *req) |
| 1854 | { |
| 1855 | struct client_obd *cli = &req->rq_import->imp_obd->u.cli; |
| 1856 | struct ptlrpc_bulk_desc *desc = req->rq_bulk; |
| 1857 | int page_count = desc->bd_iov_count; |
| 1858 | int i; |
| 1859 | |
| 1860 | /* No unstable page tracking */ |
| 1861 | if (!cli->cl_cache) |
| 1862 | return; |
| 1863 | |
| 1864 | LASSERT(page_count >= 0); |
| 1865 | |
| 1866 | for (i = 0; i < page_count; i++) |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame^] | 1867 | dec_node_page_state(desc->bd_iov[i].kiov_page, |
| 1868 | NR_UNSTABLE_NFS); |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1869 | |
| 1870 | atomic_sub(page_count, &cli->cl_cache->ccc_unstable_nr); |
| 1871 | LASSERT(atomic_read(&cli->cl_cache->ccc_unstable_nr) >= 0); |
| 1872 | |
Prakash Surya | dece0b9 | 2016-04-27 18:21:05 -0400 | [diff] [blame] | 1873 | atomic_sub(page_count, &cli->cl_unstable_count); |
| 1874 | LASSERT(atomic_read(&cli->cl_unstable_count) >= 0); |
| 1875 | |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1876 | atomic_sub(page_count, &obd_unstable_pages); |
| 1877 | LASSERT(atomic_read(&obd_unstable_pages) >= 0); |
| 1878 | |
| 1879 | spin_lock(&req->rq_lock); |
| 1880 | req->rq_committed = 1; |
| 1881 | req->rq_unstable = 0; |
| 1882 | spin_unlock(&req->rq_lock); |
| 1883 | |
| 1884 | wake_up_all(&cli->cl_cache->ccc_unstable_waitq); |
| 1885 | } |
| 1886 | |
| 1887 | /* "unstable" page accounting. See: osc_dec_unstable_pages. */ |
| 1888 | void osc_inc_unstable_pages(struct ptlrpc_request *req) |
| 1889 | { |
| 1890 | struct client_obd *cli = &req->rq_import->imp_obd->u.cli; |
| 1891 | struct ptlrpc_bulk_desc *desc = req->rq_bulk; |
| 1892 | long page_count = desc->bd_iov_count; |
| 1893 | int i; |
| 1894 | |
| 1895 | /* No unstable page tracking */ |
| 1896 | if (!cli->cl_cache) |
| 1897 | return; |
| 1898 | |
| 1899 | LASSERT(page_count >= 0); |
| 1900 | |
| 1901 | for (i = 0; i < page_count; i++) |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame^] | 1902 | inc_node_page_state(desc->bd_iov[i].kiov_page, |
| 1903 | NR_UNSTABLE_NFS); |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1904 | |
| 1905 | LASSERT(atomic_read(&cli->cl_cache->ccc_unstable_nr) >= 0); |
| 1906 | atomic_add(page_count, &cli->cl_cache->ccc_unstable_nr); |
| 1907 | |
Prakash Surya | dece0b9 | 2016-04-27 18:21:05 -0400 | [diff] [blame] | 1908 | LASSERT(atomic_read(&cli->cl_unstable_count) >= 0); |
| 1909 | atomic_add(page_count, &cli->cl_unstable_count); |
| 1910 | |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1911 | LASSERT(atomic_read(&obd_unstable_pages) >= 0); |
| 1912 | atomic_add(page_count, &obd_unstable_pages); |
| 1913 | |
| 1914 | spin_lock(&req->rq_lock); |
| 1915 | |
| 1916 | /* |
| 1917 | * If the request has already been committed (i.e. brw_commit |
| 1918 | * called via rq_commit_cb), we need to undo the unstable page |
| 1919 | * increments we just performed because rq_commit_cb wont be |
| 1920 | * called again. Otherwise, just set the commit callback so the |
| 1921 | * unstable page accounting is properly updated when the request |
| 1922 | * is committed |
| 1923 | */ |
| 1924 | if (req->rq_committed) { |
| 1925 | /* Drop lock before calling osc_dec_unstable_pages */ |
| 1926 | spin_unlock(&req->rq_lock); |
| 1927 | osc_dec_unstable_pages(req); |
| 1928 | spin_lock(&req->rq_lock); |
| 1929 | } else { |
| 1930 | req->rq_unstable = 1; |
| 1931 | req->rq_commit_cb = osc_dec_unstable_pages; |
| 1932 | } |
| 1933 | |
| 1934 | spin_unlock(&req->rq_lock); |
| 1935 | } |
| 1936 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1937 | /* this must be called holding the loi list lock to give coverage to exit_cache, |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 1938 | * async_flag maintenance, and oap_request |
| 1939 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1940 | static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli, |
| 1941 | struct osc_async_page *oap, int sent, int rc) |
| 1942 | { |
| 1943 | struct osc_object *osc = oap->oap_obj; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 1944 | struct lov_oinfo *loi = osc->oo_oinfo; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1945 | __u64 xid = 0; |
| 1946 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 1947 | if (oap->oap_request) { |
Prakash Surya | ac5b148 | 2016-04-27 18:21:04 -0400 | [diff] [blame] | 1948 | if (!rc) |
| 1949 | osc_inc_unstable_pages(oap->oap_request); |
| 1950 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1951 | xid = ptlrpc_req_xid(oap->oap_request); |
| 1952 | ptlrpc_req_finished(oap->oap_request); |
| 1953 | oap->oap_request = NULL; |
| 1954 | } |
| 1955 | |
| 1956 | /* As the transfer for this page is being done, clear the flags */ |
| 1957 | spin_lock(&oap->oap_lock); |
| 1958 | oap->oap_async_flags = 0; |
| 1959 | spin_unlock(&oap->oap_lock); |
| 1960 | oap->oap_interrupted = 0; |
| 1961 | |
| 1962 | if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) { |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1963 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1964 | osc_process_ar(&cli->cl_ar, xid, rc); |
| 1965 | osc_process_ar(&loi->loi_ar, xid, rc); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 1966 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | rc = osc_completion(env, oap, oap->oap_cmd, rc); |
| 1970 | if (rc) |
| 1971 | CERROR("completion on oap %p obj %p returns %d.\n", |
| 1972 | oap, osc, rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /** |
| 1976 | * Try to add extent to one RPC. We need to think about the following things: |
| 1977 | * - # of pages must not be over max_pages_per_rpc |
| 1978 | * - extent must be compatible with previous ones |
| 1979 | */ |
| 1980 | static int try_to_add_extent_for_io(struct client_obd *cli, |
| 1981 | struct osc_extent *ext, struct list_head *rpclist, |
| 1982 | int *pc, unsigned int *max_pages) |
| 1983 | { |
| 1984 | struct osc_extent *tmp; |
Alexander Boyko | c00266e | 2015-02-01 21:52:01 -0500 | [diff] [blame] | 1985 | struct osc_async_page *oap = list_first_entry(&ext->oe_pages, |
| 1986 | struct osc_async_page, |
| 1987 | oap_pending_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1988 | |
| 1989 | EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE), |
| 1990 | ext); |
| 1991 | |
| 1992 | *max_pages = max(ext->oe_mppr, *max_pages); |
| 1993 | if (*pc + ext->oe_nr_pages > *max_pages) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 1994 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1995 | |
| 1996 | list_for_each_entry(tmp, rpclist, oe_link) { |
Alexander Boyko | c00266e | 2015-02-01 21:52:01 -0500 | [diff] [blame] | 1997 | struct osc_async_page *oap2; |
| 1998 | |
| 1999 | oap2 = list_first_entry(&tmp->oe_pages, struct osc_async_page, |
| 2000 | oap_pending_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2001 | EASSERT(tmp->oe_owner == current, tmp); |
Alexander Boyko | c00266e | 2015-02-01 21:52:01 -0500 | [diff] [blame] | 2002 | if (oap2cl_page(oap)->cp_type != oap2cl_page(oap2)->cp_type) { |
| 2003 | CDEBUG(D_CACHE, "Do not permit different type of IO" |
| 2004 | " for a same RPC\n"); |
| 2005 | return 0; |
| 2006 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2007 | |
| 2008 | if (tmp->oe_srvlock != ext->oe_srvlock || |
| 2009 | !tmp->oe_grants != !ext->oe_grants) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2010 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2011 | |
| 2012 | /* remove break for strict check */ |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | *pc += ext->oe_nr_pages; |
| 2017 | list_move_tail(&ext->oe_link, rpclist); |
| 2018 | ext->oe_owner = current; |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2019 | return 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /** |
| 2023 | * In order to prevent multiple ptlrpcd from breaking contiguous extents, |
| 2024 | * get_write_extent() takes all appropriate extents in atomic. |
| 2025 | * |
| 2026 | * The following policy is used to collect extents for IO: |
| 2027 | * 1. Add as many HP extents as possible; |
| 2028 | * 2. Add the first urgent extent in urgent extent list and take it out of |
| 2029 | * urgent list; |
| 2030 | * 3. Add subsequent extents of this urgent extent; |
| 2031 | * 4. If urgent list is not empty, goto 2; |
| 2032 | * 5. Traverse the extent tree from the 1st extent; |
| 2033 | * 6. Above steps exit if there is no space in this RPC. |
| 2034 | */ |
| 2035 | static int get_write_extents(struct osc_object *obj, struct list_head *rpclist) |
| 2036 | { |
| 2037 | struct client_obd *cli = osc_cli(obj); |
| 2038 | struct osc_extent *ext; |
Bhaktipriya Shridhar | 4a81ce5 | 2016-03-12 01:37:35 +0530 | [diff] [blame] | 2039 | struct osc_extent *temp; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2040 | int page_count = 0; |
| 2041 | unsigned int max_pages = cli->cl_max_pages_per_rpc; |
| 2042 | |
| 2043 | LASSERT(osc_object_is_locked(obj)); |
Bhaktipriya Shridhar | 4a81ce5 | 2016-03-12 01:37:35 +0530 | [diff] [blame] | 2044 | list_for_each_entry_safe(ext, temp, &obj->oo_hp_exts, oe_link) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2045 | LASSERT(ext->oe_state == OES_CACHE); |
| 2046 | if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count, |
| 2047 | &max_pages)) |
| 2048 | return page_count; |
| 2049 | EASSERT(ext->oe_nr_pages <= max_pages, ext); |
| 2050 | } |
| 2051 | if (page_count == max_pages) |
| 2052 | return page_count; |
| 2053 | |
| 2054 | while (!list_empty(&obj->oo_urgent_exts)) { |
| 2055 | ext = list_entry(obj->oo_urgent_exts.next, |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 2056 | struct osc_extent, oe_link); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2057 | if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count, |
| 2058 | &max_pages)) |
| 2059 | return page_count; |
| 2060 | |
| 2061 | if (!ext->oe_intree) |
| 2062 | continue; |
| 2063 | |
| 2064 | while ((ext = next_extent(ext)) != NULL) { |
| 2065 | if ((ext->oe_state != OES_CACHE) || |
| 2066 | (!list_empty(&ext->oe_link) && |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2067 | ext->oe_owner)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2068 | continue; |
| 2069 | |
| 2070 | if (!try_to_add_extent_for_io(cli, ext, rpclist, |
| 2071 | &page_count, &max_pages)) |
| 2072 | return page_count; |
| 2073 | } |
| 2074 | } |
| 2075 | if (page_count == max_pages) |
| 2076 | return page_count; |
| 2077 | |
| 2078 | ext = first_extent(obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2079 | while (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2080 | if ((ext->oe_state != OES_CACHE) || |
| 2081 | /* this extent may be already in current rpclist */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2082 | (!list_empty(&ext->oe_link) && ext->oe_owner)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2083 | ext = next_extent(ext); |
| 2084 | continue; |
| 2085 | } |
| 2086 | |
| 2087 | if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count, |
| 2088 | &max_pages)) |
| 2089 | return page_count; |
| 2090 | |
| 2091 | ext = next_extent(ext); |
| 2092 | } |
| 2093 | return page_count; |
| 2094 | } |
| 2095 | |
| 2096 | static int |
| 2097 | osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli, |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2098 | struct osc_object *osc) |
frank zago | a161de8 | 2015-12-23 12:32:11 -0500 | [diff] [blame] | 2099 | __must_hold(osc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2100 | { |
| 2101 | LIST_HEAD(rpclist); |
| 2102 | struct osc_extent *ext; |
| 2103 | struct osc_extent *tmp; |
| 2104 | struct osc_extent *first = NULL; |
Oleg Drokin | 21aef7d | 2014-08-15 12:55:56 -0400 | [diff] [blame] | 2105 | u32 page_count = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2106 | int srvlock = 0; |
| 2107 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2108 | |
| 2109 | LASSERT(osc_object_is_locked(osc)); |
| 2110 | |
| 2111 | page_count = get_write_extents(osc, &rpclist); |
| 2112 | LASSERT(equi(page_count == 0, list_empty(&rpclist))); |
| 2113 | |
| 2114 | if (list_empty(&rpclist)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2115 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2116 | |
| 2117 | osc_update_pending(osc, OBD_BRW_WRITE, -page_count); |
| 2118 | |
| 2119 | list_for_each_entry(ext, &rpclist, oe_link) { |
| 2120 | LASSERT(ext->oe_state == OES_CACHE || |
| 2121 | ext->oe_state == OES_LOCK_DONE); |
| 2122 | if (ext->oe_state == OES_CACHE) |
| 2123 | osc_extent_state_set(ext, OES_LOCKING); |
| 2124 | else |
| 2125 | osc_extent_state_set(ext, OES_RPC); |
| 2126 | } |
| 2127 | |
| 2128 | /* we're going to grab page lock, so release object lock because |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2129 | * lock order is page lock -> object lock. |
| 2130 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2131 | osc_object_unlock(osc); |
| 2132 | |
| 2133 | list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) { |
| 2134 | if (ext->oe_state == OES_LOCKING) { |
| 2135 | rc = osc_extent_make_ready(env, ext); |
| 2136 | if (unlikely(rc < 0)) { |
| 2137 | list_del_init(&ext->oe_link); |
| 2138 | osc_extent_finish(env, ext, 0, rc); |
| 2139 | continue; |
| 2140 | } |
| 2141 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2142 | if (!first) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2143 | first = ext; |
| 2144 | srvlock = ext->oe_srvlock; |
| 2145 | } else { |
| 2146 | LASSERT(srvlock == ext->oe_srvlock); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | if (!list_empty(&rpclist)) { |
| 2151 | LASSERT(page_count > 0); |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2152 | rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2153 | LASSERT(list_empty(&rpclist)); |
| 2154 | } |
| 2155 | |
| 2156 | osc_object_lock(osc); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2157 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /** |
| 2161 | * prepare pages for ASYNC io and put pages in send queue. |
| 2162 | * |
| 2163 | * \param cmd OBD_BRW_* macroses |
| 2164 | * \param lop pending pages |
| 2165 | * |
| 2166 | * \return zero if no page added to send queue. |
| 2167 | * \return 1 if pages successfully added to send queue. |
| 2168 | * \return negative on errors. |
| 2169 | */ |
| 2170 | static int |
| 2171 | osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli, |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2172 | struct osc_object *osc) |
frank zago | a161de8 | 2015-12-23 12:32:11 -0500 | [diff] [blame] | 2173 | __must_hold(osc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2174 | { |
| 2175 | struct osc_extent *ext; |
| 2176 | struct osc_extent *next; |
| 2177 | LIST_HEAD(rpclist); |
| 2178 | int page_count = 0; |
| 2179 | unsigned int max_pages = cli->cl_max_pages_per_rpc; |
| 2180 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2181 | |
| 2182 | LASSERT(osc_object_is_locked(osc)); |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 2183 | list_for_each_entry_safe(ext, next, &osc->oo_reading_exts, oe_link) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2184 | EASSERT(ext->oe_state == OES_LOCK_DONE, ext); |
| 2185 | if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count, |
| 2186 | &max_pages)) |
| 2187 | break; |
| 2188 | osc_extent_state_set(ext, OES_RPC); |
| 2189 | EASSERT(ext->oe_nr_pages <= max_pages, ext); |
| 2190 | } |
| 2191 | LASSERT(page_count <= max_pages); |
| 2192 | |
| 2193 | osc_update_pending(osc, OBD_BRW_READ, -page_count); |
| 2194 | |
| 2195 | if (!list_empty(&rpclist)) { |
| 2196 | osc_object_unlock(osc); |
| 2197 | |
| 2198 | LASSERT(page_count > 0); |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2199 | rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2200 | LASSERT(list_empty(&rpclist)); |
| 2201 | |
| 2202 | osc_object_lock(osc); |
| 2203 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2204 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | #define list_to_obj(list, item) ({ \ |
| 2208 | struct list_head *__tmp = (list)->next; \ |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2209 | list_del_init(__tmp); \ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2210 | list_entry(__tmp, struct osc_object, oo_##item); \ |
| 2211 | }) |
| 2212 | |
| 2213 | /* This is called by osc_check_rpcs() to find which objects have pages that |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2214 | * we could be sending. These lists are maintained by osc_makes_rpc(). |
| 2215 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2216 | static struct osc_object *osc_next_obj(struct client_obd *cli) |
| 2217 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2218 | /* First return objects that have blocked locks so that they |
| 2219 | * will be flushed quickly and other clients can get the lock, |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2220 | * then objects which have pages ready to be stuffed into RPCs |
| 2221 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2222 | if (!list_empty(&cli->cl_loi_hp_ready_list)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2223 | return list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2224 | if (!list_empty(&cli->cl_loi_ready_list)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2225 | return list_to_obj(&cli->cl_loi_ready_list, ready_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2226 | |
| 2227 | /* then if we have cache waiters, return all objects with queued |
| 2228 | * writes. This is especially important when many small files |
| 2229 | * have filled up the cache and not been fired into rpcs because |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2230 | * they don't pass the nr_pending/object threshold |
| 2231 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2232 | if (!list_empty(&cli->cl_cache_waiters) && |
| 2233 | !list_empty(&cli->cl_loi_write_list)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2234 | return list_to_obj(&cli->cl_loi_write_list, write_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2235 | |
| 2236 | /* then return all queued objects when we have an invalid import |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2237 | * so that they get flushed |
| 2238 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2239 | if (!cli->cl_import || cli->cl_import->imp_invalid) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2240 | if (!list_empty(&cli->cl_loi_write_list)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2241 | return list_to_obj(&cli->cl_loi_write_list, write_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2242 | if (!list_empty(&cli->cl_loi_read_list)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2243 | return list_to_obj(&cli->cl_loi_read_list, read_item); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2244 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2245 | return NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* called with the loi list lock held */ |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2249 | static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli) |
frank zago | a161de8 | 2015-12-23 12:32:11 -0500 | [diff] [blame] | 2250 | __must_hold(&cli->cl_loi_list_lock) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2251 | { |
| 2252 | struct osc_object *osc; |
| 2253 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2254 | |
| 2255 | while ((osc = osc_next_obj(cli)) != NULL) { |
| 2256 | struct cl_object *obj = osc2cl(osc); |
John L. Hammond | 631abc6 | 2013-07-25 01:17:30 +0800 | [diff] [blame] | 2257 | struct lu_ref_link link; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2258 | |
| 2259 | OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli)); |
| 2260 | |
| 2261 | if (osc_max_rpc_in_flight(cli, osc)) { |
| 2262 | __osc_list_maint(cli, osc); |
| 2263 | break; |
| 2264 | } |
| 2265 | |
| 2266 | cl_object_get(obj); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2267 | spin_unlock(&cli->cl_loi_list_lock); |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 2268 | lu_object_ref_add_at(&obj->co_lu, &link, "check", current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2269 | |
| 2270 | /* attempt some read/write balancing by alternating between |
| 2271 | * reads and writes in an object. The makes_rpc checks here |
| 2272 | * would be redundant if we were getting read/write work items |
| 2273 | * instead of objects. we don't want send_oap_rpc to drain a |
| 2274 | * partial read pending queue when we're given this object to |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2275 | * do io on writes while there are cache waiters |
| 2276 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2277 | osc_object_lock(osc); |
| 2278 | if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) { |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2279 | rc = osc_send_write_rpc(env, cli, osc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2280 | if (rc < 0) { |
| 2281 | CERROR("Write request failed with %d\n", rc); |
| 2282 | |
| 2283 | /* osc_send_write_rpc failed, mostly because of |
| 2284 | * memory pressure. |
| 2285 | * |
| 2286 | * It can't break here, because if: |
| 2287 | * - a page was submitted by osc_io_submit, so |
| 2288 | * page locked; |
| 2289 | * - no request in flight |
| 2290 | * - no subsequent request |
| 2291 | * The system will be in live-lock state, |
| 2292 | * because there is no chance to call |
| 2293 | * osc_io_unplug() and osc_check_rpcs() any |
| 2294 | * more. pdflush can't help in this case, |
| 2295 | * because it might be blocked at grabbing |
| 2296 | * the page lock as we mentioned. |
| 2297 | * |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2298 | * Anyway, continue to drain pages. |
| 2299 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2300 | /* break; */ |
| 2301 | } |
| 2302 | } |
| 2303 | if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) { |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2304 | rc = osc_send_read_rpc(env, cli, osc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2305 | if (rc < 0) |
| 2306 | CERROR("Read request failed with %d\n", rc); |
| 2307 | } |
| 2308 | osc_object_unlock(osc); |
| 2309 | |
| 2310 | osc_list_maint(cli, osc); |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 2311 | lu_object_ref_del_at(&obj->co_lu, &link, "check", current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2312 | cl_object_put(env, obj); |
| 2313 | |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2314 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli, |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2319 | struct osc_object *osc, int async) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2320 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2321 | int rc = 0; |
| 2322 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2323 | if (osc && osc_list_maint(cli, osc) == 0) |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 2324 | return 0; |
| 2325 | |
| 2326 | if (!async) { |
| 2327 | /* disable osc_lru_shrink() temporarily to avoid |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2328 | * potential stack overrun problem. LU-2859 |
| 2329 | */ |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 2330 | atomic_inc(&cli->cl_lru_shrinkers); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2331 | spin_lock(&cli->cl_loi_list_lock); |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2332 | osc_check_rpcs(env, cli); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2333 | spin_unlock(&cli->cl_loi_list_lock); |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 2334 | atomic_dec(&cli->cl_lru_shrinkers); |
| 2335 | } else { |
| 2336 | CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2337 | LASSERT(cli->cl_writeback_work); |
Bobi Jam | cad6faf | 2013-06-03 21:40:45 +0800 | [diff] [blame] | 2338 | rc = ptlrpcd_queue_work(cli->cl_writeback_work); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2339 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2340 | return rc; |
| 2341 | } |
| 2342 | |
| 2343 | static int osc_io_unplug_async(const struct lu_env *env, |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2344 | struct client_obd *cli, struct osc_object *osc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2345 | { |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2346 | return osc_io_unplug0(env, cli, osc, 1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | void osc_io_unplug(const struct lu_env *env, struct client_obd *cli, |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2350 | struct osc_object *osc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2351 | { |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 2352 | (void)osc_io_unplug0(env, cli, osc, 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops, |
| 2356 | struct page *page, loff_t offset) |
| 2357 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2358 | struct obd_export *exp = osc_export(osc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2359 | struct osc_async_page *oap = &ops->ops_oap; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2360 | |
| 2361 | if (!page) |
| 2362 | return cfs_size_round(sizeof(*oap)); |
| 2363 | |
| 2364 | oap->oap_magic = OAP_MAGIC; |
| 2365 | oap->oap_cli = &exp->exp_obd->u.cli; |
| 2366 | oap->oap_obj = osc; |
| 2367 | |
| 2368 | oap->oap_page = page; |
| 2369 | oap->oap_obj_off = offset; |
Oleg Drokin | 616387e | 2016-03-30 19:48:23 -0400 | [diff] [blame] | 2370 | LASSERT(!(offset & ~PAGE_MASK)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2371 | |
Fan Yong | 341f1f0 | 2016-06-19 22:53:53 -0400 | [diff] [blame] | 2372 | if (capable(CFS_CAP_SYS_RESOURCE)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2373 | oap->oap_brw_flags = OBD_BRW_NOQUOTA; |
| 2374 | |
| 2375 | INIT_LIST_HEAD(&oap->oap_pending_item); |
| 2376 | INIT_LIST_HEAD(&oap->oap_rpc_item); |
| 2377 | |
| 2378 | spin_lock_init(&oap->oap_lock); |
Greg Kroah-Hartman | b0f5aad | 2014-07-12 20:06:04 -0700 | [diff] [blame] | 2379 | CDEBUG(D_INFO, "oap %p page %p obj off %llu\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2380 | oap, page, oap->oap_obj_off); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2381 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | int osc_queue_async_io(const struct lu_env *env, struct cl_io *io, |
| 2385 | struct osc_page *ops) |
| 2386 | { |
| 2387 | struct osc_io *oio = osc_env_io(env); |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2388 | struct osc_extent *ext = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2389 | struct osc_async_page *oap = &ops->ops_oap; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2390 | struct client_obd *cli = oap->oap_cli; |
| 2391 | struct osc_object *osc = oap->oap_obj; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2392 | pgoff_t index; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2393 | int grants = 0; |
| 2394 | int brw_flags = OBD_BRW_ASYNC; |
| 2395 | int cmd = OBD_BRW_WRITE; |
| 2396 | int need_release = 0; |
| 2397 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2398 | |
| 2399 | if (oap->oap_magic != OAP_MAGIC) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2400 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2401 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2402 | if (!cli->cl_import || cli->cl_import->imp_invalid) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2403 | return -EIO; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2404 | |
| 2405 | if (!list_empty(&oap->oap_pending_item) || |
| 2406 | !list_empty(&oap->oap_rpc_item)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2407 | return -EBUSY; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2408 | |
| 2409 | /* Set the OBD_BRW_SRVLOCK before the page is queued. */ |
| 2410 | brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0; |
Fan Yong | 341f1f0 | 2016-06-19 22:53:53 -0400 | [diff] [blame] | 2411 | if (capable(CFS_CAP_SYS_RESOURCE)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2412 | brw_flags |= OBD_BRW_NOQUOTA; |
| 2413 | cmd |= OBD_BRW_NOQUOTA; |
| 2414 | } |
| 2415 | |
| 2416 | /* check if the file's owner/group is over quota */ |
| 2417 | if (!(cmd & OBD_BRW_NOQUOTA)) { |
| 2418 | struct cl_object *obj; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2419 | struct cl_attr *attr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2420 | unsigned int qid[MAXQUOTAS]; |
| 2421 | |
| 2422 | obj = cl_object_top(&osc->oo_cl); |
| 2423 | attr = &osc_env_info(env)->oti_attr; |
| 2424 | |
| 2425 | cl_object_attr_lock(obj); |
| 2426 | rc = cl_object_attr_get(env, obj, attr); |
| 2427 | cl_object_attr_unlock(obj); |
| 2428 | |
| 2429 | qid[USRQUOTA] = attr->cat_uid; |
| 2430 | qid[GRPQUOTA] = attr->cat_gid; |
| 2431 | if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA) |
| 2432 | rc = -EDQUOT; |
| 2433 | if (rc) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2434 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2435 | } |
| 2436 | |
Prakash Surya | ad47928 | 2016-04-27 18:21:06 -0400 | [diff] [blame] | 2437 | if (osc_over_unstable_soft_limit(cli)) |
| 2438 | brw_flags |= OBD_BRW_SOFT_SYNC; |
| 2439 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2440 | oap->oap_cmd = cmd; |
| 2441 | oap->oap_page_off = ops->ops_from; |
| 2442 | oap->oap_count = ops->ops_to - ops->ops_from; |
Sebastien Buisson | 82281bc | 2016-04-12 16:14:01 -0400 | [diff] [blame] | 2443 | /* |
| 2444 | * No need to hold a lock here, |
| 2445 | * since this page is not in any list yet. |
| 2446 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2447 | oap->oap_async_flags = 0; |
| 2448 | oap->oap_brw_flags = brw_flags; |
| 2449 | |
| 2450 | OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n", |
| 2451 | oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK); |
| 2452 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2453 | index = osc_index(oap2osc(oap)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2454 | |
| 2455 | /* Add this page into extent by the following steps: |
| 2456 | * 1. if there exists an active extent for this IO, mostly this page |
| 2457 | * can be added to the active extent and sometimes we need to |
Masanari Iida | 11d66e8 | 2013-12-14 02:24:04 +0900 | [diff] [blame] | 2458 | * expand extent to accommodate this page; |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2459 | * 2. otherwise, a new extent will be allocated. |
| 2460 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2461 | |
| 2462 | ext = oio->oi_active; |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2463 | if (ext && ext->oe_start <= index && ext->oe_max_end >= index) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2464 | /* one chunk plus extent overhead must be enough to write this |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2465 | * page |
| 2466 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2467 | grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax; |
| 2468 | if (ext->oe_end >= index) |
| 2469 | grants = 0; |
| 2470 | |
| 2471 | /* it doesn't need any grant to dirty this page */ |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2472 | spin_lock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2473 | rc = osc_enter_cache_try(cli, oap, grants, 0); |
John L. Hammond | 7d53d8f | 2016-03-30 19:48:36 -0400 | [diff] [blame] | 2474 | spin_unlock(&cli->cl_loi_list_lock); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2475 | if (rc == 0) { /* try failed */ |
| 2476 | grants = 0; |
| 2477 | need_release = 1; |
| 2478 | } else if (ext->oe_end < index) { |
| 2479 | int tmp = grants; |
| 2480 | /* try to expand this extent */ |
| 2481 | rc = osc_extent_expand(ext, index, &tmp); |
| 2482 | if (rc < 0) { |
| 2483 | need_release = 1; |
| 2484 | /* don't free reserved grant */ |
| 2485 | } else { |
| 2486 | OSC_EXTENT_DUMP(D_CACHE, ext, |
| 2487 | "expanded for %lu.\n", index); |
| 2488 | osc_unreserve_grant(cli, grants, tmp); |
| 2489 | grants = 0; |
| 2490 | } |
| 2491 | } |
| 2492 | rc = 0; |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2493 | } else if (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2494 | /* index is located outside of active extent */ |
| 2495 | need_release = 1; |
| 2496 | } |
| 2497 | if (need_release) { |
| 2498 | osc_extent_release(env, ext); |
| 2499 | oio->oi_active = NULL; |
| 2500 | ext = NULL; |
| 2501 | } |
| 2502 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2503 | if (!ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2504 | int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax; |
| 2505 | |
| 2506 | /* try to find new extent to cover this page */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2507 | LASSERT(!oio->oi_active); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2508 | /* we may have allocated grant for this page if we failed |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2509 | * to expand the previous active extent. |
| 2510 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2511 | LASSERT(ergo(grants > 0, grants >= tmp)); |
| 2512 | |
| 2513 | rc = 0; |
| 2514 | if (grants == 0) { |
| 2515 | /* we haven't allocated grant for this page. */ |
| 2516 | rc = osc_enter_cache(env, cli, oap, tmp); |
| 2517 | if (rc == 0) |
| 2518 | grants = tmp; |
| 2519 | } |
| 2520 | |
| 2521 | tmp = grants; |
| 2522 | if (rc == 0) { |
| 2523 | ext = osc_extent_find(env, osc, index, &tmp); |
| 2524 | if (IS_ERR(ext)) { |
| 2525 | LASSERT(tmp == grants); |
| 2526 | osc_exit_cache(cli, oap); |
| 2527 | rc = PTR_ERR(ext); |
| 2528 | ext = NULL; |
| 2529 | } else { |
| 2530 | oio->oi_active = ext; |
| 2531 | } |
| 2532 | } |
| 2533 | if (grants > 0) |
| 2534 | osc_unreserve_grant(cli, grants, tmp); |
| 2535 | } |
| 2536 | |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2537 | LASSERT(ergo(rc == 0, ext)); |
| 2538 | if (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2539 | EASSERTF(ext->oe_end >= index && ext->oe_start <= index, |
| 2540 | ext, "index = %lu.\n", index); |
| 2541 | LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0); |
| 2542 | |
| 2543 | osc_object_lock(osc); |
| 2544 | if (ext->oe_nr_pages == 0) |
| 2545 | ext->oe_srvlock = ops->ops_srvlock; |
| 2546 | else |
| 2547 | LASSERT(ext->oe_srvlock == ops->ops_srvlock); |
| 2548 | ++ext->oe_nr_pages; |
| 2549 | list_add_tail(&oap->oap_pending_item, &ext->oe_pages); |
| 2550 | osc_object_unlock(osc); |
| 2551 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2552 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | int osc_teardown_async_page(const struct lu_env *env, |
| 2556 | struct osc_object *obj, struct osc_page *ops) |
| 2557 | { |
| 2558 | struct osc_async_page *oap = &ops->ops_oap; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2559 | struct osc_extent *ext = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2560 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2561 | |
| 2562 | LASSERT(oap->oap_magic == OAP_MAGIC); |
| 2563 | |
| 2564 | CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n", |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2565 | oap, ops, osc_index(oap2osc(oap))); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2566 | |
| 2567 | osc_object_lock(obj); |
| 2568 | if (!list_empty(&oap->oap_rpc_item)) { |
| 2569 | CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap); |
| 2570 | rc = -EBUSY; |
| 2571 | } else if (!list_empty(&oap->oap_pending_item)) { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2572 | ext = osc_extent_lookup(obj, osc_index(oap2osc(oap))); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2573 | /* only truncated pages are allowed to be taken out. |
| 2574 | * See osc_extent_truncate() and osc_cache_truncate_start() |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2575 | * for details. |
| 2576 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2577 | if (ext && ext->oe_state != OES_TRUNC) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2578 | OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n", |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2579 | osc_index(oap2osc(oap))); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2580 | rc = -EBUSY; |
| 2581 | } |
| 2582 | } |
| 2583 | osc_object_unlock(obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2584 | if (ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2585 | osc_extent_put(env, ext); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2586 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /** |
| 2590 | * This is called when a page is picked up by kernel to write out. |
| 2591 | * |
| 2592 | * We should find out the corresponding extent and add the whole extent |
| 2593 | * into urgent list. The extent may be being truncated or used, handle it |
| 2594 | * carefully. |
| 2595 | */ |
| 2596 | int osc_flush_async_page(const struct lu_env *env, struct cl_io *io, |
| 2597 | struct osc_page *ops) |
| 2598 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2599 | struct osc_extent *ext = NULL; |
| 2600 | struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj); |
| 2601 | struct cl_page *cp = ops->ops_cl.cpl_page; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2602 | pgoff_t index = osc_index(ops); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2603 | struct osc_async_page *oap = &ops->ops_oap; |
| 2604 | bool unplug = false; |
| 2605 | int rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2606 | |
| 2607 | osc_object_lock(obj); |
| 2608 | ext = osc_extent_lookup(obj, index); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2609 | if (!ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2610 | osc_extent_tree_dump(D_ERROR, obj); |
| 2611 | LASSERTF(0, "page index %lu is NOT covered.\n", index); |
| 2612 | } |
| 2613 | |
| 2614 | switch (ext->oe_state) { |
| 2615 | case OES_RPC: |
| 2616 | case OES_LOCK_DONE: |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2617 | CL_PAGE_DEBUG(D_ERROR, env, cp, "flush an in-rpc page?\n"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2618 | LASSERT(0); |
| 2619 | break; |
| 2620 | case OES_LOCKING: |
| 2621 | /* If we know this extent is being written out, we should abort |
| 2622 | * so that the writer can make this page ready. Otherwise, there |
| 2623 | * exists a deadlock problem because other process can wait for |
| 2624 | * page writeback bit holding page lock; and meanwhile in |
| 2625 | * vvp_page_make_ready(), we need to grab page lock before |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2626 | * really sending the RPC. |
| 2627 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2628 | case OES_TRUNC: |
| 2629 | /* race with truncate, page will be redirtied */ |
Ann Koehler | 15f13cd | 2014-02-28 21:16:44 -0500 | [diff] [blame] | 2630 | case OES_ACTIVE: |
| 2631 | /* The extent is active so we need to abort and let the caller |
| 2632 | * re-dirty the page. If we continued on here, and we were the |
| 2633 | * one making the extent active, we could deadlock waiting for |
| 2634 | * the page writeback to clear but it won't because the extent |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2635 | * is active and won't be written out. |
| 2636 | */ |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 2637 | rc = -EAGAIN; |
| 2638 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2639 | default: |
| 2640 | break; |
| 2641 | } |
| 2642 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2643 | rc = cl_page_prep(env, io, cp, CRT_WRITE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2644 | if (rc) |
Julia Lawall | 490e0e8 | 2014-11-09 10:06:32 +0100 | [diff] [blame] | 2645 | goto out; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2646 | |
| 2647 | spin_lock(&oap->oap_lock); |
| 2648 | oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT; |
| 2649 | spin_unlock(&oap->oap_lock); |
| 2650 | |
| 2651 | if (memory_pressure_get()) |
| 2652 | ext->oe_memalloc = 1; |
| 2653 | |
| 2654 | ext->oe_urgent = 1; |
| 2655 | if (ext->oe_state == OES_CACHE) { |
| 2656 | OSC_EXTENT_DUMP(D_CACHE, ext, |
| 2657 | "flush page %p make it urgent.\n", oap); |
| 2658 | if (list_empty(&ext->oe_link)) |
| 2659 | list_add_tail(&ext->oe_link, &obj->oo_urgent_exts); |
| 2660 | unplug = true; |
| 2661 | } |
| 2662 | rc = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2663 | |
| 2664 | out: |
| 2665 | osc_object_unlock(obj); |
| 2666 | osc_extent_put(env, ext); |
| 2667 | if (unplug) |
| 2668 | osc_io_unplug_async(env, osc_cli(obj), obj); |
| 2669 | return rc; |
| 2670 | } |
| 2671 | |
| 2672 | /** |
| 2673 | * this is called when a sync waiter receives an interruption. Its job is to |
| 2674 | * get the caller woken as soon as possible. If its page hasn't been put in an |
| 2675 | * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as |
| 2676 | * desiring interruption which will forcefully complete the rpc once the rpc |
| 2677 | * has timed out. |
| 2678 | */ |
| 2679 | int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops) |
| 2680 | { |
| 2681 | struct osc_async_page *oap = &ops->ops_oap; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2682 | struct osc_object *obj = oap->oap_obj; |
| 2683 | struct client_obd *cli = osc_cli(obj); |
| 2684 | struct osc_extent *ext; |
| 2685 | struct osc_extent *found = NULL; |
| 2686 | struct list_head *plist; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2687 | pgoff_t index = osc_index(ops); |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2688 | int rc = -EBUSY; |
| 2689 | int cmd; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2690 | |
| 2691 | LASSERT(!oap->oap_interrupted); |
| 2692 | oap->oap_interrupted = 1; |
| 2693 | |
| 2694 | /* Find out the caching extent */ |
| 2695 | osc_object_lock(obj); |
| 2696 | if (oap->oap_cmd & OBD_BRW_WRITE) { |
| 2697 | plist = &obj->oo_urgent_exts; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2698 | cmd = OBD_BRW_WRITE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2699 | } else { |
| 2700 | plist = &obj->oo_reading_exts; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2701 | cmd = OBD_BRW_READ; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2702 | } |
| 2703 | list_for_each_entry(ext, plist, oe_link) { |
| 2704 | if (ext->oe_start <= index && ext->oe_end >= index) { |
| 2705 | LASSERT(ext->oe_state == OES_LOCK_DONE); |
| 2706 | /* For OES_LOCK_DONE state extent, it has already held |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2707 | * a refcount for RPC. |
| 2708 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2709 | found = osc_extent_get(ext); |
| 2710 | break; |
| 2711 | } |
| 2712 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2713 | if (found) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2714 | list_del_init(&found->oe_link); |
| 2715 | osc_update_pending(obj, cmd, -found->oe_nr_pages); |
| 2716 | osc_object_unlock(obj); |
| 2717 | |
| 2718 | osc_extent_finish(env, found, 0, -EINTR); |
| 2719 | osc_extent_put(env, found); |
| 2720 | rc = 0; |
| 2721 | } else { |
| 2722 | osc_object_unlock(obj); |
| 2723 | /* ok, it's been put in an rpc. only one oap gets a request |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2724 | * reference |
| 2725 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2726 | if (oap->oap_request) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2727 | ptlrpc_mark_interrupted(oap->oap_request); |
| 2728 | ptlrpcd_wake(oap->oap_request); |
| 2729 | ptlrpc_req_finished(oap->oap_request); |
| 2730 | oap->oap_request = NULL; |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | osc_list_maint(cli, obj); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2735 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj, |
| 2739 | struct list_head *list, int cmd, int brw_flags) |
| 2740 | { |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2741 | struct client_obd *cli = osc_cli(obj); |
| 2742 | struct osc_extent *ext; |
Wei Yongjun | f13ab92 | 2013-05-17 16:27:05 +0800 | [diff] [blame] | 2743 | struct osc_async_page *oap, *tmp; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2744 | int page_count = 0; |
| 2745 | int mppr = cli->cl_max_pages_per_rpc; |
| 2746 | pgoff_t start = CL_PAGE_EOF; |
| 2747 | pgoff_t end = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2748 | |
| 2749 | list_for_each_entry(oap, list, oap_pending_item) { |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2750 | pgoff_t index = osc_index(oap2osc(oap)); |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 2751 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 2752 | if (index > end) |
| 2753 | end = index; |
| 2754 | if (index < start) |
| 2755 | start = index; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2756 | ++page_count; |
| 2757 | mppr <<= (page_count > mppr); |
| 2758 | } |
| 2759 | |
| 2760 | ext = osc_extent_alloc(obj); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2761 | if (!ext) { |
Wei Yongjun | f13ab92 | 2013-05-17 16:27:05 +0800 | [diff] [blame] | 2762 | list_for_each_entry_safe(oap, tmp, list, oap_pending_item) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2763 | list_del_init(&oap->oap_pending_item); |
| 2764 | osc_ap_completion(env, cli, oap, 0, -ENOMEM); |
| 2765 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2766 | return -ENOMEM; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | ext->oe_rw = !!(cmd & OBD_BRW_READ); |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 2770 | ext->oe_sync = 1; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2771 | ext->oe_urgent = 1; |
| 2772 | ext->oe_start = start; |
Nathaniel Clark | 04a6284 | 2016-06-09 22:35:11 -0400 | [diff] [blame] | 2773 | ext->oe_end = end; |
| 2774 | ext->oe_max_end = end; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2775 | ext->oe_obj = obj; |
| 2776 | ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK); |
| 2777 | ext->oe_nr_pages = page_count; |
| 2778 | ext->oe_mppr = mppr; |
| 2779 | list_splice_init(list, &ext->oe_pages); |
| 2780 | |
| 2781 | osc_object_lock(obj); |
| 2782 | /* Reuse the initial refcount for RPC, don't drop it */ |
| 2783 | osc_extent_state_set(ext, OES_LOCK_DONE); |
| 2784 | if (cmd & OBD_BRW_WRITE) { |
| 2785 | list_add_tail(&ext->oe_link, &obj->oo_urgent_exts); |
| 2786 | osc_update_pending(obj, OBD_BRW_WRITE, page_count); |
| 2787 | } else { |
| 2788 | list_add_tail(&ext->oe_link, &obj->oo_reading_exts); |
| 2789 | osc_update_pending(obj, OBD_BRW_READ, page_count); |
| 2790 | } |
| 2791 | osc_object_unlock(obj); |
| 2792 | |
Bobi Jam | c61ac97 | 2015-03-25 22:04:53 -0400 | [diff] [blame] | 2793 | osc_io_unplug_async(env, cli, obj); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2794 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | /** |
| 2798 | * Called by osc_io_setattr_start() to freeze and destroy covering extents. |
| 2799 | */ |
| 2800 | int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio, |
| 2801 | struct osc_object *obj, __u64 size) |
| 2802 | { |
| 2803 | struct client_obd *cli = osc_cli(obj); |
| 2804 | struct osc_extent *ext; |
Bhaktipriya Shridhar | 4a81ce5 | 2016-03-12 01:37:35 +0530 | [diff] [blame] | 2805 | struct osc_extent *temp; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2806 | struct osc_extent *waiting = NULL; |
| 2807 | pgoff_t index; |
| 2808 | LIST_HEAD(list); |
| 2809 | int result = 0; |
| 2810 | bool partial; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2811 | |
| 2812 | /* pages with index greater or equal to index will be truncated. */ |
| 2813 | index = cl_index(osc2cl(obj), size); |
| 2814 | partial = size > cl_offset(osc2cl(obj), index); |
| 2815 | |
| 2816 | again: |
| 2817 | osc_object_lock(obj); |
| 2818 | ext = osc_extent_search(obj, index); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2819 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2820 | ext = first_extent(obj); |
| 2821 | else if (ext->oe_end < index) |
| 2822 | ext = next_extent(ext); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2823 | while (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2824 | EASSERT(ext->oe_state != OES_TRUNC, ext); |
| 2825 | |
| 2826 | if (ext->oe_state > OES_CACHE || ext->oe_urgent) { |
| 2827 | /* if ext is in urgent state, it means there must exist |
| 2828 | * a page already having been flushed by write_page(). |
| 2829 | * We have to wait for this extent because we can't |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2830 | * truncate that page. |
| 2831 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2832 | LASSERT(!ext->oe_hp); |
| 2833 | OSC_EXTENT_DUMP(D_CACHE, ext, |
| 2834 | "waiting for busy extent\n"); |
| 2835 | waiting = osc_extent_get(ext); |
| 2836 | break; |
| 2837 | } |
| 2838 | |
Greg Kroah-Hartman | b0f5aad | 2014-07-12 20:06:04 -0700 | [diff] [blame] | 2839 | OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:%llu.\n", size); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2840 | |
| 2841 | osc_extent_get(ext); |
| 2842 | if (ext->oe_state == OES_ACTIVE) { |
| 2843 | /* though we grab inode mutex for write path, but we |
| 2844 | * release it before releasing extent(in osc_io_end()), |
| 2845 | * so there is a race window that an extent is still |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2846 | * in OES_ACTIVE when truncate starts. |
| 2847 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2848 | LASSERT(!ext->oe_trunc_pending); |
| 2849 | ext->oe_trunc_pending = 1; |
| 2850 | } else { |
| 2851 | EASSERT(ext->oe_state == OES_CACHE, ext); |
| 2852 | osc_extent_state_set(ext, OES_TRUNC); |
| 2853 | osc_update_pending(obj, OBD_BRW_WRITE, |
| 2854 | -ext->oe_nr_pages); |
| 2855 | } |
| 2856 | EASSERT(list_empty(&ext->oe_link), ext); |
| 2857 | list_add_tail(&ext->oe_link, &list); |
| 2858 | |
| 2859 | ext = next_extent(ext); |
| 2860 | } |
| 2861 | osc_object_unlock(obj); |
| 2862 | |
| 2863 | osc_list_maint(cli, obj); |
| 2864 | |
Bhaktipriya Shridhar | 4a81ce5 | 2016-03-12 01:37:35 +0530 | [diff] [blame] | 2865 | list_for_each_entry_safe(ext, temp, &list, oe_link) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2866 | int rc; |
| 2867 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2868 | list_del_init(&ext->oe_link); |
| 2869 | |
| 2870 | /* extent may be in OES_ACTIVE state because inode mutex |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2871 | * is released before osc_io_end() in file write case |
| 2872 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2873 | if (ext->oe_state != OES_TRUNC) |
| 2874 | osc_extent_wait(env, ext, OES_TRUNC); |
| 2875 | |
| 2876 | rc = osc_extent_truncate(ext, index, partial); |
| 2877 | if (rc < 0) { |
| 2878 | if (result == 0) |
| 2879 | result = rc; |
| 2880 | |
| 2881 | OSC_EXTENT_DUMP(D_ERROR, ext, |
| 2882 | "truncate error %d\n", rc); |
| 2883 | } else if (ext->oe_nr_pages == 0) { |
| 2884 | osc_extent_remove(ext); |
| 2885 | } else { |
| 2886 | /* this must be an overlapped extent which means only |
| 2887 | * part of pages in this extent have been truncated. |
| 2888 | */ |
| 2889 | EASSERTF(ext->oe_start <= index, ext, |
| 2890 | "trunc index = %lu/%d.\n", index, partial); |
| 2891 | /* fix index to skip this partially truncated extent */ |
| 2892 | index = ext->oe_end + 1; |
| 2893 | partial = false; |
| 2894 | |
| 2895 | /* we need to hold this extent in OES_TRUNC state so |
| 2896 | * that no writeback will happen. This is to avoid |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2897 | * BUG 17397. |
| 2898 | */ |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2899 | LASSERT(!oio->oi_trunc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2900 | oio->oi_trunc = osc_extent_get(ext); |
| 2901 | OSC_EXTENT_DUMP(D_CACHE, ext, |
Greg Kroah-Hartman | b0f5aad | 2014-07-12 20:06:04 -0700 | [diff] [blame] | 2902 | "trunc at %llu\n", size); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2903 | } |
| 2904 | osc_extent_put(env, ext); |
| 2905 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2906 | if (waiting) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2907 | int rc; |
| 2908 | |
| 2909 | /* ignore the result of osc_extent_wait the write initiator |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 2910 | * should take care of it. |
| 2911 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2912 | rc = osc_extent_wait(env, waiting, OES_INV); |
| 2913 | if (rc < 0) |
Jinshan Xiong | 451721c | 2013-06-03 21:40:58 +0800 | [diff] [blame] | 2914 | OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2915 | |
| 2916 | osc_extent_put(env, waiting); |
| 2917 | waiting = NULL; |
| 2918 | goto again; |
| 2919 | } |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 2920 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | /** |
| 2924 | * Called after osc_io_setattr_end to add oio->oi_trunc back to cache. |
| 2925 | */ |
| 2926 | void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio, |
| 2927 | struct osc_object *obj) |
| 2928 | { |
| 2929 | struct osc_extent *ext = oio->oi_trunc; |
| 2930 | |
| 2931 | oio->oi_trunc = NULL; |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2932 | if (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2933 | bool unplug = false; |
| 2934 | |
| 2935 | EASSERT(ext->oe_nr_pages > 0, ext); |
| 2936 | EASSERT(ext->oe_state == OES_TRUNC, ext); |
| 2937 | EASSERT(!ext->oe_urgent, ext); |
| 2938 | |
| 2939 | OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n"); |
| 2940 | osc_object_lock(obj); |
| 2941 | osc_extent_state_set(ext, OES_CACHE); |
| 2942 | if (ext->oe_fsync_wait && !ext->oe_urgent) { |
| 2943 | ext->oe_urgent = 1; |
| 2944 | list_move_tail(&ext->oe_link, &obj->oo_urgent_exts); |
| 2945 | unplug = true; |
| 2946 | } |
| 2947 | osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages); |
| 2948 | osc_object_unlock(obj); |
| 2949 | osc_extent_put(env, ext); |
| 2950 | |
| 2951 | if (unplug) |
| 2952 | osc_io_unplug_async(env, osc_cli(obj), obj); |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | /** |
| 2957 | * Wait for extents in a specific range to be written out. |
| 2958 | * The caller must have called osc_cache_writeback_range() to issue IO |
| 2959 | * otherwise it will take a long time for this function to finish. |
| 2960 | * |
| 2961 | * Caller must hold inode_mutex , or cancel exclusive dlm lock so that |
| 2962 | * nobody else can dirty this range of file while we're waiting for |
| 2963 | * extents to be written. |
| 2964 | */ |
| 2965 | int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj, |
| 2966 | pgoff_t start, pgoff_t end) |
| 2967 | { |
| 2968 | struct osc_extent *ext; |
| 2969 | pgoff_t index = start; |
Chris Hanna | 29ac684 | 2015-06-03 10:23:42 -0400 | [diff] [blame] | 2970 | int result = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2971 | |
| 2972 | again: |
| 2973 | osc_object_lock(obj); |
| 2974 | ext = osc_extent_search(obj, index); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2975 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2976 | ext = first_extent(obj); |
| 2977 | else if (ext->oe_end < index) |
| 2978 | ext = next_extent(ext); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 2979 | while (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2980 | int rc; |
| 2981 | |
| 2982 | if (ext->oe_start > end) |
| 2983 | break; |
| 2984 | |
| 2985 | if (!ext->oe_fsync_wait) { |
| 2986 | ext = next_extent(ext); |
| 2987 | continue; |
| 2988 | } |
| 2989 | |
| 2990 | EASSERT(ergo(ext->oe_state == OES_CACHE, |
| 2991 | ext->oe_hp || ext->oe_urgent), ext); |
| 2992 | EASSERT(ergo(ext->oe_state == OES_ACTIVE, |
| 2993 | !ext->oe_hp && ext->oe_urgent), ext); |
| 2994 | |
| 2995 | index = ext->oe_end + 1; |
| 2996 | osc_extent_get(ext); |
| 2997 | osc_object_unlock(obj); |
| 2998 | |
| 2999 | rc = osc_extent_wait(env, ext, OES_INV); |
| 3000 | if (result == 0) |
| 3001 | result = rc; |
| 3002 | osc_extent_put(env, ext); |
| 3003 | goto again; |
| 3004 | } |
| 3005 | osc_object_unlock(obj); |
| 3006 | |
| 3007 | OSC_IO_DEBUG(obj, "sync file range.\n"); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3008 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | /** |
| 3012 | * Called to write out a range of osc object. |
| 3013 | * |
| 3014 | * @hp : should be set this is caused by lock cancel; |
| 3015 | * @discard: is set if dirty pages should be dropped - file will be deleted or |
| 3016 | * truncated, this implies there is no partially discarding extents. |
| 3017 | * |
| 3018 | * Return how many pages will be issued, or error code if error occurred. |
| 3019 | */ |
| 3020 | int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj, |
| 3021 | pgoff_t start, pgoff_t end, int hp, int discard) |
| 3022 | { |
| 3023 | struct osc_extent *ext; |
| 3024 | LIST_HEAD(discard_list); |
| 3025 | bool unplug = false; |
| 3026 | int result = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3027 | |
| 3028 | osc_object_lock(obj); |
| 3029 | ext = osc_extent_search(obj, start); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 3030 | if (!ext) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3031 | ext = first_extent(obj); |
| 3032 | else if (ext->oe_end < start) |
| 3033 | ext = next_extent(ext); |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 3034 | while (ext) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3035 | if (ext->oe_start > end) |
| 3036 | break; |
| 3037 | |
| 3038 | ext->oe_fsync_wait = 1; |
| 3039 | switch (ext->oe_state) { |
| 3040 | case OES_CACHE: |
| 3041 | result += ext->oe_nr_pages; |
| 3042 | if (!discard) { |
| 3043 | struct list_head *list = NULL; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 3044 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3045 | if (hp) { |
| 3046 | EASSERT(!ext->oe_hp, ext); |
| 3047 | ext->oe_hp = 1; |
| 3048 | list = &obj->oo_hp_exts; |
| 3049 | } else if (!ext->oe_urgent) { |
| 3050 | ext->oe_urgent = 1; |
| 3051 | list = &obj->oo_urgent_exts; |
| 3052 | } |
Oleg Drokin | 7f1ae4c | 2016-02-16 00:46:57 -0500 | [diff] [blame] | 3053 | if (list) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3054 | list_move_tail(&ext->oe_link, list); |
| 3055 | unplug = true; |
| 3056 | } else { |
| 3057 | /* the only discarder is lock cancelling, so |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 3058 | * [start, end] must contain this extent |
| 3059 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3060 | EASSERT(ext->oe_start >= start && |
| 3061 | ext->oe_max_end <= end, ext); |
| 3062 | osc_extent_state_set(ext, OES_LOCKING); |
| 3063 | ext->oe_owner = current; |
Oleg Drokin | 79910d7 | 2016-02-26 01:50:03 -0500 | [diff] [blame] | 3064 | list_move_tail(&ext->oe_link, &discard_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3065 | osc_update_pending(obj, OBD_BRW_WRITE, |
| 3066 | -ext->oe_nr_pages); |
| 3067 | } |
| 3068 | break; |
| 3069 | case OES_ACTIVE: |
| 3070 | /* It's pretty bad to wait for ACTIVE extents, because |
| 3071 | * we don't know how long we will wait for it to be |
| 3072 | * flushed since it may be blocked at awaiting more |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 3073 | * grants. We do this for the correctness of fsync. |
| 3074 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3075 | LASSERT(hp == 0 && discard == 0); |
| 3076 | ext->oe_urgent = 1; |
| 3077 | break; |
| 3078 | case OES_TRUNC: |
| 3079 | /* this extent is being truncated, can't do anything |
| 3080 | * for it now. it will be set to urgent after truncate |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 3081 | * is finished in osc_cache_truncate_end(). |
| 3082 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3083 | default: |
| 3084 | break; |
| 3085 | } |
| 3086 | ext = next_extent(ext); |
| 3087 | } |
| 3088 | osc_object_unlock(obj); |
| 3089 | |
| 3090 | LASSERT(ergo(!discard, list_empty(&discard_list))); |
| 3091 | if (!list_empty(&discard_list)) { |
| 3092 | struct osc_extent *tmp; |
| 3093 | int rc; |
| 3094 | |
| 3095 | osc_list_maint(osc_cli(obj), obj); |
| 3096 | list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) { |
| 3097 | list_del_init(&ext->oe_link); |
| 3098 | EASSERT(ext->oe_state == OES_LOCKING, ext); |
| 3099 | |
| 3100 | /* Discard caching pages. We don't actually write this |
Oleg Drokin | 30aa9c5 | 2016-02-24 22:00:37 -0500 | [diff] [blame] | 3101 | * extent out but we complete it as if we did. |
| 3102 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3103 | rc = osc_extent_make_ready(env, ext); |
| 3104 | if (unlikely(rc < 0)) { |
| 3105 | OSC_EXTENT_DUMP(D_ERROR, ext, |
| 3106 | "make_ready returned %d\n", rc); |
| 3107 | if (result >= 0) |
| 3108 | result = rc; |
| 3109 | } |
| 3110 | |
| 3111 | /* finish the extent as if the pages were sent */ |
| 3112 | osc_extent_finish(env, ext, 0, 0); |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | if (unplug) |
Olaf Weber | c5c4c6f | 2015-09-14 18:41:35 -0400 | [diff] [blame] | 3117 | osc_io_unplug(env, osc_cli(obj), obj); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3118 | |
| 3119 | if (hp || discard) { |
| 3120 | int rc; |
Mike Rapoport | 50ffcb7 | 2015-10-13 16:03:40 +0300 | [diff] [blame] | 3121 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3122 | rc = osc_cache_wait_range(env, obj, start, end); |
| 3123 | if (result >= 0 && rc < 0) |
| 3124 | result = rc; |
| 3125 | } |
| 3126 | |
Jinshan Xiong | c355855 | 2016-04-27 18:20:53 -0400 | [diff] [blame] | 3127 | OSC_IO_DEBUG(obj, "pageout [%lu, %lu], %d.\n", start, end, result); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 3128 | return result; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3131 | /** |
| 3132 | * Returns a list of pages by a given [start, end] of \a obj. |
| 3133 | * |
| 3134 | * \param resched If not NULL, then we give up before hogging CPU for too |
| 3135 | * long and set *resched = 1, in that case caller should implement a retry |
| 3136 | * logic. |
| 3137 | * |
| 3138 | * Gang tree lookup (radix_tree_gang_lookup()) optimization is absolutely |
| 3139 | * crucial in the face of [offset, EOF] locks. |
| 3140 | * |
| 3141 | * Return at least one page in @queue unless there is no covered page. |
| 3142 | */ |
| 3143 | int osc_page_gang_lookup(const struct lu_env *env, struct cl_io *io, |
| 3144 | struct osc_object *osc, pgoff_t start, pgoff_t end, |
| 3145 | osc_page_gang_cbt cb, void *cbdata) |
| 3146 | { |
| 3147 | struct osc_page *ops; |
| 3148 | void **pvec; |
| 3149 | pgoff_t idx; |
| 3150 | unsigned int nr; |
| 3151 | unsigned int i; |
| 3152 | unsigned int j; |
| 3153 | int res = CLP_GANG_OKAY; |
| 3154 | bool tree_lock = true; |
| 3155 | |
| 3156 | idx = start; |
| 3157 | pvec = osc_env_info(env)->oti_pvec; |
| 3158 | spin_lock(&osc->oo_tree_lock); |
| 3159 | while ((nr = radix_tree_gang_lookup(&osc->oo_tree, pvec, |
| 3160 | idx, OTI_PVEC_SIZE)) > 0) { |
| 3161 | struct cl_page *page; |
| 3162 | bool end_of_region = false; |
| 3163 | |
| 3164 | for (i = 0, j = 0; i < nr; ++i) { |
| 3165 | ops = pvec[i]; |
| 3166 | pvec[i] = NULL; |
| 3167 | |
| 3168 | idx = osc_index(ops); |
| 3169 | if (idx > end) { |
| 3170 | end_of_region = true; |
| 3171 | break; |
| 3172 | } |
| 3173 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 3174 | page = ops->ops_cl.cpl_page; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3175 | LASSERT(page->cp_type == CPT_CACHEABLE); |
| 3176 | if (page->cp_state == CPS_FREEING) |
| 3177 | continue; |
| 3178 | |
| 3179 | cl_page_get(page); |
| 3180 | lu_ref_add_atomic(&page->cp_reference, |
| 3181 | "gang_lookup", current); |
| 3182 | pvec[j++] = ops; |
| 3183 | } |
| 3184 | ++idx; |
| 3185 | |
| 3186 | /* |
| 3187 | * Here a delicate locking dance is performed. Current thread |
| 3188 | * holds a reference to a page, but has to own it before it |
| 3189 | * can be placed into queue. Owning implies waiting, so |
| 3190 | * radix-tree lock is to be released. After a wait one has to |
| 3191 | * check that pages weren't truncated (cl_page_own() returns |
| 3192 | * error in the latter case). |
| 3193 | */ |
| 3194 | spin_unlock(&osc->oo_tree_lock); |
| 3195 | tree_lock = false; |
| 3196 | |
| 3197 | for (i = 0; i < j; ++i) { |
| 3198 | ops = pvec[i]; |
| 3199 | if (res == CLP_GANG_OKAY) |
| 3200 | res = (*cb)(env, io, ops, cbdata); |
| 3201 | |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 3202 | page = ops->ops_cl.cpl_page; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3203 | lu_ref_del(&page->cp_reference, "gang_lookup", current); |
| 3204 | cl_page_put(env, page); |
| 3205 | } |
| 3206 | if (nr < OTI_PVEC_SIZE || end_of_region) |
| 3207 | break; |
| 3208 | |
| 3209 | if (res == CLP_GANG_OKAY && need_resched()) |
| 3210 | res = CLP_GANG_RESCHED; |
| 3211 | if (res != CLP_GANG_OKAY) |
| 3212 | break; |
| 3213 | |
| 3214 | spin_lock(&osc->oo_tree_lock); |
| 3215 | tree_lock = true; |
| 3216 | } |
| 3217 | if (tree_lock) |
| 3218 | spin_unlock(&osc->oo_tree_lock); |
| 3219 | return res; |
| 3220 | } |
| 3221 | |
| 3222 | /** |
| 3223 | * Check if page @page is covered by an extra lock or discard it. |
| 3224 | */ |
| 3225 | static int check_and_discard_cb(const struct lu_env *env, struct cl_io *io, |
| 3226 | struct osc_page *ops, void *cbdata) |
| 3227 | { |
| 3228 | struct osc_thread_info *info = osc_env_info(env); |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3229 | struct osc_object *osc = cbdata; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3230 | pgoff_t index; |
| 3231 | |
| 3232 | index = osc_index(ops); |
| 3233 | if (index >= info->oti_fn_index) { |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3234 | struct ldlm_lock *tmp; |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 3235 | struct cl_page *page = ops->ops_cl.cpl_page; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3236 | |
| 3237 | /* refresh non-overlapped index */ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3238 | tmp = osc_dlmlock_at_pgoff(env, osc, index, 0, 0); |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3239 | if (tmp) { |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3240 | __u64 end = tmp->l_policy_data.l_extent.end; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3241 | /* Cache the first-non-overlapped index so as to skip |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3242 | * all pages within [index, oti_fn_index). This is safe |
| 3243 | * because if tmp lock is canceled, it will discard |
| 3244 | * these pages. |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3245 | */ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3246 | info->oti_fn_index = cl_index(osc2cl(osc), end + 1); |
| 3247 | if (end == OBD_OBJECT_EOF) |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3248 | info->oti_fn_index = CL_PAGE_EOF; |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3249 | LDLM_LOCK_PUT(tmp); |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3250 | } else if (cl_page_own(env, io, page) == 0) { |
| 3251 | /* discard the page */ |
| 3252 | cl_page_discard(env, io, page); |
| 3253 | cl_page_disown(env, io, page); |
| 3254 | } else { |
| 3255 | LASSERT(page->cp_state == CPS_FREEING); |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | info->oti_next_index = index + 1; |
| 3260 | return CLP_GANG_OKAY; |
| 3261 | } |
| 3262 | |
| 3263 | static int discard_cb(const struct lu_env *env, struct cl_io *io, |
| 3264 | struct osc_page *ops, void *cbdata) |
| 3265 | { |
| 3266 | struct osc_thread_info *info = osc_env_info(env); |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 3267 | struct cl_page *page = ops->ops_cl.cpl_page; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3268 | |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3269 | /* page is top page. */ |
| 3270 | info->oti_next_index = osc_index(ops) + 1; |
| 3271 | if (cl_page_own(env, io, page) == 0) { |
Jinshan Xiong | 77605e41 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 3272 | KLASSERT(ergo(page->cp_type == CPT_CACHEABLE, |
Jinshan Xiong | 7addf40 | 2016-03-30 19:48:32 -0400 | [diff] [blame] | 3273 | !PageDirty(cl_page_vmpage(page)))); |
Jinshan Xiong | 77605e41 | 2016-03-30 19:48:30 -0400 | [diff] [blame] | 3274 | |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3275 | /* discard the page */ |
| 3276 | cl_page_discard(env, io, page); |
| 3277 | cl_page_disown(env, io, page); |
| 3278 | } else { |
| 3279 | LASSERT(page->cp_state == CPS_FREEING); |
| 3280 | } |
| 3281 | |
| 3282 | return CLP_GANG_OKAY; |
| 3283 | } |
| 3284 | |
| 3285 | /** |
| 3286 | * Discard pages protected by the given lock. This function traverses radix |
| 3287 | * tree to find all covering pages and discard them. If a page is being covered |
| 3288 | * by other locks, it should remain in cache. |
| 3289 | * |
| 3290 | * If error happens on any step, the process continues anyway (the reasoning |
| 3291 | * behind this being that lock cancellation cannot be delayed indefinitely). |
| 3292 | */ |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3293 | int osc_lock_discard_pages(const struct lu_env *env, struct osc_object *osc, |
| 3294 | pgoff_t start, pgoff_t end, enum cl_lock_mode mode) |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3295 | { |
| 3296 | struct osc_thread_info *info = osc_env_info(env); |
| 3297 | struct cl_io *io = &info->oti_io; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3298 | osc_page_gang_cbt cb; |
| 3299 | int res; |
| 3300 | int result; |
| 3301 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3302 | io->ci_obj = cl_object_top(osc2cl(osc)); |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3303 | io->ci_ignore_layout = 1; |
| 3304 | result = cl_io_init(env, io, CIT_MISC, io->ci_obj); |
| 3305 | if (result != 0) |
| 3306 | goto out; |
| 3307 | |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3308 | cb = mode == CLM_READ ? check_and_discard_cb : discard_cb; |
Nathaniel Clark | 04a6284 | 2016-06-09 22:35:11 -0400 | [diff] [blame] | 3309 | info->oti_fn_index = start; |
| 3310 | info->oti_next_index = start; |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3311 | do { |
Jinshan Xiong | 06563b5 | 2016-03-30 19:48:40 -0400 | [diff] [blame] | 3312 | res = osc_page_gang_lookup(env, io, osc, |
| 3313 | info->oti_next_index, end, cb, osc); |
| 3314 | if (info->oti_next_index > end) |
Jinshan Xiong | d9d4790 | 2016-03-30 19:48:28 -0400 | [diff] [blame] | 3315 | break; |
| 3316 | |
| 3317 | if (res == CLP_GANG_RESCHED) |
| 3318 | cond_resched(); |
| 3319 | } while (res != CLP_GANG_OKAY); |
| 3320 | out: |
| 3321 | cl_io_fini(env, io); |
| 3322 | return result; |
| 3323 | } |
| 3324 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3325 | /** @} osc */ |