.. | .. |
---|
142 | 142 | */ |
---|
143 | 143 | |
---|
144 | 144 | static struct kmem_cache *ext4_es_cachep; |
---|
| 145 | +static struct kmem_cache *ext4_pending_cachep; |
---|
145 | 146 | |
---|
146 | 147 | static int __es_insert_extent(struct inode *inode, struct extent_status *newes); |
---|
147 | 148 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
---|
148 | | - ext4_lblk_t end); |
---|
| 149 | + ext4_lblk_t end, int *reserved); |
---|
149 | 150 | static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan); |
---|
150 | 151 | static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan, |
---|
151 | 152 | struct ext4_inode_info *locked_ei); |
---|
| 153 | +static void __revise_pending(struct inode *inode, ext4_lblk_t lblk, |
---|
| 154 | + ext4_lblk_t len); |
---|
152 | 155 | |
---|
153 | 156 | int __init ext4_init_es(void) |
---|
154 | 157 | { |
---|
.. | .. |
---|
233 | 236 | } |
---|
234 | 237 | |
---|
235 | 238 | /* |
---|
236 | | - * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering |
---|
237 | | - * @es->lblk if it exists, otherwise, the next extent after @es->lblk. |
---|
| 239 | + * ext4_es_find_extent_range - find extent with specified status within block |
---|
| 240 | + * range or next extent following block range in |
---|
| 241 | + * extents status tree |
---|
238 | 242 | * |
---|
239 | | - * @inode: the inode which owns delayed extents |
---|
240 | | - * @lblk: the offset where we start to search |
---|
241 | | - * @end: the offset where we stop to search |
---|
242 | | - * @es: delayed extent that we found |
---|
| 243 | + * @inode - file containing the range |
---|
| 244 | + * @matching_fn - pointer to function that matches extents with desired status |
---|
| 245 | + * @lblk - logical block defining start of range |
---|
| 246 | + * @end - logical block defining end of range |
---|
| 247 | + * @es - extent found, if any |
---|
| 248 | + * |
---|
| 249 | + * Find the first extent within the block range specified by @lblk and @end |
---|
| 250 | + * in the extents status tree that satisfies @matching_fn. If a match |
---|
| 251 | + * is found, it's returned in @es. If not, and a matching extent is found |
---|
| 252 | + * beyond the block range, it's returned in @es. If no match is found, an |
---|
| 253 | + * extent is returned in @es whose es_lblk, es_len, and es_pblk components |
---|
| 254 | + * are 0. |
---|
243 | 255 | */ |
---|
244 | | -void ext4_es_find_delayed_extent_range(struct inode *inode, |
---|
245 | | - ext4_lblk_t lblk, ext4_lblk_t end, |
---|
246 | | - struct extent_status *es) |
---|
| 256 | +static void __es_find_extent_range(struct inode *inode, |
---|
| 257 | + int (*matching_fn)(struct extent_status *es), |
---|
| 258 | + ext4_lblk_t lblk, ext4_lblk_t end, |
---|
| 259 | + struct extent_status *es) |
---|
247 | 260 | { |
---|
248 | 261 | struct ext4_es_tree *tree = NULL; |
---|
249 | 262 | struct extent_status *es1 = NULL; |
---|
250 | 263 | struct rb_node *node; |
---|
251 | 264 | |
---|
252 | | - BUG_ON(es == NULL); |
---|
253 | | - BUG_ON(end < lblk); |
---|
254 | | - trace_ext4_es_find_delayed_extent_range_enter(inode, lblk); |
---|
| 265 | + WARN_ON(es == NULL); |
---|
| 266 | + WARN_ON(end < lblk); |
---|
255 | 267 | |
---|
256 | | - read_lock(&EXT4_I(inode)->i_es_lock); |
---|
257 | 268 | tree = &EXT4_I(inode)->i_es_tree; |
---|
258 | 269 | |
---|
259 | | - /* find extent in cache firstly */ |
---|
| 270 | + /* see if the extent has been cached */ |
---|
260 | 271 | es->es_lblk = es->es_len = es->es_pblk = 0; |
---|
261 | | - if (tree->cache_es) { |
---|
262 | | - es1 = tree->cache_es; |
---|
263 | | - if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
---|
264 | | - es_debug("%u cached by [%u/%u) %llu %x\n", |
---|
265 | | - lblk, es1->es_lblk, es1->es_len, |
---|
266 | | - ext4_es_pblock(es1), ext4_es_status(es1)); |
---|
267 | | - goto out; |
---|
268 | | - } |
---|
| 272 | + es1 = READ_ONCE(tree->cache_es); |
---|
| 273 | + if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) { |
---|
| 274 | + es_debug("%u cached by [%u/%u) %llu %x\n", |
---|
| 275 | + lblk, es1->es_lblk, es1->es_len, |
---|
| 276 | + ext4_es_pblock(es1), ext4_es_status(es1)); |
---|
| 277 | + goto out; |
---|
269 | 278 | } |
---|
270 | 279 | |
---|
271 | 280 | es1 = __es_tree_search(&tree->root, lblk); |
---|
272 | 281 | |
---|
273 | 282 | out: |
---|
274 | | - if (es1 && !ext4_es_is_delayed(es1)) { |
---|
| 283 | + if (es1 && !matching_fn(es1)) { |
---|
275 | 284 | while ((node = rb_next(&es1->rb_node)) != NULL) { |
---|
276 | 285 | es1 = rb_entry(node, struct extent_status, rb_node); |
---|
277 | 286 | if (es1->es_lblk > end) { |
---|
278 | 287 | es1 = NULL; |
---|
279 | 288 | break; |
---|
280 | 289 | } |
---|
281 | | - if (ext4_es_is_delayed(es1)) |
---|
| 290 | + if (matching_fn(es1)) |
---|
282 | 291 | break; |
---|
283 | 292 | } |
---|
284 | 293 | } |
---|
285 | 294 | |
---|
286 | | - if (es1 && ext4_es_is_delayed(es1)) { |
---|
287 | | - tree->cache_es = es1; |
---|
| 295 | + if (es1 && matching_fn(es1)) { |
---|
| 296 | + WRITE_ONCE(tree->cache_es, es1); |
---|
288 | 297 | es->es_lblk = es1->es_lblk; |
---|
289 | 298 | es->es_len = es1->es_len; |
---|
290 | 299 | es->es_pblk = es1->es_pblk; |
---|
291 | 300 | } |
---|
292 | 301 | |
---|
| 302 | +} |
---|
| 303 | + |
---|
| 304 | +/* |
---|
| 305 | + * Locking for __es_find_extent_range() for external use |
---|
| 306 | + */ |
---|
| 307 | +void ext4_es_find_extent_range(struct inode *inode, |
---|
| 308 | + int (*matching_fn)(struct extent_status *es), |
---|
| 309 | + ext4_lblk_t lblk, ext4_lblk_t end, |
---|
| 310 | + struct extent_status *es) |
---|
| 311 | +{ |
---|
| 312 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 313 | + return; |
---|
| 314 | + |
---|
| 315 | + trace_ext4_es_find_extent_range_enter(inode, lblk); |
---|
| 316 | + |
---|
| 317 | + read_lock(&EXT4_I(inode)->i_es_lock); |
---|
| 318 | + __es_find_extent_range(inode, matching_fn, lblk, end, es); |
---|
293 | 319 | read_unlock(&EXT4_I(inode)->i_es_lock); |
---|
294 | 320 | |
---|
295 | | - trace_ext4_es_find_delayed_extent_range_exit(inode, es); |
---|
| 321 | + trace_ext4_es_find_extent_range_exit(inode, es); |
---|
| 322 | +} |
---|
| 323 | + |
---|
| 324 | +/* |
---|
| 325 | + * __es_scan_range - search block range for block with specified status |
---|
| 326 | + * in extents status tree |
---|
| 327 | + * |
---|
| 328 | + * @inode - file containing the range |
---|
| 329 | + * @matching_fn - pointer to function that matches extents with desired status |
---|
| 330 | + * @lblk - logical block defining start of range |
---|
| 331 | + * @end - logical block defining end of range |
---|
| 332 | + * |
---|
| 333 | + * Returns true if at least one block in the specified block range satisfies |
---|
| 334 | + * the criterion specified by @matching_fn, and false if not. If at least |
---|
| 335 | + * one extent has the specified status, then there is at least one block |
---|
| 336 | + * in the cluster with that status. Should only be called by code that has |
---|
| 337 | + * taken i_es_lock. |
---|
| 338 | + */ |
---|
| 339 | +static bool __es_scan_range(struct inode *inode, |
---|
| 340 | + int (*matching_fn)(struct extent_status *es), |
---|
| 341 | + ext4_lblk_t start, ext4_lblk_t end) |
---|
| 342 | +{ |
---|
| 343 | + struct extent_status es; |
---|
| 344 | + |
---|
| 345 | + __es_find_extent_range(inode, matching_fn, start, end, &es); |
---|
| 346 | + if (es.es_len == 0) |
---|
| 347 | + return false; /* no matching extent in the tree */ |
---|
| 348 | + else if (es.es_lblk <= start && |
---|
| 349 | + start < es.es_lblk + es.es_len) |
---|
| 350 | + return true; |
---|
| 351 | + else if (start <= es.es_lblk && es.es_lblk <= end) |
---|
| 352 | + return true; |
---|
| 353 | + else |
---|
| 354 | + return false; |
---|
| 355 | +} |
---|
| 356 | +/* |
---|
| 357 | + * Locking for __es_scan_range() for external use |
---|
| 358 | + */ |
---|
| 359 | +bool ext4_es_scan_range(struct inode *inode, |
---|
| 360 | + int (*matching_fn)(struct extent_status *es), |
---|
| 361 | + ext4_lblk_t lblk, ext4_lblk_t end) |
---|
| 362 | +{ |
---|
| 363 | + bool ret; |
---|
| 364 | + |
---|
| 365 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 366 | + return false; |
---|
| 367 | + |
---|
| 368 | + read_lock(&EXT4_I(inode)->i_es_lock); |
---|
| 369 | + ret = __es_scan_range(inode, matching_fn, lblk, end); |
---|
| 370 | + read_unlock(&EXT4_I(inode)->i_es_lock); |
---|
| 371 | + |
---|
| 372 | + return ret; |
---|
| 373 | +} |
---|
| 374 | + |
---|
| 375 | +/* |
---|
| 376 | + * __es_scan_clu - search cluster for block with specified status in |
---|
| 377 | + * extents status tree |
---|
| 378 | + * |
---|
| 379 | + * @inode - file containing the cluster |
---|
| 380 | + * @matching_fn - pointer to function that matches extents with desired status |
---|
| 381 | + * @lblk - logical block in cluster to be searched |
---|
| 382 | + * |
---|
| 383 | + * Returns true if at least one extent in the cluster containing @lblk |
---|
| 384 | + * satisfies the criterion specified by @matching_fn, and false if not. If at |
---|
| 385 | + * least one extent has the specified status, then there is at least one block |
---|
| 386 | + * in the cluster with that status. Should only be called by code that has |
---|
| 387 | + * taken i_es_lock. |
---|
| 388 | + */ |
---|
| 389 | +static bool __es_scan_clu(struct inode *inode, |
---|
| 390 | + int (*matching_fn)(struct extent_status *es), |
---|
| 391 | + ext4_lblk_t lblk) |
---|
| 392 | +{ |
---|
| 393 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 394 | + ext4_lblk_t lblk_start, lblk_end; |
---|
| 395 | + |
---|
| 396 | + lblk_start = EXT4_LBLK_CMASK(sbi, lblk); |
---|
| 397 | + lblk_end = lblk_start + sbi->s_cluster_ratio - 1; |
---|
| 398 | + |
---|
| 399 | + return __es_scan_range(inode, matching_fn, lblk_start, lblk_end); |
---|
| 400 | +} |
---|
| 401 | + |
---|
| 402 | +/* |
---|
| 403 | + * Locking for __es_scan_clu() for external use |
---|
| 404 | + */ |
---|
| 405 | +bool ext4_es_scan_clu(struct inode *inode, |
---|
| 406 | + int (*matching_fn)(struct extent_status *es), |
---|
| 407 | + ext4_lblk_t lblk) |
---|
| 408 | +{ |
---|
| 409 | + bool ret; |
---|
| 410 | + |
---|
| 411 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 412 | + return false; |
---|
| 413 | + |
---|
| 414 | + read_lock(&EXT4_I(inode)->i_es_lock); |
---|
| 415 | + ret = __es_scan_clu(inode, matching_fn, lblk); |
---|
| 416 | + read_unlock(&EXT4_I(inode)->i_es_lock); |
---|
| 417 | + |
---|
| 418 | + return ret; |
---|
296 | 419 | } |
---|
297 | 420 | |
---|
298 | 421 | static void ext4_es_list_add(struct inode *inode) |
---|
.. | .. |
---|
595 | 718 | * We don't need to check unwritten extent because |
---|
596 | 719 | * indirect-based file doesn't have it. |
---|
597 | 720 | */ |
---|
598 | | - BUG_ON(1); |
---|
| 721 | + BUG(); |
---|
599 | 722 | } |
---|
600 | 723 | } else if (retval == 0) { |
---|
601 | 724 | if (ext4_es_is_written(es)) { |
---|
.. | .. |
---|
664 | 787 | } |
---|
665 | 788 | p = &(*p)->rb_right; |
---|
666 | 789 | } else { |
---|
667 | | - BUG_ON(1); |
---|
| 790 | + BUG(); |
---|
668 | 791 | return -EINVAL; |
---|
669 | 792 | } |
---|
670 | 793 | } |
---|
.. | .. |
---|
694 | 817 | struct extent_status newes; |
---|
695 | 818 | ext4_lblk_t end = lblk + len - 1; |
---|
696 | 819 | int err = 0; |
---|
| 820 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 821 | + |
---|
| 822 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 823 | + return 0; |
---|
697 | 824 | |
---|
698 | 825 | es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n", |
---|
699 | 826 | lblk, len, pblk, status, inode->i_ino); |
---|
.. | .. |
---|
719 | 846 | ext4_es_insert_extent_check(inode, &newes); |
---|
720 | 847 | |
---|
721 | 848 | write_lock(&EXT4_I(inode)->i_es_lock); |
---|
722 | | - err = __es_remove_extent(inode, lblk, end); |
---|
| 849 | + err = __es_remove_extent(inode, lblk, end, NULL); |
---|
723 | 850 | if (err != 0) |
---|
724 | 851 | goto error; |
---|
725 | 852 | retry: |
---|
.. | .. |
---|
729 | 856 | goto retry; |
---|
730 | 857 | if (err == -ENOMEM && !ext4_es_is_delayed(&newes)) |
---|
731 | 858 | err = 0; |
---|
| 859 | + |
---|
| 860 | + if (sbi->s_cluster_ratio > 1 && test_opt(inode->i_sb, DELALLOC) && |
---|
| 861 | + (status & EXTENT_STATUS_WRITTEN || |
---|
| 862 | + status & EXTENT_STATUS_UNWRITTEN)) |
---|
| 863 | + __revise_pending(inode, lblk, len); |
---|
732 | 864 | |
---|
733 | 865 | error: |
---|
734 | 866 | write_unlock(&EXT4_I(inode)->i_es_lock); |
---|
.. | .. |
---|
750 | 882 | struct extent_status *es; |
---|
751 | 883 | struct extent_status newes; |
---|
752 | 884 | ext4_lblk_t end = lblk + len - 1; |
---|
| 885 | + |
---|
| 886 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 887 | + return; |
---|
753 | 888 | |
---|
754 | 889 | newes.es_lblk = lblk; |
---|
755 | 890 | newes.es_len = len; |
---|
.. | .. |
---|
777 | 912 | * Return: 1 on found, 0 on not |
---|
778 | 913 | */ |
---|
779 | 914 | int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk, |
---|
| 915 | + ext4_lblk_t *next_lblk, |
---|
780 | 916 | struct extent_status *es) |
---|
781 | 917 | { |
---|
782 | 918 | struct ext4_es_tree *tree; |
---|
.. | .. |
---|
784 | 920 | struct extent_status *es1 = NULL; |
---|
785 | 921 | struct rb_node *node; |
---|
786 | 922 | int found = 0; |
---|
| 923 | + |
---|
| 924 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 925 | + return 0; |
---|
787 | 926 | |
---|
788 | 927 | trace_ext4_es_lookup_extent_enter(inode, lblk); |
---|
789 | 928 | es_debug("lookup extent in block %u\n", lblk); |
---|
.. | .. |
---|
793 | 932 | |
---|
794 | 933 | /* find extent in cache firstly */ |
---|
795 | 934 | es->es_lblk = es->es_len = es->es_pblk = 0; |
---|
796 | | - if (tree->cache_es) { |
---|
797 | | - es1 = tree->cache_es; |
---|
798 | | - if (in_range(lblk, es1->es_lblk, es1->es_len)) { |
---|
799 | | - es_debug("%u cached by [%u/%u)\n", |
---|
800 | | - lblk, es1->es_lblk, es1->es_len); |
---|
801 | | - found = 1; |
---|
802 | | - goto out; |
---|
803 | | - } |
---|
| 935 | + es1 = READ_ONCE(tree->cache_es); |
---|
| 936 | + if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) { |
---|
| 937 | + es_debug("%u cached by [%u/%u)\n", |
---|
| 938 | + lblk, es1->es_lblk, es1->es_len); |
---|
| 939 | + found = 1; |
---|
| 940 | + goto out; |
---|
804 | 941 | } |
---|
805 | 942 | |
---|
806 | 943 | node = tree->root.rb_node; |
---|
.. | .. |
---|
825 | 962 | es->es_pblk = es1->es_pblk; |
---|
826 | 963 | if (!ext4_es_is_referenced(es1)) |
---|
827 | 964 | ext4_es_set_referenced(es1); |
---|
828 | | - stats->es_stats_cache_hits++; |
---|
| 965 | + percpu_counter_inc(&stats->es_stats_cache_hits); |
---|
| 966 | + if (next_lblk) { |
---|
| 967 | + node = rb_next(&es1->rb_node); |
---|
| 968 | + if (node) { |
---|
| 969 | + es1 = rb_entry(node, struct extent_status, |
---|
| 970 | + rb_node); |
---|
| 971 | + *next_lblk = es1->es_lblk; |
---|
| 972 | + } else |
---|
| 973 | + *next_lblk = 0; |
---|
| 974 | + } |
---|
829 | 975 | } else { |
---|
830 | | - stats->es_stats_cache_misses++; |
---|
| 976 | + percpu_counter_inc(&stats->es_stats_cache_misses); |
---|
831 | 977 | } |
---|
832 | 978 | |
---|
833 | 979 | read_unlock(&EXT4_I(inode)->i_es_lock); |
---|
.. | .. |
---|
836 | 982 | return found; |
---|
837 | 983 | } |
---|
838 | 984 | |
---|
| 985 | +struct rsvd_count { |
---|
| 986 | + int ndelonly; |
---|
| 987 | + bool first_do_lblk_found; |
---|
| 988 | + ext4_lblk_t first_do_lblk; |
---|
| 989 | + ext4_lblk_t last_do_lblk; |
---|
| 990 | + struct extent_status *left_es; |
---|
| 991 | + bool partial; |
---|
| 992 | + ext4_lblk_t lclu; |
---|
| 993 | +}; |
---|
| 994 | + |
---|
| 995 | +/* |
---|
| 996 | + * init_rsvd - initialize reserved count data before removing block range |
---|
| 997 | + * in file from extent status tree |
---|
| 998 | + * |
---|
| 999 | + * @inode - file containing range |
---|
| 1000 | + * @lblk - first block in range |
---|
| 1001 | + * @es - pointer to first extent in range |
---|
| 1002 | + * @rc - pointer to reserved count data |
---|
| 1003 | + * |
---|
| 1004 | + * Assumes es is not NULL |
---|
| 1005 | + */ |
---|
| 1006 | +static void init_rsvd(struct inode *inode, ext4_lblk_t lblk, |
---|
| 1007 | + struct extent_status *es, struct rsvd_count *rc) |
---|
| 1008 | +{ |
---|
| 1009 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1010 | + struct rb_node *node; |
---|
| 1011 | + |
---|
| 1012 | + rc->ndelonly = 0; |
---|
| 1013 | + |
---|
| 1014 | + /* |
---|
| 1015 | + * for bigalloc, note the first delonly block in the range has not |
---|
| 1016 | + * been found, record the extent containing the block to the left of |
---|
| 1017 | + * the region to be removed, if any, and note that there's no partial |
---|
| 1018 | + * cluster to track |
---|
| 1019 | + */ |
---|
| 1020 | + if (sbi->s_cluster_ratio > 1) { |
---|
| 1021 | + rc->first_do_lblk_found = false; |
---|
| 1022 | + if (lblk > es->es_lblk) { |
---|
| 1023 | + rc->left_es = es; |
---|
| 1024 | + } else { |
---|
| 1025 | + node = rb_prev(&es->rb_node); |
---|
| 1026 | + rc->left_es = node ? rb_entry(node, |
---|
| 1027 | + struct extent_status, |
---|
| 1028 | + rb_node) : NULL; |
---|
| 1029 | + } |
---|
| 1030 | + rc->partial = false; |
---|
| 1031 | + } |
---|
| 1032 | +} |
---|
| 1033 | + |
---|
| 1034 | +/* |
---|
| 1035 | + * count_rsvd - count the clusters containing delayed and not unwritten |
---|
| 1036 | + * (delonly) blocks in a range within an extent and add to |
---|
| 1037 | + * the running tally in rsvd_count |
---|
| 1038 | + * |
---|
| 1039 | + * @inode - file containing extent |
---|
| 1040 | + * @lblk - first block in range |
---|
| 1041 | + * @len - length of range in blocks |
---|
| 1042 | + * @es - pointer to extent containing clusters to be counted |
---|
| 1043 | + * @rc - pointer to reserved count data |
---|
| 1044 | + * |
---|
| 1045 | + * Tracks partial clusters found at the beginning and end of extents so |
---|
| 1046 | + * they aren't overcounted when they span adjacent extents |
---|
| 1047 | + */ |
---|
| 1048 | +static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len, |
---|
| 1049 | + struct extent_status *es, struct rsvd_count *rc) |
---|
| 1050 | +{ |
---|
| 1051 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1052 | + ext4_lblk_t i, end, nclu; |
---|
| 1053 | + |
---|
| 1054 | + if (!ext4_es_is_delonly(es)) |
---|
| 1055 | + return; |
---|
| 1056 | + |
---|
| 1057 | + WARN_ON(len <= 0); |
---|
| 1058 | + |
---|
| 1059 | + if (sbi->s_cluster_ratio == 1) { |
---|
| 1060 | + rc->ndelonly += (int) len; |
---|
| 1061 | + return; |
---|
| 1062 | + } |
---|
| 1063 | + |
---|
| 1064 | + /* bigalloc */ |
---|
| 1065 | + |
---|
| 1066 | + i = (lblk < es->es_lblk) ? es->es_lblk : lblk; |
---|
| 1067 | + end = lblk + (ext4_lblk_t) len - 1; |
---|
| 1068 | + end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end; |
---|
| 1069 | + |
---|
| 1070 | + /* record the first block of the first delonly extent seen */ |
---|
| 1071 | + if (!rc->first_do_lblk_found) { |
---|
| 1072 | + rc->first_do_lblk = i; |
---|
| 1073 | + rc->first_do_lblk_found = true; |
---|
| 1074 | + } |
---|
| 1075 | + |
---|
| 1076 | + /* update the last lblk in the region seen so far */ |
---|
| 1077 | + rc->last_do_lblk = end; |
---|
| 1078 | + |
---|
| 1079 | + /* |
---|
| 1080 | + * if we're tracking a partial cluster and the current extent |
---|
| 1081 | + * doesn't start with it, count it and stop tracking |
---|
| 1082 | + */ |
---|
| 1083 | + if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) { |
---|
| 1084 | + rc->ndelonly++; |
---|
| 1085 | + rc->partial = false; |
---|
| 1086 | + } |
---|
| 1087 | + |
---|
| 1088 | + /* |
---|
| 1089 | + * if the first cluster doesn't start on a cluster boundary but |
---|
| 1090 | + * ends on one, count it |
---|
| 1091 | + */ |
---|
| 1092 | + if (EXT4_LBLK_COFF(sbi, i) != 0) { |
---|
| 1093 | + if (end >= EXT4_LBLK_CFILL(sbi, i)) { |
---|
| 1094 | + rc->ndelonly++; |
---|
| 1095 | + rc->partial = false; |
---|
| 1096 | + i = EXT4_LBLK_CFILL(sbi, i) + 1; |
---|
| 1097 | + } |
---|
| 1098 | + } |
---|
| 1099 | + |
---|
| 1100 | + /* |
---|
| 1101 | + * if the current cluster starts on a cluster boundary, count the |
---|
| 1102 | + * number of whole delonly clusters in the extent |
---|
| 1103 | + */ |
---|
| 1104 | + if ((i + sbi->s_cluster_ratio - 1) <= end) { |
---|
| 1105 | + nclu = (end - i + 1) >> sbi->s_cluster_bits; |
---|
| 1106 | + rc->ndelonly += nclu; |
---|
| 1107 | + i += nclu << sbi->s_cluster_bits; |
---|
| 1108 | + } |
---|
| 1109 | + |
---|
| 1110 | + /* |
---|
| 1111 | + * start tracking a partial cluster if there's a partial at the end |
---|
| 1112 | + * of the current extent and we're not already tracking one |
---|
| 1113 | + */ |
---|
| 1114 | + if (!rc->partial && i <= end) { |
---|
| 1115 | + rc->partial = true; |
---|
| 1116 | + rc->lclu = EXT4_B2C(sbi, i); |
---|
| 1117 | + } |
---|
| 1118 | +} |
---|
| 1119 | + |
---|
| 1120 | +/* |
---|
| 1121 | + * __pr_tree_search - search for a pending cluster reservation |
---|
| 1122 | + * |
---|
| 1123 | + * @root - root of pending reservation tree |
---|
| 1124 | + * @lclu - logical cluster to search for |
---|
| 1125 | + * |
---|
| 1126 | + * Returns the pending reservation for the cluster identified by @lclu |
---|
| 1127 | + * if found. If not, returns a reservation for the next cluster if any, |
---|
| 1128 | + * and if not, returns NULL. |
---|
| 1129 | + */ |
---|
| 1130 | +static struct pending_reservation *__pr_tree_search(struct rb_root *root, |
---|
| 1131 | + ext4_lblk_t lclu) |
---|
| 1132 | +{ |
---|
| 1133 | + struct rb_node *node = root->rb_node; |
---|
| 1134 | + struct pending_reservation *pr = NULL; |
---|
| 1135 | + |
---|
| 1136 | + while (node) { |
---|
| 1137 | + pr = rb_entry(node, struct pending_reservation, rb_node); |
---|
| 1138 | + if (lclu < pr->lclu) |
---|
| 1139 | + node = node->rb_left; |
---|
| 1140 | + else if (lclu > pr->lclu) |
---|
| 1141 | + node = node->rb_right; |
---|
| 1142 | + else |
---|
| 1143 | + return pr; |
---|
| 1144 | + } |
---|
| 1145 | + if (pr && lclu < pr->lclu) |
---|
| 1146 | + return pr; |
---|
| 1147 | + if (pr && lclu > pr->lclu) { |
---|
| 1148 | + node = rb_next(&pr->rb_node); |
---|
| 1149 | + return node ? rb_entry(node, struct pending_reservation, |
---|
| 1150 | + rb_node) : NULL; |
---|
| 1151 | + } |
---|
| 1152 | + return NULL; |
---|
| 1153 | +} |
---|
| 1154 | + |
---|
| 1155 | +/* |
---|
| 1156 | + * get_rsvd - calculates and returns the number of cluster reservations to be |
---|
| 1157 | + * released when removing a block range from the extent status tree |
---|
| 1158 | + * and releases any pending reservations within the range |
---|
| 1159 | + * |
---|
| 1160 | + * @inode - file containing block range |
---|
| 1161 | + * @end - last block in range |
---|
| 1162 | + * @right_es - pointer to extent containing next block beyond end or NULL |
---|
| 1163 | + * @rc - pointer to reserved count data |
---|
| 1164 | + * |
---|
| 1165 | + * The number of reservations to be released is equal to the number of |
---|
| 1166 | + * clusters containing delayed and not unwritten (delonly) blocks within |
---|
| 1167 | + * the range, minus the number of clusters still containing delonly blocks |
---|
| 1168 | + * at the ends of the range, and minus the number of pending reservations |
---|
| 1169 | + * within the range. |
---|
| 1170 | + */ |
---|
| 1171 | +static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end, |
---|
| 1172 | + struct extent_status *right_es, |
---|
| 1173 | + struct rsvd_count *rc) |
---|
| 1174 | +{ |
---|
| 1175 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1176 | + struct pending_reservation *pr; |
---|
| 1177 | + struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree; |
---|
| 1178 | + struct rb_node *node; |
---|
| 1179 | + ext4_lblk_t first_lclu, last_lclu; |
---|
| 1180 | + bool left_delonly, right_delonly, count_pending; |
---|
| 1181 | + struct extent_status *es; |
---|
| 1182 | + |
---|
| 1183 | + if (sbi->s_cluster_ratio > 1) { |
---|
| 1184 | + /* count any remaining partial cluster */ |
---|
| 1185 | + if (rc->partial) |
---|
| 1186 | + rc->ndelonly++; |
---|
| 1187 | + |
---|
| 1188 | + if (rc->ndelonly == 0) |
---|
| 1189 | + return 0; |
---|
| 1190 | + |
---|
| 1191 | + first_lclu = EXT4_B2C(sbi, rc->first_do_lblk); |
---|
| 1192 | + last_lclu = EXT4_B2C(sbi, rc->last_do_lblk); |
---|
| 1193 | + |
---|
| 1194 | + /* |
---|
| 1195 | + * decrease the delonly count by the number of clusters at the |
---|
| 1196 | + * ends of the range that still contain delonly blocks - |
---|
| 1197 | + * these clusters still need to be reserved |
---|
| 1198 | + */ |
---|
| 1199 | + left_delonly = right_delonly = false; |
---|
| 1200 | + |
---|
| 1201 | + es = rc->left_es; |
---|
| 1202 | + while (es && ext4_es_end(es) >= |
---|
| 1203 | + EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) { |
---|
| 1204 | + if (ext4_es_is_delonly(es)) { |
---|
| 1205 | + rc->ndelonly--; |
---|
| 1206 | + left_delonly = true; |
---|
| 1207 | + break; |
---|
| 1208 | + } |
---|
| 1209 | + node = rb_prev(&es->rb_node); |
---|
| 1210 | + if (!node) |
---|
| 1211 | + break; |
---|
| 1212 | + es = rb_entry(node, struct extent_status, rb_node); |
---|
| 1213 | + } |
---|
| 1214 | + if (right_es && (!left_delonly || first_lclu != last_lclu)) { |
---|
| 1215 | + if (end < ext4_es_end(right_es)) { |
---|
| 1216 | + es = right_es; |
---|
| 1217 | + } else { |
---|
| 1218 | + node = rb_next(&right_es->rb_node); |
---|
| 1219 | + es = node ? rb_entry(node, struct extent_status, |
---|
| 1220 | + rb_node) : NULL; |
---|
| 1221 | + } |
---|
| 1222 | + while (es && es->es_lblk <= |
---|
| 1223 | + EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) { |
---|
| 1224 | + if (ext4_es_is_delonly(es)) { |
---|
| 1225 | + rc->ndelonly--; |
---|
| 1226 | + right_delonly = true; |
---|
| 1227 | + break; |
---|
| 1228 | + } |
---|
| 1229 | + node = rb_next(&es->rb_node); |
---|
| 1230 | + if (!node) |
---|
| 1231 | + break; |
---|
| 1232 | + es = rb_entry(node, struct extent_status, |
---|
| 1233 | + rb_node); |
---|
| 1234 | + } |
---|
| 1235 | + } |
---|
| 1236 | + |
---|
| 1237 | + /* |
---|
| 1238 | + * Determine the block range that should be searched for |
---|
| 1239 | + * pending reservations, if any. Clusters on the ends of the |
---|
| 1240 | + * original removed range containing delonly blocks are |
---|
| 1241 | + * excluded. They've already been accounted for and it's not |
---|
| 1242 | + * possible to determine if an associated pending reservation |
---|
| 1243 | + * should be released with the information available in the |
---|
| 1244 | + * extents status tree. |
---|
| 1245 | + */ |
---|
| 1246 | + if (first_lclu == last_lclu) { |
---|
| 1247 | + if (left_delonly | right_delonly) |
---|
| 1248 | + count_pending = false; |
---|
| 1249 | + else |
---|
| 1250 | + count_pending = true; |
---|
| 1251 | + } else { |
---|
| 1252 | + if (left_delonly) |
---|
| 1253 | + first_lclu++; |
---|
| 1254 | + if (right_delonly) |
---|
| 1255 | + last_lclu--; |
---|
| 1256 | + if (first_lclu <= last_lclu) |
---|
| 1257 | + count_pending = true; |
---|
| 1258 | + else |
---|
| 1259 | + count_pending = false; |
---|
| 1260 | + } |
---|
| 1261 | + |
---|
| 1262 | + /* |
---|
| 1263 | + * a pending reservation found between first_lclu and last_lclu |
---|
| 1264 | + * represents an allocated cluster that contained at least one |
---|
| 1265 | + * delonly block, so the delonly total must be reduced by one |
---|
| 1266 | + * for each pending reservation found and released |
---|
| 1267 | + */ |
---|
| 1268 | + if (count_pending) { |
---|
| 1269 | + pr = __pr_tree_search(&tree->root, first_lclu); |
---|
| 1270 | + while (pr && pr->lclu <= last_lclu) { |
---|
| 1271 | + rc->ndelonly--; |
---|
| 1272 | + node = rb_next(&pr->rb_node); |
---|
| 1273 | + rb_erase(&pr->rb_node, &tree->root); |
---|
| 1274 | + kmem_cache_free(ext4_pending_cachep, pr); |
---|
| 1275 | + if (!node) |
---|
| 1276 | + break; |
---|
| 1277 | + pr = rb_entry(node, struct pending_reservation, |
---|
| 1278 | + rb_node); |
---|
| 1279 | + } |
---|
| 1280 | + } |
---|
| 1281 | + } |
---|
| 1282 | + return rc->ndelonly; |
---|
| 1283 | +} |
---|
| 1284 | + |
---|
| 1285 | + |
---|
| 1286 | +/* |
---|
| 1287 | + * __es_remove_extent - removes block range from extent status tree |
---|
| 1288 | + * |
---|
| 1289 | + * @inode - file containing range |
---|
| 1290 | + * @lblk - first block in range |
---|
| 1291 | + * @end - last block in range |
---|
| 1292 | + * @reserved - number of cluster reservations released |
---|
| 1293 | + * |
---|
| 1294 | + * If @reserved is not NULL and delayed allocation is enabled, counts |
---|
| 1295 | + * block/cluster reservations freed by removing range and if bigalloc |
---|
| 1296 | + * enabled cancels pending reservations as needed. Returns 0 on success, |
---|
| 1297 | + * error code on failure. |
---|
| 1298 | + */ |
---|
839 | 1299 | static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
---|
840 | | - ext4_lblk_t end) |
---|
| 1300 | + ext4_lblk_t end, int *reserved) |
---|
841 | 1301 | { |
---|
842 | 1302 | struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
---|
843 | 1303 | struct rb_node *node; |
---|
.. | .. |
---|
846 | 1306 | ext4_lblk_t len1, len2; |
---|
847 | 1307 | ext4_fsblk_t block; |
---|
848 | 1308 | int err; |
---|
| 1309 | + bool count_reserved = true; |
---|
| 1310 | + struct rsvd_count rc; |
---|
849 | 1311 | |
---|
| 1312 | + if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC)) |
---|
| 1313 | + count_reserved = false; |
---|
850 | 1314 | retry: |
---|
851 | 1315 | err = 0; |
---|
| 1316 | + |
---|
852 | 1317 | es = __es_tree_search(&tree->root, lblk); |
---|
853 | 1318 | if (!es) |
---|
854 | 1319 | goto out; |
---|
.. | .. |
---|
857 | 1322 | |
---|
858 | 1323 | /* Simply invalidate cache_es. */ |
---|
859 | 1324 | tree->cache_es = NULL; |
---|
| 1325 | + if (count_reserved) |
---|
| 1326 | + init_rsvd(inode, lblk, es, &rc); |
---|
860 | 1327 | |
---|
861 | 1328 | orig_es.es_lblk = es->es_lblk; |
---|
862 | 1329 | orig_es.es_len = es->es_len; |
---|
.. | .. |
---|
898 | 1365 | ext4_es_store_pblock(es, block); |
---|
899 | 1366 | } |
---|
900 | 1367 | } |
---|
901 | | - goto out; |
---|
| 1368 | + if (count_reserved) |
---|
| 1369 | + count_rsvd(inode, lblk, orig_es.es_len - len1 - len2, |
---|
| 1370 | + &orig_es, &rc); |
---|
| 1371 | + goto out_get_reserved; |
---|
902 | 1372 | } |
---|
903 | 1373 | |
---|
904 | 1374 | if (len1 > 0) { |
---|
| 1375 | + if (count_reserved) |
---|
| 1376 | + count_rsvd(inode, lblk, orig_es.es_len - len1, |
---|
| 1377 | + &orig_es, &rc); |
---|
905 | 1378 | node = rb_next(&es->rb_node); |
---|
906 | 1379 | if (node) |
---|
907 | 1380 | es = rb_entry(node, struct extent_status, rb_node); |
---|
.. | .. |
---|
910 | 1383 | } |
---|
911 | 1384 | |
---|
912 | 1385 | while (es && ext4_es_end(es) <= end) { |
---|
| 1386 | + if (count_reserved) |
---|
| 1387 | + count_rsvd(inode, es->es_lblk, es->es_len, es, &rc); |
---|
913 | 1388 | node = rb_next(&es->rb_node); |
---|
914 | 1389 | rb_erase(&es->rb_node, &tree->root); |
---|
915 | 1390 | ext4_es_free_extent(inode, es); |
---|
.. | .. |
---|
924 | 1399 | ext4_lblk_t orig_len = es->es_len; |
---|
925 | 1400 | |
---|
926 | 1401 | len1 = ext4_es_end(es) - end; |
---|
| 1402 | + if (count_reserved) |
---|
| 1403 | + count_rsvd(inode, es->es_lblk, orig_len - len1, |
---|
| 1404 | + es, &rc); |
---|
927 | 1405 | es->es_lblk = end + 1; |
---|
928 | 1406 | es->es_len = len1; |
---|
929 | 1407 | if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) { |
---|
.. | .. |
---|
932 | 1410 | } |
---|
933 | 1411 | } |
---|
934 | 1412 | |
---|
| 1413 | +out_get_reserved: |
---|
| 1414 | + if (count_reserved) |
---|
| 1415 | + *reserved = get_rsvd(inode, end, es, &rc); |
---|
935 | 1416 | out: |
---|
936 | 1417 | return err; |
---|
937 | 1418 | } |
---|
938 | 1419 | |
---|
939 | 1420 | /* |
---|
940 | | - * ext4_es_remove_extent() removes a space from a extent status tree. |
---|
| 1421 | + * ext4_es_remove_extent - removes block range from extent status tree |
---|
941 | 1422 | * |
---|
942 | | - * Return 0 on success, error code on failure. |
---|
| 1423 | + * @inode - file containing range |
---|
| 1424 | + * @lblk - first block in range |
---|
| 1425 | + * @len - number of blocks to remove |
---|
| 1426 | + * |
---|
| 1427 | + * Reduces block/cluster reservation count and for bigalloc cancels pending |
---|
| 1428 | + * reservations as needed. Returns 0 on success, error code on failure. |
---|
943 | 1429 | */ |
---|
944 | 1430 | int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk, |
---|
945 | 1431 | ext4_lblk_t len) |
---|
946 | 1432 | { |
---|
947 | 1433 | ext4_lblk_t end; |
---|
948 | 1434 | int err = 0; |
---|
| 1435 | + int reserved = 0; |
---|
| 1436 | + |
---|
| 1437 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 1438 | + return 0; |
---|
949 | 1439 | |
---|
950 | 1440 | trace_ext4_es_remove_extent(inode, lblk, len); |
---|
951 | 1441 | es_debug("remove [%u/%u) from extent status tree of inode %lu\n", |
---|
.. | .. |
---|
963 | 1453 | * is reclaimed. |
---|
964 | 1454 | */ |
---|
965 | 1455 | write_lock(&EXT4_I(inode)->i_es_lock); |
---|
966 | | - err = __es_remove_extent(inode, lblk, end); |
---|
| 1456 | + err = __es_remove_extent(inode, lblk, end, &reserved); |
---|
967 | 1457 | write_unlock(&EXT4_I(inode)->i_es_lock); |
---|
968 | 1458 | ext4_es_print_tree(inode); |
---|
| 1459 | + ext4_da_release_space(inode, reserved); |
---|
969 | 1460 | return err; |
---|
970 | 1461 | } |
---|
971 | 1462 | |
---|
.. | .. |
---|
1111 | 1602 | seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n", |
---|
1112 | 1603 | percpu_counter_sum_positive(&es_stats->es_stats_all_cnt), |
---|
1113 | 1604 | percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt)); |
---|
1114 | | - seq_printf(seq, " %lu/%lu cache hits/misses\n", |
---|
1115 | | - es_stats->es_stats_cache_hits, |
---|
1116 | | - es_stats->es_stats_cache_misses); |
---|
| 1605 | + seq_printf(seq, " %lld/%lld cache hits/misses\n", |
---|
| 1606 | + percpu_counter_sum_positive(&es_stats->es_stats_cache_hits), |
---|
| 1607 | + percpu_counter_sum_positive(&es_stats->es_stats_cache_misses)); |
---|
1117 | 1608 | if (inode_cnt) |
---|
1118 | 1609 | seq_printf(seq, " %d inodes on list\n", inode_cnt); |
---|
1119 | 1610 | |
---|
.. | .. |
---|
1140 | 1631 | sbi->s_es_nr_inode = 0; |
---|
1141 | 1632 | spin_lock_init(&sbi->s_es_lock); |
---|
1142 | 1633 | sbi->s_es_stats.es_stats_shrunk = 0; |
---|
1143 | | - sbi->s_es_stats.es_stats_cache_hits = 0; |
---|
1144 | | - sbi->s_es_stats.es_stats_cache_misses = 0; |
---|
| 1634 | + err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0, |
---|
| 1635 | + GFP_KERNEL); |
---|
| 1636 | + if (err) |
---|
| 1637 | + return err; |
---|
| 1638 | + err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0, |
---|
| 1639 | + GFP_KERNEL); |
---|
| 1640 | + if (err) |
---|
| 1641 | + goto err1; |
---|
1145 | 1642 | sbi->s_es_stats.es_stats_scan_time = 0; |
---|
1146 | 1643 | sbi->s_es_stats.es_stats_max_scan_time = 0; |
---|
1147 | 1644 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL); |
---|
1148 | 1645 | if (err) |
---|
1149 | | - return err; |
---|
| 1646 | + goto err2; |
---|
1150 | 1647 | err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL); |
---|
1151 | 1648 | if (err) |
---|
1152 | | - goto err1; |
---|
| 1649 | + goto err3; |
---|
1153 | 1650 | |
---|
1154 | 1651 | sbi->s_es_shrinker.scan_objects = ext4_es_scan; |
---|
1155 | 1652 | sbi->s_es_shrinker.count_objects = ext4_es_count; |
---|
1156 | 1653 | sbi->s_es_shrinker.seeks = DEFAULT_SEEKS; |
---|
1157 | 1654 | err = register_shrinker(&sbi->s_es_shrinker); |
---|
1158 | 1655 | if (err) |
---|
1159 | | - goto err2; |
---|
| 1656 | + goto err4; |
---|
1160 | 1657 | |
---|
1161 | 1658 | return 0; |
---|
1162 | | - |
---|
1163 | | -err2: |
---|
| 1659 | +err4: |
---|
1164 | 1660 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
---|
1165 | | -err1: |
---|
| 1661 | +err3: |
---|
1166 | 1662 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
---|
| 1663 | +err2: |
---|
| 1664 | + percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses); |
---|
| 1665 | +err1: |
---|
| 1666 | + percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits); |
---|
1167 | 1667 | return err; |
---|
1168 | 1668 | } |
---|
1169 | 1669 | |
---|
1170 | 1670 | void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi) |
---|
1171 | 1671 | { |
---|
| 1672 | + percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits); |
---|
| 1673 | + percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses); |
---|
1172 | 1674 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt); |
---|
1173 | 1675 | percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt); |
---|
1174 | 1676 | unregister_shrinker(&sbi->s_es_shrinker); |
---|
.. | .. |
---|
1193 | 1695 | es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk); |
---|
1194 | 1696 | if (!es) |
---|
1195 | 1697 | goto out_wrap; |
---|
1196 | | - node = &es->rb_node; |
---|
| 1698 | + |
---|
1197 | 1699 | while (*nr_to_scan > 0) { |
---|
1198 | 1700 | if (es->es_lblk > end) { |
---|
1199 | 1701 | ei->i_es_shrink_lblk = end + 1; |
---|
.. | .. |
---|
1250 | 1752 | ei->i_es_tree.cache_es = NULL; |
---|
1251 | 1753 | return nr_shrunk; |
---|
1252 | 1754 | } |
---|
| 1755 | + |
---|
| 1756 | +/* |
---|
| 1757 | + * Called to support EXT4_IOC_CLEAR_ES_CACHE. We can only remove |
---|
| 1758 | + * discretionary entries from the extent status cache. (Some entries |
---|
| 1759 | + * must be present for proper operations.) |
---|
| 1760 | + */ |
---|
| 1761 | +void ext4_clear_inode_es(struct inode *inode) |
---|
| 1762 | +{ |
---|
| 1763 | + struct ext4_inode_info *ei = EXT4_I(inode); |
---|
| 1764 | + struct extent_status *es; |
---|
| 1765 | + struct ext4_es_tree *tree; |
---|
| 1766 | + struct rb_node *node; |
---|
| 1767 | + |
---|
| 1768 | + write_lock(&ei->i_es_lock); |
---|
| 1769 | + tree = &EXT4_I(inode)->i_es_tree; |
---|
| 1770 | + tree->cache_es = NULL; |
---|
| 1771 | + node = rb_first(&tree->root); |
---|
| 1772 | + while (node) { |
---|
| 1773 | + es = rb_entry(node, struct extent_status, rb_node); |
---|
| 1774 | + node = rb_next(node); |
---|
| 1775 | + if (!ext4_es_is_delayed(es)) { |
---|
| 1776 | + rb_erase(&es->rb_node, &tree->root); |
---|
| 1777 | + ext4_es_free_extent(inode, es); |
---|
| 1778 | + } |
---|
| 1779 | + } |
---|
| 1780 | + ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
---|
| 1781 | + write_unlock(&ei->i_es_lock); |
---|
| 1782 | +} |
---|
| 1783 | + |
---|
| 1784 | +#ifdef ES_DEBUG__ |
---|
| 1785 | +static void ext4_print_pending_tree(struct inode *inode) |
---|
| 1786 | +{ |
---|
| 1787 | + struct ext4_pending_tree *tree; |
---|
| 1788 | + struct rb_node *node; |
---|
| 1789 | + struct pending_reservation *pr; |
---|
| 1790 | + |
---|
| 1791 | + printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino); |
---|
| 1792 | + tree = &EXT4_I(inode)->i_pending_tree; |
---|
| 1793 | + node = rb_first(&tree->root); |
---|
| 1794 | + while (node) { |
---|
| 1795 | + pr = rb_entry(node, struct pending_reservation, rb_node); |
---|
| 1796 | + printk(KERN_DEBUG " %u", pr->lclu); |
---|
| 1797 | + node = rb_next(node); |
---|
| 1798 | + } |
---|
| 1799 | + printk(KERN_DEBUG "\n"); |
---|
| 1800 | +} |
---|
| 1801 | +#else |
---|
| 1802 | +#define ext4_print_pending_tree(inode) |
---|
| 1803 | +#endif |
---|
| 1804 | + |
---|
| 1805 | +int __init ext4_init_pending(void) |
---|
| 1806 | +{ |
---|
| 1807 | + ext4_pending_cachep = kmem_cache_create("ext4_pending_reservation", |
---|
| 1808 | + sizeof(struct pending_reservation), |
---|
| 1809 | + 0, (SLAB_RECLAIM_ACCOUNT), NULL); |
---|
| 1810 | + if (ext4_pending_cachep == NULL) |
---|
| 1811 | + return -ENOMEM; |
---|
| 1812 | + return 0; |
---|
| 1813 | +} |
---|
| 1814 | + |
---|
| 1815 | +void ext4_exit_pending(void) |
---|
| 1816 | +{ |
---|
| 1817 | + kmem_cache_destroy(ext4_pending_cachep); |
---|
| 1818 | +} |
---|
| 1819 | + |
---|
| 1820 | +void ext4_init_pending_tree(struct ext4_pending_tree *tree) |
---|
| 1821 | +{ |
---|
| 1822 | + tree->root = RB_ROOT; |
---|
| 1823 | +} |
---|
| 1824 | + |
---|
| 1825 | +/* |
---|
| 1826 | + * __get_pending - retrieve a pointer to a pending reservation |
---|
| 1827 | + * |
---|
| 1828 | + * @inode - file containing the pending cluster reservation |
---|
| 1829 | + * @lclu - logical cluster of interest |
---|
| 1830 | + * |
---|
| 1831 | + * Returns a pointer to a pending reservation if it's a member of |
---|
| 1832 | + * the set, and NULL if not. Must be called holding i_es_lock. |
---|
| 1833 | + */ |
---|
| 1834 | +static struct pending_reservation *__get_pending(struct inode *inode, |
---|
| 1835 | + ext4_lblk_t lclu) |
---|
| 1836 | +{ |
---|
| 1837 | + struct ext4_pending_tree *tree; |
---|
| 1838 | + struct rb_node *node; |
---|
| 1839 | + struct pending_reservation *pr = NULL; |
---|
| 1840 | + |
---|
| 1841 | + tree = &EXT4_I(inode)->i_pending_tree; |
---|
| 1842 | + node = (&tree->root)->rb_node; |
---|
| 1843 | + |
---|
| 1844 | + while (node) { |
---|
| 1845 | + pr = rb_entry(node, struct pending_reservation, rb_node); |
---|
| 1846 | + if (lclu < pr->lclu) |
---|
| 1847 | + node = node->rb_left; |
---|
| 1848 | + else if (lclu > pr->lclu) |
---|
| 1849 | + node = node->rb_right; |
---|
| 1850 | + else if (lclu == pr->lclu) |
---|
| 1851 | + return pr; |
---|
| 1852 | + } |
---|
| 1853 | + return NULL; |
---|
| 1854 | +} |
---|
| 1855 | + |
---|
| 1856 | +/* |
---|
| 1857 | + * __insert_pending - adds a pending cluster reservation to the set of |
---|
| 1858 | + * pending reservations |
---|
| 1859 | + * |
---|
| 1860 | + * @inode - file containing the cluster |
---|
| 1861 | + * @lblk - logical block in the cluster to be added |
---|
| 1862 | + * |
---|
| 1863 | + * Returns 0 on successful insertion and -ENOMEM on failure. If the |
---|
| 1864 | + * pending reservation is already in the set, returns successfully. |
---|
| 1865 | + */ |
---|
| 1866 | +static int __insert_pending(struct inode *inode, ext4_lblk_t lblk) |
---|
| 1867 | +{ |
---|
| 1868 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1869 | + struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree; |
---|
| 1870 | + struct rb_node **p = &tree->root.rb_node; |
---|
| 1871 | + struct rb_node *parent = NULL; |
---|
| 1872 | + struct pending_reservation *pr; |
---|
| 1873 | + ext4_lblk_t lclu; |
---|
| 1874 | + int ret = 0; |
---|
| 1875 | + |
---|
| 1876 | + lclu = EXT4_B2C(sbi, lblk); |
---|
| 1877 | + /* search to find parent for insertion */ |
---|
| 1878 | + while (*p) { |
---|
| 1879 | + parent = *p; |
---|
| 1880 | + pr = rb_entry(parent, struct pending_reservation, rb_node); |
---|
| 1881 | + |
---|
| 1882 | + if (lclu < pr->lclu) { |
---|
| 1883 | + p = &(*p)->rb_left; |
---|
| 1884 | + } else if (lclu > pr->lclu) { |
---|
| 1885 | + p = &(*p)->rb_right; |
---|
| 1886 | + } else { |
---|
| 1887 | + /* pending reservation already inserted */ |
---|
| 1888 | + goto out; |
---|
| 1889 | + } |
---|
| 1890 | + } |
---|
| 1891 | + |
---|
| 1892 | + pr = kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC); |
---|
| 1893 | + if (pr == NULL) { |
---|
| 1894 | + ret = -ENOMEM; |
---|
| 1895 | + goto out; |
---|
| 1896 | + } |
---|
| 1897 | + pr->lclu = lclu; |
---|
| 1898 | + |
---|
| 1899 | + rb_link_node(&pr->rb_node, parent, p); |
---|
| 1900 | + rb_insert_color(&pr->rb_node, &tree->root); |
---|
| 1901 | + |
---|
| 1902 | +out: |
---|
| 1903 | + return ret; |
---|
| 1904 | +} |
---|
| 1905 | + |
---|
| 1906 | +/* |
---|
| 1907 | + * __remove_pending - removes a pending cluster reservation from the set |
---|
| 1908 | + * of pending reservations |
---|
| 1909 | + * |
---|
| 1910 | + * @inode - file containing the cluster |
---|
| 1911 | + * @lblk - logical block in the pending cluster reservation to be removed |
---|
| 1912 | + * |
---|
| 1913 | + * Returns successfully if pending reservation is not a member of the set. |
---|
| 1914 | + */ |
---|
| 1915 | +static void __remove_pending(struct inode *inode, ext4_lblk_t lblk) |
---|
| 1916 | +{ |
---|
| 1917 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1918 | + struct pending_reservation *pr; |
---|
| 1919 | + struct ext4_pending_tree *tree; |
---|
| 1920 | + |
---|
| 1921 | + pr = __get_pending(inode, EXT4_B2C(sbi, lblk)); |
---|
| 1922 | + if (pr != NULL) { |
---|
| 1923 | + tree = &EXT4_I(inode)->i_pending_tree; |
---|
| 1924 | + rb_erase(&pr->rb_node, &tree->root); |
---|
| 1925 | + kmem_cache_free(ext4_pending_cachep, pr); |
---|
| 1926 | + } |
---|
| 1927 | +} |
---|
| 1928 | + |
---|
| 1929 | +/* |
---|
| 1930 | + * ext4_remove_pending - removes a pending cluster reservation from the set |
---|
| 1931 | + * of pending reservations |
---|
| 1932 | + * |
---|
| 1933 | + * @inode - file containing the cluster |
---|
| 1934 | + * @lblk - logical block in the pending cluster reservation to be removed |
---|
| 1935 | + * |
---|
| 1936 | + * Locking for external use of __remove_pending. |
---|
| 1937 | + */ |
---|
| 1938 | +void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk) |
---|
| 1939 | +{ |
---|
| 1940 | + struct ext4_inode_info *ei = EXT4_I(inode); |
---|
| 1941 | + |
---|
| 1942 | + write_lock(&ei->i_es_lock); |
---|
| 1943 | + __remove_pending(inode, lblk); |
---|
| 1944 | + write_unlock(&ei->i_es_lock); |
---|
| 1945 | +} |
---|
| 1946 | + |
---|
| 1947 | +/* |
---|
| 1948 | + * ext4_is_pending - determine whether a cluster has a pending reservation |
---|
| 1949 | + * on it |
---|
| 1950 | + * |
---|
| 1951 | + * @inode - file containing the cluster |
---|
| 1952 | + * @lblk - logical block in the cluster |
---|
| 1953 | + * |
---|
| 1954 | + * Returns true if there's a pending reservation for the cluster in the |
---|
| 1955 | + * set of pending reservations, and false if not. |
---|
| 1956 | + */ |
---|
| 1957 | +bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk) |
---|
| 1958 | +{ |
---|
| 1959 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 1960 | + struct ext4_inode_info *ei = EXT4_I(inode); |
---|
| 1961 | + bool ret; |
---|
| 1962 | + |
---|
| 1963 | + read_lock(&ei->i_es_lock); |
---|
| 1964 | + ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL); |
---|
| 1965 | + read_unlock(&ei->i_es_lock); |
---|
| 1966 | + |
---|
| 1967 | + return ret; |
---|
| 1968 | +} |
---|
| 1969 | + |
---|
| 1970 | +/* |
---|
| 1971 | + * ext4_es_insert_delayed_block - adds a delayed block to the extents status |
---|
| 1972 | + * tree, adding a pending reservation where |
---|
| 1973 | + * needed |
---|
| 1974 | + * |
---|
| 1975 | + * @inode - file containing the newly added block |
---|
| 1976 | + * @lblk - logical block to be added |
---|
| 1977 | + * @allocated - indicates whether a physical cluster has been allocated for |
---|
| 1978 | + * the logical cluster that contains the block |
---|
| 1979 | + * |
---|
| 1980 | + * Returns 0 on success, negative error code on failure. |
---|
| 1981 | + */ |
---|
| 1982 | +int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk, |
---|
| 1983 | + bool allocated) |
---|
| 1984 | +{ |
---|
| 1985 | + struct extent_status newes; |
---|
| 1986 | + int err = 0; |
---|
| 1987 | + |
---|
| 1988 | + if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) |
---|
| 1989 | + return 0; |
---|
| 1990 | + |
---|
| 1991 | + es_debug("add [%u/1) delayed to extent status tree of inode %lu\n", |
---|
| 1992 | + lblk, inode->i_ino); |
---|
| 1993 | + |
---|
| 1994 | + newes.es_lblk = lblk; |
---|
| 1995 | + newes.es_len = 1; |
---|
| 1996 | + ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED); |
---|
| 1997 | + trace_ext4_es_insert_delayed_block(inode, &newes, allocated); |
---|
| 1998 | + |
---|
| 1999 | + ext4_es_insert_extent_check(inode, &newes); |
---|
| 2000 | + |
---|
| 2001 | + write_lock(&EXT4_I(inode)->i_es_lock); |
---|
| 2002 | + |
---|
| 2003 | + err = __es_remove_extent(inode, lblk, lblk, NULL); |
---|
| 2004 | + if (err != 0) |
---|
| 2005 | + goto error; |
---|
| 2006 | +retry: |
---|
| 2007 | + err = __es_insert_extent(inode, &newes); |
---|
| 2008 | + if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb), |
---|
| 2009 | + 128, EXT4_I(inode))) |
---|
| 2010 | + goto retry; |
---|
| 2011 | + if (err != 0) |
---|
| 2012 | + goto error; |
---|
| 2013 | + |
---|
| 2014 | + if (allocated) |
---|
| 2015 | + __insert_pending(inode, lblk); |
---|
| 2016 | + |
---|
| 2017 | +error: |
---|
| 2018 | + write_unlock(&EXT4_I(inode)->i_es_lock); |
---|
| 2019 | + |
---|
| 2020 | + ext4_es_print_tree(inode); |
---|
| 2021 | + ext4_print_pending_tree(inode); |
---|
| 2022 | + |
---|
| 2023 | + return err; |
---|
| 2024 | +} |
---|
| 2025 | + |
---|
| 2026 | +/* |
---|
| 2027 | + * __es_delayed_clu - count number of clusters containing blocks that |
---|
| 2028 | + * are delayed only |
---|
| 2029 | + * |
---|
| 2030 | + * @inode - file containing block range |
---|
| 2031 | + * @start - logical block defining start of range |
---|
| 2032 | + * @end - logical block defining end of range |
---|
| 2033 | + * |
---|
| 2034 | + * Returns the number of clusters containing only delayed (not delayed |
---|
| 2035 | + * and unwritten) blocks in the range specified by @start and @end. Any |
---|
| 2036 | + * cluster or part of a cluster within the range and containing a delayed |
---|
| 2037 | + * and not unwritten block within the range is counted as a whole cluster. |
---|
| 2038 | + */ |
---|
| 2039 | +static unsigned int __es_delayed_clu(struct inode *inode, ext4_lblk_t start, |
---|
| 2040 | + ext4_lblk_t end) |
---|
| 2041 | +{ |
---|
| 2042 | + struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree; |
---|
| 2043 | + struct extent_status *es; |
---|
| 2044 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 2045 | + struct rb_node *node; |
---|
| 2046 | + ext4_lblk_t first_lclu, last_lclu; |
---|
| 2047 | + unsigned long long last_counted_lclu; |
---|
| 2048 | + unsigned int n = 0; |
---|
| 2049 | + |
---|
| 2050 | + /* guaranteed to be unequal to any ext4_lblk_t value */ |
---|
| 2051 | + last_counted_lclu = ~0ULL; |
---|
| 2052 | + |
---|
| 2053 | + es = __es_tree_search(&tree->root, start); |
---|
| 2054 | + |
---|
| 2055 | + while (es && (es->es_lblk <= end)) { |
---|
| 2056 | + if (ext4_es_is_delonly(es)) { |
---|
| 2057 | + if (es->es_lblk <= start) |
---|
| 2058 | + first_lclu = EXT4_B2C(sbi, start); |
---|
| 2059 | + else |
---|
| 2060 | + first_lclu = EXT4_B2C(sbi, es->es_lblk); |
---|
| 2061 | + |
---|
| 2062 | + if (ext4_es_end(es) >= end) |
---|
| 2063 | + last_lclu = EXT4_B2C(sbi, end); |
---|
| 2064 | + else |
---|
| 2065 | + last_lclu = EXT4_B2C(sbi, ext4_es_end(es)); |
---|
| 2066 | + |
---|
| 2067 | + if (first_lclu == last_counted_lclu) |
---|
| 2068 | + n += last_lclu - first_lclu; |
---|
| 2069 | + else |
---|
| 2070 | + n += last_lclu - first_lclu + 1; |
---|
| 2071 | + last_counted_lclu = last_lclu; |
---|
| 2072 | + } |
---|
| 2073 | + node = rb_next(&es->rb_node); |
---|
| 2074 | + if (!node) |
---|
| 2075 | + break; |
---|
| 2076 | + es = rb_entry(node, struct extent_status, rb_node); |
---|
| 2077 | + } |
---|
| 2078 | + |
---|
| 2079 | + return n; |
---|
| 2080 | +} |
---|
| 2081 | + |
---|
| 2082 | +/* |
---|
| 2083 | + * ext4_es_delayed_clu - count number of clusters containing blocks that |
---|
| 2084 | + * are both delayed and unwritten |
---|
| 2085 | + * |
---|
| 2086 | + * @inode - file containing block range |
---|
| 2087 | + * @lblk - logical block defining start of range |
---|
| 2088 | + * @len - number of blocks in range |
---|
| 2089 | + * |
---|
| 2090 | + * Locking for external use of __es_delayed_clu(). |
---|
| 2091 | + */ |
---|
| 2092 | +unsigned int ext4_es_delayed_clu(struct inode *inode, ext4_lblk_t lblk, |
---|
| 2093 | + ext4_lblk_t len) |
---|
| 2094 | +{ |
---|
| 2095 | + struct ext4_inode_info *ei = EXT4_I(inode); |
---|
| 2096 | + ext4_lblk_t end; |
---|
| 2097 | + unsigned int n; |
---|
| 2098 | + |
---|
| 2099 | + if (len == 0) |
---|
| 2100 | + return 0; |
---|
| 2101 | + |
---|
| 2102 | + end = lblk + len - 1; |
---|
| 2103 | + WARN_ON(end < lblk); |
---|
| 2104 | + |
---|
| 2105 | + read_lock(&ei->i_es_lock); |
---|
| 2106 | + |
---|
| 2107 | + n = __es_delayed_clu(inode, lblk, end); |
---|
| 2108 | + |
---|
| 2109 | + read_unlock(&ei->i_es_lock); |
---|
| 2110 | + |
---|
| 2111 | + return n; |
---|
| 2112 | +} |
---|
| 2113 | + |
---|
| 2114 | +/* |
---|
| 2115 | + * __revise_pending - makes, cancels, or leaves unchanged pending cluster |
---|
| 2116 | + * reservations for a specified block range depending |
---|
| 2117 | + * upon the presence or absence of delayed blocks |
---|
| 2118 | + * outside the range within clusters at the ends of the |
---|
| 2119 | + * range |
---|
| 2120 | + * |
---|
| 2121 | + * @inode - file containing the range |
---|
| 2122 | + * @lblk - logical block defining the start of range |
---|
| 2123 | + * @len - length of range in blocks |
---|
| 2124 | + * |
---|
| 2125 | + * Used after a newly allocated extent is added to the extents status tree. |
---|
| 2126 | + * Requires that the extents in the range have either written or unwritten |
---|
| 2127 | + * status. Must be called while holding i_es_lock. |
---|
| 2128 | + */ |
---|
| 2129 | +static void __revise_pending(struct inode *inode, ext4_lblk_t lblk, |
---|
| 2130 | + ext4_lblk_t len) |
---|
| 2131 | +{ |
---|
| 2132 | + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
---|
| 2133 | + ext4_lblk_t end = lblk + len - 1; |
---|
| 2134 | + ext4_lblk_t first, last; |
---|
| 2135 | + bool f_del = false, l_del = false; |
---|
| 2136 | + |
---|
| 2137 | + if (len == 0) |
---|
| 2138 | + return; |
---|
| 2139 | + |
---|
| 2140 | + /* |
---|
| 2141 | + * Two cases - block range within single cluster and block range |
---|
| 2142 | + * spanning two or more clusters. Note that a cluster belonging |
---|
| 2143 | + * to a range starting and/or ending on a cluster boundary is treated |
---|
| 2144 | + * as if it does not contain a delayed extent. The new range may |
---|
| 2145 | + * have allocated space for previously delayed blocks out to the |
---|
| 2146 | + * cluster boundary, requiring that any pre-existing pending |
---|
| 2147 | + * reservation be canceled. Because this code only looks at blocks |
---|
| 2148 | + * outside the range, it should revise pending reservations |
---|
| 2149 | + * correctly even if the extent represented by the range can't be |
---|
| 2150 | + * inserted in the extents status tree due to ENOSPC. |
---|
| 2151 | + */ |
---|
| 2152 | + |
---|
| 2153 | + if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) { |
---|
| 2154 | + first = EXT4_LBLK_CMASK(sbi, lblk); |
---|
| 2155 | + if (first != lblk) |
---|
| 2156 | + f_del = __es_scan_range(inode, &ext4_es_is_delonly, |
---|
| 2157 | + first, lblk - 1); |
---|
| 2158 | + if (f_del) { |
---|
| 2159 | + __insert_pending(inode, first); |
---|
| 2160 | + } else { |
---|
| 2161 | + last = EXT4_LBLK_CMASK(sbi, end) + |
---|
| 2162 | + sbi->s_cluster_ratio - 1; |
---|
| 2163 | + if (last != end) |
---|
| 2164 | + l_del = __es_scan_range(inode, |
---|
| 2165 | + &ext4_es_is_delonly, |
---|
| 2166 | + end + 1, last); |
---|
| 2167 | + if (l_del) |
---|
| 2168 | + __insert_pending(inode, last); |
---|
| 2169 | + else |
---|
| 2170 | + __remove_pending(inode, last); |
---|
| 2171 | + } |
---|
| 2172 | + } else { |
---|
| 2173 | + first = EXT4_LBLK_CMASK(sbi, lblk); |
---|
| 2174 | + if (first != lblk) |
---|
| 2175 | + f_del = __es_scan_range(inode, &ext4_es_is_delonly, |
---|
| 2176 | + first, lblk - 1); |
---|
| 2177 | + if (f_del) |
---|
| 2178 | + __insert_pending(inode, first); |
---|
| 2179 | + else |
---|
| 2180 | + __remove_pending(inode, first); |
---|
| 2181 | + |
---|
| 2182 | + last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1; |
---|
| 2183 | + if (last != end) |
---|
| 2184 | + l_del = __es_scan_range(inode, &ext4_es_is_delonly, |
---|
| 2185 | + end + 1, last); |
---|
| 2186 | + if (l_del) |
---|
| 2187 | + __insert_pending(inode, last); |
---|
| 2188 | + else |
---|
| 2189 | + __remove_pending(inode, last); |
---|
| 2190 | + } |
---|
| 2191 | +} |
---|