Btrfs: add dir inode index
[deliverable/linux.git] / fs / btrfs / super.c
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
21 static struct inode_operations btrfs_dir_inode_operations;
22 static struct super_operations btrfs_super_ops;
23 static struct file_operations btrfs_dir_file_operations;
24 static struct inode_operations btrfs_file_inode_operations;
25 static struct address_space_operations btrfs_aops;
26 static struct file_operations btrfs_file_operations;
27
28 static 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
36 static 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
106 static 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;
136 err:
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
147 static 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
162 static 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 }
228 out:
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
238 static 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);
270 error:
271 btrfs_release_path(root, path);
272 btrfs_free_path(path);
273 return ret;
274 }
275
276 static 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;
344 error:
345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
347 return ret;
348 }
349
350 static 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;
371 no_delete:
372 clear_inode(inode);
373 }
374
375 static 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);
398 out:
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 check_inode(dir);
402 return ret;
403 }
404
405 static 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
431 static 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_INDEX_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_INDEX_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;
498 err:
499 btrfs_release_path(root, path);
500 btrfs_free_path(path);
501 mutex_unlock(&root->fs_info->fs_mutex);
502 return ret;
503 }
504
505 static 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
517 static 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
559 static 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
578 static 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]);
603 failed:
604 btrfs_release_path(root, path);
605 btrfs_free_path(path);
606 check_inode(inode);
607 return 0;
608 }
609
610 static 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
628 static 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
670 static 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
688 static 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
702 static 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;
727 out_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
740 static 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);
759 error:
760 return ret;
761 }
762
763 static 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
795 out_fail:
796 btrfs_end_transaction(trans, root);
797 out_unlock:
798 mutex_unlock(&root->fs_info->fs_mutex);
799 if (drop_on_err)
800 iput(inode);
801 return err;
802 }
803
804 static 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);
822 printk("btrfs sync_fs\n");
823 mutex_unlock(&root->fs_info->fs_mutex);
824 return 0;
825 }
826
827 #if 0
828 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
829 struct buffer_head *result, int create)
830 {
831 struct btrfs_root *root = btrfs_sb(inode->i_sb);
832 struct btrfs_path *path;
833 struct btrfs_key key;
834 struct btrfs_leaf *leaf;
835 int num_bytes = result->b_size;
836 int item_size;
837 int ret;
838 u64 pos;
839 char *ptr;
840 int copy_size;
841 int err = 0;
842 char *safe_ptr;
843 char *data_ptr;
844
845 path = btrfs_alloc_path();
846 BUG_ON(!path);
847
848 WARN_ON(create);
849 if (create) {
850 return 0;
851 }
852 pos = iblock << inode->i_blkbits;
853 key.objectid = inode->i_ino;
854 key.flags = 0;
855 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
856 ptr = kmap(result->b_page);
857 safe_ptr = ptr;
858 ptr += (pos & (PAGE_CACHE_SIZE -1));
859 again:
860 key.offset = pos;
861 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
862 if (ret) {
863 if (ret < 0)
864 err = ret;
865 else
866 err = 0;
867 goto out;
868 }
869 leaf = btrfs_buffer_leaf(path->nodes[0]);
870 item_size = btrfs_item_size(leaf->items + path->slots[0]);
871 copy_size = min(num_bytes, item_size);
872 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
873 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
874 memcpy(ptr, data_ptr, copy_size);
875 pos += copy_size;
876 num_bytes -= copy_size;
877 WARN_ON(num_bytes < 0);
878 ptr += copy_size;
879 btrfs_release_path(root, path);
880 if (num_bytes != 0) {
881 if (pos >= i_size_read(inode))
882 memset(ptr, 0, num_bytes);
883 else
884 goto again;
885 }
886 set_buffer_uptodate(result);
887 map_bh(result, inode->i_sb, 0);
888 err = 0;
889 out:
890 btrfs_free_path(path);
891 kunmap(result->b_page);
892 return err;
893 }
894 #endif
895
896 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
897 struct buffer_head *result, int create)
898 {
899 int ret;
900 int err = 0;
901 u64 blocknr;
902 u64 extent_start = 0;
903 u64 extent_end = 0;
904 u64 objectid = inode->i_ino;
905 struct btrfs_path *path;
906 struct btrfs_root *root = btrfs_sb(inode->i_sb);
907 struct btrfs_trans_handle *trans = NULL;
908 struct btrfs_file_extent_item *item;
909 struct btrfs_leaf *leaf;
910 struct btrfs_disk_key *found_key;
911
912 path = btrfs_alloc_path();
913 BUG_ON(!path);
914 btrfs_init_path(path);
915 if (create)
916 trans = btrfs_start_transaction(root, 1);
917
918
919 ret = btrfs_lookup_file_extent(trans, root, path,
920 inode->i_ino,
921 iblock << inode->i_blkbits, 0);
922 if (ret < 0) {
923 err = ret;
924 goto out;
925 }
926
927 if (ret != 0) {
928 if (path->slots[0] == 0) {
929 btrfs_release_path(root, path);
930 goto allocate;
931 }
932 path->slots[0]--;
933 }
934
935 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
936 struct btrfs_file_extent_item);
937 leaf = btrfs_buffer_leaf(path->nodes[0]);
938 blocknr = btrfs_file_extent_disk_blocknr(item);
939 blocknr += btrfs_file_extent_offset(item);
940
941 /* exact match found, use it */
942 if (ret == 0) {
943 err = 0;
944 map_bh(result, inode->i_sb, blocknr);
945 goto out;
946 }
947
948 /* are we inside the extent that was found? */
949 found_key = &leaf->items[path->slots[0]].key;
950 if (btrfs_disk_key_objectid(found_key) != objectid ||
951 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
952 extent_end = 0;
953 extent_start = 0;
954 btrfs_release_path(root, path);
955 goto allocate;
956 }
957
958 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
959 extent_start = extent_start >> inode->i_blkbits;
960 extent_start += btrfs_file_extent_offset(item);
961 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
962 if (iblock >= extent_start && iblock < extent_end) {
963 err = 0;
964 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
965 goto out;
966 }
967 allocate:
968 /* ok, create a new extent */
969 if (!create) {
970 err = 0;
971 goto out;
972 }
973 ret = btrfs_alloc_file_extent(trans, root, objectid,
974 iblock << inode->i_blkbits,
975 1, extent_end, &blocknr);
976 if (ret) {
977 err = ret;
978 goto out;
979 }
980 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
981 set_buffer_new(result);
982 map_bh(result, inode->i_sb, blocknr);
983
984 out:
985 btrfs_release_path(root, path);
986 btrfs_free_path(path);
987 if (trans)
988 btrfs_end_transaction(trans, root);
989 return err;
990 }
991
992 static int btrfs_get_block(struct inode *inode, sector_t iblock,
993 struct buffer_head *result, int create)
994 {
995 int err;
996 struct btrfs_root *root = btrfs_sb(inode->i_sb);
997 mutex_lock(&root->fs_info->fs_mutex);
998 err = btrfs_get_block_lock(inode, iblock, result, create);
999 // err = btrfs_get_block_inline(inode, iblock, result, create);
1000 mutex_unlock(&root->fs_info->fs_mutex);
1001 return err;
1002 }
1003
1004 static int btrfs_prepare_write(struct file *file, struct page *page,
1005 unsigned from, unsigned to)
1006 {
1007 return nobh_prepare_write(page, from, to, btrfs_get_block);
1008 }
1009 static int btrfs_commit_write(struct file *file, struct page *page,
1010 unsigned from, unsigned to)
1011 {
1012 return nobh_commit_write(file, page, from, to);
1013 }
1014
1015 static void btrfs_write_super(struct super_block *sb)
1016 {
1017 btrfs_sync_fs(sb, 1);
1018 }
1019
1020 static int btrfs_readpage(struct file *file, struct page *page)
1021 {
1022 return mpage_readpage(page, btrfs_get_block);
1023 }
1024
1025 static 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
1031 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1032 {
1033 return nobh_writepage(page, btrfs_get_block, wbc);
1034 }
1035
1036 static 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
1060 static 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
1090 static 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 }
1101 static 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 }
1142 failed:
1143 return err;
1144 }
1145
1146 static 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
1186 failed_release:
1187 btrfs_drop_pages(pages, num_pages);
1188 return err;
1189
1190 failed_truncate:
1191 btrfs_drop_pages(pages, num_pages);
1192 if (pos > isize)
1193 vmtruncate(inode, isize);
1194 return err;
1195 }
1196
1197 static 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);
1250 out:
1251 *ppos = pos;
1252 current->backing_dev_info = NULL;
1253 return num_written ? num_written : err;
1254 }
1255
1256 #if 0
1257 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1258 struct page *page, loff_t pos,
1259 size_t offset, size_t write_bytes)
1260 {
1261 struct btrfs_path *path;
1262 struct btrfs_trans_handle *trans;
1263 struct btrfs_key key;
1264 struct btrfs_leaf *leaf;
1265 struct btrfs_key found_key;
1266 int ret;
1267 size_t copy_size = 0;
1268 char *dst = NULL;
1269 int err = 0;
1270 size_t num_written = 0;
1271
1272 path = btrfs_alloc_path();
1273 BUG_ON(!path);
1274 mutex_lock(&root->fs_info->fs_mutex);
1275 trans = btrfs_start_transaction(root, 1);
1276 key.objectid = inode->i_ino;
1277 key.flags = 0;
1278 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1279
1280 again:
1281 key.offset = pos;
1282 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1283 if (ret < 0) {
1284 err = ret;
1285 goto out;
1286 }
1287 if (ret == 0) {
1288 leaf = btrfs_buffer_leaf(path->nodes[0]);
1289 btrfs_disk_key_to_cpu(&found_key,
1290 &leaf->items[path->slots[0]].key);
1291 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1292 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1293 copy_size = min(write_bytes, copy_size);
1294 goto copyit;
1295 } else {
1296 int slot = path->slots[0];
1297 if (slot > 0) {
1298 slot--;
1299 }
1300 // FIXME find max key
1301 leaf = btrfs_buffer_leaf(path->nodes[0]);
1302 btrfs_disk_key_to_cpu(&found_key,
1303 &leaf->items[slot].key);
1304 if (found_key.objectid != inode->i_ino)
1305 goto insert;
1306 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1307 goto insert;
1308 copy_size = btrfs_item_size(leaf->items + slot);
1309 if (found_key.offset + copy_size <= pos)
1310 goto insert;
1311 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1312 dst += pos - found_key.offset;
1313 copy_size = copy_size - (pos - found_key.offset);
1314 BUG_ON(copy_size < 0);
1315 copy_size = min(write_bytes, copy_size);
1316 WARN_ON(copy_size == 0);
1317 goto copyit;
1318 }
1319 insert:
1320 btrfs_release_path(root, path);
1321 copy_size = min(write_bytes,
1322 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1323 sizeof(struct btrfs_item) * 4);
1324 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1325 BUG_ON(ret);
1326 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1327 path->slots[0], char);
1328 copyit:
1329 WARN_ON(copy_size == 0);
1330 WARN_ON(dst + copy_size >
1331 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1332 path->slots[0], char) +
1333 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1334 path->slots[0]));
1335 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1336 page_address(page) + offset, copy_size);
1337 mark_buffer_dirty(path->nodes[0]);
1338 btrfs_release_path(root, path);
1339 pos += copy_size;
1340 offset += copy_size;
1341 num_written += copy_size;
1342 write_bytes -= copy_size;
1343 if (write_bytes)
1344 goto again;
1345 out:
1346 btrfs_free_path(path);
1347 ret = btrfs_end_transaction(trans, root);
1348 BUG_ON(ret);
1349 mutex_unlock(&root->fs_info->fs_mutex);
1350 return num_written ? num_written : err;
1351 }
1352
1353 static ssize_t btrfs_file_inline_write(struct file *file,
1354 const char __user *buf,
1355 size_t count, loff_t *ppos)
1356 {
1357 loff_t pos;
1358 size_t num_written = 0;
1359 int err = 0;
1360 int ret = 0;
1361 struct inode *inode = file->f_path.dentry->d_inode;
1362 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1363 unsigned long page_index;
1364
1365 if (file->f_flags & O_DIRECT)
1366 return -EINVAL;
1367 pos = *ppos;
1368
1369 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1370 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1371 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1372 if (err)
1373 goto out;
1374 if (count == 0)
1375 goto out;
1376 err = remove_suid(file->f_path.dentry);
1377 if (err)
1378 goto out;
1379 file_update_time(file);
1380 mutex_lock(&inode->i_mutex);
1381 while(count > 0) {
1382 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1383 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1384 struct page *page;
1385
1386 page_index = pos >> PAGE_CACHE_SHIFT;
1387 page = grab_cache_page(inode->i_mapping, page_index);
1388 if (!PageUptodate(page)) {
1389 ret = mpage_readpage(page, btrfs_get_block);
1390 BUG_ON(ret);
1391 lock_page(page);
1392 }
1393 ret = btrfs_copy_from_user(pos, 1,
1394 write_bytes, &page, buf);
1395 BUG_ON(ret);
1396 write_bytes = inline_one_page(root, inode, page, pos,
1397 offset, write_bytes);
1398 SetPageUptodate(page);
1399 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1400 i_size_write(inode, pos + write_bytes);
1401 mark_inode_dirty(inode);
1402 }
1403 page_cache_release(page);
1404 unlock_page(page);
1405 if (write_bytes < 0)
1406 goto out_unlock;
1407 buf += write_bytes;
1408 count -= write_bytes;
1409 pos += write_bytes;
1410 num_written += write_bytes;
1411
1412 balance_dirty_pages_ratelimited(inode->i_mapping);
1413 cond_resched();
1414 }
1415 out_unlock:
1416 mutex_unlock(&inode->i_mutex);
1417 out:
1418 *ppos = pos;
1419 current->backing_dev_info = NULL;
1420 return num_written ? num_written : err;
1421 }
1422 #endif
1423
1424 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1425 unsigned long offset, unsigned long size)
1426 {
1427 char *kaddr;
1428 unsigned long left, count = desc->count;
1429
1430 if (size > count)
1431 size = count;
1432
1433 if (!PageChecked(page)) {
1434 /* FIXME, do it per block */
1435 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1436 int ret = btrfs_csum_verify_file_block(root,
1437 page->mapping->host->i_ino,
1438 page->index << PAGE_CACHE_SHIFT,
1439 kmap(page), PAGE_CACHE_SIZE);
1440 if (ret) {
1441 printk("failed to verify ino %lu page %lu\n",
1442 page->mapping->host->i_ino,
1443 page->index);
1444 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1445 }
1446 SetPageChecked(page);
1447 kunmap(page);
1448 }
1449 /*
1450 * Faults on the destination of a read are common, so do it before
1451 * taking the kmap.
1452 */
1453 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1454 kaddr = kmap_atomic(page, KM_USER0);
1455 left = __copy_to_user_inatomic(desc->arg.buf,
1456 kaddr + offset, size);
1457 kunmap_atomic(kaddr, KM_USER0);
1458 if (left == 0)
1459 goto success;
1460 }
1461
1462 /* Do it the slow way */
1463 kaddr = kmap(page);
1464 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1465 kunmap(page);
1466
1467 if (left) {
1468 size -= left;
1469 desc->error = -EFAULT;
1470 }
1471 success:
1472 desc->count = count - size;
1473 desc->written += size;
1474 desc->arg.buf += size;
1475 return size;
1476 }
1477
1478 /**
1479 * btrfs_file_aio_read - filesystem read routine
1480 * @iocb: kernel I/O control block
1481 * @iov: io vector request
1482 * @nr_segs: number of segments in the iovec
1483 * @pos: current file position
1484 */
1485 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1486 unsigned long nr_segs, loff_t pos)
1487 {
1488 struct file *filp = iocb->ki_filp;
1489 ssize_t retval;
1490 unsigned long seg;
1491 size_t count;
1492 loff_t *ppos = &iocb->ki_pos;
1493
1494 count = 0;
1495 for (seg = 0; seg < nr_segs; seg++) {
1496 const struct iovec *iv = &iov[seg];
1497
1498 /*
1499 * If any segment has a negative length, or the cumulative
1500 * length ever wraps negative then return -EINVAL.
1501 */
1502 count += iv->iov_len;
1503 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1504 return -EINVAL;
1505 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1506 continue;
1507 if (seg == 0)
1508 return -EFAULT;
1509 nr_segs = seg;
1510 count -= iv->iov_len; /* This segment is no good */
1511 break;
1512 }
1513 retval = 0;
1514 if (count) {
1515 for (seg = 0; seg < nr_segs; seg++) {
1516 read_descriptor_t desc;
1517
1518 desc.written = 0;
1519 desc.arg.buf = iov[seg].iov_base;
1520 desc.count = iov[seg].iov_len;
1521 if (desc.count == 0)
1522 continue;
1523 desc.error = 0;
1524 do_generic_file_read(filp, ppos, &desc,
1525 btrfs_read_actor);
1526 retval += desc.written;
1527 if (desc.error) {
1528 retval = retval ?: desc.error;
1529 break;
1530 }
1531 }
1532 }
1533 return retval;
1534 }
1535
1536 static struct kmem_cache *btrfs_inode_cachep;
1537 struct kmem_cache *btrfs_trans_handle_cachep;
1538 struct kmem_cache *btrfs_transaction_cachep;
1539 struct kmem_cache *btrfs_bit_radix_cachep;
1540 struct kmem_cache *btrfs_path_cachep;
1541
1542 /*
1543 * Called inside transaction, so use GFP_NOFS
1544 */
1545 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1546 {
1547 struct btrfs_inode *ei;
1548
1549 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1550 if (!ei)
1551 return NULL;
1552 ei->magic = 0xDEADBEEF;
1553 ei->magic2 = 0xDEADBEAF;
1554 return &ei->vfs_inode;
1555 }
1556
1557 static void btrfs_destroy_inode(struct inode *inode)
1558 {
1559 struct btrfs_inode *ei = BTRFS_I(inode);
1560 WARN_ON(ei->magic != 0xDEADBEEF);
1561 WARN_ON(ei->magic2 != 0xDEADBEAF);
1562 WARN_ON(!list_empty(&inode->i_dentry));
1563 WARN_ON(inode->i_data.nrpages);
1564
1565 ei->magic = 0;
1566 ei->magic2 = 0;
1567 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1568 }
1569
1570 static void init_once(void * foo, struct kmem_cache * cachep,
1571 unsigned long flags)
1572 {
1573 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1574
1575 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1576 SLAB_CTOR_CONSTRUCTOR) {
1577 inode_init_once(&ei->vfs_inode);
1578 }
1579 }
1580
1581 static int init_inodecache(void)
1582 {
1583 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1584 sizeof(struct btrfs_inode),
1585 0, (SLAB_RECLAIM_ACCOUNT|
1586 SLAB_MEM_SPREAD),
1587 init_once, NULL);
1588 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1589 sizeof(struct btrfs_trans_handle),
1590 0, (SLAB_RECLAIM_ACCOUNT|
1591 SLAB_MEM_SPREAD),
1592 NULL, NULL);
1593 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1594 sizeof(struct btrfs_transaction),
1595 0, (SLAB_RECLAIM_ACCOUNT|
1596 SLAB_MEM_SPREAD),
1597 NULL, NULL);
1598 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1599 sizeof(struct btrfs_transaction),
1600 0, (SLAB_RECLAIM_ACCOUNT|
1601 SLAB_MEM_SPREAD),
1602 NULL, NULL);
1603 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1604 256,
1605 0, (SLAB_RECLAIM_ACCOUNT|
1606 SLAB_MEM_SPREAD |
1607 SLAB_DESTROY_BY_RCU),
1608 NULL, NULL);
1609 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1610 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1611 return -ENOMEM;
1612 return 0;
1613 }
1614
1615 static void destroy_inodecache(void)
1616 {
1617 kmem_cache_destroy(btrfs_inode_cachep);
1618 kmem_cache_destroy(btrfs_trans_handle_cachep);
1619 kmem_cache_destroy(btrfs_transaction_cachep);
1620 kmem_cache_destroy(btrfs_bit_radix_cachep);
1621 kmem_cache_destroy(btrfs_path_cachep);
1622 }
1623
1624 static int btrfs_get_sb(struct file_system_type *fs_type,
1625 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1626 {
1627 return get_sb_bdev(fs_type, flags, dev_name, data,
1628 btrfs_fill_super, mnt);
1629 }
1630
1631 static struct file_system_type btrfs_fs_type = {
1632 .owner = THIS_MODULE,
1633 .name = "btrfs",
1634 .get_sb = btrfs_get_sb,
1635 .kill_sb = kill_block_super,
1636 .fs_flags = FS_REQUIRES_DEV,
1637 };
1638
1639 static struct super_operations btrfs_super_ops = {
1640 .statfs = simple_statfs,
1641 .delete_inode = btrfs_delete_inode,
1642 .put_super = btrfs_put_super,
1643 .read_inode = btrfs_read_locked_inode,
1644 .write_super = btrfs_write_super,
1645 .sync_fs = btrfs_sync_fs,
1646 .write_inode = btrfs_write_inode,
1647 .alloc_inode = btrfs_alloc_inode,
1648 .destroy_inode = btrfs_destroy_inode,
1649 };
1650
1651 static struct inode_operations btrfs_dir_inode_operations = {
1652 .lookup = btrfs_lookup,
1653 .create = btrfs_create,
1654 .unlink = btrfs_unlink,
1655 .mkdir = btrfs_mkdir,
1656 .rmdir = btrfs_rmdir,
1657 };
1658
1659 static struct file_operations btrfs_dir_file_operations = {
1660 .llseek = generic_file_llseek,
1661 .read = generic_read_dir,
1662 .readdir = btrfs_readdir,
1663 };
1664
1665 static struct address_space_operations btrfs_aops = {
1666 .readpage = btrfs_readpage,
1667 .readpages = btrfs_readpages,
1668 .writepage = btrfs_writepage,
1669 .sync_page = block_sync_page,
1670 .prepare_write = btrfs_prepare_write,
1671 .commit_write = btrfs_commit_write,
1672 };
1673
1674 static struct inode_operations btrfs_file_inode_operations = {
1675 .truncate = btrfs_truncate,
1676 };
1677
1678 static struct file_operations btrfs_file_operations = {
1679 .llseek = generic_file_llseek,
1680 .read = do_sync_read,
1681 .aio_read = btrfs_file_aio_read,
1682 .write = btrfs_file_write,
1683 .mmap = generic_file_mmap,
1684 .open = generic_file_open,
1685 };
1686
1687 static int __init init_btrfs_fs(void)
1688 {
1689 int err;
1690 printk("btrfs loaded!\n");
1691 err = init_inodecache();
1692 if (err)
1693 return err;
1694 return register_filesystem(&btrfs_fs_type);
1695 }
1696
1697 static void __exit exit_btrfs_fs(void)
1698 {
1699 destroy_inodecache();
1700 unregister_filesystem(&btrfs_fs_type);
1701 printk("btrfs unloaded\n");
1702 }
1703
1704 module_init(init_btrfs_fs)
1705 module_exit(exit_btrfs_fs)
1706
1707 MODULE_LICENSE("GPL");
This page took 0.114992 seconds and 6 git commands to generate.