4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * lustre/target/tgt_grant.c
31 * This file provides code related to grant space management on Lustre Targets
32 * (OSTs and MDTs). Grant is a mechanism used by client nodes to reserve disk
33 * space on a target for the data writeback cache. The Lustre client is thus
34 * assured that enough space will be available when flushing dirty pages
35 * asynchronously. Each client node is granted an initial amount of reserved
36 * space at connect time and gets additional space back from target in bulk
39 * We actually support three different cases:
40 * - The client supports the new grant parameters (i.e. OBD_CONNECT_GRANT_PARAM)
41 * which means that all grant overhead calculation happens on the client side.
42 * The server reports at connect time the backend filesystem block size, the
43 * maximum extent size as well as the extent insertion cost and it is then up
44 * to the osc layer to the track dirty extents and consume grant accordingly
45 * (see osc_cache.c). In each bulk write request, the client provides how much
46 * grant space was consumed for this RPC.
47 * - The client does not support OBD_CONNECT_GRANT_PARAM and always assumes a
48 * a backend file system block size of 4KB. We then have two cases:
49 * - If the block size is really 4KB, then the client can deal with grant
50 * allocation for partial block writes, but won't take extent insertion cost
51 * into account. For such clients, we inflate grant by 100% on the server
52 * side. It means that when 32MB of grant is hold by the client, 64MB of
53 * grant space is actually reserved on the server. All grant counters
54 * provided by such a client are inflated by 100%.
55 * - The backend filesystem block size is bigger than 4KB, which isn't
56 * supported by the client. In this case, we emulate a 4KB block size and
57 * consume one block size on the server for each 4KB of grant returned to
58 * client. With a 128KB blocksize, it means that 32MB dirty pages of 4KB
59 * on the client will actually consume 1GB of grant on the server.
60 * All grant counters provided by such a client are inflated by the block
63 * This file handles the core logic for:
64 * - grant allocation strategy
65 * - maintaining per-client as well as global grant space accounting
66 * - processing grant information packed in incoming requests
67 * - allocating server-side grant space for synchronous write RPCs which did not
68 * consume grant on the client side (OBD_BRW_FROM_GRANT flag not set). If not
69 * enough space is available, such RPCs fail with ENOSPC
71 * Author: Johann Lombardi <johann.lombardi@intel.com>
74 #define DEBUG_SUBSYSTEM S_FILTER
77 #include <obd_class.h>
79 #include "tgt_internal.h"
81 /* Clients typically hold 2x their max_rpcs_in_flight of grant space */
82 #define TGT_GRANT_SHRINK_LIMIT(exp) (2ULL * 8 * exp_max_brw_size(exp))
84 /* Helpers to inflate/deflate grants for clients that do not support the grant
86 static inline u64 tgt_grant_inflate(struct tg_grants_data *tgd, u64 val)
88 if (tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT)
89 /* Client does not support such large block size, grant
90 * is thus inflated. We already significantly overestimate
91 * overhead, no need to add the extent tax in this case */
92 return val << (tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT);
96 /* Companion of tgt_grant_inflate() */
97 static inline u64 tgt_grant_deflate(struct tg_grants_data *tgd, u64 val)
99 if (tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT)
100 return val >> (tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT);
104 /* Grant chunk is used as a unit for grant allocation. It should be inflated
105 * if the client does not support the grant paramaters.
106 * Check connection flag against \a data if not NULL. This is used during
107 * connection creation where exp->exp_connect_data isn't populated yet */
108 static inline u64 tgt_grant_chunk(struct obd_export *exp,
109 struct lu_target *lut,
110 struct obd_connect_data *data)
112 struct tg_grants_data *tgd = &lut->lut_tgd;
113 u64 chunk = exp_max_brw_size(exp);
116 if (exp->exp_obd->obd_self_export == exp)
117 /* Grant enough space to handle a big precreate request */
118 return OST_MAX_PRECREATE * lut->lut_dt_conf.ddp_inodespace / 2;
120 if ((data == NULL && !(exp_grant_param_supp(exp))) ||
121 (data != NULL && !OCD_HAS_FLAG(data, GRANT_PARAM)))
122 /* Try to grant enough space to send 2 full-size RPCs */
123 return tgt_grant_inflate(tgd, chunk) << 1;
125 /* Try to return enough to send two full-size RPCs
126 * = 2 * (BRW_size + #extents_in_BRW * grant_tax) */
127 tax = 1ULL << tgd->tgd_blockbits; /* block size */
128 tax *= lut->lut_dt_conf.ddp_max_extent_blks; /* max extent size */
129 tax = (chunk + tax - 1) / tax; /* #extents in a RPC */
130 tax *= lut->lut_dt_conf.ddp_extent_tax; /* extent tax for a RPC */
131 chunk = (chunk + tax) * 2; /* we said two full RPCs */
135 static int tgt_check_export_grants(struct obd_export *exp, u64 *dirty,
136 u64 *pending, u64 *granted, u64 maxsize)
138 struct tg_export_data *ted = &exp->exp_target_data;
141 if (ted->ted_grant < 0 || ted->ted_pending < 0 || ted->ted_dirty < 0)
143 CDEBUG_LIMIT(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
144 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
145 ted->ted_dirty, ted->ted_pending, ted->ted_grant);
147 if (ted->ted_grant + ted->ted_pending > maxsize) {
148 CERROR("%s: cli %s/%p ted_grant(%ld) + ted_pending(%ld)"
149 " > maxsize(%llu)\n", exp->exp_obd->obd_name,
150 exp->exp_client_uuid.uuid, exp, ted->ted_grant,
151 ted->ted_pending, maxsize);
154 if (ted->ted_dirty > maxsize) {
155 CERROR("%s: cli %s/%p ted_dirty(%ld) > maxsize(%llu)\n",
156 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
157 exp, ted->ted_dirty, maxsize);
160 *granted += ted->ted_grant + ted->ted_pending;
161 *pending += ted->ted_pending;
162 *dirty += ted->ted_dirty;
167 * Perform extra sanity checks for grant accounting.
169 * This function scans the export list, sanity checks per-export grant counters
170 * and verifies accuracy of global grant accounting. If an inconsistency is
171 * found, a CERROR is printed with the function name \func that was passed as
172 * argument. LBUG is only called in case of serious counter corruption (i.e.
173 * value larger than the device size).
174 * Those sanity checks can be pretty expensive and are disabled if the OBD
175 * device has more than 100 connected exports.
177 * \param[in] obd OBD device for which grant accounting should be
179 * \param[in] func caller's function name
181 void tgt_grant_sanity_check(struct obd_device *obd, const char *func)
183 struct lu_target *lut = obd->u.obt.obt_lut;
184 struct tg_grants_data *tgd = &lut->lut_tgd;
185 struct obd_export *exp;
186 struct tg_export_data *ted;
196 if (list_empty(&obd->obd_exports))
199 /* We don't want to do this for large machines that do lots of
200 * mounts or unmounts. It burns... */
201 if (obd->obd_num_exports > 100)
204 maxsize = tgd->tgd_osfs.os_blocks << tgd->tgd_blockbits;
206 spin_lock(&obd->obd_dev_lock);
207 spin_lock(&tgd->tgd_grant_lock);
208 exp = obd->obd_self_export;
209 ted = &exp->exp_target_data;
210 CDEBUG(D_CACHE, "%s: processing self export: %ld %ld "
211 "%ld\n", obd->obd_name, ted->ted_grant,
212 ted->ted_pending, ted->ted_dirty);
213 tot_granted += ted->ted_grant + ted->ted_pending;
214 tot_pending += ted->ted_pending;
215 tot_dirty += ted->ted_dirty;
217 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
218 error = tgt_check_export_grants(exp, &tot_dirty, &tot_pending,
219 &tot_granted, maxsize);
221 spin_unlock(&obd->obd_dev_lock);
222 spin_unlock(&tgd->tgd_grant_lock);
227 /* exports about to be unlinked should also be taken into account since
228 * they might still hold pending grant space to be released at
230 list_for_each_entry(exp, &obd->obd_unlinked_exports, exp_obd_chain) {
231 error = tgt_check_export_grants(exp, &tot_dirty, &tot_pending,
232 &tot_granted, maxsize);
234 spin_unlock(&obd->obd_dev_lock);
235 spin_unlock(&tgd->tgd_grant_lock);
240 fo_tot_granted = tgd->tgd_tot_granted;
241 fo_tot_pending = tgd->tgd_tot_pending;
242 fo_tot_dirty = tgd->tgd_tot_dirty;
243 spin_unlock(&obd->obd_dev_lock);
244 spin_unlock(&tgd->tgd_grant_lock);
246 if (tot_granted != fo_tot_granted)
247 CERROR("%s: tot_granted %llu != fo_tot_granted %llu\n",
248 func, tot_granted, fo_tot_granted);
249 if (tot_pending != fo_tot_pending)
250 CERROR("%s: tot_pending %llu != fo_tot_pending %llu\n",
251 func, tot_pending, fo_tot_pending);
252 if (tot_dirty != fo_tot_dirty)
253 CERROR("%s: tot_dirty %llu != fo_tot_dirty %llu\n",
254 func, tot_dirty, fo_tot_dirty);
255 if (tot_pending > tot_granted)
256 CERROR("%s: tot_pending %llu > tot_granted %llu\n",
257 func, tot_pending, tot_granted);
258 if (tot_granted > maxsize)
259 CERROR("%s: tot_granted %llu > maxsize %llu\n",
260 func, tot_granted, maxsize);
261 if (tot_dirty > maxsize)
262 CERROR("%s: tot_dirty %llu > maxsize %llu\n",
263 func, tot_dirty, maxsize);
265 EXPORT_SYMBOL(tgt_grant_sanity_check);
268 * Get file system statistics of target.
270 * Helper function for statfs(), also used by grant code.
271 * Implements caching for statistics to avoid calling OSD device each time.
273 * \param[in] env execution environment
274 * \param[in] lut LU target
275 * \param[out] osfs statistic data to return
276 * \param[in] max_age maximum age for cached data
277 * \param[in] from_cache show that data was get from cache or not
279 * \retval 0 if successful
280 * \retval negative value on error
282 int tgt_statfs_internal(const struct lu_env *env, struct lu_target *lut,
283 struct obd_statfs *osfs, __u64 max_age, int *from_cache)
285 struct tg_grants_data *tgd = &lut->lut_tgd;
289 spin_lock(&tgd->tgd_osfs_lock);
290 if (cfs_time_before_64(tgd->tgd_osfs_age, max_age) || max_age == 0) {
293 /* statfs data are too old, get up-to-date one.
294 * we must be cautious here since multiple threads might be
295 * willing to update statfs data concurrently and we must
296 * grant that cached statfs data are always consistent */
298 if (tgd->tgd_statfs_inflight == 0)
299 /* clear inflight counter if no users, although it would
300 * take a while to overflow this 64-bit counter ... */
301 tgd->tgd_osfs_inflight = 0;
302 /* notify tgt_grant_commit() that we want to track writes
303 * completed as of now */
304 tgd->tgd_statfs_inflight++;
305 /* record value of inflight counter before running statfs to
306 * compute the diff once statfs is completed */
307 unstable = tgd->tgd_osfs_inflight;
308 spin_unlock(&tgd->tgd_osfs_lock);
310 /* statfs can sleep ... hopefully not for too long since we can
311 * call it fairly often as space fills up */
312 rc = dt_statfs(env, lut->lut_bottom, osfs);
316 osfs->os_namelen = min_t(__u32, osfs->os_namelen, NAME_MAX);
318 spin_lock(&tgd->tgd_grant_lock);
319 spin_lock(&tgd->tgd_osfs_lock);
320 /* calculate how much space was written while we released the
322 unstable = tgd->tgd_osfs_inflight - unstable;
323 tgd->tgd_osfs_unstable = 0;
325 /* some writes committed while we were running statfs
326 * w/o the tgd_osfs_lock. Those ones got added to
327 * the cached statfs data that we are about to crunch.
328 * Take them into account in the new statfs data */
329 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
330 unstable >> tgd->tgd_blockbits);
331 /* However, we don't really know if those writes got
332 * accounted in the statfs call, so tell
333 * tgt_grant_space_left() there is some uncertainty
334 * on the accounting of those writes.
335 * The purpose is to prevent spurious error messages in
336 * tgt_grant_space_left() since those writes might be
337 * accounted twice. */
338 tgd->tgd_osfs_unstable += unstable;
340 /* similarly, there is some uncertainty on write requests
341 * between prepare & commit */
342 tgd->tgd_osfs_unstable += tgd->tgd_tot_pending;
343 spin_unlock(&tgd->tgd_grant_lock);
345 /* finally udpate cached statfs data */
346 tgd->tgd_osfs = *osfs;
347 tgd->tgd_osfs_age = cfs_time_current_64();
349 tgd->tgd_statfs_inflight--; /* stop tracking */
350 if (tgd->tgd_statfs_inflight == 0)
351 tgd->tgd_osfs_inflight = 0;
352 spin_unlock(&tgd->tgd_osfs_lock);
357 /* use cached statfs data */
358 *osfs = tgd->tgd_osfs;
359 spin_unlock(&tgd->tgd_osfs_lock);
368 EXPORT_SYMBOL(tgt_statfs_internal);
371 * Update cached statfs information from the OSD layer
373 * Refresh statfs information cached in tgd::tgd_osfs if the cache is older
374 * than 1s or if force is set. The OSD layer is in charge of estimating data &
376 * This function can sleep so it should not be called with any spinlock held.
378 * \param[in] env LU environment passed by the caller
379 * \param[in] exp export used to print client info in debug
381 * \param[in] force force a refresh of statfs information
382 * \param[out] from_cache returns whether the statfs information are
385 static void tgt_grant_statfs(const struct lu_env *env, struct obd_export *exp,
386 int force, int *from_cache)
388 struct obd_device *obd = exp->exp_obd;
389 struct lu_target *lut = obd->u.obt.obt_lut;
390 struct tg_grants_data *tgd = &lut->lut_tgd;
391 struct tgt_thread_info *tti;
392 struct obd_statfs *osfs;
397 max_age = 0; /* get fresh statfs data */
399 max_age = cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS);
401 tti = tgt_th_info(env);
402 osfs = &tti->tti_u.osfs;
403 rc = tgt_statfs_internal(env, lut, osfs, max_age, from_cache);
410 CDEBUG(D_CACHE, "%s: cli %s/%p free: %llu avail: %llu\n",
411 obd->obd_name, exp->exp_client_uuid.uuid, exp,
412 osfs->os_bfree << tgd->tgd_blockbits,
413 osfs->os_bavail << tgd->tgd_blockbits);
417 * Figure out how much space is available on the backend filesystem after
418 * removing grant space already booked by clients.
420 * This is done by accessing cached statfs data previously populated by
421 * tgt_grant_statfs(), from which we withdraw the space already granted to
422 * clients and the reserved space.
423 * Caller must hold tgd_grant_lock spinlock.
425 * \param[in] exp export associated with the device for which the amount
426 * of available space is requested
427 * \retval amount of non-allocated space, in bytes
429 static u64 tgt_grant_space_left(struct obd_export *exp)
431 struct obd_device *obd = exp->exp_obd;
432 struct lu_target *lut = obd->u.obt.obt_lut;
433 struct tg_grants_data *tgd = &lut->lut_tgd;
441 assert_spin_locked(&tgd->tgd_grant_lock);
443 spin_lock(&tgd->tgd_osfs_lock);
444 /* get available space from cached statfs data */
445 left = tgd->tgd_osfs.os_bavail << tgd->tgd_blockbits;
446 unstable = tgd->tgd_osfs_unstable; /* those might be accounted twice */
447 spin_unlock(&tgd->tgd_osfs_lock);
449 reserved = left * tgd->tgd_reserved_pcnt / 100;
450 tot_granted = tgd->tgd_tot_granted + reserved;
452 if (left < tot_granted) {
453 int mask = (left + unstable <
454 tot_granted - tgd->tgd_tot_pending) ?
457 CDEBUG_LIMIT(mask, "%s: cli %s/%p left %llu < tot_grant "
458 "%llu unstable %llu pending %llu "
460 obd->obd_name, exp->exp_client_uuid.uuid, exp,
461 left, tot_granted, unstable,
462 tgd->tgd_tot_pending,
468 /* Withdraw space already granted to clients */
471 /* Align left on block size */
472 left &= ~((1ULL << tgd->tgd_blockbits) - 1);
474 CDEBUG(D_CACHE, "%s: cli %s/%p avail %llu left %llu unstable "
475 "%llu tot_grant %llu pending %llu\n", obd->obd_name,
476 exp->exp_client_uuid.uuid, exp, avail, left, unstable,
477 tot_granted, tgd->tgd_tot_pending);
483 * Process grant information from obdo structure packed in incoming BRW
484 * and inflate grant counters if required.
486 * Grab the dirty and seen grant announcements from the incoming obdo and
487 * inflate all grant counters passed in the request if the client does not
488 * support the grant parameters.
489 * We will later calculate the client's new grant and return it.
490 * Caller must hold tgd_grant_lock spinlock.
492 * \param[in] env LU environment supplying osfs storage
493 * \param[in] exp export for which we received the request
494 * \param[in,out] oa incoming obdo sent by the client
496 static void tgt_grant_incoming(const struct lu_env *env, struct obd_export *exp,
497 struct obdo *oa, long chunk)
499 struct tg_export_data *ted = &exp->exp_target_data;
500 struct obd_device *obd = exp->exp_obd;
501 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
506 assert_spin_locked(&tgd->tgd_grant_lock);
508 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
509 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
510 oa->o_valid &= ~OBD_MD_FLGRANT;
514 /* Add some margin, since there is a small race if other RPCs arrive
515 * out-or-order and have already consumed some grant. We want to
516 * leave this here in case there is a large error in accounting. */
518 "%s: cli %s/%p reports grant %llu dropped %u, local %lu\n",
519 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
520 oa->o_dropped, ted->ted_grant);
522 if ((long long)oa->o_dirty < 0)
525 /* inflate grant counters if required */
526 if (!exp_grant_param_supp(exp)) {
527 oa->o_grant = tgt_grant_inflate(tgd, oa->o_grant);
528 oa->o_dirty = tgt_grant_inflate(tgd, oa->o_dirty);
529 oa->o_dropped = tgt_grant_inflate(tgd, (u64)oa->o_dropped);
530 oa->o_undirty = tgt_grant_inflate(tgd, oa->o_undirty);
534 dropped = oa->o_dropped;
536 /* Update our accounting now so that statfs takes it into account.
537 * Note that ted_dirty is only approximate and can become incorrect
538 * if RPCs arrive out-of-order. No important calculations depend
539 * on ted_dirty however, but we must check sanity to not assert. */
540 if (dirty > ted->ted_grant + 4 * chunk)
541 dirty = ted->ted_grant + 4 * chunk;
542 tgd->tgd_tot_dirty += dirty - ted->ted_dirty;
543 if (ted->ted_grant < dropped) {
545 "%s: cli %s/%p reports %lu dropped > grant %lu\n",
546 obd->obd_name, exp->exp_client_uuid.uuid, exp, dropped,
550 if (tgd->tgd_tot_granted < dropped) {
551 CERROR("%s: cli %s/%p reports %lu dropped > tot_grant %llu\n",
552 obd->obd_name, exp->exp_client_uuid.uuid, exp,
553 dropped, tgd->tgd_tot_granted);
556 tgd->tgd_tot_granted -= dropped;
557 ted->ted_grant -= dropped;
558 ted->ted_dirty = dirty;
560 if (ted->ted_dirty < 0 || ted->ted_grant < 0 || ted->ted_pending < 0) {
561 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
562 obd->obd_name, exp->exp_client_uuid.uuid, exp,
563 ted->ted_dirty, ted->ted_pending, ted->ted_grant);
564 spin_unlock(&tgd->tgd_grant_lock);
571 * Grant shrink request handler.
573 * Client nodes can explicitly release grant space (i.e. process called grant
574 * shrinking). This function proceeds with the shrink request when there is
575 * less ungranted space remaining than the amount all of the connected clients
576 * would consume if they used their full grant.
577 * Caller must hold tgd_grant_lock spinlock.
579 * \param[in] exp export releasing grant space
580 * \param[in,out] oa incoming obdo sent by the client
581 * \param[in] left_space remaining free space with space already granted
584 static void tgt_grant_shrink(struct obd_export *exp, struct obdo *oa,
587 struct tg_export_data *ted = &exp->exp_target_data;
588 struct obd_device *obd = exp->exp_obd;
589 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
592 assert_spin_locked(&tgd->tgd_grant_lock);
594 if (left_space >= tgd->tgd_tot_granted_clients *
595 TGT_GRANT_SHRINK_LIMIT(exp))
598 grant_shrink = oa->o_grant;
600 ted->ted_grant -= grant_shrink;
601 tgd->tgd_tot_granted -= grant_shrink;
603 CDEBUG(D_CACHE, "%s: cli %s/%p shrink %ld ted_grant %ld total %llu\n",
604 obd->obd_name, exp->exp_client_uuid.uuid, exp, grant_shrink,
605 ted->ted_grant, tgd->tgd_tot_granted);
607 /* client has just released some grant, don't grant any space back */
612 * Calculate how much space is required to write a given network buffer
614 * This function takes block alignment into account to estimate how much on-disk
615 * space will be required to successfully write the whole niobuf.
616 * Estimated space is inflated if the export does not support
617 * OBD_CONNECT_GRANT_PARAM and if the backend filesystem has a block size
618 * larger than the minimal supported page size (i.e. 4KB).
620 * \param[in] exp export associated which the write request
621 * if NULL, then size estimate is done for server-side
623 * \param[in] lut LU target handling the request
624 * \param[in] rnb network buffer to estimate size of
626 * \retval space (in bytes) that will be consumed to write the
629 static inline u64 tgt_grant_rnb_size(struct obd_export *exp,
630 struct lu_target *lut,
631 struct niobuf_remote *rnb)
633 struct tg_grants_data *tgd = &lut->lut_tgd;
638 if (exp && !exp_grant_param_supp(exp) &&
639 tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT)
640 blksize = 1ULL << COMPAT_BSIZE_SHIFT;
642 blksize = 1ULL << tgd->tgd_blockbits;
644 /* The network buffer might span several blocks, align it on block
646 bytes = rnb->rnb_offset & (blksize - 1);
647 bytes += rnb->rnb_len;
648 end = bytes & (blksize - 1);
650 bytes += blksize - end;
652 if (exp == NULL || exp_grant_param_supp(exp)) {
653 /* add per-extent insertion cost */
657 max_ext = blksize * lut->lut_dt_conf.ddp_max_extent_blks;
658 nr_ext = (bytes + max_ext - 1) / max_ext;
659 bytes += nr_ext * lut->lut_dt_conf.ddp_extent_tax;
661 /* Inflate grant space if client does not support extent-based
662 * grant allocation */
663 bytes = tgt_grant_inflate(tgd, (u64)bytes);
670 * Validate grant accounting for each incoming remote network buffer.
672 * When clients have dirtied as much space as they've been granted they
673 * fall through to sync writes. These sync writes haven't been expressed
674 * in grants and need to error with ENOSPC when there isn't room in the
675 * filesystem for them after grants are taken into account. However,
676 * writeback of the dirty data that was already granted space can write
678 * The OBD_BRW_GRANTED flag will be set in the rnb_flags of each network
679 * buffer which has been granted enough space to proceed. Buffers without
680 * this flag will fail to be written with -ENOSPC (see tgt_preprw_write().
681 * Caller must hold tgd_grant_lock spinlock.
683 * \param[in] env LU environment passed by the caller
684 * \param[in] exp export identifying the client which sent the RPC
685 * \param[in] oa incoming obdo in which we should return the pack the
687 * \param[in,out] rnb the list of network buffers
688 * \param[in] niocount the number of network buffers in the list
689 * \param[in] left the remaining free space with space already granted
692 static void tgt_grant_check(const struct lu_env *env, struct obd_export *exp,
693 struct obdo *oa, struct niobuf_remote *rnb,
694 int niocount, u64 *left)
696 struct tg_export_data *ted = &exp->exp_target_data;
697 struct obd_device *obd = exp->exp_obd;
698 struct lu_target *lut = obd->u.obt.obt_lut;
699 struct tg_grants_data *tgd = &lut->lut_tgd;
700 unsigned long ungranted = 0;
701 unsigned long granted = 0;
707 assert_spin_locked(&tgd->tgd_grant_lock);
709 if (obd->obd_recovering) {
710 /* Replaying write. Grant info have been processed already so no
711 * need to do any enforcement here. It is worth noting that only
712 * bulk writes with all rnbs having OBD_BRW_FROM_GRANT can be
713 * replayed. If one page hasn't OBD_BRW_FROM_GRANT set, then
714 * the whole bulk is written synchronously */
716 CDEBUG(D_CACHE, "Replaying write, skipping accounting\n");
717 } else if ((oa->o_valid & OBD_MD_FLFLAGS) &&
718 (oa->o_flags & OBD_FL_RECOV_RESEND)) {
719 /* Recoverable resend, grant info have already been processed as
722 CDEBUG(D_CACHE, "Recoverable resend arrived, skipping "
724 } else if (exp_grant_param_supp(exp) && oa->o_grant_used > 0) {
725 /* Client supports the new grant parameters and is telling us
726 * how much grant space it consumed for this bulk write.
727 * Although all rnbs are supposed to have the OBD_BRW_FROM_GRANT
728 * flag set, we will scan the rnb list and looks for non-cache
729 * I/O in case it changes in the future */
730 if (ted->ted_grant >= oa->o_grant_used) {
731 /* skip grant accounting for rnbs with
732 * OBD_BRW_FROM_GRANT and just used grant consumption
733 * claimed in the request */
734 granted = oa->o_grant_used;
737 /* client has used more grants for this request that
739 CERROR("%s: cli %s claims %lu GRANT, real grant %lu\n",
740 exp->exp_obd->obd_name,
741 exp->exp_client_uuid.uuid,
742 (unsigned long)oa->o_grant_used, ted->ted_grant);
744 /* check whether we can fill the gap with unallocated
746 if (*left > (oa->o_grant_used - ted->ted_grant)) {
747 /* ouf .. we are safe for now */
748 granted = ted->ted_grant;
749 ungranted = oa->o_grant_used - granted;
753 /* too bad, but we cannot afford to blow up our grant
754 * accounting. The loop below will handle each rnb in
759 for (i = 0; i < niocount; i++) {
762 if ((rnb[i].rnb_flags & OBD_BRW_FROM_GRANT)) {
764 rnb[i].rnb_flags |= OBD_BRW_GRANTED;
768 /* compute how much grant space is actually needed for
769 * this rnb, inflate grant if required */
770 bytes = tgt_grant_rnb_size(exp, lut, &rnb[i]);
771 if (ted->ted_grant >= granted + bytes) {
773 rnb[i].rnb_flags |= OBD_BRW_GRANTED;
777 CDEBUG(D_CACHE, "%s: cli %s/%p claims %ld+%d GRANT, "
778 "real grant %lu idx %d\n", obd->obd_name,
779 exp->exp_client_uuid.uuid, exp, granted, bytes,
783 if (obd->obd_recovering)
784 CERROR("%s: cli %s is replaying OST_WRITE while one rnb"
785 " hasn't OBD_BRW_FROM_GRANT set (0x%x)\n",
786 obd->obd_name, exp->exp_client_uuid.uuid,
789 /* Consume grant space on the server.
790 * Unlike above, tgt_grant_rnb_size() is called with exp = NULL
791 * so that the required grant space isn't inflated. This is
792 * done on purpose since the server can deal with large block
793 * size, unlike some clients */
794 bytes = tgt_grant_rnb_size(NULL, lut, &rnb[i]);
796 /* if enough space, pretend it was granted */
799 rnb[i].rnb_flags |= OBD_BRW_GRANTED;
803 /* We can't check for already-mapped blocks here (make sense
804 * when backend filesystem does not use COW) as it requires
805 * dropping the grant lock.
806 * Instead, we clear OBD_BRW_GRANTED and in that case we need
807 * to go through and verify if all of the blocks not marked
808 * BRW_GRANTED are already mapped and we can ignore this error.
810 rnb[i].rnb_flags &= ~OBD_BRW_GRANTED;
811 CDEBUG(D_CACHE, "%s: cli %s/%p idx %d no space for %d\n",
812 obd->obd_name, exp->exp_client_uuid.uuid, exp, i, bytes);
815 /* record in o_grant_used the actual space reserved for the I/O, will be
816 * used later in tgt_grant_commmit() */
817 oa->o_grant_used = granted + ungranted;
819 /* record space used for the I/O, will be used in tgt_grant_commmit() */
820 /* Now substract what the clients has used already. We don't subtract
821 * this from the tot_granted yet, so that other client's can't grab
822 * that space before we have actually allocated our blocks. That
823 * happens in tgt_grant_commit() after the writes are done. */
824 ted->ted_grant -= granted;
825 ted->ted_pending += oa->o_grant_used;
826 tgd->tgd_tot_granted += ungranted;
827 tgd->tgd_tot_pending += oa->o_grant_used;
830 "%s: cli %s/%p granted: %lu ungranted: %lu grant: %lu dirty: %lu"
831 "\n", obd->obd_name, exp->exp_client_uuid.uuid, exp,
832 granted, ungranted, ted->ted_grant, ted->ted_dirty);
834 if (obd->obd_recovering || (oa->o_valid & OBD_MD_FLGRANT) == 0)
835 /* don't update dirty accounting during recovery or
836 * if grant information got discarded (e.g. during resend) */
839 if (ted->ted_dirty < granted) {
840 CWARN("%s: cli %s/%p claims granted %lu > ted_dirty %lu\n",
841 obd->obd_name, exp->exp_client_uuid.uuid, exp,
842 granted, ted->ted_dirty);
843 granted = ted->ted_dirty;
845 tgd->tgd_tot_dirty -= granted;
846 ted->ted_dirty -= granted;
848 if (ted->ted_dirty < 0 || ted->ted_grant < 0 || ted->ted_pending < 0) {
849 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
850 obd->obd_name, exp->exp_client_uuid.uuid, exp,
851 ted->ted_dirty, ted->ted_pending, ted->ted_grant);
852 spin_unlock(&tgd->tgd_grant_lock);
859 * Allocate additional grant space to a client
861 * Calculate how much grant space to return to client, based on how much space
862 * is currently free and how much of that is already granted.
863 * Caller must hold tgd_grant_lock spinlock.
865 * \param[in] exp export of the client which sent the request
866 * \param[in] curgrant current grant claimed by the client
867 * \param[in] want how much grant space the client would like to
869 * \param[in] left remaining free space with granted space taken
871 * \param[in] conservative if set to true, the server should be cautious
872 * and limit how much space is granted back to the
873 * client. Otherwise, the server should try hard to
874 * satisfy the client request.
876 * \retval amount of grant space allocated
878 static long tgt_grant_alloc(struct obd_export *exp, u64 curgrant,
879 u64 want, u64 left, long chunk,
882 struct obd_device *obd = exp->exp_obd;
883 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
884 struct tg_export_data *ted = &exp->exp_target_data;
889 /* When tgd_grant_compat_disable is set, we don't grant any space to
890 * clients not supporting OBD_CONNECT_GRANT_PARAM.
891 * Otherwise, space granted to such a client is inflated since it
892 * consumes PAGE_SIZE of grant space per block */
893 if ((obd->obd_self_export != exp && !exp_grant_param_supp(exp) &&
894 tgd->tgd_grant_compat_disable) || left == 0 || exp->exp_failed)
897 if (want > 0x7fffffff) {
898 CERROR("%s: client %s/%p requesting > 2GB grant %llu\n",
899 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
903 /* Grant some fraction of the client's requested grant space so that
904 * they are not always waiting for write credits (not all of it to
905 * avoid overgranting in face of multiple RPCs in flight). This
906 * essentially will be able to control the OSC_MAX_RIF for a client.
908 * If we do have a large disparity between what the client thinks it
909 * has and what we think it has, don't grant very much and let the
910 * client consume its grant first. Either it just has lots of RPCs
911 * in flight, or it was evicted and its grants will soon be used up. */
912 if (curgrant >= want || curgrant >= ted->ted_grant + chunk)
915 if (obd->obd_recovering)
916 conservative = false;
919 /* don't grant more than 1/8th of the remaining free space in
922 grant = min(want - curgrant, left);
923 /* round grant up to the next block size */
924 grant = (grant + (1 << tgd->tgd_blockbits) - 1) &
925 ~((1ULL << tgd->tgd_blockbits) - 1);
930 /* Limit to grant_chunk if not reconnect/recovery */
931 if ((grant > chunk) && conservative)
935 * Limit grant so that export' grant does not exceed what the
936 * client would like to have by more than grants for 2 full
939 if (ted->ted_grant + grant > want + chunk)
940 grant = want + chunk - ted->ted_grant;
942 tgd->tgd_tot_granted += grant;
943 ted->ted_grant += grant;
945 if (ted->ted_grant < 0) {
946 CERROR("%s: cli %s/%p grant %ld want %llu current %llu\n",
947 obd->obd_name, exp->exp_client_uuid.uuid, exp,
948 ted->ted_grant, want, curgrant);
949 spin_unlock(&tgd->tgd_grant_lock);
954 "%s: cli %s/%p wants: %llu current grant %llu"
955 " granting: %llu\n", obd->obd_name, exp->exp_client_uuid.uuid,
956 exp, want, curgrant, grant);
958 "%s: cli %s/%p tot cached:%llu granted:%llu"
959 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
960 exp, tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
961 obd->obd_num_exports);
967 * Handle grant space allocation on client connection & reconnection.
969 * A new non-readonly connection gets an initial grant allocation equals to
970 * tgt_grant_chunk() (i.e. twice the max BRW size in most of the cases).
971 * On reconnection, grant counters between client & target are resynchronized
972 * and additional space might be granted back if possible.
974 * \param[in] env LU environment provided by the caller
975 * \param[in] exp client's export which is (re)connecting
976 * \param[in,out] data obd_connect_data structure sent by the client in the
978 * \param[in] new_conn must set to true if this is a new connection and false
981 void tgt_grant_connect(const struct lu_env *env, struct obd_export *exp,
982 struct obd_connect_data *data, bool new_conn)
984 struct lu_target *lut = exp->exp_obd->u.obt.obt_lut;
985 struct tg_grants_data *tgd = &lut->lut_tgd;
986 struct tg_export_data *ted = &exp->exp_target_data;
991 int force = 0; /* can use cached data */
993 /* don't grant space to client with read-only access */
994 if (OCD_HAS_FLAG(data, RDONLY) ||
995 (!OCD_HAS_FLAG(data, GRANT_PARAM) &&
996 tgd->tgd_grant_compat_disable)) {
998 data->ocd_connect_flags &= ~(OBD_CONNECT_GRANT |
999 OBD_CONNECT_GRANT_PARAM);
1003 if (OCD_HAS_FLAG(data, GRANT_PARAM))
1004 want = data->ocd_grant;
1006 want = tgt_grant_inflate(tgd, data->ocd_grant);
1007 chunk = tgt_grant_chunk(exp, lut, data);
1009 tgt_grant_statfs(env, exp, force, &from_cache);
1011 spin_lock(&tgd->tgd_grant_lock);
1013 /* Grab free space from cached info and take out space already granted
1014 * to clients as well as reserved space */
1015 left = tgt_grant_space_left(exp);
1017 /* get fresh statfs data if we are short in ungranted space */
1018 if (from_cache && left < 32 * chunk) {
1019 spin_unlock(&tgd->tgd_grant_lock);
1020 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
1025 tgt_grant_alloc(exp, (u64)ted->ted_grant, want, left, chunk, new_conn);
1027 /* return to client its current grant */
1028 if (OCD_HAS_FLAG(data, GRANT_PARAM))
1029 data->ocd_grant = ted->ted_grant;
1032 data->ocd_grant = tgt_grant_deflate(tgd, (u64)ted->ted_grant);
1034 /* reset dirty accounting */
1035 tgd->tgd_tot_dirty -= ted->ted_dirty;
1038 if (new_conn && OCD_HAS_FLAG(data, GRANT))
1039 tgd->tgd_tot_granted_clients++;
1041 spin_unlock(&tgd->tgd_grant_lock);
1043 CDEBUG(D_CACHE, "%s: cli %s/%p ocd_grant: %d want: %llu left: %llu\n",
1044 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1045 exp, data->ocd_grant, want, left);
1049 EXPORT_SYMBOL(tgt_grant_connect);
1052 * Release all grant space attached to a given export.
1054 * Remove a client from the grant accounting totals. We also remove
1055 * the export from the obd device under the osfs and dev locks to ensure
1056 * that the tgt_grant_sanity_check() calculations are always valid.
1057 * The client should do something similar when it invalidates its import.
1059 * \param[in] exp client's export to remove from grant accounting
1061 void tgt_grant_discard(struct obd_export *exp)
1063 struct obd_device *obd = exp->exp_obd;
1064 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
1065 struct tg_export_data *ted = &exp->exp_target_data;
1067 spin_lock(&tgd->tgd_grant_lock);
1068 LASSERTF(tgd->tgd_tot_granted >= ted->ted_grant,
1069 "%s: tot_granted %llu cli %s/%p ted_grant %ld\n",
1070 obd->obd_name, tgd->tgd_tot_granted,
1071 exp->exp_client_uuid.uuid, exp, ted->ted_grant);
1072 tgd->tgd_tot_granted -= ted->ted_grant;
1074 LASSERTF(tgd->tgd_tot_pending >= ted->ted_pending,
1075 "%s: tot_pending %llu cli %s/%p ted_pending %ld\n",
1076 obd->obd_name, tgd->tgd_tot_pending,
1077 exp->exp_client_uuid.uuid, exp, ted->ted_pending);
1078 /* tgd_tot_pending is handled in tgt_grant_commit as bulk
1080 LASSERTF(tgd->tgd_tot_dirty >= ted->ted_dirty,
1081 "%s: tot_dirty %llu cli %s/%p ted_dirty %ld\n",
1082 obd->obd_name, tgd->tgd_tot_dirty,
1083 exp->exp_client_uuid.uuid, exp, ted->ted_dirty);
1084 tgd->tgd_tot_dirty -= ted->ted_dirty;
1086 spin_unlock(&tgd->tgd_grant_lock);
1088 EXPORT_SYMBOL(tgt_grant_discard);
1091 * Process grant information from incoming bulk read request.
1093 * Extract grant information packed in obdo structure (OBD_MD_FLGRANT set in
1094 * o_valid). Bulk reads usually comes with grant announcements (number of dirty
1095 * blocks, remaining amount of grant space, ...) and could also include a grant
1096 * shrink request. Unlike bulk write, no additional grant space is returned on
1097 * bulk read request.
1099 * \param[in] env is the lu environment provided by the caller
1100 * \param[in] exp is the export of the client which sent the request
1101 * \param[in,out] oa is the incoming obdo sent by the client
1103 void tgt_grant_prepare_read(const struct lu_env *env,
1104 struct obd_export *exp, struct obdo *oa)
1106 struct lu_target *lut = exp->exp_obd->u.obt.obt_lut;
1107 struct tg_grants_data *tgd = &lut->lut_tgd;
1116 if ((oa->o_valid & OBD_MD_FLGRANT) == 0)
1117 /* The read request does not contain any grant
1121 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1122 (oa->o_flags & OBD_FL_SHRINK_GRANT)) {
1123 /* To process grant shrink request, we need to know how much
1124 * available space remains on the backend filesystem.
1125 * Shrink requests are not so common, we always get fresh
1126 * statfs information. */
1127 tgt_grant_statfs(env, exp, 1, NULL);
1129 /* protect all grant counters */
1130 spin_lock(&tgd->tgd_grant_lock);
1132 /* Grab free space from cached statfs data and take out space
1133 * already granted to clients as well as reserved space */
1134 left = tgt_grant_space_left(exp);
1136 /* all set now to proceed with shrinking */
1139 /* no grant shrinking request packed in the obdo and
1140 * since we don't grant space back on reads, no point
1141 * in running statfs, so just skip it and process
1142 * incoming grant data directly. */
1143 spin_lock(&tgd->tgd_grant_lock);
1147 /* extract incoming grant information provided by the client and
1148 * inflate grant counters if required */
1149 tgt_grant_incoming(env, exp, oa, tgt_grant_chunk(exp, lut, NULL));
1151 /* unlike writes, we don't return grants back on reads unless a grant
1152 * shrink request was packed and we decided to turn it down. */
1154 tgt_grant_shrink(exp, oa, left);
1158 if (!exp_grant_param_supp(exp))
1159 oa->o_grant = tgt_grant_deflate(tgd, oa->o_grant);
1160 spin_unlock(&tgd->tgd_grant_lock);
1163 EXPORT_SYMBOL(tgt_grant_prepare_read);
1166 * Process grant information from incoming bulk write request.
1168 * This function extracts client's grant announcements from incoming bulk write
1169 * request and attempts to allocate grant space for network buffers that need it
1170 * (i.e. OBD_BRW_FROM_GRANT not set in rnb_fags).
1171 * Network buffers which aren't granted the OBD_BRW_GRANTED flag should not
1172 * proceed further and should fail with -ENOSPC.
1173 * Whenever possible, additional grant space will be returned to the client
1174 * in the bulk write reply.
1175 * tgt_grant_prepare_write() must be called before writting any buffers to
1176 * the backend storage. This function works in pair with tgt_grant_commit()
1177 * which must be invoked once all buffers have been written to disk in order
1178 * to release space from the pending grant counter.
1180 * \param[in] env LU environment provided by the caller
1181 * \param[in] exp export of the client which sent the request
1182 * \param[in] oa incoming obdo sent by the client
1183 * \param[in] rnb list of network buffers
1184 * \param[in] niocount number of network buffers in the list
1186 void tgt_grant_prepare_write(const struct lu_env *env,
1187 struct obd_export *exp, struct obdo *oa,
1188 struct niobuf_remote *rnb, int niocount)
1190 struct obd_device *obd = exp->exp_obd;
1191 struct lu_target *lut = obd->u.obt.obt_lut;
1192 struct tg_grants_data *tgd = &lut->lut_tgd;
1195 int force = 0; /* can use cached data intially */
1196 long chunk = tgt_grant_chunk(exp, lut, NULL);
1201 /* get statfs information from OSD layer */
1202 tgt_grant_statfs(env, exp, force, &from_cache);
1204 spin_lock(&tgd->tgd_grant_lock); /* protect all grant counters */
1206 /* Grab free space from cached statfs data and take out space already
1207 * granted to clients as well as reserved space */
1208 left = tgt_grant_space_left(exp);
1210 /* Get fresh statfs data if we are short in ungranted space */
1211 if (from_cache && left < 32 * chunk) {
1212 spin_unlock(&tgd->tgd_grant_lock);
1213 CDEBUG(D_CACHE, "%s: fs has no space left and statfs too old\n",
1219 /* When close to free space exhaustion, trigger a sync to force
1220 * writeback cache to consume required space immediately and release as
1221 * much space as possible. */
1222 if (!obd->obd_recovering && force != 2 && left < chunk) {
1223 bool from_grant = true;
1226 /* That said, it is worth running a sync only if some pages did
1227 * not consume grant space on the client and could thus fail
1228 * with ENOSPC later in tgt_grant_check() */
1229 for (i = 0; i < niocount; i++)
1230 if (!(rnb[i].rnb_flags & OBD_BRW_FROM_GRANT))
1234 /* at least one network buffer requires acquiring grant
1235 * space on the server */
1236 spin_unlock(&tgd->tgd_grant_lock);
1237 /* discard errors, at least we tried ... */
1238 dt_sync(env, lut->lut_bottom);
1244 /* extract incoming grant information provided by the client,
1245 * and inflate grant counters if required */
1246 tgt_grant_incoming(env, exp, oa, chunk);
1249 tgt_grant_check(env, exp, oa, rnb, niocount, &left);
1251 if (!(oa->o_valid & OBD_MD_FLGRANT)) {
1252 spin_unlock(&tgd->tgd_grant_lock);
1256 /* if OBD_FL_SHRINK_GRANT is set, the client is willing to release some
1258 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1259 (oa->o_flags & OBD_FL_SHRINK_GRANT))
1260 tgt_grant_shrink(exp, oa, left);
1262 /* grant more space back to the client if possible */
1263 oa->o_grant = tgt_grant_alloc(exp, oa->o_grant, oa->o_undirty,
1266 if (!exp_grant_param_supp(exp))
1267 oa->o_grant = tgt_grant_deflate(tgd, oa->o_grant);
1268 spin_unlock(&tgd->tgd_grant_lock);
1271 EXPORT_SYMBOL(tgt_grant_prepare_write);
1274 * Consume grant space reserved for object creation.
1276 * Grant space is allocated to the local self export for object precreation.
1277 * This is required to prevent object precreation from consuming grant space
1278 * allocated to client nodes for the data writeback cache.
1279 * This function consumes enough space to create \a nr objects and allocates
1280 * more grant space to the self export for future precreation requests, if
1283 * \param[in] env LU environment provided by the caller
1284 * \param[in] exp export holding the grant space for precreation (= self
1286 * \param[in] nr number of objects to be created
1288 * \retval >= 0 amount of grant space allocated to the precreate request
1289 * \retval -ENOSPC on failure
1291 long tgt_grant_create(const struct lu_env *env, struct obd_export *exp, s64 *nr)
1293 struct lu_target *lut = exp->exp_obd->u.obt.obt_lut;
1294 struct tg_grants_data *tgd = &lut->lut_tgd;
1295 struct tg_export_data *ted = &exp->exp_target_data;
1297 unsigned long wanted;
1298 unsigned long granted;
1301 if (exp->exp_obd->obd_recovering ||
1302 lut->lut_dt_conf.ddp_inodespace == 0)
1303 /* don't enforce grant during recovery */
1306 /* Update statfs data if required */
1307 tgt_grant_statfs(env, exp, 1, NULL);
1309 /* protect all grant counters */
1310 spin_lock(&tgd->tgd_grant_lock);
1312 /* fail precreate request if there is not enough blocks available for
1314 if (tgd->tgd_osfs.os_bavail - (ted->ted_grant >> tgd->tgd_blockbits) <
1315 (tgd->tgd_osfs.os_blocks >> 10)) {
1316 spin_unlock(&tgd->tgd_grant_lock);
1317 CDEBUG(D_RPCTRACE, "%s: not enough space for create %llu\n",
1318 exp->exp_obd->obd_name,
1319 tgd->tgd_osfs.os_bavail * tgd->tgd_osfs.os_blocks);
1323 /* Grab free space from cached statfs data and take out space
1324 * already granted to clients as well as reserved space */
1325 left = tgt_grant_space_left(exp);
1327 /* compute how much space is required to handle the precreation
1329 wanted = *nr * lut->lut_dt_conf.ddp_inodespace;
1330 if (wanted > ted->ted_grant + left) {
1331 /* that's beyond what remains, adjust the number of objects that
1332 * can be safely precreated */
1333 wanted = ted->ted_grant + left;
1334 *nr = wanted / lut->lut_dt_conf.ddp_inodespace;
1336 /* we really have no space any more for precreation,
1337 * fail the precreate request with ENOSPC */
1338 spin_unlock(&tgd->tgd_grant_lock);
1341 /* compute space needed for the new number of creations */
1342 wanted = *nr * lut->lut_dt_conf.ddp_inodespace;
1344 LASSERT(wanted <= ted->ted_grant + left);
1346 if (wanted <= ted->ted_grant) {
1347 /* we've enough grant space to handle this precreate request */
1348 ted->ted_grant -= wanted;
1350 /* we need to take some space from the ungranted pool */
1351 tgd->tgd_tot_granted += wanted - ted->ted_grant;
1352 left -= wanted - ted->ted_grant;
1356 ted->ted_pending += granted;
1357 tgd->tgd_tot_pending += granted;
1359 /* grant more space for precreate purpose if possible. */
1360 wanted = OST_MAX_PRECREATE * lut->lut_dt_conf.ddp_inodespace / 2;
1361 if (wanted > ted->ted_grant) {
1364 /* always try to book enough space to handle a large precreate
1366 chunk = tgt_grant_chunk(exp, lut, NULL);
1367 wanted -= ted->ted_grant;
1368 tgt_grant_alloc(exp, ted->ted_grant, wanted, left, chunk,
1371 spin_unlock(&tgd->tgd_grant_lock);
1374 EXPORT_SYMBOL(tgt_grant_create);
1377 * Release grant space added to the pending counter by tgt_grant_prepare_write()
1379 * Update pending grant counter once buffers have been written to the disk.
1381 * \param[in] exp export of the client which sent the request
1382 * \param[in] pending amount of reserved space to be released
1383 * \param[in] rc return code of pre-commit operations
1385 void tgt_grant_commit(struct obd_export *exp, unsigned long pending,
1388 struct tg_grants_data *tgd = &exp->exp_obd->u.obt.obt_lut->lut_tgd;
1392 /* get space accounted in tot_pending for the I/O, set in
1393 * tgt_grant_check() */
1397 spin_lock(&tgd->tgd_grant_lock);
1398 /* Don't update statfs data for errors raised before commit (e.g.
1399 * bulk transfer failed, ...) since we know those writes have not been
1400 * processed. For other errors hit during commit, we cannot really tell
1401 * whether or not something was written, so we update statfs data.
1402 * In any case, this should not be fatal since we always get fresh
1403 * statfs data before failing a request with ENOSPC */
1405 spin_lock(&tgd->tgd_osfs_lock);
1406 /* Take pending out of cached statfs data */
1407 tgd->tgd_osfs.os_bavail -= min_t(u64,
1408 tgd->tgd_osfs.os_bavail,
1409 pending >> tgd->tgd_blockbits);
1410 if (tgd->tgd_statfs_inflight)
1411 /* someone is running statfs and want to be notified of
1412 * writes happening meanwhile */
1413 tgd->tgd_osfs_inflight += pending;
1414 spin_unlock(&tgd->tgd_osfs_lock);
1417 if (exp->exp_target_data.ted_pending < pending) {
1418 CERROR("%s: cli %s/%p ted_pending(%lu) < grant_used(%lu)\n",
1419 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
1420 exp->exp_target_data.ted_pending, pending);
1421 spin_unlock(&tgd->tgd_grant_lock);
1424 exp->exp_target_data.ted_pending -= pending;
1426 if (tgd->tgd_tot_granted < pending) {
1427 CERROR("%s: cli %s/%p tot_granted(%llu) < grant_used(%lu)\n",
1428 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
1429 tgd->tgd_tot_granted, pending);
1430 spin_unlock(&tgd->tgd_grant_lock);
1433 tgd->tgd_tot_granted -= pending;
1435 if (tgd->tgd_tot_pending < pending) {
1436 CERROR("%s: cli %s/%p tot_pending(%llu) < grant_used(%lu)\n",
1437 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
1438 tgd->tgd_tot_pending, pending);
1439 spin_unlock(&tgd->tgd_grant_lock);
1442 tgd->tgd_tot_pending -= pending;
1443 spin_unlock(&tgd->tgd_grant_lock);
1446 EXPORT_SYMBOL(tgt_grant_commit);
1448 struct tgt_grant_cb {
1449 /* commit callback structure */
1450 struct dt_txn_commit_cb tgc_cb;
1451 /* export associated with the bulk write */
1452 struct obd_export *tgc_exp;
1453 /* pending grant to be released */
1454 unsigned long tgc_granted;
1458 * Callback function for grant releasing
1460 * Release grant space reserved by the client node.
1462 * \param[in] env execution environment
1463 * \param[in] th transaction handle
1464 * \param[in] cb callback data
1465 * \param[in] err error code
1467 static void tgt_grant_commit_cb(struct lu_env *env, struct thandle *th,
1468 struct dt_txn_commit_cb *cb, int err)
1470 struct tgt_grant_cb *tgc;
1472 tgc = container_of(cb, struct tgt_grant_cb, tgc_cb);
1474 tgt_grant_commit(tgc->tgc_exp, tgc->tgc_granted, err);
1475 class_export_cb_put(tgc->tgc_exp);
1480 * Add callback for grant releasing
1482 * Register a commit callback to release grant space.
1484 * \param[in] th transaction handle
1485 * \param[in] exp OBD export of client
1486 * \param[in] granted amount of grant space to be released upon commit
1488 * \retval 0 on successful callback adding
1489 * \retval negative value on error
1491 int tgt_grant_commit_cb_add(struct thandle *th, struct obd_export *exp,
1492 unsigned long granted)
1494 struct tgt_grant_cb *tgc;
1495 struct dt_txn_commit_cb *dcb;
1503 tgc->tgc_exp = class_export_cb_get(exp);
1504 tgc->tgc_granted = granted;
1507 dcb->dcb_func = tgt_grant_commit_cb;
1508 INIT_LIST_HEAD(&dcb->dcb_linkage);
1509 strlcpy(dcb->dcb_name, "tgt_grant_commit_cb", sizeof(dcb->dcb_name));
1511 rc = dt_trans_cb_add(th, dcb);
1513 class_export_cb_put(tgc->tgc_exp);
1519 EXPORT_SYMBOL(tgt_grant_commit_cb_add);
1522 * Show estimate of total amount of dirty data on clients.
1524 * \param[in] m seq_file handle
1525 * \param[in] data unused for single entry
1527 * \retval 0 on success
1528 * \retval negative value on error
1530 int tgt_tot_dirty_seq_show(struct seq_file *m, void *data)
1532 struct obd_device *obd = m->private;
1533 struct tg_grants_data *tgd;
1535 LASSERT(obd != NULL);
1536 tgd = &obd->u.obt.obt_lut->lut_tgd;
1537 seq_printf(m, "%llu\n", tgd->tgd_tot_dirty);
1540 EXPORT_SYMBOL(tgt_tot_dirty_seq_show);
1543 * Show total amount of space granted to clients.
1545 * \param[in] m seq_file handle
1546 * \param[in] data unused for single entry
1548 * \retval 0 on success
1549 * \retval negative value on error
1551 int tgt_tot_granted_seq_show(struct seq_file *m, void *data)
1553 struct obd_device *obd = m->private;
1554 struct tg_grants_data *tgd;
1556 LASSERT(obd != NULL);
1557 tgd = &obd->u.obt.obt_lut->lut_tgd;
1558 seq_printf(m, "%llu\n", tgd->tgd_tot_granted);
1561 EXPORT_SYMBOL(tgt_tot_granted_seq_show);
1564 * Show total amount of space used by IO in progress.
1566 * \param[in] m seq_file handle
1567 * \param[in] data unused for single entry
1569 * \retval 0 on success
1570 * \retval negative value on error
1572 int tgt_tot_pending_seq_show(struct seq_file *m, void *data)
1574 struct obd_device *obd = m->private;
1575 struct tg_grants_data *tgd;
1577 LASSERT(obd != NULL);
1578 tgd = &obd->u.obt.obt_lut->lut_tgd;
1579 seq_printf(m, "%llu\n", tgd->tgd_tot_pending);
1582 EXPORT_SYMBOL(tgt_tot_pending_seq_show);
1585 * Show if grants compatibility mode is disabled.
1587 * When tgd_grant_compat_disable is set, we don't grant any space to clients
1588 * not supporting OBD_CONNECT_GRANT_PARAM. Otherwise, space granted to such
1589 * a client is inflated since it consumes PAGE_SIZE of grant space per
1590 * block, (i.e. typically 4kB units), but underlaying file system might have
1591 * block size bigger than page size, e.g. ZFS. See LU-2049 for details.
1593 * \param[in] m seq_file handle
1594 * \param[in] data unused for single entry
1596 * \retval 0 on success
1597 * \retval negative value on error
1599 int tgt_grant_compat_disable_seq_show(struct seq_file *m, void *data)
1601 struct obd_device *obd = m->private;
1602 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
1604 seq_printf(m, "%u\n", tgd->tgd_grant_compat_disable);
1607 EXPORT_SYMBOL(tgt_grant_compat_disable_seq_show);
1610 * Change grant compatibility mode.
1612 * Setting tgd_grant_compat_disable prohibit any space granting to clients
1613 * not supporting OBD_CONNECT_GRANT_PARAM. See details above.
1615 * \param[in] file proc file
1616 * \param[in] buffer string which represents mode
1617 * 1: disable compatibility mode
1618 * 0: enable compatibility mode
1619 * \param[in] count \a buffer length
1620 * \param[in] off unused for single entry
1622 * \retval \a count on success
1623 * \retval negative number on error
1625 ssize_t tgt_grant_compat_disable_seq_write(struct file *file,
1626 const char __user *buffer,
1627 size_t count, loff_t *off)
1629 struct seq_file *m = file->private_data;
1630 struct obd_device *obd = m->private;
1631 struct tg_grants_data *tgd = &obd->u.obt.obt_lut->lut_tgd;
1635 rc = lprocfs_str_to_s64(buffer, count, &val);
1642 tgd->tgd_grant_compat_disable = !!val;
1646 EXPORT_SYMBOL(tgt_grant_compat_disable_seq_write);