Btrfs: support for items bigger than 1/2 the blocksize
[deliverable/linux.git] / fs / btrfs / super.c
... / ...
CommitLineData
1#include <linux/module.h>
2#include <linux/buffer_head.h>
3#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>
11#include <linux/mpage.h>
12#include <linux/swap.h>
13#include <linux/writeback.h>
14#include "ctree.h"
15#include "disk-io.h"
16#include "transaction.h"
17#include "btrfs_inode.h"
18
19#define BTRFS_SUPER_MAGIC 0x9123682E
20
21static struct inode_operations btrfs_dir_inode_operations;
22static struct super_operations btrfs_super_ops;
23static struct file_operations btrfs_dir_file_operations;
24static struct inode_operations btrfs_file_inode_operations;
25static struct address_space_operations btrfs_aops;
26static struct file_operations btrfs_file_operations;
27
28static 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
36static void btrfs_read_locked_inode(struct inode *inode)
37{
38 struct btrfs_path *path;
39 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = btrfs_sb(inode->i_sb);
41 int ret;
42
43 path = btrfs_alloc_path();
44 BUG_ON(!path);
45 btrfs_init_path(path);
46 mutex_lock(&root->fs_info->fs_mutex);
47
48 check_inode(inode);
49 ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
50 if (ret) {
51 btrfs_release_path(root, path);
52 btrfs_free_path(path);
53 mutex_unlock(&root->fs_info->fs_mutex);
54 make_bad_inode(inode);
55 return;
56 }
57 check_inode(inode);
58 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 path->slots[0],
60 struct btrfs_inode_item);
61
62 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);
75
76 btrfs_release_path(root, path);
77 btrfs_free_path(path);
78 inode_item = NULL;
79
80 mutex_unlock(&root->fs_info->fs_mutex);
81 check_inode(inode);
82 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:
90 inode->i_mapping->a_ops = &btrfs_aops;
91 inode->i_fop = &btrfs_file_operations;
92 inode->i_op = &btrfs_file_inode_operations;
93 break;
94 case S_IFDIR:
95 inode->i_op = &btrfs_dir_inode_operations;
96 inode->i_fop = &btrfs_dir_file_operations;
97 break;
98 case S_IFLNK:
99 // inode->i_op = &page_symlink_inode_operations;
100 break;
101 }
102 check_inode(inode);
103 return;
104}
105
106static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct inode *dir,
109 struct dentry *dentry)
110{
111 struct btrfs_path *path;
112 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
118 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,
122 name, name_len, -1);
123 if (ret < 0)
124 goto err;
125 if (ret > 0) {
126 ret = -ENOENT;
127 goto err;
128 }
129 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
130 struct btrfs_dir_item);
131 objectid = btrfs_dir_objectid(di);
132
133 ret = btrfs_del_item(trans, root, path);
134 BUG_ON(ret);
135 dentry->d_inode->i_ctime = dir->i_ctime;
136err:
137 btrfs_release_path(root, path);
138 btrfs_free_path(path);
139 if (ret == 0) {
140 inode_dec_link_count(dentry->d_inode);
141 dir->i_size -= name_len;
142 mark_inode_dirty(dir);
143 }
144 return ret;
145}
146
147static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
148{
149 struct btrfs_root *root;
150 struct btrfs_trans_handle *trans;
151 int ret;
152
153 root = btrfs_sb(dir->i_sb);
154 mutex_lock(&root->fs_info->fs_mutex);
155 trans = btrfs_start_transaction(root, 1);
156 ret = btrfs_unlink_trans(trans, root, dir, dentry);
157 btrfs_end_transaction(trans, root);
158 mutex_unlock(&root->fs_info->fs_mutex);
159 return ret;
160}
161
162static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
163{
164 struct inode *inode = dentry->d_inode;
165 int err;
166 int ret;
167 struct btrfs_root *root = btrfs_sb(dir->i_sb);
168 struct btrfs_path *path;
169 struct btrfs_key key;
170 struct btrfs_trans_handle *trans;
171 struct btrfs_disk_key *found_key;
172 struct btrfs_leaf *leaf;
173
174 path = btrfs_alloc_path();
175 BUG_ON(!path);
176 btrfs_init_path(path);
177 mutex_lock(&root->fs_info->fs_mutex);
178 trans = btrfs_start_transaction(root, 1);
179 key.objectid = inode->i_ino;
180 key.offset = (u64)-1;
181 key.flags = 0;
182 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
183 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
184 if (ret < 0) {
185 err = ret;
186 goto out;
187 }
188
189 BUG_ON(ret == 0);
190 BUG_ON(path->slots[0] == 0);
191 path->slots[0]--;
192 leaf = btrfs_buffer_leaf(path->nodes[0]);
193 found_key = &leaf->items[path->slots[0]].key;
194 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
195 err = -ENOENT;
196 goto out;
197 }
198 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
199 btrfs_disk_key_offset(found_key) != 2) {
200 err = -ENOTEMPTY;
201 goto out;
202 }
203 ret = btrfs_del_item(trans, root, path);
204 BUG_ON(ret);
205 btrfs_release_path(root, path);
206 key.offset = 1;
207 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
208 if (ret < 0) {
209 err = ret;
210 goto out;
211 }
212 if (ret > 0) {
213 err = -ENOTEMPTY;
214 goto out;
215 }
216 ret = btrfs_del_item(trans, root, path);
217 if (ret) {
218 err = ret;
219 goto out;
220 }
221 btrfs_release_path(root, path);
222
223 /* now the directory is empty */
224 err = btrfs_unlink_trans(trans, root, dir, dentry);
225 if (!err) {
226 inode->i_size = 0;
227 }
228out:
229 btrfs_release_path(root, path);
230 btrfs_free_path(path);
231 mutex_unlock(&root->fs_info->fs_mutex);
232 ret = btrfs_end_transaction(trans, root);
233 if (ret && !err)
234 err = ret;
235 return err;
236}
237
238static int btrfs_free_inode(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root,
240 struct inode *inode)
241{
242 u64 objectid = inode->i_ino;
243 struct btrfs_path *path;
244 struct btrfs_inode_map_item *map;
245 struct btrfs_key stat_data_key;
246 int ret;
247
248 clear_inode(inode);
249
250 path = btrfs_alloc_path();
251 BUG_ON(!path);
252 btrfs_init_path(path);
253 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
254 if (ret) {
255 if (ret > 0)
256 ret = -ENOENT;
257 goto error;
258 }
259 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
260 struct btrfs_inode_map_item);
261 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
262 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
263 BUG_ON(ret);
264 btrfs_release_path(root, path);
265
266 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
267 BUG_ON(ret);
268 ret = btrfs_del_item(trans, root, path);
269 BUG_ON(ret);
270error:
271 btrfs_release_path(root, path);
272 btrfs_free_path(path);
273 return ret;
274}
275
276static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
277 struct btrfs_root *root,
278 struct inode *inode)
279{
280 int ret;
281 struct btrfs_path *path;
282 struct btrfs_key key;
283 struct btrfs_disk_key *found_key;
284 struct btrfs_leaf *leaf;
285 struct btrfs_file_extent_item *fi = NULL;
286 u64 extent_start = 0;
287 u64 extent_num_blocks = 0;
288 int found_extent;
289
290 path = btrfs_alloc_path();
291 BUG_ON(!path);
292 /* FIXME, add redo link to tree so we don't leak on crash */
293 key.objectid = inode->i_ino;
294 key.offset = (u64)-1;
295 key.flags = 0;
296 /*
297 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
298 * or extent data
299 */
300 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
301 while(1) {
302 btrfs_init_path(path);
303 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
304 if (ret < 0) {
305 goto error;
306 }
307 if (ret > 0) {
308 BUG_ON(path->slots[0] == 0);
309 path->slots[0]--;
310 }
311 leaf = btrfs_buffer_leaf(path->nodes[0]);
312 found_key = &leaf->items[path->slots[0]].key;
313 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
314 break;
315 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
316 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
317 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
318 break;
319 if (btrfs_disk_key_offset(found_key) < inode->i_size)
320 break;
321 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
322 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
323 path->slots[0],
324 struct btrfs_file_extent_item);
325 extent_start = btrfs_file_extent_disk_blocknr(fi);
326 extent_num_blocks =
327 btrfs_file_extent_disk_num_blocks(fi);
328 inode->i_blocks -=
329 btrfs_file_extent_num_blocks(fi) >> 9;
330 found_extent = 1;
331 } else {
332 found_extent = 0;
333 }
334 ret = btrfs_del_item(trans, root, path);
335 BUG_ON(ret);
336 btrfs_release_path(root, path);
337 if (found_extent) {
338 ret = btrfs_free_extent(trans, root, extent_start,
339 extent_num_blocks, 0);
340 BUG_ON(ret);
341 }
342 }
343 ret = 0;
344error:
345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
347 return ret;
348}
349
350static void btrfs_delete_inode(struct inode *inode)
351{
352 struct btrfs_trans_handle *trans;
353 struct btrfs_root *root = btrfs_sb(inode->i_sb);
354 int ret;
355
356 truncate_inode_pages(&inode->i_data, 0);
357 if (is_bad_inode(inode)) {
358 goto no_delete;
359 }
360 inode->i_size = 0;
361 mutex_lock(&root->fs_info->fs_mutex);
362 trans = btrfs_start_transaction(root, 1);
363 if (S_ISREG(inode->i_mode)) {
364 ret = btrfs_truncate_in_trans(trans, root, inode);
365 BUG_ON(ret);
366 }
367 btrfs_free_inode(trans, root, inode);
368 btrfs_end_transaction(trans, root);
369 mutex_unlock(&root->fs_info->fs_mutex);
370 return;
371no_delete:
372 clear_inode(inode);
373}
374
375static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
376 ino_t *ino)
377{
378 const char *name = dentry->d_name.name;
379 int namelen = dentry->d_name.len;
380 struct btrfs_dir_item *di;
381 struct btrfs_path *path;
382 struct btrfs_root *root = btrfs_sb(dir->i_sb);
383 int ret;
384
385 path = btrfs_alloc_path();
386 BUG_ON(!path);
387 btrfs_init_path(path);
388 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
389 namelen, 0);
390 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
391 *ino = 0;
392 ret = 0;
393 goto out;
394 }
395 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
396 struct btrfs_dir_item);
397 *ino = btrfs_dir_objectid(di);
398out:
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 check_inode(dir);
402 return ret;
403}
404
405static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
406 struct nameidata *nd)
407{
408 struct inode * inode;
409 struct btrfs_root *root = btrfs_sb(dir->i_sb);
410 ino_t ino;
411 int ret;
412
413 if (dentry->d_name.len > BTRFS_NAME_LEN)
414 return ERR_PTR(-ENAMETOOLONG);
415 mutex_lock(&root->fs_info->fs_mutex);
416 ret = btrfs_inode_by_name(dir, dentry, &ino);
417 mutex_unlock(&root->fs_info->fs_mutex);
418 if (ret < 0)
419 return ERR_PTR(ret);
420 inode = NULL;
421 if (ino) {
422 inode = iget(dir->i_sb, ino);
423 if (!inode)
424 return ERR_PTR(-EACCES);
425 check_inode(inode);
426 }
427 check_inode(dir);
428 return d_splice_alias(inode, dentry);
429}
430
431static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
432{
433 struct inode *inode = filp->f_path.dentry->d_inode;
434 struct btrfs_root *root = btrfs_sb(inode->i_sb);
435 struct btrfs_item *item;
436 struct btrfs_dir_item *di;
437 struct btrfs_key key;
438 struct btrfs_path *path;
439 int ret;
440 u32 nritems;
441 struct btrfs_leaf *leaf;
442 int slot;
443 int advance;
444 unsigned char d_type = DT_UNKNOWN;
445 int over = 0;
446
447 mutex_lock(&root->fs_info->fs_mutex);
448 key.objectid = inode->i_ino;
449 key.flags = 0;
450 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
451 key.offset = filp->f_pos;
452 path = btrfs_alloc_path();
453 btrfs_init_path(path);
454 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
455 if (ret < 0) {
456 goto err;
457 }
458 advance = 0;
459 while(1) {
460 leaf = btrfs_buffer_leaf(path->nodes[0]);
461 nritems = btrfs_header_nritems(&leaf->header);
462 slot = path->slots[0];
463 if (advance || slot >= nritems) {
464 if (slot >= nritems -1) {
465 ret = btrfs_next_leaf(root, path);
466 if (ret)
467 break;
468 leaf = btrfs_buffer_leaf(path->nodes[0]);
469 nritems = btrfs_header_nritems(&leaf->header);
470 slot = path->slots[0];
471 } else {
472 slot++;
473 path->slots[0]++;
474 }
475 }
476 advance = 1;
477 item = leaf->items + slot;
478 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
479 break;
480 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
481 continue;
482 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
483 continue;
484
485 advance = 1;
486 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
487 over = filldir(dirent, (const char *)(di + 1),
488 btrfs_dir_name_len(di),
489 btrfs_disk_key_offset(&item->key),
490 btrfs_dir_objectid(di), d_type);
491 if (over) {
492 filp->f_pos = btrfs_disk_key_offset(&item->key);
493 break;
494 }
495 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
496 }
497 ret = 0;
498err:
499 btrfs_release_path(root, path);
500 btrfs_free_path(path);
501 mutex_unlock(&root->fs_info->fs_mutex);
502 return ret;
503}
504
505static void btrfs_put_super (struct super_block * sb)
506{
507 struct btrfs_root *root = btrfs_sb(sb);
508 int ret;
509
510 ret = close_ctree(root);
511 if (ret) {
512 printk("close ctree returns %d\n", ret);
513 }
514 sb->s_fs_info = NULL;
515}
516
517static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
518{
519 struct inode * inode;
520 struct dentry * root_dentry;
521 struct btrfs_super_block *disk_super;
522 struct btrfs_root *root;
523
524 sb->s_maxbytes = MAX_LFS_FILESIZE;
525 sb->s_magic = BTRFS_SUPER_MAGIC;
526 sb->s_op = &btrfs_super_ops;
527 sb->s_time_gran = 1;
528
529 root = open_ctree(sb);
530
531 if (!root) {
532 printk("btrfs: open_ctree failed\n");
533 return -EIO;
534 }
535 sb->s_fs_info = root;
536 disk_super = root->fs_info->disk_super;
537 printk("read in super total blocks %Lu root %Lu\n",
538 btrfs_super_total_blocks(disk_super),
539 btrfs_super_root_dir(disk_super));
540
541 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
542 if (!inode)
543 return -ENOMEM;
544 if (inode->i_state & I_NEW) {
545 btrfs_read_locked_inode(inode);
546 unlock_new_inode(inode);
547 }
548
549 root_dentry = d_alloc_root(inode);
550 if (!root_dentry) {
551 iput(inode);
552 return -ENOMEM;
553 }
554 sb->s_root = root_dentry;
555
556 return 0;
557}
558
559static void fill_inode_item(struct btrfs_inode_item *item,
560 struct inode *inode)
561{
562 btrfs_set_inode_uid(item, inode->i_uid);
563 btrfs_set_inode_gid(item, inode->i_gid);
564 btrfs_set_inode_size(item, inode->i_size);
565 btrfs_set_inode_mode(item, inode->i_mode);
566 btrfs_set_inode_nlink(item, inode->i_nlink);
567 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
568 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
569 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
570 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
571 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
572 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
573 btrfs_set_inode_nblocks(item, inode->i_blocks);
574 btrfs_set_inode_generation(item, inode->i_generation);
575 check_inode(inode);
576}
577
578static int btrfs_update_inode(struct btrfs_trans_handle *trans,
579 struct btrfs_root *root,
580 struct inode *inode)
581{
582 struct btrfs_inode_item *inode_item;
583 struct btrfs_path *path;
584 int ret;
585
586 path = btrfs_alloc_path();
587 BUG_ON(!path);
588 btrfs_init_path(path);
589
590 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
591 if (ret) {
592 if (ret > 0)
593 ret = -ENOENT;
594 goto failed;
595 }
596
597 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
598 path->slots[0],
599 struct btrfs_inode_item);
600
601 fill_inode_item(inode_item, inode);
602 btrfs_mark_buffer_dirty(path->nodes[0]);
603failed:
604 btrfs_release_path(root, path);
605 btrfs_free_path(path);
606 check_inode(inode);
607 return 0;
608}
609
610static int btrfs_write_inode(struct inode *inode, int wait)
611{
612 struct btrfs_root *root = btrfs_sb(inode->i_sb);
613 struct btrfs_trans_handle *trans;
614 int ret;
615
616 mutex_lock(&root->fs_info->fs_mutex);
617 trans = btrfs_start_transaction(root, 1);
618 ret = btrfs_update_inode(trans, root, inode);
619 if (wait)
620 btrfs_commit_transaction(trans, root);
621 else
622 btrfs_end_transaction(trans, root);
623 mutex_unlock(&root->fs_info->fs_mutex);
624 check_inode(inode);
625 return ret;
626}
627
628static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
629 struct inode *dir, int mode)
630{
631 struct inode *inode;
632 struct btrfs_inode_item inode_item;
633 struct btrfs_root *root = btrfs_sb(dir->i_sb);
634 struct btrfs_key key;
635 int ret;
636 u64 objectid;
637
638 inode = new_inode(dir->i_sb);
639 if (!inode)
640 return ERR_PTR(-ENOMEM);
641
642 check_inode(inode);
643 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
644 BUG_ON(ret);
645
646 inode->i_uid = current->fsuid;
647 inode->i_gid = current->fsgid;
648 inode->i_mode = mode;
649 inode->i_ino = objectid;
650 inode->i_blocks = 0;
651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
652 fill_inode_item(&inode_item, inode);
653
654 key.objectid = objectid;
655 key.flags = 0;
656 key.offset = 0;
657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
658 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
659 BUG_ON(ret);
660
661 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
662 BUG_ON(ret);
663
664 insert_inode_hash(inode);
665 check_inode(inode);
666 check_inode(dir);
667 return inode;
668}
669
670static int btrfs_add_link(struct btrfs_trans_handle *trans,
671 struct dentry *dentry, struct inode *inode)
672{
673 int ret;
674 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
675 dentry->d_name.name, dentry->d_name.len,
676 dentry->d_parent->d_inode->i_ino,
677 inode->i_ino, 0);
678 if (ret == 0) {
679 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
680 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
681 dentry->d_parent->d_inode);
682 }
683 check_inode(inode);
684 check_inode(dentry->d_parent->d_inode);
685 return ret;
686}
687
688static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
689 struct dentry *dentry, struct inode *inode)
690{
691 int err = btrfs_add_link(trans, dentry, inode);
692 if (!err) {
693 d_instantiate(dentry, inode);
694 return 0;
695 }
696 if (err > 0)
697 err = -EEXIST;
698 check_inode(inode);
699 return err;
700}
701
702static int btrfs_create(struct inode *dir, struct dentry *dentry,
703 int mode, struct nameidata *nd)
704{
705 struct btrfs_trans_handle *trans;
706 struct btrfs_root *root = btrfs_sb(dir->i_sb);
707 struct inode *inode;
708 int err;
709 int drop_inode = 0;
710
711 mutex_lock(&root->fs_info->fs_mutex);
712 trans = btrfs_start_transaction(root, 1);
713 inode = btrfs_new_inode(trans, dir, mode);
714 err = PTR_ERR(inode);
715 if (IS_ERR(inode))
716 goto out_unlock;
717 // FIXME mark the inode dirty
718 err = btrfs_add_nondir(trans, dentry, inode);
719 if (err)
720 drop_inode = 1;
721 else {
722 inode->i_mapping->a_ops = &btrfs_aops;
723 inode->i_fop = &btrfs_file_operations;
724 inode->i_op = &btrfs_file_inode_operations;
725 }
726 dir->i_sb->s_dirt = 1;
727out_unlock:
728 btrfs_end_transaction(trans, root);
729 mutex_unlock(&root->fs_info->fs_mutex);
730 check_inode(inode);
731 check_inode(dir);
732
733 if (drop_inode) {
734 inode_dec_link_count(inode);
735 iput(inode);
736 }
737 return err;
738}
739
740static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
741 struct inode *inode, struct inode *dir)
742{
743 struct btrfs_root *root = btrfs_sb(inode->i_sb);
744 int ret;
745 char buf[2];
746 buf[0] = '.';
747 buf[1] = '.';
748
749 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
750 inode->i_ino, 1);
751 if (ret)
752 goto error;
753 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
754 dir->i_ino, 1);
755 if (ret)
756 goto error;
757 inode->i_size = 3;
758 ret = btrfs_update_inode(trans, root, inode);
759error:
760 return ret;
761}
762
763static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
764{
765 struct inode *inode;
766 struct btrfs_trans_handle *trans;
767 struct btrfs_root *root = btrfs_sb(dir->i_sb);
768 int err = 0;
769 int drop_on_err = 0;
770
771 mutex_lock(&root->fs_info->fs_mutex);
772 trans = btrfs_start_transaction(root, 1);
773 if (IS_ERR(trans)) {
774 err = PTR_ERR(trans);
775 goto out_unlock;
776 }
777 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
778 if (IS_ERR(inode)) {
779 err = PTR_ERR(inode);
780 goto out_fail;
781 }
782 drop_on_err = 1;
783 inode->i_op = &btrfs_dir_inode_operations;
784 inode->i_fop = &btrfs_dir_file_operations;
785
786 err = btrfs_make_empty_dir(trans, inode, dir);
787 if (err)
788 goto out_fail;
789 err = btrfs_add_link(trans, dentry, inode);
790 if (err)
791 goto out_fail;
792 d_instantiate(dentry, inode);
793 drop_on_err = 0;
794
795out_fail:
796 btrfs_end_transaction(trans, root);
797out_unlock:
798 mutex_unlock(&root->fs_info->fs_mutex);
799 if (drop_on_err)
800 iput(inode);
801 return err;
802}
803
804static int btrfs_sync_fs(struct super_block *sb, int wait)
805{
806 struct btrfs_trans_handle *trans;
807 struct btrfs_root *root;
808 int ret;
809 root = btrfs_sb(sb);
810
811 sb->s_dirt = 0;
812 if (!wait) {
813 filemap_flush(root->fs_info->btree_inode->i_mapping);
814 return 0;
815 }
816 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
817 mutex_lock(&root->fs_info->fs_mutex);
818 trans = btrfs_start_transaction(root, 1);
819 ret = btrfs_commit_transaction(trans, root);
820 sb->s_dirt = 0;
821 BUG_ON(ret);
822printk("btrfs sync_fs\n");
823 mutex_unlock(&root->fs_info->fs_mutex);
824 return 0;
825}
826
827static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
828 struct buffer_head *result, int create)
829{
830 struct btrfs_root *root = btrfs_sb(inode->i_sb);
831 struct btrfs_path *path;
832 struct btrfs_key key;
833 struct btrfs_leaf *leaf;
834 int num_bytes = result->b_size;
835 int item_size;
836 int ret;
837 u64 pos;
838 char *ptr;
839 int copy_size;
840 int err = 0;
841 char *safe_ptr;
842 char *data_ptr;
843
844 path = btrfs_alloc_path();
845 BUG_ON(!path);
846
847 WARN_ON(create);
848 if (create) {
849 return 0;
850 }
851 pos = iblock << inode->i_blkbits;
852 key.objectid = inode->i_ino;
853 key.flags = 0;
854 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
855 ptr = kmap(result->b_page);
856 safe_ptr = ptr;
857 ptr += (pos & (PAGE_CACHE_SIZE -1));
858again:
859 key.offset = pos;
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
861 if (ret) {
862 if (ret < 0)
863 err = ret;
864 else
865 err = 0;
866 goto out;
867 }
868 leaf = btrfs_buffer_leaf(path->nodes[0]);
869 item_size = btrfs_item_size(leaf->items + path->slots[0]);
870 copy_size = min(num_bytes, item_size);
871 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
872 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
873 memcpy(ptr, data_ptr, copy_size);
874 pos += copy_size;
875 num_bytes -= copy_size;
876 WARN_ON(num_bytes < 0);
877 ptr += copy_size;
878 btrfs_release_path(root, path);
879 if (num_bytes != 0) {
880 if (pos >= i_size_read(inode))
881 memset(ptr, 0, num_bytes);
882 else
883 goto again;
884 }
885 set_buffer_uptodate(result);
886 map_bh(result, inode->i_sb, 0);
887 err = 0;
888out:
889 btrfs_free_path(path);
890 kunmap(result->b_page);
891 return err;
892}
893
894static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
895 struct buffer_head *result, int create)
896{
897 int ret;
898 int err = 0;
899 u64 blocknr;
900 u64 extent_start = 0;
901 u64 extent_end = 0;
902 u64 objectid = inode->i_ino;
903 struct btrfs_path *path;
904 struct btrfs_root *root = btrfs_sb(inode->i_sb);
905 struct btrfs_trans_handle *trans = NULL;
906 struct btrfs_file_extent_item *item;
907 struct btrfs_leaf *leaf;
908 struct btrfs_disk_key *found_key;
909
910 path = btrfs_alloc_path();
911 BUG_ON(!path);
912 btrfs_init_path(path);
913 if (create)
914 trans = btrfs_start_transaction(root, 1);
915
916
917 ret = btrfs_lookup_file_extent(trans, root, path,
918 inode->i_ino,
919 iblock << inode->i_blkbits, 0);
920 if (ret < 0) {
921 err = ret;
922 goto out;
923 }
924
925 if (ret != 0) {
926 if (path->slots[0] == 0) {
927 btrfs_release_path(root, path);
928 goto allocate;
929 }
930 path->slots[0]--;
931 }
932
933 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
934 struct btrfs_file_extent_item);
935 leaf = btrfs_buffer_leaf(path->nodes[0]);
936 blocknr = btrfs_file_extent_disk_blocknr(item);
937 blocknr += btrfs_file_extent_offset(item);
938
939 /* exact match found, use it */
940 if (ret == 0) {
941 err = 0;
942 map_bh(result, inode->i_sb, blocknr);
943 goto out;
944 }
945
946 /* are we inside the extent that was found? */
947 found_key = &leaf->items[path->slots[0]].key;
948 if (btrfs_disk_key_objectid(found_key) != objectid ||
949 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
950 extent_end = 0;
951 extent_start = 0;
952 btrfs_release_path(root, path);
953 goto allocate;
954 }
955
956 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
957 extent_start = extent_start >> inode->i_blkbits;
958 extent_start += btrfs_file_extent_offset(item);
959 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
960 if (iblock >= extent_start && iblock < extent_end) {
961 err = 0;
962 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
963 goto out;
964 }
965allocate:
966 /* ok, create a new extent */
967 if (!create) {
968 err = 0;
969 goto out;
970 }
971 ret = btrfs_alloc_file_extent(trans, root, objectid,
972 iblock << inode->i_blkbits,
973 1, extent_end, &blocknr);
974 if (ret) {
975 err = ret;
976 goto out;
977 }
978 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
979 set_buffer_new(result);
980 map_bh(result, inode->i_sb, blocknr);
981
982out:
983 btrfs_release_path(root, path);
984 btrfs_free_path(path);
985 if (trans)
986 btrfs_end_transaction(trans, root);
987 return err;
988}
989
990static int btrfs_get_block(struct inode *inode, sector_t iblock,
991 struct buffer_head *result, int create)
992{
993 int err;
994 struct btrfs_root *root = btrfs_sb(inode->i_sb);
995 mutex_lock(&root->fs_info->fs_mutex);
996 // err = btrfs_get_block_lock(inode, iblock, result, create);
997 err = btrfs_get_block_inline(inode, iblock, result, create);
998 mutex_unlock(&root->fs_info->fs_mutex);
999 return err;
1000}
1001
1002static int btrfs_prepare_write(struct file *file, struct page *page,
1003 unsigned from, unsigned to)
1004{
1005 WARN_ON(1);
1006 return nobh_prepare_write(page, from, to, btrfs_get_block);
1007}
1008static int btrfs_commit_write(struct file *file, struct page *page,
1009 unsigned from, unsigned to)
1010{
1011 WARN_ON(1);
1012 return nobh_commit_write(file, page, from, to);
1013}
1014
1015static void btrfs_write_super(struct super_block *sb)
1016{
1017 btrfs_sync_fs(sb, 1);
1018}
1019
1020static int btrfs_readpage(struct file *file, struct page *page)
1021{
1022 return mpage_readpage(page, btrfs_get_block);
1023}
1024
1025static int btrfs_readpages(struct file *file, struct address_space *mapping,
1026 struct list_head *pages, unsigned nr_pages)
1027{
1028 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1029}
1030
1031static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1032{
1033 return nobh_writepage(page, btrfs_get_block, wbc);
1034}
1035
1036static void btrfs_truncate(struct inode *inode)
1037{
1038 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1039 int ret;
1040 struct btrfs_trans_handle *trans;
1041
1042 if (!S_ISREG(inode->i_mode))
1043 return;
1044 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1045 return;
1046
1047 // nobh_truncate_page(inode->i_mapping, inode->i_size);
1048
1049 /* FIXME, add redo link to tree so we don't leak on crash */
1050 mutex_lock(&root->fs_info->fs_mutex);
1051 trans = btrfs_start_transaction(root, 1);
1052 ret = btrfs_truncate_in_trans(trans, root, inode);
1053 BUG_ON(ret);
1054 ret = btrfs_end_transaction(trans, root);
1055 BUG_ON(ret);
1056 mutex_unlock(&root->fs_info->fs_mutex);
1057 mark_inode_dirty(inode);
1058}
1059
1060static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1061 struct page **prepared_pages,
1062 const char __user * buf)
1063{
1064 long page_fault = 0;
1065 int i;
1066 int offset = pos & (PAGE_CACHE_SIZE - 1);
1067
1068 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1069 size_t count = min_t(size_t,
1070 PAGE_CACHE_SIZE - offset, write_bytes);
1071 struct page *page = prepared_pages[i];
1072 fault_in_pages_readable(buf, count);
1073
1074 /* Copy data from userspace to the current page */
1075 kmap(page);
1076 page_fault = __copy_from_user(page_address(page) + offset,
1077 buf, count);
1078 /* Flush processor's dcache for this page */
1079 flush_dcache_page(page);
1080 kunmap(page);
1081 buf += count;
1082 write_bytes -= count;
1083
1084 if (page_fault)
1085 break;
1086 }
1087 return page_fault ? -EFAULT : 0;
1088}
1089
1090static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1091{
1092 size_t i;
1093 for (i = 0; i < num_pages; i++) {
1094 if (!pages[i])
1095 break;
1096 unlock_page(pages[i]);
1097 mark_page_accessed(pages[i]);
1098 page_cache_release(pages[i]);
1099 }
1100}
1101static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct file *file,
1104 struct page **pages,
1105 size_t num_pages,
1106 loff_t pos,
1107 size_t write_bytes)
1108{
1109 int i;
1110 int offset;
1111 int err = 0;
1112 int ret;
1113 int this_write;
1114 struct inode *inode = file->f_path.dentry->d_inode;
1115
1116 for (i = 0; i < num_pages; i++) {
1117 offset = pos & (PAGE_CACHE_SIZE -1);
1118 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1119 /* FIXME, one block at a time */
1120
1121 mutex_lock(&root->fs_info->fs_mutex);
1122 trans = btrfs_start_transaction(root, 1);
1123 btrfs_csum_file_block(trans, root, inode->i_ino,
1124 pages[i]->index << PAGE_CACHE_SHIFT,
1125 kmap(pages[i]), PAGE_CACHE_SIZE);
1126 kunmap(pages[i]);
1127 SetPageChecked(pages[i]);
1128 ret = btrfs_end_transaction(trans, root);
1129 BUG_ON(ret);
1130 mutex_unlock(&root->fs_info->fs_mutex);
1131
1132 ret = nobh_commit_write(file, pages[i], offset,
1133 offset + this_write);
1134 pos += this_write;
1135 if (ret) {
1136 err = ret;
1137 goto failed;
1138 }
1139 WARN_ON(this_write > write_bytes);
1140 write_bytes -= this_write;
1141 }
1142failed:
1143 return err;
1144}
1145
1146static int prepare_pages(struct btrfs_trans_handle *trans,
1147 struct btrfs_root *root,
1148 struct file *file,
1149 struct page **pages,
1150 size_t num_pages,
1151 loff_t pos,
1152 size_t write_bytes)
1153{
1154 int i;
1155 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1156 struct inode *inode = file->f_path.dentry->d_inode;
1157 int offset;
1158 int err = 0;
1159 int ret;
1160 int this_write;
1161 loff_t isize = i_size_read(inode);
1162
1163 memset(pages, 0, num_pages * sizeof(struct page *));
1164
1165 for (i = 0; i < num_pages; i++) {
1166 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1167 if (!pages[i]) {
1168 err = -ENOMEM;
1169 goto failed_release;
1170 }
1171 offset = pos & (PAGE_CACHE_SIZE -1);
1172 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1173 ret = nobh_prepare_write(pages[i], offset,
1174 offset + this_write,
1175 btrfs_get_block);
1176 pos += this_write;
1177 if (ret) {
1178 err = ret;
1179 goto failed_truncate;
1180 }
1181 WARN_ON(this_write > write_bytes);
1182 write_bytes -= this_write;
1183 }
1184 return 0;
1185
1186failed_release:
1187 btrfs_drop_pages(pages, num_pages);
1188 return err;
1189
1190failed_truncate:
1191 btrfs_drop_pages(pages, num_pages);
1192 if (pos > isize)
1193 vmtruncate(inode, isize);
1194 return err;
1195}
1196
1197static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *ppos)
1199{
1200 loff_t pos;
1201 size_t num_written = 0;
1202 int err = 0;
1203 int ret = 0;
1204 struct inode *inode = file->f_path.dentry->d_inode;
1205 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1206 struct page *pages[1];
1207
1208 if (file->f_flags & O_DIRECT)
1209 return -EINVAL;
1210 pos = *ppos;
1211
1212 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1213 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1214 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1215 if (err)
1216 goto out;
1217 if (count == 0)
1218 goto out;
1219 err = remove_suid(file->f_path.dentry);
1220 if (err)
1221 goto out;
1222 file_update_time(file);
1223 mutex_lock(&inode->i_mutex);
1224 while(count > 0) {
1225 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1226 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1227 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1228 PAGE_CACHE_SHIFT;
1229 ret = prepare_pages(NULL, root, file, pages, num_pages,
1230 pos, write_bytes);
1231 BUG_ON(ret);
1232 ret = btrfs_copy_from_user(pos, num_pages,
1233 write_bytes, pages, buf);
1234 BUG_ON(ret);
1235
1236 ret = dirty_and_release_pages(NULL, root, file, pages,
1237 num_pages, pos, write_bytes);
1238 BUG_ON(ret);
1239 btrfs_drop_pages(pages, num_pages);
1240
1241 buf += write_bytes;
1242 count -= write_bytes;
1243 pos += write_bytes;
1244 num_written += write_bytes;
1245
1246 balance_dirty_pages_ratelimited(inode->i_mapping);
1247 cond_resched();
1248 }
1249 mutex_unlock(&inode->i_mutex);
1250out:
1251 *ppos = pos;
1252 current->backing_dev_info = NULL;
1253 return num_written ? num_written : err;
1254}
1255
1256static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1257 struct page *page, loff_t pos,
1258 size_t offset, size_t write_bytes)
1259{
1260 struct btrfs_path *path;
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_key key;
1263 struct btrfs_leaf *leaf;
1264 struct btrfs_key found_key;
1265 int ret;
1266 size_t copy_size = 0;
1267 char *dst = NULL;
1268 int err = 0;
1269 size_t num_written = 0;
1270
1271 path = btrfs_alloc_path();
1272 BUG_ON(!path);
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 trans = btrfs_start_transaction(root, 1);
1275 key.objectid = inode->i_ino;
1276 key.flags = 0;
1277 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1278
1279again:
1280 key.offset = pos;
1281 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1282 if (ret < 0) {
1283 err = ret;
1284 goto out;
1285 }
1286 if (ret == 0) {
1287 leaf = btrfs_buffer_leaf(path->nodes[0]);
1288 btrfs_disk_key_to_cpu(&found_key,
1289 &leaf->items[path->slots[0]].key);
1290 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1291 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1292 copy_size = min(write_bytes, copy_size);
1293 goto copyit;
1294 } else {
1295 int slot = path->slots[0];
1296 if (slot > 0) {
1297 slot--;
1298 }
1299 // FIXME find max key
1300 leaf = btrfs_buffer_leaf(path->nodes[0]);
1301 btrfs_disk_key_to_cpu(&found_key,
1302 &leaf->items[slot].key);
1303 if (found_key.objectid != inode->i_ino)
1304 goto insert;
1305 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1306 goto insert;
1307 copy_size = btrfs_item_size(leaf->items + slot);
1308 if (found_key.offset + copy_size <= pos)
1309 goto insert;
1310 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1311 dst += pos - found_key.offset;
1312 copy_size = copy_size - (pos - found_key.offset);
1313 BUG_ON(copy_size < 0);
1314 copy_size = min(write_bytes, copy_size);
1315 WARN_ON(copy_size == 0);
1316 goto copyit;
1317 }
1318insert:
1319 btrfs_release_path(root, path);
1320 copy_size = min(write_bytes,
1321 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1322 sizeof(struct btrfs_item) * 4);
1323 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1324 BUG_ON(ret);
1325 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1326 path->slots[0], char);
1327copyit:
1328 WARN_ON(copy_size == 0);
1329 WARN_ON(dst + copy_size >
1330 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1331 path->slots[0], char) +
1332 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1333 path->slots[0]));
1334 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1335 page_address(page) + offset, copy_size);
1336 mark_buffer_dirty(path->nodes[0]);
1337 btrfs_release_path(root, path);
1338 pos += copy_size;
1339 offset += copy_size;
1340 num_written += copy_size;
1341 write_bytes -= copy_size;
1342 if (write_bytes)
1343 goto again;
1344out:
1345 btrfs_free_path(path);
1346 ret = btrfs_end_transaction(trans, root);
1347 BUG_ON(ret);
1348 mutex_unlock(&root->fs_info->fs_mutex);
1349 return num_written ? num_written : err;
1350}
1351
1352static ssize_t btrfs_file_inline_write(struct file *file,
1353 const char __user *buf,
1354 size_t count, loff_t *ppos)
1355{
1356 loff_t pos;
1357 size_t num_written = 0;
1358 int err = 0;
1359 int ret = 0;
1360 struct inode *inode = file->f_path.dentry->d_inode;
1361 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1362 unsigned long page_index;
1363
1364 if (file->f_flags & O_DIRECT)
1365 return -EINVAL;
1366 pos = *ppos;
1367
1368 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1369 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1370 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1371 if (err)
1372 goto out;
1373 if (count == 0)
1374 goto out;
1375 err = remove_suid(file->f_path.dentry);
1376 if (err)
1377 goto out;
1378 file_update_time(file);
1379 mutex_lock(&inode->i_mutex);
1380 while(count > 0) {
1381 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1382 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1383 struct page *page;
1384
1385 page_index = pos >> PAGE_CACHE_SHIFT;
1386 page = grab_cache_page(inode->i_mapping, page_index);
1387 if (!PageUptodate(page)) {
1388 ret = mpage_readpage(page, btrfs_get_block);
1389 BUG_ON(ret);
1390 lock_page(page);
1391 }
1392 ret = btrfs_copy_from_user(pos, 1,
1393 write_bytes, &page, buf);
1394 BUG_ON(ret);
1395 write_bytes = inline_one_page(root, inode, page, pos,
1396 offset, write_bytes);
1397 SetPageUptodate(page);
1398 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1399 i_size_write(inode, pos + write_bytes);
1400 mark_inode_dirty(inode);
1401 }
1402 page_cache_release(page);
1403 unlock_page(page);
1404 if (write_bytes < 0)
1405 goto out_unlock;
1406 buf += write_bytes;
1407 count -= write_bytes;
1408 pos += write_bytes;
1409 num_written += write_bytes;
1410
1411 balance_dirty_pages_ratelimited(inode->i_mapping);
1412 cond_resched();
1413 }
1414out_unlock:
1415 mutex_unlock(&inode->i_mutex);
1416out:
1417 *ppos = pos;
1418 current->backing_dev_info = NULL;
1419 return num_written ? num_written : err;
1420}
1421
1422static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1423 unsigned long offset, unsigned long size)
1424{
1425 char *kaddr;
1426 unsigned long left, count = desc->count;
1427
1428 if (size > count)
1429 size = count;
1430
1431 if (!PageChecked(page)) {
1432 /* FIXME, do it per block */
1433 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1434 int ret = btrfs_csum_verify_file_block(root,
1435 page->mapping->host->i_ino,
1436 page->index << PAGE_CACHE_SHIFT,
1437 kmap(page), PAGE_CACHE_SIZE);
1438 if (ret) {
1439 printk("failed to verify ino %lu page %lu\n",
1440 page->mapping->host->i_ino,
1441 page->index);
1442 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1443 }
1444 SetPageChecked(page);
1445 kunmap(page);
1446 }
1447 /*
1448 * Faults on the destination of a read are common, so do it before
1449 * taking the kmap.
1450 */
1451 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1452 kaddr = kmap_atomic(page, KM_USER0);
1453 left = __copy_to_user_inatomic(desc->arg.buf,
1454 kaddr + offset, size);
1455 kunmap_atomic(kaddr, KM_USER0);
1456 if (left == 0)
1457 goto success;
1458 }
1459
1460 /* Do it the slow way */
1461 kaddr = kmap(page);
1462 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1463 kunmap(page);
1464
1465 if (left) {
1466 size -= left;
1467 desc->error = -EFAULT;
1468 }
1469success:
1470 desc->count = count - size;
1471 desc->written += size;
1472 desc->arg.buf += size;
1473 return size;
1474}
1475
1476/**
1477 * btrfs_file_aio_read - filesystem read routine
1478 * @iocb: kernel I/O control block
1479 * @iov: io vector request
1480 * @nr_segs: number of segments in the iovec
1481 * @pos: current file position
1482 */
1483static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1484 unsigned long nr_segs, loff_t pos)
1485{
1486 struct file *filp = iocb->ki_filp;
1487 ssize_t retval;
1488 unsigned long seg;
1489 size_t count;
1490 loff_t *ppos = &iocb->ki_pos;
1491
1492 count = 0;
1493 for (seg = 0; seg < nr_segs; seg++) {
1494 const struct iovec *iv = &iov[seg];
1495
1496 /*
1497 * If any segment has a negative length, or the cumulative
1498 * length ever wraps negative then return -EINVAL.
1499 */
1500 count += iv->iov_len;
1501 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1502 return -EINVAL;
1503 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1504 continue;
1505 if (seg == 0)
1506 return -EFAULT;
1507 nr_segs = seg;
1508 count -= iv->iov_len; /* This segment is no good */
1509 break;
1510 }
1511 retval = 0;
1512 if (count) {
1513 for (seg = 0; seg < nr_segs; seg++) {
1514 read_descriptor_t desc;
1515
1516 desc.written = 0;
1517 desc.arg.buf = iov[seg].iov_base;
1518 desc.count = iov[seg].iov_len;
1519 if (desc.count == 0)
1520 continue;
1521 desc.error = 0;
1522 do_generic_file_read(filp, ppos, &desc,
1523 btrfs_read_actor);
1524 retval += desc.written;
1525 if (desc.error) {
1526 retval = retval ?: desc.error;
1527 break;
1528 }
1529 }
1530 }
1531 return retval;
1532}
1533
1534static struct kmem_cache *btrfs_inode_cachep;
1535struct kmem_cache *btrfs_trans_handle_cachep;
1536struct kmem_cache *btrfs_transaction_cachep;
1537struct kmem_cache *btrfs_bit_radix_cachep;
1538struct kmem_cache *btrfs_path_cachep;
1539
1540/*
1541 * Called inside transaction, so use GFP_NOFS
1542 */
1543static struct inode *btrfs_alloc_inode(struct super_block *sb)
1544{
1545 struct btrfs_inode *ei;
1546
1547 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1548 if (!ei)
1549 return NULL;
1550 ei->magic = 0xDEADBEEF;
1551 ei->magic2 = 0xDEADBEAF;
1552 return &ei->vfs_inode;
1553}
1554
1555static void btrfs_destroy_inode(struct inode *inode)
1556{
1557 struct btrfs_inode *ei = BTRFS_I(inode);
1558 WARN_ON(ei->magic != 0xDEADBEEF);
1559 WARN_ON(ei->magic2 != 0xDEADBEAF);
1560 WARN_ON(!list_empty(&inode->i_dentry));
1561 WARN_ON(inode->i_data.nrpages);
1562
1563 ei->magic = 0;
1564 ei->magic2 = 0;
1565 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1566}
1567
1568static void init_once(void * foo, struct kmem_cache * cachep,
1569 unsigned long flags)
1570{
1571 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1572
1573 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1574 SLAB_CTOR_CONSTRUCTOR) {
1575 inode_init_once(&ei->vfs_inode);
1576 }
1577}
1578
1579static int init_inodecache(void)
1580{
1581 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1582 sizeof(struct btrfs_inode),
1583 0, (SLAB_RECLAIM_ACCOUNT|
1584 SLAB_MEM_SPREAD),
1585 init_once, NULL);
1586 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1587 sizeof(struct btrfs_trans_handle),
1588 0, (SLAB_RECLAIM_ACCOUNT|
1589 SLAB_MEM_SPREAD),
1590 NULL, NULL);
1591 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1592 sizeof(struct btrfs_transaction),
1593 0, (SLAB_RECLAIM_ACCOUNT|
1594 SLAB_MEM_SPREAD),
1595 NULL, NULL);
1596 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1597 sizeof(struct btrfs_transaction),
1598 0, (SLAB_RECLAIM_ACCOUNT|
1599 SLAB_MEM_SPREAD),
1600 NULL, NULL);
1601 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1602 256,
1603 0, (SLAB_RECLAIM_ACCOUNT|
1604 SLAB_MEM_SPREAD |
1605 SLAB_DESTROY_BY_RCU),
1606 NULL, NULL);
1607 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1608 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1609 return -ENOMEM;
1610 return 0;
1611}
1612
1613static void destroy_inodecache(void)
1614{
1615 kmem_cache_destroy(btrfs_inode_cachep);
1616 kmem_cache_destroy(btrfs_trans_handle_cachep);
1617 kmem_cache_destroy(btrfs_transaction_cachep);
1618 kmem_cache_destroy(btrfs_bit_radix_cachep);
1619 kmem_cache_destroy(btrfs_path_cachep);
1620}
1621
1622static int btrfs_get_sb(struct file_system_type *fs_type,
1623 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1624{
1625 return get_sb_bdev(fs_type, flags, dev_name, data,
1626 btrfs_fill_super, mnt);
1627}
1628
1629static struct file_system_type btrfs_fs_type = {
1630 .owner = THIS_MODULE,
1631 .name = "btrfs",
1632 .get_sb = btrfs_get_sb,
1633 .kill_sb = kill_block_super,
1634 .fs_flags = FS_REQUIRES_DEV,
1635};
1636
1637static struct super_operations btrfs_super_ops = {
1638 .statfs = simple_statfs,
1639 .delete_inode = btrfs_delete_inode,
1640 .put_super = btrfs_put_super,
1641 .read_inode = btrfs_read_locked_inode,
1642 .write_super = btrfs_write_super,
1643 .sync_fs = btrfs_sync_fs,
1644 .write_inode = btrfs_write_inode,
1645 .alloc_inode = btrfs_alloc_inode,
1646 .destroy_inode = btrfs_destroy_inode,
1647};
1648
1649static struct inode_operations btrfs_dir_inode_operations = {
1650 .lookup = btrfs_lookup,
1651 .create = btrfs_create,
1652 .unlink = btrfs_unlink,
1653 .mkdir = btrfs_mkdir,
1654 .rmdir = btrfs_rmdir,
1655};
1656
1657static struct file_operations btrfs_dir_file_operations = {
1658 .llseek = generic_file_llseek,
1659 .read = generic_read_dir,
1660 .readdir = btrfs_readdir,
1661};
1662
1663static struct address_space_operations btrfs_aops = {
1664 .readpage = btrfs_readpage,
1665 // .readpages = btrfs_readpages,
1666 .writepage = btrfs_writepage,
1667 .sync_page = block_sync_page,
1668 .prepare_write = btrfs_prepare_write,
1669 .commit_write = btrfs_commit_write,
1670};
1671
1672static struct inode_operations btrfs_file_inode_operations = {
1673 .truncate = btrfs_truncate,
1674};
1675
1676static struct file_operations btrfs_file_operations = {
1677 .llseek = generic_file_llseek,
1678 .read = do_sync_read,
1679 .aio_read = generic_file_aio_read,
1680 .write = btrfs_file_inline_write,
1681 .mmap = generic_file_mmap,
1682 .open = generic_file_open,
1683};
1684
1685static int __init init_btrfs_fs(void)
1686{
1687 int err;
1688 printk("btrfs loaded!\n");
1689 err = init_inodecache();
1690 if (err)
1691 return err;
1692 return register_filesystem(&btrfs_fs_type);
1693}
1694
1695static void __exit exit_btrfs_fs(void)
1696{
1697 destroy_inodecache();
1698 unregister_filesystem(&btrfs_fs_type);
1699 printk("btrfs unloaded\n");
1700}
1701
1702module_init(init_btrfs_fs)
1703module_exit(exit_btrfs_fs)
1704
1705MODULE_LICENSE("GPL");
This page took 0.029637 seconds and 5 git commands to generate.