btrfs: make attach_extent_buffer_page() handle subpage case
[linux-2.6-microblaze.git] / fs / btrfs / subpage.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/slab.h>
4 #include "ctree.h"
5 #include "subpage.h"
6
7 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
8                          struct page *page, enum btrfs_subpage_type type)
9 {
10         struct btrfs_subpage *subpage = NULL;
11         int ret;
12
13         /*
14          * We have cases like a dummy extent buffer page, which is not mappped
15          * and doesn't need to be locked.
16          */
17         if (page->mapping)
18                 ASSERT(PageLocked(page));
19         /* Either not subpage, or the page already has private attached */
20         if (fs_info->sectorsize == PAGE_SIZE || PagePrivate(page))
21                 return 0;
22
23         ret = btrfs_alloc_subpage(fs_info, &subpage, type);
24         if (ret < 0)
25                 return ret;
26         attach_page_private(page, subpage);
27         return 0;
28 }
29
30 void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
31                           struct page *page)
32 {
33         struct btrfs_subpage *subpage;
34
35         /* Either not subpage, or already detached */
36         if (fs_info->sectorsize == PAGE_SIZE || !PagePrivate(page))
37                 return;
38
39         subpage = (struct btrfs_subpage *)detach_page_private(page);
40         ASSERT(subpage);
41         btrfs_free_subpage(subpage);
42 }
43
44 int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
45                         struct btrfs_subpage **ret,
46                         enum btrfs_subpage_type type)
47 {
48         if (fs_info->sectorsize == PAGE_SIZE)
49                 return 0;
50
51         *ret = kzalloc(sizeof(struct btrfs_subpage), GFP_NOFS);
52         if (!*ret)
53                 return -ENOMEM;
54         spin_lock_init(&(*ret)->lock);
55         return 0;
56 }
57
58 void btrfs_free_subpage(struct btrfs_subpage *subpage)
59 {
60         kfree(subpage);
61 }