blob: d4ee78046b867d67d1c3e23e87516acab9e4f27c [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 Mason2e635a22007-03-21 11:12:56 -040014#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040015#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040016#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040017#include "btrfs_inode.h"
Chris Mason2e635a22007-03-21 11:12:56 -040018
19#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040020
21static struct inode_operations btrfs_dir_inode_operations;
22static struct super_operations btrfs_super_ops;
23static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040024static struct inode_operations btrfs_file_inode_operations;
25static struct address_space_operations btrfs_aops;
26static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040027
Chris Mason2c90e5d2007-04-02 10:50:19 -040028static int check_inode(struct inode *inode)
29{
30 struct btrfs_inode *ei = BTRFS_I(inode);
31 WARN_ON(ei->magic != 0xDEADBEEF);
32 WARN_ON(ei->magic2 != 0xDEADBEAF);
33 return 0;
34}
35
Chris Masone20d96d2007-03-22 12:13:20 -040036static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040037{
Chris Mason5caf2a02007-04-02 11:20:42 -040038 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = btrfs_sb(inode->i_sb);
41 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040042
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
44 BUG_ON(!path);
45 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040046 mutex_lock(&root->fs_info->fs_mutex);
47
Chris Mason2c90e5d2007-04-02 10:50:19 -040048 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040049 ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040050 if (ret) {
Chris Mason5caf2a02007-04-02 11:20:42 -040051 btrfs_release_path(root, path);
Chris Mason7cfcc172007-04-02 14:53:59 -040052 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040053 mutex_unlock(&root->fs_info->fs_mutex);
54 make_bad_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040055 return;
Chris Mason2e635a22007-03-21 11:12:56 -040056 }
Chris Mason2c90e5d2007-04-02 10:50:19 -040057 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040058 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040060 struct btrfs_inode_item);
61
Chris Masone20d96d2007-03-22 12:13:20 -040062 inode->i_mode = btrfs_inode_mode(inode_item);
63 inode->i_nlink = btrfs_inode_nlink(inode_item);
64 inode->i_uid = btrfs_inode_uid(inode_item);
65 inode->i_gid = btrfs_inode_gid(inode_item);
66 inode->i_size = btrfs_inode_size(inode_item);
67 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
68 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
69 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
70 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
71 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
72 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
73 inode->i_blocks = btrfs_inode_nblocks(inode_item);
74 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040075
76 btrfs_release_path(root, path);
77 btrfs_free_path(path);
78 inode_item = NULL;
79
Chris Masonf4b9aa82007-03-27 11:05:53 -040080 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040081 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040082 switch (inode->i_mode & S_IFMT) {
83#if 0
84 default:
85 init_special_inode(inode, inode->i_mode,
86 btrfs_inode_rdev(inode_item));
87 break;
88#endif
89 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040090 inode->i_mapping->a_ops = &btrfs_aops;
91 inode->i_fop = &btrfs_file_operations;
92 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040093 break;
94 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040095 inode->i_op = &btrfs_dir_inode_operations;
96 inode->i_fop = &btrfs_dir_file_operations;
97 break;
98 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -040099 // inode->i_op = &page_symlink_inode_operations;
100 break;
101 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400102 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400103 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400104}
105
Chris Mason5f443fd2007-03-27 13:42:32 -0400106static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct inode *dir,
109 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400110{
Chris Mason5caf2a02007-04-02 11:20:42 -0400111 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400112 const char *name = dentry->d_name.name;
113 int name_len = dentry->d_name.len;
114 int ret;
115 u64 objectid;
116 struct btrfs_dir_item *di;
117
Chris Mason5caf2a02007-04-02 11:20:42 -0400118 path = btrfs_alloc_path();
119 BUG_ON(!path);
120 btrfs_init_path(path);
121 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400122 name, name_len, -1);
123 if (ret < 0)
124 goto err;
125 if (ret > 0) {
126 ret = -ENOENT;
127 goto err;
128 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400130 struct btrfs_dir_item);
131 objectid = btrfs_dir_objectid(di);
132
Chris Mason5caf2a02007-04-02 11:20:42 -0400133 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400134 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400135
136 btrfs_release_path(root, path);
137 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
138 objectid, -1);
139 BUG_ON(ret);
140 ret = btrfs_del_item(trans, root, path);
141 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400142 dentry->d_inode->i_ctime = dir->i_ctime;
143err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400144 btrfs_release_path(root, path);
145 btrfs_free_path(path);
Chris Masond4dbff92007-04-04 14:08:15 -0400146 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400147 inode_dec_link_count(dentry->d_inode);
Chris Mason5f26f772007-04-05 10:38:44 -0400148 dir->i_size -= name_len * 2;
Chris Masond4dbff92007-04-04 14:08:15 -0400149 mark_inode_dirty(dir);
150 }
Chris Mason134e9732007-03-25 13:44:56 -0400151 return ret;
152}
153
Chris Mason5f443fd2007-03-27 13:42:32 -0400154static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
155{
156 struct btrfs_root *root;
157 struct btrfs_trans_handle *trans;
158 int ret;
159
160 root = btrfs_sb(dir->i_sb);
161 mutex_lock(&root->fs_info->fs_mutex);
162 trans = btrfs_start_transaction(root, 1);
163 ret = btrfs_unlink_trans(trans, root, dir, dentry);
164 btrfs_end_transaction(trans, root);
165 mutex_unlock(&root->fs_info->fs_mutex);
166 return ret;
167}
168
169static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
170{
171 struct inode *inode = dentry->d_inode;
172 int err;
173 int ret;
174 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400175 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400176 struct btrfs_key key;
177 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400178 struct btrfs_key found_key;
179 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400180 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400181 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400182
Chris Mason5caf2a02007-04-02 11:20:42 -0400183 path = btrfs_alloc_path();
184 BUG_ON(!path);
185 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400186 mutex_lock(&root->fs_info->fs_mutex);
187 trans = btrfs_start_transaction(root, 1);
188 key.objectid = inode->i_ino;
189 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400190 key.flags = (u32)-1;
191 while(1) {
192 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
193 if (ret < 0) {
194 err = ret;
195 goto out;
196 }
197 BUG_ON(ret == 0);
198 if (path->slots[0] == 0) {
199 err = -ENOENT;
200 goto out;
201 }
202 path->slots[0]--;
203 leaf = btrfs_buffer_leaf(path->nodes[0]);
204 btrfs_disk_key_to_cpu(&found_key,
205 &leaf->items[path->slots[0]].key);
206 found_type = btrfs_key_type(&found_key);
207 if (found_key.objectid != inode->i_ino) {
208 err = -ENOENT;
209 goto out;
210 }
211 if ((found_type != BTRFS_DIR_ITEM_KEY &&
212 found_type != BTRFS_DIR_INDEX_KEY) ||
213 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
214 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
215 err = -ENOTEMPTY;
216 goto out;
217 }
218 ret = btrfs_del_item(trans, root, path);
219 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400220
Chris Mason5f26f772007-04-05 10:38:44 -0400221 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
222 break;
223 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400224 }
Chris Mason5f26f772007-04-05 10:38:44 -0400225 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400226 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400227
228 /* now the directory is empty */
229 err = btrfs_unlink_trans(trans, root, dir, dentry);
230 if (!err) {
231 inode->i_size = 0;
232 }
233out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400234 btrfs_release_path(root, path);
235 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400236 mutex_unlock(&root->fs_info->fs_mutex);
237 ret = btrfs_end_transaction(trans, root);
238 if (ret && !err)
239 err = ret;
240 return err;
241}
242
Chris Mason134e9732007-03-25 13:44:56 -0400243static int btrfs_free_inode(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root,
245 struct inode *inode)
246{
247 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400248 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400249 struct btrfs_inode_map_item *map;
250 struct btrfs_key stat_data_key;
251 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400252
Chris Mason134e9732007-03-25 13:44:56 -0400253 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400254
255 path = btrfs_alloc_path();
256 BUG_ON(!path);
257 btrfs_init_path(path);
258 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400259 if (ret) {
260 if (ret > 0)
261 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400262 goto error;
263 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400265 struct btrfs_inode_map_item);
266 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400267 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400268 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400269 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400270
Chris Mason5caf2a02007-04-02 11:20:42 -0400271 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400272 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400273 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400274 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400275error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400276 btrfs_release_path(root, path);
277 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400278 return ret;
279}
280
Chris Masonf4b9aa82007-03-27 11:05:53 -0400281static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
282 struct btrfs_root *root,
283 struct inode *inode)
284{
285 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400286 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400287 struct btrfs_key key;
288 struct btrfs_disk_key *found_key;
289 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400290 struct btrfs_file_extent_item *fi = NULL;
291 u64 extent_start = 0;
292 u64 extent_num_blocks = 0;
293 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400294
Chris Mason5caf2a02007-04-02 11:20:42 -0400295 path = btrfs_alloc_path();
296 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400297 /* FIXME, add redo link to tree so we don't leak on crash */
298 key.objectid = inode->i_ino;
299 key.offset = (u64)-1;
300 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400301 /*
302 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
303 * or extent data
304 */
Chris Masonf254e522007-03-29 15:15:27 -0400305 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400306 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400307 btrfs_init_path(path);
308 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400309 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 goto error;
311 }
312 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400313 BUG_ON(path->slots[0] == 0);
314 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400315 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400316 leaf = btrfs_buffer_leaf(path->nodes[0]);
317 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400318 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
319 break;
Chris Masonf254e522007-03-29 15:15:27 -0400320 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400321 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400322 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400323 break;
324 if (btrfs_disk_key_offset(found_key) < inode->i_size)
325 break;
Chris Masonf254e522007-03-29 15:15:27 -0400326 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400327 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
328 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400329 struct btrfs_file_extent_item);
330 extent_start = btrfs_file_extent_disk_blocknr(fi);
331 extent_num_blocks =
332 btrfs_file_extent_disk_num_blocks(fi);
333 inode->i_blocks -=
334 btrfs_file_extent_num_blocks(fi) >> 9;
335 found_extent = 1;
336 } else {
337 found_extent = 0;
338 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400339 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400340 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400341 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400342 if (found_extent) {
343 ret = btrfs_free_extent(trans, root, extent_start,
344 extent_num_blocks, 0);
345 BUG_ON(ret);
346 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400347 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400348 ret = 0;
349error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400350 btrfs_release_path(root, path);
351 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400352 return ret;
353}
354
Chris Mason134e9732007-03-25 13:44:56 -0400355static void btrfs_delete_inode(struct inode *inode)
356{
357 struct btrfs_trans_handle *trans;
358 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400359 int ret;
360
Chris Mason134e9732007-03-25 13:44:56 -0400361 truncate_inode_pages(&inode->i_data, 0);
362 if (is_bad_inode(inode)) {
363 goto no_delete;
364 }
365 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400366 mutex_lock(&root->fs_info->fs_mutex);
367 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400368 if (S_ISREG(inode->i_mode)) {
369 ret = btrfs_truncate_in_trans(trans, root, inode);
370 BUG_ON(ret);
371 }
Chris Mason134e9732007-03-25 13:44:56 -0400372 btrfs_free_inode(trans, root, inode);
373 btrfs_end_transaction(trans, root);
374 mutex_unlock(&root->fs_info->fs_mutex);
375 return;
376no_delete:
377 clear_inode(inode);
378}
379
Chris Masone20d96d2007-03-22 12:13:20 -0400380static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
381 ino_t *ino)
382{
383 const char *name = dentry->d_name.name;
384 int namelen = dentry->d_name.len;
385 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400387 struct btrfs_root *root = btrfs_sb(dir->i_sb);
388 int ret;
389
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 path = btrfs_alloc_path();
391 BUG_ON(!path);
392 btrfs_init_path(path);
393 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400394 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400396 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400397 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400398 goto out;
399 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400400 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400401 struct btrfs_dir_item);
402 *ino = btrfs_dir_objectid(di);
403out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_release_path(root, path);
405 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400406 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400407 return ret;
408}
409
410static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
411 struct nameidata *nd)
412{
413 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400414 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400415 ino_t ino;
416 int ret;
417
418 if (dentry->d_name.len > BTRFS_NAME_LEN)
419 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400420 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400421 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400422 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400423 if (ret < 0)
424 return ERR_PTR(ret);
425 inode = NULL;
426 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400427 inode = iget(dir->i_sb, ino);
428 if (!inode)
429 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400430 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400431 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400432 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400433 return d_splice_alias(inode, dentry);
434}
435
436static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
437{
438 struct inode *inode = filp->f_path.dentry->d_inode;
439 struct btrfs_root *root = btrfs_sb(inode->i_sb);
440 struct btrfs_item *item;
441 struct btrfs_dir_item *di;
442 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400443 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400444 int ret;
445 u32 nritems;
446 struct btrfs_leaf *leaf;
447 int slot;
448 int advance;
449 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400450 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400451
Chris Mason22b0ebd2007-03-30 08:47:31 -0400452 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400453 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400454 key.flags = 0;
Chris Masonbae45de2007-04-04 21:22:22 -0400455 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -0400456 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400457 path = btrfs_alloc_path();
458 btrfs_init_path(path);
459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400460 if (ret < 0) {
461 goto err;
462 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400463 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400464 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400466 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400467 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400468 if (advance || slot >= nritems) {
469 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400470 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400471 if (ret)
472 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400473 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400474 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400475 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400476 } else {
477 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400478 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400479 }
480 }
481 advance = 1;
482 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400483 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
484 break;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400485 if (btrfs_disk_key_offset(&item->key) >
486 root->fs_info->highest_inode) {
487printk("stopping at highest inode %Lu\n", root->fs_info->highest_inode);
488 break;
489 }
Chris Masonbae45de2007-04-04 21:22:22 -0400490 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_INDEX_KEY)
Chris Masone20d96d2007-03-22 12:13:20 -0400491 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400492 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
493 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400494 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400495 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400496 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400497 over = filldir(dirent, (const char *)(di + 1),
498 btrfs_dir_name_len(di),
499 btrfs_disk_key_offset(&item->key),
500 btrfs_dir_objectid(di), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400501 if (over)
502 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400503 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400504 filp->f_pos++;
505nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400506 ret = 0;
507err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400508 btrfs_release_path(root, path);
509 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400510 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400511 return ret;
512}
513
514static void btrfs_put_super (struct super_block * sb)
515{
516 struct btrfs_root *root = btrfs_sb(sb);
517 int ret;
518
519 ret = close_ctree(root);
520 if (ret) {
521 printk("close ctree returns %d\n", ret);
522 }
523 sb->s_fs_info = NULL;
524}
Chris Mason2e635a22007-03-21 11:12:56 -0400525
526static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
527{
528 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400529 struct dentry * root_dentry;
530 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400531 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400532
533 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400534 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400535 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400536 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400537
Chris Mason2c90e5d2007-04-02 10:50:19 -0400538 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400539
Chris Masone20d96d2007-03-22 12:13:20 -0400540 if (!root) {
541 printk("btrfs: open_ctree failed\n");
542 return -EIO;
543 }
Chris Mason87cbda52007-03-28 19:44:27 -0400544 sb->s_fs_info = root;
545 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400546 printk("read in super total blocks %Lu root %Lu\n",
547 btrfs_super_total_blocks(disk_super),
548 btrfs_super_root_dir(disk_super));
549
550 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400551 if (!inode)
552 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400553 if (inode->i_state & I_NEW) {
554 btrfs_read_locked_inode(inode);
555 unlock_new_inode(inode);
556 }
Chris Mason2e635a22007-03-21 11:12:56 -0400557
Chris Masone20d96d2007-03-22 12:13:20 -0400558 root_dentry = d_alloc_root(inode);
559 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400560 iput(inode);
561 return -ENOMEM;
562 }
Chris Masone20d96d2007-03-22 12:13:20 -0400563 sb->s_root = root_dentry;
564
Chris Mason2e635a22007-03-21 11:12:56 -0400565 return 0;
566}
567
Chris Masond5719762007-03-23 10:01:08 -0400568static void fill_inode_item(struct btrfs_inode_item *item,
569 struct inode *inode)
570{
571 btrfs_set_inode_uid(item, inode->i_uid);
572 btrfs_set_inode_gid(item, inode->i_gid);
573 btrfs_set_inode_size(item, inode->i_size);
574 btrfs_set_inode_mode(item, inode->i_mode);
575 btrfs_set_inode_nlink(item, inode->i_nlink);
576 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
577 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
578 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
579 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
580 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
581 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
582 btrfs_set_inode_nblocks(item, inode->i_blocks);
583 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400584 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400585}
586
Chris Mason4730a4b2007-03-26 12:00:39 -0400587static int btrfs_update_inode(struct btrfs_trans_handle *trans,
588 struct btrfs_root *root,
589 struct inode *inode)
590{
591 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400592 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400593 int ret;
594
Chris Mason5caf2a02007-04-02 11:20:42 -0400595 path = btrfs_alloc_path();
596 BUG_ON(!path);
597 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400598
Chris Mason5caf2a02007-04-02 11:20:42 -0400599 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400600 if (ret) {
601 if (ret > 0)
602 ret = -ENOENT;
603 goto failed;
604 }
605
Chris Mason5caf2a02007-04-02 11:20:42 -0400606 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
607 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400608 struct btrfs_inode_item);
609
610 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400611 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400612failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400613 btrfs_release_path(root, path);
614 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400615 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400616 return 0;
617}
618
619static int btrfs_write_inode(struct inode *inode, int wait)
620{
621 struct btrfs_root *root = btrfs_sb(inode->i_sb);
622 struct btrfs_trans_handle *trans;
623 int ret;
624
625 mutex_lock(&root->fs_info->fs_mutex);
626 trans = btrfs_start_transaction(root, 1);
627 ret = btrfs_update_inode(trans, root, inode);
628 if (wait)
629 btrfs_commit_transaction(trans, root);
630 else
631 btrfs_end_transaction(trans, root);
632 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400633 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400634 return ret;
635}
636
Chris Masond5719762007-03-23 10:01:08 -0400637static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
638 struct inode *dir, int mode)
639{
640 struct inode *inode;
641 struct btrfs_inode_item inode_item;
642 struct btrfs_root *root = btrfs_sb(dir->i_sb);
643 struct btrfs_key key;
644 int ret;
645 u64 objectid;
646
647 inode = new_inode(dir->i_sb);
648 if (!inode)
649 return ERR_PTR(-ENOMEM);
650
Chris Mason2c90e5d2007-04-02 10:50:19 -0400651 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400652 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
653 BUG_ON(ret);
654
655 inode->i_uid = current->fsuid;
656 inode->i_gid = current->fsgid;
657 inode->i_mode = mode;
658 inode->i_ino = objectid;
659 inode->i_blocks = 0;
660 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
661 fill_inode_item(&inode_item, inode);
662
Chris Masond5719762007-03-23 10:01:08 -0400663 key.objectid = objectid;
664 key.flags = 0;
665 key.offset = 0;
666 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
667 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
668 BUG_ON(ret);
669
670 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
671 BUG_ON(ret);
672
673 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400674 check_inode(inode);
675 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400676 return inode;
677}
678
679static int btrfs_add_link(struct btrfs_trans_handle *trans,
680 struct dentry *dentry, struct inode *inode)
681{
682 int ret;
683 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
684 dentry->d_name.name, dentry->d_name.len,
685 dentry->d_parent->d_inode->i_ino,
686 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400687 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400688 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Mason4730a4b2007-03-26 12:00:39 -0400689 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
690 dentry->d_parent->d_inode);
691 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400692 check_inode(inode);
693 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400694 return ret;
695}
696
697static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
698 struct dentry *dentry, struct inode *inode)
699{
700 int err = btrfs_add_link(trans, dentry, inode);
701 if (!err) {
702 d_instantiate(dentry, inode);
703 return 0;
704 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400705 if (err > 0)
706 err = -EEXIST;
707 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400708 return err;
709}
710
711static int btrfs_create(struct inode *dir, struct dentry *dentry,
712 int mode, struct nameidata *nd)
713{
714 struct btrfs_trans_handle *trans;
715 struct btrfs_root *root = btrfs_sb(dir->i_sb);
716 struct inode *inode;
717 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400718 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400719
Chris Masond561c022007-03-23 19:47:49 -0400720 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400721 trans = btrfs_start_transaction(root, 1);
722 inode = btrfs_new_inode(trans, dir, mode);
723 err = PTR_ERR(inode);
724 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400725 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400726 // FIXME mark the inode dirty
727 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400728 if (err)
729 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400730 else {
731 inode->i_mapping->a_ops = &btrfs_aops;
732 inode->i_fop = &btrfs_file_operations;
733 inode->i_op = &btrfs_file_inode_operations;
734 }
Chris Masond5719762007-03-23 10:01:08 -0400735 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400736out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400737 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400738 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400739 check_inode(inode);
740 check_inode(dir);
741
Chris Mason134e9732007-03-25 13:44:56 -0400742 if (drop_inode) {
743 inode_dec_link_count(inode);
744 iput(inode);
745 }
Chris Masond5719762007-03-23 10:01:08 -0400746 return err;
747}
748
Chris Masonf7922032007-03-25 20:17:36 -0400749static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
750 struct inode *inode, struct inode *dir)
751{
752 struct btrfs_root *root = btrfs_sb(inode->i_sb);
753 int ret;
754 char buf[2];
755 buf[0] = '.';
756 buf[1] = '.';
757
758 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
759 inode->i_ino, 1);
760 if (ret)
761 goto error;
762 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
763 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400764 if (ret)
765 goto error;
Chris Mason5f26f772007-04-05 10:38:44 -0400766 inode->i_size = 6;
Chris Mason4730a4b2007-03-26 12:00:39 -0400767 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400768error:
769 return ret;
770}
771
772static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
773{
774 struct inode *inode;
775 struct btrfs_trans_handle *trans;
776 struct btrfs_root *root = btrfs_sb(dir->i_sb);
777 int err = 0;
778 int drop_on_err = 0;
779
780 mutex_lock(&root->fs_info->fs_mutex);
781 trans = btrfs_start_transaction(root, 1);
782 if (IS_ERR(trans)) {
783 err = PTR_ERR(trans);
784 goto out_unlock;
785 }
786 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
787 if (IS_ERR(inode)) {
788 err = PTR_ERR(inode);
789 goto out_fail;
790 }
791 drop_on_err = 1;
792 inode->i_op = &btrfs_dir_inode_operations;
793 inode->i_fop = &btrfs_dir_file_operations;
794
795 err = btrfs_make_empty_dir(trans, inode, dir);
796 if (err)
797 goto out_fail;
798 err = btrfs_add_link(trans, dentry, inode);
799 if (err)
800 goto out_fail;
801 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400802 drop_on_err = 0;
803
804out_fail:
805 btrfs_end_transaction(trans, root);
806out_unlock:
807 mutex_unlock(&root->fs_info->fs_mutex);
808 if (drop_on_err)
809 iput(inode);
810 return err;
811}
812
Chris Masond5719762007-03-23 10:01:08 -0400813static int btrfs_sync_fs(struct super_block *sb, int wait)
814{
815 struct btrfs_trans_handle *trans;
816 struct btrfs_root *root;
817 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400818 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400819
Chris Masond5719762007-03-23 10:01:08 -0400820 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400821 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400822 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400823 return 0;
824 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400825 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400826 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400827 trans = btrfs_start_transaction(root, 1);
828 ret = btrfs_commit_transaction(trans, root);
829 sb->s_dirt = 0;
830 BUG_ON(ret);
831printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400832 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400833 return 0;
834}
835
Chris Masone8f05c42007-04-04 14:30:09 -0400836#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400837static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
838 struct buffer_head *result, int create)
839{
840 struct btrfs_root *root = btrfs_sb(inode->i_sb);
841 struct btrfs_path *path;
842 struct btrfs_key key;
843 struct btrfs_leaf *leaf;
844 int num_bytes = result->b_size;
845 int item_size;
846 int ret;
847 u64 pos;
848 char *ptr;
849 int copy_size;
850 int err = 0;
851 char *safe_ptr;
852 char *data_ptr;
853
854 path = btrfs_alloc_path();
855 BUG_ON(!path);
856
857 WARN_ON(create);
858 if (create) {
859 return 0;
860 }
861 pos = iblock << inode->i_blkbits;
862 key.objectid = inode->i_ino;
863 key.flags = 0;
864 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
865 ptr = kmap(result->b_page);
866 safe_ptr = ptr;
867 ptr += (pos & (PAGE_CACHE_SIZE -1));
868again:
869 key.offset = pos;
870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
871 if (ret) {
872 if (ret < 0)
873 err = ret;
874 else
875 err = 0;
876 goto out;
877 }
878 leaf = btrfs_buffer_leaf(path->nodes[0]);
879 item_size = btrfs_item_size(leaf->items + path->slots[0]);
880 copy_size = min(num_bytes, item_size);
881 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
882 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
883 memcpy(ptr, data_ptr, copy_size);
884 pos += copy_size;
885 num_bytes -= copy_size;
886 WARN_ON(num_bytes < 0);
887 ptr += copy_size;
888 btrfs_release_path(root, path);
889 if (num_bytes != 0) {
890 if (pos >= i_size_read(inode))
891 memset(ptr, 0, num_bytes);
892 else
893 goto again;
894 }
895 set_buffer_uptodate(result);
896 map_bh(result, inode->i_sb, 0);
897 err = 0;
898out:
899 btrfs_free_path(path);
900 kunmap(result->b_page);
901 return err;
902}
Chris Masone8f05c42007-04-04 14:30:09 -0400903#endif
Chris Masondf24a2b2007-04-04 09:36:31 -0400904
Chris Mason75dfe392007-03-29 11:56:46 -0400905static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400906 struct buffer_head *result, int create)
907{
908 int ret;
909 int err = 0;
910 u64 blocknr;
911 u64 extent_start = 0;
912 u64 extent_end = 0;
913 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400914 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400915 struct btrfs_root *root = btrfs_sb(inode->i_sb);
916 struct btrfs_trans_handle *trans = NULL;
917 struct btrfs_file_extent_item *item;
918 struct btrfs_leaf *leaf;
919 struct btrfs_disk_key *found_key;
920
Chris Mason5caf2a02007-04-02 11:20:42 -0400921 path = btrfs_alloc_path();
922 BUG_ON(!path);
923 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400924 if (create)
925 trans = btrfs_start_transaction(root, 1);
926
Chris Mason5caf2a02007-04-02 11:20:42 -0400927 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400928 inode->i_ino,
929 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400930 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400931 err = ret;
932 goto out;
933 }
934
935 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400936 if (path->slots[0] == 0) {
937 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400938 goto allocate;
939 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400940 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400941 }
942
Chris Mason5caf2a02007-04-02 11:20:42 -0400943 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400944 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400945 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400946 blocknr = btrfs_file_extent_disk_blocknr(item);
947 blocknr += btrfs_file_extent_offset(item);
948
949 /* exact match found, use it */
950 if (ret == 0) {
951 err = 0;
952 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400953 goto out;
954 }
955
956 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400957 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400958 if (btrfs_disk_key_objectid(found_key) != objectid ||
959 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
960 extent_end = 0;
961 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400962 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400963 goto allocate;
964 }
965
Chris Mason5caf2a02007-04-02 11:20:42 -0400966 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400967 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400968 extent_start += btrfs_file_extent_offset(item);
969 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400970 if (iblock >= extent_start && iblock < extent_end) {
971 err = 0;
972 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
973 goto out;
974 }
975allocate:
976 /* ok, create a new extent */
977 if (!create) {
978 err = 0;
979 goto out;
980 }
Chris Mason9773a782007-03-27 11:26:26 -0400981 ret = btrfs_alloc_file_extent(trans, root, objectid,
982 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400983 1, extent_end, &blocknr);
984 if (ret) {
985 err = ret;
986 goto out;
987 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400988 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
989 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400990 map_bh(result, inode->i_sb, blocknr);
991
992out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400993 btrfs_release_path(root, path);
994 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400995 if (trans)
996 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400997 return err;
998}
999
1000static int btrfs_get_block(struct inode *inode, sector_t iblock,
1001 struct buffer_head *result, int create)
1002{
1003 int err;
1004 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1005 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001006 err = btrfs_get_block_lock(inode, iblock, result, create);
1007 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001008 mutex_unlock(&root->fs_info->fs_mutex);
1009 return err;
1010}
1011
1012static int btrfs_prepare_write(struct file *file, struct page *page,
1013 unsigned from, unsigned to)
1014{
Chris Mason6407bf62007-03-27 06:33:00 -04001015 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001016}
Chris Mason75dfe392007-03-29 11:56:46 -04001017static int btrfs_commit_write(struct file *file, struct page *page,
1018 unsigned from, unsigned to)
1019{
Chris Mason75dfe392007-03-29 11:56:46 -04001020 return nobh_commit_write(file, page, from, to);
1021}
Chris Masondee26a92007-03-26 16:00:06 -04001022
Chris Masond561c022007-03-23 19:47:49 -04001023static void btrfs_write_super(struct super_block *sb)
1024{
1025 btrfs_sync_fs(sb, 1);
1026}
1027
Chris Masondee26a92007-03-26 16:00:06 -04001028static int btrfs_readpage(struct file *file, struct page *page)
1029{
1030 return mpage_readpage(page, btrfs_get_block);
1031}
1032
1033static int btrfs_readpages(struct file *file, struct address_space *mapping,
1034 struct list_head *pages, unsigned nr_pages)
1035{
1036 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1037}
1038
1039static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1040{
Chris Mason6407bf62007-03-27 06:33:00 -04001041 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001042}
Chris Masond561c022007-03-23 19:47:49 -04001043
Chris Masonf4b9aa82007-03-27 11:05:53 -04001044static void btrfs_truncate(struct inode *inode)
1045{
1046 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1047 int ret;
1048 struct btrfs_trans_handle *trans;
1049
1050 if (!S_ISREG(inode->i_mode))
1051 return;
1052 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1053 return;
1054
Chris Masone8f05c42007-04-04 14:30:09 -04001055 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001056
1057 /* FIXME, add redo link to tree so we don't leak on crash */
1058 mutex_lock(&root->fs_info->fs_mutex);
1059 trans = btrfs_start_transaction(root, 1);
1060 ret = btrfs_truncate_in_trans(trans, root, inode);
1061 BUG_ON(ret);
1062 ret = btrfs_end_transaction(trans, root);
1063 BUG_ON(ret);
1064 mutex_unlock(&root->fs_info->fs_mutex);
1065 mark_inode_dirty(inode);
1066}
1067
Chris Mason75dfe392007-03-29 11:56:46 -04001068static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1069 struct page **prepared_pages,
1070 const char __user * buf)
1071{
1072 long page_fault = 0;
1073 int i;
1074 int offset = pos & (PAGE_CACHE_SIZE - 1);
1075
1076 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1077 size_t count = min_t(size_t,
1078 PAGE_CACHE_SIZE - offset, write_bytes);
1079 struct page *page = prepared_pages[i];
1080 fault_in_pages_readable(buf, count);
1081
1082 /* Copy data from userspace to the current page */
1083 kmap(page);
1084 page_fault = __copy_from_user(page_address(page) + offset,
1085 buf, count);
1086 /* Flush processor's dcache for this page */
1087 flush_dcache_page(page);
1088 kunmap(page);
1089 buf += count;
1090 write_bytes -= count;
1091
1092 if (page_fault)
1093 break;
1094 }
1095 return page_fault ? -EFAULT : 0;
1096}
1097
1098static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1099{
1100 size_t i;
1101 for (i = 0; i < num_pages; i++) {
1102 if (!pages[i])
1103 break;
1104 unlock_page(pages[i]);
1105 mark_page_accessed(pages[i]);
1106 page_cache_release(pages[i]);
1107 }
1108}
1109static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root,
1111 struct file *file,
1112 struct page **pages,
1113 size_t num_pages,
1114 loff_t pos,
1115 size_t write_bytes)
1116{
1117 int i;
1118 int offset;
1119 int err = 0;
1120 int ret;
1121 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001122 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001123
1124 for (i = 0; i < num_pages; i++) {
1125 offset = pos & (PAGE_CACHE_SIZE -1);
1126 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001127 /* FIXME, one block at a time */
1128
1129 mutex_lock(&root->fs_info->fs_mutex);
1130 trans = btrfs_start_transaction(root, 1);
1131 btrfs_csum_file_block(trans, root, inode->i_ino,
1132 pages[i]->index << PAGE_CACHE_SHIFT,
1133 kmap(pages[i]), PAGE_CACHE_SIZE);
1134 kunmap(pages[i]);
1135 SetPageChecked(pages[i]);
1136 ret = btrfs_end_transaction(trans, root);
1137 BUG_ON(ret);
1138 mutex_unlock(&root->fs_info->fs_mutex);
1139
Chris Mason75dfe392007-03-29 11:56:46 -04001140 ret = nobh_commit_write(file, pages[i], offset,
1141 offset + this_write);
1142 pos += this_write;
1143 if (ret) {
1144 err = ret;
1145 goto failed;
1146 }
1147 WARN_ON(this_write > write_bytes);
1148 write_bytes -= this_write;
1149 }
1150failed:
1151 return err;
1152}
1153
1154static int prepare_pages(struct btrfs_trans_handle *trans,
1155 struct btrfs_root *root,
1156 struct file *file,
1157 struct page **pages,
1158 size_t num_pages,
1159 loff_t pos,
1160 size_t write_bytes)
1161{
1162 int i;
1163 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1164 struct inode *inode = file->f_path.dentry->d_inode;
1165 int offset;
1166 int err = 0;
1167 int ret;
1168 int this_write;
1169 loff_t isize = i_size_read(inode);
1170
1171 memset(pages, 0, num_pages * sizeof(struct page *));
1172
1173 for (i = 0; i < num_pages; i++) {
1174 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1175 if (!pages[i]) {
1176 err = -ENOMEM;
1177 goto failed_release;
1178 }
1179 offset = pos & (PAGE_CACHE_SIZE -1);
1180 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1181 ret = nobh_prepare_write(pages[i], offset,
1182 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001183 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001184 pos += this_write;
1185 if (ret) {
1186 err = ret;
1187 goto failed_truncate;
1188 }
1189 WARN_ON(this_write > write_bytes);
1190 write_bytes -= this_write;
1191 }
1192 return 0;
1193
1194failed_release:
1195 btrfs_drop_pages(pages, num_pages);
1196 return err;
1197
1198failed_truncate:
1199 btrfs_drop_pages(pages, num_pages);
1200 if (pos > isize)
1201 vmtruncate(inode, isize);
1202 return err;
1203}
1204
1205static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1206 size_t count, loff_t *ppos)
1207{
1208 loff_t pos;
1209 size_t num_written = 0;
1210 int err = 0;
1211 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001212 struct inode *inode = file->f_path.dentry->d_inode;
1213 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1214 struct page *pages[1];
1215
1216 if (file->f_flags & O_DIRECT)
1217 return -EINVAL;
1218 pos = *ppos;
1219
1220 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1221 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1222 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1223 if (err)
1224 goto out;
1225 if (count == 0)
1226 goto out;
1227 err = remove_suid(file->f_path.dentry);
1228 if (err)
1229 goto out;
1230 file_update_time(file);
1231 mutex_lock(&inode->i_mutex);
1232 while(count > 0) {
1233 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1234 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1235 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1236 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001237 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001238 pos, write_bytes);
1239 BUG_ON(ret);
1240 ret = btrfs_copy_from_user(pos, num_pages,
1241 write_bytes, pages, buf);
1242 BUG_ON(ret);
1243
Chris Masonf254e522007-03-29 15:15:27 -04001244 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001245 num_pages, pos, write_bytes);
1246 BUG_ON(ret);
1247 btrfs_drop_pages(pages, num_pages);
1248
Chris Mason75dfe392007-03-29 11:56:46 -04001249 buf += write_bytes;
1250 count -= write_bytes;
1251 pos += write_bytes;
1252 num_written += write_bytes;
1253
1254 balance_dirty_pages_ratelimited(inode->i_mapping);
1255 cond_resched();
1256 }
1257 mutex_unlock(&inode->i_mutex);
1258out:
1259 *ppos = pos;
1260 current->backing_dev_info = NULL;
1261 return num_written ? num_written : err;
1262}
1263
Chris Masone8f05c42007-04-04 14:30:09 -04001264#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001265static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1266 struct page *page, loff_t pos,
1267 size_t offset, size_t write_bytes)
1268{
1269 struct btrfs_path *path;
1270 struct btrfs_trans_handle *trans;
1271 struct btrfs_key key;
1272 struct btrfs_leaf *leaf;
1273 struct btrfs_key found_key;
1274 int ret;
1275 size_t copy_size = 0;
1276 char *dst = NULL;
1277 int err = 0;
1278 size_t num_written = 0;
1279
1280 path = btrfs_alloc_path();
1281 BUG_ON(!path);
1282 mutex_lock(&root->fs_info->fs_mutex);
1283 trans = btrfs_start_transaction(root, 1);
1284 key.objectid = inode->i_ino;
1285 key.flags = 0;
1286 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1287
1288again:
1289 key.offset = pos;
1290 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1291 if (ret < 0) {
1292 err = ret;
1293 goto out;
1294 }
1295 if (ret == 0) {
1296 leaf = btrfs_buffer_leaf(path->nodes[0]);
1297 btrfs_disk_key_to_cpu(&found_key,
1298 &leaf->items[path->slots[0]].key);
1299 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1300 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1301 copy_size = min(write_bytes, copy_size);
1302 goto copyit;
1303 } else {
1304 int slot = path->slots[0];
1305 if (slot > 0) {
1306 slot--;
1307 }
1308 // FIXME find max key
1309 leaf = btrfs_buffer_leaf(path->nodes[0]);
1310 btrfs_disk_key_to_cpu(&found_key,
1311 &leaf->items[slot].key);
1312 if (found_key.objectid != inode->i_ino)
1313 goto insert;
1314 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1315 goto insert;
1316 copy_size = btrfs_item_size(leaf->items + slot);
1317 if (found_key.offset + copy_size <= pos)
1318 goto insert;
1319 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1320 dst += pos - found_key.offset;
1321 copy_size = copy_size - (pos - found_key.offset);
1322 BUG_ON(copy_size < 0);
1323 copy_size = min(write_bytes, copy_size);
1324 WARN_ON(copy_size == 0);
1325 goto copyit;
1326 }
1327insert:
1328 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001329 copy_size = min(write_bytes,
1330 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1331 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001332 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1333 BUG_ON(ret);
1334 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1335 path->slots[0], char);
1336copyit:
1337 WARN_ON(copy_size == 0);
1338 WARN_ON(dst + copy_size >
1339 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1340 path->slots[0], char) +
1341 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1342 path->slots[0]));
1343 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1344 page_address(page) + offset, copy_size);
1345 mark_buffer_dirty(path->nodes[0]);
1346 btrfs_release_path(root, path);
1347 pos += copy_size;
1348 offset += copy_size;
1349 num_written += copy_size;
1350 write_bytes -= copy_size;
1351 if (write_bytes)
1352 goto again;
1353out:
1354 btrfs_free_path(path);
1355 ret = btrfs_end_transaction(trans, root);
1356 BUG_ON(ret);
1357 mutex_unlock(&root->fs_info->fs_mutex);
1358 return num_written ? num_written : err;
1359}
1360
1361static ssize_t btrfs_file_inline_write(struct file *file,
1362 const char __user *buf,
1363 size_t count, loff_t *ppos)
1364{
1365 loff_t pos;
1366 size_t num_written = 0;
1367 int err = 0;
1368 int ret = 0;
1369 struct inode *inode = file->f_path.dentry->d_inode;
1370 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1371 unsigned long page_index;
1372
1373 if (file->f_flags & O_DIRECT)
1374 return -EINVAL;
1375 pos = *ppos;
1376
1377 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1378 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1379 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1380 if (err)
1381 goto out;
1382 if (count == 0)
1383 goto out;
1384 err = remove_suid(file->f_path.dentry);
1385 if (err)
1386 goto out;
1387 file_update_time(file);
1388 mutex_lock(&inode->i_mutex);
1389 while(count > 0) {
1390 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1391 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1392 struct page *page;
1393
1394 page_index = pos >> PAGE_CACHE_SHIFT;
1395 page = grab_cache_page(inode->i_mapping, page_index);
1396 if (!PageUptodate(page)) {
1397 ret = mpage_readpage(page, btrfs_get_block);
1398 BUG_ON(ret);
1399 lock_page(page);
1400 }
1401 ret = btrfs_copy_from_user(pos, 1,
1402 write_bytes, &page, buf);
1403 BUG_ON(ret);
1404 write_bytes = inline_one_page(root, inode, page, pos,
1405 offset, write_bytes);
1406 SetPageUptodate(page);
1407 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1408 i_size_write(inode, pos + write_bytes);
1409 mark_inode_dirty(inode);
1410 }
1411 page_cache_release(page);
1412 unlock_page(page);
1413 if (write_bytes < 0)
1414 goto out_unlock;
1415 buf += write_bytes;
1416 count -= write_bytes;
1417 pos += write_bytes;
1418 num_written += write_bytes;
1419
1420 balance_dirty_pages_ratelimited(inode->i_mapping);
1421 cond_resched();
1422 }
1423out_unlock:
1424 mutex_unlock(&inode->i_mutex);
1425out:
1426 *ppos = pos;
1427 current->backing_dev_info = NULL;
1428 return num_written ? num_written : err;
1429}
Chris Masone8f05c42007-04-04 14:30:09 -04001430#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001431
Chris Masonf254e522007-03-29 15:15:27 -04001432static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1433 unsigned long offset, unsigned long size)
1434{
1435 char *kaddr;
1436 unsigned long left, count = desc->count;
1437
1438 if (size > count)
1439 size = count;
1440
1441 if (!PageChecked(page)) {
1442 /* FIXME, do it per block */
1443 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1444 int ret = btrfs_csum_verify_file_block(root,
1445 page->mapping->host->i_ino,
1446 page->index << PAGE_CACHE_SHIFT,
1447 kmap(page), PAGE_CACHE_SIZE);
1448 if (ret) {
1449 printk("failed to verify ino %lu page %lu\n",
1450 page->mapping->host->i_ino,
1451 page->index);
1452 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1453 }
1454 SetPageChecked(page);
1455 kunmap(page);
1456 }
1457 /*
1458 * Faults on the destination of a read are common, so do it before
1459 * taking the kmap.
1460 */
1461 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1462 kaddr = kmap_atomic(page, KM_USER0);
1463 left = __copy_to_user_inatomic(desc->arg.buf,
1464 kaddr + offset, size);
1465 kunmap_atomic(kaddr, KM_USER0);
1466 if (left == 0)
1467 goto success;
1468 }
1469
1470 /* Do it the slow way */
1471 kaddr = kmap(page);
1472 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1473 kunmap(page);
1474
1475 if (left) {
1476 size -= left;
1477 desc->error = -EFAULT;
1478 }
1479success:
1480 desc->count = count - size;
1481 desc->written += size;
1482 desc->arg.buf += size;
1483 return size;
1484}
1485
1486/**
1487 * btrfs_file_aio_read - filesystem read routine
1488 * @iocb: kernel I/O control block
1489 * @iov: io vector request
1490 * @nr_segs: number of segments in the iovec
1491 * @pos: current file position
1492 */
1493static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1494 unsigned long nr_segs, loff_t pos)
1495{
1496 struct file *filp = iocb->ki_filp;
1497 ssize_t retval;
1498 unsigned long seg;
1499 size_t count;
1500 loff_t *ppos = &iocb->ki_pos;
1501
1502 count = 0;
1503 for (seg = 0; seg < nr_segs; seg++) {
1504 const struct iovec *iv = &iov[seg];
1505
1506 /*
1507 * If any segment has a negative length, or the cumulative
1508 * length ever wraps negative then return -EINVAL.
1509 */
1510 count += iv->iov_len;
1511 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1512 return -EINVAL;
1513 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1514 continue;
1515 if (seg == 0)
1516 return -EFAULT;
1517 nr_segs = seg;
1518 count -= iv->iov_len; /* This segment is no good */
1519 break;
1520 }
1521 retval = 0;
1522 if (count) {
1523 for (seg = 0; seg < nr_segs; seg++) {
1524 read_descriptor_t desc;
1525
1526 desc.written = 0;
1527 desc.arg.buf = iov[seg].iov_base;
1528 desc.count = iov[seg].iov_len;
1529 if (desc.count == 0)
1530 continue;
1531 desc.error = 0;
1532 do_generic_file_read(filp, ppos, &desc,
1533 btrfs_read_actor);
1534 retval += desc.written;
1535 if (desc.error) {
1536 retval = retval ?: desc.error;
1537 break;
1538 }
1539 }
1540 }
1541 return retval;
1542}
1543
Chris Mason2c90e5d2007-04-02 10:50:19 -04001544static struct kmem_cache *btrfs_inode_cachep;
1545struct kmem_cache *btrfs_trans_handle_cachep;
1546struct kmem_cache *btrfs_transaction_cachep;
1547struct kmem_cache *btrfs_bit_radix_cachep;
1548struct kmem_cache *btrfs_path_cachep;
1549
1550/*
1551 * Called inside transaction, so use GFP_NOFS
1552 */
1553static struct inode *btrfs_alloc_inode(struct super_block *sb)
1554{
1555 struct btrfs_inode *ei;
1556
1557 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1558 if (!ei)
1559 return NULL;
1560 ei->magic = 0xDEADBEEF;
1561 ei->magic2 = 0xDEADBEAF;
1562 return &ei->vfs_inode;
1563}
1564
1565static void btrfs_destroy_inode(struct inode *inode)
1566{
1567 struct btrfs_inode *ei = BTRFS_I(inode);
1568 WARN_ON(ei->magic != 0xDEADBEEF);
1569 WARN_ON(ei->magic2 != 0xDEADBEAF);
1570 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001571 WARN_ON(inode->i_data.nrpages);
1572
1573 ei->magic = 0;
1574 ei->magic2 = 0;
1575 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1576}
1577
1578static void init_once(void * foo, struct kmem_cache * cachep,
1579 unsigned long flags)
1580{
1581 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1582
1583 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1584 SLAB_CTOR_CONSTRUCTOR) {
1585 inode_init_once(&ei->vfs_inode);
1586 }
1587}
1588
1589static int init_inodecache(void)
1590{
1591 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1592 sizeof(struct btrfs_inode),
1593 0, (SLAB_RECLAIM_ACCOUNT|
1594 SLAB_MEM_SPREAD),
1595 init_once, NULL);
1596 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1597 sizeof(struct btrfs_trans_handle),
1598 0, (SLAB_RECLAIM_ACCOUNT|
1599 SLAB_MEM_SPREAD),
1600 NULL, NULL);
1601 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1602 sizeof(struct btrfs_transaction),
1603 0, (SLAB_RECLAIM_ACCOUNT|
1604 SLAB_MEM_SPREAD),
1605 NULL, NULL);
1606 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1607 sizeof(struct btrfs_transaction),
1608 0, (SLAB_RECLAIM_ACCOUNT|
1609 SLAB_MEM_SPREAD),
1610 NULL, NULL);
1611 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1612 256,
1613 0, (SLAB_RECLAIM_ACCOUNT|
1614 SLAB_MEM_SPREAD |
1615 SLAB_DESTROY_BY_RCU),
1616 NULL, NULL);
1617 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1618 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1619 return -ENOMEM;
1620 return 0;
1621}
1622
1623static void destroy_inodecache(void)
1624{
1625 kmem_cache_destroy(btrfs_inode_cachep);
1626 kmem_cache_destroy(btrfs_trans_handle_cachep);
1627 kmem_cache_destroy(btrfs_transaction_cachep);
1628 kmem_cache_destroy(btrfs_bit_radix_cachep);
1629 kmem_cache_destroy(btrfs_path_cachep);
1630}
1631
Chris Mason2e635a22007-03-21 11:12:56 -04001632static int btrfs_get_sb(struct file_system_type *fs_type,
1633 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1634{
1635 return get_sb_bdev(fs_type, flags, dev_name, data,
1636 btrfs_fill_super, mnt);
1637}
1638
1639static struct file_system_type btrfs_fs_type = {
1640 .owner = THIS_MODULE,
1641 .name = "btrfs",
1642 .get_sb = btrfs_get_sb,
1643 .kill_sb = kill_block_super,
1644 .fs_flags = FS_REQUIRES_DEV,
1645};
1646
Chris Masone20d96d2007-03-22 12:13:20 -04001647static struct super_operations btrfs_super_ops = {
1648 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001649 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001650 .put_super = btrfs_put_super,
1651 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001652 .write_super = btrfs_write_super,
1653 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001654 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001655 .alloc_inode = btrfs_alloc_inode,
1656 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001657};
1658
1659static struct inode_operations btrfs_dir_inode_operations = {
1660 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001661 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001662 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001663 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001664 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001665};
1666
1667static struct file_operations btrfs_dir_file_operations = {
1668 .llseek = generic_file_llseek,
1669 .read = generic_read_dir,
1670 .readdir = btrfs_readdir,
1671};
1672
Chris Masondee26a92007-03-26 16:00:06 -04001673static struct address_space_operations btrfs_aops = {
1674 .readpage = btrfs_readpage,
Chris Masone8f05c42007-04-04 14:30:09 -04001675 .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001676 .writepage = btrfs_writepage,
1677 .sync_page = block_sync_page,
1678 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001679 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001680};
1681
1682static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001683 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001684};
1685
1686static struct file_operations btrfs_file_operations = {
1687 .llseek = generic_file_llseek,
1688 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04001689 .aio_read = btrfs_file_aio_read,
1690 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001691 .mmap = generic_file_mmap,
1692 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001693};
Chris Masone20d96d2007-03-22 12:13:20 -04001694
Chris Mason2e635a22007-03-21 11:12:56 -04001695static int __init init_btrfs_fs(void)
1696{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001697 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001698 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001699 err = init_inodecache();
1700 if (err)
1701 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001702 return register_filesystem(&btrfs_fs_type);
1703}
1704
1705static void __exit exit_btrfs_fs(void)
1706{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001707 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001708 unregister_filesystem(&btrfs_fs_type);
1709 printk("btrfs unloaded\n");
1710}
1711
1712module_init(init_btrfs_fs)
1713module_exit(exit_btrfs_fs)
1714
1715MODULE_LICENSE("GPL");