~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/f2fs/extent_cache.c

Version: ~ [ linux-5.12-rc1 ] ~ [ linux-5.11.2 ] ~ [ linux-5.10.19 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.101 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.177 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.222 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.258 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.258 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * f2fs extent cache support
  3  *
  4  * Copyright (c) 2015 Motorola Mobility
  5  * Copyright (c) 2015 Samsung Electronics
  6  * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
  7  *          Chao Yu <chao2.yu@samsung.com>
  8  *
  9  * This program is free software; you can redistribute it and/or modify
 10  * it under the terms of the GNU General Public License version 2 as
 11  * published by the Free Software Foundation.
 12  */
 13 
 14 #include <linux/fs.h>
 15 #include <linux/f2fs_fs.h>
 16 
 17 #include "f2fs.h"
 18 #include "node.h"
 19 #include <trace/events/f2fs.h>
 20 
 21 static struct kmem_cache *extent_tree_slab;
 22 static struct kmem_cache *extent_node_slab;
 23 
 24 static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
 25                                 struct extent_tree *et, struct extent_info *ei,
 26                                 struct rb_node *parent, struct rb_node **p)
 27 {
 28         struct extent_node *en;
 29 
 30         en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
 31         if (!en)
 32                 return NULL;
 33 
 34         en->ei = *ei;
 35         INIT_LIST_HEAD(&en->list);
 36         en->et = et;
 37 
 38         rb_link_node(&en->rb_node, parent, p);
 39         rb_insert_color(&en->rb_node, &et->root);
 40         atomic_inc(&et->node_cnt);
 41         atomic_inc(&sbi->total_ext_node);
 42         return en;
 43 }
 44 
 45 static void __detach_extent_node(struct f2fs_sb_info *sbi,
 46                                 struct extent_tree *et, struct extent_node *en)
 47 {
 48         rb_erase(&en->rb_node, &et->root);
 49         atomic_dec(&et->node_cnt);
 50         atomic_dec(&sbi->total_ext_node);
 51 
 52         if (et->cached_en == en)
 53                 et->cached_en = NULL;
 54         kmem_cache_free(extent_node_slab, en);
 55 }
 56 
 57 /*
 58  * Flow to release an extent_node:
 59  * 1. list_del_init
 60  * 2. __detach_extent_node
 61  * 3. kmem_cache_free.
 62  */
 63 static void __release_extent_node(struct f2fs_sb_info *sbi,
 64                         struct extent_tree *et, struct extent_node *en)
 65 {
 66         spin_lock(&sbi->extent_lock);
 67         f2fs_bug_on(sbi, list_empty(&en->list));
 68         list_del_init(&en->list);
 69         spin_unlock(&sbi->extent_lock);
 70 
 71         __detach_extent_node(sbi, et, en);
 72 }
 73 
 74 static struct extent_tree *__grab_extent_tree(struct inode *inode)
 75 {
 76         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 77         struct extent_tree *et;
 78         nid_t ino = inode->i_ino;
 79 
 80         down_write(&sbi->extent_tree_lock);
 81         et = radix_tree_lookup(&sbi->extent_tree_root, ino);
 82         if (!et) {
 83                 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
 84                 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
 85                 memset(et, 0, sizeof(struct extent_tree));
 86                 et->ino = ino;
 87                 et->root = RB_ROOT;
 88                 et->cached_en = NULL;
 89                 rwlock_init(&et->lock);
 90                 INIT_LIST_HEAD(&et->list);
 91                 atomic_set(&et->node_cnt, 0);
 92                 atomic_inc(&sbi->total_ext_tree);
 93         } else {
 94                 atomic_dec(&sbi->total_zombie_tree);
 95                 list_del_init(&et->list);
 96         }
 97         up_write(&sbi->extent_tree_lock);
 98 
 99         /* never died until evict_inode */
100         F2FS_I(inode)->extent_tree = et;
101 
102         return et;
103 }
104 
105 static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
106                                 struct extent_tree *et, unsigned int fofs)
107 {
108         struct rb_node *node = et->root.rb_node;
109         struct extent_node *en = et->cached_en;
110 
111         if (en) {
112                 struct extent_info *cei = &en->ei;
113 
114                 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
115                         stat_inc_cached_node_hit(sbi);
116                         return en;
117                 }
118         }
119 
120         while (node) {
121                 en = rb_entry(node, struct extent_node, rb_node);
122 
123                 if (fofs < en->ei.fofs) {
124                         node = node->rb_left;
125                 } else if (fofs >= en->ei.fofs + en->ei.len) {
126                         node = node->rb_right;
127                 } else {
128                         stat_inc_rbtree_node_hit(sbi);
129                         return en;
130                 }
131         }
132         return NULL;
133 }
134 
135 static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
136                                 struct extent_tree *et, struct extent_info *ei)
137 {
138         struct rb_node **p = &et->root.rb_node;
139         struct extent_node *en;
140 
141         en = __attach_extent_node(sbi, et, ei, NULL, p);
142         if (!en)
143                 return NULL;
144 
145         et->largest = en->ei;
146         et->cached_en = en;
147         return en;
148 }
149 
150 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
151                                         struct extent_tree *et)
152 {
153         struct rb_node *node, *next;
154         struct extent_node *en;
155         unsigned int count = atomic_read(&et->node_cnt);
156 
157         node = rb_first(&et->root);
158         while (node) {
159                 next = rb_next(node);
160                 en = rb_entry(node, struct extent_node, rb_node);
161                 __release_extent_node(sbi, et, en);
162                 node = next;
163         }
164 
165         return count - atomic_read(&et->node_cnt);
166 }
167 
168 static void __drop_largest_extent(struct inode *inode,
169                                         pgoff_t fofs, unsigned int len)
170 {
171         struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
172 
173         if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs)
174                 largest->len = 0;
175 }
176 
177 /* return true, if inode page is changed */
178 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
179 {
180         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
181         struct extent_tree *et;
182         struct extent_node *en;
183         struct extent_info ei;
184 
185         if (!f2fs_may_extent_tree(inode)) {
186                 /* drop largest extent */
187                 if (i_ext && i_ext->len) {
188                         i_ext->len = 0;
189                         return true;
190                 }
191                 return false;
192         }
193 
194         et = __grab_extent_tree(inode);
195 
196         if (!i_ext || !i_ext->len)
197                 return false;
198 
199         get_extent_info(&ei, i_ext);
200 
201         write_lock(&et->lock);
202         if (atomic_read(&et->node_cnt))
203                 goto out;
204 
205         en = __init_extent_tree(sbi, et, &ei);
206         if (en) {
207                 spin_lock(&sbi->extent_lock);
208                 list_add_tail(&en->list, &sbi->extent_list);
209                 spin_unlock(&sbi->extent_lock);
210         }
211 out:
212         write_unlock(&et->lock);
213         return false;
214 }
215 
216 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
217                                                         struct extent_info *ei)
218 {
219         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
220         struct extent_tree *et = F2FS_I(inode)->extent_tree;
221         struct extent_node *en;
222         bool ret = false;
223 
224         f2fs_bug_on(sbi, !et);
225 
226         trace_f2fs_lookup_extent_tree_start(inode, pgofs);
227 
228         read_lock(&et->lock);
229 
230         if (et->largest.fofs <= pgofs &&
231                         et->largest.fofs + et->largest.len > pgofs) {
232                 *ei = et->largest;
233                 ret = true;
234                 stat_inc_largest_node_hit(sbi);
235                 goto out;
236         }
237 
238         en = __lookup_extent_tree(sbi, et, pgofs);
239         if (en) {
240                 *ei = en->ei;
241                 spin_lock(&sbi->extent_lock);
242                 if (!list_empty(&en->list)) {
243                         list_move_tail(&en->list, &sbi->extent_list);
244                         et->cached_en = en;
245                 }
246                 spin_unlock(&sbi->extent_lock);
247                 ret = true;
248         }
249 out:
250         stat_inc_total_hit(sbi);
251         read_unlock(&et->lock);
252 
253         trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
254         return ret;
255 }
256 
257 
258 /*
259  * lookup extent at @fofs, if hit, return the extent
260  * if not, return NULL and
261  * @prev_ex: extent before fofs
262  * @next_ex: extent after fofs
263  * @insert_p: insert point for new extent at fofs
264  * in order to simpfy the insertion after.
265  * tree must stay unchanged between lookup and insertion.
266  */
267 static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
268                                 unsigned int fofs,
269                                 struct extent_node **prev_ex,
270                                 struct extent_node **next_ex,
271                                 struct rb_node ***insert_p,
272                                 struct rb_node **insert_parent)
273 {
274         struct rb_node **pnode = &et->root.rb_node;
275         struct rb_node *parent = NULL, *tmp_node;
276         struct extent_node *en = et->cached_en;
277 
278         *insert_p = NULL;
279         *insert_parent = NULL;
280         *prev_ex = NULL;
281         *next_ex = NULL;
282 
283         if (RB_EMPTY_ROOT(&et->root))
284                 return NULL;
285 
286         if (en) {
287                 struct extent_info *cei = &en->ei;
288 
289                 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
290                         goto lookup_neighbors;
291         }
292 
293         while (*pnode) {
294                 parent = *pnode;
295                 en = rb_entry(*pnode, struct extent_node, rb_node);
296 
297                 if (fofs < en->ei.fofs)
298                         pnode = &(*pnode)->rb_left;
299                 else if (fofs >= en->ei.fofs + en->ei.len)
300                         pnode = &(*pnode)->rb_right;
301                 else
302                         goto lookup_neighbors;
303         }
304 
305         *insert_p = pnode;
306         *insert_parent = parent;
307 
308         en = rb_entry(parent, struct extent_node, rb_node);
309         tmp_node = parent;
310         if (parent && fofs > en->ei.fofs)
311                 tmp_node = rb_next(parent);
312         *next_ex = tmp_node ?
313                 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
314 
315         tmp_node = parent;
316         if (parent && fofs < en->ei.fofs)
317                 tmp_node = rb_prev(parent);
318         *prev_ex = tmp_node ?
319                 rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
320         return NULL;
321 
322 lookup_neighbors:
323         if (fofs == en->ei.fofs) {
324                 /* lookup prev node for merging backward later */
325                 tmp_node = rb_prev(&en->rb_node);
326                 *prev_ex = tmp_node ?
327                         rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
328         }
329         if (fofs == en->ei.fofs + en->ei.len - 1) {
330                 /* lookup next node for merging frontward later */
331                 tmp_node = rb_next(&en->rb_node);
332                 *next_ex = tmp_node ?
333                         rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
334         }
335         return en;
336 }
337 
338 static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
339                                 struct extent_tree *et, struct extent_info *ei,
340                                 struct extent_node *prev_ex,
341                                 struct extent_node *next_ex)
342 {
343         struct extent_node *en = NULL;
344 
345         if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
346                 prev_ex->ei.len += ei->len;
347                 ei = &prev_ex->ei;
348                 en = prev_ex;
349         }
350 
351         if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
352                 if (en)
353                         __release_extent_node(sbi, et, prev_ex);
354                 next_ex->ei.fofs = ei->fofs;
355                 next_ex->ei.blk = ei->blk;
356                 next_ex->ei.len += ei->len;
357                 en = next_ex;
358         }
359 
360         if (!en)
361                 return NULL;
362 
363         __try_update_largest_extent(et, en);
364 
365         spin_lock(&sbi->extent_lock);
366         if (!list_empty(&en->list)) {
367                 list_move_tail(&en->list, &sbi->extent_list);
368                 et->cached_en = en;
369         }
370         spin_unlock(&sbi->extent_lock);
371         return en;
372 }
373 
374 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
375                                 struct extent_tree *et, struct extent_info *ei,
376                                 struct rb_node **insert_p,
377                                 struct rb_node *insert_parent)
378 {
379         struct rb_node **p = &et->root.rb_node;
380         struct rb_node *parent = NULL;
381         struct extent_node *en = NULL;
382 
383         if (insert_p && insert_parent) {
384                 parent = insert_parent;
385                 p = insert_p;
386                 goto do_insert;
387         }
388 
389         while (*p) {
390                 parent = *p;
391                 en = rb_entry(parent, struct extent_node, rb_node);
392 
393                 if (ei->fofs < en->ei.fofs)
394                         p = &(*p)->rb_left;
395                 else if (ei->fofs >= en->ei.fofs + en->ei.len)
396                         p = &(*p)->rb_right;
397                 else
398                         f2fs_bug_on(sbi, 1);
399         }
400 do_insert:
401         en = __attach_extent_node(sbi, et, ei, parent, p);
402         if (!en)
403                 return NULL;
404 
405         __try_update_largest_extent(et, en);
406 
407         /* update in global extent list */
408         spin_lock(&sbi->extent_lock);
409         list_add_tail(&en->list, &sbi->extent_list);
410         et->cached_en = en;
411         spin_unlock(&sbi->extent_lock);
412         return en;
413 }
414 
415 static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
416                                 pgoff_t fofs, block_t blkaddr, unsigned int len)
417 {
418         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
419         struct extent_tree *et = F2FS_I(inode)->extent_tree;
420         struct extent_node *en = NULL, *en1 = NULL;
421         struct extent_node *prev_en = NULL, *next_en = NULL;
422         struct extent_info ei, dei, prev;
423         struct rb_node **insert_p = NULL, *insert_parent = NULL;
424         unsigned int end = fofs + len;
425         unsigned int pos = (unsigned int)fofs;
426 
427         if (!et)
428                 return false;
429 
430         trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
431 
432         write_lock(&et->lock);
433 
434         if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT)) {
435                 write_unlock(&et->lock);
436                 return false;
437         }
438 
439         prev = et->largest;
440         dei.len = 0;
441 
442         /*
443          * drop largest extent before lookup, in case it's already
444          * been shrunk from extent tree
445          */
446         __drop_largest_extent(inode, fofs, len);
447 
448         /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
449         en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
450                                         &insert_p, &insert_parent);
451         if (!en)
452                 en = next_en;
453 
454         /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
455         while (en && en->ei.fofs < end) {
456                 unsigned int org_end;
457                 int parts = 0;  /* # of parts current extent split into */
458 
459                 next_en = en1 = NULL;
460 
461                 dei = en->ei;
462                 org_end = dei.fofs + dei.len;
463                 f2fs_bug_on(sbi, pos >= org_end);
464 
465                 if (pos > dei.fofs &&   pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
466                         en->ei.len = pos - en->ei.fofs;
467                         prev_en = en;
468                         parts = 1;
469                 }
470 
471                 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
472                         if (parts) {
473                                 set_extent_info(&ei, end,
474                                                 end - dei.fofs + dei.blk,
475                                                 org_end - end);
476                                 en1 = __insert_extent_tree(sbi, et, &ei,
477                                                         NULL, NULL);
478                                 next_en = en1;
479                         } else {
480                                 en->ei.fofs = end;
481                                 en->ei.blk += end - dei.fofs;
482                                 en->ei.len -= end - dei.fofs;
483                                 next_en = en;
484                         }
485                         parts++;
486                 }
487 
488                 if (!next_en) {
489                         struct rb_node *node = rb_next(&en->rb_node);
490 
491                         next_en = node ?
492                                 rb_entry(node, struct extent_node, rb_node)
493                                 : NULL;
494                 }
495 
496                 if (parts)
497                         __try_update_largest_extent(et, en);
498                 else
499                         __release_extent_node(sbi, et, en);
500 
501                 /*
502                  * if original extent is split into zero or two parts, extent
503                  * tree has been altered by deletion or insertion, therefore
504                  * invalidate pointers regard to tree.
505                  */
506                 if (parts != 1) {
507                         insert_p = NULL;
508                         insert_parent = NULL;
509                 }
510                 en = next_en;
511         }
512 
513         /* 3. update extent in extent cache */
514         if (blkaddr) {
515 
516                 set_extent_info(&ei, fofs, blkaddr, len);
517                 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
518                         __insert_extent_tree(sbi, et, &ei,
519                                                 insert_p, insert_parent);
520 
521                 /* give up extent_cache, if split and small updates happen */
522                 if (dei.len >= 1 &&
523                                 prev.len < F2FS_MIN_EXTENT_LEN &&
524                                 et->largest.len < F2FS_MIN_EXTENT_LEN) {
525                         et->largest.len = 0;
526                         set_inode_flag(F2FS_I(inode), FI_NO_EXTENT);
527                 }
528         }
529 
530         if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
531                 __free_extent_tree(sbi, et);
532 
533         write_unlock(&et->lock);
534 
535         return !__is_extent_same(&prev, &et->largest);
536 }
537 
538 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
539 {
540         struct extent_tree *et, *next;
541         struct extent_node *en;
542         unsigned int node_cnt = 0, tree_cnt = 0;
543         int remained;
544 
545         if (!test_opt(sbi, EXTENT_CACHE))
546                 return 0;
547 
548         if (!atomic_read(&sbi->total_zombie_tree))
549                 goto free_node;
550 
551         if (!down_write_trylock(&sbi->extent_tree_lock))
552                 goto out;
553 
554         /* 1. remove unreferenced extent tree */
555         list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
556                 if (atomic_read(&et->node_cnt)) {
557                         write_lock(&et->lock);
558                         node_cnt += __free_extent_tree(sbi, et);
559                         write_unlock(&et->lock);
560                 }
561                 f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
562                 list_del_init(&et->list);
563                 radix_tree_delete(&sbi->extent_tree_root, et->ino);
564                 kmem_cache_free(extent_tree_slab, et);
565                 atomic_dec(&sbi->total_ext_tree);
566                 atomic_dec(&sbi->total_zombie_tree);
567                 tree_cnt++;
568 
569                 if (node_cnt + tree_cnt >= nr_shrink)
570                         goto unlock_out;
571                 cond_resched();
572         }
573         up_write(&sbi->extent_tree_lock);
574 
575 free_node:
576         /* 2. remove LRU extent entries */
577         if (!down_write_trylock(&sbi->extent_tree_lock))
578                 goto out;
579 
580         remained = nr_shrink - (node_cnt + tree_cnt);
581 
582         spin_lock(&sbi->extent_lock);
583         for (; remained > 0; remained--) {
584                 if (list_empty(&sbi->extent_list))
585                         break;
586                 en = list_first_entry(&sbi->extent_list,
587                                         struct extent_node, list);
588                 et = en->et;
589                 if (!write_trylock(&et->lock)) {
590                         /* refresh this extent node's position in extent list */
591                         list_move_tail(&en->list, &sbi->extent_list);
592                         continue;
593                 }
594 
595                 list_del_init(&en->list);
596                 spin_unlock(&sbi->extent_lock);
597 
598                 __detach_extent_node(sbi, et, en);
599 
600                 write_unlock(&et->lock);
601                 node_cnt++;
602                 spin_lock(&sbi->extent_lock);
603         }
604         spin_unlock(&sbi->extent_lock);
605 
606 unlock_out:
607         up_write(&sbi->extent_tree_lock);
608 out:
609         trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
610 
611         return node_cnt + tree_cnt;
612 }
613 
614 unsigned int f2fs_destroy_extent_node(struct inode *inode)
615 {
616         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
617         struct extent_tree *et = F2FS_I(inode)->extent_tree;
618         unsigned int node_cnt = 0;
619 
620         if (!et || !atomic_read(&et->node_cnt))
621                 return 0;
622 
623         write_lock(&et->lock);
624         node_cnt = __free_extent_tree(sbi, et);
625         write_unlock(&et->lock);
626 
627         return node_cnt;
628 }
629 
630 void f2fs_destroy_extent_tree(struct inode *inode)
631 {
632         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
633         struct extent_tree *et = F2FS_I(inode)->extent_tree;
634         unsigned int node_cnt = 0;
635 
636         if (!et)
637                 return;
638 
639         if (inode->i_nlink && !is_bad_inode(inode) &&
640                                         atomic_read(&et->node_cnt)) {
641                 down_write(&sbi->extent_tree_lock);
642                 list_add_tail(&et->list, &sbi->zombie_list);
643                 atomic_inc(&sbi->total_zombie_tree);
644                 up_write(&sbi->extent_tree_lock);
645                 return;
646         }
647 
648         /* free all extent info belong to this extent tree */
649         node_cnt = f2fs_destroy_extent_node(inode);
650 
651         /* delete extent tree entry in radix tree */
652         down_write(&sbi->extent_tree_lock);
653         f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
654         radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
655         kmem_cache_free(extent_tree_slab, et);
656         atomic_dec(&sbi->total_ext_tree);
657         up_write(&sbi->extent_tree_lock);
658 
659         F2FS_I(inode)->extent_tree = NULL;
660 
661         trace_f2fs_destroy_extent_tree(inode, node_cnt);
662 }
663 
664 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
665                                         struct extent_info *ei)
666 {
667         if (!f2fs_may_extent_tree(inode))
668                 return false;
669 
670         return f2fs_lookup_extent_tree(inode, pgofs, ei);
671 }
672 
673 void f2fs_update_extent_cache(struct dnode_of_data *dn)
674 {
675         pgoff_t fofs;
676         block_t blkaddr;
677 
678         if (!f2fs_may_extent_tree(dn->inode))
679                 return;
680 
681         if (dn->data_blkaddr == NEW_ADDR)
682                 blkaddr = NULL_ADDR;
683         else
684                 blkaddr = dn->data_blkaddr;
685 
686         fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
687                                                                 dn->ofs_in_node;
688 
689         if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1))
690                 sync_inode_page(dn);
691 }
692 
693 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
694                                 pgoff_t fofs, block_t blkaddr, unsigned int len)
695 
696 {
697         if (!f2fs_may_extent_tree(dn->inode))
698                 return;
699 
700         if (f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len))
701                 sync_inode_page(dn);
702 }
703 
704 void init_extent_cache_info(struct f2fs_sb_info *sbi)
705 {
706         INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
707         init_rwsem(&sbi->extent_tree_lock);
708         INIT_LIST_HEAD(&sbi->extent_list);
709         spin_lock_init(&sbi->extent_lock);
710         atomic_set(&sbi->total_ext_tree, 0);
711         INIT_LIST_HEAD(&sbi->zombie_list);
712         atomic_set(&sbi->total_zombie_tree, 0);
713         atomic_set(&sbi->total_ext_node, 0);
714 }
715 
716 int __init create_extent_cache(void)
717 {
718         extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
719                         sizeof(struct extent_tree));
720         if (!extent_tree_slab)
721                 return -ENOMEM;
722         extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
723                         sizeof(struct extent_node));
724         if (!extent_node_slab) {
725                 kmem_cache_destroy(extent_tree_slab);
726                 return -ENOMEM;
727         }
728         return 0;
729 }
730 
731 void destroy_extent_cache(void)
732 {
733         kmem_cache_destroy(extent_node_slab);
734         kmem_cache_destroy(extent_tree_slab);
735 }
736 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp