blob: 4e0a357ed43d739fce16a86391702194e8446de0 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050030 * Copyright (c) 2012, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080031 *
32 */
33/*
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
36 *
37 * osc cache management.
38 *
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40 */
41
42#define DEBUG_SUBSYSTEM S_OSC
43
44#include "osc_cl_internal.h"
45#include "osc_internal.h"
46
47static int extent_debug; /* set it to be true for more debug */
48
49static void osc_update_pending(struct osc_object *obj, int cmd, int delta);
50static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
51 int state);
52static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
53 struct osc_async_page *oap, int sent, int rc);
54static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
55 int cmd);
56static int osc_refresh_count(const struct lu_env *env,
57 struct osc_async_page *oap, int cmd);
58static int osc_io_unplug_async(const struct lu_env *env,
59 struct client_obd *cli, struct osc_object *osc);
60static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
61 unsigned int lost_grant);
62
63static void osc_extent_tree_dump0(int level, struct osc_object *obj,
64 const char *func, int line);
65#define osc_extent_tree_dump(lvl, obj) \
66 osc_extent_tree_dump0(lvl, obj, __func__, __LINE__)
67
68/** \addtogroup osc
69 * @{
70 */
71
72/* ------------------ osc extent ------------------ */
73static inline char *ext_flags(struct osc_extent *ext, char *flags)
74{
75 char *buf = flags;
76 *buf++ = ext->oe_rw ? 'r' : 'w';
77 if (ext->oe_intree)
78 *buf++ = 'i';
79 if (ext->oe_srvlock)
80 *buf++ = 's';
81 if (ext->oe_hp)
82 *buf++ = 'h';
83 if (ext->oe_urgent)
84 *buf++ = 'u';
85 if (ext->oe_memalloc)
86 *buf++ = 'm';
87 if (ext->oe_trunc_pending)
88 *buf++ = 't';
89 if (ext->oe_fsync_wait)
90 *buf++ = 'Y';
91 *buf = 0;
92 return flags;
93}
94
95static inline char list_empty_marker(struct list_head *list)
96{
97 return list_empty(list) ? '-' : '+';
98}
99
100#define EXTSTR "[%lu -> %lu/%lu]"
101#define EXTPARA(ext) (ext)->oe_start, (ext)->oe_end, (ext)->oe_max_end
Bobi Jamcad6faf2013-06-03 21:40:45 +0800102static const char *oes_strings[] = {
103 "inv", "active", "cache", "locking", "lockdone", "rpc", "trunc", NULL };
Peng Taod7e09d02013-05-02 16:46:55 +0800104
105#define OSC_EXTENT_DUMP(lvl, extent, fmt, ...) do { \
106 struct osc_extent *__ext = (extent); \
Peng Taod7e09d02013-05-02 16:46:55 +0800107 char __buf[16]; \
108 \
109 CDEBUG(lvl, \
110 "extent %p@{" EXTSTR ", " \
111 "[%d|%d|%c|%s|%s|%p], [%d|%d|%c|%c|%p|%u|%p]} " fmt, \
112 /* ----- extent part 0 ----- */ \
113 __ext, EXTPARA(__ext), \
114 /* ----- part 1 ----- */ \
Chris Hanna29ac6842015-06-03 10:23:42 -0400115 atomic_read(&__ext->oe_refc), \
116 atomic_read(&__ext->oe_users), \
Peng Taod7e09d02013-05-02 16:46:55 +0800117 list_empty_marker(&__ext->oe_link), \
Bobi Jamcad6faf2013-06-03 21:40:45 +0800118 oes_strings[__ext->oe_state], ext_flags(__ext, __buf), \
Peng Taod7e09d02013-05-02 16:46:55 +0800119 __ext->oe_obj, \
120 /* ----- part 2 ----- */ \
121 __ext->oe_grants, __ext->oe_nr_pages, \
122 list_empty_marker(&__ext->oe_pages), \
123 waitqueue_active(&__ext->oe_waitq) ? '+' : '-', \
124 __ext->oe_osclock, __ext->oe_mppr, __ext->oe_owner, \
125 /* ----- part 4 ----- */ \
126 ## __VA_ARGS__); \
127} while (0)
128
129#undef EASSERTF
130#define EASSERTF(expr, ext, fmt, args...) do { \
131 if (!(expr)) { \
Bobi Jamcad6faf2013-06-03 21:40:45 +0800132 OSC_EXTENT_DUMP(D_ERROR, (ext), fmt, ##args); \
133 osc_extent_tree_dump(D_ERROR, (ext)->oe_obj); \
Peng Taod7e09d02013-05-02 16:46:55 +0800134 LASSERT(expr); \
Bobi Jamcad6faf2013-06-03 21:40:45 +0800135 } \
Peng Taod7e09d02013-05-02 16:46:55 +0800136} while (0)
137
138#undef EASSERT
139#define EASSERT(expr, ext) EASSERTF(expr, ext, "\n")
140
141static inline struct osc_extent *rb_extent(struct rb_node *n)
142{
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500143 if (!n)
Peng Taod7e09d02013-05-02 16:46:55 +0800144 return NULL;
145
146 return container_of(n, struct osc_extent, oe_node);
147}
148
149static inline struct osc_extent *next_extent(struct osc_extent *ext)
150{
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500151 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +0800152 return NULL;
153
154 LASSERT(ext->oe_intree);
155 return rb_extent(rb_next(&ext->oe_node));
156}
157
158static inline struct osc_extent *prev_extent(struct osc_extent *ext)
159{
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500160 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +0800161 return NULL;
162
163 LASSERT(ext->oe_intree);
164 return rb_extent(rb_prev(&ext->oe_node));
165}
166
167static inline struct osc_extent *first_extent(struct osc_object *obj)
168{
169 return rb_extent(rb_first(&obj->oo_root));
170}
171
172/* object must be locked by caller. */
173static int osc_extent_sanity_check0(struct osc_extent *ext,
174 const char *func, const int line)
175{
176 struct osc_object *obj = ext->oe_obj;
177 struct osc_async_page *oap;
178 int page_count;
179 int rc = 0;
180
Julia Lawall490e0e82014-11-09 10:06:32 +0100181 if (!osc_object_is_locked(obj)) {
182 rc = 9;
183 goto out;
184 }
Peng Taod7e09d02013-05-02 16:46:55 +0800185
Julia Lawall490e0e82014-11-09 10:06:32 +0100186 if (ext->oe_state >= OES_STATE_MAX) {
187 rc = 10;
188 goto out;
189 }
Peng Taod7e09d02013-05-02 16:46:55 +0800190
Julia Lawall490e0e82014-11-09 10:06:32 +0100191 if (atomic_read(&ext->oe_refc) <= 0) {
192 rc = 20;
193 goto out;
194 }
Peng Taod7e09d02013-05-02 16:46:55 +0800195
Julia Lawall490e0e82014-11-09 10:06:32 +0100196 if (atomic_read(&ext->oe_refc) < atomic_read(&ext->oe_users)) {
197 rc = 30;
198 goto out;
199 }
Peng Taod7e09d02013-05-02 16:46:55 +0800200
201 switch (ext->oe_state) {
202 case OES_INV:
203 if (ext->oe_nr_pages > 0 || !list_empty(&ext->oe_pages))
Julia Lawall490e0e82014-11-09 10:06:32 +0100204 rc = 35;
205 else
206 rc = 0;
207 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800208 case OES_ACTIVE:
Julia Lawall490e0e82014-11-09 10:06:32 +0100209 if (atomic_read(&ext->oe_users) == 0) {
210 rc = 40;
211 goto out;
212 }
213 if (ext->oe_hp) {
214 rc = 50;
215 goto out;
216 }
217 if (ext->oe_fsync_wait && !ext->oe_urgent) {
218 rc = 55;
219 goto out;
220 }
Peng Taod7e09d02013-05-02 16:46:55 +0800221 break;
222 case OES_CACHE:
Julia Lawall490e0e82014-11-09 10:06:32 +0100223 if (ext->oe_grants == 0) {
224 rc = 60;
225 goto out;
226 }
227 if (ext->oe_fsync_wait && !ext->oe_urgent && !ext->oe_hp) {
228 rc = 65;
229 goto out;
230 }
Peng Taod7e09d02013-05-02 16:46:55 +0800231 default:
Julia Lawall490e0e82014-11-09 10:06:32 +0100232 if (atomic_read(&ext->oe_users) > 0) {
233 rc = 70;
234 goto out;
235 }
Peng Taod7e09d02013-05-02 16:46:55 +0800236 }
237
Julia Lawall490e0e82014-11-09 10:06:32 +0100238 if (ext->oe_max_end < ext->oe_end || ext->oe_end < ext->oe_start) {
239 rc = 80;
240 goto out;
241 }
Peng Taod7e09d02013-05-02 16:46:55 +0800242
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500243 if (!ext->oe_osclock && ext->oe_grants > 0) {
Julia Lawall490e0e82014-11-09 10:06:32 +0100244 rc = 90;
245 goto out;
246 }
Peng Taod7e09d02013-05-02 16:46:55 +0800247
248 if (ext->oe_osclock) {
249 struct cl_lock_descr *descr;
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300250
Peng Taod7e09d02013-05-02 16:46:55 +0800251 descr = &ext->oe_osclock->cll_descr;
252 if (!(descr->cld_start <= ext->oe_start &&
Julia Lawall490e0e82014-11-09 10:06:32 +0100253 descr->cld_end >= ext->oe_max_end)) {
254 rc = 100;
255 goto out;
256 }
Peng Taod7e09d02013-05-02 16:46:55 +0800257 }
258
Julia Lawall490e0e82014-11-09 10:06:32 +0100259 if (ext->oe_nr_pages > ext->oe_mppr) {
260 rc = 105;
261 goto out;
262 }
Peng Taod7e09d02013-05-02 16:46:55 +0800263
264 /* Do not verify page list if extent is in RPC. This is because an
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500265 * in-RPC extent is supposed to be exclusively accessible w/o lock.
266 */
Julia Lawall490e0e82014-11-09 10:06:32 +0100267 if (ext->oe_state > OES_CACHE) {
268 rc = 0;
269 goto out;
270 }
Peng Taod7e09d02013-05-02 16:46:55 +0800271
Julia Lawall490e0e82014-11-09 10:06:32 +0100272 if (!extent_debug) {
273 rc = 0;
274 goto out;
275 }
Peng Taod7e09d02013-05-02 16:46:55 +0800276
277 page_count = 0;
278 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
279 pgoff_t index = oap2cl_page(oap)->cp_index;
280 ++page_count;
Julia Lawall490e0e82014-11-09 10:06:32 +0100281 if (index > ext->oe_end || index < ext->oe_start) {
282 rc = 110;
283 goto out;
284 }
Peng Taod7e09d02013-05-02 16:46:55 +0800285 }
Julia Lawall490e0e82014-11-09 10:06:32 +0100286 if (page_count != ext->oe_nr_pages) {
287 rc = 120;
288 goto out;
289 }
Peng Taod7e09d02013-05-02 16:46:55 +0800290
291out:
292 if (rc != 0)
293 OSC_EXTENT_DUMP(D_ERROR, ext,
294 "%s:%d sanity check %p failed with rc = %d\n",
295 func, line, ext, rc);
296 return rc;
297}
298
299#define sanity_check_nolock(ext) \
300 osc_extent_sanity_check0(ext, __func__, __LINE__)
301
Chris Hanna29ac6842015-06-03 10:23:42 -0400302#define sanity_check(ext) ({ \
303 int __res; \
Peng Taod7e09d02013-05-02 16:46:55 +0800304 osc_object_lock((ext)->oe_obj); \
Chris Hanna29ac6842015-06-03 10:23:42 -0400305 __res = sanity_check_nolock(ext); \
306 osc_object_unlock((ext)->oe_obj); \
307 __res; \
Peng Taod7e09d02013-05-02 16:46:55 +0800308})
309
Peng Taod7e09d02013-05-02 16:46:55 +0800310/**
311 * sanity check - to make sure there is no overlapped extent in the tree.
312 */
313static int osc_extent_is_overlapped(struct osc_object *obj,
314 struct osc_extent *ext)
315{
316 struct osc_extent *tmp;
317
318 LASSERT(osc_object_is_locked(obj));
319
320 if (!extent_debug)
321 return 0;
322
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500323 for (tmp = first_extent(obj); tmp; tmp = next_extent(tmp)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800324 if (tmp == ext)
325 continue;
326 if (tmp->oe_end >= ext->oe_start &&
327 tmp->oe_start <= ext->oe_end)
328 return 1;
329 }
330 return 0;
331}
332
333static void osc_extent_state_set(struct osc_extent *ext, int state)
334{
335 LASSERT(osc_object_is_locked(ext->oe_obj));
336 LASSERT(state >= OES_INV && state < OES_STATE_MAX);
337
338 /* Never try to sanity check a state changing extent :-) */
339 /* LASSERT(sanity_check_nolock(ext) == 0); */
340
341 /* TODO: validate the state machine */
342 ext->oe_state = state;
343 wake_up_all(&ext->oe_waitq);
344}
345
346static struct osc_extent *osc_extent_alloc(struct osc_object *obj)
347{
348 struct osc_extent *ext;
349
Amitoj Kaur Chawlac4418da2016-02-26 14:24:55 +0530350 ext = kmem_cache_zalloc(osc_extent_kmem, GFP_NOFS);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500351 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +0800352 return NULL;
353
354 RB_CLEAR_NODE(&ext->oe_node);
355 ext->oe_obj = obj;
356 atomic_set(&ext->oe_refc, 1);
357 atomic_set(&ext->oe_users, 0);
358 INIT_LIST_HEAD(&ext->oe_link);
359 ext->oe_state = OES_INV;
360 INIT_LIST_HEAD(&ext->oe_pages);
361 init_waitqueue_head(&ext->oe_waitq);
362 ext->oe_osclock = NULL;
363
364 return ext;
365}
366
367static void osc_extent_free(struct osc_extent *ext)
368{
Mike Rapoport50d30362015-10-20 12:39:51 +0300369 kmem_cache_free(osc_extent_kmem, ext);
Peng Taod7e09d02013-05-02 16:46:55 +0800370}
371
372static struct osc_extent *osc_extent_get(struct osc_extent *ext)
373{
374 LASSERT(atomic_read(&ext->oe_refc) >= 0);
375 atomic_inc(&ext->oe_refc);
376 return ext;
377}
378
379static void osc_extent_put(const struct lu_env *env, struct osc_extent *ext)
380{
381 LASSERT(atomic_read(&ext->oe_refc) > 0);
382 if (atomic_dec_and_test(&ext->oe_refc)) {
383 LASSERT(list_empty(&ext->oe_link));
384 LASSERT(atomic_read(&ext->oe_users) == 0);
385 LASSERT(ext->oe_state == OES_INV);
386 LASSERT(!ext->oe_intree);
387
388 if (ext->oe_osclock) {
389 cl_lock_put(env, ext->oe_osclock);
390 ext->oe_osclock = NULL;
391 }
392 osc_extent_free(ext);
393 }
394}
395
396/**
397 * osc_extent_put_trust() is a special version of osc_extent_put() when
398 * it's known that the caller is not the last user. This is to address the
399 * problem of lacking of lu_env ;-).
400 */
401static void osc_extent_put_trust(struct osc_extent *ext)
402{
403 LASSERT(atomic_read(&ext->oe_refc) > 1);
404 LASSERT(osc_object_is_locked(ext->oe_obj));
405 atomic_dec(&ext->oe_refc);
406}
407
408/**
409 * Return the extent which includes pgoff @index, or return the greatest
410 * previous extent in the tree.
411 */
412static struct osc_extent *osc_extent_search(struct osc_object *obj,
413 pgoff_t index)
414{
Chris Hanna29ac6842015-06-03 10:23:42 -0400415 struct rb_node *n = obj->oo_root.rb_node;
Peng Taod7e09d02013-05-02 16:46:55 +0800416 struct osc_extent *tmp, *p = NULL;
417
418 LASSERT(osc_object_is_locked(obj));
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500419 while (n) {
Peng Taod7e09d02013-05-02 16:46:55 +0800420 tmp = rb_extent(n);
421 if (index < tmp->oe_start) {
422 n = n->rb_left;
423 } else if (index > tmp->oe_end) {
424 p = rb_extent(n);
425 n = n->rb_right;
426 } else {
427 return tmp;
428 }
429 }
430 return p;
431}
432
433/*
434 * Return the extent covering @index, otherwise return NULL.
435 * caller must have held object lock.
436 */
437static struct osc_extent *osc_extent_lookup(struct osc_object *obj,
438 pgoff_t index)
439{
440 struct osc_extent *ext;
441
442 ext = osc_extent_search(obj, index);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500443 if (ext && ext->oe_start <= index && index <= ext->oe_end)
Peng Taod7e09d02013-05-02 16:46:55 +0800444 return osc_extent_get(ext);
445 return NULL;
446}
447
448/* caller must have held object lock. */
449static void osc_extent_insert(struct osc_object *obj, struct osc_extent *ext)
450{
Chris Hanna29ac6842015-06-03 10:23:42 -0400451 struct rb_node **n = &obj->oo_root.rb_node;
452 struct rb_node *parent = NULL;
Peng Taod7e09d02013-05-02 16:46:55 +0800453 struct osc_extent *tmp;
454
455 LASSERT(ext->oe_intree == 0);
456 LASSERT(ext->oe_obj == obj);
457 LASSERT(osc_object_is_locked(obj));
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500458 while (*n) {
Peng Taod7e09d02013-05-02 16:46:55 +0800459 tmp = rb_extent(*n);
460 parent = *n;
461
462 if (ext->oe_end < tmp->oe_start)
463 n = &(*n)->rb_left;
464 else if (ext->oe_start > tmp->oe_end)
465 n = &(*n)->rb_right;
466 else
James Nunez19b20562016-03-07 18:10:21 -0500467 EASSERTF(0, tmp, EXTSTR"\n", EXTPARA(ext));
Peng Taod7e09d02013-05-02 16:46:55 +0800468 }
469 rb_link_node(&ext->oe_node, parent, n);
470 rb_insert_color(&ext->oe_node, &obj->oo_root);
471 osc_extent_get(ext);
472 ext->oe_intree = 1;
473}
474
475/* caller must have held object lock. */
476static void osc_extent_erase(struct osc_extent *ext)
477{
478 struct osc_object *obj = ext->oe_obj;
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300479
Peng Taod7e09d02013-05-02 16:46:55 +0800480 LASSERT(osc_object_is_locked(obj));
481 if (ext->oe_intree) {
482 rb_erase(&ext->oe_node, &obj->oo_root);
483 ext->oe_intree = 0;
484 /* rbtree held a refcount */
485 osc_extent_put_trust(ext);
486 }
487}
488
489static struct osc_extent *osc_extent_hold(struct osc_extent *ext)
490{
491 struct osc_object *obj = ext->oe_obj;
492
493 LASSERT(osc_object_is_locked(obj));
494 LASSERT(ext->oe_state == OES_ACTIVE || ext->oe_state == OES_CACHE);
495 if (ext->oe_state == OES_CACHE) {
496 osc_extent_state_set(ext, OES_ACTIVE);
497 osc_update_pending(obj, OBD_BRW_WRITE, -ext->oe_nr_pages);
498 }
499 atomic_inc(&ext->oe_users);
500 list_del_init(&ext->oe_link);
501 return osc_extent_get(ext);
502}
503
504static void __osc_extent_remove(struct osc_extent *ext)
505{
506 LASSERT(osc_object_is_locked(ext->oe_obj));
507 LASSERT(list_empty(&ext->oe_pages));
508 osc_extent_erase(ext);
509 list_del_init(&ext->oe_link);
510 osc_extent_state_set(ext, OES_INV);
511 OSC_EXTENT_DUMP(D_CACHE, ext, "destroyed.\n");
512}
513
514static void osc_extent_remove(struct osc_extent *ext)
515{
516 struct osc_object *obj = ext->oe_obj;
517
518 osc_object_lock(obj);
519 __osc_extent_remove(ext);
520 osc_object_unlock(obj);
521}
522
523/**
524 * This function is used to merge extents to get better performance. It checks
525 * if @cur and @victim are contiguous at chunk level.
526 */
527static int osc_extent_merge(const struct lu_env *env, struct osc_extent *cur,
528 struct osc_extent *victim)
529{
530 struct osc_object *obj = cur->oe_obj;
531 pgoff_t chunk_start;
532 pgoff_t chunk_end;
533 int ppc_bits;
534
535 LASSERT(cur->oe_state == OES_CACHE);
536 LASSERT(osc_object_is_locked(obj));
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500537 if (!victim)
Peng Taod7e09d02013-05-02 16:46:55 +0800538 return -EINVAL;
539
540 if (victim->oe_state != OES_CACHE || victim->oe_fsync_wait)
541 return -EBUSY;
542
543 if (cur->oe_max_end != victim->oe_max_end)
544 return -ERANGE;
545
546 LASSERT(cur->oe_osclock == victim->oe_osclock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300547 ppc_bits = osc_cli(obj)->cl_chunkbits - PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +0800548 chunk_start = cur->oe_start >> ppc_bits;
Chris Hanna29ac6842015-06-03 10:23:42 -0400549 chunk_end = cur->oe_end >> ppc_bits;
550 if (chunk_start != (victim->oe_end >> ppc_bits) + 1 &&
Peng Taod7e09d02013-05-02 16:46:55 +0800551 chunk_end + 1 != victim->oe_start >> ppc_bits)
552 return -ERANGE;
553
554 OSC_EXTENT_DUMP(D_CACHE, victim, "will be merged by %p.\n", cur);
555
Chris Hanna29ac6842015-06-03 10:23:42 -0400556 cur->oe_start = min(cur->oe_start, victim->oe_start);
557 cur->oe_end = max(cur->oe_end, victim->oe_end);
558 cur->oe_grants += victim->oe_grants;
Peng Taod7e09d02013-05-02 16:46:55 +0800559 cur->oe_nr_pages += victim->oe_nr_pages;
560 /* only the following bits are needed to merge */
Chris Hanna29ac6842015-06-03 10:23:42 -0400561 cur->oe_urgent |= victim->oe_urgent;
Peng Taod7e09d02013-05-02 16:46:55 +0800562 cur->oe_memalloc |= victim->oe_memalloc;
563 list_splice_init(&victim->oe_pages, &cur->oe_pages);
564 list_del_init(&victim->oe_link);
565 victim->oe_nr_pages = 0;
566
567 osc_extent_get(victim);
568 __osc_extent_remove(victim);
569 osc_extent_put(env, victim);
570
571 OSC_EXTENT_DUMP(D_CACHE, cur, "after merging %p.\n", victim);
572 return 0;
573}
574
575/**
576 * Drop user count of osc_extent, and unplug IO asynchronously.
577 */
Heena Sirwani882e7e22014-10-07 17:29:11 +0530578void osc_extent_release(const struct lu_env *env, struct osc_extent *ext)
Peng Taod7e09d02013-05-02 16:46:55 +0800579{
580 struct osc_object *obj = ext->oe_obj;
Peng Taod7e09d02013-05-02 16:46:55 +0800581
582 LASSERT(atomic_read(&ext->oe_users) > 0);
583 LASSERT(sanity_check(ext) == 0);
584 LASSERT(ext->oe_grants > 0);
585
586 if (atomic_dec_and_lock(&ext->oe_users, &obj->oo_lock)) {
587 LASSERT(ext->oe_state == OES_ACTIVE);
588 if (ext->oe_trunc_pending) {
589 /* a truncate process is waiting for this extent.
590 * This may happen due to a race, check
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500591 * osc_cache_truncate_start().
592 */
Peng Taod7e09d02013-05-02 16:46:55 +0800593 osc_extent_state_set(ext, OES_TRUNC);
594 ext->oe_trunc_pending = 0;
595 } else {
596 osc_extent_state_set(ext, OES_CACHE);
597 osc_update_pending(obj, OBD_BRW_WRITE,
598 ext->oe_nr_pages);
599
600 /* try to merge the previous and next extent. */
601 osc_extent_merge(env, ext, prev_extent(ext));
602 osc_extent_merge(env, ext, next_extent(ext));
603
604 if (ext->oe_urgent)
605 list_move_tail(&ext->oe_link,
Oleg Drokin79910d72016-02-26 01:50:03 -0500606 &obj->oo_urgent_exts);
Peng Taod7e09d02013-05-02 16:46:55 +0800607 }
608 osc_object_unlock(obj);
609
610 osc_io_unplug_async(env, osc_cli(obj), obj);
611 }
612 osc_extent_put(env, ext);
Peng Taod7e09d02013-05-02 16:46:55 +0800613}
614
615static inline int overlapped(struct osc_extent *ex1, struct osc_extent *ex2)
616{
617 return !(ex1->oe_end < ex2->oe_start || ex2->oe_end < ex1->oe_start);
618}
619
620/**
621 * Find or create an extent which includes @index, core function to manage
622 * extent tree.
623 */
Shraddha Barke6ef3f3c2015-12-18 02:40:28 +0530624static struct osc_extent *osc_extent_find(const struct lu_env *env,
625 struct osc_object *obj, pgoff_t index,
626 int *grants)
Peng Taod7e09d02013-05-02 16:46:55 +0800627
628{
629 struct client_obd *cli = osc_cli(obj);
Chris Hanna29ac6842015-06-03 10:23:42 -0400630 struct cl_lock *lock;
Peng Taod7e09d02013-05-02 16:46:55 +0800631 struct osc_extent *cur;
632 struct osc_extent *ext;
633 struct osc_extent *conflict = NULL;
634 struct osc_extent *found = NULL;
Chris Hanna29ac6842015-06-03 10:23:42 -0400635 pgoff_t chunk;
636 pgoff_t max_end;
637 int max_pages; /* max_pages_per_rpc */
638 int chunksize;
639 int ppc_bits; /* pages per chunk bits */
640 int chunk_mask;
641 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800642
643 cur = osc_extent_alloc(obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500644 if (!cur)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800645 return ERR_PTR(-ENOMEM);
Peng Taod7e09d02013-05-02 16:46:55 +0800646
647 lock = cl_lock_at_pgoff(env, osc2cl(obj), index, NULL, 1, 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800648 LASSERT(lock->cll_descr.cld_mode >= CLM_WRITE);
649
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300650 LASSERT(cli->cl_chunkbits >= PAGE_SHIFT);
651 ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +0800652 chunk_mask = ~((1 << ppc_bits) - 1);
Chris Hanna29ac6842015-06-03 10:23:42 -0400653 chunksize = 1 << cli->cl_chunkbits;
654 chunk = index >> ppc_bits;
Peng Taod7e09d02013-05-02 16:46:55 +0800655
656 /* align end to rpc edge, rpc size may not be a power 2 integer. */
657 max_pages = cli->cl_max_pages_per_rpc;
658 LASSERT((max_pages & ~chunk_mask) == 0);
659 max_end = index - (index % max_pages) + max_pages - 1;
660 max_end = min_t(pgoff_t, max_end, lock->cll_descr.cld_end);
661
662 /* initialize new extent by parameters so far */
663 cur->oe_max_end = max_end;
Chris Hanna29ac6842015-06-03 10:23:42 -0400664 cur->oe_start = index & chunk_mask;
665 cur->oe_end = ((index + ~chunk_mask + 1) & chunk_mask) - 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800666 if (cur->oe_start < lock->cll_descr.cld_start)
667 cur->oe_start = lock->cll_descr.cld_start;
668 if (cur->oe_end > max_end)
669 cur->oe_end = max_end;
670 cur->oe_osclock = lock;
Chris Hanna29ac6842015-06-03 10:23:42 -0400671 cur->oe_grants = 0;
672 cur->oe_mppr = max_pages;
Peng Taod7e09d02013-05-02 16:46:55 +0800673
674 /* grants has been allocated by caller */
675 LASSERTF(*grants >= chunksize + cli->cl_extent_tax,
676 "%u/%u/%u.\n", *grants, chunksize, cli->cl_extent_tax);
James Nunez19b20562016-03-07 18:10:21 -0500677 LASSERTF((max_end - cur->oe_start) < max_pages, EXTSTR"\n",
678 EXTPARA(cur));
Peng Taod7e09d02013-05-02 16:46:55 +0800679
680restart:
681 osc_object_lock(obj);
682 ext = osc_extent_search(obj, cur->oe_start);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500683 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +0800684 ext = first_extent(obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500685 while (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +0800686 loff_t ext_chk_start = ext->oe_start >> ppc_bits;
Chris Hanna29ac6842015-06-03 10:23:42 -0400687 loff_t ext_chk_end = ext->oe_end >> ppc_bits;
Peng Taod7e09d02013-05-02 16:46:55 +0800688
689 LASSERT(sanity_check_nolock(ext) == 0);
690 if (chunk > ext_chk_end + 1)
691 break;
692
693 /* if covering by different locks, no chance to match */
694 if (lock != ext->oe_osclock) {
695 EASSERTF(!overlapped(ext, cur), ext,
James Nunez19b20562016-03-07 18:10:21 -0500696 EXTSTR"\n", EXTPARA(cur));
Peng Taod7e09d02013-05-02 16:46:55 +0800697
698 ext = next_extent(ext);
699 continue;
700 }
701
702 /* discontiguous chunks? */
703 if (chunk + 1 < ext_chk_start) {
704 ext = next_extent(ext);
705 continue;
706 }
707
708 /* ok, from now on, ext and cur have these attrs:
709 * 1. covered by the same lock
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500710 * 2. contiguous at chunk level or overlapping.
711 */
Peng Taod7e09d02013-05-02 16:46:55 +0800712
713 if (overlapped(ext, cur)) {
714 /* cur is the minimum unit, so overlapping means
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500715 * full contain.
716 */
Peng Taod7e09d02013-05-02 16:46:55 +0800717 EASSERTF((ext->oe_start <= cur->oe_start &&
718 ext->oe_end >= cur->oe_end),
James Nunez19b20562016-03-07 18:10:21 -0500719 ext, EXTSTR"\n", EXTPARA(cur));
Peng Taod7e09d02013-05-02 16:46:55 +0800720
721 if (ext->oe_state > OES_CACHE || ext->oe_fsync_wait) {
722 /* for simplicity, we wait for this extent to
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500723 * finish before going forward.
724 */
Peng Taod7e09d02013-05-02 16:46:55 +0800725 conflict = osc_extent_get(ext);
726 break;
727 }
728
729 found = osc_extent_hold(ext);
730 break;
731 }
732
733 /* non-overlapped extent */
734 if (ext->oe_state != OES_CACHE || ext->oe_fsync_wait) {
735 /* we can't do anything for a non OES_CACHE extent, or
736 * if there is someone waiting for this extent to be
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500737 * flushed, try next one.
738 */
Peng Taod7e09d02013-05-02 16:46:55 +0800739 ext = next_extent(ext);
740 continue;
741 }
742
743 /* check if they belong to the same rpc slot before trying to
744 * merge. the extents are not overlapped and contiguous at
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500745 * chunk level to get here.
746 */
Peng Taod7e09d02013-05-02 16:46:55 +0800747 if (ext->oe_max_end != max_end) {
748 /* if they don't belong to the same RPC slot or
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500749 * max_pages_per_rpc has ever changed, do not merge.
750 */
Peng Taod7e09d02013-05-02 16:46:55 +0800751 ext = next_extent(ext);
752 continue;
753 }
754
755 /* it's required that an extent must be contiguous at chunk
756 * level so that we know the whole extent is covered by grant
757 * (the pages in the extent are NOT required to be contiguous).
758 * Otherwise, it will be too much difficult to know which
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500759 * chunks have grants allocated.
760 */
Peng Taod7e09d02013-05-02 16:46:55 +0800761
762 /* try to do front merge - extend ext's start */
763 if (chunk + 1 == ext_chk_start) {
764 /* ext must be chunk size aligned */
765 EASSERT((ext->oe_start & ~chunk_mask) == 0, ext);
766
767 /* pull ext's start back to cover cur */
Chris Hanna29ac6842015-06-03 10:23:42 -0400768 ext->oe_start = cur->oe_start;
Peng Taod7e09d02013-05-02 16:46:55 +0800769 ext->oe_grants += chunksize;
770 *grants -= chunksize;
771
772 found = osc_extent_hold(ext);
773 } else if (chunk == ext_chk_end + 1) {
774 /* rear merge */
Chris Hanna29ac6842015-06-03 10:23:42 -0400775 ext->oe_end = cur->oe_end;
Peng Taod7e09d02013-05-02 16:46:55 +0800776 ext->oe_grants += chunksize;
777 *grants -= chunksize;
778
779 /* try to merge with the next one because we just fill
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500780 * in a gap
781 */
Peng Taod7e09d02013-05-02 16:46:55 +0800782 if (osc_extent_merge(env, ext, next_extent(ext)) == 0)
783 /* we can save extent tax from next extent */
784 *grants += cli->cl_extent_tax;
785
786 found = osc_extent_hold(ext);
787 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500788 if (found)
Peng Taod7e09d02013-05-02 16:46:55 +0800789 break;
790
791 ext = next_extent(ext);
792 }
793
794 osc_extent_tree_dump(D_CACHE, obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500795 if (found) {
796 LASSERT(!conflict);
Peng Taod7e09d02013-05-02 16:46:55 +0800797 if (!IS_ERR(found)) {
798 LASSERT(found->oe_osclock == cur->oe_osclock);
799 OSC_EXTENT_DUMP(D_CACHE, found,
800 "found caching ext for %lu.\n", index);
801 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500802 } else if (!conflict) {
Peng Taod7e09d02013-05-02 16:46:55 +0800803 /* create a new extent */
804 EASSERT(osc_extent_is_overlapped(obj, cur) == 0, cur);
805 cur->oe_grants = chunksize + cli->cl_extent_tax;
806 *grants -= cur->oe_grants;
807 LASSERT(*grants >= 0);
808
809 cur->oe_state = OES_CACHE;
810 found = osc_extent_hold(cur);
811 osc_extent_insert(obj, cur);
812 OSC_EXTENT_DUMP(D_CACHE, cur, "add into tree %lu/%lu.\n",
813 index, lock->cll_descr.cld_end);
814 }
815 osc_object_unlock(obj);
816
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -0500817 if (conflict) {
818 LASSERT(!found);
Peng Taod7e09d02013-05-02 16:46:55 +0800819
820 /* waiting for IO to finish. Please notice that it's impossible
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500821 * to be an OES_TRUNC extent.
822 */
Peng Taod7e09d02013-05-02 16:46:55 +0800823 rc = osc_extent_wait(env, conflict, OES_INV);
824 osc_extent_put(env, conflict);
825 conflict = NULL;
Julia Lawall490e0e82014-11-09 10:06:32 +0100826 if (rc < 0) {
827 found = ERR_PTR(rc);
828 goto out;
829 }
Peng Taod7e09d02013-05-02 16:46:55 +0800830
831 goto restart;
832 }
Peng Taod7e09d02013-05-02 16:46:55 +0800833
834out:
835 osc_extent_put(env, cur);
836 LASSERT(*grants >= 0);
837 return found;
838}
839
840/**
841 * Called when IO is finished to an extent.
842 */
843int osc_extent_finish(const struct lu_env *env, struct osc_extent *ext,
844 int sent, int rc)
845{
846 struct client_obd *cli = osc_cli(ext->oe_obj);
847 struct osc_async_page *oap;
848 struct osc_async_page *tmp;
849 int nr_pages = ext->oe_nr_pages;
850 int lost_grant = 0;
851 int blocksize = cli->cl_import->imp_obd->obd_osfs.os_bsize ? : 4096;
852 __u64 last_off = 0;
853 int last_count = -1;
Peng Taod7e09d02013-05-02 16:46:55 +0800854
855 OSC_EXTENT_DUMP(D_CACHE, ext, "extent finished.\n");
856
857 ext->oe_rc = rc ?: ext->oe_nr_pages;
858 EASSERT(ergo(rc == 0, ext->oe_state == OES_RPC), ext);
Oleg Drokin79910d72016-02-26 01:50:03 -0500859 list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) {
Peng Taod7e09d02013-05-02 16:46:55 +0800860 list_del_init(&oap->oap_rpc_item);
861 list_del_init(&oap->oap_pending_item);
862 if (last_off <= oap->oap_obj_off) {
863 last_off = oap->oap_obj_off;
864 last_count = oap->oap_count;
865 }
866
867 --ext->oe_nr_pages;
868 osc_ap_completion(env, cli, oap, sent, rc);
869 }
870 EASSERT(ext->oe_nr_pages == 0, ext);
871
872 if (!sent) {
873 lost_grant = ext->oe_grants;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300874 } else if (blocksize < PAGE_SIZE &&
875 last_count != PAGE_SIZE) {
Peng Taod7e09d02013-05-02 16:46:55 +0800876 /* For short writes we shouldn't count parts of pages that
877 * span a whole chunk on the OST side, or our accounting goes
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500878 * wrong. Should match the code in filter_grant_check.
879 */
Peng Taod7e09d02013-05-02 16:46:55 +0800880 int offset = oap->oap_page_off & ~CFS_PAGE_MASK;
881 int count = oap->oap_count + (offset & (blocksize - 1));
882 int end = (offset + oap->oap_count) & (blocksize - 1);
Mike Rapoport50ffcb72015-10-13 16:03:40 +0300883
Peng Taod7e09d02013-05-02 16:46:55 +0800884 if (end)
885 count += blocksize - end;
886
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300887 lost_grant = PAGE_SIZE - count;
Peng Taod7e09d02013-05-02 16:46:55 +0800888 }
889 if (ext->oe_grants > 0)
890 osc_free_grant(cli, nr_pages, lost_grant);
891
892 osc_extent_remove(ext);
893 /* put the refcount for RPC */
894 osc_extent_put(env, ext);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800895 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800896}
897
898static int extent_wait_cb(struct osc_extent *ext, int state)
899{
900 int ret;
901
902 osc_object_lock(ext->oe_obj);
903 ret = ext->oe_state == state;
904 osc_object_unlock(ext->oe_obj);
905
906 return ret;
907}
908
909/**
910 * Wait for the extent's state to become @state.
911 */
912static int osc_extent_wait(const struct lu_env *env, struct osc_extent *ext,
913 int state)
914{
915 struct osc_object *obj = ext->oe_obj;
916 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(600), NULL,
917 LWI_ON_SIGNAL_NOOP, NULL);
918 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800919
920 osc_object_lock(obj);
921 LASSERT(sanity_check_nolock(ext) == 0);
922 /* `Kick' this extent only if the caller is waiting for it to be
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500923 * written out.
924 */
Andriy Skulyshce248d52014-06-22 21:32:16 -0400925 if (state == OES_INV && !ext->oe_urgent && !ext->oe_hp &&
926 !ext->oe_trunc_pending) {
Peng Taod7e09d02013-05-02 16:46:55 +0800927 if (ext->oe_state == OES_ACTIVE) {
928 ext->oe_urgent = 1;
929 } else if (ext->oe_state == OES_CACHE) {
930 ext->oe_urgent = 1;
931 osc_extent_hold(ext);
932 rc = 1;
933 }
934 }
935 osc_object_unlock(obj);
936 if (rc == 1)
937 osc_extent_release(env, ext);
938
939 /* wait for the extent until its state becomes @state */
940 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state), &lwi);
941 if (rc == -ETIMEDOUT) {
942 OSC_EXTENT_DUMP(D_ERROR, ext,
943 "%s: wait ext to %d timedout, recovery in progress?\n",
944 osc_export(obj)->exp_obd->obd_name, state);
945
946 lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
947 rc = l_wait_event(ext->oe_waitq, extent_wait_cb(ext, state),
948 &lwi);
949 }
950 if (rc == 0 && ext->oe_rc < 0)
951 rc = ext->oe_rc;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800952 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800953}
954
955/**
956 * Discard pages with index greater than @size. If @ext is overlapped with
957 * @size, then partial truncate happens.
958 */
959static int osc_extent_truncate(struct osc_extent *ext, pgoff_t trunc_index,
Chris Hanna29ac6842015-06-03 10:23:42 -0400960 bool partial)
Peng Taod7e09d02013-05-02 16:46:55 +0800961{
Chris Hanna29ac6842015-06-03 10:23:42 -0400962 struct cl_env_nest nest;
963 struct lu_env *env;
964 struct cl_io *io;
965 struct osc_object *obj = ext->oe_obj;
966 struct client_obd *cli = osc_cli(obj);
Peng Taod7e09d02013-05-02 16:46:55 +0800967 struct osc_async_page *oap;
968 struct osc_async_page *tmp;
Chris Hanna29ac6842015-06-03 10:23:42 -0400969 int pages_in_chunk = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300970 int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
Chris Hanna29ac6842015-06-03 10:23:42 -0400971 __u64 trunc_chunk = trunc_index >> ppc_bits;
972 int grants = 0;
973 int nr_pages = 0;
974 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800975
976 LASSERT(sanity_check(ext) == 0);
Andriy Skulyshce248d52014-06-22 21:32:16 -0400977 EASSERT(ext->oe_state == OES_TRUNC, ext);
978 EASSERT(!ext->oe_urgent, ext);
Peng Taod7e09d02013-05-02 16:46:55 +0800979
980 /* Request new lu_env.
981 * We can't use that env from osc_cache_truncate_start() because
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500982 * it's from lov_io_sub and not fully initialized.
983 */
Peng Taod7e09d02013-05-02 16:46:55 +0800984 env = cl_env_nested_get(&nest);
985 io = &osc_env_info(env)->oti_io;
986 io->ci_obj = cl_object_top(osc2cl(obj));
987 rc = cl_io_init(env, io, CIT_MISC, io->ci_obj);
988 if (rc < 0)
Julia Lawall490e0e82014-11-09 10:06:32 +0100989 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +0800990
991 /* discard all pages with index greater then trunc_index */
Oleg Drokin79910d72016-02-26 01:50:03 -0500992 list_for_each_entry_safe(oap, tmp, &ext->oe_pages, oap_pending_item) {
Chris Hanna29ac6842015-06-03 10:23:42 -0400993 struct cl_page *sub = oap2cl_page(oap);
994 struct cl_page *page = cl_page_top(sub);
Peng Taod7e09d02013-05-02 16:46:55 +0800995
996 LASSERT(list_empty(&oap->oap_rpc_item));
997
998 /* only discard the pages with their index greater than
Oleg Drokin30aa9c52016-02-24 22:00:37 -0500999 * trunc_index, and ...
1000 */
Peng Taod7e09d02013-05-02 16:46:55 +08001001 if (sub->cp_index < trunc_index ||
1002 (sub->cp_index == trunc_index && partial)) {
1003 /* accounting how many pages remaining in the chunk
1004 * so that we can calculate grants correctly. */
1005 if (sub->cp_index >> ppc_bits == trunc_chunk)
1006 ++pages_in_chunk;
1007 continue;
1008 }
1009
1010 list_del_init(&oap->oap_pending_item);
1011
1012 cl_page_get(page);
1013 lu_ref_add(&page->cp_reference, "truncate", current);
1014
1015 if (cl_page_own(env, io, page) == 0) {
1016 cl_page_unmap(env, io, page);
1017 cl_page_discard(env, io, page);
1018 cl_page_disown(env, io, page);
1019 } else {
1020 LASSERT(page->cp_state == CPS_FREEING);
1021 LASSERT(0);
1022 }
1023
1024 lu_ref_del(&page->cp_reference, "truncate", current);
1025 cl_page_put(env, page);
1026
1027 --ext->oe_nr_pages;
1028 ++nr_pages;
1029 }
1030 EASSERTF(ergo(ext->oe_start >= trunc_index + !!partial,
1031 ext->oe_nr_pages == 0),
1032 ext, "trunc_index %lu, partial %d\n", trunc_index, partial);
1033
1034 osc_object_lock(obj);
1035 if (ext->oe_nr_pages == 0) {
1036 LASSERT(pages_in_chunk == 0);
1037 grants = ext->oe_grants;
1038 ext->oe_grants = 0;
1039 } else { /* calculate how many grants we can free */
Chris Hanna29ac6842015-06-03 10:23:42 -04001040 int chunks = (ext->oe_end >> ppc_bits) - trunc_chunk;
Peng Taod7e09d02013-05-02 16:46:55 +08001041 pgoff_t last_index;
1042
Peng Taod7e09d02013-05-02 16:46:55 +08001043 /* if there is no pages in this chunk, we can also free grants
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001044 * for the last chunk
1045 */
Peng Taod7e09d02013-05-02 16:46:55 +08001046 if (pages_in_chunk == 0) {
1047 /* if this is the 1st chunk and no pages in this chunk,
1048 * ext->oe_nr_pages must be zero, so we should be in
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001049 * the other if-clause.
1050 */
Peng Taod7e09d02013-05-02 16:46:55 +08001051 LASSERT(trunc_chunk > 0);
1052 --trunc_chunk;
1053 ++chunks;
1054 }
1055
1056 /* this is what we can free from this extent */
Chris Hanna29ac6842015-06-03 10:23:42 -04001057 grants = chunks << cli->cl_chunkbits;
Peng Taod7e09d02013-05-02 16:46:55 +08001058 ext->oe_grants -= grants;
Chris Hanna29ac6842015-06-03 10:23:42 -04001059 last_index = ((trunc_chunk + 1) << ppc_bits) - 1;
1060 ext->oe_end = min(last_index, ext->oe_max_end);
Peng Taod7e09d02013-05-02 16:46:55 +08001061 LASSERT(ext->oe_end >= ext->oe_start);
1062 LASSERT(ext->oe_grants > 0);
1063 }
1064 osc_object_unlock(obj);
1065
1066 if (grants > 0 || nr_pages > 0)
1067 osc_free_grant(cli, nr_pages, grants);
1068
1069out:
1070 cl_io_fini(env, io);
1071 cl_env_nested_put(&nest, env);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001072 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001073}
1074
1075/**
1076 * This function is used to make the extent prepared for transfer.
Masanari Iida34ca87482014-03-08 22:58:36 +09001077 * A race with flushing page - ll_writepage() has to be handled cautiously.
Peng Taod7e09d02013-05-02 16:46:55 +08001078 */
1079static int osc_extent_make_ready(const struct lu_env *env,
1080 struct osc_extent *ext)
1081{
1082 struct osc_async_page *oap;
1083 struct osc_async_page *last = NULL;
1084 struct osc_object *obj = ext->oe_obj;
1085 int page_count = 0;
1086 int rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001087
1088 /* we're going to grab page lock, so object lock must not be taken. */
1089 LASSERT(sanity_check(ext) == 0);
1090 /* in locking state, any process should not touch this extent. */
1091 EASSERT(ext->oe_state == OES_LOCKING, ext);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001092 EASSERT(ext->oe_owner, ext);
Peng Taod7e09d02013-05-02 16:46:55 +08001093
1094 OSC_EXTENT_DUMP(D_CACHE, ext, "make ready\n");
1095
1096 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1097 ++page_count;
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001098 if (!last || last->oap_obj_off < oap->oap_obj_off)
Peng Taod7e09d02013-05-02 16:46:55 +08001099 last = oap;
1100
1101 /* checking ASYNC_READY is race safe */
1102 if ((oap->oap_async_flags & ASYNC_READY) != 0)
1103 continue;
1104
1105 rc = osc_make_ready(env, oap, OBD_BRW_WRITE);
1106 switch (rc) {
1107 case 0:
1108 spin_lock(&oap->oap_lock);
1109 oap->oap_async_flags |= ASYNC_READY;
1110 spin_unlock(&oap->oap_lock);
1111 break;
1112 case -EALREADY:
1113 LASSERT((oap->oap_async_flags & ASYNC_READY) != 0);
1114 break;
1115 default:
1116 LASSERTF(0, "unknown return code: %d\n", rc);
1117 }
1118 }
1119
1120 LASSERT(page_count == ext->oe_nr_pages);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001121 LASSERT(last);
Peng Taod7e09d02013-05-02 16:46:55 +08001122 /* the last page is the only one we need to refresh its count by
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001123 * the size of file.
1124 */
Peng Taod7e09d02013-05-02 16:46:55 +08001125 if (!(last->oap_async_flags & ASYNC_COUNT_STABLE)) {
1126 last->oap_count = osc_refresh_count(env, last, OBD_BRW_WRITE);
1127 LASSERT(last->oap_count > 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001128 LASSERT(last->oap_page_off + last->oap_count <= PAGE_SIZE);
Peng Taod7e09d02013-05-02 16:46:55 +08001129 last->oap_async_flags |= ASYNC_COUNT_STABLE;
1130 }
1131
1132 /* for the rest of pages, we don't need to call osf_refresh_count()
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001133 * because it's known they are not the last page
1134 */
Peng Taod7e09d02013-05-02 16:46:55 +08001135 list_for_each_entry(oap, &ext->oe_pages, oap_pending_item) {
1136 if (!(oap->oap_async_flags & ASYNC_COUNT_STABLE)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001137 oap->oap_count = PAGE_SIZE - oap->oap_page_off;
Peng Taod7e09d02013-05-02 16:46:55 +08001138 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
1139 }
1140 }
1141
1142 osc_object_lock(obj);
1143 osc_extent_state_set(ext, OES_RPC);
1144 osc_object_unlock(obj);
1145 /* get a refcount for RPC. */
1146 osc_extent_get(ext);
1147
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001148 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001149}
1150
1151/**
1152 * Quick and simple version of osc_extent_find(). This function is frequently
1153 * called to expand the extent for the same IO. To expand the extent, the
1154 * page index must be in the same or next chunk of ext->oe_end.
1155 */
1156static int osc_extent_expand(struct osc_extent *ext, pgoff_t index, int *grants)
1157{
1158 struct osc_object *obj = ext->oe_obj;
1159 struct client_obd *cli = osc_cli(obj);
1160 struct osc_extent *next;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001161 int ppc_bits = cli->cl_chunkbits - PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +08001162 pgoff_t chunk = index >> ppc_bits;
1163 pgoff_t end_chunk;
1164 pgoff_t end_index;
1165 int chunksize = 1 << cli->cl_chunkbits;
1166 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001167
1168 LASSERT(ext->oe_max_end >= index && ext->oe_start <= index);
1169 osc_object_lock(obj);
1170 LASSERT(sanity_check_nolock(ext) == 0);
1171 end_chunk = ext->oe_end >> ppc_bits;
Julia Lawall490e0e82014-11-09 10:06:32 +01001172 if (chunk > end_chunk + 1) {
1173 rc = -ERANGE;
1174 goto out;
1175 }
Peng Taod7e09d02013-05-02 16:46:55 +08001176
Julia Lawall490e0e82014-11-09 10:06:32 +01001177 if (end_chunk >= chunk) {
1178 rc = 0;
1179 goto out;
1180 }
Peng Taod7e09d02013-05-02 16:46:55 +08001181
1182 LASSERT(end_chunk + 1 == chunk);
1183 /* try to expand this extent to cover @index */
1184 end_index = min(ext->oe_max_end, ((chunk + 1) << ppc_bits) - 1);
1185
1186 next = next_extent(ext);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001187 if (next && next->oe_start <= end_index) {
Peng Taod7e09d02013-05-02 16:46:55 +08001188 /* complex mode - overlapped with the next extent,
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001189 * this case will be handled by osc_extent_find()
1190 */
Julia Lawall490e0e82014-11-09 10:06:32 +01001191 rc = -EAGAIN;
1192 goto out;
1193 }
Peng Taod7e09d02013-05-02 16:46:55 +08001194
1195 ext->oe_end = end_index;
1196 ext->oe_grants += chunksize;
1197 *grants -= chunksize;
1198 LASSERT(*grants >= 0);
1199 EASSERTF(osc_extent_is_overlapped(obj, ext) == 0, ext,
1200 "overlapped after expanding for %lu.\n", index);
Peng Taod7e09d02013-05-02 16:46:55 +08001201
1202out:
1203 osc_object_unlock(obj);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001204 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001205}
1206
1207static void osc_extent_tree_dump0(int level, struct osc_object *obj,
1208 const char *func, int line)
1209{
1210 struct osc_extent *ext;
1211 int cnt;
1212
1213 CDEBUG(level, "Dump object %p extents at %s:%d, mppr: %u.\n",
1214 obj, func, line, osc_cli(obj)->cl_max_pages_per_rpc);
1215
1216 /* osc_object_lock(obj); */
1217 cnt = 1;
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001218 for (ext = first_extent(obj); ext; ext = next_extent(ext))
Peng Taod7e09d02013-05-02 16:46:55 +08001219 OSC_EXTENT_DUMP(level, ext, "in tree %d.\n", cnt++);
1220
1221 cnt = 1;
1222 list_for_each_entry(ext, &obj->oo_hp_exts, oe_link)
1223 OSC_EXTENT_DUMP(level, ext, "hp %d.\n", cnt++);
1224
1225 cnt = 1;
1226 list_for_each_entry(ext, &obj->oo_urgent_exts, oe_link)
1227 OSC_EXTENT_DUMP(level, ext, "urgent %d.\n", cnt++);
1228
1229 cnt = 1;
1230 list_for_each_entry(ext, &obj->oo_reading_exts, oe_link)
1231 OSC_EXTENT_DUMP(level, ext, "reading %d.\n", cnt++);
1232 /* osc_object_unlock(obj); */
1233}
1234
1235/* ------------------ osc extent end ------------------ */
1236
1237static inline int osc_is_ready(struct osc_object *osc)
1238{
1239 return !list_empty(&osc->oo_ready_item) ||
1240 !list_empty(&osc->oo_hp_ready_item);
1241}
1242
1243#define OSC_IO_DEBUG(OSC, STR, args...) \
1244 CDEBUG(D_CACHE, "obj %p ready %d|%c|%c wr %d|%c|%c rd %d|%c " STR, \
1245 (OSC), osc_is_ready(OSC), \
1246 list_empty_marker(&(OSC)->oo_hp_ready_item), \
1247 list_empty_marker(&(OSC)->oo_ready_item), \
1248 atomic_read(&(OSC)->oo_nr_writes), \
1249 list_empty_marker(&(OSC)->oo_hp_exts), \
1250 list_empty_marker(&(OSC)->oo_urgent_exts), \
1251 atomic_read(&(OSC)->oo_nr_reads), \
1252 list_empty_marker(&(OSC)->oo_reading_exts), \
1253 ##args)
1254
1255static int osc_make_ready(const struct lu_env *env, struct osc_async_page *oap,
1256 int cmd)
1257{
Chris Hanna29ac6842015-06-03 10:23:42 -04001258 struct osc_page *opg = oap2osc_page(oap);
1259 struct cl_page *page = cl_page_top(oap2cl_page(oap));
Peng Taod7e09d02013-05-02 16:46:55 +08001260 int result;
1261
1262 LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
1263
Peng Taod7e09d02013-05-02 16:46:55 +08001264 result = cl_page_make_ready(env, page, CRT_WRITE);
1265 if (result == 0)
1266 opg->ops_submit_time = cfs_time_current();
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001267 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08001268}
1269
1270static int osc_refresh_count(const struct lu_env *env,
1271 struct osc_async_page *oap, int cmd)
1272{
Chris Hanna29ac6842015-06-03 10:23:42 -04001273 struct osc_page *opg = oap2osc_page(oap);
1274 struct cl_page *page = oap2cl_page(oap);
Peng Taod7e09d02013-05-02 16:46:55 +08001275 struct cl_object *obj;
Chris Hanna29ac6842015-06-03 10:23:42 -04001276 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
Peng Taod7e09d02013-05-02 16:46:55 +08001277
1278 int result;
1279 loff_t kms;
1280
1281 /* readpage queues with _COUNT_STABLE, shouldn't get here. */
1282 LASSERT(!(cmd & OBD_BRW_READ));
Peng Taod7e09d02013-05-02 16:46:55 +08001283 obj = opg->ops_cl.cpl_obj;
1284
1285 cl_object_attr_lock(obj);
1286 result = cl_object_attr_get(env, obj, attr);
1287 cl_object_attr_unlock(obj);
1288 if (result < 0)
1289 return result;
1290 kms = attr->cat_kms;
1291 if (cl_offset(obj, page->cp_index) >= kms)
1292 /* catch race with truncate */
1293 return 0;
1294 else if (cl_offset(obj, page->cp_index + 1) > kms)
1295 /* catch sub-page write at end of file */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001296 return kms % PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001297 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001298 return PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001299}
1300
1301static int osc_completion(const struct lu_env *env, struct osc_async_page *oap,
1302 int cmd, int rc)
1303{
Chris Hanna29ac6842015-06-03 10:23:42 -04001304 struct osc_page *opg = oap2osc_page(oap);
1305 struct cl_page *page = cl_page_top(oap2cl_page(oap));
1306 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
1307 enum cl_req_type crt;
Peng Taod7e09d02013-05-02 16:46:55 +08001308 int srvlock;
1309
Peng Taod7e09d02013-05-02 16:46:55 +08001310 cmd &= ~OBD_BRW_NOQUOTA;
1311 LASSERT(equi(page->cp_state == CPS_PAGEIN, cmd == OBD_BRW_READ));
1312 LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
1313 LASSERT(opg->ops_transfer_pinned);
1314
1315 /*
1316 * page->cp_req can be NULL if io submission failed before
1317 * cl_req was allocated.
1318 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001319 if (page->cp_req)
Peng Taod7e09d02013-05-02 16:46:55 +08001320 cl_req_page_done(env, page);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001321 LASSERT(!page->cp_req);
Peng Taod7e09d02013-05-02 16:46:55 +08001322
1323 crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
1324 /* Clear opg->ops_transfer_pinned before VM lock is released. */
1325 opg->ops_transfer_pinned = 0;
1326
1327 spin_lock(&obj->oo_seatbelt);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001328 LASSERT(opg->ops_submitter);
Peng Taod7e09d02013-05-02 16:46:55 +08001329 LASSERT(!list_empty(&opg->ops_inflight));
1330 list_del_init(&opg->ops_inflight);
1331 opg->ops_submitter = NULL;
1332 spin_unlock(&obj->oo_seatbelt);
1333
1334 opg->ops_submit_time = 0;
1335 srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
1336
1337 /* statistic */
1338 if (rc == 0 && srvlock) {
Chris Hanna29ac6842015-06-03 10:23:42 -04001339 struct lu_device *ld = opg->ops_cl.cpl_obj->co_lu.lo_dev;
Peng Taod7e09d02013-05-02 16:46:55 +08001340 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
1341 int bytes = oap->oap_count;
1342
1343 if (crt == CRT_READ)
1344 stats->os_lockless_reads += bytes;
1345 else
1346 stats->os_lockless_writes += bytes;
1347 }
1348
1349 /*
1350 * This has to be the last operation with the page, as locks are
1351 * released in cl_page_completion() and nothing except for the
1352 * reference counter protects page from concurrent reclaim.
1353 */
1354 lu_ref_del(&page->cp_reference, "transfer", page);
1355
1356 cl_page_completion(env, page, crt, rc);
1357
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001358 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001359}
1360
1361#define OSC_DUMP_GRANT(cli, fmt, args...) do { \
1362 struct client_obd *__tmp = (cli); \
1363 CDEBUG(D_CACHE, "%s: { dirty: %ld/%ld dirty_pages: %d/%d " \
Oleg Drokinc52f69c2013-06-03 21:40:43 +08001364 "dropped: %ld avail: %ld, reserved: %ld, flight: %d } " fmt, \
Peng Taod7e09d02013-05-02 16:46:55 +08001365 __tmp->cl_import->imp_obd->obd_name, \
1366 __tmp->cl_dirty, __tmp->cl_dirty_max, \
1367 atomic_read(&obd_dirty_pages), obd_max_dirty_pages, \
Peng Taod7e09d02013-05-02 16:46:55 +08001368 __tmp->cl_lost_grant, __tmp->cl_avail_grant, \
1369 __tmp->cl_reserved_grant, __tmp->cl_w_in_flight, ##args); \
1370} while (0)
1371
1372/* caller must hold loi_list_lock */
1373static void osc_consume_write_grant(struct client_obd *cli,
1374 struct brw_page *pga)
1375{
Li Xi5e42bc92014-04-27 13:07:06 -04001376 assert_spin_locked(&cli->cl_loi_list_lock.lock);
Peng Taod7e09d02013-05-02 16:46:55 +08001377 LASSERT(!(pga->flag & OBD_BRW_FROM_GRANT));
1378 atomic_inc(&obd_dirty_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 cli->cl_dirty += PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001380 pga->flag |= OBD_BRW_FROM_GRANT;
1381 CDEBUG(D_CACHE, "using %lu grant credits for brw %p page %p\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 PAGE_SIZE, pga, pga->pg);
Peng Taod7e09d02013-05-02 16:46:55 +08001383 osc_update_next_shrink(cli);
1384}
1385
1386/* the companion to osc_consume_write_grant, called when a brw has completed.
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001387 * must be called with the loi lock held.
1388 */
Peng Taod7e09d02013-05-02 16:46:55 +08001389static void osc_release_write_grant(struct client_obd *cli,
1390 struct brw_page *pga)
1391{
Li Xi5e42bc92014-04-27 13:07:06 -04001392 assert_spin_locked(&cli->cl_loi_list_lock.lock);
Peng Taod7e09d02013-05-02 16:46:55 +08001393 if (!(pga->flag & OBD_BRW_FROM_GRANT)) {
Peng Taod7e09d02013-05-02 16:46:55 +08001394 return;
1395 }
1396
1397 pga->flag &= ~OBD_BRW_FROM_GRANT;
1398 atomic_dec(&obd_dirty_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 cli->cl_dirty -= PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001400 if (pga->flag & OBD_BRW_NOCACHE) {
1401 pga->flag &= ~OBD_BRW_NOCACHE;
1402 atomic_dec(&obd_dirty_transit_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001403 cli->cl_dirty_transit -= PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001404 }
Peng Taod7e09d02013-05-02 16:46:55 +08001405}
1406
1407/**
1408 * To avoid sleeping with object lock held, it's good for us allocate enough
1409 * grants before entering into critical section.
1410 *
1411 * client_obd_list_lock held by caller
1412 */
1413static int osc_reserve_grant(struct client_obd *cli, unsigned int bytes)
1414{
1415 int rc = -EDQUOT;
1416
1417 if (cli->cl_avail_grant >= bytes) {
Chris Hanna29ac6842015-06-03 10:23:42 -04001418 cli->cl_avail_grant -= bytes;
Peng Taod7e09d02013-05-02 16:46:55 +08001419 cli->cl_reserved_grant += bytes;
1420 rc = 0;
1421 }
1422 return rc;
1423}
1424
1425static void __osc_unreserve_grant(struct client_obd *cli,
1426 unsigned int reserved, unsigned int unused)
1427{
1428 /* it's quite normal for us to get more grant than reserved.
1429 * Thinking about a case that two extents merged by adding a new
1430 * chunk, we can save one extent tax. If extent tax is greater than
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001431 * one chunk, we can save more grant by adding a new chunk
1432 */
Peng Taod7e09d02013-05-02 16:46:55 +08001433 cli->cl_reserved_grant -= reserved;
1434 if (unused > reserved) {
1435 cli->cl_avail_grant += reserved;
1436 cli->cl_lost_grant += unused - reserved;
1437 } else {
1438 cli->cl_avail_grant += unused;
1439 }
1440}
1441
Shraddha Barke6ef3f3c2015-12-18 02:40:28 +05301442static void osc_unreserve_grant(struct client_obd *cli,
1443 unsigned int reserved, unsigned int unused)
Peng Taod7e09d02013-05-02 16:46:55 +08001444{
1445 client_obd_list_lock(&cli->cl_loi_list_lock);
1446 __osc_unreserve_grant(cli, reserved, unused);
1447 if (unused > 0)
1448 osc_wake_cache_waiters(cli);
1449 client_obd_list_unlock(&cli->cl_loi_list_lock);
1450}
1451
1452/**
1453 * Free grant after IO is finished or canceled.
1454 *
1455 * @lost_grant is used to remember how many grants we have allocated but not
1456 * used, we should return these grants to OST. There're two cases where grants
1457 * can be lost:
1458 * 1. truncate;
1459 * 2. blocksize at OST is less than PAGE_CACHE_SIZE and a partial page was
1460 * written. In this case OST may use less chunks to serve this partial
1461 * write. OSTs don't actually know the page size on the client side. so
1462 * clients have to calculate lost grant by the blocksize on the OST.
1463 * See filter_grant_check() for details.
1464 */
1465static void osc_free_grant(struct client_obd *cli, unsigned int nr_pages,
1466 unsigned int lost_grant)
1467{
1468 int grant = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
1469
1470 client_obd_list_lock(&cli->cl_loi_list_lock);
1471 atomic_sub(nr_pages, &obd_dirty_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001472 cli->cl_dirty -= nr_pages << PAGE_SHIFT;
Peng Taod7e09d02013-05-02 16:46:55 +08001473 cli->cl_lost_grant += lost_grant;
1474 if (cli->cl_avail_grant < grant && cli->cl_lost_grant >= grant) {
1475 /* borrow some grant from truncate to avoid the case that
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001476 * truncate uses up all avail grant
1477 */
Peng Taod7e09d02013-05-02 16:46:55 +08001478 cli->cl_lost_grant -= grant;
1479 cli->cl_avail_grant += grant;
1480 }
1481 osc_wake_cache_waiters(cli);
1482 client_obd_list_unlock(&cli->cl_loi_list_lock);
1483 CDEBUG(D_CACHE, "lost %u grant: %lu avail: %lu dirty: %lu\n",
1484 lost_grant, cli->cl_lost_grant,
1485 cli->cl_avail_grant, cli->cl_dirty);
1486}
1487
1488/**
1489 * The companion to osc_enter_cache(), called when @oap is no longer part of
1490 * the dirty accounting due to error.
1491 */
1492static void osc_exit_cache(struct client_obd *cli, struct osc_async_page *oap)
1493{
1494 client_obd_list_lock(&cli->cl_loi_list_lock);
1495 osc_release_write_grant(cli, &oap->oap_brw_page);
1496 client_obd_list_unlock(&cli->cl_loi_list_lock);
1497}
1498
1499/**
1500 * Non-blocking version of osc_enter_cache() that consumes grant only when it
1501 * is available.
1502 */
1503static int osc_enter_cache_try(struct client_obd *cli,
1504 struct osc_async_page *oap,
1505 int bytes, int transient)
1506{
1507 int rc;
1508
1509 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1510
1511 rc = osc_reserve_grant(cli, bytes);
1512 if (rc < 0)
1513 return 0;
1514
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001515 if (cli->cl_dirty + PAGE_SIZE <= cli->cl_dirty_max &&
Oleg Drokinc52f69c2013-06-03 21:40:43 +08001516 atomic_read(&obd_dirty_pages) + 1 <= obd_max_dirty_pages) {
Peng Taod7e09d02013-05-02 16:46:55 +08001517 osc_consume_write_grant(cli, &oap->oap_brw_page);
1518 if (transient) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001519 cli->cl_dirty_transit += PAGE_SIZE;
Peng Taod7e09d02013-05-02 16:46:55 +08001520 atomic_inc(&obd_dirty_transit_pages);
1521 oap->oap_brw_flags |= OBD_BRW_NOCACHE;
1522 }
1523 rc = 1;
1524 } else {
1525 __osc_unreserve_grant(cli, bytes, bytes);
1526 rc = 0;
1527 }
1528 return rc;
1529}
1530
1531static int ocw_granted(struct client_obd *cli, struct osc_cache_waiter *ocw)
1532{
1533 int rc;
Mike Rapoport50ffcb72015-10-13 16:03:40 +03001534
Peng Taod7e09d02013-05-02 16:46:55 +08001535 client_obd_list_lock(&cli->cl_loi_list_lock);
1536 rc = list_empty(&ocw->ocw_entry);
1537 client_obd_list_unlock(&cli->cl_loi_list_lock);
1538 return rc;
1539}
1540
1541/**
1542 * The main entry to reserve dirty page accounting. Usually the grant reserved
1543 * in this function will be freed in bulk in osc_free_grant() unless it fails
1544 * to add osc cache, in that case, it will be freed in osc_exit_cache().
1545 *
1546 * The process will be put into sleep if it's already run out of grant.
1547 */
1548static int osc_enter_cache(const struct lu_env *env, struct client_obd *cli,
1549 struct osc_async_page *oap, int bytes)
1550{
1551 struct osc_object *osc = oap->oap_obj;
Chris Hanna29ac6842015-06-03 10:23:42 -04001552 struct lov_oinfo *loi = osc->oo_oinfo;
Peng Taod7e09d02013-05-02 16:46:55 +08001553 struct osc_cache_waiter ocw;
1554 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
1555 int rc = -EDQUOT;
Peng Taod7e09d02013-05-02 16:46:55 +08001556
1557 OSC_DUMP_GRANT(cli, "need:%d.\n", bytes);
1558
1559 client_obd_list_lock(&cli->cl_loi_list_lock);
1560
1561 /* force the caller to try sync io. this can jump the list
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001562 * of queued writes and create a discontiguous rpc stream
1563 */
Peng Taod7e09d02013-05-02 16:46:55 +08001564 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_NO_GRANT) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001565 cli->cl_dirty_max < PAGE_SIZE ||
Julia Lawall490e0e82014-11-09 10:06:32 +01001566 cli->cl_ar.ar_force_sync || loi->loi_ar.ar_force_sync) {
1567 rc = -EDQUOT;
1568 goto out;
1569 }
Peng Taod7e09d02013-05-02 16:46:55 +08001570
1571 /* Hopefully normal case - cache space and write credits available */
Julia Lawall490e0e82014-11-09 10:06:32 +01001572 if (osc_enter_cache_try(cli, oap, bytes, 0)) {
1573 rc = 0;
1574 goto out;
1575 }
Peng Taod7e09d02013-05-02 16:46:55 +08001576
1577 /* We can get here for two reasons: too many dirty pages in cache, or
1578 * run out of grants. In both cases we should write dirty pages out.
1579 * Adding a cache waiter will trigger urgent write-out no matter what
1580 * RPC size will be.
1581 * The exiting condition is no avail grants and no dirty pages caching,
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001582 * that really means there is no space on the OST.
1583 */
Peng Taod7e09d02013-05-02 16:46:55 +08001584 init_waitqueue_head(&ocw.ocw_waitq);
1585 ocw.ocw_oap = oap;
1586 ocw.ocw_grant = bytes;
1587 while (cli->cl_dirty > 0 || cli->cl_w_in_flight > 0) {
1588 list_add_tail(&ocw.ocw_entry, &cli->cl_cache_waiters);
1589 ocw.ocw_rc = 0;
1590 client_obd_list_unlock(&cli->cl_loi_list_lock);
1591
1592 osc_io_unplug_async(env, cli, NULL);
1593
1594 CDEBUG(D_CACHE, "%s: sleeping for cache space @ %p for %p\n",
1595 cli->cl_import->imp_obd->obd_name, &ocw, oap);
1596
1597 rc = l_wait_event(ocw.ocw_waitq, ocw_granted(cli, &ocw), &lwi);
1598
1599 client_obd_list_lock(&cli->cl_loi_list_lock);
1600
1601 /* l_wait_event is interrupted by signal */
1602 if (rc < 0) {
1603 list_del_init(&ocw.ocw_entry);
Julia Lawall490e0e82014-11-09 10:06:32 +01001604 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08001605 }
1606
1607 LASSERT(list_empty(&ocw.ocw_entry));
1608 rc = ocw.ocw_rc;
1609
1610 if (rc != -EDQUOT)
Julia Lawall490e0e82014-11-09 10:06:32 +01001611 goto out;
1612 if (osc_enter_cache_try(cli, oap, bytes, 0)) {
1613 rc = 0;
1614 goto out;
1615 }
Peng Taod7e09d02013-05-02 16:46:55 +08001616 }
Peng Taod7e09d02013-05-02 16:46:55 +08001617out:
1618 client_obd_list_unlock(&cli->cl_loi_list_lock);
1619 OSC_DUMP_GRANT(cli, "returned %d.\n", rc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001620 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08001621}
1622
1623/* caller must hold loi_list_lock */
1624void osc_wake_cache_waiters(struct client_obd *cli)
1625{
1626 struct list_head *l, *tmp;
1627 struct osc_cache_waiter *ocw;
1628
Peng Taod7e09d02013-05-02 16:46:55 +08001629 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
1630 ocw = list_entry(l, struct osc_cache_waiter, ocw_entry);
1631 list_del_init(&ocw->ocw_entry);
1632
1633 ocw->ocw_rc = -EDQUOT;
1634 /* we can't dirty more */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001635 if ((cli->cl_dirty + PAGE_SIZE > cli->cl_dirty_max) ||
Oleg Drokinc52f69c2013-06-03 21:40:43 +08001636 (atomic_read(&obd_dirty_pages) + 1 >
1637 obd_max_dirty_pages)) {
Joe Perches2d00bd12014-11-23 11:28:50 -08001638 CDEBUG(D_CACHE, "no dirty room: dirty: %ld osc max %ld, sys max %d\n",
1639 cli->cl_dirty,
Peng Taod7e09d02013-05-02 16:46:55 +08001640 cli->cl_dirty_max, obd_max_dirty_pages);
1641 goto wakeup;
1642 }
1643
1644 ocw->ocw_rc = 0;
1645 if (!osc_enter_cache_try(cli, ocw->ocw_oap, ocw->ocw_grant, 0))
1646 ocw->ocw_rc = -EDQUOT;
1647
1648wakeup:
1649 CDEBUG(D_CACHE, "wake up %p for oap %p, avail grant %ld, %d\n",
1650 ocw, ocw->ocw_oap, cli->cl_avail_grant, ocw->ocw_rc);
1651
1652 wake_up(&ocw->ocw_waitq);
1653 }
Peng Taod7e09d02013-05-02 16:46:55 +08001654}
1655
1656static int osc_max_rpc_in_flight(struct client_obd *cli, struct osc_object *osc)
1657{
1658 int hprpc = !!list_empty(&osc->oo_hp_exts);
Mike Rapoport50ffcb72015-10-13 16:03:40 +03001659
Peng Taod7e09d02013-05-02 16:46:55 +08001660 return rpcs_in_flight(cli) >= cli->cl_max_rpcs_in_flight + hprpc;
1661}
1662
1663/* This maintains the lists of pending pages to read/write for a given object
1664 * (lop). This is used by osc_check_rpcs->osc_next_obj() and osc_list_maint()
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001665 * to quickly find objects that are ready to send an RPC.
1666 */
Peng Taod7e09d02013-05-02 16:46:55 +08001667static int osc_makes_rpc(struct client_obd *cli, struct osc_object *osc,
1668 int cmd)
1669{
1670 int invalid_import = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001671
1672 /* if we have an invalid import we want to drain the queued pages
1673 * by forcing them through rpcs that immediately fail and complete
1674 * the pages. recovery relies on this to empty the queued pages
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001675 * before canceling the locks and evicting down the llite pages
1676 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001677 if (!cli->cl_import || cli->cl_import->imp_invalid)
Peng Taod7e09d02013-05-02 16:46:55 +08001678 invalid_import = 1;
1679
1680 if (cmd & OBD_BRW_WRITE) {
1681 if (atomic_read(&osc->oo_nr_writes) == 0)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001682 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001683 if (invalid_import) {
1684 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001685 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001686 }
1687 if (!list_empty(&osc->oo_hp_exts)) {
1688 CDEBUG(D_CACHE, "high prio request forcing RPC\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001689 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001690 }
1691 if (!list_empty(&osc->oo_urgent_exts)) {
1692 CDEBUG(D_CACHE, "urgent request forcing RPC\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001693 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001694 }
1695 /* trigger a write rpc stream as long as there are dirtiers
1696 * waiting for space. as they're waiting, they're not going to
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001697 * create more pages to coalesce with what's waiting..
1698 */
Peng Taod7e09d02013-05-02 16:46:55 +08001699 if (!list_empty(&cli->cl_cache_waiters)) {
1700 CDEBUG(D_CACHE, "cache waiters forcing RPC\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001701 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001702 }
1703 if (atomic_read(&osc->oo_nr_writes) >=
1704 cli->cl_max_pages_per_rpc)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001705 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001706 } else {
1707 if (atomic_read(&osc->oo_nr_reads) == 0)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001708 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001709 if (invalid_import) {
1710 CDEBUG(D_CACHE, "invalid import forcing RPC\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001711 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001712 }
1713 /* all read are urgent. */
1714 if (!list_empty(&osc->oo_reading_exts))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001715 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001716 }
1717
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001718 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001719}
1720
1721static void osc_update_pending(struct osc_object *obj, int cmd, int delta)
1722{
1723 struct client_obd *cli = osc_cli(obj);
Mike Rapoport50ffcb72015-10-13 16:03:40 +03001724
Peng Taod7e09d02013-05-02 16:46:55 +08001725 if (cmd & OBD_BRW_WRITE) {
1726 atomic_add(delta, &obj->oo_nr_writes);
1727 atomic_add(delta, &cli->cl_pending_w_pages);
1728 LASSERT(atomic_read(&obj->oo_nr_writes) >= 0);
1729 } else {
1730 atomic_add(delta, &obj->oo_nr_reads);
1731 atomic_add(delta, &cli->cl_pending_r_pages);
1732 LASSERT(atomic_read(&obj->oo_nr_reads) >= 0);
1733 }
1734 OSC_IO_DEBUG(obj, "update pending cmd %d delta %d.\n", cmd, delta);
1735}
1736
1737static int osc_makes_hprpc(struct osc_object *obj)
1738{
1739 return !list_empty(&obj->oo_hp_exts);
1740}
1741
1742static void on_list(struct list_head *item, struct list_head *list, int should_be_on)
1743{
1744 if (list_empty(item) && should_be_on)
1745 list_add_tail(item, list);
1746 else if (!list_empty(item) && !should_be_on)
1747 list_del_init(item);
1748}
1749
1750/* maintain the osc's cli list membership invariants so that osc_send_oap_rpc
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001751 * can find pages to build into rpcs quickly
1752 */
Peng Taod7e09d02013-05-02 16:46:55 +08001753static int __osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1754{
1755 if (osc_makes_hprpc(osc)) {
1756 /* HP rpc */
1757 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list, 0);
1758 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 1);
1759 } else {
1760 on_list(&osc->oo_hp_ready_item, &cli->cl_loi_hp_ready_list, 0);
1761 on_list(&osc->oo_ready_item, &cli->cl_loi_ready_list,
1762 osc_makes_rpc(cli, osc, OBD_BRW_WRITE) ||
1763 osc_makes_rpc(cli, osc, OBD_BRW_READ));
1764 }
1765
1766 on_list(&osc->oo_write_item, &cli->cl_loi_write_list,
1767 atomic_read(&osc->oo_nr_writes) > 0);
1768
1769 on_list(&osc->oo_read_item, &cli->cl_loi_read_list,
1770 atomic_read(&osc->oo_nr_reads) > 0);
1771
1772 return osc_is_ready(osc);
1773}
1774
1775static int osc_list_maint(struct client_obd *cli, struct osc_object *osc)
1776{
1777 int is_ready;
1778
1779 client_obd_list_lock(&cli->cl_loi_list_lock);
1780 is_ready = __osc_list_maint(cli, osc);
1781 client_obd_list_unlock(&cli->cl_loi_list_lock);
1782
1783 return is_ready;
1784}
1785
Masanari Iida11d66e82013-12-14 02:24:04 +09001786/* this is trying to propagate async writeback errors back up to the
Peng Taod7e09d02013-05-02 16:46:55 +08001787 * application. As an async write fails we record the error code for later if
1788 * the app does an fsync. As long as errors persist we force future rpcs to be
1789 * sync so that the app can get a sync error and break the cycle of queueing
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001790 * pages for which writeback will fail.
1791 */
Peng Taod7e09d02013-05-02 16:46:55 +08001792static void osc_process_ar(struct osc_async_rc *ar, __u64 xid,
1793 int rc)
1794{
1795 if (rc) {
1796 if (!ar->ar_rc)
1797 ar->ar_rc = rc;
1798
1799 ar->ar_force_sync = 1;
1800 ar->ar_min_xid = ptlrpc_sample_next_xid();
1801 return;
1802
1803 }
1804
1805 if (ar->ar_force_sync && (xid >= ar->ar_min_xid))
1806 ar->ar_force_sync = 0;
1807}
1808
Peng Taod7e09d02013-05-02 16:46:55 +08001809/* this must be called holding the loi list lock to give coverage to exit_cache,
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001810 * async_flag maintenance, and oap_request
1811 */
Peng Taod7e09d02013-05-02 16:46:55 +08001812static void osc_ap_completion(const struct lu_env *env, struct client_obd *cli,
1813 struct osc_async_page *oap, int sent, int rc)
1814{
1815 struct osc_object *osc = oap->oap_obj;
Chris Hanna29ac6842015-06-03 10:23:42 -04001816 struct lov_oinfo *loi = osc->oo_oinfo;
Peng Taod7e09d02013-05-02 16:46:55 +08001817 __u64 xid = 0;
1818
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001819 if (oap->oap_request) {
Peng Taod7e09d02013-05-02 16:46:55 +08001820 xid = ptlrpc_req_xid(oap->oap_request);
1821 ptlrpc_req_finished(oap->oap_request);
1822 oap->oap_request = NULL;
1823 }
1824
1825 /* As the transfer for this page is being done, clear the flags */
1826 spin_lock(&oap->oap_lock);
1827 oap->oap_async_flags = 0;
1828 spin_unlock(&oap->oap_lock);
1829 oap->oap_interrupted = 0;
1830
1831 if (oap->oap_cmd & OBD_BRW_WRITE && xid > 0) {
1832 client_obd_list_lock(&cli->cl_loi_list_lock);
1833 osc_process_ar(&cli->cl_ar, xid, rc);
1834 osc_process_ar(&loi->loi_ar, xid, rc);
1835 client_obd_list_unlock(&cli->cl_loi_list_lock);
1836 }
1837
1838 rc = osc_completion(env, oap, oap->oap_cmd, rc);
1839 if (rc)
1840 CERROR("completion on oap %p obj %p returns %d.\n",
1841 oap, osc, rc);
Peng Taod7e09d02013-05-02 16:46:55 +08001842}
1843
1844/**
1845 * Try to add extent to one RPC. We need to think about the following things:
1846 * - # of pages must not be over max_pages_per_rpc
1847 * - extent must be compatible with previous ones
1848 */
1849static int try_to_add_extent_for_io(struct client_obd *cli,
1850 struct osc_extent *ext, struct list_head *rpclist,
1851 int *pc, unsigned int *max_pages)
1852{
1853 struct osc_extent *tmp;
Alexander Boykoc00266e2015-02-01 21:52:01 -05001854 struct osc_async_page *oap = list_first_entry(&ext->oe_pages,
1855 struct osc_async_page,
1856 oap_pending_item);
Peng Taod7e09d02013-05-02 16:46:55 +08001857
1858 EASSERT((ext->oe_state == OES_CACHE || ext->oe_state == OES_LOCK_DONE),
1859 ext);
1860
1861 *max_pages = max(ext->oe_mppr, *max_pages);
1862 if (*pc + ext->oe_nr_pages > *max_pages)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001863 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001864
1865 list_for_each_entry(tmp, rpclist, oe_link) {
Alexander Boykoc00266e2015-02-01 21:52:01 -05001866 struct osc_async_page *oap2;
1867
1868 oap2 = list_first_entry(&tmp->oe_pages, struct osc_async_page,
1869 oap_pending_item);
Peng Taod7e09d02013-05-02 16:46:55 +08001870 EASSERT(tmp->oe_owner == current, tmp);
Alexander Boykoc00266e2015-02-01 21:52:01 -05001871 if (oap2cl_page(oap)->cp_type != oap2cl_page(oap2)->cp_type) {
1872 CDEBUG(D_CACHE, "Do not permit different type of IO"
1873 " for a same RPC\n");
1874 return 0;
1875 }
Peng Taod7e09d02013-05-02 16:46:55 +08001876
1877 if (tmp->oe_srvlock != ext->oe_srvlock ||
1878 !tmp->oe_grants != !ext->oe_grants)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001879 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001880
1881 /* remove break for strict check */
1882 break;
1883 }
1884
1885 *pc += ext->oe_nr_pages;
1886 list_move_tail(&ext->oe_link, rpclist);
1887 ext->oe_owner = current;
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001888 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +08001889}
1890
1891/**
1892 * In order to prevent multiple ptlrpcd from breaking contiguous extents,
1893 * get_write_extent() takes all appropriate extents in atomic.
1894 *
1895 * The following policy is used to collect extents for IO:
1896 * 1. Add as many HP extents as possible;
1897 * 2. Add the first urgent extent in urgent extent list and take it out of
1898 * urgent list;
1899 * 3. Add subsequent extents of this urgent extent;
1900 * 4. If urgent list is not empty, goto 2;
1901 * 5. Traverse the extent tree from the 1st extent;
1902 * 6. Above steps exit if there is no space in this RPC.
1903 */
1904static int get_write_extents(struct osc_object *obj, struct list_head *rpclist)
1905{
1906 struct client_obd *cli = osc_cli(obj);
1907 struct osc_extent *ext;
Bhaktipriya Shridhar4a81ce52016-03-12 01:37:35 +05301908 struct osc_extent *temp;
Peng Taod7e09d02013-05-02 16:46:55 +08001909 int page_count = 0;
1910 unsigned int max_pages = cli->cl_max_pages_per_rpc;
1911
1912 LASSERT(osc_object_is_locked(obj));
Bhaktipriya Shridhar4a81ce52016-03-12 01:37:35 +05301913 list_for_each_entry_safe(ext, temp, &obj->oo_hp_exts, oe_link) {
Peng Taod7e09d02013-05-02 16:46:55 +08001914 LASSERT(ext->oe_state == OES_CACHE);
1915 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1916 &max_pages))
1917 return page_count;
1918 EASSERT(ext->oe_nr_pages <= max_pages, ext);
1919 }
1920 if (page_count == max_pages)
1921 return page_count;
1922
1923 while (!list_empty(&obj->oo_urgent_exts)) {
1924 ext = list_entry(obj->oo_urgent_exts.next,
Oleg Drokin79910d72016-02-26 01:50:03 -05001925 struct osc_extent, oe_link);
Peng Taod7e09d02013-05-02 16:46:55 +08001926 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1927 &max_pages))
1928 return page_count;
1929
1930 if (!ext->oe_intree)
1931 continue;
1932
1933 while ((ext = next_extent(ext)) != NULL) {
1934 if ((ext->oe_state != OES_CACHE) ||
1935 (!list_empty(&ext->oe_link) &&
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001936 ext->oe_owner))
Peng Taod7e09d02013-05-02 16:46:55 +08001937 continue;
1938
1939 if (!try_to_add_extent_for_io(cli, ext, rpclist,
1940 &page_count, &max_pages))
1941 return page_count;
1942 }
1943 }
1944 if (page_count == max_pages)
1945 return page_count;
1946
1947 ext = first_extent(obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001948 while (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08001949 if ((ext->oe_state != OES_CACHE) ||
1950 /* this extent may be already in current rpclist */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05001951 (!list_empty(&ext->oe_link) && ext->oe_owner)) {
Peng Taod7e09d02013-05-02 16:46:55 +08001952 ext = next_extent(ext);
1953 continue;
1954 }
1955
1956 if (!try_to_add_extent_for_io(cli, ext, rpclist, &page_count,
1957 &max_pages))
1958 return page_count;
1959
1960 ext = next_extent(ext);
1961 }
1962 return page_count;
1963}
1964
1965static int
1966osc_send_write_rpc(const struct lu_env *env, struct client_obd *cli,
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04001967 struct osc_object *osc)
frank zagoa161de82015-12-23 12:32:11 -05001968 __must_hold(osc)
Peng Taod7e09d02013-05-02 16:46:55 +08001969{
1970 LIST_HEAD(rpclist);
1971 struct osc_extent *ext;
1972 struct osc_extent *tmp;
1973 struct osc_extent *first = NULL;
Oleg Drokin21aef7d2014-08-15 12:55:56 -04001974 u32 page_count = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001975 int srvlock = 0;
1976 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001977
1978 LASSERT(osc_object_is_locked(osc));
1979
1980 page_count = get_write_extents(osc, &rpclist);
1981 LASSERT(equi(page_count == 0, list_empty(&rpclist)));
1982
1983 if (list_empty(&rpclist))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08001984 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08001985
1986 osc_update_pending(osc, OBD_BRW_WRITE, -page_count);
1987
1988 list_for_each_entry(ext, &rpclist, oe_link) {
1989 LASSERT(ext->oe_state == OES_CACHE ||
1990 ext->oe_state == OES_LOCK_DONE);
1991 if (ext->oe_state == OES_CACHE)
1992 osc_extent_state_set(ext, OES_LOCKING);
1993 else
1994 osc_extent_state_set(ext, OES_RPC);
1995 }
1996
1997 /* we're going to grab page lock, so release object lock because
Oleg Drokin30aa9c52016-02-24 22:00:37 -05001998 * lock order is page lock -> object lock.
1999 */
Peng Taod7e09d02013-05-02 16:46:55 +08002000 osc_object_unlock(osc);
2001
2002 list_for_each_entry_safe(ext, tmp, &rpclist, oe_link) {
2003 if (ext->oe_state == OES_LOCKING) {
2004 rc = osc_extent_make_ready(env, ext);
2005 if (unlikely(rc < 0)) {
2006 list_del_init(&ext->oe_link);
2007 osc_extent_finish(env, ext, 0, rc);
2008 continue;
2009 }
2010 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002011 if (!first) {
Peng Taod7e09d02013-05-02 16:46:55 +08002012 first = ext;
2013 srvlock = ext->oe_srvlock;
2014 } else {
2015 LASSERT(srvlock == ext->oe_srvlock);
2016 }
2017 }
2018
2019 if (!list_empty(&rpclist)) {
2020 LASSERT(page_count > 0);
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002021 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_WRITE);
Peng Taod7e09d02013-05-02 16:46:55 +08002022 LASSERT(list_empty(&rpclist));
2023 }
2024
2025 osc_object_lock(osc);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002026 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002027}
2028
2029/**
2030 * prepare pages for ASYNC io and put pages in send queue.
2031 *
2032 * \param cmd OBD_BRW_* macroses
2033 * \param lop pending pages
2034 *
2035 * \return zero if no page added to send queue.
2036 * \return 1 if pages successfully added to send queue.
2037 * \return negative on errors.
2038 */
2039static int
2040osc_send_read_rpc(const struct lu_env *env, struct client_obd *cli,
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002041 struct osc_object *osc)
frank zagoa161de82015-12-23 12:32:11 -05002042 __must_hold(osc)
Peng Taod7e09d02013-05-02 16:46:55 +08002043{
2044 struct osc_extent *ext;
2045 struct osc_extent *next;
2046 LIST_HEAD(rpclist);
2047 int page_count = 0;
2048 unsigned int max_pages = cli->cl_max_pages_per_rpc;
2049 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002050
2051 LASSERT(osc_object_is_locked(osc));
Oleg Drokin79910d72016-02-26 01:50:03 -05002052 list_for_each_entry_safe(ext, next, &osc->oo_reading_exts, oe_link) {
Peng Taod7e09d02013-05-02 16:46:55 +08002053 EASSERT(ext->oe_state == OES_LOCK_DONE, ext);
2054 if (!try_to_add_extent_for_io(cli, ext, &rpclist, &page_count,
2055 &max_pages))
2056 break;
2057 osc_extent_state_set(ext, OES_RPC);
2058 EASSERT(ext->oe_nr_pages <= max_pages, ext);
2059 }
2060 LASSERT(page_count <= max_pages);
2061
2062 osc_update_pending(osc, OBD_BRW_READ, -page_count);
2063
2064 if (!list_empty(&rpclist)) {
2065 osc_object_unlock(osc);
2066
2067 LASSERT(page_count > 0);
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002068 rc = osc_build_rpc(env, cli, &rpclist, OBD_BRW_READ);
Peng Taod7e09d02013-05-02 16:46:55 +08002069 LASSERT(list_empty(&rpclist));
2070
2071 osc_object_lock(osc);
2072 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002073 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002074}
2075
2076#define list_to_obj(list, item) ({ \
2077 struct list_head *__tmp = (list)->next; \
Chris Hanna29ac6842015-06-03 10:23:42 -04002078 list_del_init(__tmp); \
Peng Taod7e09d02013-05-02 16:46:55 +08002079 list_entry(__tmp, struct osc_object, oo_##item); \
2080})
2081
2082/* This is called by osc_check_rpcs() to find which objects have pages that
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002083 * we could be sending. These lists are maintained by osc_makes_rpc().
2084 */
Peng Taod7e09d02013-05-02 16:46:55 +08002085static struct osc_object *osc_next_obj(struct client_obd *cli)
2086{
Peng Taod7e09d02013-05-02 16:46:55 +08002087 /* First return objects that have blocked locks so that they
2088 * will be flushed quickly and other clients can get the lock,
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002089 * then objects which have pages ready to be stuffed into RPCs
2090 */
Peng Taod7e09d02013-05-02 16:46:55 +08002091 if (!list_empty(&cli->cl_loi_hp_ready_list))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002092 return list_to_obj(&cli->cl_loi_hp_ready_list, hp_ready_item);
Peng Taod7e09d02013-05-02 16:46:55 +08002093 if (!list_empty(&cli->cl_loi_ready_list))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002094 return list_to_obj(&cli->cl_loi_ready_list, ready_item);
Peng Taod7e09d02013-05-02 16:46:55 +08002095
2096 /* then if we have cache waiters, return all objects with queued
2097 * writes. This is especially important when many small files
2098 * have filled up the cache and not been fired into rpcs because
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002099 * they don't pass the nr_pending/object threshold
2100 */
Peng Taod7e09d02013-05-02 16:46:55 +08002101 if (!list_empty(&cli->cl_cache_waiters) &&
2102 !list_empty(&cli->cl_loi_write_list))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002103 return list_to_obj(&cli->cl_loi_write_list, write_item);
Peng Taod7e09d02013-05-02 16:46:55 +08002104
2105 /* then return all queued objects when we have an invalid import
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002106 * so that they get flushed
2107 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002108 if (!cli->cl_import || cli->cl_import->imp_invalid) {
Peng Taod7e09d02013-05-02 16:46:55 +08002109 if (!list_empty(&cli->cl_loi_write_list))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002110 return list_to_obj(&cli->cl_loi_write_list, write_item);
Peng Taod7e09d02013-05-02 16:46:55 +08002111 if (!list_empty(&cli->cl_loi_read_list))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002112 return list_to_obj(&cli->cl_loi_read_list, read_item);
Peng Taod7e09d02013-05-02 16:46:55 +08002113 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002114 return NULL;
Peng Taod7e09d02013-05-02 16:46:55 +08002115}
2116
2117/* called with the loi list lock held */
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002118static void osc_check_rpcs(const struct lu_env *env, struct client_obd *cli)
frank zagoa161de82015-12-23 12:32:11 -05002119 __must_hold(&cli->cl_loi_list_lock)
Peng Taod7e09d02013-05-02 16:46:55 +08002120{
2121 struct osc_object *osc;
2122 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002123
2124 while ((osc = osc_next_obj(cli)) != NULL) {
2125 struct cl_object *obj = osc2cl(osc);
John L. Hammond631abc62013-07-25 01:17:30 +08002126 struct lu_ref_link link;
Peng Taod7e09d02013-05-02 16:46:55 +08002127
2128 OSC_IO_DEBUG(osc, "%lu in flight\n", rpcs_in_flight(cli));
2129
2130 if (osc_max_rpc_in_flight(cli, osc)) {
2131 __osc_list_maint(cli, osc);
2132 break;
2133 }
2134
2135 cl_object_get(obj);
2136 client_obd_list_unlock(&cli->cl_loi_list_lock);
John L. Hammond631abc62013-07-25 01:17:30 +08002137 lu_object_ref_add_at(&obj->co_lu, &link, "check",
2138 current);
Peng Taod7e09d02013-05-02 16:46:55 +08002139
2140 /* attempt some read/write balancing by alternating between
2141 * reads and writes in an object. The makes_rpc checks here
2142 * would be redundant if we were getting read/write work items
2143 * instead of objects. we don't want send_oap_rpc to drain a
2144 * partial read pending queue when we're given this object to
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002145 * do io on writes while there are cache waiters
2146 */
Peng Taod7e09d02013-05-02 16:46:55 +08002147 osc_object_lock(osc);
2148 if (osc_makes_rpc(cli, osc, OBD_BRW_WRITE)) {
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002149 rc = osc_send_write_rpc(env, cli, osc);
Peng Taod7e09d02013-05-02 16:46:55 +08002150 if (rc < 0) {
2151 CERROR("Write request failed with %d\n", rc);
2152
2153 /* osc_send_write_rpc failed, mostly because of
2154 * memory pressure.
2155 *
2156 * It can't break here, because if:
2157 * - a page was submitted by osc_io_submit, so
2158 * page locked;
2159 * - no request in flight
2160 * - no subsequent request
2161 * The system will be in live-lock state,
2162 * because there is no chance to call
2163 * osc_io_unplug() and osc_check_rpcs() any
2164 * more. pdflush can't help in this case,
2165 * because it might be blocked at grabbing
2166 * the page lock as we mentioned.
2167 *
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002168 * Anyway, continue to drain pages.
2169 */
Peng Taod7e09d02013-05-02 16:46:55 +08002170 /* break; */
2171 }
2172 }
2173 if (osc_makes_rpc(cli, osc, OBD_BRW_READ)) {
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002174 rc = osc_send_read_rpc(env, cli, osc);
Peng Taod7e09d02013-05-02 16:46:55 +08002175 if (rc < 0)
2176 CERROR("Read request failed with %d\n", rc);
2177 }
2178 osc_object_unlock(osc);
2179
2180 osc_list_maint(cli, osc);
John L. Hammond631abc62013-07-25 01:17:30 +08002181 lu_object_ref_del_at(&obj->co_lu, &link, "check",
2182 current);
Peng Taod7e09d02013-05-02 16:46:55 +08002183 cl_object_put(env, obj);
2184
2185 client_obd_list_lock(&cli->cl_loi_list_lock);
2186 }
2187}
2188
2189static int osc_io_unplug0(const struct lu_env *env, struct client_obd *cli,
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002190 struct osc_object *osc, int async)
Peng Taod7e09d02013-05-02 16:46:55 +08002191{
Peng Taod7e09d02013-05-02 16:46:55 +08002192 int rc = 0;
2193
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002194 if (osc && osc_list_maint(cli, osc) == 0)
Bobi Jamcad6faf2013-06-03 21:40:45 +08002195 return 0;
2196
2197 if (!async) {
2198 /* disable osc_lru_shrink() temporarily to avoid
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002199 * potential stack overrun problem. LU-2859
2200 */
Bobi Jamcad6faf2013-06-03 21:40:45 +08002201 atomic_inc(&cli->cl_lru_shrinkers);
2202 client_obd_list_lock(&cli->cl_loi_list_lock);
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002203 osc_check_rpcs(env, cli);
Bobi Jamcad6faf2013-06-03 21:40:45 +08002204 client_obd_list_unlock(&cli->cl_loi_list_lock);
2205 atomic_dec(&cli->cl_lru_shrinkers);
2206 } else {
2207 CDEBUG(D_CACHE, "Queue writeback work for client %p.\n", cli);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002208 LASSERT(cli->cl_writeback_work);
Bobi Jamcad6faf2013-06-03 21:40:45 +08002209 rc = ptlrpcd_queue_work(cli->cl_writeback_work);
Peng Taod7e09d02013-05-02 16:46:55 +08002210 }
Peng Taod7e09d02013-05-02 16:46:55 +08002211 return rc;
2212}
2213
2214static int osc_io_unplug_async(const struct lu_env *env,
Chris Hanna29ac6842015-06-03 10:23:42 -04002215 struct client_obd *cli, struct osc_object *osc)
Peng Taod7e09d02013-05-02 16:46:55 +08002216{
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002217 return osc_io_unplug0(env, cli, osc, 1);
Peng Taod7e09d02013-05-02 16:46:55 +08002218}
2219
2220void osc_io_unplug(const struct lu_env *env, struct client_obd *cli,
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002221 struct osc_object *osc)
Peng Taod7e09d02013-05-02 16:46:55 +08002222{
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002223 (void)osc_io_unplug0(env, cli, osc, 0);
Peng Taod7e09d02013-05-02 16:46:55 +08002224}
2225
2226int osc_prep_async_page(struct osc_object *osc, struct osc_page *ops,
2227 struct page *page, loff_t offset)
2228{
Chris Hanna29ac6842015-06-03 10:23:42 -04002229 struct obd_export *exp = osc_export(osc);
Peng Taod7e09d02013-05-02 16:46:55 +08002230 struct osc_async_page *oap = &ops->ops_oap;
Peng Taod7e09d02013-05-02 16:46:55 +08002231
2232 if (!page)
2233 return cfs_size_round(sizeof(*oap));
2234
2235 oap->oap_magic = OAP_MAGIC;
2236 oap->oap_cli = &exp->exp_obd->u.cli;
2237 oap->oap_obj = osc;
2238
2239 oap->oap_page = page;
2240 oap->oap_obj_off = offset;
2241 LASSERT(!(offset & ~CFS_PAGE_MASK));
2242
Peng Tao2eb90a72014-01-22 21:47:37 +08002243 if (!client_is_remote(exp) && capable(CFS_CAP_SYS_RESOURCE))
Peng Taod7e09d02013-05-02 16:46:55 +08002244 oap->oap_brw_flags = OBD_BRW_NOQUOTA;
2245
2246 INIT_LIST_HEAD(&oap->oap_pending_item);
2247 INIT_LIST_HEAD(&oap->oap_rpc_item);
2248
2249 spin_lock_init(&oap->oap_lock);
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07002250 CDEBUG(D_INFO, "oap %p page %p obj off %llu\n",
Peng Taod7e09d02013-05-02 16:46:55 +08002251 oap, page, oap->oap_obj_off);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002252 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002253}
2254
2255int osc_queue_async_io(const struct lu_env *env, struct cl_io *io,
2256 struct osc_page *ops)
2257{
2258 struct osc_io *oio = osc_env_io(env);
Chris Hanna29ac6842015-06-03 10:23:42 -04002259 struct osc_extent *ext = NULL;
Peng Taod7e09d02013-05-02 16:46:55 +08002260 struct osc_async_page *oap = &ops->ops_oap;
Chris Hanna29ac6842015-06-03 10:23:42 -04002261 struct client_obd *cli = oap->oap_cli;
2262 struct osc_object *osc = oap->oap_obj;
Peng Taod7e09d02013-05-02 16:46:55 +08002263 pgoff_t index;
Chris Hanna29ac6842015-06-03 10:23:42 -04002264 int grants = 0;
2265 int brw_flags = OBD_BRW_ASYNC;
2266 int cmd = OBD_BRW_WRITE;
2267 int need_release = 0;
2268 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002269
2270 if (oap->oap_magic != OAP_MAGIC)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002271 return -EINVAL;
Peng Taod7e09d02013-05-02 16:46:55 +08002272
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002273 if (!cli->cl_import || cli->cl_import->imp_invalid)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002274 return -EIO;
Peng Taod7e09d02013-05-02 16:46:55 +08002275
2276 if (!list_empty(&oap->oap_pending_item) ||
2277 !list_empty(&oap->oap_rpc_item))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002278 return -EBUSY;
Peng Taod7e09d02013-05-02 16:46:55 +08002279
2280 /* Set the OBD_BRW_SRVLOCK before the page is queued. */
2281 brw_flags |= ops->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
2282 if (!client_is_remote(osc_export(osc)) &&
Peng Tao2eb90a72014-01-22 21:47:37 +08002283 capable(CFS_CAP_SYS_RESOURCE)) {
Peng Taod7e09d02013-05-02 16:46:55 +08002284 brw_flags |= OBD_BRW_NOQUOTA;
2285 cmd |= OBD_BRW_NOQUOTA;
2286 }
2287
2288 /* check if the file's owner/group is over quota */
2289 if (!(cmd & OBD_BRW_NOQUOTA)) {
2290 struct cl_object *obj;
Chris Hanna29ac6842015-06-03 10:23:42 -04002291 struct cl_attr *attr;
Peng Taod7e09d02013-05-02 16:46:55 +08002292 unsigned int qid[MAXQUOTAS];
2293
2294 obj = cl_object_top(&osc->oo_cl);
2295 attr = &osc_env_info(env)->oti_attr;
2296
2297 cl_object_attr_lock(obj);
2298 rc = cl_object_attr_get(env, obj, attr);
2299 cl_object_attr_unlock(obj);
2300
2301 qid[USRQUOTA] = attr->cat_uid;
2302 qid[GRPQUOTA] = attr->cat_gid;
2303 if (rc == 0 && osc_quota_chkdq(cli, qid) == NO_QUOTA)
2304 rc = -EDQUOT;
2305 if (rc)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002306 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002307 }
2308
2309 oap->oap_cmd = cmd;
2310 oap->oap_page_off = ops->ops_from;
2311 oap->oap_count = ops->ops_to - ops->ops_from;
2312 oap->oap_async_flags = 0;
2313 oap->oap_brw_flags = brw_flags;
2314
2315 OSC_IO_DEBUG(osc, "oap %p page %p added for cmd %d\n",
2316 oap, oap->oap_page, oap->oap_cmd & OBD_BRW_RWMASK);
2317
2318 index = oap2cl_page(oap)->cp_index;
2319
2320 /* Add this page into extent by the following steps:
2321 * 1. if there exists an active extent for this IO, mostly this page
2322 * can be added to the active extent and sometimes we need to
Masanari Iida11d66e82013-12-14 02:24:04 +09002323 * expand extent to accommodate this page;
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002324 * 2. otherwise, a new extent will be allocated.
2325 */
Peng Taod7e09d02013-05-02 16:46:55 +08002326
2327 ext = oio->oi_active;
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002328 if (ext && ext->oe_start <= index && ext->oe_max_end >= index) {
Peng Taod7e09d02013-05-02 16:46:55 +08002329 /* one chunk plus extent overhead must be enough to write this
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002330 * page
2331 */
Peng Taod7e09d02013-05-02 16:46:55 +08002332 grants = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2333 if (ext->oe_end >= index)
2334 grants = 0;
2335
2336 /* it doesn't need any grant to dirty this page */
2337 client_obd_list_lock(&cli->cl_loi_list_lock);
2338 rc = osc_enter_cache_try(cli, oap, grants, 0);
2339 client_obd_list_unlock(&cli->cl_loi_list_lock);
2340 if (rc == 0) { /* try failed */
2341 grants = 0;
2342 need_release = 1;
2343 } else if (ext->oe_end < index) {
2344 int tmp = grants;
2345 /* try to expand this extent */
2346 rc = osc_extent_expand(ext, index, &tmp);
2347 if (rc < 0) {
2348 need_release = 1;
2349 /* don't free reserved grant */
2350 } else {
2351 OSC_EXTENT_DUMP(D_CACHE, ext,
2352 "expanded for %lu.\n", index);
2353 osc_unreserve_grant(cli, grants, tmp);
2354 grants = 0;
2355 }
2356 }
2357 rc = 0;
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002358 } else if (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002359 /* index is located outside of active extent */
2360 need_release = 1;
2361 }
2362 if (need_release) {
2363 osc_extent_release(env, ext);
2364 oio->oi_active = NULL;
2365 ext = NULL;
2366 }
2367
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002368 if (!ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002369 int tmp = (1 << cli->cl_chunkbits) + cli->cl_extent_tax;
2370
2371 /* try to find new extent to cover this page */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002372 LASSERT(!oio->oi_active);
Peng Taod7e09d02013-05-02 16:46:55 +08002373 /* we may have allocated grant for this page if we failed
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002374 * to expand the previous active extent.
2375 */
Peng Taod7e09d02013-05-02 16:46:55 +08002376 LASSERT(ergo(grants > 0, grants >= tmp));
2377
2378 rc = 0;
2379 if (grants == 0) {
2380 /* we haven't allocated grant for this page. */
2381 rc = osc_enter_cache(env, cli, oap, tmp);
2382 if (rc == 0)
2383 grants = tmp;
2384 }
2385
2386 tmp = grants;
2387 if (rc == 0) {
2388 ext = osc_extent_find(env, osc, index, &tmp);
2389 if (IS_ERR(ext)) {
2390 LASSERT(tmp == grants);
2391 osc_exit_cache(cli, oap);
2392 rc = PTR_ERR(ext);
2393 ext = NULL;
2394 } else {
2395 oio->oi_active = ext;
2396 }
2397 }
2398 if (grants > 0)
2399 osc_unreserve_grant(cli, grants, tmp);
2400 }
2401
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002402 LASSERT(ergo(rc == 0, ext));
2403 if (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002404 EASSERTF(ext->oe_end >= index && ext->oe_start <= index,
2405 ext, "index = %lu.\n", index);
2406 LASSERT((oap->oap_brw_flags & OBD_BRW_FROM_GRANT) != 0);
2407
2408 osc_object_lock(osc);
2409 if (ext->oe_nr_pages == 0)
2410 ext->oe_srvlock = ops->ops_srvlock;
2411 else
2412 LASSERT(ext->oe_srvlock == ops->ops_srvlock);
2413 ++ext->oe_nr_pages;
2414 list_add_tail(&oap->oap_pending_item, &ext->oe_pages);
2415 osc_object_unlock(osc);
2416 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002417 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002418}
2419
2420int osc_teardown_async_page(const struct lu_env *env,
2421 struct osc_object *obj, struct osc_page *ops)
2422{
2423 struct osc_async_page *oap = &ops->ops_oap;
Chris Hanna29ac6842015-06-03 10:23:42 -04002424 struct osc_extent *ext = NULL;
Peng Taod7e09d02013-05-02 16:46:55 +08002425 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002426
2427 LASSERT(oap->oap_magic == OAP_MAGIC);
2428
2429 CDEBUG(D_INFO, "teardown oap %p page %p at index %lu.\n",
2430 oap, ops, oap2cl_page(oap)->cp_index);
2431
2432 osc_object_lock(obj);
2433 if (!list_empty(&oap->oap_rpc_item)) {
2434 CDEBUG(D_CACHE, "oap %p is not in cache.\n", oap);
2435 rc = -EBUSY;
2436 } else if (!list_empty(&oap->oap_pending_item)) {
2437 ext = osc_extent_lookup(obj, oap2cl_page(oap)->cp_index);
2438 /* only truncated pages are allowed to be taken out.
2439 * See osc_extent_truncate() and osc_cache_truncate_start()
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002440 * for details.
2441 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002442 if (ext && ext->oe_state != OES_TRUNC) {
Peng Taod7e09d02013-05-02 16:46:55 +08002443 OSC_EXTENT_DUMP(D_ERROR, ext, "trunc at %lu.\n",
2444 oap2cl_page(oap)->cp_index);
2445 rc = -EBUSY;
2446 }
2447 }
2448 osc_object_unlock(obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002449 if (ext)
Peng Taod7e09d02013-05-02 16:46:55 +08002450 osc_extent_put(env, ext);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002451 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002452}
2453
2454/**
2455 * This is called when a page is picked up by kernel to write out.
2456 *
2457 * We should find out the corresponding extent and add the whole extent
2458 * into urgent list. The extent may be being truncated or used, handle it
2459 * carefully.
2460 */
2461int osc_flush_async_page(const struct lu_env *env, struct cl_io *io,
2462 struct osc_page *ops)
2463{
Chris Hanna29ac6842015-06-03 10:23:42 -04002464 struct osc_extent *ext = NULL;
2465 struct osc_object *obj = cl2osc(ops->ops_cl.cpl_obj);
2466 struct cl_page *cp = ops->ops_cl.cpl_page;
2467 pgoff_t index = cp->cp_index;
Peng Taod7e09d02013-05-02 16:46:55 +08002468 struct osc_async_page *oap = &ops->ops_oap;
2469 bool unplug = false;
2470 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002471
2472 osc_object_lock(obj);
2473 ext = osc_extent_lookup(obj, index);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002474 if (!ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002475 osc_extent_tree_dump(D_ERROR, obj);
2476 LASSERTF(0, "page index %lu is NOT covered.\n", index);
2477 }
2478
2479 switch (ext->oe_state) {
2480 case OES_RPC:
2481 case OES_LOCK_DONE:
2482 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(cp),
2483 "flush an in-rpc page?\n");
2484 LASSERT(0);
2485 break;
2486 case OES_LOCKING:
2487 /* If we know this extent is being written out, we should abort
2488 * so that the writer can make this page ready. Otherwise, there
2489 * exists a deadlock problem because other process can wait for
2490 * page writeback bit holding page lock; and meanwhile in
2491 * vvp_page_make_ready(), we need to grab page lock before
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002492 * really sending the RPC.
2493 */
Peng Taod7e09d02013-05-02 16:46:55 +08002494 case OES_TRUNC:
2495 /* race with truncate, page will be redirtied */
Ann Koehler15f13cd2014-02-28 21:16:44 -05002496 case OES_ACTIVE:
2497 /* The extent is active so we need to abort and let the caller
2498 * re-dirty the page. If we continued on here, and we were the
2499 * one making the extent active, we could deadlock waiting for
2500 * the page writeback to clear but it won't because the extent
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002501 * is active and won't be written out.
2502 */
Julia Lawall490e0e82014-11-09 10:06:32 +01002503 rc = -EAGAIN;
2504 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002505 default:
2506 break;
2507 }
2508
2509 rc = cl_page_prep(env, io, cl_page_top(cp), CRT_WRITE);
2510 if (rc)
Julia Lawall490e0e82014-11-09 10:06:32 +01002511 goto out;
Peng Taod7e09d02013-05-02 16:46:55 +08002512
2513 spin_lock(&oap->oap_lock);
2514 oap->oap_async_flags |= ASYNC_READY|ASYNC_URGENT;
2515 spin_unlock(&oap->oap_lock);
2516
2517 if (memory_pressure_get())
2518 ext->oe_memalloc = 1;
2519
2520 ext->oe_urgent = 1;
2521 if (ext->oe_state == OES_CACHE) {
2522 OSC_EXTENT_DUMP(D_CACHE, ext,
2523 "flush page %p make it urgent.\n", oap);
2524 if (list_empty(&ext->oe_link))
2525 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2526 unplug = true;
2527 }
2528 rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002529
2530out:
2531 osc_object_unlock(obj);
2532 osc_extent_put(env, ext);
2533 if (unplug)
2534 osc_io_unplug_async(env, osc_cli(obj), obj);
2535 return rc;
2536}
2537
2538/**
2539 * this is called when a sync waiter receives an interruption. Its job is to
2540 * get the caller woken as soon as possible. If its page hasn't been put in an
2541 * rpc yet it can dequeue immediately. Otherwise it has to mark the rpc as
2542 * desiring interruption which will forcefully complete the rpc once the rpc
2543 * has timed out.
2544 */
2545int osc_cancel_async_page(const struct lu_env *env, struct osc_page *ops)
2546{
2547 struct osc_async_page *oap = &ops->ops_oap;
Chris Hanna29ac6842015-06-03 10:23:42 -04002548 struct osc_object *obj = oap->oap_obj;
2549 struct client_obd *cli = osc_cli(obj);
2550 struct osc_extent *ext;
2551 struct osc_extent *found = NULL;
2552 struct list_head *plist;
Peng Taod7e09d02013-05-02 16:46:55 +08002553 pgoff_t index = oap2cl_page(oap)->cp_index;
Chris Hanna29ac6842015-06-03 10:23:42 -04002554 int rc = -EBUSY;
2555 int cmd;
Peng Taod7e09d02013-05-02 16:46:55 +08002556
2557 LASSERT(!oap->oap_interrupted);
2558 oap->oap_interrupted = 1;
2559
2560 /* Find out the caching extent */
2561 osc_object_lock(obj);
2562 if (oap->oap_cmd & OBD_BRW_WRITE) {
2563 plist = &obj->oo_urgent_exts;
Chris Hanna29ac6842015-06-03 10:23:42 -04002564 cmd = OBD_BRW_WRITE;
Peng Taod7e09d02013-05-02 16:46:55 +08002565 } else {
2566 plist = &obj->oo_reading_exts;
Chris Hanna29ac6842015-06-03 10:23:42 -04002567 cmd = OBD_BRW_READ;
Peng Taod7e09d02013-05-02 16:46:55 +08002568 }
2569 list_for_each_entry(ext, plist, oe_link) {
2570 if (ext->oe_start <= index && ext->oe_end >= index) {
2571 LASSERT(ext->oe_state == OES_LOCK_DONE);
2572 /* For OES_LOCK_DONE state extent, it has already held
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002573 * a refcount for RPC.
2574 */
Peng Taod7e09d02013-05-02 16:46:55 +08002575 found = osc_extent_get(ext);
2576 break;
2577 }
2578 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002579 if (found) {
Peng Taod7e09d02013-05-02 16:46:55 +08002580 list_del_init(&found->oe_link);
2581 osc_update_pending(obj, cmd, -found->oe_nr_pages);
2582 osc_object_unlock(obj);
2583
2584 osc_extent_finish(env, found, 0, -EINTR);
2585 osc_extent_put(env, found);
2586 rc = 0;
2587 } else {
2588 osc_object_unlock(obj);
2589 /* ok, it's been put in an rpc. only one oap gets a request
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002590 * reference
2591 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002592 if (oap->oap_request) {
Peng Taod7e09d02013-05-02 16:46:55 +08002593 ptlrpc_mark_interrupted(oap->oap_request);
2594 ptlrpcd_wake(oap->oap_request);
2595 ptlrpc_req_finished(oap->oap_request);
2596 oap->oap_request = NULL;
2597 }
2598 }
2599
2600 osc_list_maint(cli, obj);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002601 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +08002602}
2603
2604int osc_queue_sync_pages(const struct lu_env *env, struct osc_object *obj,
2605 struct list_head *list, int cmd, int brw_flags)
2606{
Chris Hanna29ac6842015-06-03 10:23:42 -04002607 struct client_obd *cli = osc_cli(obj);
2608 struct osc_extent *ext;
Wei Yongjunf13ab922013-05-17 16:27:05 +08002609 struct osc_async_page *oap, *tmp;
Chris Hanna29ac6842015-06-03 10:23:42 -04002610 int page_count = 0;
2611 int mppr = cli->cl_max_pages_per_rpc;
2612 pgoff_t start = CL_PAGE_EOF;
2613 pgoff_t end = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002614
2615 list_for_each_entry(oap, list, oap_pending_item) {
2616 struct cl_page *cp = oap2cl_page(oap);
Mike Rapoport50ffcb72015-10-13 16:03:40 +03002617
Peng Taod7e09d02013-05-02 16:46:55 +08002618 if (cp->cp_index > end)
2619 end = cp->cp_index;
2620 if (cp->cp_index < start)
2621 start = cp->cp_index;
2622 ++page_count;
2623 mppr <<= (page_count > mppr);
2624 }
2625
2626 ext = osc_extent_alloc(obj);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002627 if (!ext) {
Wei Yongjunf13ab922013-05-17 16:27:05 +08002628 list_for_each_entry_safe(oap, tmp, list, oap_pending_item) {
Peng Taod7e09d02013-05-02 16:46:55 +08002629 list_del_init(&oap->oap_pending_item);
2630 osc_ap_completion(env, cli, oap, 0, -ENOMEM);
2631 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002632 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +08002633 }
2634
2635 ext->oe_rw = !!(cmd & OBD_BRW_READ);
2636 ext->oe_urgent = 1;
2637 ext->oe_start = start;
2638 ext->oe_end = ext->oe_max_end = end;
2639 ext->oe_obj = obj;
2640 ext->oe_srvlock = !!(brw_flags & OBD_BRW_SRVLOCK);
2641 ext->oe_nr_pages = page_count;
2642 ext->oe_mppr = mppr;
2643 list_splice_init(list, &ext->oe_pages);
2644
2645 osc_object_lock(obj);
2646 /* Reuse the initial refcount for RPC, don't drop it */
2647 osc_extent_state_set(ext, OES_LOCK_DONE);
2648 if (cmd & OBD_BRW_WRITE) {
2649 list_add_tail(&ext->oe_link, &obj->oo_urgent_exts);
2650 osc_update_pending(obj, OBD_BRW_WRITE, page_count);
2651 } else {
2652 list_add_tail(&ext->oe_link, &obj->oo_reading_exts);
2653 osc_update_pending(obj, OBD_BRW_READ, page_count);
2654 }
2655 osc_object_unlock(obj);
2656
Bobi Jamc61ac972015-03-25 22:04:53 -04002657 osc_io_unplug_async(env, cli, obj);
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002658 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002659}
2660
2661/**
2662 * Called by osc_io_setattr_start() to freeze and destroy covering extents.
2663 */
2664int osc_cache_truncate_start(const struct lu_env *env, struct osc_io *oio,
2665 struct osc_object *obj, __u64 size)
2666{
2667 struct client_obd *cli = osc_cli(obj);
2668 struct osc_extent *ext;
Bhaktipriya Shridhar4a81ce52016-03-12 01:37:35 +05302669 struct osc_extent *temp;
Peng Taod7e09d02013-05-02 16:46:55 +08002670 struct osc_extent *waiting = NULL;
2671 pgoff_t index;
2672 LIST_HEAD(list);
2673 int result = 0;
2674 bool partial;
Peng Taod7e09d02013-05-02 16:46:55 +08002675
2676 /* pages with index greater or equal to index will be truncated. */
2677 index = cl_index(osc2cl(obj), size);
2678 partial = size > cl_offset(osc2cl(obj), index);
2679
2680again:
2681 osc_object_lock(obj);
2682 ext = osc_extent_search(obj, index);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002683 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +08002684 ext = first_extent(obj);
2685 else if (ext->oe_end < index)
2686 ext = next_extent(ext);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002687 while (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002688 EASSERT(ext->oe_state != OES_TRUNC, ext);
2689
2690 if (ext->oe_state > OES_CACHE || ext->oe_urgent) {
2691 /* if ext is in urgent state, it means there must exist
2692 * a page already having been flushed by write_page().
2693 * We have to wait for this extent because we can't
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002694 * truncate that page.
2695 */
Peng Taod7e09d02013-05-02 16:46:55 +08002696 LASSERT(!ext->oe_hp);
2697 OSC_EXTENT_DUMP(D_CACHE, ext,
2698 "waiting for busy extent\n");
2699 waiting = osc_extent_get(ext);
2700 break;
2701 }
2702
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07002703 OSC_EXTENT_DUMP(D_CACHE, ext, "try to trunc:%llu.\n", size);
Peng Taod7e09d02013-05-02 16:46:55 +08002704
2705 osc_extent_get(ext);
2706 if (ext->oe_state == OES_ACTIVE) {
2707 /* though we grab inode mutex for write path, but we
2708 * release it before releasing extent(in osc_io_end()),
2709 * so there is a race window that an extent is still
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002710 * in OES_ACTIVE when truncate starts.
2711 */
Peng Taod7e09d02013-05-02 16:46:55 +08002712 LASSERT(!ext->oe_trunc_pending);
2713 ext->oe_trunc_pending = 1;
2714 } else {
2715 EASSERT(ext->oe_state == OES_CACHE, ext);
2716 osc_extent_state_set(ext, OES_TRUNC);
2717 osc_update_pending(obj, OBD_BRW_WRITE,
2718 -ext->oe_nr_pages);
2719 }
2720 EASSERT(list_empty(&ext->oe_link), ext);
2721 list_add_tail(&ext->oe_link, &list);
2722
2723 ext = next_extent(ext);
2724 }
2725 osc_object_unlock(obj);
2726
2727 osc_list_maint(cli, obj);
2728
Bhaktipriya Shridhar4a81ce52016-03-12 01:37:35 +05302729 list_for_each_entry_safe(ext, temp, &list, oe_link) {
Peng Taod7e09d02013-05-02 16:46:55 +08002730 int rc;
2731
Peng Taod7e09d02013-05-02 16:46:55 +08002732 list_del_init(&ext->oe_link);
2733
2734 /* extent may be in OES_ACTIVE state because inode mutex
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002735 * is released before osc_io_end() in file write case
2736 */
Peng Taod7e09d02013-05-02 16:46:55 +08002737 if (ext->oe_state != OES_TRUNC)
2738 osc_extent_wait(env, ext, OES_TRUNC);
2739
2740 rc = osc_extent_truncate(ext, index, partial);
2741 if (rc < 0) {
2742 if (result == 0)
2743 result = rc;
2744
2745 OSC_EXTENT_DUMP(D_ERROR, ext,
2746 "truncate error %d\n", rc);
2747 } else if (ext->oe_nr_pages == 0) {
2748 osc_extent_remove(ext);
2749 } else {
2750 /* this must be an overlapped extent which means only
2751 * part of pages in this extent have been truncated.
2752 */
2753 EASSERTF(ext->oe_start <= index, ext,
2754 "trunc index = %lu/%d.\n", index, partial);
2755 /* fix index to skip this partially truncated extent */
2756 index = ext->oe_end + 1;
2757 partial = false;
2758
2759 /* we need to hold this extent in OES_TRUNC state so
2760 * that no writeback will happen. This is to avoid
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002761 * BUG 17397.
2762 */
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002763 LASSERT(!oio->oi_trunc);
Peng Taod7e09d02013-05-02 16:46:55 +08002764 oio->oi_trunc = osc_extent_get(ext);
2765 OSC_EXTENT_DUMP(D_CACHE, ext,
Greg Kroah-Hartmanb0f5aad2014-07-12 20:06:04 -07002766 "trunc at %llu\n", size);
Peng Taod7e09d02013-05-02 16:46:55 +08002767 }
2768 osc_extent_put(env, ext);
2769 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002770 if (waiting) {
Peng Taod7e09d02013-05-02 16:46:55 +08002771 int rc;
2772
2773 /* ignore the result of osc_extent_wait the write initiator
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002774 * should take care of it.
2775 */
Peng Taod7e09d02013-05-02 16:46:55 +08002776 rc = osc_extent_wait(env, waiting, OES_INV);
2777 if (rc < 0)
Jinshan Xiong451721c2013-06-03 21:40:58 +08002778 OSC_EXTENT_DUMP(D_CACHE, waiting, "error: %d.\n", rc);
Peng Taod7e09d02013-05-02 16:46:55 +08002779
2780 osc_extent_put(env, waiting);
2781 waiting = NULL;
2782 goto again;
2783 }
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002784 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08002785}
2786
2787/**
2788 * Called after osc_io_setattr_end to add oio->oi_trunc back to cache.
2789 */
2790void osc_cache_truncate_end(const struct lu_env *env, struct osc_io *oio,
2791 struct osc_object *obj)
2792{
2793 struct osc_extent *ext = oio->oi_trunc;
2794
2795 oio->oi_trunc = NULL;
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002796 if (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002797 bool unplug = false;
2798
2799 EASSERT(ext->oe_nr_pages > 0, ext);
2800 EASSERT(ext->oe_state == OES_TRUNC, ext);
2801 EASSERT(!ext->oe_urgent, ext);
2802
2803 OSC_EXTENT_DUMP(D_CACHE, ext, "trunc -> cache.\n");
2804 osc_object_lock(obj);
2805 osc_extent_state_set(ext, OES_CACHE);
2806 if (ext->oe_fsync_wait && !ext->oe_urgent) {
2807 ext->oe_urgent = 1;
2808 list_move_tail(&ext->oe_link, &obj->oo_urgent_exts);
2809 unplug = true;
2810 }
2811 osc_update_pending(obj, OBD_BRW_WRITE, ext->oe_nr_pages);
2812 osc_object_unlock(obj);
2813 osc_extent_put(env, ext);
2814
2815 if (unplug)
2816 osc_io_unplug_async(env, osc_cli(obj), obj);
2817 }
2818}
2819
2820/**
2821 * Wait for extents in a specific range to be written out.
2822 * The caller must have called osc_cache_writeback_range() to issue IO
2823 * otherwise it will take a long time for this function to finish.
2824 *
2825 * Caller must hold inode_mutex , or cancel exclusive dlm lock so that
2826 * nobody else can dirty this range of file while we're waiting for
2827 * extents to be written.
2828 */
2829int osc_cache_wait_range(const struct lu_env *env, struct osc_object *obj,
2830 pgoff_t start, pgoff_t end)
2831{
2832 struct osc_extent *ext;
2833 pgoff_t index = start;
Chris Hanna29ac6842015-06-03 10:23:42 -04002834 int result = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002835
2836again:
2837 osc_object_lock(obj);
2838 ext = osc_extent_search(obj, index);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002839 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +08002840 ext = first_extent(obj);
2841 else if (ext->oe_end < index)
2842 ext = next_extent(ext);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002843 while (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002844 int rc;
2845
2846 if (ext->oe_start > end)
2847 break;
2848
2849 if (!ext->oe_fsync_wait) {
2850 ext = next_extent(ext);
2851 continue;
2852 }
2853
2854 EASSERT(ergo(ext->oe_state == OES_CACHE,
2855 ext->oe_hp || ext->oe_urgent), ext);
2856 EASSERT(ergo(ext->oe_state == OES_ACTIVE,
2857 !ext->oe_hp && ext->oe_urgent), ext);
2858
2859 index = ext->oe_end + 1;
2860 osc_extent_get(ext);
2861 osc_object_unlock(obj);
2862
2863 rc = osc_extent_wait(env, ext, OES_INV);
2864 if (result == 0)
2865 result = rc;
2866 osc_extent_put(env, ext);
2867 goto again;
2868 }
2869 osc_object_unlock(obj);
2870
2871 OSC_IO_DEBUG(obj, "sync file range.\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002872 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08002873}
2874
2875/**
2876 * Called to write out a range of osc object.
2877 *
2878 * @hp : should be set this is caused by lock cancel;
2879 * @discard: is set if dirty pages should be dropped - file will be deleted or
2880 * truncated, this implies there is no partially discarding extents.
2881 *
2882 * Return how many pages will be issued, or error code if error occurred.
2883 */
2884int osc_cache_writeback_range(const struct lu_env *env, struct osc_object *obj,
2885 pgoff_t start, pgoff_t end, int hp, int discard)
2886{
2887 struct osc_extent *ext;
2888 LIST_HEAD(discard_list);
2889 bool unplug = false;
2890 int result = 0;
Peng Taod7e09d02013-05-02 16:46:55 +08002891
2892 osc_object_lock(obj);
2893 ext = osc_extent_search(obj, start);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002894 if (!ext)
Peng Taod7e09d02013-05-02 16:46:55 +08002895 ext = first_extent(obj);
2896 else if (ext->oe_end < start)
2897 ext = next_extent(ext);
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002898 while (ext) {
Peng Taod7e09d02013-05-02 16:46:55 +08002899 if (ext->oe_start > end)
2900 break;
2901
2902 ext->oe_fsync_wait = 1;
2903 switch (ext->oe_state) {
2904 case OES_CACHE:
2905 result += ext->oe_nr_pages;
2906 if (!discard) {
2907 struct list_head *list = NULL;
Mike Rapoport50ffcb72015-10-13 16:03:40 +03002908
Peng Taod7e09d02013-05-02 16:46:55 +08002909 if (hp) {
2910 EASSERT(!ext->oe_hp, ext);
2911 ext->oe_hp = 1;
2912 list = &obj->oo_hp_exts;
2913 } else if (!ext->oe_urgent) {
2914 ext->oe_urgent = 1;
2915 list = &obj->oo_urgent_exts;
2916 }
Oleg Drokin7f1ae4c2016-02-16 00:46:57 -05002917 if (list)
Peng Taod7e09d02013-05-02 16:46:55 +08002918 list_move_tail(&ext->oe_link, list);
2919 unplug = true;
2920 } else {
2921 /* the only discarder is lock cancelling, so
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002922 * [start, end] must contain this extent
2923 */
Peng Taod7e09d02013-05-02 16:46:55 +08002924 EASSERT(ext->oe_start >= start &&
2925 ext->oe_max_end <= end, ext);
2926 osc_extent_state_set(ext, OES_LOCKING);
2927 ext->oe_owner = current;
Oleg Drokin79910d72016-02-26 01:50:03 -05002928 list_move_tail(&ext->oe_link, &discard_list);
Peng Taod7e09d02013-05-02 16:46:55 +08002929 osc_update_pending(obj, OBD_BRW_WRITE,
2930 -ext->oe_nr_pages);
2931 }
2932 break;
2933 case OES_ACTIVE:
2934 /* It's pretty bad to wait for ACTIVE extents, because
2935 * we don't know how long we will wait for it to be
2936 * flushed since it may be blocked at awaiting more
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002937 * grants. We do this for the correctness of fsync.
2938 */
Peng Taod7e09d02013-05-02 16:46:55 +08002939 LASSERT(hp == 0 && discard == 0);
2940 ext->oe_urgent = 1;
2941 break;
2942 case OES_TRUNC:
2943 /* this extent is being truncated, can't do anything
2944 * for it now. it will be set to urgent after truncate
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002945 * is finished in osc_cache_truncate_end().
2946 */
Peng Taod7e09d02013-05-02 16:46:55 +08002947 default:
2948 break;
2949 }
2950 ext = next_extent(ext);
2951 }
2952 osc_object_unlock(obj);
2953
2954 LASSERT(ergo(!discard, list_empty(&discard_list)));
2955 if (!list_empty(&discard_list)) {
2956 struct osc_extent *tmp;
2957 int rc;
2958
2959 osc_list_maint(osc_cli(obj), obj);
2960 list_for_each_entry_safe(ext, tmp, &discard_list, oe_link) {
2961 list_del_init(&ext->oe_link);
2962 EASSERT(ext->oe_state == OES_LOCKING, ext);
2963
2964 /* Discard caching pages. We don't actually write this
Oleg Drokin30aa9c52016-02-24 22:00:37 -05002965 * extent out but we complete it as if we did.
2966 */
Peng Taod7e09d02013-05-02 16:46:55 +08002967 rc = osc_extent_make_ready(env, ext);
2968 if (unlikely(rc < 0)) {
2969 OSC_EXTENT_DUMP(D_ERROR, ext,
2970 "make_ready returned %d\n", rc);
2971 if (result >= 0)
2972 result = rc;
2973 }
2974
2975 /* finish the extent as if the pages were sent */
2976 osc_extent_finish(env, ext, 0, 0);
2977 }
2978 }
2979
2980 if (unplug)
Olaf Weberc5c4c6f2015-09-14 18:41:35 -04002981 osc_io_unplug(env, osc_cli(obj), obj);
Peng Taod7e09d02013-05-02 16:46:55 +08002982
2983 if (hp || discard) {
2984 int rc;
Mike Rapoport50ffcb72015-10-13 16:03:40 +03002985
Peng Taod7e09d02013-05-02 16:46:55 +08002986 rc = osc_cache_wait_range(env, obj, start, end);
2987 if (result >= 0 && rc < 0)
2988 result = rc;
2989 }
2990
2991 OSC_IO_DEBUG(obj, "cache page out.\n");
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +08002992 return result;
Peng Taod7e09d02013-05-02 16:46:55 +08002993}
2994
2995/** @} osc */