blob: 68127b8462abca96f1caab00eb1c7cd5c456f3f7 [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);
135 dentry->d_inode->i_ctime = dir->i_ctime;
136err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400137 btrfs_release_path(root, path);
138 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400139 if (ret == 0)
140 inode_dec_link_count(dentry->d_inode);
141 return ret;
142}
143
Chris Mason5f443fd2007-03-27 13:42:32 -0400144static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
145{
146 struct btrfs_root *root;
147 struct btrfs_trans_handle *trans;
148 int ret;
149
150 root = btrfs_sb(dir->i_sb);
151 mutex_lock(&root->fs_info->fs_mutex);
152 trans = btrfs_start_transaction(root, 1);
153 ret = btrfs_unlink_trans(trans, root, dir, dentry);
154 btrfs_end_transaction(trans, root);
155 mutex_unlock(&root->fs_info->fs_mutex);
156 return ret;
157}
158
159static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
160{
161 struct inode *inode = dentry->d_inode;
162 int err;
163 int ret;
164 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400165 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400166 struct btrfs_key key;
167 struct btrfs_trans_handle *trans;
168 struct btrfs_disk_key *found_key;
169 struct btrfs_leaf *leaf;
170
Chris Mason5caf2a02007-04-02 11:20:42 -0400171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400174 mutex_lock(&root->fs_info->fs_mutex);
175 trans = btrfs_start_transaction(root, 1);
176 key.objectid = inode->i_ino;
177 key.offset = (u64)-1;
178 key.flags = 0;
179 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400180 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400181 if (ret < 0) {
182 err = ret;
183 goto out;
184 }
185
186 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400187 BUG_ON(path->slots[0] == 0);
188 path->slots[0]--;
189 leaf = btrfs_buffer_leaf(path->nodes[0]);
190 found_key = &leaf->items[path->slots[0]].key;
Chris Mason5f443fd2007-03-27 13:42:32 -0400191 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
192 err = -ENOENT;
193 goto out;
194 }
195 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
196 btrfs_disk_key_offset(found_key) != 2) {
197 err = -ENOTEMPTY;
198 goto out;
199 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400200 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400201 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400202 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400203 key.offset = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -0400204 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400205 if (ret < 0) {
206 err = ret;
207 goto out;
208 }
209 if (ret > 0) {
210 err = -ENOTEMPTY;
211 goto out;
212 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400213 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400214 if (ret) {
215 err = ret;
216 goto out;
217 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400218 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400219
220 /* now the directory is empty */
221 err = btrfs_unlink_trans(trans, root, dir, dentry);
222 if (!err) {
223 inode->i_size = 0;
224 }
225out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400226 btrfs_release_path(root, path);
227 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400228 mutex_unlock(&root->fs_info->fs_mutex);
229 ret = btrfs_end_transaction(trans, root);
230 if (ret && !err)
231 err = ret;
232 return err;
233}
234
Chris Mason134e9732007-03-25 13:44:56 -0400235static int btrfs_free_inode(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct inode *inode)
238{
239 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400240 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400241 struct btrfs_inode_map_item *map;
242 struct btrfs_key stat_data_key;
243 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400244
Chris Mason134e9732007-03-25 13:44:56 -0400245 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400246
247 path = btrfs_alloc_path();
248 BUG_ON(!path);
249 btrfs_init_path(path);
250 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400251 if (ret) {
252 if (ret > 0)
253 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400254 goto error;
255 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400256 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400257 struct btrfs_inode_map_item);
258 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400259 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400260 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400261 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400262
Chris Mason5caf2a02007-04-02 11:20:42 -0400263 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400264 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400265 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400266 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400267error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400268 btrfs_release_path(root, path);
269 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400270 return ret;
271}
272
Chris Masonf4b9aa82007-03-27 11:05:53 -0400273static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root,
275 struct inode *inode)
276{
277 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400278 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400279 struct btrfs_key key;
280 struct btrfs_disk_key *found_key;
281 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400282 struct btrfs_file_extent_item *fi = NULL;
283 u64 extent_start = 0;
284 u64 extent_num_blocks = 0;
285 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400286
Chris Mason5caf2a02007-04-02 11:20:42 -0400287 path = btrfs_alloc_path();
288 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400289 /* FIXME, add redo link to tree so we don't leak on crash */
290 key.objectid = inode->i_ino;
291 key.offset = (u64)-1;
292 key.flags = 0;
Chris Masonf254e522007-03-29 15:15:27 -0400293 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400294 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400295 btrfs_init_path(path);
296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400297 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400298 goto error;
299 }
300 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400301 BUG_ON(path->slots[0] == 0);
302 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400303 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400304 leaf = btrfs_buffer_leaf(path->nodes[0]);
305 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400306 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
307 break;
Chris Masonf254e522007-03-29 15:15:27 -0400308 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
309 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 break;
311 if (btrfs_disk_key_offset(found_key) < inode->i_size)
312 break;
Chris Masonf254e522007-03-29 15:15:27 -0400313 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400314 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
315 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400316 struct btrfs_file_extent_item);
317 extent_start = btrfs_file_extent_disk_blocknr(fi);
318 extent_num_blocks =
319 btrfs_file_extent_disk_num_blocks(fi);
320 inode->i_blocks -=
321 btrfs_file_extent_num_blocks(fi) >> 9;
322 found_extent = 1;
323 } else {
324 found_extent = 0;
325 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400326 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400327 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400328 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400329 if (found_extent) {
330 ret = btrfs_free_extent(trans, root, extent_start,
331 extent_num_blocks, 0);
332 BUG_ON(ret);
333 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400334 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400335 ret = 0;
336error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400337 btrfs_release_path(root, path);
338 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400339 return ret;
340}
341
Chris Mason134e9732007-03-25 13:44:56 -0400342static void btrfs_delete_inode(struct inode *inode)
343{
344 struct btrfs_trans_handle *trans;
345 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400346 int ret;
347
Chris Mason134e9732007-03-25 13:44:56 -0400348 truncate_inode_pages(&inode->i_data, 0);
349 if (is_bad_inode(inode)) {
350 goto no_delete;
351 }
352 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400353 mutex_lock(&root->fs_info->fs_mutex);
354 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400355 if (S_ISREG(inode->i_mode)) {
356 ret = btrfs_truncate_in_trans(trans, root, inode);
357 BUG_ON(ret);
358 }
Chris Mason134e9732007-03-25 13:44:56 -0400359 btrfs_free_inode(trans, root, inode);
360 btrfs_end_transaction(trans, root);
361 mutex_unlock(&root->fs_info->fs_mutex);
362 return;
363no_delete:
364 clear_inode(inode);
365}
366
Chris Masone20d96d2007-03-22 12:13:20 -0400367static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
368 ino_t *ino)
369{
370 const char *name = dentry->d_name.name;
371 int namelen = dentry->d_name.len;
372 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400374 struct btrfs_root *root = btrfs_sb(dir->i_sb);
375 int ret;
376
Chris Mason5caf2a02007-04-02 11:20:42 -0400377 path = btrfs_alloc_path();
378 BUG_ON(!path);
379 btrfs_init_path(path);
380 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400381 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400382 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400383 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400384 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400385 goto out;
386 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400387 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400388 struct btrfs_dir_item);
389 *ino = btrfs_dir_objectid(di);
390out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400393 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400394 return ret;
395}
396
397static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
398 struct nameidata *nd)
399{
400 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400401 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400402 ino_t ino;
403 int ret;
404
405 if (dentry->d_name.len > BTRFS_NAME_LEN)
406 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400407 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400408 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400409 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400410 if (ret < 0)
411 return ERR_PTR(ret);
412 inode = NULL;
413 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400414 inode = iget(dir->i_sb, ino);
415 if (!inode)
416 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400417 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400418 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400419 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400420 return d_splice_alias(inode, dentry);
421}
422
423static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
424{
425 struct inode *inode = filp->f_path.dentry->d_inode;
426 struct btrfs_root *root = btrfs_sb(inode->i_sb);
427 struct btrfs_item *item;
428 struct btrfs_dir_item *di;
429 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400430 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400431 int ret;
432 u32 nritems;
433 struct btrfs_leaf *leaf;
434 int slot;
435 int advance;
436 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400437 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400438
Chris Mason22b0ebd2007-03-30 08:47:31 -0400439 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400440 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400441 key.flags = 0;
442 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
443 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400444 path = btrfs_alloc_path();
445 btrfs_init_path(path);
446 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400447 if (ret < 0) {
448 goto err;
449 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400450 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400451 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400452 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400453 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400454 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400455 if (advance || slot >= nritems) {
456 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400457 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400458 if (ret)
459 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400460 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400461 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400462 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400463 } else {
464 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400466 }
467 }
468 advance = 1;
469 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400470 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
471 break;
472 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
473 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400474 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
475 continue;
Chris Masondee26a92007-03-26 16:00:06 -0400476
477 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400478 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400479 over = filldir(dirent, (const char *)(di + 1),
480 btrfs_dir_name_len(di),
481 btrfs_disk_key_offset(&item->key),
482 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400483 if (over) {
484 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400485 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400486 }
Chris Masone20d96d2007-03-22 12:13:20 -0400487 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
488 }
Chris Masone20d96d2007-03-22 12:13:20 -0400489 ret = 0;
490err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400491 btrfs_release_path(root, path);
492 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400493 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400494 return ret;
495}
496
497static void btrfs_put_super (struct super_block * sb)
498{
499 struct btrfs_root *root = btrfs_sb(sb);
500 int ret;
501
502 ret = close_ctree(root);
503 if (ret) {
504 printk("close ctree returns %d\n", ret);
505 }
506 sb->s_fs_info = NULL;
507}
Chris Mason2e635a22007-03-21 11:12:56 -0400508
509static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
510{
511 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400512 struct dentry * root_dentry;
513 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400514 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400515
516 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400517 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400518 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400519 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400520
Chris Mason2c90e5d2007-04-02 10:50:19 -0400521 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400522
Chris Masone20d96d2007-03-22 12:13:20 -0400523 if (!root) {
524 printk("btrfs: open_ctree failed\n");
525 return -EIO;
526 }
Chris Mason87cbda52007-03-28 19:44:27 -0400527 sb->s_fs_info = root;
528 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400529 printk("read in super total blocks %Lu root %Lu\n",
530 btrfs_super_total_blocks(disk_super),
531 btrfs_super_root_dir(disk_super));
532
533 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400534 if (!inode)
535 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400536 if (inode->i_state & I_NEW) {
537 btrfs_read_locked_inode(inode);
538 unlock_new_inode(inode);
539 }
Chris Mason2e635a22007-03-21 11:12:56 -0400540
Chris Masone20d96d2007-03-22 12:13:20 -0400541 root_dentry = d_alloc_root(inode);
542 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400543 iput(inode);
544 return -ENOMEM;
545 }
Chris Masone20d96d2007-03-22 12:13:20 -0400546 sb->s_root = root_dentry;
547
Chris Mason2e635a22007-03-21 11:12:56 -0400548 return 0;
549}
550
Chris Masond5719762007-03-23 10:01:08 -0400551static void fill_inode_item(struct btrfs_inode_item *item,
552 struct inode *inode)
553{
554 btrfs_set_inode_uid(item, inode->i_uid);
555 btrfs_set_inode_gid(item, inode->i_gid);
556 btrfs_set_inode_size(item, inode->i_size);
557 btrfs_set_inode_mode(item, inode->i_mode);
558 btrfs_set_inode_nlink(item, inode->i_nlink);
559 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
560 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
561 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
562 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
563 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
564 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
565 btrfs_set_inode_nblocks(item, inode->i_blocks);
566 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400567 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400568}
569
Chris Mason4730a4b2007-03-26 12:00:39 -0400570static int btrfs_update_inode(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root,
572 struct inode *inode)
573{
574 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400575 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400576 int ret;
577
Chris Mason5caf2a02007-04-02 11:20:42 -0400578 path = btrfs_alloc_path();
579 BUG_ON(!path);
580 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400581
Chris Mason5caf2a02007-04-02 11:20:42 -0400582 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400583 if (ret) {
584 if (ret > 0)
585 ret = -ENOENT;
586 goto failed;
587 }
588
Chris Mason5caf2a02007-04-02 11:20:42 -0400589 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
590 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400591 struct btrfs_inode_item);
592
593 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400594 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400595failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400596 btrfs_release_path(root, path);
597 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400598 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400599 return 0;
600}
601
602static int btrfs_write_inode(struct inode *inode, int wait)
603{
604 struct btrfs_root *root = btrfs_sb(inode->i_sb);
605 struct btrfs_trans_handle *trans;
606 int ret;
607
608 mutex_lock(&root->fs_info->fs_mutex);
609 trans = btrfs_start_transaction(root, 1);
610 ret = btrfs_update_inode(trans, root, inode);
611 if (wait)
612 btrfs_commit_transaction(trans, root);
613 else
614 btrfs_end_transaction(trans, root);
615 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400616 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400617 return ret;
618}
619
Chris Masond5719762007-03-23 10:01:08 -0400620static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
621 struct inode *dir, int mode)
622{
623 struct inode *inode;
624 struct btrfs_inode_item inode_item;
625 struct btrfs_root *root = btrfs_sb(dir->i_sb);
626 struct btrfs_key key;
627 int ret;
628 u64 objectid;
629
630 inode = new_inode(dir->i_sb);
631 if (!inode)
632 return ERR_PTR(-ENOMEM);
633
Chris Mason2c90e5d2007-04-02 10:50:19 -0400634 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400635 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
636 BUG_ON(ret);
637
638 inode->i_uid = current->fsuid;
639 inode->i_gid = current->fsgid;
640 inode->i_mode = mode;
641 inode->i_ino = objectid;
642 inode->i_blocks = 0;
643 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
644 fill_inode_item(&inode_item, inode);
645
Chris Masond5719762007-03-23 10:01:08 -0400646 key.objectid = objectid;
647 key.flags = 0;
648 key.offset = 0;
649 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
650 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
651 BUG_ON(ret);
652
653 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
654 BUG_ON(ret);
655
656 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400657 check_inode(inode);
658 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400659 return inode;
660}
661
662static int btrfs_add_link(struct btrfs_trans_handle *trans,
663 struct dentry *dentry, struct inode *inode)
664{
665 int ret;
666 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
667 dentry->d_name.name, dentry->d_name.len,
668 dentry->d_parent->d_inode->i_ino,
669 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400670 if (ret == 0) {
671 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
672 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
673 dentry->d_parent->d_inode);
674 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400675 check_inode(inode);
676 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400677 return ret;
678}
679
680static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
681 struct dentry *dentry, struct inode *inode)
682{
683 int err = btrfs_add_link(trans, dentry, inode);
684 if (!err) {
685 d_instantiate(dentry, inode);
686 return 0;
687 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400688 if (err > 0)
689 err = -EEXIST;
690 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400691 return err;
692}
693
694static int btrfs_create(struct inode *dir, struct dentry *dentry,
695 int mode, struct nameidata *nd)
696{
697 struct btrfs_trans_handle *trans;
698 struct btrfs_root *root = btrfs_sb(dir->i_sb);
699 struct inode *inode;
700 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400701 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400702
Chris Masond561c022007-03-23 19:47:49 -0400703 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400704 trans = btrfs_start_transaction(root, 1);
705 inode = btrfs_new_inode(trans, dir, mode);
706 err = PTR_ERR(inode);
707 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400708 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400709 // FIXME mark the inode dirty
710 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400711 if (err)
712 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400713 else {
714 inode->i_mapping->a_ops = &btrfs_aops;
715 inode->i_fop = &btrfs_file_operations;
716 inode->i_op = &btrfs_file_inode_operations;
717 }
Chris Masond5719762007-03-23 10:01:08 -0400718 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400719out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400720 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400721 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400722 check_inode(inode);
723 check_inode(dir);
724
Chris Mason134e9732007-03-25 13:44:56 -0400725 if (drop_inode) {
726 inode_dec_link_count(inode);
727 iput(inode);
728 }
Chris Masond5719762007-03-23 10:01:08 -0400729 return err;
730}
731
Chris Masonf7922032007-03-25 20:17:36 -0400732static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
733 struct inode *inode, struct inode *dir)
734{
735 struct btrfs_root *root = btrfs_sb(inode->i_sb);
736 int ret;
737 char buf[2];
738 buf[0] = '.';
739 buf[1] = '.';
740
741 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
742 inode->i_ino, 1);
743 if (ret)
744 goto error;
745 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
746 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400747 if (ret)
748 goto error;
749 inode->i_size = 3;
750 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400751error:
752 return ret;
753}
754
755static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
756{
757 struct inode *inode;
758 struct btrfs_trans_handle *trans;
759 struct btrfs_root *root = btrfs_sb(dir->i_sb);
760 int err = 0;
761 int drop_on_err = 0;
762
763 mutex_lock(&root->fs_info->fs_mutex);
764 trans = btrfs_start_transaction(root, 1);
765 if (IS_ERR(trans)) {
766 err = PTR_ERR(trans);
767 goto out_unlock;
768 }
769 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
770 if (IS_ERR(inode)) {
771 err = PTR_ERR(inode);
772 goto out_fail;
773 }
774 drop_on_err = 1;
775 inode->i_op = &btrfs_dir_inode_operations;
776 inode->i_fop = &btrfs_dir_file_operations;
777
778 err = btrfs_make_empty_dir(trans, inode, dir);
779 if (err)
780 goto out_fail;
781 err = btrfs_add_link(trans, dentry, inode);
782 if (err)
783 goto out_fail;
784 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400785 drop_on_err = 0;
786
787out_fail:
788 btrfs_end_transaction(trans, root);
789out_unlock:
790 mutex_unlock(&root->fs_info->fs_mutex);
791 if (drop_on_err)
792 iput(inode);
793 return err;
794}
795
Chris Masond5719762007-03-23 10:01:08 -0400796static int btrfs_sync_fs(struct super_block *sb, int wait)
797{
798 struct btrfs_trans_handle *trans;
799 struct btrfs_root *root;
800 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400801 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400802
Chris Masond5719762007-03-23 10:01:08 -0400803 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400804 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400805 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400806 return 0;
807 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400808 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400809 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400810 trans = btrfs_start_transaction(root, 1);
811 ret = btrfs_commit_transaction(trans, root);
812 sb->s_dirt = 0;
813 BUG_ON(ret);
814printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400815 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400816 return 0;
817}
818
Chris Masondf24a2b2007-04-04 09:36:31 -0400819static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
820 struct buffer_head *result, int create)
821{
822 struct btrfs_root *root = btrfs_sb(inode->i_sb);
823 struct btrfs_path *path;
824 struct btrfs_key key;
825 struct btrfs_leaf *leaf;
826 int num_bytes = result->b_size;
827 int item_size;
828 int ret;
829 u64 pos;
830 char *ptr;
831 int copy_size;
832 int err = 0;
833 char *safe_ptr;
834 char *data_ptr;
835
836 path = btrfs_alloc_path();
837 BUG_ON(!path);
838
839 WARN_ON(create);
840 if (create) {
841 return 0;
842 }
843 pos = iblock << inode->i_blkbits;
844 key.objectid = inode->i_ino;
845 key.flags = 0;
846 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
847 ptr = kmap(result->b_page);
848 safe_ptr = ptr;
849 ptr += (pos & (PAGE_CACHE_SIZE -1));
850again:
851 key.offset = pos;
852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
853 if (ret) {
854 if (ret < 0)
855 err = ret;
856 else
857 err = 0;
858 goto out;
859 }
860 leaf = btrfs_buffer_leaf(path->nodes[0]);
861 item_size = btrfs_item_size(leaf->items + path->slots[0]);
862 copy_size = min(num_bytes, item_size);
863 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
864 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
865 memcpy(ptr, data_ptr, copy_size);
866 pos += copy_size;
867 num_bytes -= copy_size;
868 WARN_ON(num_bytes < 0);
869 ptr += copy_size;
870 btrfs_release_path(root, path);
871 if (num_bytes != 0) {
872 if (pos >= i_size_read(inode))
873 memset(ptr, 0, num_bytes);
874 else
875 goto again;
876 }
877 set_buffer_uptodate(result);
878 map_bh(result, inode->i_sb, 0);
879 err = 0;
880out:
881 btrfs_free_path(path);
882 kunmap(result->b_page);
883 return err;
884}
885
Chris Mason75dfe392007-03-29 11:56:46 -0400886static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400887 struct buffer_head *result, int create)
888{
889 int ret;
890 int err = 0;
891 u64 blocknr;
892 u64 extent_start = 0;
893 u64 extent_end = 0;
894 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400895 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400896 struct btrfs_root *root = btrfs_sb(inode->i_sb);
897 struct btrfs_trans_handle *trans = NULL;
898 struct btrfs_file_extent_item *item;
899 struct btrfs_leaf *leaf;
900 struct btrfs_disk_key *found_key;
901
Chris Mason5caf2a02007-04-02 11:20:42 -0400902 path = btrfs_alloc_path();
903 BUG_ON(!path);
904 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400905 if (create)
906 trans = btrfs_start_transaction(root, 1);
907
908
Chris Mason5caf2a02007-04-02 11:20:42 -0400909 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400910 inode->i_ino,
911 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400912 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400913 err = ret;
914 goto out;
915 }
916
917 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400918 if (path->slots[0] == 0) {
919 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400920 goto allocate;
921 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400922 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400923 }
924
Chris Mason5caf2a02007-04-02 11:20:42 -0400925 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400926 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400927 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400928 blocknr = btrfs_file_extent_disk_blocknr(item);
929 blocknr += btrfs_file_extent_offset(item);
930
931 /* exact match found, use it */
932 if (ret == 0) {
933 err = 0;
934 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400935 goto out;
936 }
937
938 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400939 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400940 if (btrfs_disk_key_objectid(found_key) != objectid ||
941 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
942 extent_end = 0;
943 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400944 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400945 goto allocate;
946 }
947
Chris Mason5caf2a02007-04-02 11:20:42 -0400948 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400949 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400950 extent_start += btrfs_file_extent_offset(item);
951 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400952 if (iblock >= extent_start && iblock < extent_end) {
953 err = 0;
954 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
955 goto out;
956 }
957allocate:
958 /* ok, create a new extent */
959 if (!create) {
960 err = 0;
961 goto out;
962 }
Chris Mason9773a782007-03-27 11:26:26 -0400963 ret = btrfs_alloc_file_extent(trans, root, objectid,
964 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400965 1, extent_end, &blocknr);
966 if (ret) {
967 err = ret;
968 goto out;
969 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400970 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
971 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400972 map_bh(result, inode->i_sb, blocknr);
973
974out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400975 btrfs_release_path(root, path);
976 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400977 if (trans)
978 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400979 return err;
980}
981
982static int btrfs_get_block(struct inode *inode, sector_t iblock,
983 struct buffer_head *result, int create)
984{
985 int err;
986 struct btrfs_root *root = btrfs_sb(inode->i_sb);
987 mutex_lock(&root->fs_info->fs_mutex);
Chris Masondf24a2b2007-04-04 09:36:31 -0400988 // err = btrfs_get_block_lock(inode, iblock, result, create);
989 err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -0400990 mutex_unlock(&root->fs_info->fs_mutex);
991 return err;
992}
993
994static int btrfs_prepare_write(struct file *file, struct page *page,
995 unsigned from, unsigned to)
996{
Chris Mason75dfe392007-03-29 11:56:46 -0400997 WARN_ON(1);
Chris Mason6407bf62007-03-27 06:33:00 -0400998 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -0400999}
Chris Mason75dfe392007-03-29 11:56:46 -04001000static int btrfs_commit_write(struct file *file, struct page *page,
1001 unsigned from, unsigned to)
1002{
1003 WARN_ON(1);
1004 return nobh_commit_write(file, page, from, to);
1005}
Chris Masondee26a92007-03-26 16:00:06 -04001006
Chris Masond561c022007-03-23 19:47:49 -04001007static void btrfs_write_super(struct super_block *sb)
1008{
1009 btrfs_sync_fs(sb, 1);
1010}
1011
Chris Masondee26a92007-03-26 16:00:06 -04001012static int btrfs_readpage(struct file *file, struct page *page)
1013{
1014 return mpage_readpage(page, btrfs_get_block);
1015}
1016
1017static int btrfs_readpages(struct file *file, struct address_space *mapping,
1018 struct list_head *pages, unsigned nr_pages)
1019{
1020 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1021}
1022
1023static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1024{
Chris Mason6407bf62007-03-27 06:33:00 -04001025 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001026}
Chris Masond561c022007-03-23 19:47:49 -04001027
Chris Masonf4b9aa82007-03-27 11:05:53 -04001028static void btrfs_truncate(struct inode *inode)
1029{
1030 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1031 int ret;
1032 struct btrfs_trans_handle *trans;
1033
1034 if (!S_ISREG(inode->i_mode))
1035 return;
1036 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1037 return;
1038
1039 nobh_truncate_page(inode->i_mapping, inode->i_size);
1040
1041 /* FIXME, add redo link to tree so we don't leak on crash */
1042 mutex_lock(&root->fs_info->fs_mutex);
1043 trans = btrfs_start_transaction(root, 1);
1044 ret = btrfs_truncate_in_trans(trans, root, inode);
1045 BUG_ON(ret);
1046 ret = btrfs_end_transaction(trans, root);
1047 BUG_ON(ret);
1048 mutex_unlock(&root->fs_info->fs_mutex);
1049 mark_inode_dirty(inode);
1050}
1051
Chris Mason75dfe392007-03-29 11:56:46 -04001052static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1053 struct page **prepared_pages,
1054 const char __user * buf)
1055{
1056 long page_fault = 0;
1057 int i;
1058 int offset = pos & (PAGE_CACHE_SIZE - 1);
1059
1060 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1061 size_t count = min_t(size_t,
1062 PAGE_CACHE_SIZE - offset, write_bytes);
1063 struct page *page = prepared_pages[i];
1064 fault_in_pages_readable(buf, count);
1065
1066 /* Copy data from userspace to the current page */
1067 kmap(page);
1068 page_fault = __copy_from_user(page_address(page) + offset,
1069 buf, count);
1070 /* Flush processor's dcache for this page */
1071 flush_dcache_page(page);
1072 kunmap(page);
1073 buf += count;
1074 write_bytes -= count;
1075
1076 if (page_fault)
1077 break;
1078 }
1079 return page_fault ? -EFAULT : 0;
1080}
1081
1082static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1083{
1084 size_t i;
1085 for (i = 0; i < num_pages; i++) {
1086 if (!pages[i])
1087 break;
1088 unlock_page(pages[i]);
1089 mark_page_accessed(pages[i]);
1090 page_cache_release(pages[i]);
1091 }
1092}
1093static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1094 struct btrfs_root *root,
1095 struct file *file,
1096 struct page **pages,
1097 size_t num_pages,
1098 loff_t pos,
1099 size_t write_bytes)
1100{
1101 int i;
1102 int offset;
1103 int err = 0;
1104 int ret;
1105 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001106 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001107
1108 for (i = 0; i < num_pages; i++) {
1109 offset = pos & (PAGE_CACHE_SIZE -1);
1110 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001111 /* FIXME, one block at a time */
1112
1113 mutex_lock(&root->fs_info->fs_mutex);
1114 trans = btrfs_start_transaction(root, 1);
1115 btrfs_csum_file_block(trans, root, inode->i_ino,
1116 pages[i]->index << PAGE_CACHE_SHIFT,
1117 kmap(pages[i]), PAGE_CACHE_SIZE);
1118 kunmap(pages[i]);
1119 SetPageChecked(pages[i]);
1120 ret = btrfs_end_transaction(trans, root);
1121 BUG_ON(ret);
1122 mutex_unlock(&root->fs_info->fs_mutex);
1123
Chris Mason75dfe392007-03-29 11:56:46 -04001124 ret = nobh_commit_write(file, pages[i], offset,
1125 offset + this_write);
1126 pos += this_write;
1127 if (ret) {
1128 err = ret;
1129 goto failed;
1130 }
1131 WARN_ON(this_write > write_bytes);
1132 write_bytes -= this_write;
1133 }
1134failed:
1135 return err;
1136}
1137
1138static int prepare_pages(struct btrfs_trans_handle *trans,
1139 struct btrfs_root *root,
1140 struct file *file,
1141 struct page **pages,
1142 size_t num_pages,
1143 loff_t pos,
1144 size_t write_bytes)
1145{
1146 int i;
1147 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1148 struct inode *inode = file->f_path.dentry->d_inode;
1149 int offset;
1150 int err = 0;
1151 int ret;
1152 int this_write;
1153 loff_t isize = i_size_read(inode);
1154
1155 memset(pages, 0, num_pages * sizeof(struct page *));
1156
1157 for (i = 0; i < num_pages; i++) {
1158 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1159 if (!pages[i]) {
1160 err = -ENOMEM;
1161 goto failed_release;
1162 }
1163 offset = pos & (PAGE_CACHE_SIZE -1);
1164 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1165 ret = nobh_prepare_write(pages[i], offset,
1166 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001167 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001168 pos += this_write;
1169 if (ret) {
1170 err = ret;
1171 goto failed_truncate;
1172 }
1173 WARN_ON(this_write > write_bytes);
1174 write_bytes -= this_write;
1175 }
1176 return 0;
1177
1178failed_release:
1179 btrfs_drop_pages(pages, num_pages);
1180 return err;
1181
1182failed_truncate:
1183 btrfs_drop_pages(pages, num_pages);
1184 if (pos > isize)
1185 vmtruncate(inode, isize);
1186 return err;
1187}
1188
1189static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1190 size_t count, loff_t *ppos)
1191{
1192 loff_t pos;
1193 size_t num_written = 0;
1194 int err = 0;
1195 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001196 struct inode *inode = file->f_path.dentry->d_inode;
1197 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1198 struct page *pages[1];
1199
1200 if (file->f_flags & O_DIRECT)
1201 return -EINVAL;
1202 pos = *ppos;
1203
1204 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1205 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1206 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1207 if (err)
1208 goto out;
1209 if (count == 0)
1210 goto out;
1211 err = remove_suid(file->f_path.dentry);
1212 if (err)
1213 goto out;
1214 file_update_time(file);
1215 mutex_lock(&inode->i_mutex);
1216 while(count > 0) {
1217 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1218 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1219 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1220 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001221 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001222 pos, write_bytes);
1223 BUG_ON(ret);
1224 ret = btrfs_copy_from_user(pos, num_pages,
1225 write_bytes, pages, buf);
1226 BUG_ON(ret);
1227
Chris Masonf254e522007-03-29 15:15:27 -04001228 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001229 num_pages, pos, write_bytes);
1230 BUG_ON(ret);
1231 btrfs_drop_pages(pages, num_pages);
1232
Chris Mason75dfe392007-03-29 11:56:46 -04001233 buf += write_bytes;
1234 count -= write_bytes;
1235 pos += write_bytes;
1236 num_written += write_bytes;
1237
1238 balance_dirty_pages_ratelimited(inode->i_mapping);
1239 cond_resched();
1240 }
1241 mutex_unlock(&inode->i_mutex);
1242out:
1243 *ppos = pos;
1244 current->backing_dev_info = NULL;
1245 return num_written ? num_written : err;
1246}
1247
Chris Masondf24a2b2007-04-04 09:36:31 -04001248static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1249 struct page *page, loff_t pos,
1250 size_t offset, size_t write_bytes)
1251{
1252 struct btrfs_path *path;
1253 struct btrfs_trans_handle *trans;
1254 struct btrfs_key key;
1255 struct btrfs_leaf *leaf;
1256 struct btrfs_key found_key;
1257 int ret;
1258 size_t copy_size = 0;
1259 char *dst = NULL;
1260 int err = 0;
1261 size_t num_written = 0;
1262
1263 path = btrfs_alloc_path();
1264 BUG_ON(!path);
1265 mutex_lock(&root->fs_info->fs_mutex);
1266 trans = btrfs_start_transaction(root, 1);
1267 key.objectid = inode->i_ino;
1268 key.flags = 0;
1269 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1270
1271again:
1272 key.offset = pos;
1273 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1274 if (ret < 0) {
1275 err = ret;
1276 goto out;
1277 }
1278 if (ret == 0) {
1279 leaf = btrfs_buffer_leaf(path->nodes[0]);
1280 btrfs_disk_key_to_cpu(&found_key,
1281 &leaf->items[path->slots[0]].key);
1282 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1283 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1284 copy_size = min(write_bytes, copy_size);
1285 goto copyit;
1286 } else {
1287 int slot = path->slots[0];
1288 if (slot > 0) {
1289 slot--;
1290 }
1291 // FIXME find max key
1292 leaf = btrfs_buffer_leaf(path->nodes[0]);
1293 btrfs_disk_key_to_cpu(&found_key,
1294 &leaf->items[slot].key);
1295 if (found_key.objectid != inode->i_ino)
1296 goto insert;
1297 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1298 goto insert;
1299 copy_size = btrfs_item_size(leaf->items + slot);
1300 if (found_key.offset + copy_size <= pos)
1301 goto insert;
1302 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1303 dst += pos - found_key.offset;
1304 copy_size = copy_size - (pos - found_key.offset);
1305 BUG_ON(copy_size < 0);
1306 copy_size = min(write_bytes, copy_size);
1307 WARN_ON(copy_size == 0);
1308 goto copyit;
1309 }
1310insert:
1311 btrfs_release_path(root, path);
1312 copy_size = min(write_bytes, (size_t)512);
1313 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1314 BUG_ON(ret);
1315 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1316 path->slots[0], char);
1317copyit:
1318 WARN_ON(copy_size == 0);
1319 WARN_ON(dst + copy_size >
1320 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1321 path->slots[0], char) +
1322 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1323 path->slots[0]));
1324 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1325 page_address(page) + offset, copy_size);
1326 mark_buffer_dirty(path->nodes[0]);
1327 btrfs_release_path(root, path);
1328 pos += copy_size;
1329 offset += copy_size;
1330 num_written += copy_size;
1331 write_bytes -= copy_size;
1332 if (write_bytes)
1333 goto again;
1334out:
1335 btrfs_free_path(path);
1336 ret = btrfs_end_transaction(trans, root);
1337 BUG_ON(ret);
1338 mutex_unlock(&root->fs_info->fs_mutex);
1339 return num_written ? num_written : err;
1340}
1341
1342static ssize_t btrfs_file_inline_write(struct file *file,
1343 const char __user *buf,
1344 size_t count, loff_t *ppos)
1345{
1346 loff_t pos;
1347 size_t num_written = 0;
1348 int err = 0;
1349 int ret = 0;
1350 struct inode *inode = file->f_path.dentry->d_inode;
1351 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1352 unsigned long page_index;
1353
1354 if (file->f_flags & O_DIRECT)
1355 return -EINVAL;
1356 pos = *ppos;
1357
1358 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1359 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1360 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1361 if (err)
1362 goto out;
1363 if (count == 0)
1364 goto out;
1365 err = remove_suid(file->f_path.dentry);
1366 if (err)
1367 goto out;
1368 file_update_time(file);
1369 mutex_lock(&inode->i_mutex);
1370 while(count > 0) {
1371 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1372 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1373 struct page *page;
1374
1375 page_index = pos >> PAGE_CACHE_SHIFT;
1376 page = grab_cache_page(inode->i_mapping, page_index);
1377 if (!PageUptodate(page)) {
1378 ret = mpage_readpage(page, btrfs_get_block);
1379 BUG_ON(ret);
1380 lock_page(page);
1381 }
1382 ret = btrfs_copy_from_user(pos, 1,
1383 write_bytes, &page, buf);
1384 BUG_ON(ret);
1385 write_bytes = inline_one_page(root, inode, page, pos,
1386 offset, write_bytes);
1387 SetPageUptodate(page);
1388 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1389 i_size_write(inode, pos + write_bytes);
1390 mark_inode_dirty(inode);
1391 }
1392 page_cache_release(page);
1393 unlock_page(page);
1394 if (write_bytes < 0)
1395 goto out_unlock;
1396 buf += write_bytes;
1397 count -= write_bytes;
1398 pos += write_bytes;
1399 num_written += write_bytes;
1400
1401 balance_dirty_pages_ratelimited(inode->i_mapping);
1402 cond_resched();
1403 }
1404out_unlock:
1405 mutex_unlock(&inode->i_mutex);
1406out:
1407 *ppos = pos;
1408 current->backing_dev_info = NULL;
1409 return num_written ? num_written : err;
1410}
1411
Chris Masonf254e522007-03-29 15:15:27 -04001412static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1413 unsigned long offset, unsigned long size)
1414{
1415 char *kaddr;
1416 unsigned long left, count = desc->count;
1417
1418 if (size > count)
1419 size = count;
1420
1421 if (!PageChecked(page)) {
1422 /* FIXME, do it per block */
1423 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1424 int ret = btrfs_csum_verify_file_block(root,
1425 page->mapping->host->i_ino,
1426 page->index << PAGE_CACHE_SHIFT,
1427 kmap(page), PAGE_CACHE_SIZE);
1428 if (ret) {
1429 printk("failed to verify ino %lu page %lu\n",
1430 page->mapping->host->i_ino,
1431 page->index);
1432 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1433 }
1434 SetPageChecked(page);
1435 kunmap(page);
1436 }
1437 /*
1438 * Faults on the destination of a read are common, so do it before
1439 * taking the kmap.
1440 */
1441 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1442 kaddr = kmap_atomic(page, KM_USER0);
1443 left = __copy_to_user_inatomic(desc->arg.buf,
1444 kaddr + offset, size);
1445 kunmap_atomic(kaddr, KM_USER0);
1446 if (left == 0)
1447 goto success;
1448 }
1449
1450 /* Do it the slow way */
1451 kaddr = kmap(page);
1452 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1453 kunmap(page);
1454
1455 if (left) {
1456 size -= left;
1457 desc->error = -EFAULT;
1458 }
1459success:
1460 desc->count = count - size;
1461 desc->written += size;
1462 desc->arg.buf += size;
1463 return size;
1464}
1465
1466/**
1467 * btrfs_file_aio_read - filesystem read routine
1468 * @iocb: kernel I/O control block
1469 * @iov: io vector request
1470 * @nr_segs: number of segments in the iovec
1471 * @pos: current file position
1472 */
1473static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1474 unsigned long nr_segs, loff_t pos)
1475{
1476 struct file *filp = iocb->ki_filp;
1477 ssize_t retval;
1478 unsigned long seg;
1479 size_t count;
1480 loff_t *ppos = &iocb->ki_pos;
1481
1482 count = 0;
1483 for (seg = 0; seg < nr_segs; seg++) {
1484 const struct iovec *iv = &iov[seg];
1485
1486 /*
1487 * If any segment has a negative length, or the cumulative
1488 * length ever wraps negative then return -EINVAL.
1489 */
1490 count += iv->iov_len;
1491 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1492 return -EINVAL;
1493 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1494 continue;
1495 if (seg == 0)
1496 return -EFAULT;
1497 nr_segs = seg;
1498 count -= iv->iov_len; /* This segment is no good */
1499 break;
1500 }
1501 retval = 0;
1502 if (count) {
1503 for (seg = 0; seg < nr_segs; seg++) {
1504 read_descriptor_t desc;
1505
1506 desc.written = 0;
1507 desc.arg.buf = iov[seg].iov_base;
1508 desc.count = iov[seg].iov_len;
1509 if (desc.count == 0)
1510 continue;
1511 desc.error = 0;
1512 do_generic_file_read(filp, ppos, &desc,
1513 btrfs_read_actor);
1514 retval += desc.written;
1515 if (desc.error) {
1516 retval = retval ?: desc.error;
1517 break;
1518 }
1519 }
1520 }
1521 return retval;
1522}
1523
Chris Mason2c90e5d2007-04-02 10:50:19 -04001524static struct kmem_cache *btrfs_inode_cachep;
1525struct kmem_cache *btrfs_trans_handle_cachep;
1526struct kmem_cache *btrfs_transaction_cachep;
1527struct kmem_cache *btrfs_bit_radix_cachep;
1528struct kmem_cache *btrfs_path_cachep;
1529
1530/*
1531 * Called inside transaction, so use GFP_NOFS
1532 */
1533static struct inode *btrfs_alloc_inode(struct super_block *sb)
1534{
1535 struct btrfs_inode *ei;
1536
1537 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1538 if (!ei)
1539 return NULL;
1540 ei->magic = 0xDEADBEEF;
1541 ei->magic2 = 0xDEADBEAF;
1542 return &ei->vfs_inode;
1543}
1544
1545static void btrfs_destroy_inode(struct inode *inode)
1546{
1547 struct btrfs_inode *ei = BTRFS_I(inode);
1548 WARN_ON(ei->magic != 0xDEADBEEF);
1549 WARN_ON(ei->magic2 != 0xDEADBEAF);
1550 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001551 WARN_ON(inode->i_data.nrpages);
1552
1553 ei->magic = 0;
1554 ei->magic2 = 0;
1555 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1556}
1557
1558static void init_once(void * foo, struct kmem_cache * cachep,
1559 unsigned long flags)
1560{
1561 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1562
1563 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1564 SLAB_CTOR_CONSTRUCTOR) {
1565 inode_init_once(&ei->vfs_inode);
1566 }
1567}
1568
1569static int init_inodecache(void)
1570{
1571 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1572 sizeof(struct btrfs_inode),
1573 0, (SLAB_RECLAIM_ACCOUNT|
1574 SLAB_MEM_SPREAD),
1575 init_once, NULL);
1576 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1577 sizeof(struct btrfs_trans_handle),
1578 0, (SLAB_RECLAIM_ACCOUNT|
1579 SLAB_MEM_SPREAD),
1580 NULL, NULL);
1581 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1582 sizeof(struct btrfs_transaction),
1583 0, (SLAB_RECLAIM_ACCOUNT|
1584 SLAB_MEM_SPREAD),
1585 NULL, NULL);
1586 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1587 sizeof(struct btrfs_transaction),
1588 0, (SLAB_RECLAIM_ACCOUNT|
1589 SLAB_MEM_SPREAD),
1590 NULL, NULL);
1591 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1592 256,
1593 0, (SLAB_RECLAIM_ACCOUNT|
1594 SLAB_MEM_SPREAD |
1595 SLAB_DESTROY_BY_RCU),
1596 NULL, NULL);
1597 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1598 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1599 return -ENOMEM;
1600 return 0;
1601}
1602
1603static void destroy_inodecache(void)
1604{
1605 kmem_cache_destroy(btrfs_inode_cachep);
1606 kmem_cache_destroy(btrfs_trans_handle_cachep);
1607 kmem_cache_destroy(btrfs_transaction_cachep);
1608 kmem_cache_destroy(btrfs_bit_radix_cachep);
1609 kmem_cache_destroy(btrfs_path_cachep);
1610}
1611
Chris Mason2e635a22007-03-21 11:12:56 -04001612static int btrfs_get_sb(struct file_system_type *fs_type,
1613 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1614{
1615 return get_sb_bdev(fs_type, flags, dev_name, data,
1616 btrfs_fill_super, mnt);
1617}
1618
1619static struct file_system_type btrfs_fs_type = {
1620 .owner = THIS_MODULE,
1621 .name = "btrfs",
1622 .get_sb = btrfs_get_sb,
1623 .kill_sb = kill_block_super,
1624 .fs_flags = FS_REQUIRES_DEV,
1625};
1626
Chris Masone20d96d2007-03-22 12:13:20 -04001627static struct super_operations btrfs_super_ops = {
1628 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001629 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001630 .put_super = btrfs_put_super,
1631 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001632 .write_super = btrfs_write_super,
1633 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001634 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001635 .alloc_inode = btrfs_alloc_inode,
1636 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001637};
1638
1639static struct inode_operations btrfs_dir_inode_operations = {
1640 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001641 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001642 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001643 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001644 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001645};
1646
1647static struct file_operations btrfs_dir_file_operations = {
1648 .llseek = generic_file_llseek,
1649 .read = generic_read_dir,
1650 .readdir = btrfs_readdir,
1651};
1652
Chris Masondee26a92007-03-26 16:00:06 -04001653static struct address_space_operations btrfs_aops = {
1654 .readpage = btrfs_readpage,
Chris Masondf24a2b2007-04-04 09:36:31 -04001655 // .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001656 .writepage = btrfs_writepage,
1657 .sync_page = block_sync_page,
1658 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001659 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001660};
1661
1662static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001663 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001664};
1665
1666static struct file_operations btrfs_file_operations = {
1667 .llseek = generic_file_llseek,
1668 .read = do_sync_read,
Chris Masondf24a2b2007-04-04 09:36:31 -04001669 .aio_read = generic_file_aio_read,
1670 .write = btrfs_file_inline_write,
Chris Masondee26a92007-03-26 16:00:06 -04001671 .mmap = generic_file_mmap,
1672 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001673};
Chris Masone20d96d2007-03-22 12:13:20 -04001674
Chris Mason2e635a22007-03-21 11:12:56 -04001675static int __init init_btrfs_fs(void)
1676{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001677 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001678 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001679 err = init_inodecache();
1680 if (err)
1681 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001682 return register_filesystem(&btrfs_fs_type);
1683}
1684
1685static void __exit exit_btrfs_fs(void)
1686{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001687 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001688 unregister_filesystem(&btrfs_fs_type);
1689 printk("btrfs unloaded\n");
1690}
1691
1692module_init(init_btrfs_fs)
1693module_exit(exit_btrfs_fs)
1694
1695MODULE_LICENSE("GPL");