blob: 32224ffdcecd0babaf2f598dac3cc5eaa09274b1 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -04002#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -04003#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
Chris Masondee26a92007-03-26 16:00:06 -040011#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040012#include <linux/swap.h>
13#include <linux/writeback.h>
Chris Mason2e635a22007-03-21 11:12:56 -040014#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040015#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040016#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040017#include "btrfs_inode.h"
Chris Mason2e635a22007-03-21 11:12:56 -040018
19#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040020
21static struct inode_operations btrfs_dir_inode_operations;
22static struct super_operations btrfs_super_ops;
23static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040024static struct inode_operations btrfs_file_inode_operations;
25static struct address_space_operations btrfs_aops;
26static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040027
Chris Mason2c90e5d2007-04-02 10:50:19 -040028static int check_inode(struct inode *inode)
29{
30 struct btrfs_inode *ei = BTRFS_I(inode);
31 WARN_ON(ei->magic != 0xDEADBEEF);
32 WARN_ON(ei->magic2 != 0xDEADBEAF);
33 return 0;
34}
35
Chris Masone20d96d2007-03-22 12:13:20 -040036static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040037{
Chris Mason5caf2a02007-04-02 11:20:42 -040038 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040039 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = btrfs_sb(inode->i_sb);
41 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040042
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
44 BUG_ON(!path);
45 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040046 mutex_lock(&root->fs_info->fs_mutex);
47
Chris Mason2c90e5d2007-04-02 10:50:19 -040048 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040049 ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040050 if (ret) {
Chris Mason5caf2a02007-04-02 11:20:42 -040051 btrfs_release_path(root, path);
Chris Mason7cfcc172007-04-02 14:53:59 -040052 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040053 mutex_unlock(&root->fs_info->fs_mutex);
54 make_bad_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040055 return;
Chris Mason2e635a22007-03-21 11:12:56 -040056 }
Chris Mason2c90e5d2007-04-02 10:50:19 -040057 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040058 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040060 struct btrfs_inode_item);
61
Chris Masone20d96d2007-03-22 12:13:20 -040062 inode->i_mode = btrfs_inode_mode(inode_item);
63 inode->i_nlink = btrfs_inode_nlink(inode_item);
64 inode->i_uid = btrfs_inode_uid(inode_item);
65 inode->i_gid = btrfs_inode_gid(inode_item);
66 inode->i_size = btrfs_inode_size(inode_item);
67 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
68 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
69 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
70 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
71 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
72 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
73 inode->i_blocks = btrfs_inode_nblocks(inode_item);
74 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040075
76 btrfs_release_path(root, path);
77 btrfs_free_path(path);
78 inode_item = NULL;
79
Chris Masonf4b9aa82007-03-27 11:05:53 -040080 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040081 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040082 switch (inode->i_mode & S_IFMT) {
83#if 0
84 default:
85 init_special_inode(inode, inode->i_mode,
86 btrfs_inode_rdev(inode_item));
87 break;
88#endif
89 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040090 inode->i_mapping->a_ops = &btrfs_aops;
91 inode->i_fop = &btrfs_file_operations;
92 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040093 break;
94 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040095 inode->i_op = &btrfs_dir_inode_operations;
96 inode->i_fop = &btrfs_dir_file_operations;
97 break;
98 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -040099 // inode->i_op = &page_symlink_inode_operations;
100 break;
101 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400102 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400103 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400104}
105
Chris Mason5f443fd2007-03-27 13:42:32 -0400106static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct inode *dir,
109 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400110{
Chris Mason5caf2a02007-04-02 11:20:42 -0400111 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400112 const char *name = dentry->d_name.name;
113 int name_len = dentry->d_name.len;
114 int ret;
115 u64 objectid;
116 struct btrfs_dir_item *di;
117
Chris Mason5caf2a02007-04-02 11:20:42 -0400118 path = btrfs_alloc_path();
119 BUG_ON(!path);
120 btrfs_init_path(path);
121 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400122 name, name_len, -1);
123 if (ret < 0)
124 goto err;
125 if (ret > 0) {
126 ret = -ENOENT;
127 goto err;
128 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400130 struct btrfs_dir_item);
131 objectid = btrfs_dir_objectid(di);
132
Chris Mason5caf2a02007-04-02 11:20:42 -0400133 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400134 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400135
136 btrfs_release_path(root, path);
137 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
138 objectid, -1);
139 BUG_ON(ret);
140 ret = btrfs_del_item(trans, root, path);
141 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400142 dentry->d_inode->i_ctime = dir->i_ctime;
143err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400144 btrfs_release_path(root, path);
145 btrfs_free_path(path);
Chris Masond4dbff92007-04-04 14:08:15 -0400146 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400147 inode_dec_link_count(dentry->d_inode);
Chris Mason5f26f772007-04-05 10:38:44 -0400148 dir->i_size -= name_len * 2;
Chris Masond4dbff92007-04-04 14:08:15 -0400149 mark_inode_dirty(dir);
150 }
Chris Mason134e9732007-03-25 13:44:56 -0400151 return ret;
152}
153
Chris Mason5f443fd2007-03-27 13:42:32 -0400154static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
155{
156 struct btrfs_root *root;
157 struct btrfs_trans_handle *trans;
158 int ret;
159
160 root = btrfs_sb(dir->i_sb);
161 mutex_lock(&root->fs_info->fs_mutex);
162 trans = btrfs_start_transaction(root, 1);
163 ret = btrfs_unlink_trans(trans, root, dir, dentry);
164 btrfs_end_transaction(trans, root);
165 mutex_unlock(&root->fs_info->fs_mutex);
166 return ret;
167}
168
169static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
170{
171 struct inode *inode = dentry->d_inode;
172 int err;
173 int ret;
174 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Mason5caf2a02007-04-02 11:20:42 -0400175 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400176 struct btrfs_key key;
177 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400178 struct btrfs_key found_key;
179 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400180 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400181 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400182
Chris Mason5caf2a02007-04-02 11:20:42 -0400183 path = btrfs_alloc_path();
184 BUG_ON(!path);
185 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400186 mutex_lock(&root->fs_info->fs_mutex);
187 trans = btrfs_start_transaction(root, 1);
188 key.objectid = inode->i_ino;
189 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400190 key.flags = (u32)-1;
191 while(1) {
192 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
193 if (ret < 0) {
194 err = ret;
195 goto out;
196 }
197 BUG_ON(ret == 0);
198 if (path->slots[0] == 0) {
199 err = -ENOENT;
200 goto out;
201 }
202 path->slots[0]--;
203 leaf = btrfs_buffer_leaf(path->nodes[0]);
204 btrfs_disk_key_to_cpu(&found_key,
205 &leaf->items[path->slots[0]].key);
206 found_type = btrfs_key_type(&found_key);
207 if (found_key.objectid != inode->i_ino) {
208 err = -ENOENT;
209 goto out;
210 }
211 if ((found_type != BTRFS_DIR_ITEM_KEY &&
212 found_type != BTRFS_DIR_INDEX_KEY) ||
213 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
214 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
215 err = -ENOTEMPTY;
216 goto out;
217 }
218 ret = btrfs_del_item(trans, root, path);
219 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400220
Chris Mason5f26f772007-04-05 10:38:44 -0400221 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
222 break;
223 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400224 }
Chris Mason5f26f772007-04-05 10:38:44 -0400225 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400226 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400227
228 /* now the directory is empty */
229 err = btrfs_unlink_trans(trans, root, dir, dentry);
230 if (!err) {
231 inode->i_size = 0;
232 }
233out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400234 btrfs_release_path(root, path);
235 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400236 mutex_unlock(&root->fs_info->fs_mutex);
237 ret = btrfs_end_transaction(trans, root);
238 if (ret && !err)
239 err = ret;
240 return err;
241}
242
Chris Mason134e9732007-03-25 13:44:56 -0400243static int btrfs_free_inode(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root,
245 struct inode *inode)
246{
247 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400248 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400249 struct btrfs_inode_map_item *map;
250 struct btrfs_key stat_data_key;
251 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400252
Chris Mason134e9732007-03-25 13:44:56 -0400253 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400254
255 path = btrfs_alloc_path();
256 BUG_ON(!path);
257 btrfs_init_path(path);
258 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400259 if (ret) {
260 if (ret > 0)
261 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400262 goto error;
263 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400264 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400265 struct btrfs_inode_map_item);
266 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400267 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400268 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400269 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400270
Chris Mason5caf2a02007-04-02 11:20:42 -0400271 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400272 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400273 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400274 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400275error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400276 btrfs_release_path(root, path);
277 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400278 return ret;
279}
280
Chris Masonf4b9aa82007-03-27 11:05:53 -0400281static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
282 struct btrfs_root *root,
283 struct inode *inode)
284{
285 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400286 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400287 struct btrfs_key key;
288 struct btrfs_disk_key *found_key;
289 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400290 struct btrfs_file_extent_item *fi = NULL;
291 u64 extent_start = 0;
292 u64 extent_num_blocks = 0;
293 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400294
Chris Mason5caf2a02007-04-02 11:20:42 -0400295 path = btrfs_alloc_path();
296 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400297 /* FIXME, add redo link to tree so we don't leak on crash */
298 key.objectid = inode->i_ino;
299 key.offset = (u64)-1;
300 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400301 /*
302 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
303 * or extent data
304 */
Chris Masonf254e522007-03-29 15:15:27 -0400305 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400306 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400307 btrfs_init_path(path);
308 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400309 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 goto error;
311 }
312 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400313 BUG_ON(path->slots[0] == 0);
314 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400315 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400316 leaf = btrfs_buffer_leaf(path->nodes[0]);
317 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400318 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
319 break;
Chris Masonf254e522007-03-29 15:15:27 -0400320 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400321 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400322 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400323 break;
324 if (btrfs_disk_key_offset(found_key) < inode->i_size)
325 break;
Chris Masonf254e522007-03-29 15:15:27 -0400326 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400327 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
328 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400329 struct btrfs_file_extent_item);
330 extent_start = btrfs_file_extent_disk_blocknr(fi);
331 extent_num_blocks =
332 btrfs_file_extent_disk_num_blocks(fi);
333 inode->i_blocks -=
334 btrfs_file_extent_num_blocks(fi) >> 9;
335 found_extent = 1;
336 } else {
337 found_extent = 0;
338 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400339 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400340 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400341 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400342 if (found_extent) {
343 ret = btrfs_free_extent(trans, root, extent_start,
344 extent_num_blocks, 0);
345 BUG_ON(ret);
346 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400347 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400348 ret = 0;
349error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400350 btrfs_release_path(root, path);
351 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400352 return ret;
353}
354
Chris Mason134e9732007-03-25 13:44:56 -0400355static void btrfs_delete_inode(struct inode *inode)
356{
357 struct btrfs_trans_handle *trans;
358 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400359 int ret;
360
Chris Mason134e9732007-03-25 13:44:56 -0400361 truncate_inode_pages(&inode->i_data, 0);
362 if (is_bad_inode(inode)) {
363 goto no_delete;
364 }
365 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400366 mutex_lock(&root->fs_info->fs_mutex);
367 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400368 if (S_ISREG(inode->i_mode)) {
369 ret = btrfs_truncate_in_trans(trans, root, inode);
370 BUG_ON(ret);
371 }
Chris Mason134e9732007-03-25 13:44:56 -0400372 btrfs_free_inode(trans, root, inode);
373 btrfs_end_transaction(trans, root);
374 mutex_unlock(&root->fs_info->fs_mutex);
375 return;
376no_delete:
377 clear_inode(inode);
378}
379
Chris Masone20d96d2007-03-22 12:13:20 -0400380static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
381 ino_t *ino)
382{
383 const char *name = dentry->d_name.name;
384 int namelen = dentry->d_name.len;
385 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400387 struct btrfs_root *root = btrfs_sb(dir->i_sb);
388 int ret;
389
Chris Mason5caf2a02007-04-02 11:20:42 -0400390 path = btrfs_alloc_path();
391 BUG_ON(!path);
392 btrfs_init_path(path);
393 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400394 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400395 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400396 *ino = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400397 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400398 goto out;
399 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400400 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400401 struct btrfs_dir_item);
402 *ino = btrfs_dir_objectid(di);
403out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_release_path(root, path);
405 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400406 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400407 return ret;
408}
409
410static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
411 struct nameidata *nd)
412{
413 struct inode * inode;
Chris Mason22b0ebd2007-03-30 08:47:31 -0400414 struct btrfs_root *root = btrfs_sb(dir->i_sb);
Chris Masone20d96d2007-03-22 12:13:20 -0400415 ino_t ino;
416 int ret;
417
418 if (dentry->d_name.len > BTRFS_NAME_LEN)
419 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400420 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400421 ret = btrfs_inode_by_name(dir, dentry, &ino);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400422 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400423 if (ret < 0)
424 return ERR_PTR(ret);
425 inode = NULL;
426 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400427 inode = iget(dir->i_sb, ino);
428 if (!inode)
429 return ERR_PTR(-EACCES);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400430 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400431 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400432 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400433 return d_splice_alias(inode, dentry);
434}
435
436static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
437{
438 struct inode *inode = filp->f_path.dentry->d_inode;
439 struct btrfs_root *root = btrfs_sb(inode->i_sb);
440 struct btrfs_item *item;
441 struct btrfs_dir_item *di;
442 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400443 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400444 int ret;
445 u32 nritems;
446 struct btrfs_leaf *leaf;
447 int slot;
448 int advance;
449 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400450 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400451
Chris Mason22b0ebd2007-03-30 08:47:31 -0400452 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400453 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400454 key.flags = 0;
Chris Masonbae45de2007-04-04 21:22:22 -0400455 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -0400456 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400457 path = btrfs_alloc_path();
458 btrfs_init_path(path);
459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400460 if (ret < 0) {
461 goto err;
462 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400463 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400464 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400465 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400466 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400467 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400468 if (advance || slot >= nritems) {
469 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400470 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400471 if (ret)
472 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400473 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400474 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400475 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400476 } else {
477 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400478 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400479 }
480 }
481 advance = 1;
482 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400483 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
484 break;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400485 if (btrfs_disk_key_offset(&item->key) >
Chris Mason3eb03142007-04-05 14:28:50 -0400486 root->fs_info->highest_inode)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400487 break;
Chris Masonbae45de2007-04-04 21:22:22 -0400488 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_INDEX_KEY)
Chris Masone20d96d2007-03-22 12:13:20 -0400489 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400490 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
491 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400492 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400493 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400494 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400495 over = filldir(dirent, (const char *)(di + 1),
496 btrfs_dir_name_len(di),
497 btrfs_disk_key_offset(&item->key),
498 btrfs_dir_objectid(di), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400499 if (over)
500 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400501 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400502 filp->f_pos++;
503nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400504 ret = 0;
505err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400506 btrfs_release_path(root, path);
507 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400508 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400509 return ret;
510}
511
512static void btrfs_put_super (struct super_block * sb)
513{
514 struct btrfs_root *root = btrfs_sb(sb);
515 int ret;
516
517 ret = close_ctree(root);
518 if (ret) {
519 printk("close ctree returns %d\n", ret);
520 }
521 sb->s_fs_info = NULL;
522}
Chris Mason2e635a22007-03-21 11:12:56 -0400523
524static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
525{
526 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400527 struct dentry * root_dentry;
528 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400529 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400530
531 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400532 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400533 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400534 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400535
Chris Mason2c90e5d2007-04-02 10:50:19 -0400536 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400537
Chris Masone20d96d2007-03-22 12:13:20 -0400538 if (!root) {
539 printk("btrfs: open_ctree failed\n");
540 return -EIO;
541 }
Chris Mason87cbda52007-03-28 19:44:27 -0400542 sb->s_fs_info = root;
543 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400544 printk("read in super total blocks %Lu root %Lu\n",
545 btrfs_super_total_blocks(disk_super),
546 btrfs_super_root_dir(disk_super));
547
548 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400549 if (!inode)
550 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400551 if (inode->i_state & I_NEW) {
552 btrfs_read_locked_inode(inode);
553 unlock_new_inode(inode);
554 }
Chris Mason2e635a22007-03-21 11:12:56 -0400555
Chris Masone20d96d2007-03-22 12:13:20 -0400556 root_dentry = d_alloc_root(inode);
557 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400558 iput(inode);
559 return -ENOMEM;
560 }
Chris Masone20d96d2007-03-22 12:13:20 -0400561 sb->s_root = root_dentry;
562
Chris Mason2e635a22007-03-21 11:12:56 -0400563 return 0;
564}
565
Chris Masond5719762007-03-23 10:01:08 -0400566static void fill_inode_item(struct btrfs_inode_item *item,
567 struct inode *inode)
568{
569 btrfs_set_inode_uid(item, inode->i_uid);
570 btrfs_set_inode_gid(item, inode->i_gid);
571 btrfs_set_inode_size(item, inode->i_size);
572 btrfs_set_inode_mode(item, inode->i_mode);
573 btrfs_set_inode_nlink(item, inode->i_nlink);
574 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
575 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
576 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
577 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
578 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
579 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
580 btrfs_set_inode_nblocks(item, inode->i_blocks);
581 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400582 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400583}
584
Chris Mason4730a4b2007-03-26 12:00:39 -0400585static int btrfs_update_inode(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct inode *inode)
588{
589 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400590 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400591 int ret;
592
Chris Mason5caf2a02007-04-02 11:20:42 -0400593 path = btrfs_alloc_path();
594 BUG_ON(!path);
595 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400596
Chris Mason5caf2a02007-04-02 11:20:42 -0400597 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400598 if (ret) {
599 if (ret > 0)
600 ret = -ENOENT;
601 goto failed;
602 }
603
Chris Mason5caf2a02007-04-02 11:20:42 -0400604 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
605 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400606 struct btrfs_inode_item);
607
608 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400609 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400610failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400611 btrfs_release_path(root, path);
612 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400613 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400614 return 0;
615}
616
617static int btrfs_write_inode(struct inode *inode, int wait)
618{
619 struct btrfs_root *root = btrfs_sb(inode->i_sb);
620 struct btrfs_trans_handle *trans;
621 int ret;
622
623 mutex_lock(&root->fs_info->fs_mutex);
624 trans = btrfs_start_transaction(root, 1);
625 ret = btrfs_update_inode(trans, root, inode);
626 if (wait)
627 btrfs_commit_transaction(trans, root);
628 else
629 btrfs_end_transaction(trans, root);
630 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400631 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400632 return ret;
633}
634
Chris Masond5719762007-03-23 10:01:08 -0400635static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
636 struct inode *dir, int mode)
637{
638 struct inode *inode;
639 struct btrfs_inode_item inode_item;
640 struct btrfs_root *root = btrfs_sb(dir->i_sb);
641 struct btrfs_key key;
642 int ret;
643 u64 objectid;
644
645 inode = new_inode(dir->i_sb);
646 if (!inode)
647 return ERR_PTR(-ENOMEM);
648
Chris Mason2c90e5d2007-04-02 10:50:19 -0400649 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400650 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
651 BUG_ON(ret);
652
653 inode->i_uid = current->fsuid;
654 inode->i_gid = current->fsgid;
655 inode->i_mode = mode;
656 inode->i_ino = objectid;
657 inode->i_blocks = 0;
658 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
659 fill_inode_item(&inode_item, inode);
660
Chris Masond5719762007-03-23 10:01:08 -0400661 key.objectid = objectid;
662 key.flags = 0;
663 key.offset = 0;
664 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
665 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
666 BUG_ON(ret);
667
668 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
669 BUG_ON(ret);
670
671 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400672 check_inode(inode);
673 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400674 return inode;
675}
676
677static int btrfs_add_link(struct btrfs_trans_handle *trans,
678 struct dentry *dentry, struct inode *inode)
679{
680 int ret;
681 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
682 dentry->d_name.name, dentry->d_name.len,
683 dentry->d_parent->d_inode->i_ino,
684 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400685 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400686 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Mason4730a4b2007-03-26 12:00:39 -0400687 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
688 dentry->d_parent->d_inode);
689 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400690 check_inode(inode);
691 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400692 return ret;
693}
694
695static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
696 struct dentry *dentry, struct inode *inode)
697{
698 int err = btrfs_add_link(trans, dentry, inode);
699 if (!err) {
700 d_instantiate(dentry, inode);
701 return 0;
702 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400703 if (err > 0)
704 err = -EEXIST;
705 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400706 return err;
707}
708
709static int btrfs_create(struct inode *dir, struct dentry *dentry,
710 int mode, struct nameidata *nd)
711{
712 struct btrfs_trans_handle *trans;
713 struct btrfs_root *root = btrfs_sb(dir->i_sb);
714 struct inode *inode;
715 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400716 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400717
Chris Masond561c022007-03-23 19:47:49 -0400718 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400719 trans = btrfs_start_transaction(root, 1);
720 inode = btrfs_new_inode(trans, dir, mode);
721 err = PTR_ERR(inode);
722 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400723 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400724 // FIXME mark the inode dirty
725 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400726 if (err)
727 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400728 else {
729 inode->i_mapping->a_ops = &btrfs_aops;
730 inode->i_fop = &btrfs_file_operations;
731 inode->i_op = &btrfs_file_inode_operations;
732 }
Chris Masond5719762007-03-23 10:01:08 -0400733 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400734out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400735 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400736 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400737 check_inode(inode);
738 check_inode(dir);
739
Chris Mason134e9732007-03-25 13:44:56 -0400740 if (drop_inode) {
741 inode_dec_link_count(inode);
742 iput(inode);
743 }
Chris Masond5719762007-03-23 10:01:08 -0400744 return err;
745}
746
Chris Masonf7922032007-03-25 20:17:36 -0400747static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
748 struct inode *inode, struct inode *dir)
749{
750 struct btrfs_root *root = btrfs_sb(inode->i_sb);
751 int ret;
752 char buf[2];
753 buf[0] = '.';
754 buf[1] = '.';
755
756 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
757 inode->i_ino, 1);
758 if (ret)
759 goto error;
760 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
761 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400762 if (ret)
763 goto error;
Chris Mason5f26f772007-04-05 10:38:44 -0400764 inode->i_size = 6;
Chris Mason4730a4b2007-03-26 12:00:39 -0400765 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400766error:
767 return ret;
768}
769
770static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
771{
772 struct inode *inode;
773 struct btrfs_trans_handle *trans;
774 struct btrfs_root *root = btrfs_sb(dir->i_sb);
775 int err = 0;
776 int drop_on_err = 0;
777
778 mutex_lock(&root->fs_info->fs_mutex);
779 trans = btrfs_start_transaction(root, 1);
780 if (IS_ERR(trans)) {
781 err = PTR_ERR(trans);
782 goto out_unlock;
783 }
784 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
785 if (IS_ERR(inode)) {
786 err = PTR_ERR(inode);
787 goto out_fail;
788 }
789 drop_on_err = 1;
790 inode->i_op = &btrfs_dir_inode_operations;
791 inode->i_fop = &btrfs_dir_file_operations;
792
793 err = btrfs_make_empty_dir(trans, inode, dir);
794 if (err)
795 goto out_fail;
796 err = btrfs_add_link(trans, dentry, inode);
797 if (err)
798 goto out_fail;
799 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400800 drop_on_err = 0;
801
802out_fail:
803 btrfs_end_transaction(trans, root);
804out_unlock:
805 mutex_unlock(&root->fs_info->fs_mutex);
806 if (drop_on_err)
807 iput(inode);
808 return err;
809}
810
Chris Masond5719762007-03-23 10:01:08 -0400811static int btrfs_sync_fs(struct super_block *sb, int wait)
812{
813 struct btrfs_trans_handle *trans;
814 struct btrfs_root *root;
815 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400816 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400817
Chris Masond5719762007-03-23 10:01:08 -0400818 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400819 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400820 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400821 return 0;
822 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400823 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400824 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400825 trans = btrfs_start_transaction(root, 1);
826 ret = btrfs_commit_transaction(trans, root);
827 sb->s_dirt = 0;
828 BUG_ON(ret);
829printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400830 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400831 return 0;
832}
833
Chris Masone8f05c42007-04-04 14:30:09 -0400834#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400835static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
836 struct buffer_head *result, int create)
837{
838 struct btrfs_root *root = btrfs_sb(inode->i_sb);
839 struct btrfs_path *path;
840 struct btrfs_key key;
841 struct btrfs_leaf *leaf;
842 int num_bytes = result->b_size;
843 int item_size;
844 int ret;
845 u64 pos;
846 char *ptr;
847 int copy_size;
848 int err = 0;
849 char *safe_ptr;
850 char *data_ptr;
851
852 path = btrfs_alloc_path();
853 BUG_ON(!path);
854
855 WARN_ON(create);
856 if (create) {
857 return 0;
858 }
859 pos = iblock << inode->i_blkbits;
860 key.objectid = inode->i_ino;
861 key.flags = 0;
862 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
863 ptr = kmap(result->b_page);
864 safe_ptr = ptr;
865 ptr += (pos & (PAGE_CACHE_SIZE -1));
866again:
867 key.offset = pos;
868 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
869 if (ret) {
870 if (ret < 0)
871 err = ret;
872 else
873 err = 0;
874 goto out;
875 }
876 leaf = btrfs_buffer_leaf(path->nodes[0]);
877 item_size = btrfs_item_size(leaf->items + path->slots[0]);
878 copy_size = min(num_bytes, item_size);
879 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
880 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
881 memcpy(ptr, data_ptr, copy_size);
882 pos += copy_size;
883 num_bytes -= copy_size;
884 WARN_ON(num_bytes < 0);
885 ptr += copy_size;
886 btrfs_release_path(root, path);
887 if (num_bytes != 0) {
888 if (pos >= i_size_read(inode))
889 memset(ptr, 0, num_bytes);
890 else
891 goto again;
892 }
893 set_buffer_uptodate(result);
894 map_bh(result, inode->i_sb, 0);
895 err = 0;
896out:
897 btrfs_free_path(path);
898 kunmap(result->b_page);
899 return err;
900}
Chris Masone8f05c42007-04-04 14:30:09 -0400901#endif
Chris Masondf24a2b2007-04-04 09:36:31 -0400902
Chris Mason75dfe392007-03-29 11:56:46 -0400903static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -0400904 struct buffer_head *result, int create)
905{
906 int ret;
907 int err = 0;
908 u64 blocknr;
909 u64 extent_start = 0;
910 u64 extent_end = 0;
911 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400912 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -0400913 struct btrfs_root *root = btrfs_sb(inode->i_sb);
914 struct btrfs_trans_handle *trans = NULL;
915 struct btrfs_file_extent_item *item;
916 struct btrfs_leaf *leaf;
917 struct btrfs_disk_key *found_key;
918
Chris Mason5caf2a02007-04-02 11:20:42 -0400919 path = btrfs_alloc_path();
920 BUG_ON(!path);
921 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400922 if (create)
923 trans = btrfs_start_transaction(root, 1);
924
Chris Mason5caf2a02007-04-02 11:20:42 -0400925 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -0400926 inode->i_ino,
927 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400928 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -0400929 err = ret;
930 goto out;
931 }
932
933 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400934 if (path->slots[0] == 0) {
935 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400936 goto allocate;
937 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400938 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -0400939 }
940
Chris Mason5caf2a02007-04-02 11:20:42 -0400941 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -0400942 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400943 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -0400944 blocknr = btrfs_file_extent_disk_blocknr(item);
945 blocknr += btrfs_file_extent_offset(item);
946
947 /* exact match found, use it */
948 if (ret == 0) {
949 err = 0;
950 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -0400951 goto out;
952 }
953
954 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -0400955 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -0400956 if (btrfs_disk_key_objectid(found_key) != objectid ||
957 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
958 extent_end = 0;
959 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400960 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -0400961 goto allocate;
962 }
963
Chris Mason5caf2a02007-04-02 11:20:42 -0400964 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400965 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400966 extent_start += btrfs_file_extent_offset(item);
967 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -0400968 if (iblock >= extent_start && iblock < extent_end) {
969 err = 0;
970 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
971 goto out;
972 }
973allocate:
974 /* ok, create a new extent */
975 if (!create) {
976 err = 0;
977 goto out;
978 }
Chris Mason9773a782007-03-27 11:26:26 -0400979 ret = btrfs_alloc_file_extent(trans, root, objectid,
980 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400981 1, extent_end, &blocknr);
982 if (ret) {
983 err = ret;
984 goto out;
985 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400986 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
987 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400988 map_bh(result, inode->i_sb, blocknr);
989
990out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400991 btrfs_release_path(root, path);
992 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -0400993 if (trans)
994 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -0400995 return err;
996}
997
998static int btrfs_get_block(struct inode *inode, sector_t iblock,
999 struct buffer_head *result, int create)
1000{
1001 int err;
1002 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1003 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001004 err = btrfs_get_block_lock(inode, iblock, result, create);
1005 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001006 mutex_unlock(&root->fs_info->fs_mutex);
1007 return err;
1008}
1009
1010static int btrfs_prepare_write(struct file *file, struct page *page,
1011 unsigned from, unsigned to)
1012{
Chris Mason6407bf62007-03-27 06:33:00 -04001013 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001014}
Chris Mason75dfe392007-03-29 11:56:46 -04001015static int btrfs_commit_write(struct file *file, struct page *page,
1016 unsigned from, unsigned to)
1017{
Chris Mason75dfe392007-03-29 11:56:46 -04001018 return nobh_commit_write(file, page, from, to);
1019}
Chris Masondee26a92007-03-26 16:00:06 -04001020
Chris Masond561c022007-03-23 19:47:49 -04001021static void btrfs_write_super(struct super_block *sb)
1022{
1023 btrfs_sync_fs(sb, 1);
1024}
1025
Chris Masondee26a92007-03-26 16:00:06 -04001026static int btrfs_readpage(struct file *file, struct page *page)
1027{
1028 return mpage_readpage(page, btrfs_get_block);
1029}
1030
1031static int btrfs_readpages(struct file *file, struct address_space *mapping,
1032 struct list_head *pages, unsigned nr_pages)
1033{
1034 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1035}
1036
1037static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1038{
Chris Mason6407bf62007-03-27 06:33:00 -04001039 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001040}
Chris Masond561c022007-03-23 19:47:49 -04001041
Chris Masonf4b9aa82007-03-27 11:05:53 -04001042static void btrfs_truncate(struct inode *inode)
1043{
1044 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1045 int ret;
1046 struct btrfs_trans_handle *trans;
1047
1048 if (!S_ISREG(inode->i_mode))
1049 return;
1050 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1051 return;
1052
Chris Masone8f05c42007-04-04 14:30:09 -04001053 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001054
1055 /* FIXME, add redo link to tree so we don't leak on crash */
1056 mutex_lock(&root->fs_info->fs_mutex);
1057 trans = btrfs_start_transaction(root, 1);
1058 ret = btrfs_truncate_in_trans(trans, root, inode);
1059 BUG_ON(ret);
1060 ret = btrfs_end_transaction(trans, root);
1061 BUG_ON(ret);
1062 mutex_unlock(&root->fs_info->fs_mutex);
1063 mark_inode_dirty(inode);
1064}
1065
Chris Mason75dfe392007-03-29 11:56:46 -04001066static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1067 struct page **prepared_pages,
1068 const char __user * buf)
1069{
1070 long page_fault = 0;
1071 int i;
1072 int offset = pos & (PAGE_CACHE_SIZE - 1);
1073
1074 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1075 size_t count = min_t(size_t,
1076 PAGE_CACHE_SIZE - offset, write_bytes);
1077 struct page *page = prepared_pages[i];
1078 fault_in_pages_readable(buf, count);
1079
1080 /* Copy data from userspace to the current page */
1081 kmap(page);
1082 page_fault = __copy_from_user(page_address(page) + offset,
1083 buf, count);
1084 /* Flush processor's dcache for this page */
1085 flush_dcache_page(page);
1086 kunmap(page);
1087 buf += count;
1088 write_bytes -= count;
1089
1090 if (page_fault)
1091 break;
1092 }
1093 return page_fault ? -EFAULT : 0;
1094}
1095
1096static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1097{
1098 size_t i;
1099 for (i = 0; i < num_pages; i++) {
1100 if (!pages[i])
1101 break;
1102 unlock_page(pages[i]);
1103 mark_page_accessed(pages[i]);
1104 page_cache_release(pages[i]);
1105 }
1106}
1107static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1108 struct btrfs_root *root,
1109 struct file *file,
1110 struct page **pages,
1111 size_t num_pages,
1112 loff_t pos,
1113 size_t write_bytes)
1114{
1115 int i;
1116 int offset;
1117 int err = 0;
1118 int ret;
1119 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001120 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001121
1122 for (i = 0; i < num_pages; i++) {
1123 offset = pos & (PAGE_CACHE_SIZE -1);
1124 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001125 /* FIXME, one block at a time */
1126
1127 mutex_lock(&root->fs_info->fs_mutex);
1128 trans = btrfs_start_transaction(root, 1);
1129 btrfs_csum_file_block(trans, root, inode->i_ino,
1130 pages[i]->index << PAGE_CACHE_SHIFT,
1131 kmap(pages[i]), PAGE_CACHE_SIZE);
1132 kunmap(pages[i]);
1133 SetPageChecked(pages[i]);
1134 ret = btrfs_end_transaction(trans, root);
1135 BUG_ON(ret);
1136 mutex_unlock(&root->fs_info->fs_mutex);
1137
Chris Mason75dfe392007-03-29 11:56:46 -04001138 ret = nobh_commit_write(file, pages[i], offset,
1139 offset + this_write);
1140 pos += this_write;
1141 if (ret) {
1142 err = ret;
1143 goto failed;
1144 }
1145 WARN_ON(this_write > write_bytes);
1146 write_bytes -= this_write;
1147 }
1148failed:
1149 return err;
1150}
1151
1152static int prepare_pages(struct btrfs_trans_handle *trans,
1153 struct btrfs_root *root,
1154 struct file *file,
1155 struct page **pages,
1156 size_t num_pages,
1157 loff_t pos,
1158 size_t write_bytes)
1159{
1160 int i;
1161 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1162 struct inode *inode = file->f_path.dentry->d_inode;
1163 int offset;
1164 int err = 0;
1165 int ret;
1166 int this_write;
1167 loff_t isize = i_size_read(inode);
1168
1169 memset(pages, 0, num_pages * sizeof(struct page *));
1170
1171 for (i = 0; i < num_pages; i++) {
1172 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1173 if (!pages[i]) {
1174 err = -ENOMEM;
1175 goto failed_release;
1176 }
1177 offset = pos & (PAGE_CACHE_SIZE -1);
1178 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1179 ret = nobh_prepare_write(pages[i], offset,
1180 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001181 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001182 pos += this_write;
1183 if (ret) {
1184 err = ret;
1185 goto failed_truncate;
1186 }
1187 WARN_ON(this_write > write_bytes);
1188 write_bytes -= this_write;
1189 }
1190 return 0;
1191
1192failed_release:
1193 btrfs_drop_pages(pages, num_pages);
1194 return err;
1195
1196failed_truncate:
1197 btrfs_drop_pages(pages, num_pages);
1198 if (pos > isize)
1199 vmtruncate(inode, isize);
1200 return err;
1201}
1202
1203static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1204 size_t count, loff_t *ppos)
1205{
1206 loff_t pos;
1207 size_t num_written = 0;
1208 int err = 0;
1209 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001210 struct inode *inode = file->f_path.dentry->d_inode;
1211 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1212 struct page *pages[1];
1213
1214 if (file->f_flags & O_DIRECT)
1215 return -EINVAL;
1216 pos = *ppos;
1217
1218 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1219 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1220 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1221 if (err)
1222 goto out;
1223 if (count == 0)
1224 goto out;
1225 err = remove_suid(file->f_path.dentry);
1226 if (err)
1227 goto out;
1228 file_update_time(file);
1229 mutex_lock(&inode->i_mutex);
1230 while(count > 0) {
1231 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1232 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1233 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1234 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001235 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001236 pos, write_bytes);
1237 BUG_ON(ret);
1238 ret = btrfs_copy_from_user(pos, num_pages,
1239 write_bytes, pages, buf);
1240 BUG_ON(ret);
1241
Chris Masonf254e522007-03-29 15:15:27 -04001242 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001243 num_pages, pos, write_bytes);
1244 BUG_ON(ret);
1245 btrfs_drop_pages(pages, num_pages);
1246
Chris Mason75dfe392007-03-29 11:56:46 -04001247 buf += write_bytes;
1248 count -= write_bytes;
1249 pos += write_bytes;
1250 num_written += write_bytes;
1251
1252 balance_dirty_pages_ratelimited(inode->i_mapping);
1253 cond_resched();
1254 }
1255 mutex_unlock(&inode->i_mutex);
1256out:
1257 *ppos = pos;
1258 current->backing_dev_info = NULL;
1259 return num_written ? num_written : err;
1260}
1261
Chris Masone8f05c42007-04-04 14:30:09 -04001262#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001263static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1264 struct page *page, loff_t pos,
1265 size_t offset, size_t write_bytes)
1266{
1267 struct btrfs_path *path;
1268 struct btrfs_trans_handle *trans;
1269 struct btrfs_key key;
1270 struct btrfs_leaf *leaf;
1271 struct btrfs_key found_key;
1272 int ret;
1273 size_t copy_size = 0;
1274 char *dst = NULL;
1275 int err = 0;
1276 size_t num_written = 0;
1277
1278 path = btrfs_alloc_path();
1279 BUG_ON(!path);
1280 mutex_lock(&root->fs_info->fs_mutex);
1281 trans = btrfs_start_transaction(root, 1);
1282 key.objectid = inode->i_ino;
1283 key.flags = 0;
1284 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1285
1286again:
1287 key.offset = pos;
1288 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1289 if (ret < 0) {
1290 err = ret;
1291 goto out;
1292 }
1293 if (ret == 0) {
1294 leaf = btrfs_buffer_leaf(path->nodes[0]);
1295 btrfs_disk_key_to_cpu(&found_key,
1296 &leaf->items[path->slots[0]].key);
1297 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1298 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1299 copy_size = min(write_bytes, copy_size);
1300 goto copyit;
1301 } else {
1302 int slot = path->slots[0];
1303 if (slot > 0) {
1304 slot--;
1305 }
1306 // FIXME find max key
1307 leaf = btrfs_buffer_leaf(path->nodes[0]);
1308 btrfs_disk_key_to_cpu(&found_key,
1309 &leaf->items[slot].key);
1310 if (found_key.objectid != inode->i_ino)
1311 goto insert;
1312 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1313 goto insert;
1314 copy_size = btrfs_item_size(leaf->items + slot);
1315 if (found_key.offset + copy_size <= pos)
1316 goto insert;
1317 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1318 dst += pos - found_key.offset;
1319 copy_size = copy_size - (pos - found_key.offset);
1320 BUG_ON(copy_size < 0);
1321 copy_size = min(write_bytes, copy_size);
1322 WARN_ON(copy_size == 0);
1323 goto copyit;
1324 }
1325insert:
1326 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001327 copy_size = min(write_bytes,
1328 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1329 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001330 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1331 BUG_ON(ret);
1332 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1333 path->slots[0], char);
1334copyit:
1335 WARN_ON(copy_size == 0);
1336 WARN_ON(dst + copy_size >
1337 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1338 path->slots[0], char) +
1339 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1340 path->slots[0]));
1341 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1342 page_address(page) + offset, copy_size);
1343 mark_buffer_dirty(path->nodes[0]);
1344 btrfs_release_path(root, path);
1345 pos += copy_size;
1346 offset += copy_size;
1347 num_written += copy_size;
1348 write_bytes -= copy_size;
1349 if (write_bytes)
1350 goto again;
1351out:
1352 btrfs_free_path(path);
1353 ret = btrfs_end_transaction(trans, root);
1354 BUG_ON(ret);
1355 mutex_unlock(&root->fs_info->fs_mutex);
1356 return num_written ? num_written : err;
1357}
1358
1359static ssize_t btrfs_file_inline_write(struct file *file,
1360 const char __user *buf,
1361 size_t count, loff_t *ppos)
1362{
1363 loff_t pos;
1364 size_t num_written = 0;
1365 int err = 0;
1366 int ret = 0;
1367 struct inode *inode = file->f_path.dentry->d_inode;
1368 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1369 unsigned long page_index;
1370
1371 if (file->f_flags & O_DIRECT)
1372 return -EINVAL;
1373 pos = *ppos;
1374
1375 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1376 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1377 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1378 if (err)
1379 goto out;
1380 if (count == 0)
1381 goto out;
1382 err = remove_suid(file->f_path.dentry);
1383 if (err)
1384 goto out;
1385 file_update_time(file);
1386 mutex_lock(&inode->i_mutex);
1387 while(count > 0) {
1388 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1389 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1390 struct page *page;
1391
1392 page_index = pos >> PAGE_CACHE_SHIFT;
1393 page = grab_cache_page(inode->i_mapping, page_index);
1394 if (!PageUptodate(page)) {
1395 ret = mpage_readpage(page, btrfs_get_block);
1396 BUG_ON(ret);
1397 lock_page(page);
1398 }
1399 ret = btrfs_copy_from_user(pos, 1,
1400 write_bytes, &page, buf);
1401 BUG_ON(ret);
1402 write_bytes = inline_one_page(root, inode, page, pos,
1403 offset, write_bytes);
1404 SetPageUptodate(page);
1405 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1406 i_size_write(inode, pos + write_bytes);
1407 mark_inode_dirty(inode);
1408 }
1409 page_cache_release(page);
1410 unlock_page(page);
1411 if (write_bytes < 0)
1412 goto out_unlock;
1413 buf += write_bytes;
1414 count -= write_bytes;
1415 pos += write_bytes;
1416 num_written += write_bytes;
1417
1418 balance_dirty_pages_ratelimited(inode->i_mapping);
1419 cond_resched();
1420 }
1421out_unlock:
1422 mutex_unlock(&inode->i_mutex);
1423out:
1424 *ppos = pos;
1425 current->backing_dev_info = NULL;
1426 return num_written ? num_written : err;
1427}
Chris Masone8f05c42007-04-04 14:30:09 -04001428#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001429
Chris Masonf254e522007-03-29 15:15:27 -04001430static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1431 unsigned long offset, unsigned long size)
1432{
1433 char *kaddr;
1434 unsigned long left, count = desc->count;
1435
1436 if (size > count)
1437 size = count;
1438
1439 if (!PageChecked(page)) {
1440 /* FIXME, do it per block */
1441 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1442 int ret = btrfs_csum_verify_file_block(root,
1443 page->mapping->host->i_ino,
1444 page->index << PAGE_CACHE_SHIFT,
1445 kmap(page), PAGE_CACHE_SIZE);
1446 if (ret) {
1447 printk("failed to verify ino %lu page %lu\n",
1448 page->mapping->host->i_ino,
1449 page->index);
1450 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1451 }
1452 SetPageChecked(page);
1453 kunmap(page);
1454 }
1455 /*
1456 * Faults on the destination of a read are common, so do it before
1457 * taking the kmap.
1458 */
1459 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1460 kaddr = kmap_atomic(page, KM_USER0);
1461 left = __copy_to_user_inatomic(desc->arg.buf,
1462 kaddr + offset, size);
1463 kunmap_atomic(kaddr, KM_USER0);
1464 if (left == 0)
1465 goto success;
1466 }
1467
1468 /* Do it the slow way */
1469 kaddr = kmap(page);
1470 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1471 kunmap(page);
1472
1473 if (left) {
1474 size -= left;
1475 desc->error = -EFAULT;
1476 }
1477success:
1478 desc->count = count - size;
1479 desc->written += size;
1480 desc->arg.buf += size;
1481 return size;
1482}
1483
1484/**
1485 * btrfs_file_aio_read - filesystem read routine
1486 * @iocb: kernel I/O control block
1487 * @iov: io vector request
1488 * @nr_segs: number of segments in the iovec
1489 * @pos: current file position
1490 */
1491static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1492 unsigned long nr_segs, loff_t pos)
1493{
1494 struct file *filp = iocb->ki_filp;
1495 ssize_t retval;
1496 unsigned long seg;
1497 size_t count;
1498 loff_t *ppos = &iocb->ki_pos;
1499
1500 count = 0;
1501 for (seg = 0; seg < nr_segs; seg++) {
1502 const struct iovec *iv = &iov[seg];
1503
1504 /*
1505 * If any segment has a negative length, or the cumulative
1506 * length ever wraps negative then return -EINVAL.
1507 */
1508 count += iv->iov_len;
1509 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1510 return -EINVAL;
1511 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1512 continue;
1513 if (seg == 0)
1514 return -EFAULT;
1515 nr_segs = seg;
1516 count -= iv->iov_len; /* This segment is no good */
1517 break;
1518 }
1519 retval = 0;
1520 if (count) {
1521 for (seg = 0; seg < nr_segs; seg++) {
1522 read_descriptor_t desc;
1523
1524 desc.written = 0;
1525 desc.arg.buf = iov[seg].iov_base;
1526 desc.count = iov[seg].iov_len;
1527 if (desc.count == 0)
1528 continue;
1529 desc.error = 0;
1530 do_generic_file_read(filp, ppos, &desc,
1531 btrfs_read_actor);
1532 retval += desc.written;
1533 if (desc.error) {
1534 retval = retval ?: desc.error;
1535 break;
1536 }
1537 }
1538 }
1539 return retval;
1540}
1541
Chris Mason2c90e5d2007-04-02 10:50:19 -04001542static struct kmem_cache *btrfs_inode_cachep;
1543struct kmem_cache *btrfs_trans_handle_cachep;
1544struct kmem_cache *btrfs_transaction_cachep;
1545struct kmem_cache *btrfs_bit_radix_cachep;
1546struct kmem_cache *btrfs_path_cachep;
1547
1548/*
1549 * Called inside transaction, so use GFP_NOFS
1550 */
1551static struct inode *btrfs_alloc_inode(struct super_block *sb)
1552{
1553 struct btrfs_inode *ei;
1554
1555 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1556 if (!ei)
1557 return NULL;
1558 ei->magic = 0xDEADBEEF;
1559 ei->magic2 = 0xDEADBEAF;
1560 return &ei->vfs_inode;
1561}
1562
1563static void btrfs_destroy_inode(struct inode *inode)
1564{
1565 struct btrfs_inode *ei = BTRFS_I(inode);
1566 WARN_ON(ei->magic != 0xDEADBEEF);
1567 WARN_ON(ei->magic2 != 0xDEADBEAF);
1568 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001569 WARN_ON(inode->i_data.nrpages);
1570
1571 ei->magic = 0;
1572 ei->magic2 = 0;
1573 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1574}
1575
1576static void init_once(void * foo, struct kmem_cache * cachep,
1577 unsigned long flags)
1578{
1579 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1580
1581 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1582 SLAB_CTOR_CONSTRUCTOR) {
1583 inode_init_once(&ei->vfs_inode);
1584 }
1585}
1586
1587static int init_inodecache(void)
1588{
1589 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1590 sizeof(struct btrfs_inode),
1591 0, (SLAB_RECLAIM_ACCOUNT|
1592 SLAB_MEM_SPREAD),
1593 init_once, NULL);
1594 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1595 sizeof(struct btrfs_trans_handle),
1596 0, (SLAB_RECLAIM_ACCOUNT|
1597 SLAB_MEM_SPREAD),
1598 NULL, NULL);
1599 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1600 sizeof(struct btrfs_transaction),
1601 0, (SLAB_RECLAIM_ACCOUNT|
1602 SLAB_MEM_SPREAD),
1603 NULL, NULL);
1604 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1605 sizeof(struct btrfs_transaction),
1606 0, (SLAB_RECLAIM_ACCOUNT|
1607 SLAB_MEM_SPREAD),
1608 NULL, NULL);
1609 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1610 256,
1611 0, (SLAB_RECLAIM_ACCOUNT|
1612 SLAB_MEM_SPREAD |
1613 SLAB_DESTROY_BY_RCU),
1614 NULL, NULL);
1615 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1616 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1617 return -ENOMEM;
1618 return 0;
1619}
1620
1621static void destroy_inodecache(void)
1622{
1623 kmem_cache_destroy(btrfs_inode_cachep);
1624 kmem_cache_destroy(btrfs_trans_handle_cachep);
1625 kmem_cache_destroy(btrfs_transaction_cachep);
1626 kmem_cache_destroy(btrfs_bit_radix_cachep);
1627 kmem_cache_destroy(btrfs_path_cachep);
1628}
1629
Chris Mason2e635a22007-03-21 11:12:56 -04001630static int btrfs_get_sb(struct file_system_type *fs_type,
1631 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1632{
1633 return get_sb_bdev(fs_type, flags, dev_name, data,
1634 btrfs_fill_super, mnt);
1635}
1636
1637static struct file_system_type btrfs_fs_type = {
1638 .owner = THIS_MODULE,
1639 .name = "btrfs",
1640 .get_sb = btrfs_get_sb,
1641 .kill_sb = kill_block_super,
1642 .fs_flags = FS_REQUIRES_DEV,
1643};
1644
Chris Masone20d96d2007-03-22 12:13:20 -04001645static struct super_operations btrfs_super_ops = {
1646 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001647 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001648 .put_super = btrfs_put_super,
1649 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001650 .write_super = btrfs_write_super,
1651 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001652 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001653 .alloc_inode = btrfs_alloc_inode,
1654 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001655};
1656
1657static struct inode_operations btrfs_dir_inode_operations = {
1658 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001659 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001660 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001661 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001662 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001663};
1664
1665static struct file_operations btrfs_dir_file_operations = {
1666 .llseek = generic_file_llseek,
1667 .read = generic_read_dir,
1668 .readdir = btrfs_readdir,
1669};
1670
Chris Masondee26a92007-03-26 16:00:06 -04001671static struct address_space_operations btrfs_aops = {
1672 .readpage = btrfs_readpage,
Chris Masone8f05c42007-04-04 14:30:09 -04001673 .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001674 .writepage = btrfs_writepage,
1675 .sync_page = block_sync_page,
1676 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001677 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001678};
1679
1680static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001681 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001682};
1683
1684static struct file_operations btrfs_file_operations = {
1685 .llseek = generic_file_llseek,
1686 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04001687 .aio_read = btrfs_file_aio_read,
1688 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001689 .mmap = generic_file_mmap,
1690 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001691};
Chris Masone20d96d2007-03-22 12:13:20 -04001692
Chris Mason2e635a22007-03-21 11:12:56 -04001693static int __init init_btrfs_fs(void)
1694{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001695 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001696 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001697 err = init_inodecache();
1698 if (err)
1699 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001700 return register_filesystem(&btrfs_fs_type);
1701}
1702
1703static void __exit exit_btrfs_fs(void)
1704{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001705 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001706 unregister_filesystem(&btrfs_fs_type);
1707 printk("btrfs unloaded\n");
1708}
1709
1710module_init(init_btrfs_fs)
1711module_exit(exit_btrfs_fs)
1712
1713MODULE_LICENSE("GPL");