blob: 9749090cfbb8ac1385eccab3e4e44fd0c0778b7d [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -04002#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -04003#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
Chris Masondee26a92007-03-26 16:00:06 -040011#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040012#include <linux/swap.h>
13#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040014#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040015#include <linux/compat.h>
Chris Mason2e635a22007-03-21 11:12:56 -040016#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040017#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040018#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040019#include "btrfs_inode.h"
Chris Masonc5739bb2007-04-10 09:27:04 -040020#include "ioctl.h"
Chris Mason3a686372007-05-24 13:35:57 -040021#include "print-tree.h"
Chris Mason2e635a22007-03-21 11:12:56 -040022
Chris Masonc5739bb2007-04-10 09:27:04 -040023struct btrfs_iget_args {
24 u64 ino;
25 struct btrfs_root *root;
26};
27
Chris Mason2e635a22007-03-21 11:12:56 -040028#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040029
30static struct inode_operations btrfs_dir_inode_operations;
Chris Mason2b8d99a2007-05-24 08:34:10 -040031static struct inode_operations btrfs_symlink_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040032static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040033static struct super_operations btrfs_super_ops;
34static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040035static struct inode_operations btrfs_file_inode_operations;
36static struct address_space_operations btrfs_aops;
Chris Mason2b8d99a2007-05-24 08:34:10 -040037static struct address_space_operations btrfs_symlink_aops;
Chris Masondee26a92007-03-26 16:00:06 -040038static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040039
Chris Mason1de037a2007-05-29 15:17:08 -040040static int drop_extents(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct inode *inode,
43 u64 start, u64 end, u64 *hint_block);
44static int btrfs_get_block(struct inode *inode, sector_t iblock,
45 struct buffer_head *result, int create);
46
Chris Masonfabb5682007-06-07 22:13:21 -040047
48#define S_SHIFT 12
49static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
50 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
51 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
52 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
53 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
54 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
55 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
56 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
57};
58
Chris Masone20d96d2007-03-22 12:13:20 -040059static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040060{
Chris Mason5caf2a02007-04-02 11:20:42 -040061 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040062 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040063 struct btrfs_root *root = BTRFS_I(inode)->root;
64 struct btrfs_key location;
Chris Mason31f3c992007-04-30 15:25:45 -040065 u64 alloc_group_block;
Chris Masone20d96d2007-03-22 12:13:20 -040066 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040067
Chris Mason5caf2a02007-04-02 11:20:42 -040068 path = btrfs_alloc_path();
69 BUG_ON(!path);
70 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040071 mutex_lock(&root->fs_info->fs_mutex);
72
Chris Masond6e4a422007-04-06 15:37:36 -040073 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
74 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040075 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040076 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040077 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040078 }
Chris Mason5caf2a02007-04-02 11:20:42 -040079 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
80 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040081 struct btrfs_inode_item);
82
Chris Masone20d96d2007-03-22 12:13:20 -040083 inode->i_mode = btrfs_inode_mode(inode_item);
84 inode->i_nlink = btrfs_inode_nlink(inode_item);
85 inode->i_uid = btrfs_inode_uid(inode_item);
86 inode->i_gid = btrfs_inode_gid(inode_item);
87 inode->i_size = btrfs_inode_size(inode_item);
88 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
89 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
90 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
91 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
92 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
93 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
94 inode->i_blocks = btrfs_inode_nblocks(inode_item);
95 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason31f3c992007-04-30 15:25:45 -040096 alloc_group_block = btrfs_inode_block_group(inode_item);
Chris Mason5276aed2007-06-11 21:33:38 -040097 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
98 alloc_group_block);
Chris Mason5caf2a02007-04-02 11:20:42 -040099
Chris Mason5caf2a02007-04-02 11:20:42 -0400100 btrfs_free_path(path);
101 inode_item = NULL;
102
Chris Masonf4b9aa82007-03-27 11:05:53 -0400103 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b05da22007-04-10 12:13:09 -0400104
Chris Masone20d96d2007-03-22 12:13:20 -0400105 switch (inode->i_mode & S_IFMT) {
106#if 0
107 default:
108 init_special_inode(inode, inode->i_mode,
109 btrfs_inode_rdev(inode_item));
110 break;
111#endif
112 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -0400113 inode->i_mapping->a_ops = &btrfs_aops;
114 inode->i_fop = &btrfs_file_operations;
115 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400116 break;
117 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400118 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400119 if (root == root->fs_info->tree_root)
120 inode->i_op = &btrfs_dir_ro_inode_operations;
121 else
122 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400123 break;
124 case S_IFLNK:
Chris Mason2b8d99a2007-05-24 08:34:10 -0400125 inode->i_op = &btrfs_symlink_inode_operations;
126 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Masone20d96d2007-03-22 12:13:20 -0400127 break;
128 }
Chris Masone20d96d2007-03-22 12:13:20 -0400129 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400130
131make_bad:
132 btrfs_release_path(root, path);
133 btrfs_free_path(path);
134 mutex_unlock(&root->fs_info->fs_mutex);
135 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400136}
137
Chris Masonf68cad02007-04-24 12:44:26 -0400138static void fill_inode_item(struct btrfs_inode_item *item,
139 struct inode *inode)
140{
141 btrfs_set_inode_uid(item, inode->i_uid);
142 btrfs_set_inode_gid(item, inode->i_gid);
143 btrfs_set_inode_size(item, inode->i_size);
144 btrfs_set_inode_mode(item, inode->i_mode);
145 btrfs_set_inode_nlink(item, inode->i_nlink);
146 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
147 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
148 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
149 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
150 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
151 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
152 btrfs_set_inode_nblocks(item, inode->i_blocks);
153 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason31f3c992007-04-30 15:25:45 -0400154 btrfs_set_inode_block_group(item,
155 BTRFS_I(inode)->block_group->key.objectid);
Chris Masonf68cad02007-04-24 12:44:26 -0400156}
157
Chris Masonf68cad02007-04-24 12:44:26 -0400158static int btrfs_update_inode(struct btrfs_trans_handle *trans,
159 struct btrfs_root *root,
160 struct inode *inode)
161{
162 struct btrfs_inode_item *inode_item;
163 struct btrfs_path *path;
164 int ret;
165
166 path = btrfs_alloc_path();
167 BUG_ON(!path);
168 btrfs_init_path(path);
169 ret = btrfs_lookup_inode(trans, root, path,
170 &BTRFS_I(inode)->location, 1);
171 if (ret) {
172 if (ret > 0)
173 ret = -ENOENT;
174 goto failed;
175 }
176
177 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
178 path->slots[0],
179 struct btrfs_inode_item);
180
181 fill_inode_item(inode_item, inode);
182 btrfs_mark_buffer_dirty(path->nodes[0]);
183 ret = 0;
184failed:
185 btrfs_release_path(root, path);
186 btrfs_free_path(path);
187 return ret;
188}
189
190
Chris Mason5f443fd2007-03-27 13:42:32 -0400191static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct inode *dir,
194 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400195{
Chris Mason5caf2a02007-04-02 11:20:42 -0400196 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400197 const char *name = dentry->d_name.name;
198 int name_len = dentry->d_name.len;
Chris Mason7e381802007-04-19 15:36:27 -0400199 int ret = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400200 u64 objectid;
201 struct btrfs_dir_item *di;
202
Chris Mason5caf2a02007-04-02 11:20:42 -0400203 path = btrfs_alloc_path();
204 BUG_ON(!path);
205 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400206 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400207 name, name_len, -1);
Chris Mason7e381802007-04-19 15:36:27 -0400208 if (IS_ERR(di)) {
209 ret = PTR_ERR(di);
Chris Mason134e9732007-03-25 13:44:56 -0400210 goto err;
Chris Mason7e381802007-04-19 15:36:27 -0400211 }
212 if (!di) {
Chris Mason134e9732007-03-25 13:44:56 -0400213 ret = -ENOENT;
214 goto err;
215 }
Chris Masond6e4a422007-04-06 15:37:36 -0400216 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason7e381802007-04-19 15:36:27 -0400217 ret = btrfs_delete_one_dir_name(trans, root, path, di);
218 BUG_ON(ret);
219 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400220
Chris Mason7e381802007-04-19 15:36:27 -0400221 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
222 objectid, name, name_len, -1);
223 if (IS_ERR(di)) {
224 ret = PTR_ERR(di);
225 goto err;
226 }
227 if (!di) {
228 ret = -ENOENT;
229 goto err;
230 }
231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason134e9732007-03-25 13:44:56 -0400232 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400233
Chris Mason134e9732007-03-25 13:44:56 -0400234 dentry->d_inode->i_ctime = dir->i_ctime;
235err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400236 btrfs_free_path(path);
Chris Masonf68cad02007-04-24 12:44:26 -0400237 if (!ret) {
Chris Mason5f26f772007-04-05 10:38:44 -0400238 dir->i_size -= name_len * 2;
Chris Masonf68cad02007-04-24 12:44:26 -0400239 btrfs_update_inode(trans, root, dir);
240 drop_nlink(dentry->d_inode);
241 btrfs_update_inode(trans, root, dentry->d_inode);
Chris Masoncd1bc462007-04-27 10:08:34 -0400242 dir->i_sb->s_dirt = 1;
Chris Masond4dbff92007-04-04 14:08:15 -0400243 }
Chris Mason134e9732007-03-25 13:44:56 -0400244 return ret;
245}
246
Chris Mason5f443fd2007-03-27 13:42:32 -0400247static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
248{
249 struct btrfs_root *root;
250 struct btrfs_trans_handle *trans;
251 int ret;
252
Chris Masond6e4a422007-04-06 15:37:36 -0400253 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400254 mutex_lock(&root->fs_info->fs_mutex);
255 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400256 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400257 ret = btrfs_unlink_trans(trans, root, dir, dentry);
258 btrfs_end_transaction(trans, root);
259 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400260 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400261 return ret;
262}
263
264static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
265{
266 struct inode *inode = dentry->d_inode;
267 int err;
268 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400269 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400270 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400271 struct btrfs_key key;
272 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400273 struct btrfs_key found_key;
274 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400275 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400276 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400277
Chris Mason5caf2a02007-04-02 11:20:42 -0400278 path = btrfs_alloc_path();
279 BUG_ON(!path);
280 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400281 mutex_lock(&root->fs_info->fs_mutex);
282 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400283 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f443fd2007-03-27 13:42:32 -0400284 key.objectid = inode->i_ino;
285 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400286 key.flags = (u32)-1;
287 while(1) {
288 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
289 if (ret < 0) {
290 err = ret;
291 goto out;
292 }
293 BUG_ON(ret == 0);
294 if (path->slots[0] == 0) {
295 err = -ENOENT;
296 goto out;
297 }
298 path->slots[0]--;
299 leaf = btrfs_buffer_leaf(path->nodes[0]);
300 btrfs_disk_key_to_cpu(&found_key,
301 &leaf->items[path->slots[0]].key);
302 found_type = btrfs_key_type(&found_key);
303 if (found_key.objectid != inode->i_ino) {
304 err = -ENOENT;
305 goto out;
306 }
307 if ((found_type != BTRFS_DIR_ITEM_KEY &&
308 found_type != BTRFS_DIR_INDEX_KEY) ||
309 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
310 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
311 err = -ENOTEMPTY;
312 goto out;
313 }
314 ret = btrfs_del_item(trans, root, path);
315 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400316
Chris Mason5f26f772007-04-05 10:38:44 -0400317 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
318 break;
319 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400320 }
Chris Mason5f26f772007-04-05 10:38:44 -0400321 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400322 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400323
324 /* now the directory is empty */
325 err = btrfs_unlink_trans(trans, root, dir, dentry);
326 if (!err) {
327 inode->i_size = 0;
328 }
329out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400330 btrfs_release_path(root, path);
331 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400332 mutex_unlock(&root->fs_info->fs_mutex);
333 ret = btrfs_end_transaction(trans, root);
Chris Mason35b7e472007-05-02 15:53:43 -0400334 btrfs_btree_balance_dirty(root);
Chris Mason5f443fd2007-03-27 13:42:32 -0400335 if (ret && !err)
336 err = ret;
337 return err;
338}
339
Chris Mason134e9732007-03-25 13:44:56 -0400340static int btrfs_free_inode(struct btrfs_trans_handle *trans,
341 struct btrfs_root *root,
342 struct inode *inode)
343{
Chris Mason5caf2a02007-04-02 11:20:42 -0400344 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400345 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400346
Chris Mason134e9732007-03-25 13:44:56 -0400347 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400348
349 path = btrfs_alloc_path();
350 BUG_ON(!path);
351 btrfs_init_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -0400352 ret = btrfs_lookup_inode(trans, root, path,
353 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400354 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400355 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400356 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400357 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400358 return ret;
359}
360
Chris Masonde428b62007-05-18 13:28:27 -0400361static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
362 u64 objectid)
363{
364 struct btrfs_node *node;
365 int i;
366 int nritems;
367 u64 item_objectid;
368 u64 blocknr;
369 int slot;
370 int ret;
371
372 if (!path->nodes[1])
373 return;
374 node = btrfs_buffer_node(path->nodes[1]);
375 slot = path->slots[1];
376 if (slot == 0)
377 return;
378 nritems = btrfs_header_nritems(&node->header);
379 for (i = slot - 1; i >= 0; i--) {
380 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
381 if (item_objectid != objectid)
382 break;
383 blocknr = btrfs_node_blockptr(node, i);
384 ret = readahead_tree_block(root, blocknr);
385 if (ret)
386 break;
387 }
388}
389
Chris Masonf4b9aa82007-03-27 11:05:53 -0400390static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
391 struct btrfs_root *root,
392 struct inode *inode)
393{
394 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400396 struct btrfs_key key;
397 struct btrfs_disk_key *found_key;
Chris Masone06afa82007-05-23 15:44:28 -0400398 u32 found_type;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400399 struct btrfs_leaf *leaf;
Chris Mason1de037a2007-05-29 15:17:08 -0400400 struct btrfs_file_extent_item *fi;
Chris Masonf254e522007-03-29 15:15:27 -0400401 u64 extent_start = 0;
402 u64 extent_num_blocks = 0;
Chris Mason1de037a2007-05-29 15:17:08 -0400403 u64 item_end = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400404 int found_extent;
Chris Mason1de037a2007-05-29 15:17:08 -0400405 int del_item;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400406
Chris Mason5caf2a02007-04-02 11:20:42 -0400407 path = btrfs_alloc_path();
408 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400409 /* FIXME, add redo link to tree so we don't leak on crash */
410 key.objectid = inode->i_ino;
411 key.offset = (u64)-1;
Chris Masone06afa82007-05-23 15:44:28 -0400412 key.flags = (u32)-1;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400413 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 btrfs_init_path(path);
Chris Mason1de037a2007-05-29 15:17:08 -0400415 fi = NULL;
Chris Mason5caf2a02007-04-02 11:20:42 -0400416 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400417 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400418 goto error;
419 }
420 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400421 BUG_ON(path->slots[0] == 0);
422 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400423 }
Chris Masonde428b62007-05-18 13:28:27 -0400424 reada_truncate(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400425 leaf = btrfs_buffer_leaf(path->nodes[0]);
426 found_key = &leaf->items[path->slots[0]].key;
Chris Masone06afa82007-05-23 15:44:28 -0400427 found_type = btrfs_disk_key_type(found_key);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400428 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
429 break;
Chris Masone06afa82007-05-23 15:44:28 -0400430 if (found_type != BTRFS_CSUM_ITEM_KEY &&
431 found_type != BTRFS_DIR_ITEM_KEY &&
432 found_type != BTRFS_DIR_INDEX_KEY &&
433 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400434 break;
Chris Mason1de037a2007-05-29 15:17:08 -0400435 item_end = btrfs_disk_key_offset(found_key);
436 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400437 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
438 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400439 struct btrfs_file_extent_item);
Chris Mason236454df2007-04-19 13:37:44 -0400440 if (btrfs_file_extent_type(fi) !=
441 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason1de037a2007-05-29 15:17:08 -0400442 item_end += btrfs_file_extent_num_blocks(fi) <<
443 inode->i_blkbits;
444 }
445 }
446 if (found_type == BTRFS_CSUM_ITEM_KEY) {
447 ret = btrfs_csum_truncate(trans, root, path,
448 inode->i_size);
449 BUG_ON(ret);
450 }
451 if (item_end < inode->i_size) {
452 if (found_type) {
453 btrfs_set_key_type(&key, found_type - 1);
454 continue;
455 }
456 break;
457 }
458 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
459 del_item = 1;
460 else
461 del_item = 0;
462 found_extent = 0;
463
464 if (found_type == BTRFS_EXTENT_DATA_KEY &&
465 btrfs_file_extent_type(fi) !=
466 BTRFS_FILE_EXTENT_INLINE) {
467 u64 num_dec;
468 if (!del_item) {
469 u64 orig_num_blocks =
470 btrfs_file_extent_num_blocks(fi);
471 extent_num_blocks = inode->i_size -
472 btrfs_disk_key_offset(found_key) +
473 root->blocksize - 1;
474 extent_num_blocks >>= inode->i_blkbits;
475 btrfs_set_file_extent_num_blocks(fi,
476 extent_num_blocks);
477 inode->i_blocks -= (orig_num_blocks -
478 extent_num_blocks) << 3;
479 mark_buffer_dirty(path->nodes[0]);
480 } else {
Chris Mason236454df2007-04-19 13:37:44 -0400481 extent_start =
482 btrfs_file_extent_disk_blocknr(fi);
483 extent_num_blocks =
484 btrfs_file_extent_disk_num_blocks(fi);
485 /* FIXME blocksize != 4096 */
Chris Mason3a686372007-05-24 13:35:57 -0400486 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
487 if (extent_start != 0) {
488 found_extent = 1;
489 inode->i_blocks -= num_dec;
490 }
Chris Mason236454df2007-04-19 13:37:44 -0400491 }
Chris Masonf254e522007-03-29 15:15:27 -0400492 }
Chris Mason1de037a2007-05-29 15:17:08 -0400493 if (del_item) {
494 ret = btrfs_del_item(trans, root, path);
495 BUG_ON(ret);
496 } else {
497 break;
498 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400499 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400500 if (found_extent) {
501 ret = btrfs_free_extent(trans, root, extent_start,
502 extent_num_blocks, 0);
503 BUG_ON(ret);
504 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400505 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400506 ret = 0;
507error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400508 btrfs_release_path(root, path);
509 btrfs_free_path(path);
Chris Masoncd1bc462007-04-27 10:08:34 -0400510 inode->i_sb->s_dirt = 1;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400511 return ret;
512}
513
Chris Mason1de037a2007-05-29 15:17:08 -0400514static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
515{
516 struct inode *inode = mapping->host;
517 unsigned blocksize = 1 << inode->i_blkbits;
518 pgoff_t index = from >> PAGE_CACHE_SHIFT;
519 unsigned offset = from & (PAGE_CACHE_SIZE-1);
520 struct page *page;
521 char *kaddr;
522 int ret = 0;
523 struct btrfs_root *root = BTRFS_I(inode)->root;
524 u64 alloc_hint;
525 struct btrfs_key ins;
526 struct btrfs_trans_handle *trans;
527
528 if ((offset & (blocksize - 1)) == 0)
529 goto out;
530
531 ret = -ENOMEM;
532 page = grab_cache_page(mapping, index);
533 if (!page)
534 goto out;
535
536 if (!PageUptodate(page)) {
537 ret = mpage_readpage(page, btrfs_get_block);
538 lock_page(page);
539 if (!PageUptodate(page)) {
540 ret = -EIO;
541 goto out;
542 }
543 }
544 mutex_lock(&root->fs_info->fs_mutex);
545 trans = btrfs_start_transaction(root, 1);
546 btrfs_set_trans_block_group(trans, inode);
547
548 ret = drop_extents(trans, root, inode, page->index << PAGE_CACHE_SHIFT,
549 (page->index + 1) << PAGE_CACHE_SHIFT, &alloc_hint);
550 BUG_ON(ret);
551 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
552 alloc_hint, (u64)-1, &ins, 1);
553 BUG_ON(ret);
554 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
555 page->index << PAGE_CACHE_SHIFT,
556 ins.objectid, 1, 1);
557 BUG_ON(ret);
558 SetPageChecked(page);
559 kaddr = kmap(page);
560 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
561 flush_dcache_page(page);
562 btrfs_csum_file_block(trans, root, inode->i_ino,
563 page->index << PAGE_CACHE_SHIFT,
564 kaddr, PAGE_CACHE_SIZE);
565 kunmap(page);
566 btrfs_end_transaction(trans, root);
567 mutex_unlock(&root->fs_info->fs_mutex);
568
569 set_page_dirty(page);
570 unlock_page(page);
571 page_cache_release(page);
572out:
573 return ret;
574}
575
Chris Mason3a686372007-05-24 13:35:57 -0400576static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
577{
578 struct inode *inode = dentry->d_inode;
579 int err;
580
581 err = inode_change_ok(inode, attr);
582 if (err)
583 return err;
584
585 if (S_ISREG(inode->i_mode) &&
586 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
587 struct btrfs_trans_handle *trans;
588 struct btrfs_root *root = BTRFS_I(inode)->root;
589 u64 mask = root->blocksize - 1;
590 u64 pos = (inode->i_size + mask) & ~mask;
591 u64 hole_size;
592
Chris Mason1de037a2007-05-29 15:17:08 -0400593 if (attr->ia_size <= pos)
Chris Mason3a686372007-05-24 13:35:57 -0400594 goto out;
Chris Mason1de037a2007-05-29 15:17:08 -0400595
596 btrfs_truncate_page(inode->i_mapping, inode->i_size);
597
Chris Mason3a686372007-05-24 13:35:57 -0400598 hole_size = (attr->ia_size - pos + mask) & ~mask;
599 hole_size >>= inode->i_blkbits;
600
601 mutex_lock(&root->fs_info->fs_mutex);
602 trans = btrfs_start_transaction(root, 1);
603 btrfs_set_trans_block_group(trans, inode);
604 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
605 pos, 0, 0, hole_size);
606 BUG_ON(err);
607 btrfs_end_transaction(trans, root);
608 mutex_unlock(&root->fs_info->fs_mutex);
609 }
Chris Mason1de037a2007-05-29 15:17:08 -0400610out:
Chris Mason3a686372007-05-24 13:35:57 -0400611 err = inode_setattr(inode, attr);
612
Chris Mason3a686372007-05-24 13:35:57 -0400613 return err;
614}
Chris Mason134e9732007-03-25 13:44:56 -0400615static void btrfs_delete_inode(struct inode *inode)
616{
617 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400618 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400619 int ret;
620
Chris Mason134e9732007-03-25 13:44:56 -0400621 truncate_inode_pages(&inode->i_data, 0);
622 if (is_bad_inode(inode)) {
623 goto no_delete;
624 }
625 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400626 mutex_lock(&root->fs_info->fs_mutex);
627 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400628 btrfs_set_trans_block_group(trans, inode);
Chris Masone06afa82007-05-23 15:44:28 -0400629 ret = btrfs_truncate_in_trans(trans, root, inode);
630 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400631 btrfs_free_inode(trans, root, inode);
632 btrfs_end_transaction(trans, root);
633 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400634 btrfs_btree_balance_dirty(root);
Chris Mason134e9732007-03-25 13:44:56 -0400635 return;
636no_delete:
637 clear_inode(inode);
638}
639
Chris Masone20d96d2007-03-22 12:13:20 -0400640static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400641 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400642{
643 const char *name = dentry->d_name.name;
644 int namelen = dentry->d_name.len;
645 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400646 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400647 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400648 int ret;
649
Chris Mason5caf2a02007-04-02 11:20:42 -0400650 path = btrfs_alloc_path();
651 BUG_ON(!path);
652 btrfs_init_path(path);
Chris Mason7e381802007-04-19 15:36:27 -0400653 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400654 namelen, 0);
Chris Mason7e381802007-04-19 15:36:27 -0400655 if (!di || IS_ERR(di)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400656 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400657 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400658 goto out;
659 }
Chris Masond6e4a422007-04-06 15:37:36 -0400660 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400661out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400662 btrfs_release_path(root, path);
663 btrfs_free_path(path);
Chris Masone20d96d2007-03-22 12:13:20 -0400664 return ret;
665}
666
Chris Mason35b7e472007-05-02 15:53:43 -0400667static int fixup_tree_root_location(struct btrfs_root *root,
Chris Masond6e4a422007-04-06 15:37:36 -0400668 struct btrfs_key *location,
669 struct btrfs_root **sub_root)
670{
671 struct btrfs_path *path;
672 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400673
674 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
675 return 0;
676 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
677 return 0;
678
679 path = btrfs_alloc_path();
680 BUG_ON(!path);
681 mutex_lock(&root->fs_info->fs_mutex);
682
Chris Mason0f7d52f2007-04-09 10:42:37 -0400683 *sub_root = btrfs_read_fs_root(root->fs_info, location);
684 if (IS_ERR(*sub_root))
685 return PTR_ERR(*sub_root);
686
687 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400688 location->objectid = btrfs_root_dirid(ri);
689 location->flags = 0;
690 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
691 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400692
Chris Masond6e4a422007-04-06 15:37:36 -0400693 btrfs_free_path(path);
694 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400695 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400696}
697
Chris Mason35b7e472007-05-02 15:53:43 -0400698static int btrfs_init_locked_inode(struct inode *inode, void *p)
Chris Masonc5739bb2007-04-10 09:27:04 -0400699{
700 struct btrfs_iget_args *args = p;
701 inode->i_ino = args->ino;
702 BTRFS_I(inode)->root = args->root;
703 return 0;
704}
705
Chris Mason35b7e472007-05-02 15:53:43 -0400706static int btrfs_find_actor(struct inode *inode, void *opaque)
Chris Masonc5739bb2007-04-10 09:27:04 -0400707{
708 struct btrfs_iget_args *args = opaque;
709 return (args->ino == inode->i_ino &&
710 args->root == BTRFS_I(inode)->root);
711}
712
Chris Mason35b7e472007-05-02 15:53:43 -0400713static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
714 struct btrfs_root *root)
Chris Masonc5739bb2007-04-10 09:27:04 -0400715{
716 struct inode *inode;
717 struct btrfs_iget_args args;
718 args.ino = objectid;
719 args.root = root;
720
721 inode = iget5_locked(s, objectid, btrfs_find_actor,
722 btrfs_init_locked_inode,
723 (void *)&args);
724 return inode;
725}
Chris Masond6e4a422007-04-06 15:37:36 -0400726
Chris Masone20d96d2007-03-22 12:13:20 -0400727static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
728 struct nameidata *nd)
729{
730 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400731 struct btrfs_inode *bi = BTRFS_I(dir);
732 struct btrfs_root *root = bi->root;
733 struct btrfs_root *sub_root = root;
734 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400735 int ret;
736
737 if (dentry->d_name.len > BTRFS_NAME_LEN)
738 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400739 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400740 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400741 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400742 if (ret < 0)
743 return ERR_PTR(ret);
744 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400745 if (location.objectid) {
746 ret = fixup_tree_root_location(root, &location, &sub_root);
747 if (ret < 0)
748 return ERR_PTR(ret);
749 if (ret > 0)
750 return ERR_PTR(-ENOENT);
Chris Masonc5739bb2007-04-10 09:27:04 -0400751 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
752 sub_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400753 if (!inode)
754 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400755 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400756 if (sub_root != root) {
Chris Masonc5739bb2007-04-10 09:27:04 -0400757printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400758 igrab(inode);
759 sub_root->inode = inode;
760 }
Chris Masond6e4a422007-04-06 15:37:36 -0400761 BTRFS_I(inode)->root = sub_root;
762 memcpy(&BTRFS_I(inode)->location, &location,
763 sizeof(location));
764 btrfs_read_locked_inode(inode);
765 unlock_new_inode(inode);
766 }
Chris Masone20d96d2007-03-22 12:13:20 -0400767 }
768 return d_splice_alias(inode, dentry);
769}
770
Chris Masonde428b62007-05-18 13:28:27 -0400771static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
772 u64 objectid)
Chris Mason090d1872007-05-01 08:53:32 -0400773{
774 struct btrfs_node *node;
775 int i;
Chris Masonde428b62007-05-18 13:28:27 -0400776 u32 nritems;
Chris Mason090d1872007-05-01 08:53:32 -0400777 u64 item_objectid;
778 u64 blocknr;
779 int slot;
Chris Masonde428b62007-05-18 13:28:27 -0400780 int ret;
Chris Mason090d1872007-05-01 08:53:32 -0400781
782 if (!path->nodes[1])
783 return;
784 node = btrfs_buffer_node(path->nodes[1]);
785 slot = path->slots[1];
Chris Mason090d1872007-05-01 08:53:32 -0400786 nritems = btrfs_header_nritems(&node->header);
Chris Masonde428b62007-05-18 13:28:27 -0400787 for (i = slot + 1; i < nritems; i++) {
Chris Mason090d1872007-05-01 08:53:32 -0400788 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
789 if (item_objectid != objectid)
790 break;
791 blocknr = btrfs_node_blockptr(node, i);
Chris Masonde428b62007-05-18 13:28:27 -0400792 ret = readahead_tree_block(root, blocknr);
793 if (ret)
794 break;
Chris Mason090d1872007-05-01 08:53:32 -0400795 }
796}
Chris Masonfabb5682007-06-07 22:13:21 -0400797static unsigned char btrfs_filetype_table[] = {
798 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
799};
Chris Mason090d1872007-05-01 08:53:32 -0400800
Chris Masone20d96d2007-03-22 12:13:20 -0400801static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
802{
803 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400804 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400805 struct btrfs_item *item;
806 struct btrfs_dir_item *di;
807 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400809 int ret;
810 u32 nritems;
811 struct btrfs_leaf *leaf;
812 int slot;
813 int advance;
Chris Masonfabb5682007-06-07 22:13:21 -0400814 unsigned char d_type;
Chris Mason7f5c1512007-03-23 15:56:19 -0400815 int over = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400816 u32 di_cur;
817 u32 di_total;
818 u32 di_len;
819 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Masond6e4a422007-04-06 15:37:36 -0400820
821 /* FIXME, use a real flag for deciding about the key type */
822 if (root->fs_info->tree_root == root)
823 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400824 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400825 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400826 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400827 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400828 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400829 path = btrfs_alloc_path();
830 btrfs_init_path(path);
831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason1b05da22007-04-10 12:13:09 -0400832 if (ret < 0)
Chris Masone20d96d2007-03-22 12:13:20 -0400833 goto err;
Chris Mason7f5c1512007-03-23 15:56:19 -0400834 advance = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400835 reada_leaves(root, path, inode->i_ino);
Chris Masone20d96d2007-03-22 12:13:20 -0400836 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400837 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400838 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400839 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400840 if (advance || slot >= nritems) {
841 if (slot >= nritems -1) {
Chris Masonde428b62007-05-18 13:28:27 -0400842 reada_leaves(root, path, inode->i_ino);
Chris Mason5caf2a02007-04-02 11:20:42 -0400843 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400844 if (ret)
845 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400846 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400847 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400848 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400849 } else {
850 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400852 }
853 }
854 advance = 1;
855 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400856 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
857 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400858 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masona429e512007-04-18 16:15:28 -0400859 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400860 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
861 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400862 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400863 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400864 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400865 di_cur = 0;
866 di_total = btrfs_item_size(leaf->items + slot);
867 while(di_cur < di_total) {
Chris Masonfabb5682007-06-07 22:13:21 -0400868 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
Chris Mason7e381802007-04-19 15:36:27 -0400869 over = filldir(dirent, (const char *)(di + 1),
870 btrfs_dir_name_len(di),
871 btrfs_disk_key_offset(&item->key),
872 btrfs_disk_key_objectid(&di->location),
873 d_type);
874 if (over)
875 goto nopos;
876 di_len = btrfs_dir_name_len(di) + sizeof(*di);
877 di_cur += di_len;
878 di = (struct btrfs_dir_item *)((char *)di + di_len);
879 }
Chris Masone20d96d2007-03-22 12:13:20 -0400880 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400881 filp->f_pos++;
882nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400883 ret = 0;
884err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400885 btrfs_release_path(root, path);
886 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400887 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400888 return ret;
889}
890
891static void btrfs_put_super (struct super_block * sb)
892{
893 struct btrfs_root *root = btrfs_sb(sb);
894 int ret;
895
896 ret = close_ctree(root);
897 if (ret) {
898 printk("close ctree returns %d\n", ret);
899 }
900 sb->s_fs_info = NULL;
901}
Chris Mason2e635a22007-03-21 11:12:56 -0400902
903static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
904{
905 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400906 struct dentry * root_dentry;
907 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400908 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400909 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400910
911 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400912 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400913 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400914 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400915
Chris Mason0f7d52f2007-04-09 10:42:37 -0400916 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400917
Chris Mason0f7d52f2007-04-09 10:42:37 -0400918 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400919 printk("btrfs: open_ctree failed\n");
920 return -EIO;
921 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400922 sb->s_fs_info = tree_root;
923 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400924 printk("read in super total blocks %Lu root %Lu\n",
925 btrfs_super_total_blocks(disk_super),
926 btrfs_super_root_dir(disk_super));
927
Chris Masonc5739bb2007-04-10 09:27:04 -0400928 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
929 tree_root);
Chris Masond6e4a422007-04-06 15:37:36 -0400930 bi = BTRFS_I(inode);
931 bi->location.objectid = inode->i_ino;
932 bi->location.offset = 0;
933 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400934 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400935 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
936
Chris Mason2e635a22007-03-21 11:12:56 -0400937 if (!inode)
938 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400939 if (inode->i_state & I_NEW) {
940 btrfs_read_locked_inode(inode);
941 unlock_new_inode(inode);
942 }
Chris Mason2e635a22007-03-21 11:12:56 -0400943
Chris Masone20d96d2007-03-22 12:13:20 -0400944 root_dentry = d_alloc_root(inode);
945 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400946 iput(inode);
947 return -ENOMEM;
948 }
Chris Masone20d96d2007-03-22 12:13:20 -0400949 sb->s_root = root_dentry;
Chris Mason08607c12007-06-08 15:33:54 -0400950 btrfs_transaction_queue_work(tree_root, HZ * 30);
Chris Mason2e635a22007-03-21 11:12:56 -0400951 return 0;
952}
953
Chris Mason4730a4b2007-03-26 12:00:39 -0400954static int btrfs_write_inode(struct inode *inode, int wait)
955{
Chris Masond6e4a422007-04-06 15:37:36 -0400956 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400957 struct btrfs_trans_handle *trans;
Chris Masonb5133862007-04-24 11:52:22 -0400958 int ret = 0;
959
960 if (wait) {
961 mutex_lock(&root->fs_info->fs_mutex);
962 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400963 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400964 ret = btrfs_commit_transaction(trans, root);
965 mutex_unlock(&root->fs_info->fs_mutex);
966 }
967 return ret;
968}
969
970static void btrfs_dirty_inode(struct inode *inode)
971{
972 struct btrfs_root *root = BTRFS_I(inode)->root;
973 struct btrfs_trans_handle *trans;
Chris Mason4730a4b2007-03-26 12:00:39 -0400974
975 mutex_lock(&root->fs_info->fs_mutex);
976 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -0400977 btrfs_set_trans_block_group(trans, inode);
Chris Masonb5133862007-04-24 11:52:22 -0400978 btrfs_update_inode(trans, root, inode);
979 btrfs_end_transaction(trans, root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400980 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -0400981 btrfs_btree_balance_dirty(root);
Chris Mason4730a4b2007-03-26 12:00:39 -0400982}
983
Chris Masond5719762007-03-23 10:01:08 -0400984static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -0400985 struct btrfs_root *root,
Chris Mason31f3c992007-04-30 15:25:45 -0400986 u64 objectid,
987 struct btrfs_block_group_cache *group,
988 int mode)
Chris Masond5719762007-03-23 10:01:08 -0400989{
990 struct inode *inode;
991 struct btrfs_inode_item inode_item;
Chris Mason1b05da22007-04-10 12:13:09 -0400992 struct btrfs_key *location;
Chris Masond5719762007-03-23 10:01:08 -0400993 int ret;
Chris Masonde428b62007-05-18 13:28:27 -0400994 int owner;
Chris Masond5719762007-03-23 10:01:08 -0400995
Chris Mason2619ba12007-04-10 16:58:11 -0400996 inode = new_inode(root->fs_info->sb);
Chris Masond5719762007-03-23 10:01:08 -0400997 if (!inode)
998 return ERR_PTR(-ENOMEM);
999
Chris Mason2619ba12007-04-10 16:58:11 -04001000 BTRFS_I(inode)->root = root;
Chris Masonde428b62007-05-18 13:28:27 -04001001 if (mode & S_IFDIR)
1002 owner = 0;
1003 else
1004 owner = 1;
1005 group = btrfs_find_block_group(root, group, 0, 0, owner);
Chris Mason31f3c992007-04-30 15:25:45 -04001006 BTRFS_I(inode)->block_group = group;
Chris Masond5719762007-03-23 10:01:08 -04001007
1008 inode->i_uid = current->fsuid;
1009 inode->i_gid = current->fsgid;
1010 inode->i_mode = mode;
1011 inode->i_ino = objectid;
1012 inode->i_blocks = 0;
Chris Masonc5739bb2007-04-10 09:27:04 -04001013 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Masond5719762007-03-23 10:01:08 -04001014 fill_inode_item(&inode_item, inode);
Chris Mason1b05da22007-04-10 12:13:09 -04001015 location = &BTRFS_I(inode)->location;
1016 location->objectid = objectid;
1017 location->flags = 0;
1018 location->offset = 0;
1019 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
Chris Masond5719762007-03-23 10:01:08 -04001020
1021 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1022 BUG_ON(ret);
1023
1024 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -04001025 return inode;
1026}
1027
Chris Masonfabb5682007-06-07 22:13:21 -04001028static inline u8 btrfs_inode_type(struct inode *inode)
1029{
1030 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1031}
1032
Chris Masond5719762007-03-23 10:01:08 -04001033static int btrfs_add_link(struct btrfs_trans_handle *trans,
1034 struct dentry *dentry, struct inode *inode)
1035{
1036 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -04001037 struct btrfs_key key;
1038 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1039 key.objectid = inode->i_ino;
1040 key.flags = 0;
1041 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1042 key.offset = 0;
1043
1044 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -04001045 dentry->d_name.name, dentry->d_name.len,
1046 dentry->d_parent->d_inode->i_ino,
Chris Masonfabb5682007-06-07 22:13:21 -04001047 &key, btrfs_inode_type(inode));
Chris Mason4730a4b2007-03-26 12:00:39 -04001048 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -04001049 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -04001050 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -04001051 dentry->d_parent->d_inode);
1052 }
Chris Masond5719762007-03-23 10:01:08 -04001053 return ret;
1054}
1055
1056static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1057 struct dentry *dentry, struct inode *inode)
1058{
1059 int err = btrfs_add_link(trans, dentry, inode);
1060 if (!err) {
1061 d_instantiate(dentry, inode);
1062 return 0;
1063 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001064 if (err > 0)
1065 err = -EEXIST;
Chris Masond5719762007-03-23 10:01:08 -04001066 return err;
1067}
1068
1069static int btrfs_create(struct inode *dir, struct dentry *dentry,
1070 int mode, struct nameidata *nd)
1071{
1072 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -04001073 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -04001074 struct inode *inode;
1075 int err;
Chris Mason134e9732007-03-25 13:44:56 -04001076 int drop_inode = 0;
Chris Mason2619ba12007-04-10 16:58:11 -04001077 u64 objectid;
Chris Masond5719762007-03-23 10:01:08 -04001078
Chris Masond561c022007-03-23 19:47:49 -04001079 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001080 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001081 btrfs_set_trans_block_group(trans, dir);
Chris Mason2619ba12007-04-10 16:58:11 -04001082
1083 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1084 if (err) {
1085 err = -ENOSPC;
1086 goto out_unlock;
1087 }
1088
Chris Mason31f3c992007-04-30 15:25:45 -04001089 inode = btrfs_new_inode(trans, root, objectid,
1090 BTRFS_I(dir)->block_group, mode);
Chris Masond5719762007-03-23 10:01:08 -04001091 err = PTR_ERR(inode);
1092 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -04001093 goto out_unlock;
Chris Mason31f3c992007-04-30 15:25:45 -04001094
1095 btrfs_set_trans_block_group(trans, inode);
Chris Masond5719762007-03-23 10:01:08 -04001096 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -04001097 if (err)
1098 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -04001099 else {
1100 inode->i_mapping->a_ops = &btrfs_aops;
1101 inode->i_fop = &btrfs_file_operations;
1102 inode->i_op = &btrfs_file_inode_operations;
1103 }
Chris Masond5719762007-03-23 10:01:08 -04001104 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -04001105 btrfs_update_inode_block_group(trans, inode);
1106 btrfs_update_inode_block_group(trans, dir);
Chris Masond561c022007-03-23 19:47:49 -04001107out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -04001108 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -04001109 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001110
Chris Mason134e9732007-03-25 13:44:56 -04001111 if (drop_inode) {
1112 inode_dec_link_count(inode);
1113 iput(inode);
1114 }
Chris Mason35b7e472007-05-02 15:53:43 -04001115 btrfs_btree_balance_dirty(root);
Chris Masond5719762007-03-23 10:01:08 -04001116 return err;
1117}
1118
Chris Mason2b8d99a2007-05-24 08:34:10 -04001119static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1120 struct dentry *dentry)
1121{
1122 struct btrfs_trans_handle *trans;
1123 struct btrfs_root *root = BTRFS_I(dir)->root;
1124 struct inode *inode = old_dentry->d_inode;
1125 int err;
1126 int drop_inode = 0;
1127
1128 if (inode->i_nlink == 0)
1129 return -ENOENT;
1130
1131 inc_nlink(inode);
1132 mutex_lock(&root->fs_info->fs_mutex);
1133 trans = btrfs_start_transaction(root, 1);
1134 btrfs_set_trans_block_group(trans, dir);
1135 atomic_inc(&inode->i_count);
1136 err = btrfs_add_nondir(trans, dentry, inode);
1137 if (err)
1138 drop_inode = 1;
1139 dir->i_sb->s_dirt = 1;
1140 btrfs_update_inode_block_group(trans, dir);
1141 btrfs_update_inode(trans, root, inode);
1142
1143 btrfs_end_transaction(trans, root);
1144 mutex_unlock(&root->fs_info->fs_mutex);
1145
1146 if (drop_inode) {
1147 inode_dec_link_count(inode);
1148 iput(inode);
1149 }
1150 btrfs_btree_balance_dirty(root);
1151 return err;
1152}
1153
Chris Masonf7922032007-03-25 20:17:36 -04001154static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
Chris Mason2619ba12007-04-10 16:58:11 -04001155 struct btrfs_root *root,
1156 u64 objectid, u64 dirid)
Chris Masonf7922032007-03-25 20:17:36 -04001157{
Chris Masonf7922032007-03-25 20:17:36 -04001158 int ret;
1159 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -04001160 struct btrfs_key key;
1161
Chris Masonf7922032007-03-25 20:17:36 -04001162 buf[0] = '.';
1163 buf[1] = '.';
1164
Chris Mason2619ba12007-04-10 16:58:11 -04001165 key.objectid = objectid;
Chris Masond6e4a422007-04-06 15:37:36 -04001166 key.offset = 0;
1167 key.flags = 0;
1168 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1169
Chris Mason2619ba12007-04-10 16:58:11 -04001170 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
Chris Masonfabb5682007-06-07 22:13:21 -04001171 &key, BTRFS_FT_DIR);
Chris Masonf7922032007-03-25 20:17:36 -04001172 if (ret)
1173 goto error;
Chris Mason2619ba12007-04-10 16:58:11 -04001174 key.objectid = dirid;
1175 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
Chris Masonfabb5682007-06-07 22:13:21 -04001176 &key, BTRFS_FT_DIR);
Chris Mason4730a4b2007-03-26 12:00:39 -04001177 if (ret)
1178 goto error;
Chris Masonf7922032007-03-25 20:17:36 -04001179error:
1180 return ret;
1181}
1182
1183static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1184{
1185 struct inode *inode;
1186 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -04001187 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -04001188 int err = 0;
1189 int drop_on_err = 0;
Chris Mason2619ba12007-04-10 16:58:11 -04001190 u64 objectid;
Chris Masonf7922032007-03-25 20:17:36 -04001191
1192 mutex_lock(&root->fs_info->fs_mutex);
1193 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001194 btrfs_set_trans_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -04001195 if (IS_ERR(trans)) {
1196 err = PTR_ERR(trans);
1197 goto out_unlock;
1198 }
Chris Mason2619ba12007-04-10 16:58:11 -04001199
1200 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1201 if (err) {
1202 err = -ENOSPC;
1203 goto out_unlock;
1204 }
1205
Chris Mason31f3c992007-04-30 15:25:45 -04001206 inode = btrfs_new_inode(trans, root, objectid,
1207 BTRFS_I(dir)->block_group, S_IFDIR | mode);
Chris Masonf7922032007-03-25 20:17:36 -04001208 if (IS_ERR(inode)) {
1209 err = PTR_ERR(inode);
1210 goto out_fail;
1211 }
1212 drop_on_err = 1;
1213 inode->i_op = &btrfs_dir_inode_operations;
1214 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason31f3c992007-04-30 15:25:45 -04001215 btrfs_set_trans_block_group(trans, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001216
Chris Mason2619ba12007-04-10 16:58:11 -04001217 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1218 if (err)
1219 goto out_fail;
1220
1221 inode->i_size = 6;
1222 err = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001223 if (err)
1224 goto out_fail;
1225 err = btrfs_add_link(trans, dentry, inode);
1226 if (err)
1227 goto out_fail;
1228 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -04001229 drop_on_err = 0;
Chris Masoncd1bc462007-04-27 10:08:34 -04001230 dir->i_sb->s_dirt = 1;
Chris Mason31f3c992007-04-30 15:25:45 -04001231 btrfs_update_inode_block_group(trans, inode);
1232 btrfs_update_inode_block_group(trans, dir);
Chris Masonf7922032007-03-25 20:17:36 -04001233
1234out_fail:
1235 btrfs_end_transaction(trans, root);
1236out_unlock:
1237 mutex_unlock(&root->fs_info->fs_mutex);
1238 if (drop_on_err)
1239 iput(inode);
Chris Mason35b7e472007-05-02 15:53:43 -04001240 btrfs_btree_balance_dirty(root);
Chris Masonf7922032007-03-25 20:17:36 -04001241 return err;
1242}
1243
Chris Mason8fd17792007-04-19 21:01:03 -04001244static int btrfs_sync_file(struct file *file,
1245 struct dentry *dentry, int datasync)
1246{
1247 struct inode *inode = dentry->d_inode;
1248 struct btrfs_root *root = BTRFS_I(inode)->root;
1249 int ret;
1250 struct btrfs_trans_handle *trans;
1251
1252 mutex_lock(&root->fs_info->fs_mutex);
1253 trans = btrfs_start_transaction(root, 1);
1254 if (!trans) {
1255 ret = -ENOMEM;
1256 goto out;
1257 }
1258 ret = btrfs_commit_transaction(trans, root);
1259 mutex_unlock(&root->fs_info->fs_mutex);
1260out:
1261 return ret > 0 ? EIO : ret;
1262}
1263
Chris Masond5719762007-03-23 10:01:08 -04001264static int btrfs_sync_fs(struct super_block *sb, int wait)
1265{
1266 struct btrfs_trans_handle *trans;
1267 struct btrfs_root *root;
1268 int ret;
Chris Masond98237b2007-03-28 13:57:48 -04001269 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -04001270
Chris Masond5719762007-03-23 10:01:08 -04001271 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -04001272 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -04001273 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -04001274 return 0;
1275 }
Chris Masond561c022007-03-23 19:47:49 -04001276 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001277 trans = btrfs_start_transaction(root, 1);
1278 ret = btrfs_commit_transaction(trans, root);
1279 sb->s_dirt = 0;
1280 BUG_ON(ret);
1281printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -04001282 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -04001283 return 0;
1284}
1285
Chris Masonfabb5682007-06-07 22:13:21 -04001286#define BTRFS_GET_BLOCK_NO_CREATE 0
1287#define BTRFS_GET_BLOCK_CREATE 1
1288#define BTRFS_GET_BLOCK_NO_DIRECT 2
1289
Chris Mason75dfe392007-03-29 11:56:46 -04001290static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001291 struct buffer_head *result, int create)
1292{
1293 int ret;
1294 int err = 0;
1295 u64 blocknr;
1296 u64 extent_start = 0;
1297 u64 extent_end = 0;
1298 u64 objectid = inode->i_ino;
Chris Mason236454df2007-04-19 13:37:44 -04001299 u32 found_type;
Chris Mason1de037a2007-05-29 15:17:08 -04001300 u64 alloc_hint = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001301 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001303 struct btrfs_file_extent_item *item;
1304 struct btrfs_leaf *leaf;
1305 struct btrfs_disk_key *found_key;
Chris Mason1de037a2007-05-29 15:17:08 -04001306 struct btrfs_trans_handle *trans = NULL;
Chris Masondee26a92007-03-26 16:00:06 -04001307
Chris Mason5caf2a02007-04-02 11:20:42 -04001308 path = btrfs_alloc_path();
1309 BUG_ON(!path);
1310 btrfs_init_path(path);
Chris Masonfabb5682007-06-07 22:13:21 -04001311 if (create & BTRFS_GET_BLOCK_CREATE) {
Chris Mason6567e832007-04-16 09:22:45 -04001312 WARN_ON(1);
Chris Mason1de037a2007-05-29 15:17:08 -04001313 /* this almost but not quite works */
1314 trans = btrfs_start_transaction(root, 1);
1315 if (!trans) {
1316 err = -ENOMEM;
1317 goto out;
1318 }
1319 ret = drop_extents(trans, root, inode,
1320 iblock << inode->i_blkbits,
1321 (iblock + 1) << inode->i_blkbits,
1322 &alloc_hint);
1323 BUG_ON(ret);
Chris Mason6567e832007-04-16 09:22:45 -04001324 }
Chris Masondee26a92007-03-26 16:00:06 -04001325
Chris Mason236454df2007-04-19 13:37:44 -04001326 ret = btrfs_lookup_file_extent(NULL, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001327 inode->i_ino,
Chris Mason236454df2007-04-19 13:37:44 -04001328 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001329 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001330 err = ret;
1331 goto out;
1332 }
1333
1334 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001335 if (path->slots[0] == 0) {
1336 btrfs_release_path(root, path);
Chris Mason1de037a2007-05-29 15:17:08 -04001337 goto not_found;
Chris Masondee26a92007-03-26 16:00:06 -04001338 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001339 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001340 }
1341
Chris Mason5caf2a02007-04-02 11:20:42 -04001342 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001343 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001344 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001345 blocknr = btrfs_file_extent_disk_blocknr(item);
1346 blocknr += btrfs_file_extent_offset(item);
1347
Chris Masondee26a92007-03-26 16:00:06 -04001348 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001349 found_key = &leaf->items[path->slots[0]].key;
Chris Mason236454df2007-04-19 13:37:44 -04001350 found_type = btrfs_disk_key_type(found_key);
Chris Masondee26a92007-03-26 16:00:06 -04001351 if (btrfs_disk_key_objectid(found_key) != objectid ||
Chris Mason236454df2007-04-19 13:37:44 -04001352 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masondee26a92007-03-26 16:00:06 -04001353 extent_end = 0;
1354 extent_start = 0;
Chris Mason1de037a2007-05-29 15:17:08 -04001355 goto not_found;
Chris Masondee26a92007-03-26 16:00:06 -04001356 }
Chris Mason236454df2007-04-19 13:37:44 -04001357 found_type = btrfs_file_extent_type(item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001358 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason236454df2007-04-19 13:37:44 -04001359 if (found_type == BTRFS_FILE_EXTENT_REG) {
1360 extent_start = extent_start >> inode->i_blkbits;
1361 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Mason3a686372007-05-24 13:35:57 -04001362 err = 0;
Chris Mason1de037a2007-05-29 15:17:08 -04001363 if (btrfs_file_extent_disk_blocknr(item) == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001364 goto out;
Chris Mason236454df2007-04-19 13:37:44 -04001365 if (iblock >= extent_start && iblock < extent_end) {
Chris Mason236454df2007-04-19 13:37:44 -04001366 btrfs_map_bh_to_logical(root, result, blocknr +
1367 iblock - extent_start);
1368 goto out;
1369 }
1370 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1371 char *ptr;
1372 char *map;
1373 u32 size;
Chris Masonfabb5682007-06-07 22:13:21 -04001374
1375 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1376 err = -EINVAL;
1377 goto out;
1378 }
Chris Mason236454df2007-04-19 13:37:44 -04001379 size = btrfs_file_extent_inline_len(leaf->items +
1380 path->slots[0]);
1381 extent_end = (extent_start + size) >> inode->i_blkbits;
1382 extent_start >>= inode->i_blkbits;
1383 if (iblock < extent_start || iblock > extent_end) {
Chris Mason1de037a2007-05-29 15:17:08 -04001384 goto not_found;
Chris Mason236454df2007-04-19 13:37:44 -04001385 }
1386 ptr = btrfs_file_extent_inline_start(item);
1387 map = kmap(result->b_page);
1388 memcpy(map, ptr, size);
1389 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1390 flush_dcache_page(result->b_page);
1391 kunmap(result->b_page);
1392 set_buffer_uptodate(result);
1393 SetPageChecked(result->b_page);
1394 btrfs_map_bh_to_logical(root, result, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001395 }
Chris Mason1de037a2007-05-29 15:17:08 -04001396not_found:
Chris Masonfabb5682007-06-07 22:13:21 -04001397 if (create & BTRFS_GET_BLOCK_CREATE) {
Chris Mason1de037a2007-05-29 15:17:08 -04001398 struct btrfs_key ins;
1399 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1400 1, alloc_hint, (u64)-1,
1401 &ins, 1);
1402 BUG_ON(ret);
1403 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1404 iblock << inode->i_blkbits,
1405 ins.objectid, ins.offset,
1406 ins.offset);
1407 BUG_ON(ret);
1408 SetPageChecked(result->b_page);
1409 btrfs_map_bh_to_logical(root, result, ins.objectid);
1410 }
Chris Masondee26a92007-03-26 16:00:06 -04001411out:
Chris Mason1de037a2007-05-29 15:17:08 -04001412 if (trans)
1413 err = btrfs_end_transaction(trans, root);
Chris Mason5caf2a02007-04-02 11:20:42 -04001414 btrfs_free_path(path);
Chris Mason75dfe392007-03-29 11:56:46 -04001415 return err;
1416}
1417
1418static int btrfs_get_block(struct inode *inode, sector_t iblock,
1419 struct buffer_head *result, int create)
1420{
1421 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001422 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001423 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001424 err = btrfs_get_block_lock(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001425 mutex_unlock(&root->fs_info->fs_mutex);
1426 return err;
1427}
1428
Chris Masonfabb5682007-06-07 22:13:21 -04001429static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1430 struct buffer_head *result, int create)
1431{
1432 struct btrfs_root *root = BTRFS_I(inode)->root;
1433 mutex_lock(&root->fs_info->fs_mutex);
1434 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1435 mutex_unlock(&root->fs_info->fs_mutex);
1436 return 0;
1437}
1438
1439static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1440{
1441 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1442}
1443
Chris Masondee26a92007-03-26 16:00:06 -04001444static int btrfs_prepare_write(struct file *file, struct page *page,
1445 unsigned from, unsigned to)
1446{
Chris Mason1de037a2007-05-29 15:17:08 -04001447 return block_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001448}
1449
Chris Masond561c022007-03-23 19:47:49 -04001450static void btrfs_write_super(struct super_block *sb)
1451{
Chris Mason08607c12007-06-08 15:33:54 -04001452 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -04001453}
1454
Chris Masondee26a92007-03-26 16:00:06 -04001455static int btrfs_readpage(struct file *file, struct page *page)
1456{
1457 return mpage_readpage(page, btrfs_get_block);
1458}
1459
Chris Mason236454df2007-04-19 13:37:44 -04001460/*
1461 * While block_write_full_page is writing back the dirty buffers under
1462 * the page lock, whoever dirtied the buffers may decide to clean them
1463 * again at any time. We handle that by only looking at the buffer
1464 * state inside lock_buffer().
1465 *
1466 * If block_write_full_page() is called for regular writeback
1467 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1468 * locked buffer. This only can happen if someone has written the buffer
1469 * directly, with submit_bh(). At the address_space level PageWriteback
1470 * prevents this contention from occurring.
1471 */
1472static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1473 struct writeback_control *wbc)
1474{
1475 int err;
1476 sector_t block;
1477 sector_t last_block;
1478 struct buffer_head *bh, *head;
1479 const unsigned blocksize = 1 << inode->i_blkbits;
1480 int nr_underway = 0;
1481
1482 BUG_ON(!PageLocked(page));
1483
1484 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1485
1486 if (!page_has_buffers(page)) {
1487 create_empty_buffers(page, blocksize,
1488 (1 << BH_Dirty)|(1 << BH_Uptodate));
1489 }
1490
1491 /*
1492 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1493 * here, and the (potentially unmapped) buffers may become dirty at
1494 * any time. If a buffer becomes dirty here after we've inspected it
1495 * then we just miss that fact, and the page stays dirty.
1496 *
1497 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1498 * handle that here by just cleaning them.
1499 */
1500
1501 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1502 head = page_buffers(page);
1503 bh = head;
1504
1505 /*
1506 * Get all the dirty buffers mapped to disk addresses and
1507 * handle any aliases from the underlying blockdev's mapping.
1508 */
1509 do {
1510 if (block > last_block) {
1511 /*
1512 * mapped buffers outside i_size will occur, because
1513 * this page can be outside i_size when there is a
1514 * truncate in progress.
1515 */
1516 /*
1517 * The buffer was zeroed by block_write_full_page()
1518 */
1519 clear_buffer_dirty(bh);
1520 set_buffer_uptodate(bh);
1521 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1522 WARN_ON(bh->b_size != blocksize);
1523 err = btrfs_get_block(inode, block, bh, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04001524 if (err) {
1525printk("writepage going to recovery err %d\n", err);
Chris Mason236454df2007-04-19 13:37:44 -04001526 goto recover;
Chris Mason35b7e472007-05-02 15:53:43 -04001527 }
Chris Mason236454df2007-04-19 13:37:44 -04001528 if (buffer_new(bh)) {
1529 /* blockdev mappings never come here */
1530 clear_buffer_new(bh);
Chris Mason236454df2007-04-19 13:37:44 -04001531 }
1532 }
1533 bh = bh->b_this_page;
1534 block++;
1535 } while (bh != head);
1536
1537 do {
1538 if (!buffer_mapped(bh))
1539 continue;
1540 /*
1541 * If it's a fully non-blocking write attempt and we cannot
1542 * lock the buffer then redirty the page. Note that this can
1543 * potentially cause a busy-wait loop from pdflush and kswapd
1544 * activity, but those code paths have their own higher-level
1545 * throttling.
1546 */
1547 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1548 lock_buffer(bh);
1549 } else if (test_set_buffer_locked(bh)) {
1550 redirty_page_for_writepage(wbc, page);
1551 continue;
1552 }
1553 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1554 mark_buffer_async_write(bh);
1555 } else {
1556 unlock_buffer(bh);
1557 }
1558 } while ((bh = bh->b_this_page) != head);
1559
1560 /*
1561 * The page and its buffers are protected by PageWriteback(), so we can
1562 * drop the bh refcounts early.
1563 */
1564 BUG_ON(PageWriteback(page));
1565 set_page_writeback(page);
1566
1567 do {
1568 struct buffer_head *next = bh->b_this_page;
1569 if (buffer_async_write(bh)) {
1570 submit_bh(WRITE, bh);
1571 nr_underway++;
1572 }
1573 bh = next;
1574 } while (bh != head);
1575 unlock_page(page);
1576
1577 err = 0;
1578done:
1579 if (nr_underway == 0) {
1580 /*
1581 * The page was marked dirty, but the buffers were
1582 * clean. Someone wrote them back by hand with
1583 * ll_rw_block/submit_bh. A rare case.
1584 */
1585 int uptodate = 1;
1586 do {
1587 if (!buffer_uptodate(bh)) {
1588 uptodate = 0;
1589 break;
1590 }
1591 bh = bh->b_this_page;
1592 } while (bh != head);
1593 if (uptodate)
1594 SetPageUptodate(page);
1595 end_page_writeback(page);
Chris Mason236454df2007-04-19 13:37:44 -04001596 }
1597 return err;
1598
1599recover:
1600 /*
1601 * ENOSPC, or some other error. We may already have added some
1602 * blocks to the file, so we need to write these out to avoid
1603 * exposing stale data.
1604 * The page is currently locked and not marked for writeback
1605 */
1606 bh = head;
1607 /* Recovery: lock and submit the mapped buffers */
1608 do {
1609 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1610 lock_buffer(bh);
1611 mark_buffer_async_write(bh);
1612 } else {
1613 /*
1614 * The buffer may have been set dirty during
1615 * attachment to a dirty page.
1616 */
1617 clear_buffer_dirty(bh);
1618 }
1619 } while ((bh = bh->b_this_page) != head);
1620 SetPageError(page);
1621 BUG_ON(PageWriteback(page));
1622 set_page_writeback(page);
1623 do {
1624 struct buffer_head *next = bh->b_this_page;
1625 if (buffer_async_write(bh)) {
1626 clear_buffer_dirty(bh);
1627 submit_bh(WRITE, bh);
1628 nr_underway++;
1629 }
1630 bh = next;
1631 } while (bh != head);
1632 unlock_page(page);
1633 goto done;
1634}
1635
1636/*
1637 * The generic ->writepage function for buffer-backed address_spaces
1638 */
Chris Masondee26a92007-03-26 16:00:06 -04001639static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1640{
Chris Mason236454df2007-04-19 13:37:44 -04001641 struct inode * const inode = page->mapping->host;
1642 loff_t i_size = i_size_read(inode);
1643 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1644 unsigned offset;
1645 void *kaddr;
1646
1647 /* Is the page fully inside i_size? */
1648 if (page->index < end_index)
1649 return __btrfs_write_full_page(inode, page, wbc);
1650
1651 /* Is the page fully outside i_size? (truncate in progress) */
1652 offset = i_size & (PAGE_CACHE_SIZE-1);
1653 if (page->index >= end_index+1 || !offset) {
1654 /*
1655 * The page may have dirty, unmapped buffers. For example,
1656 * they may have been added in ext3_writepage(). Make them
1657 * freeable here, so the page does not leak.
1658 */
1659 block_invalidatepage(page, 0);
1660 unlock_page(page);
1661 return 0; /* don't care */
1662 }
1663
1664 /*
1665 * The page straddles i_size. It must be zeroed out on each and every
1666 * writepage invokation because it may be mmapped. "A file is mapped
1667 * in multiples of the page size. For a file that is not a multiple of
1668 * the page size, the remaining memory is zeroed when mapped, and
1669 * writes to that region are not written out to the file."
1670 */
1671 kaddr = kmap_atomic(page, KM_USER0);
1672 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1673 flush_dcache_page(page);
1674 kunmap_atomic(kaddr, KM_USER0);
1675 return __btrfs_write_full_page(inode, page, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001676}
Chris Masond561c022007-03-23 19:47:49 -04001677
Chris Masonf4b9aa82007-03-27 11:05:53 -04001678static void btrfs_truncate(struct inode *inode)
1679{
Chris Masond6e4a422007-04-06 15:37:36 -04001680 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001681 int ret;
1682 struct btrfs_trans_handle *trans;
1683
1684 if (!S_ISREG(inode->i_mode))
1685 return;
1686 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1687 return;
1688
Chris Mason1de037a2007-05-29 15:17:08 -04001689 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001690
Chris Masonf4b9aa82007-03-27 11:05:53 -04001691 mutex_lock(&root->fs_info->fs_mutex);
1692 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001693 btrfs_set_trans_block_group(trans, inode);
Chris Mason1de037a2007-05-29 15:17:08 -04001694
1695 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masonf4b9aa82007-03-27 11:05:53 -04001696 ret = btrfs_truncate_in_trans(trans, root, inode);
1697 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04001698 btrfs_update_inode(trans, root, inode);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001699 ret = btrfs_end_transaction(trans, root);
1700 BUG_ON(ret);
1701 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04001702 btrfs_btree_balance_dirty(root);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001703}
1704
Chris Mason236454df2007-04-19 13:37:44 -04001705static int btrfs_commit_write(struct file *file, struct page *page,
1706 unsigned from, unsigned to)
1707{
1708 struct inode *inode = page->mapping->host;
1709 struct buffer_head *bh;
1710 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1711
1712 SetPageUptodate(page);
1713 bh = page_buffers(page);
Chris Mason1de037a2007-05-29 15:17:08 -04001714 set_buffer_uptodate(bh);
Chris Mason236454df2007-04-19 13:37:44 -04001715 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1716 set_page_dirty(page);
1717 }
1718 if (pos > inode->i_size) {
1719 i_size_write(inode, pos);
1720 mark_inode_dirty(inode);
1721 }
1722 return 0;
1723}
1724
Chris Mason75dfe392007-03-29 11:56:46 -04001725static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1726 struct page **prepared_pages,
1727 const char __user * buf)
1728{
1729 long page_fault = 0;
1730 int i;
1731 int offset = pos & (PAGE_CACHE_SIZE - 1);
1732
1733 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1734 size_t count = min_t(size_t,
1735 PAGE_CACHE_SIZE - offset, write_bytes);
1736 struct page *page = prepared_pages[i];
1737 fault_in_pages_readable(buf, count);
1738
1739 /* Copy data from userspace to the current page */
1740 kmap(page);
1741 page_fault = __copy_from_user(page_address(page) + offset,
1742 buf, count);
1743 /* Flush processor's dcache for this page */
1744 flush_dcache_page(page);
1745 kunmap(page);
1746 buf += count;
1747 write_bytes -= count;
1748
1749 if (page_fault)
1750 break;
1751 }
1752 return page_fault ? -EFAULT : 0;
1753}
1754
1755static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1756{
1757 size_t i;
1758 for (i = 0; i < num_pages; i++) {
1759 if (!pages[i])
1760 break;
1761 unlock_page(pages[i]);
1762 mark_page_accessed(pages[i]);
1763 page_cache_release(pages[i]);
1764 }
1765}
1766static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1767 struct btrfs_root *root,
1768 struct file *file,
1769 struct page **pages,
1770 size_t num_pages,
1771 loff_t pos,
1772 size_t write_bytes)
1773{
1774 int i;
1775 int offset;
1776 int err = 0;
1777 int ret;
1778 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001779 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason236454df2007-04-19 13:37:44 -04001780 struct buffer_head *bh;
1781 struct btrfs_file_extent_item *ei;
Chris Mason75dfe392007-03-29 11:56:46 -04001782
1783 for (i = 0; i < num_pages; i++) {
1784 offset = pos & (PAGE_CACHE_SIZE -1);
1785 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001786 /* FIXME, one block at a time */
1787
1788 mutex_lock(&root->fs_info->fs_mutex);
1789 trans = btrfs_start_transaction(root, 1);
Chris Mason31f3c992007-04-30 15:25:45 -04001790 btrfs_set_trans_block_group(trans, inode);
Chris Mason236454df2007-04-19 13:37:44 -04001791
1792 bh = page_buffers(pages[i]);
1793 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1794 struct btrfs_key key;
1795 struct btrfs_path *path;
1796 char *ptr;
1797 u32 datasize;
1798
1799 path = btrfs_alloc_path();
1800 BUG_ON(!path);
1801 key.objectid = inode->i_ino;
1802 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1803 key.flags = 0;
1804 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1805 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1806 datasize = offset +
1807 btrfs_file_extent_calc_inline_size(write_bytes);
1808 ret = btrfs_insert_empty_item(trans, root, path, &key,
1809 datasize);
1810 BUG_ON(ret);
1811 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1812 path->slots[0], struct btrfs_file_extent_item);
1813 btrfs_set_file_extent_generation(ei, trans->transid);
1814 btrfs_set_file_extent_type(ei,
1815 BTRFS_FILE_EXTENT_INLINE);
1816 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason098f59c2007-05-11 11:33:21 -04001817 btrfs_memcpy(root, path->nodes[0]->b_data,
1818 ptr, bh->b_data, offset + write_bytes);
Chris Mason236454df2007-04-19 13:37:44 -04001819 mark_buffer_dirty(path->nodes[0]);
1820 btrfs_free_path(path);
Chris Mason3a686372007-05-24 13:35:57 -04001821 } else if (buffer_mapped(bh)) {
Chris Mason236454df2007-04-19 13:37:44 -04001822 btrfs_csum_file_block(trans, root, inode->i_ino,
Chris Masonf254e522007-03-29 15:15:27 -04001823 pages[i]->index << PAGE_CACHE_SHIFT,
1824 kmap(pages[i]), PAGE_CACHE_SIZE);
Chris Mason236454df2007-04-19 13:37:44 -04001825 kunmap(pages[i]);
1826 }
Chris Masonf254e522007-03-29 15:15:27 -04001827 SetPageChecked(pages[i]);
Chris Masone37c9e62007-05-09 20:13:14 -04001828 // btrfs_update_inode_block_group(trans, inode);
Chris Masonf254e522007-03-29 15:15:27 -04001829 ret = btrfs_end_transaction(trans, root);
1830 BUG_ON(ret);
1831 mutex_unlock(&root->fs_info->fs_mutex);
1832
Chris Mason236454df2007-04-19 13:37:44 -04001833 ret = btrfs_commit_write(file, pages[i], offset,
Chris Mason75dfe392007-03-29 11:56:46 -04001834 offset + this_write);
1835 pos += this_write;
1836 if (ret) {
1837 err = ret;
1838 goto failed;
1839 }
1840 WARN_ON(this_write > write_bytes);
1841 write_bytes -= this_write;
1842 }
1843failed:
1844 return err;
1845}
1846
Chris Masonb18c6682007-04-17 13:26:50 -04001847static int drop_extents(struct btrfs_trans_handle *trans,
1848 struct btrfs_root *root,
1849 struct inode *inode,
Chris Masonde428b62007-05-18 13:28:27 -04001850 u64 start, u64 end, u64 *hint_block)
Chris Masonb18c6682007-04-17 13:26:50 -04001851{
1852 int ret;
1853 struct btrfs_key key;
1854 struct btrfs_leaf *leaf;
1855 int slot;
1856 struct btrfs_file_extent_item *extent;
Chris Mason236454df2007-04-19 13:37:44 -04001857 u64 extent_end = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04001858 int keep;
1859 struct btrfs_file_extent_item old;
1860 struct btrfs_path *path;
1861 u64 search_start = start;
1862 int bookend;
Chris Mason236454df2007-04-19 13:37:44 -04001863 int found_type;
1864 int found_extent;
1865 int found_inline;
1866
Chris Masonb18c6682007-04-17 13:26:50 -04001867 path = btrfs_alloc_path();
1868 if (!path)
1869 return -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04001870 while(1) {
Chris Masona429e512007-04-18 16:15:28 -04001871 btrfs_release_path(root, path);
1872 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1873 search_start, -1);
1874 if (ret < 0)
1875 goto out;
1876 if (ret > 0) {
1877 if (path->slots[0] == 0) {
Chris Mason236454df2007-04-19 13:37:44 -04001878 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001879 goto out;
1880 }
1881 path->slots[0]--;
1882 }
Chris Masonb18c6682007-04-17 13:26:50 -04001883 keep = 0;
1884 bookend = 0;
Chris Mason236454df2007-04-19 13:37:44 -04001885 found_extent = 0;
1886 found_inline = 0;
1887 extent = NULL;
Chris Masonb18c6682007-04-17 13:26:50 -04001888 leaf = btrfs_buffer_leaf(path->nodes[0]);
1889 slot = path->slots[0];
1890 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
Chris Masonb18c6682007-04-17 13:26:50 -04001891 if (key.offset >= end || key.objectid != inode->i_ino) {
1892 ret = 0;
1893 goto out;
1894 }
Chris Mason236454df2007-04-19 13:37:44 -04001895 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1896 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001897 goto out;
Chris Mason236454df2007-04-19 13:37:44 -04001898 }
1899 extent = btrfs_item_ptr(leaf, slot,
1900 struct btrfs_file_extent_item);
1901 found_type = btrfs_file_extent_type(extent);
1902 if (found_type == BTRFS_FILE_EXTENT_REG) {
1903 extent_end = key.offset +
1904 (btrfs_file_extent_num_blocks(extent) <<
1905 inode->i_blkbits);
1906 found_extent = 1;
1907 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1908 found_inline = 1;
1909 extent_end = key.offset +
1910 btrfs_file_extent_inline_len(leaf->items + slot);
1911 }
1912
1913 if (!found_extent && !found_inline) {
1914 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -04001915 goto out;
Chris Mason236454df2007-04-19 13:37:44 -04001916 }
1917
1918 if (search_start >= extent_end) {
1919 ret = 0;
1920 goto out;
1921 }
1922
Chris Mason3a686372007-05-24 13:35:57 -04001923 if (found_inline) {
1924 u64 mask = root->blocksize - 1;
1925 search_start = (extent_end + mask) & ~mask;
1926 } else
1927 search_start = extent_end;
Chris Masonb18c6682007-04-17 13:26:50 -04001928
1929 if (end < extent_end && end >= key.offset) {
Chris Mason236454df2007-04-19 13:37:44 -04001930 if (found_extent) {
Chris Mason3a686372007-05-24 13:35:57 -04001931 u64 disk_blocknr =
1932 btrfs_file_extent_disk_blocknr(extent);
1933 u64 disk_num_blocks =
1934 btrfs_file_extent_disk_num_blocks(extent);
Chris Mason236454df2007-04-19 13:37:44 -04001935 memcpy(&old, extent, sizeof(old));
Chris Mason3a686372007-05-24 13:35:57 -04001936 if (disk_blocknr != 0) {
1937 ret = btrfs_inc_extent_ref(trans, root,
1938 disk_blocknr, disk_num_blocks);
1939 BUG_ON(ret);
1940 }
Chris Mason236454df2007-04-19 13:37:44 -04001941 }
1942 WARN_ON(found_inline);
Chris Masonb18c6682007-04-17 13:26:50 -04001943 bookend = 1;
1944 }
1945
1946 if (start > key.offset) {
1947 u64 new_num;
Chris Masona429e512007-04-18 16:15:28 -04001948 u64 old_num;
Chris Masonb18c6682007-04-17 13:26:50 -04001949 /* truncate existing extent */
1950 keep = 1;
1951 WARN_ON(start & (root->blocksize - 1));
Chris Mason236454df2007-04-19 13:37:44 -04001952 if (found_extent) {
1953 new_num = (start - key.offset) >>
1954 inode->i_blkbits;
1955 old_num = btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001956 *hint_block =
1957 btrfs_file_extent_disk_blocknr(extent);
Chris Mason3a686372007-05-24 13:35:57 -04001958 if (btrfs_file_extent_disk_blocknr(extent)) {
1959 inode->i_blocks -=
1960 (old_num - new_num) << 3;
1961 }
Chris Mason236454df2007-04-19 13:37:44 -04001962 btrfs_set_file_extent_num_blocks(extent,
1963 new_num);
1964 mark_buffer_dirty(path->nodes[0]);
1965 } else {
1966 WARN_ON(1);
Chris Mason236454df2007-04-19 13:37:44 -04001967 }
Chris Masonb18c6682007-04-17 13:26:50 -04001968 }
1969 if (!keep) {
Chris Mason236454df2007-04-19 13:37:44 -04001970 u64 disk_blocknr = 0;
1971 u64 disk_num_blocks = 0;
1972 u64 extent_num_blocks = 0;
1973 if (found_extent) {
1974 disk_blocknr =
1975 btrfs_file_extent_disk_blocknr(extent);
1976 disk_num_blocks =
1977 btrfs_file_extent_disk_num_blocks(extent);
1978 extent_num_blocks =
1979 btrfs_file_extent_num_blocks(extent);
Chris Masonde428b62007-05-18 13:28:27 -04001980 *hint_block =
1981 btrfs_file_extent_disk_blocknr(extent);
Chris Mason236454df2007-04-19 13:37:44 -04001982 }
Chris Masonb18c6682007-04-17 13:26:50 -04001983 ret = btrfs_del_item(trans, root, path);
1984 BUG_ON(ret);
1985 btrfs_release_path(root, path);
Chris Mason098f59c2007-05-11 11:33:21 -04001986 extent = NULL;
Chris Mason3a686372007-05-24 13:35:57 -04001987 if (found_extent && disk_blocknr != 0) {
Chris Mason098f59c2007-05-11 11:33:21 -04001988 inode->i_blocks -= extent_num_blocks << 3;
Chris Mason236454df2007-04-19 13:37:44 -04001989 ret = btrfs_free_extent(trans, root,
1990 disk_blocknr,
1991 disk_num_blocks, 0);
1992 }
Chris Masonb18c6682007-04-17 13:26:50 -04001993
1994 BUG_ON(ret);
1995 if (!bookend && search_start >= end) {
1996 ret = 0;
1997 goto out;
1998 }
1999 if (!bookend)
Chris Masona429e512007-04-18 16:15:28 -04002000 continue;
Chris Masonb18c6682007-04-17 13:26:50 -04002001 }
Chris Mason236454df2007-04-19 13:37:44 -04002002 if (bookend && found_extent) {
Chris Masonb18c6682007-04-17 13:26:50 -04002003 /* create bookend */
2004 struct btrfs_key ins;
Chris Masonb18c6682007-04-17 13:26:50 -04002005 ins.objectid = inode->i_ino;
2006 ins.offset = end;
2007 ins.flags = 0;
2008 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
2009
2010 btrfs_release_path(root, path);
Chris Masonb18c6682007-04-17 13:26:50 -04002011 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2012 sizeof(*extent));
2013 BUG_ON(ret);
2014 extent = btrfs_item_ptr(
2015 btrfs_buffer_leaf(path->nodes[0]),
2016 path->slots[0],
2017 struct btrfs_file_extent_item);
2018 btrfs_set_file_extent_disk_blocknr(extent,
2019 btrfs_file_extent_disk_blocknr(&old));
2020 btrfs_set_file_extent_disk_num_blocks(extent,
2021 btrfs_file_extent_disk_num_blocks(&old));
2022
2023 btrfs_set_file_extent_offset(extent,
2024 btrfs_file_extent_offset(&old) +
2025 ((end - key.offset) >> inode->i_blkbits));
2026 WARN_ON(btrfs_file_extent_num_blocks(&old) <
Chris Mason3a686372007-05-24 13:35:57 -04002027 (extent_end - end) >> inode->i_blkbits);
Chris Masonb18c6682007-04-17 13:26:50 -04002028 btrfs_set_file_extent_num_blocks(extent,
Chris Mason3a686372007-05-24 13:35:57 -04002029 (extent_end - end) >> inode->i_blkbits);
Chris Masonb18c6682007-04-17 13:26:50 -04002030
Chris Mason236454df2007-04-19 13:37:44 -04002031 btrfs_set_file_extent_type(extent,
2032 BTRFS_FILE_EXTENT_REG);
Chris Masonb18c6682007-04-17 13:26:50 -04002033 btrfs_set_file_extent_generation(extent,
2034 btrfs_file_extent_generation(&old));
Chris Masonb18c6682007-04-17 13:26:50 -04002035 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3a686372007-05-24 13:35:57 -04002036 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
2037 inode->i_blocks +=
2038 btrfs_file_extent_num_blocks(extent) << 3;
2039 }
Chris Masonb18c6682007-04-17 13:26:50 -04002040 ret = 0;
Chris Mason70b2bef2007-04-17 15:39:32 -04002041 goto out;
Chris Masonb18c6682007-04-17 13:26:50 -04002042 }
Chris Masonb18c6682007-04-17 13:26:50 -04002043 }
Chris Masonb18c6682007-04-17 13:26:50 -04002044out:
Chris Masonb18c6682007-04-17 13:26:50 -04002045 btrfs_free_path(path);
2046 return ret;
2047}
2048
2049static int prepare_pages(struct btrfs_root *root,
Chris Mason75dfe392007-03-29 11:56:46 -04002050 struct file *file,
2051 struct page **pages,
2052 size_t num_pages,
2053 loff_t pos,
Chris Mason2932f3e2007-04-10 14:22:02 -04002054 unsigned long first_index,
2055 unsigned long last_index,
Chris Mason6567e832007-04-16 09:22:45 -04002056 size_t write_bytes,
2057 u64 alloc_extent_start)
Chris Mason75dfe392007-03-29 11:56:46 -04002058{
2059 int i;
2060 unsigned long index = pos >> PAGE_CACHE_SHIFT;
2061 struct inode *inode = file->f_path.dentry->d_inode;
2062 int offset;
2063 int err = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04002064 int this_write;
Chris Mason6567e832007-04-16 09:22:45 -04002065 struct buffer_head *bh;
2066 struct buffer_head *head;
Chris Mason75dfe392007-03-29 11:56:46 -04002067 loff_t isize = i_size_read(inode);
2068
2069 memset(pages, 0, num_pages * sizeof(struct page *));
2070
2071 for (i = 0; i < num_pages; i++) {
2072 pages[i] = grab_cache_page(inode->i_mapping, index + i);
2073 if (!pages[i]) {
2074 err = -ENOMEM;
2075 goto failed_release;
2076 }
Chris Mason35b7e472007-05-02 15:53:43 -04002077 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
2078 wait_on_page_writeback(pages[i]);
Chris Mason75dfe392007-03-29 11:56:46 -04002079 offset = pos & (PAGE_CACHE_SIZE -1);
2080 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Mason35b7e472007-05-02 15:53:43 -04002081 if (!page_has_buffers(pages[i])) {
2082 create_empty_buffers(pages[i],
2083 root->fs_info->sb->s_blocksize,
2084 (1 << BH_Uptodate));
2085 }
Chris Mason6567e832007-04-16 09:22:45 -04002086 head = page_buffers(pages[i]);
2087 bh = head;
2088 do {
2089 err = btrfs_map_bh_to_logical(root, bh,
2090 alloc_extent_start);
2091 BUG_ON(err);
2092 if (err)
2093 goto failed_truncate;
2094 bh = bh->b_this_page;
Chris Mason236454df2007-04-19 13:37:44 -04002095 if (alloc_extent_start)
2096 alloc_extent_start++;
Chris Mason6567e832007-04-16 09:22:45 -04002097 } while (bh != head);
Chris Mason75dfe392007-03-29 11:56:46 -04002098 pos += this_write;
Chris Mason75dfe392007-03-29 11:56:46 -04002099 WARN_ON(this_write > write_bytes);
2100 write_bytes -= this_write;
2101 }
2102 return 0;
2103
2104failed_release:
2105 btrfs_drop_pages(pages, num_pages);
2106 return err;
2107
2108failed_truncate:
2109 btrfs_drop_pages(pages, num_pages);
2110 if (pos > isize)
2111 vmtruncate(inode, isize);
2112 return err;
2113}
2114
2115static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
2116 size_t count, loff_t *ppos)
2117{
2118 loff_t pos;
2119 size_t num_written = 0;
2120 int err = 0;
2121 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04002122 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04002123 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonb18c6682007-04-17 13:26:50 -04002124 struct page *pages[8];
Chris Mason35b7e472007-05-02 15:53:43 -04002125 struct page *pinned[2];
Chris Mason2932f3e2007-04-10 14:22:02 -04002126 unsigned long first_index;
2127 unsigned long last_index;
Chris Mason6567e832007-04-16 09:22:45 -04002128 u64 start_pos;
2129 u64 num_blocks;
2130 u64 alloc_extent_start;
Chris Masonde428b62007-05-18 13:28:27 -04002131 u64 hint_block;
Chris Mason6567e832007-04-16 09:22:45 -04002132 struct btrfs_trans_handle *trans;
Chris Masonb18c6682007-04-17 13:26:50 -04002133 struct btrfs_key ins;
Chris Mason35b7e472007-05-02 15:53:43 -04002134 pinned[0] = NULL;
2135 pinned[1] = NULL;
Chris Mason75dfe392007-03-29 11:56:46 -04002136 if (file->f_flags & O_DIRECT)
2137 return -EINVAL;
2138 pos = *ppos;
Chris Mason75dfe392007-03-29 11:56:46 -04002139 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2140 current->backing_dev_info = inode->i_mapping->backing_dev_info;
2141 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2142 if (err)
2143 goto out;
2144 if (count == 0)
2145 goto out;
2146 err = remove_suid(file->f_path.dentry);
2147 if (err)
2148 goto out;
2149 file_update_time(file);
Chris Masona429e512007-04-18 16:15:28 -04002150
2151 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
2152 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
2153 inode->i_blkbits;
2154
Chris Mason75dfe392007-03-29 11:56:46 -04002155 mutex_lock(&inode->i_mutex);
Chris Mason2932f3e2007-04-10 14:22:02 -04002156 first_index = pos >> PAGE_CACHE_SHIFT;
2157 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
Chris Mason6567e832007-04-16 09:22:45 -04002158
Chris Mason1de037a2007-05-29 15:17:08 -04002159 if ((pos & (PAGE_CACHE_SIZE - 1))) {
Chris Masona429e512007-04-18 16:15:28 -04002160 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
2161 if (!PageUptodate(pinned[0])) {
2162 ret = mpage_readpage(pinned[0], btrfs_get_block);
2163 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04002164 wait_on_page_locked(pinned[0]);
Chris Masona429e512007-04-18 16:15:28 -04002165 } else {
2166 unlock_page(pinned[0]);
2167 }
2168 }
Chris Mason1de037a2007-05-29 15:17:08 -04002169 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
Chris Masona429e512007-04-18 16:15:28 -04002170 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
2171 if (!PageUptodate(pinned[1])) {
2172 ret = mpage_readpage(pinned[1], btrfs_get_block);
2173 BUG_ON(ret);
Chris Mason35b7e472007-05-02 15:53:43 -04002174 wait_on_page_locked(pinned[1]);
Chris Masona429e512007-04-18 16:15:28 -04002175 } else {
2176 unlock_page(pinned[1]);
2177 }
2178 }
2179
Chris Mason6567e832007-04-16 09:22:45 -04002180 mutex_lock(&root->fs_info->fs_mutex);
2181 trans = btrfs_start_transaction(root, 1);
2182 if (!trans) {
2183 err = -ENOMEM;
Chris Masonb18c6682007-04-17 13:26:50 -04002184 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -04002185 goto out_unlock;
2186 }
Chris Mason31f3c992007-04-30 15:25:45 -04002187 btrfs_set_trans_block_group(trans, inode);
Chris Masona429e512007-04-18 16:15:28 -04002188 /* FIXME blocksize != 4096 */
2189 inode->i_blocks += num_blocks << 3;
Chris Masonde428b62007-05-18 13:28:27 -04002190 hint_block = 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002191 if (start_pos < inode->i_size) {
Chris Masona429e512007-04-18 16:15:28 -04002192 /* FIXME blocksize != pagesize */
Chris Masonb18c6682007-04-17 13:26:50 -04002193 ret = drop_extents(trans, root, inode,
2194 start_pos,
2195 (pos + count + root->blocksize -1) &
Chris Masonde428b62007-05-18 13:28:27 -04002196 ~((u64)root->blocksize - 1), &hint_block);
Chris Mason236454df2007-04-19 13:37:44 -04002197 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04002198 }
Chris Mason3a686372007-05-24 13:35:57 -04002199 if (inode->i_size < start_pos) {
2200 u64 last_pos_in_file;
2201 u64 hole_size;
2202 u64 mask = root->blocksize - 1;
2203 last_pos_in_file = (inode->i_size + mask) & ~mask;
2204 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
2205 hole_size >>= inode->i_blkbits;
2206 if (last_pos_in_file < start_pos) {
2207 ret = btrfs_insert_file_extent(trans, root,
2208 inode->i_ino,
2209 last_pos_in_file,
2210 0, 0, hole_size);
2211 }
2212 BUG_ON(ret);
2213 }
Chris Mason236454df2007-04-19 13:37:44 -04002214 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
2215 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
Chris Mason4d775672007-04-20 20:23:12 -04002216 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
Chris Masonde428b62007-05-18 13:28:27 -04002217 num_blocks, hint_block, (u64)-1,
2218 &ins, 1);
Chris Mason236454df2007-04-19 13:37:44 -04002219 BUG_ON(ret);
2220 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
Chris Mason3a686372007-05-24 13:35:57 -04002221 start_pos, ins.objectid, ins.offset,
2222 ins.offset);
Chris Mason236454df2007-04-19 13:37:44 -04002223 BUG_ON(ret);
2224 } else {
2225 ins.offset = 0;
2226 ins.objectid = 0;
2227 }
Chris Masonb18c6682007-04-17 13:26:50 -04002228 BUG_ON(ret);
2229 alloc_extent_start = ins.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04002230 // btrfs_update_inode_block_group(trans, inode);
Chris Masonb18c6682007-04-17 13:26:50 -04002231 ret = btrfs_end_transaction(trans, root);
Chris Mason6567e832007-04-16 09:22:45 -04002232 mutex_unlock(&root->fs_info->fs_mutex);
2233
Chris Mason75dfe392007-03-29 11:56:46 -04002234 while(count > 0) {
2235 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
2236 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
2237 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
2238 PAGE_CACHE_SHIFT;
Chris Masonb18c6682007-04-17 13:26:50 -04002239
2240 memset(pages, 0, sizeof(pages));
2241 ret = prepare_pages(root, file, pages, num_pages,
Chris Mason6567e832007-04-16 09:22:45 -04002242 pos, first_index, last_index,
2243 write_bytes, alloc_extent_start);
Chris Mason75dfe392007-03-29 11:56:46 -04002244 BUG_ON(ret);
Chris Masonb18c6682007-04-17 13:26:50 -04002245
Chris Mason6567e832007-04-16 09:22:45 -04002246 /* FIXME blocks != pagesize */
Chris Mason236454df2007-04-19 13:37:44 -04002247 if (alloc_extent_start)
2248 alloc_extent_start += num_pages;
Chris Mason75dfe392007-03-29 11:56:46 -04002249 ret = btrfs_copy_from_user(pos, num_pages,
2250 write_bytes, pages, buf);
2251 BUG_ON(ret);
2252
Chris Masonf254e522007-03-29 15:15:27 -04002253 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason70b2bef2007-04-17 15:39:32 -04002254 num_pages, pos, write_bytes);
Chris Mason75dfe392007-03-29 11:56:46 -04002255 BUG_ON(ret);
2256 btrfs_drop_pages(pages, num_pages);
2257
Chris Mason75dfe392007-03-29 11:56:46 -04002258 buf += write_bytes;
2259 count -= write_bytes;
2260 pos += write_bytes;
2261 num_written += write_bytes;
2262
2263 balance_dirty_pages_ratelimited(inode->i_mapping);
Chris Mason35b7e472007-05-02 15:53:43 -04002264 btrfs_btree_balance_dirty(root);
Chris Mason75dfe392007-03-29 11:56:46 -04002265 cond_resched();
2266 }
Chris Mason6567e832007-04-16 09:22:45 -04002267out_unlock:
Chris Mason75dfe392007-03-29 11:56:46 -04002268 mutex_unlock(&inode->i_mutex);
2269out:
Chris Masona429e512007-04-18 16:15:28 -04002270 if (pinned[0])
2271 page_cache_release(pinned[0]);
2272 if (pinned[1])
2273 page_cache_release(pinned[1]);
Chris Mason75dfe392007-03-29 11:56:46 -04002274 *ppos = pos;
2275 current->backing_dev_info = NULL;
Chris Masona429e512007-04-18 16:15:28 -04002276 mark_inode_dirty(inode);
Chris Mason75dfe392007-03-29 11:56:46 -04002277 return num_written ? num_written : err;
2278}
2279
Chris Masonf254e522007-03-29 15:15:27 -04002280static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2281 unsigned long offset, unsigned long size)
2282{
2283 char *kaddr;
2284 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04002285 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04002286
2287 if (size > count)
2288 size = count;
2289
2290 if (!PageChecked(page)) {
2291 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04002292 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1de037a2007-05-29 15:17:08 -04002293 int ret;
2294 struct buffer_head *bh;
Chris Mason236454df2007-04-19 13:37:44 -04002295
Chris Mason1de037a2007-05-29 15:17:08 -04002296 if (page_has_buffers(page)) {
2297 bh = page_buffers(page);
2298 if (!buffer_mapped(bh)) {
2299 SetPageChecked(page);
2300 goto checked;
2301 }
2302 }
2303
2304 ret = btrfs_csum_verify_file_block(root,
Chris Mason236454df2007-04-19 13:37:44 -04002305 page->mapping->host->i_ino,
2306 page->index << PAGE_CACHE_SHIFT,
2307 kmap(page), PAGE_CACHE_SIZE);
Chris Masonf254e522007-03-29 15:15:27 -04002308 if (ret) {
Chris Mason3a686372007-05-24 13:35:57 -04002309 if (ret != -ENOENT) {
Chris Mason1de037a2007-05-29 15:17:08 -04002310 printk("failed to verify ino %lu page %lu ret %d\n",
Chris Mason3a686372007-05-24 13:35:57 -04002311 page->mapping->host->i_ino,
Chris Mason1de037a2007-05-29 15:17:08 -04002312 page->index, ret);
2313 memset(page_address(page), 1, PAGE_CACHE_SIZE);
Chris Mason3a686372007-05-24 13:35:57 -04002314 flush_dcache_page(page);
2315 }
Chris Masonf254e522007-03-29 15:15:27 -04002316 }
2317 SetPageChecked(page);
2318 kunmap(page);
2319 }
Chris Mason1de037a2007-05-29 15:17:08 -04002320checked:
Chris Masonf254e522007-03-29 15:15:27 -04002321 /*
2322 * Faults on the destination of a read are common, so do it before
2323 * taking the kmap.
2324 */
2325 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2326 kaddr = kmap_atomic(page, KM_USER0);
2327 left = __copy_to_user_inatomic(desc->arg.buf,
2328 kaddr + offset, size);
2329 kunmap_atomic(kaddr, KM_USER0);
2330 if (left == 0)
2331 goto success;
2332 }
2333
2334 /* Do it the slow way */
2335 kaddr = kmap(page);
2336 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2337 kunmap(page);
2338
2339 if (left) {
2340 size -= left;
2341 desc->error = -EFAULT;
2342 }
2343success:
2344 desc->count = count - size;
2345 desc->written += size;
2346 desc->arg.buf += size;
2347 return size;
2348}
2349
2350/**
2351 * btrfs_file_aio_read - filesystem read routine
2352 * @iocb: kernel I/O control block
2353 * @iov: io vector request
2354 * @nr_segs: number of segments in the iovec
2355 * @pos: current file position
2356 */
2357static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2358 unsigned long nr_segs, loff_t pos)
2359{
2360 struct file *filp = iocb->ki_filp;
2361 ssize_t retval;
2362 unsigned long seg;
2363 size_t count;
2364 loff_t *ppos = &iocb->ki_pos;
2365
2366 count = 0;
2367 for (seg = 0; seg < nr_segs; seg++) {
2368 const struct iovec *iv = &iov[seg];
2369
2370 /*
2371 * If any segment has a negative length, or the cumulative
2372 * length ever wraps negative then return -EINVAL.
2373 */
2374 count += iv->iov_len;
2375 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2376 return -EINVAL;
2377 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2378 continue;
2379 if (seg == 0)
2380 return -EFAULT;
2381 nr_segs = seg;
2382 count -= iv->iov_len; /* This segment is no good */
2383 break;
2384 }
2385 retval = 0;
2386 if (count) {
2387 for (seg = 0; seg < nr_segs; seg++) {
2388 read_descriptor_t desc;
2389
2390 desc.written = 0;
2391 desc.arg.buf = iov[seg].iov_base;
2392 desc.count = iov[seg].iov_len;
2393 if (desc.count == 0)
2394 continue;
2395 desc.error = 0;
2396 do_generic_file_read(filp, ppos, &desc,
2397 btrfs_read_actor);
2398 retval += desc.written;
2399 if (desc.error) {
2400 retval = retval ?: desc.error;
2401 break;
2402 }
2403 }
2404 }
2405 return retval;
2406}
2407
Chris Mason2619ba12007-04-10 16:58:11 -04002408static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2409{
2410 struct btrfs_trans_handle *trans;
2411 struct btrfs_key key;
2412 struct btrfs_root_item root_item;
2413 struct btrfs_inode_item *inode_item;
2414 struct buffer_head *subvol;
2415 struct btrfs_leaf *leaf;
2416 struct btrfs_root *new_root;
2417 struct inode *inode;
Chris Mason31f3c992007-04-30 15:25:45 -04002418 struct inode *dir;
Chris Mason2619ba12007-04-10 16:58:11 -04002419 int ret;
2420 u64 objectid;
2421 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2422
2423 mutex_lock(&root->fs_info->fs_mutex);
2424 trans = btrfs_start_transaction(root, 1);
2425 BUG_ON(!trans);
2426
Chris Mason31f3c992007-04-30 15:25:45 -04002427 subvol = btrfs_alloc_free_block(trans, root, 0);
Chris Mason5e828492007-04-23 19:10:49 -04002428 if (subvol == NULL)
2429 return -ENOSPC;
Chris Mason2619ba12007-04-10 16:58:11 -04002430 leaf = btrfs_buffer_leaf(subvol);
2431 btrfs_set_header_nritems(&leaf->header, 0);
2432 btrfs_set_header_level(&leaf->header, 0);
Chris Mason7eccb902007-04-11 15:53:25 -04002433 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002434 btrfs_set_header_generation(&leaf->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04002435 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
Chris Mason2619ba12007-04-10 16:58:11 -04002436 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2437 sizeof(leaf->header.fsid));
Chris Mason4d775672007-04-20 20:23:12 -04002438 mark_buffer_dirty(subvol);
Chris Mason2619ba12007-04-10 16:58:11 -04002439
2440 inode_item = &root_item.inode;
2441 memset(inode_item, 0, sizeof(*inode_item));
2442 btrfs_set_inode_generation(inode_item, 1);
2443 btrfs_set_inode_size(inode_item, 3);
2444 btrfs_set_inode_nlink(inode_item, 1);
2445 btrfs_set_inode_nblocks(inode_item, 1);
2446 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2447
Chris Mason7eccb902007-04-11 15:53:25 -04002448 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
Chris Mason2619ba12007-04-10 16:58:11 -04002449 btrfs_set_root_refs(&root_item, 1);
Chris Mason5e828492007-04-23 19:10:49 -04002450 brelse(subvol);
2451 subvol = NULL;
Chris Mason2619ba12007-04-10 16:58:11 -04002452
Chris Mason2619ba12007-04-10 16:58:11 -04002453 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2454 0, &objectid);
2455 BUG_ON(ret);
2456
2457 btrfs_set_root_dirid(&root_item, new_dirid);
2458
2459 key.objectid = objectid;
2460 key.offset = 1;
2461 key.flags = 0;
2462 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2463 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2464 &root_item);
2465 BUG_ON(ret);
2466
2467 /*
2468 * insert the directory item
2469 */
2470 key.offset = (u64)-1;
Chris Mason31f3c992007-04-30 15:25:45 -04002471 dir = root->fs_info->sb->s_root->d_inode;
Chris Mason2619ba12007-04-10 16:58:11 -04002472 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
Chris Masonfabb5682007-06-07 22:13:21 -04002473 name, namelen, dir->i_ino, &key,
2474 BTRFS_FT_DIR);
Chris Mason2619ba12007-04-10 16:58:11 -04002475 BUG_ON(ret);
2476
2477 ret = btrfs_commit_transaction(trans, root);
2478 BUG_ON(ret);
2479
2480 new_root = btrfs_read_fs_root(root->fs_info, &key);
2481 BUG_ON(!new_root);
2482
2483 trans = btrfs_start_transaction(new_root, 1);
2484 BUG_ON(!trans);
2485
Chris Mason31f3c992007-04-30 15:25:45 -04002486 inode = btrfs_new_inode(trans, new_root, new_dirid,
2487 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason2619ba12007-04-10 16:58:11 -04002488 inode->i_op = &btrfs_dir_inode_operations;
2489 inode->i_fop = &btrfs_dir_file_operations;
2490
2491 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2492 BUG_ON(ret);
2493
2494 inode->i_nlink = 1;
2495 inode->i_size = 6;
2496 ret = btrfs_update_inode(trans, new_root, inode);
2497 BUG_ON(ret);
2498
2499 ret = btrfs_commit_transaction(trans, new_root);
2500 BUG_ON(ret);
2501
2502 iput(inode);
2503
2504 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002505 btrfs_btree_balance_dirty(root);
Chris Mason2619ba12007-04-10 16:58:11 -04002506 return 0;
2507}
2508
Chris Masonc5739bb2007-04-10 09:27:04 -04002509static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2510{
2511 struct btrfs_trans_handle *trans;
2512 struct btrfs_key key;
2513 struct btrfs_root_item new_root_item;
2514 int ret;
2515 u64 objectid;
2516
Chris Mason2619ba12007-04-10 16:58:11 -04002517 if (!root->ref_cows)
2518 return -EINVAL;
2519
Chris Masonc5739bb2007-04-10 09:27:04 -04002520 mutex_lock(&root->fs_info->fs_mutex);
2521 trans = btrfs_start_transaction(root, 1);
2522 BUG_ON(!trans);
2523
2524 ret = btrfs_update_inode(trans, root, root->inode);
2525 BUG_ON(ret);
2526
Chris Mason1b05da22007-04-10 12:13:09 -04002527 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2528 0, &objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -04002529 BUG_ON(ret);
2530
Chris Masonc5739bb2007-04-10 09:27:04 -04002531 memcpy(&new_root_item, &root->root_item,
2532 sizeof(new_root_item));
2533
2534 key.objectid = objectid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002535 key.offset = 1;
2536 key.flags = 0;
2537 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason7eccb902007-04-11 15:53:25 -04002538 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
Chris Masonc5739bb2007-04-10 09:27:04 -04002539
2540 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2541 &new_root_item);
2542 BUG_ON(ret);
2543
Chris Masonc5739bb2007-04-10 09:27:04 -04002544 /*
2545 * insert the directory item
2546 */
2547 key.offset = (u64)-1;
2548 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2549 name, namelen,
2550 root->fs_info->sb->s_root->d_inode->i_ino,
Chris Masonfabb5682007-06-07 22:13:21 -04002551 &key, BTRFS_FT_DIR);
Chris Masonc5739bb2007-04-10 09:27:04 -04002552
2553 BUG_ON(ret);
2554
2555 ret = btrfs_inc_root_ref(trans, root);
2556 BUG_ON(ret);
2557
2558 ret = btrfs_commit_transaction(trans, root);
2559 BUG_ON(ret);
2560 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason35b7e472007-05-02 15:53:43 -04002561 btrfs_btree_balance_dirty(root);
Chris Masonc5739bb2007-04-10 09:27:04 -04002562 return 0;
2563}
2564
2565static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2566 cmd, unsigned long arg)
2567{
2568 struct btrfs_root *root = BTRFS_I(inode)->root;
2569 struct btrfs_ioctl_vol_args vol_args;
Chris Mason8352d8a2007-04-12 10:43:05 -04002570 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -04002571 struct btrfs_dir_item *di;
Chris Masonc5739bb2007-04-10 09:27:04 -04002572 int namelen;
Chris Mason2619ba12007-04-10 16:58:11 -04002573 struct btrfs_path *path;
2574 u64 root_dirid;
Chris Masonc5739bb2007-04-10 09:27:04 -04002575
Chris Masonc5739bb2007-04-10 09:27:04 -04002576 switch (cmd) {
2577 case BTRFS_IOC_SNAP_CREATE:
2578 if (copy_from_user(&vol_args,
2579 (struct btrfs_ioctl_vol_args __user *)arg,
2580 sizeof(vol_args)))
2581 return -EFAULT;
2582 namelen = strlen(vol_args.name);
2583 if (namelen > BTRFS_VOL_NAME_MAX)
2584 return -EINVAL;
Chris Mason2619ba12007-04-10 16:58:11 -04002585 path = btrfs_alloc_path();
2586 if (!path)
2587 return -ENOMEM;
Chris Mason2d13d8d2007-04-10 20:07:20 -04002588 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Chris Mason2619ba12007-04-10 16:58:11 -04002589 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason7e381802007-04-19 15:36:27 -04002590 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
Chris Mason2619ba12007-04-10 16:58:11 -04002591 path, root_dirid,
2592 vol_args.name, namelen, 0);
2593 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2d13d8d2007-04-10 20:07:20 -04002594 btrfs_free_path(path);
Chris Mason7e381802007-04-19 15:36:27 -04002595 if (di && !IS_ERR(di))
Chris Mason2619ba12007-04-10 16:58:11 -04002596 return -EEXIST;
2597
2598 if (root == root->fs_info->tree_root)
2599 ret = create_subvol(root, vol_args.name, namelen);
2600 else
2601 ret = create_snapshot(root, vol_args.name, namelen);
Chris Masonc5739bb2007-04-10 09:27:04 -04002602 WARN_ON(ret);
2603 break;
2604 default:
2605 return -ENOTTY;
2606 }
Chris Mason8352d8a2007-04-12 10:43:05 -04002607 return ret;
Chris Masonc5739bb2007-04-10 09:27:04 -04002608}
2609
Chris Mason08607c12007-06-08 15:33:54 -04002610#ifdef CONFIG_COMPAT
2611static long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2612 unsigned long arg)
2613{
2614 struct inode *inode = file->f_path.dentry->d_inode;
2615 int ret;
2616 lock_kernel();
2617 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2618 unlock_kernel();
2619 return ret;
2620
2621}
2622#endif
2623
Chris Mason2c90e5d2007-04-02 10:50:19 -04002624static struct kmem_cache *btrfs_inode_cachep;
2625struct kmem_cache *btrfs_trans_handle_cachep;
2626struct kmem_cache *btrfs_transaction_cachep;
2627struct kmem_cache *btrfs_bit_radix_cachep;
2628struct kmem_cache *btrfs_path_cachep;
2629
2630/*
2631 * Called inside transaction, so use GFP_NOFS
2632 */
2633static struct inode *btrfs_alloc_inode(struct super_block *sb)
2634{
2635 struct btrfs_inode *ei;
2636
2637 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2638 if (!ei)
2639 return NULL;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002640 return &ei->vfs_inode;
2641}
2642
2643static void btrfs_destroy_inode(struct inode *inode)
2644{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002645 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04002646 WARN_ON(inode->i_data.nrpages);
2647
Chris Mason2c90e5d2007-04-02 10:50:19 -04002648 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2649}
2650
2651static void init_once(void * foo, struct kmem_cache * cachep,
2652 unsigned long flags)
2653{
2654 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2655
Chris Masonf9f3c6b2007-05-21 14:05:12 -04002656 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
Chris Mason2c90e5d2007-04-02 10:50:19 -04002657 SLAB_CTOR_CONSTRUCTOR) {
2658 inode_init_once(&ei->vfs_inode);
2659 }
2660}
2661
2662static int init_inodecache(void)
2663{
2664 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2665 sizeof(struct btrfs_inode),
2666 0, (SLAB_RECLAIM_ACCOUNT|
2667 SLAB_MEM_SPREAD),
2668 init_once, NULL);
2669 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2670 sizeof(struct btrfs_trans_handle),
2671 0, (SLAB_RECLAIM_ACCOUNT|
2672 SLAB_MEM_SPREAD),
2673 NULL, NULL);
2674 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2675 sizeof(struct btrfs_transaction),
2676 0, (SLAB_RECLAIM_ACCOUNT|
2677 SLAB_MEM_SPREAD),
2678 NULL, NULL);
2679 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2680 sizeof(struct btrfs_transaction),
2681 0, (SLAB_RECLAIM_ACCOUNT|
2682 SLAB_MEM_SPREAD),
2683 NULL, NULL);
2684 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2685 256,
2686 0, (SLAB_RECLAIM_ACCOUNT|
2687 SLAB_MEM_SPREAD |
2688 SLAB_DESTROY_BY_RCU),
2689 NULL, NULL);
2690 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2691 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2692 return -ENOMEM;
2693 return 0;
2694}
2695
2696static void destroy_inodecache(void)
2697{
2698 kmem_cache_destroy(btrfs_inode_cachep);
2699 kmem_cache_destroy(btrfs_trans_handle_cachep);
2700 kmem_cache_destroy(btrfs_transaction_cachep);
2701 kmem_cache_destroy(btrfs_bit_radix_cachep);
2702 kmem_cache_destroy(btrfs_path_cachep);
2703}
2704
Chris Mason2e635a22007-03-21 11:12:56 -04002705static int btrfs_get_sb(struct file_system_type *fs_type,
2706 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2707{
2708 return get_sb_bdev(fs_type, flags, dev_name, data,
2709 btrfs_fill_super, mnt);
2710}
2711
Chris Mason236454df2007-04-19 13:37:44 -04002712static int btrfs_getattr(struct vfsmount *mnt,
2713 struct dentry *dentry, struct kstat *stat)
2714{
2715 struct inode *inode = dentry->d_inode;
2716 generic_fillattr(inode, stat);
2717 stat->blksize = 256 * 1024;
2718 return 0;
2719}
2720
Chris Mason8fd17792007-04-19 21:01:03 -04002721static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2722{
2723 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2724 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2725
2726 buf->f_namelen = BTRFS_NAME_LEN;
2727 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2728 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2729 buf->f_bavail = buf->f_bfree;
2730 buf->f_bsize = dentry->d_sb->s_blocksize;
2731 buf->f_type = BTRFS_SUPER_MAGIC;
2732 return 0;
2733}
Chris Masonb5133862007-04-24 11:52:22 -04002734
Chris Masone06afa82007-05-23 15:44:28 -04002735static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2736 struct inode * new_dir,struct dentry *new_dentry)
2737{
2738 struct btrfs_trans_handle *trans;
2739 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2740 struct inode *new_inode = new_dentry->d_inode;
2741 struct inode *old_inode = old_dentry->d_inode;
2742 struct timespec ctime = CURRENT_TIME;
2743 struct btrfs_path *path;
2744 struct btrfs_dir_item *di;
2745 int ret;
2746
2747 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2748 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2749 return -ENOTEMPTY;
2750 }
2751 mutex_lock(&root->fs_info->fs_mutex);
2752 trans = btrfs_start_transaction(root, 1);
2753 btrfs_set_trans_block_group(trans, new_dir);
2754 path = btrfs_alloc_path();
2755 if (!path) {
2756 ret = -ENOMEM;
2757 goto out_fail;
2758 }
2759
2760 old_dentry->d_inode->i_nlink++;
2761 old_dir->i_ctime = old_dir->i_mtime = ctime;
2762 new_dir->i_ctime = new_dir->i_mtime = ctime;
2763 old_inode->i_ctime = ctime;
2764 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2765 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2766 u64 old_parent_oid;
2767 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2768 "..", 2, -1);
2769 if (IS_ERR(di)) {
2770 ret = PTR_ERR(di);
2771 goto out_fail;
2772 }
2773 if (!di) {
2774 ret = -ENOENT;
2775 goto out_fail;
2776 }
2777 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2778 ret = btrfs_del_item(trans, root, path);
2779 if (ret) {
2780 ret = -EIO;
2781 goto out_fail;
2782 }
2783 btrfs_release_path(root, path);
2784
2785 di = btrfs_lookup_dir_index_item(trans, root, path,
2786 old_inode->i_ino,
2787 old_parent_oid,
2788 "..", 2, -1);
2789 if (IS_ERR(di)) {
2790 ret = PTR_ERR(di);
2791 goto out_fail;
2792 }
2793 if (!di) {
2794 ret = -ENOENT;
2795 goto out_fail;
2796 }
2797 ret = btrfs_del_item(trans, root, path);
2798 if (ret) {
2799 ret = -EIO;
2800 goto out_fail;
2801 }
2802 btrfs_release_path(root, path);
2803
2804 ret = btrfs_insert_dir_item(trans, root, "..", 2,
Chris Masonfabb5682007-06-07 22:13:21 -04002805 old_inode->i_ino, location,
2806 BTRFS_FT_DIR);
Chris Masone06afa82007-05-23 15:44:28 -04002807 if (ret)
2808 goto out_fail;
2809 }
2810
2811
Chris Masone06afa82007-05-23 15:44:28 -04002812 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2813 if (ret)
2814 goto out_fail;
2815
2816 if (new_inode) {
2817 new_inode->i_ctime = CURRENT_TIME;
Chris Mason2b8d99a2007-05-24 08:34:10 -04002818 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2819 if (ret)
2820 goto out_fail;
Chris Masone06afa82007-05-23 15:44:28 -04002821 if (S_ISDIR(new_inode->i_mode))
2822 clear_nlink(new_inode);
2823 else
2824 drop_nlink(new_inode);
2825 btrfs_update_inode(trans, root, new_inode);
2826 }
Chris Mason2b8d99a2007-05-24 08:34:10 -04002827 ret = btrfs_add_link(trans, new_dentry, old_inode);
2828 if (ret)
2829 goto out_fail;
2830
Chris Masone06afa82007-05-23 15:44:28 -04002831out_fail:
2832 btrfs_free_path(path);
2833 btrfs_end_transaction(trans, root);
2834 mutex_unlock(&root->fs_info->fs_mutex);
2835 return ret;
2836}
2837
Chris Mason2b8d99a2007-05-24 08:34:10 -04002838static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2839 const char *symname)
2840{
2841 struct btrfs_trans_handle *trans;
2842 struct btrfs_root *root = BTRFS_I(dir)->root;
2843 struct btrfs_path *path;
2844 struct btrfs_key key;
2845 struct inode *inode;
2846 int err;
2847 int drop_inode = 0;
2848 u64 objectid;
2849 int name_len;
2850 int datasize;
2851 char *ptr;
2852 struct btrfs_file_extent_item *ei;
2853
2854 name_len = strlen(symname) + 1;
2855 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2856 return -ENAMETOOLONG;
2857 mutex_lock(&root->fs_info->fs_mutex);
2858 trans = btrfs_start_transaction(root, 1);
2859 btrfs_set_trans_block_group(trans, dir);
2860
2861 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2862 if (err) {
2863 err = -ENOSPC;
2864 goto out_unlock;
2865 }
2866
2867 inode = btrfs_new_inode(trans, root, objectid,
2868 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2869 err = PTR_ERR(inode);
2870 if (IS_ERR(inode))
2871 goto out_unlock;
2872
2873 btrfs_set_trans_block_group(trans, inode);
2874 err = btrfs_add_nondir(trans, dentry, inode);
2875 if (err)
2876 drop_inode = 1;
2877 else {
2878 inode->i_mapping->a_ops = &btrfs_aops;
2879 inode->i_fop = &btrfs_file_operations;
2880 inode->i_op = &btrfs_file_inode_operations;
2881 }
2882 dir->i_sb->s_dirt = 1;
2883 btrfs_update_inode_block_group(trans, inode);
2884 btrfs_update_inode_block_group(trans, dir);
2885 if (drop_inode)
2886 goto out_unlock;
2887
2888 path = btrfs_alloc_path();
2889 BUG_ON(!path);
2890 key.objectid = inode->i_ino;
2891 key.offset = 0;
2892 key.flags = 0;
2893 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2894 datasize = btrfs_file_extent_calc_inline_size(name_len);
2895 err = btrfs_insert_empty_item(trans, root, path, &key,
2896 datasize);
2897 BUG_ON(err);
2898 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2899 path->slots[0], struct btrfs_file_extent_item);
2900 btrfs_set_file_extent_generation(ei, trans->transid);
2901 btrfs_set_file_extent_type(ei,
2902 BTRFS_FILE_EXTENT_INLINE);
2903 ptr = btrfs_file_extent_inline_start(ei);
2904 btrfs_memcpy(root, path->nodes[0]->b_data,
2905 ptr, symname, name_len);
2906 mark_buffer_dirty(path->nodes[0]);
2907 btrfs_free_path(path);
2908 inode->i_op = &btrfs_symlink_inode_operations;
2909 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2910 inode->i_size = name_len - 1;
2911 btrfs_update_inode(trans, root, inode);
2912 err = 0;
2913
2914out_unlock:
2915 btrfs_end_transaction(trans, root);
2916 mutex_unlock(&root->fs_info->fs_mutex);
2917
2918 if (drop_inode) {
2919 inode_dec_link_count(inode);
2920 iput(inode);
2921 }
2922 btrfs_btree_balance_dirty(root);
2923 return err;
2924}
2925
Chris Mason2e635a22007-03-21 11:12:56 -04002926static struct file_system_type btrfs_fs_type = {
2927 .owner = THIS_MODULE,
2928 .name = "btrfs",
2929 .get_sb = btrfs_get_sb,
2930 .kill_sb = kill_block_super,
2931 .fs_flags = FS_REQUIRES_DEV,
2932};
2933
Chris Masone20d96d2007-03-22 12:13:20 -04002934static struct super_operations btrfs_super_ops = {
Chris Mason134e9732007-03-25 13:44:56 -04002935 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002936 .put_super = btrfs_put_super,
2937 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04002938 .write_super = btrfs_write_super,
2939 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04002940 .write_inode = btrfs_write_inode,
Chris Masonb5133862007-04-24 11:52:22 -04002941 .dirty_inode = btrfs_dirty_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002942 .alloc_inode = btrfs_alloc_inode,
2943 .destroy_inode = btrfs_destroy_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002944 .statfs = btrfs_statfs,
Chris Masone20d96d2007-03-22 12:13:20 -04002945};
2946
2947static struct inode_operations btrfs_dir_inode_operations = {
2948 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04002949 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04002950 .unlink = btrfs_unlink,
Chris Mason2b8d99a2007-05-24 08:34:10 -04002951 .link = btrfs_link,
Chris Masonf7922032007-03-25 20:17:36 -04002952 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04002953 .rmdir = btrfs_rmdir,
Chris Masone06afa82007-05-23 15:44:28 -04002954 .rename = btrfs_rename,
Chris Mason2b8d99a2007-05-24 08:34:10 -04002955 .symlink = btrfs_symlink,
Chris Mason3a686372007-05-24 13:35:57 -04002956 .setattr = btrfs_setattr,
Chris Masone20d96d2007-03-22 12:13:20 -04002957};
2958
Chris Masond6e4a422007-04-06 15:37:36 -04002959static struct inode_operations btrfs_dir_ro_inode_operations = {
2960 .lookup = btrfs_lookup,
2961};
2962
Chris Masone20d96d2007-03-22 12:13:20 -04002963static struct file_operations btrfs_dir_file_operations = {
2964 .llseek = generic_file_llseek,
2965 .read = generic_read_dir,
2966 .readdir = btrfs_readdir,
Chris Masonc5739bb2007-04-10 09:27:04 -04002967 .ioctl = btrfs_ioctl,
Chris Mason08607c12007-06-08 15:33:54 -04002968#ifdef CONFIG_COMPAT
2969 .compat_ioctl = btrfs_compat_ioctl,
2970#endif
Chris Masone20d96d2007-03-22 12:13:20 -04002971};
2972
Chris Masondee26a92007-03-26 16:00:06 -04002973static struct address_space_operations btrfs_aops = {
2974 .readpage = btrfs_readpage,
Chris Masondee26a92007-03-26 16:00:06 -04002975 .writepage = btrfs_writepage,
2976 .sync_page = block_sync_page,
2977 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04002978 .commit_write = btrfs_commit_write,
Chris Masonfabb5682007-06-07 22:13:21 -04002979 .bmap = btrfs_bmap,
Chris Masondee26a92007-03-26 16:00:06 -04002980};
2981
Chris Mason2b8d99a2007-05-24 08:34:10 -04002982static struct address_space_operations btrfs_symlink_aops = {
2983 .readpage = btrfs_readpage,
2984 .writepage = btrfs_writepage,
2985};
2986
Chris Masondee26a92007-03-26 16:00:06 -04002987static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04002988 .truncate = btrfs_truncate,
Chris Mason236454df2007-04-19 13:37:44 -04002989 .getattr = btrfs_getattr,
Chris Mason3a686372007-05-24 13:35:57 -04002990 .setattr = btrfs_setattr,
Chris Masondee26a92007-03-26 16:00:06 -04002991};
2992
2993static struct file_operations btrfs_file_operations = {
2994 .llseek = generic_file_llseek,
2995 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04002996 .aio_read = btrfs_file_aio_read,
2997 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04002998 .mmap = generic_file_mmap,
2999 .open = generic_file_open,
Chris Masonc5739bb2007-04-10 09:27:04 -04003000 .ioctl = btrfs_ioctl,
Chris Mason8fd17792007-04-19 21:01:03 -04003001 .fsync = btrfs_sync_file,
Chris Mason08607c12007-06-08 15:33:54 -04003002#ifdef CONFIG_COMPAT
3003 .compat_ioctl = btrfs_compat_ioctl,
3004#endif
Chris Masondee26a92007-03-26 16:00:06 -04003005};
Chris Masone20d96d2007-03-22 12:13:20 -04003006
Chris Mason2b8d99a2007-05-24 08:34:10 -04003007static struct inode_operations btrfs_symlink_inode_operations = {
3008 .readlink = generic_readlink,
3009 .follow_link = page_follow_link_light,
3010 .put_link = page_put_link,
3011};
3012
Chris Mason2e635a22007-03-21 11:12:56 -04003013static int __init init_btrfs_fs(void)
3014{
Chris Mason2c90e5d2007-04-02 10:50:19 -04003015 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04003016 printk("btrfs loaded!\n");
Chris Mason08607c12007-06-08 15:33:54 -04003017 btrfs_init_transaction_sys();
Chris Mason2c90e5d2007-04-02 10:50:19 -04003018 err = init_inodecache();
3019 if (err)
3020 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04003021 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04003022 destroy_inodecache();
3023 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04003024}
3025
3026static void __exit exit_btrfs_fs(void)
3027{
Chris Mason08607c12007-06-08 15:33:54 -04003028 btrfs_exit_transaction_sys();
Chris Mason2c90e5d2007-04-02 10:50:19 -04003029 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04003030 unregister_filesystem(&btrfs_fs_type);
Chris Mason2e635a22007-03-21 11:12:56 -04003031 printk("btrfs unloaded\n");
3032}
3033
3034module_init(init_btrfs_fs)
3035module_exit(exit_btrfs_fs)
3036
3037MODULE_LICENSE("GPL");