blob: 8f07f462236a40d80f642ca77217ef812f01da32 [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 Mason2e635a22007-03-21 11:12:56 -040012#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040013#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040014#include "transaction.h"
Chris Mason2e635a22007-03-21 11:12:56 -040015
16#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040017
18static struct inode_operations btrfs_dir_inode_operations;
19static struct super_operations btrfs_super_ops;
20static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040021static struct inode_operations btrfs_file_inode_operations;
22static struct address_space_operations btrfs_aops;
23static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040024
Chris Masone20d96d2007-03-22 12:13:20 -040025static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040026{
Chris Masone20d96d2007-03-22 12:13:20 -040027 struct btrfs_path path;
28 struct btrfs_inode_item *inode_item;
29 struct btrfs_root *root = btrfs_sb(inode->i_sb);
30 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040031
Chris Masone20d96d2007-03-22 12:13:20 -040032 btrfs_init_path(&path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040033 mutex_lock(&root->fs_info->fs_mutex);
34
Chris Masone20d96d2007-03-22 12:13:20 -040035 ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0);
36 if (ret) {
Chris Mason78fae272007-03-25 11:35:08 -040037 btrfs_release_path(root, &path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040038 mutex_unlock(&root->fs_info->fs_mutex);
39 make_bad_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040040 return;
Chris Mason2e635a22007-03-21 11:12:56 -040041 }
Chris Masone20d96d2007-03-22 12:13:20 -040042 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
43 path.slots[0],
44 struct btrfs_inode_item);
45
Chris Masone20d96d2007-03-22 12:13:20 -040046 inode->i_mode = btrfs_inode_mode(inode_item);
47 inode->i_nlink = btrfs_inode_nlink(inode_item);
48 inode->i_uid = btrfs_inode_uid(inode_item);
49 inode->i_gid = btrfs_inode_gid(inode_item);
50 inode->i_size = btrfs_inode_size(inode_item);
51 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
52 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
53 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
54 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
55 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
56 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
57 inode->i_blocks = btrfs_inode_nblocks(inode_item);
58 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Masone20d96d2007-03-22 12:13:20 -040059 btrfs_release_path(root, &path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040060 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -040061 switch (inode->i_mode & S_IFMT) {
62#if 0
63 default:
64 init_special_inode(inode, inode->i_mode,
65 btrfs_inode_rdev(inode_item));
66 break;
67#endif
68 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -040069 inode->i_mapping->a_ops = &btrfs_aops;
70 inode->i_fop = &btrfs_file_operations;
71 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040072 break;
73 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -040074 inode->i_op = &btrfs_dir_inode_operations;
75 inode->i_fop = &btrfs_dir_file_operations;
76 break;
77 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -040078 // inode->i_op = &page_symlink_inode_operations;
79 break;
80 }
Chris Masone20d96d2007-03-22 12:13:20 -040081 return;
Chris Mason2e635a22007-03-21 11:12:56 -040082}
83
Chris Mason5f443fd2007-03-27 13:42:32 -040084static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root,
86 struct inode *dir,
87 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -040088{
89 struct btrfs_path path;
Chris Mason134e9732007-03-25 13:44:56 -040090 const char *name = dentry->d_name.name;
91 int name_len = dentry->d_name.len;
92 int ret;
93 u64 objectid;
94 struct btrfs_dir_item *di;
95
96 btrfs_init_path(&path);
Chris Mason134e9732007-03-25 13:44:56 -040097 ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino,
98 name, name_len, -1);
99 if (ret < 0)
100 goto err;
101 if (ret > 0) {
102 ret = -ENOENT;
103 goto err;
104 }
105 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
106 struct btrfs_dir_item);
107 objectid = btrfs_dir_objectid(di);
108
109 ret = btrfs_del_item(trans, root, &path);
110 BUG_ON(ret);
111 dentry->d_inode->i_ctime = dir->i_ctime;
112err:
113 btrfs_release_path(root, &path);
Chris Mason134e9732007-03-25 13:44:56 -0400114 if (ret == 0)
115 inode_dec_link_count(dentry->d_inode);
116 return ret;
117}
118
Chris Mason5f443fd2007-03-27 13:42:32 -0400119static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
120{
121 struct btrfs_root *root;
122 struct btrfs_trans_handle *trans;
123 int ret;
124
125 root = btrfs_sb(dir->i_sb);
126 mutex_lock(&root->fs_info->fs_mutex);
127 trans = btrfs_start_transaction(root, 1);
128 ret = btrfs_unlink_trans(trans, root, dir, dentry);
129 btrfs_end_transaction(trans, root);
130 mutex_unlock(&root->fs_info->fs_mutex);
131 return ret;
132}
133
134static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
135{
136 struct inode *inode = dentry->d_inode;
137 int err;
138 int ret;
139 struct btrfs_root *root = btrfs_sb(dir->i_sb);
140 struct btrfs_path path;
141 struct btrfs_key key;
142 struct btrfs_trans_handle *trans;
143 struct btrfs_disk_key *found_key;
144 struct btrfs_leaf *leaf;
145
146 btrfs_init_path(&path);
147 mutex_lock(&root->fs_info->fs_mutex);
148 trans = btrfs_start_transaction(root, 1);
149 key.objectid = inode->i_ino;
150 key.offset = (u64)-1;
151 key.flags = 0;
152 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
153 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
154 if (ret < 0) {
155 err = ret;
156 goto out;
157 }
158
159 BUG_ON(ret == 0);
160 BUG_ON(path.slots[0] == 0);
161 path.slots[0]--;
162 leaf = btrfs_buffer_leaf(path.nodes[0]);
163 found_key = &leaf->items[path.slots[0]].key;
164 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
165 err = -ENOENT;
166 goto out;
167 }
168 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
169 btrfs_disk_key_offset(found_key) != 2) {
170 err = -ENOTEMPTY;
171 goto out;
172 }
173 ret = btrfs_del_item(trans, root, &path);
174 BUG_ON(ret);
175 btrfs_release_path(root, &path);
176 key.offset = 1;
177 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
178 if (ret < 0) {
179 err = ret;
180 goto out;
181 }
182 if (ret > 0) {
183 err = -ENOTEMPTY;
184 goto out;
185 }
186 ret = btrfs_del_item(trans, root, &path);
187 if (ret) {
188 err = ret;
189 goto out;
190 }
191 btrfs_release_path(root, &path);
192
193 /* now the directory is empty */
194 err = btrfs_unlink_trans(trans, root, dir, dentry);
195 if (!err) {
196 inode->i_size = 0;
197 }
198out:
199 mutex_unlock(&root->fs_info->fs_mutex);
200 ret = btrfs_end_transaction(trans, root);
201 if (ret && !err)
202 err = ret;
203 return err;
204}
205
Chris Mason134e9732007-03-25 13:44:56 -0400206static int btrfs_free_inode(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root,
208 struct inode *inode)
209{
210 u64 objectid = inode->i_ino;
211 struct btrfs_path path;
212 struct btrfs_inode_map_item *map;
213 struct btrfs_key stat_data_key;
214 int ret;
215 clear_inode(inode);
216 btrfs_init_path(&path);
217 ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1);
218 if (ret) {
219 if (ret > 0)
220 ret = -ENOENT;
221 btrfs_release_path(root, &path);
222 goto error;
223 }
224 map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
225 struct btrfs_inode_map_item);
226 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
227 ret = btrfs_del_item(trans, root->fs_info->inode_root, &path);
228 BUG_ON(ret);
229 btrfs_release_path(root, &path);
230 btrfs_init_path(&path);
231
232 ret = btrfs_lookup_inode(trans, root, &path, objectid, -1);
233 BUG_ON(ret);
234 ret = btrfs_del_item(trans, root, &path);
235 BUG_ON(ret);
236 btrfs_release_path(root, &path);
237error:
238 return ret;
239}
240
Chris Masonf4b9aa82007-03-27 11:05:53 -0400241static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
242 struct btrfs_root *root,
243 struct inode *inode)
244{
245 int ret;
246 struct btrfs_path path;
247 struct btrfs_key key;
248 struct btrfs_disk_key *found_key;
249 struct btrfs_leaf *leaf;
250 struct btrfs_file_extent_item *fi;
251 u64 extent_start;
252 u64 extent_num_blocks;
253
254 /* FIXME, add redo link to tree so we don't leak on crash */
255 key.objectid = inode->i_ino;
256 key.offset = (u64)-1;
257 key.flags = 0;
258 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
259 while(1) {
260 btrfs_init_path(&path);
261 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
262 if (ret < 0) {
263 btrfs_release_path(root, &path);
264 goto error;
265 }
266 if (ret > 0) {
267 BUG_ON(path.slots[0] == 0);
268 path.slots[0]--;
269 }
270 leaf = btrfs_buffer_leaf(path.nodes[0]);
271 found_key = &leaf->items[path.slots[0]].key;
272 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
273 break;
274 if (btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
275 break;
276 if (btrfs_disk_key_offset(found_key) < inode->i_size)
277 break;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400278 fi = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
279 path.slots[0],
280 struct btrfs_file_extent_item);
281 extent_start = btrfs_file_extent_disk_blocknr(fi);
282 extent_num_blocks = btrfs_file_extent_disk_num_blocks(fi);
283 key.offset = btrfs_disk_key_offset(found_key) - 1;
284 ret = btrfs_del_item(trans, root, &path);
285 BUG_ON(ret);
286 inode->i_blocks -= btrfs_file_extent_num_blocks(fi) >> 9;
287 btrfs_release_path(root, &path);
288 ret = btrfs_free_extent(trans, root, extent_start,
289 extent_num_blocks, 0);
290 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400291 if (key.offset + 1 == 0)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400292 break;
293 }
294 btrfs_release_path(root, &path);
295 ret = 0;
296error:
297 return ret;
298}
299
Chris Mason134e9732007-03-25 13:44:56 -0400300static void btrfs_delete_inode(struct inode *inode)
301{
302 struct btrfs_trans_handle *trans;
303 struct btrfs_root *root = btrfs_sb(inode->i_sb);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400304 int ret;
305
Chris Mason134e9732007-03-25 13:44:56 -0400306 truncate_inode_pages(&inode->i_data, 0);
307 if (is_bad_inode(inode)) {
308 goto no_delete;
309 }
310 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400311 mutex_lock(&root->fs_info->fs_mutex);
312 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400313 if (S_ISREG(inode->i_mode)) {
314 ret = btrfs_truncate_in_trans(trans, root, inode);
315 BUG_ON(ret);
316 }
Chris Mason134e9732007-03-25 13:44:56 -0400317 btrfs_free_inode(trans, root, inode);
318 btrfs_end_transaction(trans, root);
319 mutex_unlock(&root->fs_info->fs_mutex);
320 return;
321no_delete:
322 clear_inode(inode);
323}
324
Chris Masone20d96d2007-03-22 12:13:20 -0400325static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
326 ino_t *ino)
327{
328 const char *name = dentry->d_name.name;
329 int namelen = dentry->d_name.len;
330 struct btrfs_dir_item *di;
331 struct btrfs_path path;
332 struct btrfs_root *root = btrfs_sb(dir->i_sb);
333 int ret;
334
335 btrfs_init_path(&path);
336 ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
337 namelen, 0);
Chris Mason7f5c1512007-03-23 15:56:19 -0400338 if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400339 *ino = 0;
340 goto out;
341 }
342 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
343 struct btrfs_dir_item);
344 *ino = btrfs_dir_objectid(di);
345out:
346 btrfs_release_path(root, &path);
347 return ret;
348}
349
350static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
351 struct nameidata *nd)
352{
353 struct inode * inode;
354 ino_t ino;
355 int ret;
356
357 if (dentry->d_name.len > BTRFS_NAME_LEN)
358 return ERR_PTR(-ENAMETOOLONG);
359
360 ret = btrfs_inode_by_name(dir, dentry, &ino);
361 if (ret < 0)
362 return ERR_PTR(ret);
363 inode = NULL;
364 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400365 inode = iget(dir->i_sb, ino);
366 if (!inode)
367 return ERR_PTR(-EACCES);
368 }
369 return d_splice_alias(inode, dentry);
370}
371
372static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
373{
374 struct inode *inode = filp->f_path.dentry->d_inode;
375 struct btrfs_root *root = btrfs_sb(inode->i_sb);
376 struct btrfs_item *item;
377 struct btrfs_dir_item *di;
378 struct btrfs_key key;
379 struct btrfs_path path;
380 int ret;
381 u32 nritems;
382 struct btrfs_leaf *leaf;
383 int slot;
384 int advance;
385 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400386 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400387
388 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400389 key.flags = 0;
390 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
391 key.offset = filp->f_pos;
392 btrfs_init_path(&path);
393 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
394 if (ret < 0) {
395 goto err;
396 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400397 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400398 while(1) {
399 leaf = btrfs_buffer_leaf(path.nodes[0]);
400 nritems = btrfs_header_nritems(&leaf->header);
401 slot = path.slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400402 if (advance || slot >= nritems) {
403 if (slot >= nritems -1) {
Chris Masone20d96d2007-03-22 12:13:20 -0400404 ret = btrfs_next_leaf(root, &path);
405 if (ret)
406 break;
407 leaf = btrfs_buffer_leaf(path.nodes[0]);
408 nritems = btrfs_header_nritems(&leaf->header);
409 slot = path.slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400410 } else {
411 slot++;
412 path.slots[0]++;
413 }
414 }
415 advance = 1;
416 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400417 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
418 break;
419 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
420 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400421 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
422 continue;
Chris Masondee26a92007-03-26 16:00:06 -0400423
424 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400425 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400426 over = filldir(dirent, (const char *)(di + 1),
427 btrfs_dir_name_len(di),
428 btrfs_disk_key_offset(&item->key),
429 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400430 if (over) {
431 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400432 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400433 }
Chris Masone20d96d2007-03-22 12:13:20 -0400434 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
435 }
Chris Masone20d96d2007-03-22 12:13:20 -0400436 ret = 0;
437err:
438 btrfs_release_path(root, &path);
439 return ret;
440}
441
442static void btrfs_put_super (struct super_block * sb)
443{
444 struct btrfs_root *root = btrfs_sb(sb);
445 int ret;
446
447 ret = close_ctree(root);
448 if (ret) {
449 printk("close ctree returns %d\n", ret);
450 }
451 sb->s_fs_info = NULL;
452}
Chris Mason2e635a22007-03-21 11:12:56 -0400453
454static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
455{
456 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400457 struct dentry * root_dentry;
458 struct btrfs_super_block *disk_super;
459 struct buffer_head *bh;
460 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400461
462 sb->s_maxbytes = MAX_LFS_FILESIZE;
463 sb->s_blocksize = PAGE_CACHE_SIZE;
464 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
465 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400466 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400467 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400468
469 bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize);
470 if (!bh) {
471 printk("btrfs: unable to read on disk super\n");
472 return -EIO;
473 }
474 disk_super = (struct btrfs_super_block *)bh->b_data;
475 root = open_ctree(sb, bh, disk_super);
476 sb->s_fs_info = root;
Chris Masond98237b2007-03-28 13:57:48 -0400477 disk_super = root->fs_info->disk_super;
478
Chris Masone20d96d2007-03-22 12:13:20 -0400479 if (!root) {
480 printk("btrfs: open_ctree failed\n");
481 return -EIO;
482 }
483 printk("read in super total blocks %Lu root %Lu\n",
484 btrfs_super_total_blocks(disk_super),
485 btrfs_super_root_dir(disk_super));
486
487 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400488 if (!inode)
489 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400490 if (inode->i_state & I_NEW) {
491 btrfs_read_locked_inode(inode);
492 unlock_new_inode(inode);
493 }
Chris Mason2e635a22007-03-21 11:12:56 -0400494
Chris Masone20d96d2007-03-22 12:13:20 -0400495 root_dentry = d_alloc_root(inode);
496 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400497 iput(inode);
498 return -ENOMEM;
499 }
Chris Masone20d96d2007-03-22 12:13:20 -0400500 sb->s_root = root_dentry;
501
Chris Mason2e635a22007-03-21 11:12:56 -0400502 return 0;
503}
504
Chris Masond5719762007-03-23 10:01:08 -0400505static void fill_inode_item(struct btrfs_inode_item *item,
506 struct inode *inode)
507{
508 btrfs_set_inode_uid(item, inode->i_uid);
509 btrfs_set_inode_gid(item, inode->i_gid);
510 btrfs_set_inode_size(item, inode->i_size);
511 btrfs_set_inode_mode(item, inode->i_mode);
512 btrfs_set_inode_nlink(item, inode->i_nlink);
513 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
514 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
515 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
516 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
517 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
518 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
519 btrfs_set_inode_nblocks(item, inode->i_blocks);
520 btrfs_set_inode_generation(item, inode->i_generation);
521}
522
Chris Mason4730a4b2007-03-26 12:00:39 -0400523static int btrfs_update_inode(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct inode *inode)
526{
527 struct btrfs_inode_item *inode_item;
528 struct btrfs_path path;
529 int ret;
530
531 btrfs_init_path(&path);
532
533 ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1);
534 if (ret) {
535 if (ret > 0)
536 ret = -ENOENT;
537 goto failed;
538 }
539
540 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
541 path.slots[0],
542 struct btrfs_inode_item);
543
544 fill_inode_item(inode_item, inode);
545 mark_buffer_dirty(path.nodes[0]);
546failed:
547 btrfs_release_path(root, &path);
548 return 0;
549}
550
551static int btrfs_write_inode(struct inode *inode, int wait)
552{
553 struct btrfs_root *root = btrfs_sb(inode->i_sb);
554 struct btrfs_trans_handle *trans;
555 int ret;
556
557 mutex_lock(&root->fs_info->fs_mutex);
558 trans = btrfs_start_transaction(root, 1);
559 ret = btrfs_update_inode(trans, root, inode);
560 if (wait)
561 btrfs_commit_transaction(trans, root);
562 else
563 btrfs_end_transaction(trans, root);
564 mutex_unlock(&root->fs_info->fs_mutex);
565 return ret;
566}
567
Chris Masond5719762007-03-23 10:01:08 -0400568static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
569 struct inode *dir, int mode)
570{
571 struct inode *inode;
572 struct btrfs_inode_item inode_item;
573 struct btrfs_root *root = btrfs_sb(dir->i_sb);
574 struct btrfs_key key;
575 int ret;
576 u64 objectid;
577
578 inode = new_inode(dir->i_sb);
579 if (!inode)
580 return ERR_PTR(-ENOMEM);
581
582 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
583 BUG_ON(ret);
584
585 inode->i_uid = current->fsuid;
586 inode->i_gid = current->fsgid;
587 inode->i_mode = mode;
588 inode->i_ino = objectid;
589 inode->i_blocks = 0;
590 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
591 fill_inode_item(&inode_item, inode);
592
Chris Masond5719762007-03-23 10:01:08 -0400593 key.objectid = objectid;
594 key.flags = 0;
595 key.offset = 0;
596 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
597 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
598 BUG_ON(ret);
599
600 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
601 BUG_ON(ret);
602
603 insert_inode_hash(inode);
Chris Masond5719762007-03-23 10:01:08 -0400604 return inode;
605}
606
607static int btrfs_add_link(struct btrfs_trans_handle *trans,
608 struct dentry *dentry, struct inode *inode)
609{
610 int ret;
611 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
612 dentry->d_name.name, dentry->d_name.len,
613 dentry->d_parent->d_inode->i_ino,
614 inode->i_ino, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400615 if (ret == 0) {
616 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
617 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
618 dentry->d_parent->d_inode);
619 }
620
Chris Masond5719762007-03-23 10:01:08 -0400621 return ret;
622}
623
624static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
625 struct dentry *dentry, struct inode *inode)
626{
627 int err = btrfs_add_link(trans, dentry, inode);
628 if (!err) {
629 d_instantiate(dentry, inode);
630 return 0;
631 }
Chris Masond5719762007-03-23 10:01:08 -0400632 return err;
633}
634
635static int btrfs_create(struct inode *dir, struct dentry *dentry,
636 int mode, struct nameidata *nd)
637{
638 struct btrfs_trans_handle *trans;
639 struct btrfs_root *root = btrfs_sb(dir->i_sb);
640 struct inode *inode;
641 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400642 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400643
Chris Masond561c022007-03-23 19:47:49 -0400644 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400645 trans = btrfs_start_transaction(root, 1);
646 inode = btrfs_new_inode(trans, dir, mode);
647 err = PTR_ERR(inode);
648 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400649 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400650 // FIXME mark the inode dirty
651 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400652 if (err)
653 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400654 else {
655 inode->i_mapping->a_ops = &btrfs_aops;
656 inode->i_fop = &btrfs_file_operations;
657 inode->i_op = &btrfs_file_inode_operations;
658 }
Chris Masond5719762007-03-23 10:01:08 -0400659 dir->i_sb->s_dirt = 1;
660 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400661out_unlock:
662 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason134e9732007-03-25 13:44:56 -0400663 if (drop_inode) {
664 inode_dec_link_count(inode);
665 iput(inode);
666 }
Chris Masond5719762007-03-23 10:01:08 -0400667 return err;
668}
669
Chris Masonf7922032007-03-25 20:17:36 -0400670static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
671 struct inode *inode, struct inode *dir)
672{
673 struct btrfs_root *root = btrfs_sb(inode->i_sb);
674 int ret;
675 char buf[2];
676 buf[0] = '.';
677 buf[1] = '.';
678
679 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
680 inode->i_ino, 1);
681 if (ret)
682 goto error;
683 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
684 dir->i_ino, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400685 if (ret)
686 goto error;
687 inode->i_size = 3;
688 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400689error:
690 return ret;
691}
692
693static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
694{
695 struct inode *inode;
696 struct btrfs_trans_handle *trans;
697 struct btrfs_root *root = btrfs_sb(dir->i_sb);
698 int err = 0;
699 int drop_on_err = 0;
700
701 mutex_lock(&root->fs_info->fs_mutex);
702 trans = btrfs_start_transaction(root, 1);
703 if (IS_ERR(trans)) {
704 err = PTR_ERR(trans);
705 goto out_unlock;
706 }
707 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
708 if (IS_ERR(inode)) {
709 err = PTR_ERR(inode);
710 goto out_fail;
711 }
712 drop_on_err = 1;
713 inode->i_op = &btrfs_dir_inode_operations;
714 inode->i_fop = &btrfs_dir_file_operations;
715
716 err = btrfs_make_empty_dir(trans, inode, dir);
717 if (err)
718 goto out_fail;
719 err = btrfs_add_link(trans, dentry, inode);
720 if (err)
721 goto out_fail;
722 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400723 drop_on_err = 0;
724
725out_fail:
726 btrfs_end_transaction(trans, root);
727out_unlock:
728 mutex_unlock(&root->fs_info->fs_mutex);
729 if (drop_on_err)
730 iput(inode);
731 return err;
732}
733
Chris Masond5719762007-03-23 10:01:08 -0400734static int btrfs_sync_fs(struct super_block *sb, int wait)
735{
736 struct btrfs_trans_handle *trans;
737 struct btrfs_root *root;
738 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400739 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400740
Chris Masond5719762007-03-23 10:01:08 -0400741 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400742 if (!wait) {
Chris Masond98237b2007-03-28 13:57:48 -0400743 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400744 return 0;
745 }
Chris Masond98237b2007-03-28 13:57:48 -0400746 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masondf2ce342007-03-23 11:00:45 -0400747
Chris Masond561c022007-03-23 19:47:49 -0400748 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400749 trans = btrfs_start_transaction(root, 1);
750 ret = btrfs_commit_transaction(trans, root);
751 sb->s_dirt = 0;
752 BUG_ON(ret);
753printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400754 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400755 return 0;
756}
757
Chris Masondee26a92007-03-26 16:00:06 -0400758static int btrfs_get_block(struct inode *inode, sector_t iblock,
759 struct buffer_head *result, int create)
760{
761 int ret;
762 int err = 0;
763 u64 blocknr;
764 u64 extent_start = 0;
765 u64 extent_end = 0;
766 u64 objectid = inode->i_ino;
767 struct btrfs_path path;
768 struct btrfs_root *root = btrfs_sb(inode->i_sb);
769 struct btrfs_trans_handle *trans = NULL;
770 struct btrfs_file_extent_item *item;
771 struct btrfs_leaf *leaf;
772 struct btrfs_disk_key *found_key;
773
774 btrfs_init_path(&path);
775 mutex_lock(&root->fs_info->fs_mutex);
776 if (create)
777 trans = btrfs_start_transaction(root, 1);
778
779
780 ret = btrfs_lookup_file_extent(trans, root, &path,
Chris Mason9773a782007-03-27 11:26:26 -0400781 inode->i_ino,
782 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -0400783 if (ret < 0) {
784 btrfs_release_path(root, &path);
785 err = ret;
786 goto out;
787 }
788
789 if (ret != 0) {
790 if (path.slots[0] == 0) {
791 btrfs_release_path(root, &path);
792 goto allocate;
793 }
794 path.slots[0]--;
795 }
796
797 item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
798 struct btrfs_file_extent_item);
799 leaf = btrfs_buffer_leaf(path.nodes[0]);
800 blocknr = btrfs_file_extent_disk_blocknr(item);
801 blocknr += btrfs_file_extent_offset(item);
802
803 /* exact match found, use it */
804 if (ret == 0) {
805 err = 0;
806 map_bh(result, inode->i_sb, blocknr);
807 btrfs_release_path(root, &path);
808 goto out;
809 }
810
811 /* are we inside the extent that was found? */
812 found_key = &leaf->items[path.slots[0]].key;
813 if (btrfs_disk_key_objectid(found_key) != objectid ||
814 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
815 extent_end = 0;
816 extent_start = 0;
817 btrfs_release_path(root, &path);
818 goto allocate;
819 }
820
821 extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -0400822 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -0400823 extent_start += btrfs_file_extent_offset(item);
824 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
825 btrfs_release_path(root, &path);
826 if (iblock >= extent_start && iblock < extent_end) {
827 err = 0;
828 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
829 goto out;
830 }
831allocate:
832 /* ok, create a new extent */
833 if (!create) {
834 err = 0;
835 goto out;
836 }
Chris Mason9773a782007-03-27 11:26:26 -0400837 ret = btrfs_alloc_file_extent(trans, root, objectid,
838 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -0400839 1, extent_end, &blocknr);
840 if (ret) {
841 err = ret;
842 goto out;
843 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400844 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
845 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -0400846 map_bh(result, inode->i_sb, blocknr);
847
848out:
849 if (trans)
850 btrfs_end_transaction(trans, root);
851 mutex_unlock(&root->fs_info->fs_mutex);
852 return err;
853}
854
855static int btrfs_prepare_write(struct file *file, struct page *page,
856 unsigned from, unsigned to)
857{
Chris Mason6407bf62007-03-27 06:33:00 -0400858 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -0400859}
860
Chris Masond561c022007-03-23 19:47:49 -0400861static void btrfs_write_super(struct super_block *sb)
862{
863 btrfs_sync_fs(sb, 1);
864}
865
Chris Masondee26a92007-03-26 16:00:06 -0400866static int btrfs_readpage(struct file *file, struct page *page)
867{
868 return mpage_readpage(page, btrfs_get_block);
869}
870
871static int btrfs_readpages(struct file *file, struct address_space *mapping,
872 struct list_head *pages, unsigned nr_pages)
873{
874 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
875}
876
877static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
878{
Chris Mason6407bf62007-03-27 06:33:00 -0400879 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -0400880}
Chris Masond561c022007-03-23 19:47:49 -0400881
Chris Masonf4b9aa82007-03-27 11:05:53 -0400882static void btrfs_truncate(struct inode *inode)
883{
884 struct btrfs_root *root = btrfs_sb(inode->i_sb);
885 int ret;
886 struct btrfs_trans_handle *trans;
887
888 if (!S_ISREG(inode->i_mode))
889 return;
890 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
891 return;
892
893 nobh_truncate_page(inode->i_mapping, inode->i_size);
894
895 /* FIXME, add redo link to tree so we don't leak on crash */
896 mutex_lock(&root->fs_info->fs_mutex);
897 trans = btrfs_start_transaction(root, 1);
898 ret = btrfs_truncate_in_trans(trans, root, inode);
899 BUG_ON(ret);
900 ret = btrfs_end_transaction(trans, root);
901 BUG_ON(ret);
902 mutex_unlock(&root->fs_info->fs_mutex);
903 mark_inode_dirty(inode);
904}
905
Chris Mason2e635a22007-03-21 11:12:56 -0400906static int btrfs_get_sb(struct file_system_type *fs_type,
907 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
908{
909 return get_sb_bdev(fs_type, flags, dev_name, data,
910 btrfs_fill_super, mnt);
911}
912
913static struct file_system_type btrfs_fs_type = {
914 .owner = THIS_MODULE,
915 .name = "btrfs",
916 .get_sb = btrfs_get_sb,
917 .kill_sb = kill_block_super,
918 .fs_flags = FS_REQUIRES_DEV,
919};
920
Chris Masone20d96d2007-03-22 12:13:20 -0400921static struct super_operations btrfs_super_ops = {
922 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -0400923 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400924 .put_super = btrfs_put_super,
925 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400926 .write_super = btrfs_write_super,
927 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -0400928 .write_inode = btrfs_write_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400929};
930
931static struct inode_operations btrfs_dir_inode_operations = {
932 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -0400933 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -0400934 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -0400935 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -0400936 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -0400937};
938
939static struct file_operations btrfs_dir_file_operations = {
940 .llseek = generic_file_llseek,
941 .read = generic_read_dir,
942 .readdir = btrfs_readdir,
943};
944
Chris Masondee26a92007-03-26 16:00:06 -0400945static struct address_space_operations btrfs_aops = {
946 .readpage = btrfs_readpage,
947 .readpages = btrfs_readpages,
948 .writepage = btrfs_writepage,
949 .sync_page = block_sync_page,
950 .prepare_write = btrfs_prepare_write,
Chris Mason6407bf62007-03-27 06:33:00 -0400951 .commit_write = nobh_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -0400952};
953
954static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400955 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -0400956};
957
958static struct file_operations btrfs_file_operations = {
959 .llseek = generic_file_llseek,
960 .read = do_sync_read,
961 .write = do_sync_write,
962 .aio_read = generic_file_aio_read,
963 .aio_write = generic_file_aio_write,
964 .mmap = generic_file_mmap,
965 .open = generic_file_open,
966 .sendfile = generic_file_sendfile,
967 .splice_read = generic_file_splice_read,
968 .splice_write = generic_file_splice_write,
969};
Chris Masone20d96d2007-03-22 12:13:20 -0400970
Chris Mason2e635a22007-03-21 11:12:56 -0400971static int __init init_btrfs_fs(void)
972{
973 printk("btrfs loaded!\n");
974 return register_filesystem(&btrfs_fs_type);
975}
976
977static void __exit exit_btrfs_fs(void)
978{
979 unregister_filesystem(&btrfs_fs_type);
980 printk("btrfs unloaded\n");
981}
982
983module_init(init_btrfs_fs)
984module_exit(exit_btrfs_fs)
985
986MODULE_LICENSE("GPL");