btrfs-progs: move transaction implementation out of header
[platform/upstream/btrfs-progs.git] / transaction.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include "kerncompat.h"
18 #include "transaction.h"
19
20 #include "messages.h"
21
22 struct btrfs_trans_handle* btrfs_start_transaction(struct btrfs_root *root,
23                 int num_blocks)
24 {
25         struct btrfs_fs_info *fs_info = root->fs_info;
26         struct btrfs_trans_handle *h = kzalloc(sizeof(*h), GFP_NOFS);
27
28         if (!h)
29                 return ERR_PTR(-ENOMEM);
30         if (root->commit_root) {
31                 error("commit_root aleady set when starting transaction");
32                 kfree(h);
33                 return ERR_PTR(-EINVAL);
34         }
35         if (fs_info->running_transaction) {
36                 error("attempt to start transaction over already running one");
37                 kfree(h);
38                 return ERR_PTR(-EINVAL);
39         }
40         fs_info->running_transaction = h;
41         fs_info->generation++;
42         h->transid = fs_info->generation;
43         h->blocks_reserved = num_blocks;
44         root->last_trans = h->transid;
45         root->commit_root = root->node;
46         extent_buffer_get(root->node);
47
48         return h;
49 }