blob: 0afb6cece82c7f2ffaf9970d6e83bfa895f763f7 [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 Masond4dbff92007-04-04 14:08:15 -0400139 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400140 inode_dec_link_count(dentry->d_inode);
Chris Masond4dbff92007-04-04 14:08:15 -0400141 dir->i_size -= name_len;
142 mark_inode_dirty(dir);
143 }
Chris Mason134e9732007-03-25 13:44:56 -0400144 return ret;
145}
146
Chris Mason5f443fd2007-03-27 13:42:32 -0400147static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
148{
149 struct btrfs_root *root;
150 struct btrfs_trans_handle *trans;
151 int ret;
152
153 root = btrfs_sb(dir->i_sb);
154 mutex_lock(&root->fs_info->fs_mutex);
155 trans = btrfs_start_transaction(root, 1);
156 ret = btrfs_unlink_trans(trans, root, dir, dentry);
157 btrfs_end_transaction(trans, root);
158 mutex_unlock(&root->fs_info->fs_mutex);
159 return ret;
160}
161
162static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
163{
164 struct inode *inode = dentry->d_inode;
165 int err;
166 int ret;
167 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400168 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400169 struct btrfs_key key;
170 struct btrfs_trans_handle *trans;
171 struct btrfs_disk_key *found_key;
172 struct btrfs_leaf *leaf;
173
Chris Mason5caf2a02007-04-02 11:20:42 -0400174 path = btrfs_alloc_path();
175 BUG_ON(!path);
176 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400177 mutex_lock(&root->fs_info->fs_mutex);
178 trans = btrfs_start_transaction(root, 1);
179 key.objectid = inode->i_ino;
180 key.offset = (u64)-1;
181 key.flags = 0;
182 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400183 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400184 if (ret < 0) {
185 err = ret;
186 goto out;
187 }
188
189 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400190 BUG_ON(path->slots[0] == 0);
191 path->slots[0]--;
192 leaf = btrfs_buffer_leaf(path->nodes[0]);
193 found_key = &leaf->items[path->slots[0]].key;
Chris Mason5f443fd2007-03-27 13:42:32 -0400194 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
195 err = -ENOENT;
196 goto out;
197 }
198 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
199 btrfs_disk_key_offset(found_key) != 2) {
200 err = -ENOTEMPTY;
201 goto out;
202 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400203 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400204 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400205 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400206 key.offset = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -0400207 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Mason5f443fd2007-03-27 13:42:32 -0400208 if (ret < 0) {
209 err = ret;
210 goto out;
211 }
212 if (ret > 0) {
213 err = -ENOTEMPTY;
214 goto out;
215 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400216 ret = btrfs_del_item(trans, root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400217 if (ret) {
218 err = ret;
219 goto out;
220 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400221 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400222
223 /* now the directory is empty */
224 err = btrfs_unlink_trans(trans, root, dir, dentry);
225 if (!err) {
226 inode->i_size = 0;
227 }
228out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400229 btrfs_release_path(root, path);
230 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400231 mutex_unlock(&root->fs_info->fs_mutex);
232 ret = btrfs_end_transaction(trans, root);
233 if (ret && !err)
234 err = ret;
235 return err;
236}
237
Chris Mason134e9732007-03-25 13:44:56 -0400238static int btrfs_free_inode(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root,
240 struct inode *inode)
241{
242 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400243 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400244 struct btrfs_inode_map_item *map;
245 struct btrfs_key stat_data_key;
246 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400247
Chris Mason134e9732007-03-25 13:44:56 -0400248 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400249
250 path = btrfs_alloc_path();
251 BUG_ON(!path);
252 btrfs_init_path(path);
253 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400254 if (ret) {
255 if (ret > 0)
256 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400257 goto error;
258 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400259 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400260 struct btrfs_inode_map_item);
261 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400262 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400263 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400265
Chris Mason5caf2a02007-04-02 11:20:42 -0400266 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400267 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400268 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400269 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400270error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400271 btrfs_release_path(root, path);
272 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400273 return ret;
274}
275
Chris Masonf4b9aa82007-03-27 11:05:53 -0400276static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
277 struct btrfs_root *root,
278 struct inode *inode)
279{
280 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400281 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400282 struct btrfs_key key;
283 struct btrfs_disk_key *found_key;
284 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400285 struct btrfs_file_extent_item *fi = NULL;
286 u64 extent_start = 0;
287 u64 extent_num_blocks = 0;
288 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400289
Chris Mason5caf2a02007-04-02 11:20:42 -0400290 path = btrfs_alloc_path();
291 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400292 /* FIXME, add redo link to tree so we don't leak on crash */
293 key.objectid = inode->i_ino;
294 key.offset = (u64)-1;
295 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400296 /*
297 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
298 * or extent data
299 */
Chris Masonf254e522007-03-29 15:15:27 -0400300 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400301 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400302 btrfs_init_path(path);
303 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400304 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400305 goto error;
306 }
307 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400308 BUG_ON(path->slots[0] == 0);
309 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400311 leaf = btrfs_buffer_leaf(path->nodes[0]);
312 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400313 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
314 break;
Chris Masonf254e522007-03-29 15:15:27 -0400315 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400316 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400317 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400318 break;
319 if (btrfs_disk_key_offset(found_key) < inode->i_size)
320 break;
Chris Masonf254e522007-03-29 15:15:27 -0400321 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400322 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
323 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400324 struct btrfs_file_extent_item);
325 extent_start = btrfs_file_extent_disk_blocknr(fi);
326 extent_num_blocks =
327 btrfs_file_extent_disk_num_blocks(fi);
328 inode->i_blocks -=
329 btrfs_file_extent_num_blocks(fi) >> 9;
330 found_extent = 1;
331 } else {
332 found_extent = 0;
333 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400334 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400335 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400336 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400337 if (found_extent) {
338 ret = btrfs_free_extent(trans, root, extent_start,
339 extent_num_blocks, 0);
340 BUG_ON(ret);
341 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400342 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400343 ret = 0;
344error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400347 return ret;
348}
349
Chris Mason134e9732007-03-25 13:44:56 -0400350static void btrfs_delete_inode(struct inode *inode)
351{
352 struct btrfs_trans_handle *trans;
353 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400354 int ret;
355
Chris Mason134e9732007-03-25 13:44:56 -0400356 truncate_inode_pages(&inode->i_data, 0);
357 if (is_bad_inode(inode)) {
358 goto no_delete;
359 }
360 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400361 mutex_lock(&root->fs_info->fs_mutex);
362 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400363 if (S_ISREG(inode->i_mode)) {
364 ret = btrfs_truncate_in_trans(trans, root, inode);
365 BUG_ON(ret);
366 }
Chris Mason134e9732007-03-25 13:44:56 -0400367 btrfs_free_inode(trans, root, inode);
368 btrfs_end_transaction(trans, root);
369 mutex_unlock(&root->fs_info->fs_mutex);
370 return;
371no_delete:
372 clear_inode(inode);
373}
374
Chris Masone20d96d2007-03-22 12:13:20 -0400375static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
376 ino_t *ino)
377{
378 const char *name = dentry->d_name.name;
379 int namelen = dentry->d_name.len;
380 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400381 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400382 struct btrfs_root *root = btrfs_sb(dir->i_sb);
383 int ret;
384
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 path = btrfs_alloc_path();
386 BUG_ON(!path);
387 btrfs_init_path(path);
388 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400389 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400391 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400392 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400393 goto out;
394 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400396 struct btrfs_dir_item);
397 *ino = btrfs_dir_objectid(di);
398out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400401 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400402 return ret;
403}
404
405static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
406 struct nameidata *nd)
407{
408 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400409 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400410 ino_t ino;
411 int ret;
412
413 if (dentry->d_name.len > BTRFS_NAME_LEN)
414 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400415 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400416 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400417 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400418 if (ret < 0)
419 return ERR_PTR(ret);
420 inode = NULL;
421 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400422 inode = iget(dir->i_sb, ino);
423 if (!inode)
424 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400425 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400426 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400427 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400428 return d_splice_alias(inode, dentry);
429}
430
431static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
432{
433 struct inode *inode = filp->f_path.dentry->d_inode;
434 struct btrfs_root *root = btrfs_sb(inode->i_sb);
435 struct btrfs_item *item;
436 struct btrfs_dir_item *di;
437 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400438 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400439 int ret;
440 u32 nritems;
441 struct btrfs_leaf *leaf;
442 int slot;
443 int advance;
444 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400445 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400446
Chris Mason22b0ebd2007-03-30 08:47:31 -0400447 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400448 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 key.flags = 0;
450 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
451 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400452 path = btrfs_alloc_path();
453 btrfs_init_path(path);
454 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400455 if (ret < 0) {
456 goto err;
457 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400458 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400459 while(1) {
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 Masondee26a92007-03-26 16:00:06 -0400463 if (advance || slot >= nritems) {
464 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400466 if (ret)
467 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400468 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400469 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400470 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400471 } else {
472 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400473 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400474 }
475 }
476 advance = 1;
477 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400478 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
479 break;
480 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
481 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400482 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
483 continue;
Chris Masondee26a92007-03-26 16:00:06 -0400484
485 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400486 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400487 over = filldir(dirent, (const char *)(di + 1),
488 btrfs_dir_name_len(di),
489 btrfs_disk_key_offset(&item->key),
490 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400491 if (over) {
492 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400493 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400494 }
Chris Masone20d96d2007-03-22 12:13:20 -0400495 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
496 }
Chris Masone20d96d2007-03-22 12:13:20 -0400497 ret = 0;
498err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400499 btrfs_release_path(root, path);
500 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400501 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400502 return ret;
503}
504
505static void btrfs_put_super (struct super_block * sb)
506{
507 struct btrfs_root *root = btrfs_sb(sb);
508 int ret;
509
510 ret = close_ctree(root);
511 if (ret) {
512 printk("close ctree returns %d\n", ret);
513 }
514 sb->s_fs_info = NULL;
515}
Chris Mason2e635a22007-03-21 11:12:56 -0400516
517static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
518{
519 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400520 struct dentry * root_dentry;
521 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400522 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400523
524 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400525 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400526 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400527 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400528
Chris Mason2c90e5d2007-04-02 10:50:19 -0400529 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400530
Chris Masone20d96d2007-03-22 12:13:20 -0400531 if (!root) {
532 printk("btrfs: open_ctree failed\n");
533 return -EIO;
534 }
Chris Mason87cbda52007-03-28 19:44:27 -0400535 sb->s_fs_info = root;
536 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400537 printk("read in super total blocks %Lu root %Lu\n",
538 btrfs_super_total_blocks(disk_super),
539 btrfs_super_root_dir(disk_super));
540
541 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400542 if (!inode)
543 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400544 if (inode->i_state & I_NEW) {
545 btrfs_read_locked_inode(inode);
546 unlock_new_inode(inode);
547 }
Chris Mason2e635a22007-03-21 11:12:56 -0400548
Chris Masone20d96d2007-03-22 12:13:20 -0400549 root_dentry = d_alloc_root(inode);
550 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400551 iput(inode);
552 return -ENOMEM;
553 }
Chris Masone20d96d2007-03-22 12:13:20 -0400554 sb->s_root = root_dentry;
555
Chris Mason2e635a22007-03-21 11:12:56 -0400556 return 0;
557}
558
Chris Masond5719762007-03-23 10:01:08 -0400559static void fill_inode_item(struct btrfs_inode_item *item,
560 struct inode *inode)
561{
562 btrfs_set_inode_uid(item, inode->i_uid);
563 btrfs_set_inode_gid(item, inode->i_gid);
564 btrfs_set_inode_size(item, inode->i_size);
565 btrfs_set_inode_mode(item, inode->i_mode);
566 btrfs_set_inode_nlink(item, inode->i_nlink);
567 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
568 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
569 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
570 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
571 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
572 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
573 btrfs_set_inode_nblocks(item, inode->i_blocks);
574 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400575 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400576}
577
Chris Mason4730a4b2007-03-26 12:00:39 -0400578static int btrfs_update_inode(struct btrfs_trans_handle *trans,
579 struct btrfs_root *root,
580 struct inode *inode)
581{
582 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400583 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400584 int ret;
585
Chris Mason5caf2a02007-04-02 11:20:42 -0400586 path = btrfs_alloc_path();
587 BUG_ON(!path);
588 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400589
Chris Mason5caf2a02007-04-02 11:20:42 -0400590 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400591 if (ret) {
592 if (ret > 0)
593 ret = -ENOENT;
594 goto failed;
595 }
596
Chris Mason5caf2a02007-04-02 11:20:42 -0400597 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
598 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400599 struct btrfs_inode_item);
600
601 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400602 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400603failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400604 btrfs_release_path(root, path);
605 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400606 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400607 return 0;
608}
609
610static int btrfs_write_inode(struct inode *inode, int wait)
611{
612 struct btrfs_root *root = btrfs_sb(inode->i_sb);
613 struct btrfs_trans_handle *trans;
614 int ret;
615
616 mutex_lock(&root->fs_info->fs_mutex);
617 trans = btrfs_start_transaction(root, 1);
618 ret = btrfs_update_inode(trans, root, inode);
619 if (wait)
620 btrfs_commit_transaction(trans, root);
621 else
622 btrfs_end_transaction(trans, root);
623 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400624 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400625 return ret;
626}
627
Chris Masond5719762007-03-23 10:01:08 -0400628static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
629 struct inode *dir, int mode)
630{
631 struct inode *inode;
632 struct btrfs_inode_item inode_item;
633 struct btrfs_root *root = btrfs_sb(dir->i_sb);
634 struct btrfs_key key;
635 int ret;
636 u64 objectid;
637
638 inode = new_inode(dir->i_sb);
639 if (!inode)
640 return ERR_PTR(-ENOMEM);
641
Chris Mason2c90e5d2007-04-02 10:50:19 -0400642 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400643 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
644 BUG_ON(ret);
645
646 inode->i_uid = current->fsuid;
647 inode->i_gid = current->fsgid;
648 inode->i_mode = mode;
649 inode->i_ino = objectid;
650 inode->i_blocks = 0;
651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
652 fill_inode_item(&inode_item, inode);
653
Chris Masond5719762007-03-23 10:01:08 -0400654 key.objectid = objectid;
655 key.flags = 0;
656 key.offset = 0;
657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
658 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
659 BUG_ON(ret);
660
661 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
662 BUG_ON(ret);
663
664 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400665 check_inode(inode);
666 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400667 return inode;
668}
669
670static int btrfs_add_link(struct btrfs_trans_handle *trans,
671 struct dentry *dentry, struct inode *inode)
672{
673 int ret;
674 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
675 dentry->d_name.name, dentry->d_name.len,
676 dentry->d_parent->d_inode->i_ino,
677 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400678 if (ret == 0) {
679 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
680 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
681 dentry->d_parent->d_inode);
682 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400683 check_inode(inode);
684 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400685 return ret;
686}
687
688static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
689 struct dentry *dentry, struct inode *inode)
690{
691 int err = btrfs_add_link(trans, dentry, inode);
692 if (!err) {
693 d_instantiate(dentry, inode);
694 return 0;
695 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400696 if (err > 0)
697 err = -EEXIST;
698 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400699 return err;
700}
701
702static int btrfs_create(struct inode *dir, struct dentry *dentry,
703 int mode, struct nameidata *nd)
704{
705 struct btrfs_trans_handle *trans;
706 struct btrfs_root *root = btrfs_sb(dir->i_sb);
707 struct inode *inode;
708 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400709 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400710
Chris Masond561c022007-03-23 19:47:49 -0400711 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400712 trans = btrfs_start_transaction(root, 1);
713 inode = btrfs_new_inode(trans, dir, mode);
714 err = PTR_ERR(inode);
715 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400716 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400717 // FIXME mark the inode dirty
718 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400719 if (err)
720 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400721 else {
722 inode->i_mapping->a_ops = &btrfs_aops;
723 inode->i_fop = &btrfs_file_operations;
724 inode->i_op = &btrfs_file_inode_operations;
725 }
Chris Masond5719762007-03-23 10:01:08 -0400726 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400727out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400728 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400729 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400730 check_inode(inode);
731 check_inode(dir);
732
Chris Mason134e9732007-03-25 13:44:56 -0400733 if (drop_inode) {
734 inode_dec_link_count(inode);
735 iput(inode);
736 }
Chris Masond5719762007-03-23 10:01:08 -0400737 return err;
738}
739
Chris Masonf7922032007-03-25 20:17:36 -0400740static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
741 struct inode *inode, struct inode *dir)
742{
743 struct btrfs_root *root = btrfs_sb(inode->i_sb);
744 int ret;
745 char buf[2];
746 buf[0] = '.';
747 buf[1] = '.';
748
749 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
750 inode->i_ino, 1);
751 if (ret)
752 goto error;
753 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
754 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400755 if (ret)
756 goto error;
757 inode->i_size = 3;
758 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400759error:
760 return ret;
761}
762
763static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
764{
765 struct inode *inode;
766 struct btrfs_trans_handle *trans;
767 struct btrfs_root *root = btrfs_sb(dir->i_sb);
768 int err = 0;
769 int drop_on_err = 0;
770
771 mutex_lock(&root->fs_info->fs_mutex);
772 trans = btrfs_start_transaction(root, 1);
773 if (IS_ERR(trans)) {
774 err = PTR_ERR(trans);
775 goto out_unlock;
776 }
777 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
778 if (IS_ERR(inode)) {
779 err = PTR_ERR(inode);
780 goto out_fail;
781 }
782 drop_on_err = 1;
783 inode->i_op = &btrfs_dir_inode_operations;
784 inode->i_fop = &btrfs_dir_file_operations;
785
786 err = btrfs_make_empty_dir(trans, inode, dir);
787 if (err)
788 goto out_fail;
789 err = btrfs_add_link(trans, dentry, inode);
790 if (err)
791 goto out_fail;
792 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400793 drop_on_err = 0;
794
795out_fail:
796 btrfs_end_transaction(trans, root);
797out_unlock:
798 mutex_unlock(&root->fs_info->fs_mutex);
799 if (drop_on_err)
800 iput(inode);
801 return err;
802}
803
Chris Masond5719762007-03-23 10:01:08 -0400804static int btrfs_sync_fs(struct super_block *sb, int wait)
805{
806 struct btrfs_trans_handle *trans;
807 struct btrfs_root *root;
808 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400809 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400810
Chris Masond5719762007-03-23 10:01:08 -0400811 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400812 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400813 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400814 return 0;
815 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400816 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400817 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400818 trans = btrfs_start_transaction(root, 1);
819 ret = btrfs_commit_transaction(trans, root);
820 sb->s_dirt = 0;
821 BUG_ON(ret);
822printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400823 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400824 return 0;
825}
826
Chris Masondf24a2b2007-04-04 09:36:31 -0400827static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
828 struct buffer_head *result, int create)
829{
830 struct btrfs_root *root = btrfs_sb(inode->i_sb);
831 struct btrfs_path *path;
832 struct btrfs_key key;
833 struct btrfs_leaf *leaf;
834 int num_bytes = result->b_size;
835 int item_size;
836 int ret;
837 u64 pos;
838 char *ptr;
839 int copy_size;
840 int err = 0;
841 char *safe_ptr;
842 char *data_ptr;
843
844 path = btrfs_alloc_path();
845 BUG_ON(!path);
846
847 WARN_ON(create);
848 if (create) {
849 return 0;
850 }
851 pos = iblock << inode->i_blkbits;
852 key.objectid = inode->i_ino;
853 key.flags = 0;
854 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
855 ptr = kmap(result->b_page);
856 safe_ptr = ptr;
857 ptr += (pos & (PAGE_CACHE_SIZE -1));
858again:
859 key.offset = pos;
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
861 if (ret) {
862 if (ret < 0)
863 err = ret;
864 else
865 err = 0;
866 goto out;
867 }
868 leaf = btrfs_buffer_leaf(path->nodes[0]);
869 item_size = btrfs_item_size(leaf->items + path->slots[0]);
870 copy_size = min(num_bytes, item_size);
871 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
872 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
873 memcpy(ptr, data_ptr, copy_size);
874 pos += copy_size;
875 num_bytes -= copy_size;
876 WARN_ON(num_bytes < 0);
877 ptr += copy_size;
878 btrfs_release_path(root, path);
879 if (num_bytes != 0) {
880 if (pos >= i_size_read(inode))
881 memset(ptr, 0, num_bytes);
882 else
883 goto again;
884 }
885 set_buffer_uptodate(result);
886 map_bh(result, inode->i_sb, 0);
887 err = 0;
888out:
889 btrfs_free_path(path);
890 kunmap(result->b_page);
891 return err;
892}
893
Chris Mason75dfe392007-03-29 11:56:46 -0400894static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400895 struct buffer_head *result, int create)
896{
897 int ret;
898 int err = 0;
899 u64 blocknr;
900 u64 extent_start = 0;
901 u64 extent_end = 0;
902 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400903 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400904 struct btrfs_root *root = btrfs_sb(inode->i_sb);
905 struct btrfs_trans_handle *trans = NULL;
906 struct btrfs_file_extent_item *item;
907 struct btrfs_leaf *leaf;
908 struct btrfs_disk_key *found_key;
909
Chris Mason5caf2a02007-04-02 11:20:42 -0400910 path = btrfs_alloc_path();
911 BUG_ON(!path);
912 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400913 if (create)
914 trans = btrfs_start_transaction(root, 1);
915
916
Chris Mason5caf2a02007-04-02 11:20:42 -0400917 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400918 inode->i_ino,
919 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400920 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400921 err = ret;
922 goto out;
923 }
924
925 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400926 if (path->slots[0] == 0) {
927 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400928 goto allocate;
929 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400930 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400931 }
932
Chris Mason5caf2a02007-04-02 11:20:42 -0400933 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400934 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400935 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400936 blocknr = btrfs_file_extent_disk_blocknr(item);
937 blocknr += btrfs_file_extent_offset(item);
938
939 /* exact match found, use it */
940 if (ret == 0) {
941 err = 0;
942 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400943 goto out;
944 }
945
946 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400947 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400948 if (btrfs_disk_key_objectid(found_key) != objectid ||
949 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
950 extent_end = 0;
951 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400952 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400953 goto allocate;
954 }
955
Chris Mason5caf2a02007-04-02 11:20:42 -0400956 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400957 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400958 extent_start += btrfs_file_extent_offset(item);
959 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400960 if (iblock >= extent_start && iblock < extent_end) {
961 err = 0;
962 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
963 goto out;
964 }
965allocate:
966 /* ok, create a new extent */
967 if (!create) {
968 err = 0;
969 goto out;
970 }
Chris Mason9773a782007-03-27 11:26:26 -0400971 ret = btrfs_alloc_file_extent(trans, root, objectid,
972 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400973 1, extent_end, &blocknr);
974 if (ret) {
975 err = ret;
976 goto out;
977 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400978 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
979 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400980 map_bh(result, inode->i_sb, blocknr);
981
982out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400983 btrfs_release_path(root, path);
984 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400985 if (trans)
986 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400987 return err;
988}
989
990static int btrfs_get_block(struct inode *inode, sector_t iblock,
991 struct buffer_head *result, int create)
992{
993 int err;
994 struct btrfs_root *root = btrfs_sb(inode->i_sb);
995 mutex_lock(&root->fs_info->fs_mutex);
Chris Masondf24a2b2007-04-04 09:36:31 -0400996 // err = btrfs_get_block_lock(inode, iblock, result, create);
997 err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -0400998 mutex_unlock(&root->fs_info->fs_mutex);
999 return err;
1000}
1001
1002static int btrfs_prepare_write(struct file *file, struct page *page,
1003 unsigned from, unsigned to)
1004{
Chris Mason75dfe392007-03-29 11:56:46 -04001005 WARN_ON(1);
Chris Mason6407bf62007-03-27 06:33:00 -04001006 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001007}
Chris Mason75dfe392007-03-29 11:56:46 -04001008static int btrfs_commit_write(struct file *file, struct page *page,
1009 unsigned from, unsigned to)
1010{
1011 WARN_ON(1);
1012 return nobh_commit_write(file, page, from, to);
1013}
Chris Masondee26a92007-03-26 16:00:06 -04001014
Chris Masond561c022007-03-23 19:47:49 -04001015static void btrfs_write_super(struct super_block *sb)
1016{
1017 btrfs_sync_fs(sb, 1);
1018}
1019
Chris Masondee26a92007-03-26 16:00:06 -04001020static int btrfs_readpage(struct file *file, struct page *page)
1021{
1022 return mpage_readpage(page, btrfs_get_block);
1023}
1024
1025static int btrfs_readpages(struct file *file, struct address_space *mapping,
1026 struct list_head *pages, unsigned nr_pages)
1027{
1028 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1029}
1030
1031static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1032{
Chris Mason6407bf62007-03-27 06:33:00 -04001033 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001034}
Chris Masond561c022007-03-23 19:47:49 -04001035
Chris Masonf4b9aa82007-03-27 11:05:53 -04001036static void btrfs_truncate(struct inode *inode)
1037{
1038 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1039 int ret;
1040 struct btrfs_trans_handle *trans;
1041
1042 if (!S_ISREG(inode->i_mode))
1043 return;
1044 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1045 return;
1046
Chris Masond4dbff92007-04-04 14:08:15 -04001047 // nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001048
1049 /* FIXME, add redo link to tree so we don't leak on crash */
1050 mutex_lock(&root->fs_info->fs_mutex);
1051 trans = btrfs_start_transaction(root, 1);
1052 ret = btrfs_truncate_in_trans(trans, root, inode);
1053 BUG_ON(ret);
1054 ret = btrfs_end_transaction(trans, root);
1055 BUG_ON(ret);
1056 mutex_unlock(&root->fs_info->fs_mutex);
1057 mark_inode_dirty(inode);
1058}
1059
Chris Mason75dfe392007-03-29 11:56:46 -04001060static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1061 struct page **prepared_pages,
1062 const char __user * buf)
1063{
1064 long page_fault = 0;
1065 int i;
1066 int offset = pos & (PAGE_CACHE_SIZE - 1);
1067
1068 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1069 size_t count = min_t(size_t,
1070 PAGE_CACHE_SIZE - offset, write_bytes);
1071 struct page *page = prepared_pages[i];
1072 fault_in_pages_readable(buf, count);
1073
1074 /* Copy data from userspace to the current page */
1075 kmap(page);
1076 page_fault = __copy_from_user(page_address(page) + offset,
1077 buf, count);
1078 /* Flush processor's dcache for this page */
1079 flush_dcache_page(page);
1080 kunmap(page);
1081 buf += count;
1082 write_bytes -= count;
1083
1084 if (page_fault)
1085 break;
1086 }
1087 return page_fault ? -EFAULT : 0;
1088}
1089
1090static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1091{
1092 size_t i;
1093 for (i = 0; i < num_pages; i++) {
1094 if (!pages[i])
1095 break;
1096 unlock_page(pages[i]);
1097 mark_page_accessed(pages[i]);
1098 page_cache_release(pages[i]);
1099 }
1100}
1101static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct file *file,
1104 struct page **pages,
1105 size_t num_pages,
1106 loff_t pos,
1107 size_t write_bytes)
1108{
1109 int i;
1110 int offset;
1111 int err = 0;
1112 int ret;
1113 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001114 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001115
1116 for (i = 0; i < num_pages; i++) {
1117 offset = pos & (PAGE_CACHE_SIZE -1);
1118 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001119 /* FIXME, one block at a time */
1120
1121 mutex_lock(&root->fs_info->fs_mutex);
1122 trans = btrfs_start_transaction(root, 1);
1123 btrfs_csum_file_block(trans, root, inode->i_ino,
1124 pages[i]->index << PAGE_CACHE_SHIFT,
1125 kmap(pages[i]), PAGE_CACHE_SIZE);
1126 kunmap(pages[i]);
1127 SetPageChecked(pages[i]);
1128 ret = btrfs_end_transaction(trans, root);
1129 BUG_ON(ret);
1130 mutex_unlock(&root->fs_info->fs_mutex);
1131
Chris Mason75dfe392007-03-29 11:56:46 -04001132 ret = nobh_commit_write(file, pages[i], offset,
1133 offset + this_write);
1134 pos += this_write;
1135 if (ret) {
1136 err = ret;
1137 goto failed;
1138 }
1139 WARN_ON(this_write > write_bytes);
1140 write_bytes -= this_write;
1141 }
1142failed:
1143 return err;
1144}
1145
1146static int prepare_pages(struct btrfs_trans_handle *trans,
1147 struct btrfs_root *root,
1148 struct file *file,
1149 struct page **pages,
1150 size_t num_pages,
1151 loff_t pos,
1152 size_t write_bytes)
1153{
1154 int i;
1155 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1156 struct inode *inode = file->f_path.dentry->d_inode;
1157 int offset;
1158 int err = 0;
1159 int ret;
1160 int this_write;
1161 loff_t isize = i_size_read(inode);
1162
1163 memset(pages, 0, num_pages * sizeof(struct page *));
1164
1165 for (i = 0; i < num_pages; i++) {
1166 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1167 if (!pages[i]) {
1168 err = -ENOMEM;
1169 goto failed_release;
1170 }
1171 offset = pos & (PAGE_CACHE_SIZE -1);
1172 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1173 ret = nobh_prepare_write(pages[i], offset,
1174 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001175 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001176 pos += this_write;
1177 if (ret) {
1178 err = ret;
1179 goto failed_truncate;
1180 }
1181 WARN_ON(this_write > write_bytes);
1182 write_bytes -= this_write;
1183 }
1184 return 0;
1185
1186failed_release:
1187 btrfs_drop_pages(pages, num_pages);
1188 return err;
1189
1190failed_truncate:
1191 btrfs_drop_pages(pages, num_pages);
1192 if (pos > isize)
1193 vmtruncate(inode, isize);
1194 return err;
1195}
1196
1197static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *ppos)
1199{
1200 loff_t pos;
1201 size_t num_written = 0;
1202 int err = 0;
1203 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001204 struct inode *inode = file->f_path.dentry->d_inode;
1205 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1206 struct page *pages[1];
1207
1208 if (file->f_flags & O_DIRECT)
1209 return -EINVAL;
1210 pos = *ppos;
1211
1212 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1213 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1214 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1215 if (err)
1216 goto out;
1217 if (count == 0)
1218 goto out;
1219 err = remove_suid(file->f_path.dentry);
1220 if (err)
1221 goto out;
1222 file_update_time(file);
1223 mutex_lock(&inode->i_mutex);
1224 while(count > 0) {
1225 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1226 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1227 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1228 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001229 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001230 pos, write_bytes);
1231 BUG_ON(ret);
1232 ret = btrfs_copy_from_user(pos, num_pages,
1233 write_bytes, pages, buf);
1234 BUG_ON(ret);
1235
Chris Masonf254e522007-03-29 15:15:27 -04001236 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001237 num_pages, pos, write_bytes);
1238 BUG_ON(ret);
1239 btrfs_drop_pages(pages, num_pages);
1240
Chris Mason75dfe392007-03-29 11:56:46 -04001241 buf += write_bytes;
1242 count -= write_bytes;
1243 pos += write_bytes;
1244 num_written += write_bytes;
1245
1246 balance_dirty_pages_ratelimited(inode->i_mapping);
1247 cond_resched();
1248 }
1249 mutex_unlock(&inode->i_mutex);
1250out:
1251 *ppos = pos;
1252 current->backing_dev_info = NULL;
1253 return num_written ? num_written : err;
1254}
1255
Chris Masondf24a2b2007-04-04 09:36:31 -04001256static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1257 struct page *page, loff_t pos,
1258 size_t offset, size_t write_bytes)
1259{
1260 struct btrfs_path *path;
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_key key;
1263 struct btrfs_leaf *leaf;
1264 struct btrfs_key found_key;
1265 int ret;
1266 size_t copy_size = 0;
1267 char *dst = NULL;
1268 int err = 0;
1269 size_t num_written = 0;
1270
1271 path = btrfs_alloc_path();
1272 BUG_ON(!path);
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 trans = btrfs_start_transaction(root, 1);
1275 key.objectid = inode->i_ino;
1276 key.flags = 0;
1277 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1278
1279again:
1280 key.offset = pos;
1281 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1282 if (ret < 0) {
1283 err = ret;
1284 goto out;
1285 }
1286 if (ret == 0) {
1287 leaf = btrfs_buffer_leaf(path->nodes[0]);
1288 btrfs_disk_key_to_cpu(&found_key,
1289 &leaf->items[path->slots[0]].key);
1290 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1291 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1292 copy_size = min(write_bytes, copy_size);
1293 goto copyit;
1294 } else {
1295 int slot = path->slots[0];
1296 if (slot > 0) {
1297 slot--;
1298 }
1299 // FIXME find max key
1300 leaf = btrfs_buffer_leaf(path->nodes[0]);
1301 btrfs_disk_key_to_cpu(&found_key,
1302 &leaf->items[slot].key);
1303 if (found_key.objectid != inode->i_ino)
1304 goto insert;
1305 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1306 goto insert;
1307 copy_size = btrfs_item_size(leaf->items + slot);
1308 if (found_key.offset + copy_size <= pos)
1309 goto insert;
1310 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1311 dst += pos - found_key.offset;
1312 copy_size = copy_size - (pos - found_key.offset);
1313 BUG_ON(copy_size < 0);
1314 copy_size = min(write_bytes, copy_size);
1315 WARN_ON(copy_size == 0);
1316 goto copyit;
1317 }
1318insert:
1319 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001320 copy_size = min(write_bytes,
1321 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1322 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001323 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1324 BUG_ON(ret);
1325 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1326 path->slots[0], char);
1327copyit:
1328 WARN_ON(copy_size == 0);
1329 WARN_ON(dst + copy_size >
1330 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1331 path->slots[0], char) +
1332 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1333 path->slots[0]));
1334 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1335 page_address(page) + offset, copy_size);
1336 mark_buffer_dirty(path->nodes[0]);
1337 btrfs_release_path(root, path);
1338 pos += copy_size;
1339 offset += copy_size;
1340 num_written += copy_size;
1341 write_bytes -= copy_size;
1342 if (write_bytes)
1343 goto again;
1344out:
1345 btrfs_free_path(path);
1346 ret = btrfs_end_transaction(trans, root);
1347 BUG_ON(ret);
1348 mutex_unlock(&root->fs_info->fs_mutex);
1349 return num_written ? num_written : err;
1350}
1351
1352static ssize_t btrfs_file_inline_write(struct file *file,
1353 const char __user *buf,
1354 size_t count, loff_t *ppos)
1355{
1356 loff_t pos;
1357 size_t num_written = 0;
1358 int err = 0;
1359 int ret = 0;
1360 struct inode *inode = file->f_path.dentry->d_inode;
1361 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1362 unsigned long page_index;
1363
1364 if (file->f_flags & O_DIRECT)
1365 return -EINVAL;
1366 pos = *ppos;
1367
1368 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1369 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1370 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1371 if (err)
1372 goto out;
1373 if (count == 0)
1374 goto out;
1375 err = remove_suid(file->f_path.dentry);
1376 if (err)
1377 goto out;
1378 file_update_time(file);
1379 mutex_lock(&inode->i_mutex);
1380 while(count > 0) {
1381 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1382 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1383 struct page *page;
1384
1385 page_index = pos >> PAGE_CACHE_SHIFT;
1386 page = grab_cache_page(inode->i_mapping, page_index);
1387 if (!PageUptodate(page)) {
1388 ret = mpage_readpage(page, btrfs_get_block);
1389 BUG_ON(ret);
1390 lock_page(page);
1391 }
1392 ret = btrfs_copy_from_user(pos, 1,
1393 write_bytes, &page, buf);
1394 BUG_ON(ret);
1395 write_bytes = inline_one_page(root, inode, page, pos,
1396 offset, write_bytes);
1397 SetPageUptodate(page);
1398 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1399 i_size_write(inode, pos + write_bytes);
1400 mark_inode_dirty(inode);
1401 }
1402 page_cache_release(page);
1403 unlock_page(page);
1404 if (write_bytes < 0)
1405 goto out_unlock;
1406 buf += write_bytes;
1407 count -= write_bytes;
1408 pos += write_bytes;
1409 num_written += write_bytes;
1410
1411 balance_dirty_pages_ratelimited(inode->i_mapping);
1412 cond_resched();
1413 }
1414out_unlock:
1415 mutex_unlock(&inode->i_mutex);
1416out:
1417 *ppos = pos;
1418 current->backing_dev_info = NULL;
1419 return num_written ? num_written : err;
1420}
1421
Chris Masonf254e522007-03-29 15:15:27 -04001422static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1423 unsigned long offset, unsigned long size)
1424{
1425 char *kaddr;
1426 unsigned long left, count = desc->count;
1427
1428 if (size > count)
1429 size = count;
1430
1431 if (!PageChecked(page)) {
1432 /* FIXME, do it per block */
1433 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1434 int ret = btrfs_csum_verify_file_block(root,
1435 page->mapping->host->i_ino,
1436 page->index << PAGE_CACHE_SHIFT,
1437 kmap(page), PAGE_CACHE_SIZE);
1438 if (ret) {
1439 printk("failed to verify ino %lu page %lu\n",
1440 page->mapping->host->i_ino,
1441 page->index);
1442 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1443 }
1444 SetPageChecked(page);
1445 kunmap(page);
1446 }
1447 /*
1448 * Faults on the destination of a read are common, so do it before
1449 * taking the kmap.
1450 */
1451 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1452 kaddr = kmap_atomic(page, KM_USER0);
1453 left = __copy_to_user_inatomic(desc->arg.buf,
1454 kaddr + offset, size);
1455 kunmap_atomic(kaddr, KM_USER0);
1456 if (left == 0)
1457 goto success;
1458 }
1459
1460 /* Do it the slow way */
1461 kaddr = kmap(page);
1462 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1463 kunmap(page);
1464
1465 if (left) {
1466 size -= left;
1467 desc->error = -EFAULT;
1468 }
1469success:
1470 desc->count = count - size;
1471 desc->written += size;
1472 desc->arg.buf += size;
1473 return size;
1474}
1475
1476/**
1477 * btrfs_file_aio_read - filesystem read routine
1478 * @iocb: kernel I/O control block
1479 * @iov: io vector request
1480 * @nr_segs: number of segments in the iovec
1481 * @pos: current file position
1482 */
1483static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1484 unsigned long nr_segs, loff_t pos)
1485{
1486 struct file *filp = iocb->ki_filp;
1487 ssize_t retval;
1488 unsigned long seg;
1489 size_t count;
1490 loff_t *ppos = &iocb->ki_pos;
1491
1492 count = 0;
1493 for (seg = 0; seg < nr_segs; seg++) {
1494 const struct iovec *iv = &iov[seg];
1495
1496 /*
1497 * If any segment has a negative length, or the cumulative
1498 * length ever wraps negative then return -EINVAL.
1499 */
1500 count += iv->iov_len;
1501 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1502 return -EINVAL;
1503 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1504 continue;
1505 if (seg == 0)
1506 return -EFAULT;
1507 nr_segs = seg;
1508 count -= iv->iov_len; /* This segment is no good */
1509 break;
1510 }
1511 retval = 0;
1512 if (count) {
1513 for (seg = 0; seg < nr_segs; seg++) {
1514 read_descriptor_t desc;
1515
1516 desc.written = 0;
1517 desc.arg.buf = iov[seg].iov_base;
1518 desc.count = iov[seg].iov_len;
1519 if (desc.count == 0)
1520 continue;
1521 desc.error = 0;
1522 do_generic_file_read(filp, ppos, &desc,
1523 btrfs_read_actor);
1524 retval += desc.written;
1525 if (desc.error) {
1526 retval = retval ?: desc.error;
1527 break;
1528 }
1529 }
1530 }
1531 return retval;
1532}
1533
Chris Mason2c90e5d2007-04-02 10:50:19 -04001534static struct kmem_cache *btrfs_inode_cachep;
1535struct kmem_cache *btrfs_trans_handle_cachep;
1536struct kmem_cache *btrfs_transaction_cachep;
1537struct kmem_cache *btrfs_bit_radix_cachep;
1538struct kmem_cache *btrfs_path_cachep;
1539
1540/*
1541 * Called inside transaction, so use GFP_NOFS
1542 */
1543static struct inode *btrfs_alloc_inode(struct super_block *sb)
1544{
1545 struct btrfs_inode *ei;
1546
1547 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1548 if (!ei)
1549 return NULL;
1550 ei->magic = 0xDEADBEEF;
1551 ei->magic2 = 0xDEADBEAF;
1552 return &ei->vfs_inode;
1553}
1554
1555static void btrfs_destroy_inode(struct inode *inode)
1556{
1557 struct btrfs_inode *ei = BTRFS_I(inode);
1558 WARN_ON(ei->magic != 0xDEADBEEF);
1559 WARN_ON(ei->magic2 != 0xDEADBEAF);
1560 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001561 WARN_ON(inode->i_data.nrpages);
1562
1563 ei->magic = 0;
1564 ei->magic2 = 0;
1565 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1566}
1567
1568static void init_once(void * foo, struct kmem_cache * cachep,
1569 unsigned long flags)
1570{
1571 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1572
1573 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1574 SLAB_CTOR_CONSTRUCTOR) {
1575 inode_init_once(&ei->vfs_inode);
1576 }
1577}
1578
1579static int init_inodecache(void)
1580{
1581 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1582 sizeof(struct btrfs_inode),
1583 0, (SLAB_RECLAIM_ACCOUNT|
1584 SLAB_MEM_SPREAD),
1585 init_once, NULL);
1586 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1587 sizeof(struct btrfs_trans_handle),
1588 0, (SLAB_RECLAIM_ACCOUNT|
1589 SLAB_MEM_SPREAD),
1590 NULL, NULL);
1591 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1592 sizeof(struct btrfs_transaction),
1593 0, (SLAB_RECLAIM_ACCOUNT|
1594 SLAB_MEM_SPREAD),
1595 NULL, NULL);
1596 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1597 sizeof(struct btrfs_transaction),
1598 0, (SLAB_RECLAIM_ACCOUNT|
1599 SLAB_MEM_SPREAD),
1600 NULL, NULL);
1601 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1602 256,
1603 0, (SLAB_RECLAIM_ACCOUNT|
1604 SLAB_MEM_SPREAD |
1605 SLAB_DESTROY_BY_RCU),
1606 NULL, NULL);
1607 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1608 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1609 return -ENOMEM;
1610 return 0;
1611}
1612
1613static void destroy_inodecache(void)
1614{
1615 kmem_cache_destroy(btrfs_inode_cachep);
1616 kmem_cache_destroy(btrfs_trans_handle_cachep);
1617 kmem_cache_destroy(btrfs_transaction_cachep);
1618 kmem_cache_destroy(btrfs_bit_radix_cachep);
1619 kmem_cache_destroy(btrfs_path_cachep);
1620}
1621
Chris Mason2e635a22007-03-21 11:12:56 -04001622static int btrfs_get_sb(struct file_system_type *fs_type,
1623 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1624{
1625 return get_sb_bdev(fs_type, flags, dev_name, data,
1626 btrfs_fill_super, mnt);
1627}
1628
1629static struct file_system_type btrfs_fs_type = {
1630 .owner = THIS_MODULE,
1631 .name = "btrfs",
1632 .get_sb = btrfs_get_sb,
1633 .kill_sb = kill_block_super,
1634 .fs_flags = FS_REQUIRES_DEV,
1635};
1636
Chris Masone20d96d2007-03-22 12:13:20 -04001637static struct super_operations btrfs_super_ops = {
1638 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001639 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001640 .put_super = btrfs_put_super,
1641 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001642 .write_super = btrfs_write_super,
1643 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001644 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001645 .alloc_inode = btrfs_alloc_inode,
1646 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001647};
1648
1649static struct inode_operations btrfs_dir_inode_operations = {
1650 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001651 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001652 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001653 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001654 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001655};
1656
1657static struct file_operations btrfs_dir_file_operations = {
1658 .llseek = generic_file_llseek,
1659 .read = generic_read_dir,
1660 .readdir = btrfs_readdir,
1661};
1662
Chris Masondee26a92007-03-26 16:00:06 -04001663static struct address_space_operations btrfs_aops = {
1664 .readpage = btrfs_readpage,
Chris Masondf24a2b2007-04-04 09:36:31 -04001665 // .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001666 .writepage = btrfs_writepage,
1667 .sync_page = block_sync_page,
1668 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001669 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001670};
1671
1672static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001673 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001674};
1675
1676static struct file_operations btrfs_file_operations = {
1677 .llseek = generic_file_llseek,
1678 .read = do_sync_read,
Chris Masondf24a2b2007-04-04 09:36:31 -04001679 .aio_read = generic_file_aio_read,
1680 .write = btrfs_file_inline_write,
Chris Masondee26a92007-03-26 16:00:06 -04001681 .mmap = generic_file_mmap,
1682 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001683};
Chris Masone20d96d2007-03-22 12:13:20 -04001684
Chris Mason2e635a22007-03-21 11:12:56 -04001685static int __init init_btrfs_fs(void)
1686{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001687 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001688 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001689 err = init_inodecache();
1690 if (err)
1691 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001692 return register_filesystem(&btrfs_fs_type);
1693}
1694
1695static void __exit exit_btrfs_fs(void)
1696{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001697 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001698 unregister_filesystem(&btrfs_fs_type);
1699 printk("btrfs unloaded\n");
1700}
1701
1702module_init(init_btrfs_fs)
1703module_exit(exit_btrfs_fs)
1704
1705MODULE_LICENSE("GPL");