blob: a3e5b6a13d545fa34b94bd863f951de6fd63d8cd [file] [log] [blame]
Qu Wenruocac06d82021-01-26 16:33:47 +08001// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/slab.h>
4#include "ctree.h"
5#include "subpage.h"
6
7int 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;
11
12 /*
13 * We have cases like a dummy extent buffer page, which is not mappped
14 * and doesn't need to be locked.
15 */
16 if (page->mapping)
17 ASSERT(PageLocked(page));
18 /* Either not subpage, or the page already has private attached */
19 if (fs_info->sectorsize == PAGE_SIZE || PagePrivate(page))
20 return 0;
21
22 subpage = kzalloc(sizeof(struct btrfs_subpage), GFP_NOFS);
23 if (!subpage)
24 return -ENOMEM;
25
26 spin_lock_init(&subpage->lock);
27 attach_page_private(page, subpage);
28 return 0;
29}
30
31void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
32 struct page *page)
33{
34 struct btrfs_subpage *subpage;
35
36 /* Either not subpage, or already detached */
37 if (fs_info->sectorsize == PAGE_SIZE || !PagePrivate(page))
38 return;
39
40 subpage = (struct btrfs_subpage *)detach_page_private(page);
41 ASSERT(subpage);
42 kfree(subpage);
43}